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

Test case for "fping: add option to exit immediately once N hosts have been found" with copied code #260

Merged
merged 2 commits into from
Aug 27, 2023
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
20 changes: 19 additions & 1 deletion ci/test-10-option-u-x.pl
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/perl -w

use Test::Command tests => 12;
use Test::Command tests => 18;

# -u show targets that are unreachable
# -v show version
# -x shows if >=N hosts are reachable or not
# -X exits true immediately when N hosts are found

# fping -u
{
Expand Down Expand Up @@ -36,3 +38,19 @@
$cmd->stdout_is_eq("Not enough hosts reachable (required: 2, reachable: 1)\n");
$cmd->stderr_is_eq("");
}

# fping -X
{
my $cmd = Test::Command->new(cmd => "fping -X 1 --generate 127.0.0.0/29");
$cmd->exit_is_num(0);
$cmd->stdout_is_eq("Enough hosts reachable (required: 1, reachable: 1)\n");
$cmd->stderr_is_eq("");
}

# fping -X
{
my $cmd = Test::Command->new(cmd => "fping -X 2 --generate 8.8.0.0/29");
$cmd->exit_is_num(1);
$cmd->stdout_is_eq("Not enough hosts reachable (required: 2, reachable: 0)\n");
$cmd->stderr_is_eq("");
}
5 changes: 5 additions & 0 deletions doc/fping.pod
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ Print B<fping> version information.
Given a list of hosts, this mode checks if number of reachable hosts is >= N
and exits true in that case.

=item B<-X>, B<--fast-reachable>=I<N>

Given a list of hosts, this mode immediately exits true once N alive hosts
have been found.

=back

=head1 EXAMPLES
Expand Down
13 changes: 12 additions & 1 deletion src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ int generate_flag = 0; /* flag for IP list generation */
int verbose_flag, quiet_flag, stats_flag, unreachable_flag, alive_flag;
int elapsed_flag, version_flag, count_flag, loop_flag, netdata_flag;
int per_recv_flag, report_all_rtts_flag, name_flag, addr_flag, backoff_flag, rdns_flag;
int multif_flag, timeout_flag;
int multif_flag, timeout_flag, fast_reachable;
int outage_flag = 0;
int timestamp_flag = 0;
int random_data_flag = 0;
Expand Down Expand Up @@ -536,6 +536,7 @@ int main(int argc, char** argv)
{ "unreach", 'u', OPTPARSE_NONE },
{ "version", 'v', OPTPARSE_NONE },
{ "reachable", 'x', OPTPARSE_REQUIRED },
{ "fast-reachable", 'X', OPTPARSE_REQUIRED },
#if defined(DEBUG) || defined(_DEBUG)
{ NULL, 'z', OPTPARSE_REQUIRED },
#endif
Expand Down Expand Up @@ -750,6 +751,12 @@ int main(int argc, char** argv)
usage(1);
break;

case 'X':
if (!(min_reachable = (unsigned int)atoi(optparse_state.optarg)))
usage(1);
fast_reachable = 1;
break;

case 'f':
filename = optparse_state.optarg;
break;
Expand Down Expand Up @@ -2406,6 +2413,9 @@ int wait_for_reply(int64_t wait_time)
/* print "is alive" */
if (h->num_recv == 1) {
num_alive++;
if (fast_reachable && num_alive >= min_reachable)
finish_requested = 1;

if (verbose_flag || alive_flag) {
printf("%s", h->host);

Expand Down Expand Up @@ -2942,5 +2952,6 @@ void usage(int is_error)
fprintf(out, " -u, --unreach show targets that are unreachable\n");
fprintf(out, " -v, --version show version\n");
fprintf(out, " -x, --reachable=N shows if >=N hosts are reachable or not\n");
fprintf(out, " -X, --fast-reachable=N exits true immediately when N hosts are found\n");
exit(is_error);
}