From 7338b4b8598d87c97d02ede8b395c0320264122b Mon Sep 17 00:00:00 2001 From: deepak0004 Date: Wed, 17 Oct 2018 23:22:01 +0530 Subject: [PATCH 1/6] Adding -x option which allows user to specify a threshold and check if atleast those many hosts are active --- doc/fping.pod | 4 ++++ src/fping.c | 31 ++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/doc/fping.pod b/doc/fping.pod index af21bad..0af442e 100644 --- a/doc/fping.pod +++ b/doc/fping.pod @@ -225,6 +225,10 @@ Show targets that are unreachable. Print B version information. +=item B<-x>, B<--reachable>=I + +Given a list of hosts, this mode prints if number of active hosts>=N or not. + =back =head1 EXAMPLES diff --git a/src/fping.c b/src/fping.c index a4e45a1..4b40fcd 100644 --- a/src/fping.c +++ b/src/fping.c @@ -239,7 +239,7 @@ int socket6 = -1; int hints_ai_family = AF_UNSPEC; #endif -unsigned int debugging = 0; +unsigned int debugging = 1; /* times get *100 because all times are calculated in 10 usec units, not ms */ unsigned int retry = DEFAULT_RETRY; @@ -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; unsigned int trials; unsigned int report_interval = 0; unsigned int ttl = 0; @@ -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; 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; @@ -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 } }; @@ -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; @@ -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) { @@ -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) @@ -1186,7 +1195,7 @@ void main_loop() h->ev_type = EV_TYPE_PING; h->ev_time.tv_sec = last_send_time.tv_sec; h->ev_time.tv_usec = last_send_time.tv_usec; - timeval_add(&h->ev_time, perhost_interval); + timeval_add(&h->ev_time, perhost_interval); ev_enqueue(h); } /* Count mode: schedule timeout after last ping */ @@ -1272,6 +1281,13 @@ void main_loop() timeval_add(&next_report_time, report_interval); } } + if (reachable_flag) { + if ((num_hosts-num_unreachable >= reachable)) { + printf(">=%d hosts are reachable\n", reachable); + } else { + printf("<%d hosts are reachable\n", reachable); + } + } } /************************************************************ @@ -1888,7 +1904,7 @@ int decode_icmp_ipv4( } *id = ntohs(icp->icmp_id); - *seq = ntohs(icp->icmp_seq); + *seq = ntohs(icp->icmp_seq); return 1; } @@ -2767,5 +2783,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); -} +} \ No newline at end of file From b18e930bed2f760d1cc22f134395b23f404c3aa6 Mon Sep 17 00:00:00 2001 From: deepak0004 Date: Wed, 17 Oct 2018 23:31:14 +0530 Subject: [PATCH 2/6] Making the reqd changes --- src/fping.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fping.c b/src/fping.c index 4b40fcd..8f6aeaa 100644 --- a/src/fping.c +++ b/src/fping.c @@ -239,7 +239,7 @@ int socket6 = -1; int hints_ai_family = AF_UNSPEC; #endif -unsigned int debugging = 1; +unsigned int debugging = 0; /* times get *100 because all times are calculated in 10 usec units, not ms */ unsigned int retry = DEFAULT_RETRY; @@ -1195,7 +1195,7 @@ void main_loop() h->ev_type = EV_TYPE_PING; h->ev_time.tv_sec = last_send_time.tv_sec; h->ev_time.tv_usec = last_send_time.tv_usec; - timeval_add(&h->ev_time, perhost_interval); + timeval_add(&h->ev_time, perhost_interval); ev_enqueue(h); } /* Count mode: schedule timeout after last ping */ From 8e33daed93c610d5c849e780da8b7df2a5905fbb Mon Sep 17 00:00:00 2001 From: deepak0004 Date: Thu, 18 Oct 2018 03:12:13 +0530 Subject: [PATCH 3/6] Adding travis ci tests for more coverage and rectifying a mistake --- ...10-option-u-v.pl => test-10-option-u-x.pl} | 18 +++++++++++++++++- src/fping.c | 19 ++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) rename ci/{test-10-option-u-v.pl => test-10-option-u-x.pl} (54%) diff --git a/ci/test-10-option-u-v.pl b/ci/test-10-option-u-x.pl similarity index 54% rename from ci/test-10-option-u-v.pl rename to ci/test-10-option-u-x.pl index 3e9bcf6..49b9120 100755 --- a/ci/test-10-option-u-v.pl +++ b/ci/test-10-option-u-x.pl @@ -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 @@ -22,3 +22,19 @@ fping: comments to david\@schweikert\.ch }); $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(">=1 hosts are reachable\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(0); +$cmd->stdout_is_eq("<2 hosts are reachable\n"); +$cmd->stderr_is_eq(""); +} diff --git a/src/fping.c b/src/fping.c index 8f6aeaa..9101ee2 100644 --- a/src/fping.c +++ b/src/fping.c @@ -1195,7 +1195,7 @@ void main_loop() h->ev_type = EV_TYPE_PING; h->ev_time.tv_sec = last_send_time.tv_sec; h->ev_time.tv_usec = last_send_time.tv_usec; - timeval_add(&h->ev_time, perhost_interval); + timeval_add(&h->ev_time, perhost_interval); ev_enqueue(h); } /* Count mode: schedule timeout after last ping */ @@ -1281,13 +1281,6 @@ void main_loop() timeval_add(&next_report_time, report_interval); } } - if (reachable_flag) { - if ((num_hosts-num_unreachable >= reachable)) { - printf(">=%d hosts are reachable\n", reachable); - } else { - printf("<%d hosts are reachable\n", reachable); - } - } } /************************************************************ @@ -1329,6 +1322,14 @@ void finish() } } + if (reachable_flag) { + if ((num_hosts-num_unreachable >= reachable)) { + printf(">=%d hosts are reachable\n", reachable); + } else { + printf("<%d hosts are reachable\n", reachable); + } + } + if (count_flag || loop_flag) print_per_system_stats(); #if defined(DEBUG) || defined(_DEBUG) @@ -1904,7 +1905,7 @@ int decode_icmp_ipv4( } *id = ntohs(icp->icmp_id); - *seq = ntohs(icp->icmp_seq); + *seq = ntohs(icp->icmp_seq); return 1; } From 9812557c71fa56dd444bff5152abd9fdcc969c1b Mon Sep 17 00:00:00 2001 From: deepak0004 Date: Thu, 18 Oct 2018 03:28:38 +0530 Subject: [PATCH 4/6] Changing the exit code as per travis ci --- src/fping.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/fping.c b/src/fping.c index 9101ee2..8964215 100644 --- a/src/fping.c +++ b/src/fping.c @@ -1195,7 +1195,7 @@ void main_loop() h->ev_type = EV_TYPE_PING; h->ev_time.tv_sec = last_send_time.tv_sec; h->ev_time.tv_usec = last_send_time.tv_usec; - timeval_add(&h->ev_time, perhost_interval); + timeval_add(&h->ev_time, perhost_interval); ev_enqueue(h); } /* Count mode: schedule timeout after last ping */ @@ -1322,14 +1322,6 @@ void finish() } } - if (reachable_flag) { - if ((num_hosts-num_unreachable >= reachable)) { - printf(">=%d hosts are reachable\n", reachable); - } else { - printf("<%d hosts are reachable\n", reachable); - } - } - if (count_flag || loop_flag) print_per_system_stats(); #if defined(DEBUG) || defined(_DEBUG) @@ -1340,6 +1332,15 @@ void finish() if (stats_flag) print_global_stats(); + if (reachable_flag) { + if ((num_hosts-num_unreachable >= reachable)) { + printf(">=%d hosts are reachable\n", reachable); + } else { + printf("<%d hosts are reachable\n", reachable); + } + exit(0); + } + if (num_noaddress) exit(2); else if (num_alive != num_hosts) From af9b3bfbfec9b2181296e8050f15ad71cec334a4 Mon Sep 17 00:00:00 2001 From: deepak0004 Date: Sat, 27 Oct 2018 22:58:01 +0530 Subject: [PATCH 5/6] Adding separate exit codes for success or failure, and printing the number of reachable hosts --- ci/test-10-option-u-x.pl | 4 ++-- src/fping.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/ci/test-10-option-u-x.pl b/ci/test-10-option-u-x.pl index 49b9120..35a5bc4 100755 --- a/ci/test-10-option-u-x.pl +++ b/ci/test-10-option-u-x.pl @@ -27,14 +27,14 @@ $cmd->stderr_is_eq(""); { 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(">=1 hosts are reachable\n"); +$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(0); +$cmd->exit_is_num(1); $cmd->stdout_is_eq("<2 hosts are reachable\n"); $cmd->stderr_is_eq(""); } diff --git a/src/fping.c b/src/fping.c index 8964215..c157255 100644 --- a/src/fping.c +++ b/src/fping.c @@ -1334,11 +1334,12 @@ void finish() if (reachable_flag) { if ((num_hosts-num_unreachable >= reachable)) { - printf(">=%d hosts are reachable\n", reachable); + printf("Number of reachable hosts: %d\n", num_hosts-num_unreachable); + exit(0); } else { printf("<%d hosts are reachable\n", reachable); + exit(1); } - exit(0); } if (num_noaddress) From 03d821e983074594f672d593aa4f2a8c3c461bd9 Mon Sep 17 00:00:00 2001 From: deepak0004 Date: Mon, 29 Oct 2018 02:25:50 +0530 Subject: [PATCH 6/6] Removing flag variable and its dependencies, printing required and reachable hosts, and changing initialisation of min_reachable --- ci/test-10-option-u-x.pl | 4 ++-- doc/fping.pod | 2 +- src/fping.c | 23 ++++++++++------------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/ci/test-10-option-u-x.pl b/ci/test-10-option-u-x.pl index 35a5bc4..97acf9a 100755 --- a/ci/test-10-option-u-x.pl +++ b/ci/test-10-option-u-x.pl @@ -27,7 +27,7 @@ $cmd->stderr_is_eq(""); { 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->stdout_is_eq("Enough hosts reachable (required: 1, reachable: 1)\n"); $cmd->stderr_is_eq(""); } @@ -35,6 +35,6 @@ $cmd->stderr_is_eq(""); { 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->stdout_is_eq("Not enough hosts reachable (required: 2, reachable: 1)\n"); $cmd->stderr_is_eq(""); } diff --git a/doc/fping.pod b/doc/fping.pod index 0af442e..2805837 100644 --- a/doc/fping.pod +++ b/doc/fping.pod @@ -225,7 +225,7 @@ Show targets that are unreachable. Print B version information. -=item B<-x>, B<--reachable>=I +=item B<-x>, B<--min_reachable>=I Given a list of hosts, this mode prints if number of active hosts>=N or not. diff --git a/src/fping.c b/src/fping.c index c157255..7ddd58e 100644 --- a/src/fping.c +++ b/src/fping.c @@ -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, reachable = 1; +unsigned int count = 1, min_reachable = 0; unsigned int trials; unsigned int report_interval = 0; unsigned int ttl = 0; @@ -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, reachable_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 outage_flag = 0; @@ -414,7 +414,7 @@ int main(int argc, char** argv) { NULL, 'T', OPTPARSE_REQUIRED }, { "unreach", 'u', OPTPARSE_NONE }, { "version", 'v', OPTPARSE_NONE }, - { "reachable", 'x', OPTPARSE_REQUIRED }, + { "min_reachable", 'x', OPTPARSE_REQUIRED }, { 0, 0, 0 } }; @@ -606,9 +606,8 @@ int main(int argc, char** argv) exit(0); case 'x': - if (!(reachable = (unsigned int)atoi(optparse_state.optarg))) + if (!(min_reachable = (unsigned int)atoi(optparse_state.optarg))) usage(1); - reachable_flag = 1; break; case 'f': @@ -739,7 +738,7 @@ int main(int argc, char** argv) exit(1); } - if (alive_flag || unreachable_flag || reachable_flag) + if (alive_flag || unreachable_flag || min_reachable) verbose_flag = 0; if (count_flag) { @@ -819,8 +818,6 @@ 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) @@ -1332,12 +1329,12 @@ 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); + if (min_reachable) { + if ((num_hosts-num_unreachable) >= min_reachable) { + printf("Enough hosts reachable (required: %d, reachable: %d)\n", min_reachable, num_hosts-num_unreachable); exit(0); } else { - printf("<%d hosts are reachable\n", reachable); + printf("Not enough hosts reachable (required: %d, reachable: %d)\n", min_reachable, num_hosts-num_unreachable); exit(1); } } @@ -2786,6 +2783,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"); + fprintf(out, " -x, --min_reachable=N shows if >=N hosts are reachable or not\n"); exit(is_error); } \ No newline at end of file