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

Adding -x option which allows user to specify a threshold and check if atleast those many hosts are active #138

Merged
merged 6 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
18 changes: 17 additions & 1 deletion ci/test-10-option-u-v.pl → ci/test-10-option-u-x.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/perl -w

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

# -u show targets that are unreachable
# -v show version
Expand All @@ -22,3 +22,19 @@
});
$cmd->stderr_is_eq("");
}

# fping -x
{
my $cmd = Test::Command->new(cmd => "fping -x 1 8.8.0.0 127.0.0.1");
$cmd->exit_is_num(0);
$cmd->stdout_is_eq("Number of reachable hosts: 1\n");
$cmd->stderr_is_eq("");
}

# fping -x
{
my $cmd = Test::Command->new(cmd => "fping -x 2 8.8.0.0 127.0.0.1");
$cmd->exit_is_num(1);
$cmd->stdout_is_eq("<2 hosts are reachable\n");
$cmd->stderr_is_eq("");
}
4 changes: 4 additions & 0 deletions doc/fping.pod
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ Show targets that are unreachable.

Print B<fping> version information.

=item B<-x>, B<--reachable>=I<N>

Given a list of hosts, this mode prints if number of active hosts>=N or not.

=back

=head1 EXAMPLES
Expand Down
28 changes: 24 additions & 4 deletions src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ unsigned int interval = DEFAULT_INTERVAL * 100;
unsigned int perhost_interval = DEFAULT_PERHOST_INTERVAL * 100;
float backoff = DEFAULT_BACKOFF_FACTOR;
unsigned int ping_data_size = DEFAULT_PING_DATA_SIZE;
unsigned int count = 1;
unsigned int count = 1, reachable = 1;
deepak0004 marked this conversation as resolved.
Show resolved Hide resolved
unsigned int trials;
unsigned int report_interval = 0;
unsigned int ttl = 0;
Expand Down Expand Up @@ -285,7 +285,7 @@ struct timezone tz;
/* switches */
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 elapsed_flag, version_flag, count_flag, loop_flag, netdata_flag, reachable_flag;
deepak0004 marked this conversation as resolved.
Show resolved Hide resolved
int per_recv_flag, report_all_rtts_flag, name_flag, addr_flag, backoff_flag, rdns_flag;
int multif_flag, timeout_flag;
int outage_flag = 0;
Expand Down Expand Up @@ -414,6 +414,7 @@ int main(int argc, char** argv)
{ NULL, 'T', OPTPARSE_REQUIRED },
{ "unreach", 'u', OPTPARSE_NONE },
{ "version", 'v', OPTPARSE_NONE },
{ "reachable", 'x', OPTPARSE_REQUIRED },
{ 0, 0, 0 }
};

Expand Down Expand Up @@ -604,6 +605,12 @@ int main(int argc, char** argv)
printf("%s: comments to %s\n", prog, EMAIL);
exit(0);

case 'x':
if (!(reachable = (unsigned int)atoi(optparse_state.optarg)))
usage(1);
reachable_flag = 1;
break;

case 'f':
filename = optparse_state.optarg;
break;
Expand Down Expand Up @@ -732,7 +739,7 @@ int main(int argc, char** argv)
exit(1);
}

if (alive_flag || unreachable_flag)
if (alive_flag || unreachable_flag || reachable_flag)
verbose_flag = 0;

if (count_flag) {
Expand Down Expand Up @@ -812,6 +819,8 @@ int main(int argc, char** argv)
fprintf(stderr, " unreachable_flag set\n");
if (alive_flag)
fprintf(stderr, " alive_flag set\n");
if (reachable_flag)
fprintf(stderr, " reachable_flag set\n");
if (elapsed_flag)
fprintf(stderr, " elapsed_flag set\n");
if (version_flag)
Expand Down Expand Up @@ -1323,6 +1332,16 @@ void finish()
if (stats_flag)
print_global_stats();

if (reachable_flag) {
if ((num_hosts-num_unreachable >= reachable)) {
printf("Number of reachable hosts: %d\n", num_hosts-num_unreachable);
deepak0004 marked this conversation as resolved.
Show resolved Hide resolved
exit(0);
} else {
printf("<%d hosts are reachable\n", reachable);
deepak0004 marked this conversation as resolved.
Show resolved Hide resolved
exit(1);
}
}

if (num_noaddress)
exit(2);
else if (num_alive != num_hosts)
Expand Down Expand Up @@ -2767,5 +2786,6 @@ void usage(int is_error)
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");
fprintf(out, " -x, --reachable=N shows if >=N hosts are reachable or not\n");
exit(is_error);
}
}