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

Print cumulative stats with -Q SECS,cumulative #315

Merged
merged 1 commit into from
May 8, 2024
Merged
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
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
Loading