Skip to content

Commit

Permalink
Print cumulative stats with -Q SECS,cumulative
Browse files Browse the repository at this point in the history
With -Q SECS, fping emits information for the just finished
interval.  This is good for long-running fping processes where
one would like to get an update on recent results, ignoring
missing responses from older intervals.  This seems appropriate
for use with other software like netdata or smokeping.

But for short(er)-running fping processes, e.g., during a change
window, it may be more interesting to get a status update since
the beginning of the change window, i.e., the start of fping.
Adding a comma followed by the keyword "cumulative" to -Q SECS
(i.e., -Q SECS,cumulative) changes the interim reports to output
the current per system overall statistics, unless -N is used, too.

The new syntax is documented in fping.pod and added to fping -h
output.

Four tests are added:

  1. Characters after SECS are ignored as before.
  2. Unknown keywords are ignored as before.
  2. Adding ",cumulative" changes -Q output.
  3. Adding ",cumulative" also affects the additional -o output.

This addresses issue #243.
  • Loading branch information
auerswal authored and schweikert committed May 8, 2024
1 parent 0d08321 commit 90ad83f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
54 changes: 53 additions & 1 deletion ci/test-08-options-n-q.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/perl -w

use Test::Command tests => 24;
use Test::Command tests => 36;

# -n show targets by name (-d is equivalent)
# -O n set the type of service (tos) flag on the ICMP packets
Expand Down Expand Up @@ -81,6 +81,45 @@
});
}

# fping -Q n ignores non-number characters after the number, except for keywords
{
my $cmd = Test::Command->new(cmd => "fping -Q 1whatever -p 550 -c 5 127.0.0.1");
$cmd->exit_is_num(0);
$cmd->stdout_is_eq("");
$cmd->stderr_like(qr{\[\d+:\d+:\d+\]
127\.0\.0\.1 : xmt/rcv/%loss = 2/2/0%, min/avg/max = \d\.\d+/\d\.\d+/\d\.\d+
\[\d+:\d+:\d+\]
127\.0\.0\.1 : xmt/rcv/%loss = 2/2/0%, min/avg/max = \d\.\d+/\d\.\d+/\d\.\d+
127\.0\.0\.1 : xmt/rcv/%loss = 5/5/0%, min/avg/max = \d\.\d+/\d\.\d+/\d\.\d+
});
}

# fping -Q n ignores unknown keywords
{
my $cmd = Test::Command->new(cmd => "fping -Q 1,not_a_keyword -p 550 -c 5 127.0.0.1");
$cmd->exit_is_num(0);
$cmd->stdout_is_eq("");
$cmd->stderr_like(qr{\[\d+:\d+:\d+\]
127\.0\.0\.1 : xmt/rcv/%loss = 2/2/0%, min/avg/max = \d\.\d+/\d\.\d+/\d\.\d+
\[\d+:\d+:\d+\]
127\.0\.0\.1 : xmt/rcv/%loss = 2/2/0%, min/avg/max = \d\.\d+/\d\.\d+/\d\.\d+
127\.0\.0\.1 : xmt/rcv/%loss = 5/5/0%, min/avg/max = \d\.\d+/\d\.\d+/\d\.\d+
});
}

# fping -Q n,cumulative
{
my $cmd = Test::Command->new(cmd => "fping -Q 1,cumulative -p 550 -c 5 127.0.0.1");
$cmd->exit_is_num(0);
$cmd->stdout_is_eq("");
$cmd->stderr_like(qr{\[\d+:\d+:\d+\]
127\.0\.0\.1 : xmt/rcv/%loss = 2/2/0%, min/avg/max = \d\.\d+/\d\.\d+/\d\.\d+
\[\d+:\d+:\d+\]
127\.0\.0\.1 : xmt/rcv/%loss = 4/4/0%, min/avg/max = \d\.\d+/\d\.\d+/\d\.\d+
127\.0\.0\.1 : xmt/rcv/%loss = 5/5/0%, min/avg/max = \d\.\d+/\d\.\d+/\d\.\d+
});
}

# fping -Q -o
{
my $cmd = Test::Command->new(cmd => "fping -c4 -Q1 -p550 -o 8.8.8.7");
Expand All @@ -94,3 +133,16 @@
});
}

# fping -Q n,cumulative -o
{
my $cmd = Test::Command->new(cmd => "fping -c4 -Q1,cumulative -p550 -o 8.8.8.7");
$cmd->exit_is_num(1);
$cmd->stdout_is_eq("");
$cmd->stderr_like(qr{\[\d+:\d+:\d+\]
8\.8\.8\.7 : xmt/rcv/%loss = 1/0/100%, outage\(ms\) = 55\d
\[\d+:\d+:\d+\]
8\.8\.8\.7 : xmt/rcv/%loss = 3/0/100%, outage\(ms\) = 165\d
8\.8\.8\.7 : xmt/rcv/%loss = 4/0/100%, outage\(ms\) = 220\d
});
}

5 changes: 3 additions & 2 deletions doc/fping.pod
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,11 @@ an individual target. Default is 1000 and minimum is 10.
Quiet. Don't show per-probe results, but only the final summary. Also don't
show ICMP error messages.

=item B<-Q>, B<--squiet>=I<SECS>
=item B<-Q>, B<--squiet>=I<SECS[,cumulative]>

Like B<-q>, but additionally show interval summary results every I<SECS>
seconds.
seconds. With I<cumulative>, show summary results since start instead of
for the last interval, unless option B<-N> is used, too.

=item B<-r>, B<--retry>=I<N>

Expand Down
16 changes: 14 additions & 2 deletions src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ int multif_flag, timeout_flag, fast_reachable;
int outage_flag = 0;
int timestamp_flag = 0;
int random_data_flag = 0;
int cumulative_stats_flag = 0;
#if defined(DEBUG) || defined(_DEBUG)
int randomly_lose_flag, trace_flag, print_per_system_flag;
int lose_factor;
Expand Down Expand Up @@ -677,6 +678,14 @@ int main(int argc, char **argv)
}
report_interval = opt_value_float * 1e9;

/* recognize keyword(s) after number, ignore everything else */
{
char *comma = strchr(optparse_state.optarg, ',');
if ((comma != NULL) && (strcmp(++comma, "cumulative") == 0)) {
cumulative_stats_flag = 1;
}
}

break;

case 'e':
Expand Down Expand Up @@ -1813,7 +1822,9 @@ void print_per_system_splits(void)
}

fprintf(stderr, "\n");
stats_reset_interval(h);
if (!cumulative_stats_flag) {
stats_reset_interval(h);
}
}
}

Expand Down Expand Up @@ -2986,7 +2997,8 @@ void usage(int is_error)
fprintf(out, " -N, --netdata output compatible for netdata (-l -Q are required)\n");
fprintf(out, " -o, --outage show the accumulated outage time (lost packets * packet interval)\n");
fprintf(out, " -q, --quiet quiet (don't show per-target/per-ping results)\n");
fprintf(out, " -Q, --squiet=SECS same as -q, but add interval summary every SECS seconds\n");
fprintf(out, " -Q, --squiet=SECS[,cumulative] same as -q, but add interval summary every SECS seconds,\n");
fprintf(out, " with 'cumulative', print stats since beginning\n");
fprintf(out, " -s, --stats print final stats\n");
fprintf(out, " -u, --unreach show targets that are unreachable\n");
fprintf(out, " -v, --version show version\n");
Expand Down

0 comments on commit 90ad83f

Please sign in to comment.