From ba632b79c58e14d4b90b8a9263f2256d221c449a Mon Sep 17 00:00:00 2001 From: German Service Network Date: Wed, 30 Apr 2025 19:19:25 +0200 Subject: [PATCH] Add JSON support for --timestamp-format --- ci/test-16-json-output.pl | 33 ++++++++++++++++++++++++++++++--- src/fping.c | 38 ++++++++++++++++++++++---------------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/ci/test-16-json-output.pl b/ci/test-16-json-output.pl index 1fd2191..84fa3a1 100644 --- a/ci/test-16-json-output.pl +++ b/ci/test-16-json-output.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl -w -use Test::Command tests => 54; +use Test::Command tests => 63; use Test::More; # fping -J -c 2 127.0.0.1 @@ -65,8 +65,35 @@ $cmd->stderr_is_eq(""); { my $cmd = Test::Command->new(cmd => "fping -J -c 2 -D 127.0.0.1"); $cmd->exit_is_num(0); -$cmd->stdout_like(qr/^\{"resp":\s\{"timestamp":\s\d+.\d+,\s"host":\s"127\.0\.0\.1",\s"seq":\s0,\s"size":\s\d+,\s"rtt":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"loss":\s\d+\}\} -\{"resp":\s\{"timestamp":\s\d+.\d+,\s"host":\s"127\.0\.0\.1",\s"seq":\s1,\s"size":\s\d+,\s"rtt":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"loss":\s\d+\}\} +$cmd->stdout_like(qr/^\{"resp":\s\{"timestamp":\s"\d+.\d+",\s"host":\s"127\.0\.0\.1",\s"seq":\s0,\s"size":\s\d+,\s"rtt":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"loss":\s\d+\}\} +\{"resp":\s\{"timestamp":\s"\d+.\d+",\s"host":\s"127\.0\.0\.1",\s"seq":\s1,\s"size":\s\d+,\s"rtt":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"loss":\s\d+\}\} +\{"summary":\s\{"host":\s"127\.0\.0\.1",\s"xmt":\s\d+,\s"rcv":\s\d+,\s"loss":\s\d+,\s"rttMin":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"rttMax":\s\d+\.\d+\}\}\n?$/); +$cmd->stderr_is_eq(""); +} + +# fping -J -c 1 -D --timestamp-format=ctime 127.0.0.1 +{ +my $cmd = Test::Command->new(cmd => "fping -J -c 1 -D --timestamp-format=ctime 127.0.0.1"); +$cmd->exit_is_num(0); +$cmd->stdout_like(qr/^\{"resp":\s\{"timestamp":\s"\w+\s\w+\s+\d+\s[\d+:]+\s\d+",\s"host":\s"127\.0\.0\.1",\s"seq":\s0,\s"size":\s\d+,\s"rtt":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"loss":\s\d+\}\} +\{"summary":\s\{"host":\s"127\.0\.0\.1",\s"xmt":\s\d+,\s"rcv":\s\d+,\s"loss":\s\d+,\s"rttMin":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"rttMax":\s\d+\.\d+\}\}\n?$/); +$cmd->stderr_is_eq(""); +} + +# fping -J -c 1 -D --timestamp-format=iso 127.0.0.1 +{ +my $cmd = Test::Command->new(cmd => "fping -J -c 1 -D --timestamp-format=iso 127.0.0.1"); +$cmd->exit_is_num(0); +$cmd->stdout_like(qr/^\{"resp":\s\{"timestamp":\s"[\d+-]+T[\d+:]+\+\d+",\s"host":\s"127\.0\.0\.1",\s"seq":\s0,\s"size":\s\d+,\s"rtt":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"loss":\s\d+\}\} +\{"summary":\s\{"host":\s"127\.0\.0\.1",\s"xmt":\s\d+,\s"rcv":\s\d+,\s"loss":\s\d+,\s"rttMin":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"rttMax":\s\d+\.\d+\}\}\n?$/); +$cmd->stderr_is_eq(""); +} + +# fping -J -c 1 -D --timestamp-format=rfc3339 127.0.0.1 +{ +my $cmd = Test::Command->new(cmd => "fping -J -c 1 -D --timestamp-format=rfc3339 127.0.0.1"); +$cmd->exit_is_num(0); +$cmd->stdout_like(qr/^\{"resp":\s\{"timestamp":\s"[\d+-]+\s[\d+:]+",\s"host":\s"127\.0\.0\.1",\s"seq":\s0,\s"size":\s\d+,\s"rtt":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"loss":\s\d+\}\} \{"summary":\s\{"host":\s"127\.0\.0\.1",\s"xmt":\s\d+,\s"rcv":\s\d+,\s"loss":\s\d+,\s"rttMin":\s\d+\.\d+,\s"rttAvg":\s\d+\.\d+,\s"rttMax":\s\d+\.\d+\}\}\n?$/); $cmd->stderr_is_eq(""); } diff --git a/src/fping.c b/src/fping.c index 4595fa8..47f743e 100644 --- a/src/fping.c +++ b/src/fping.c @@ -1715,12 +1715,9 @@ void main_loop() if (json_flag) printf("{\"timeout\": {"); - if (timestamp_flag) { - if (json_flag) - printf("\"timestamp\": %.5f, ", (double)current_time_ns / 1e9); - else - print_timestamp_format(current_time_ns, timestamp_format_flag); - } + if (timestamp_flag) + print_timestamp_format(current_time_ns, timestamp_format_flag); + if (json_flag) { printf("\"host\": \"%s\", ", h->host); @@ -3113,12 +3110,9 @@ int wait_for_reply(int64_t wait_time) if (json_flag) printf("{\"resp\": {"); - if (timestamp_flag) { - if (json_flag) - printf("\"timestamp\": %.5f, ", (double)recv_time / 1e9); - else - print_timestamp_format(recv_time, timestamp_format_flag); - } + if (timestamp_flag) + print_timestamp_format(recv_time, timestamp_format_flag); + avg = h->total_time / h->num_recv; if (json_flag) { printf("\"host\": \"%s\", ", h->host); @@ -3659,20 +3653,32 @@ void print_timestamp_format(int64_t current_time_ns, int timestamp_format) case 1: // timestamp-format ctime strftime(time_buffer, sizeof(time_buffer), "%c", local_time); - printf("[%s] ", time_buffer); + if (json_flag) + printf("\"timestamp\": \"%s\", ", time_buffer); + else + printf("[%s] ", time_buffer); break; case 2: // timestamp-format iso strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%dT%T%z", local_time); - printf("[%s] ", time_buffer); + if (json_flag) + printf("\"timestamp\": \"%s\", ", time_buffer); + else + printf("[%s] ", time_buffer); break; case 3: // timestamp-format rfc3339 strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", local_time); - printf("[%s] ", time_buffer); + if (json_flag) + printf("\"timestamp\": \"%s\", ", time_buffer); + else + printf("[%s] ", time_buffer); break; default: - printf("[%.5f] ", (double)current_time_ns / 1e9); + if (json_flag) + printf("\"timestamp\": \"%.5f\", ", (double)current_time_ns / 1e9); + else + printf("[%.5f] ", (double)current_time_ns / 1e9); } } -- 2.43.0