Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON output support with --json/-J option #160

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions ci/test-16-json.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/usr/bin/perl -w

use Test::Command;
use Test::More;

plan tests => 18;

# summary
{
my $cmd = Test::Command->new(cmd => "fping -c 2 -J 127.0.0.1");
$cmd->exit_is_num(0);
$cmd->stdout_like(qr{\{
"hosts": \{
"127\.0\.0\.1": \{
"xmt": 2,
"rcv": 2,
"loss_percentage": 0,
"min": \d.\d+,
"avg": \d.\d+,
"max": \d.\d+
\}
\}
\}}
);
$cmd->stderr_is_eq("");
}

# all RTTs
{
my $cmd = Test::Command->new(cmd => "fping -C 2 -J 127.0.0.1");
$cmd->exit_is_num(0);
$cmd->stdout_like(qr{\{
"hosts": \{
"127\.0\.0\.1": \[
\d.\d+,
\d.\d+
\]
\}
\}}
);
$cmd->stderr_is_eq("");
}

# summary with stats and outage
{
my $cmd = Test::Command->new(cmd => "fping -c 2 -s -o -J 127.0.0.1");
$cmd->exit_is_num(0);
$cmd->stdout_like(qr{\{
"hosts": \{
"127\.0\.0\.1": \{
"xmt": 2,
"rcv": 2,
"loss_percentage": 0,
"outage": 0,
"min": \d.\d+,
"avg": \d.\d+,
"max": \d.\d+
\}
\},
"stats": \{
"targets": 1,
"alive": 1,
"unreachable": 0,
"unknown_addresses": 0,
"timeouts": 0,
"icmp_echos_sent": 2,
"icmp_echo_replies_received": 2,
"other_icmp_received": 0,
"min_rtt": \d.\d+,
"avg_rtt": \d.\d+,
"max_rtt": \d.\d+,
"elapsed_real_time": \d.\d+
\}
\}}
);
$cmd->stderr_is_eq("");
}

# all RTTs with stats
{
my $cmd = Test::Command->new(cmd => "fping -C 2 -s -J 127.0.0.1");
$cmd->exit_is_num(0);
$cmd->stdout_like(qr{\{
"hosts": \{
"127\.0\.0\.1": \[
\d.\d+,
\d.\d+
\]
\},
"stats": \{
"targets": 1,
"alive": 1,
"unreachable": 0,
"unknown_addresses": 0,
"timeouts": 0,
"icmp_echos_sent": 2,
"icmp_echo_replies_received": 2,
"other_icmp_received": 0,
"min_rtt": \d.\d+,
"avg_rtt": \d.\d+,
"max_rtt": \d.\d+,
"elapsed_real_time": \d.\d+
\}
\}}
);
$cmd->stderr_is_eq("");
}

# more indentation
{
my $cmd = Test::Command->new(cmd => "fping -c 2 --json=4 127.0.0.1");
$cmd->exit_is_num(0);
$cmd->stdout_like(qr{\{
"hosts": \{
"127\.0\.0\.1": \{
"xmt": 2,
"rcv": 2,
"loss_percentage": 0,
"min": \d.\d+,
"avg": \d.\d+,
"max": \d.\d+
\}
\}
\}}
);
$cmd->stderr_is_eq("");
}

# no pretty-print
{
my $cmd = Test::Command->new(cmd => "fping -c 2 --json=0 127.0.0.1");
$cmd->exit_is_num(0);
$cmd->stdout_like(qr{\{"hosts":\{"127\.0\.0\.1":\{"xmt":2,"rcv":2,"loss_percentage":0,"min":\d.\d+,"avg":\d.\d+,"max":\d.\d+\}\}\}});
$cmd->stderr_is_eq("");
}
6 changes: 6 additions & 0 deletions doc/fping.pod
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ to any target (default is 10, minimum is 1).

Set the interface (requires SO_BINDTODEVICE support).

=item B<-J>, B<--json>[=I<N>]

Output in JSON format to stdout (implies -q).
Optionally specify JSON indentation (default: 2, max: 16).
Indentation == 0 will disable pretty-printing.

=item B<-l>, B<--loop>

Loop sending packets to each target indefinitely. Can be interrupted with
Expand Down
Loading