Skip to content

Commit

Permalink
Merge pull request #2 from ramok/master
Browse files Browse the repository at this point in the history
Add new flag "--sync-ping"
  • Loading branch information
famzah committed Oct 27, 2017
2 parents b69ee48 + b18fed4 commit ae225e2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,8 @@ sudo ./ping-multi google.com github.com
```bash
sudo ./ping-multi -f sample.list
```
```bash
sudo ./ping-multi -S 192.168.0.{1,2,5,20}
```

Making ICMP ping requests requires "root" privileges on Linux.
40 changes: 36 additions & 4 deletions ping-multi
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ Getopt::Long::Configure qw(gnu_getopt);

use Curses;
use JSON;
use Time::HiRes qw(sleep usleep);
use Time::HiRes qw(sleep usleep time);
use POSIX;
use Statistics::Descriptive;
use File::Basename;

my $timeout = 1;
my %ping_data : shared = ();
my %resolved_hosts : shared = ();
my $sync_ping_cnt : shared = 0;
my @pressed_keys = ();
my @thr_handles = (); # used in the SIG handlers
my $scr_max = {
Expand All @@ -40,6 +41,7 @@ my %config : shared = (
'show_scale' => 0,
'scale_ms' => undef,
'pause' => 0,
'sync_ping' => 0,
);

sub ping_host {
Expand All @@ -52,6 +54,7 @@ sub ping_host {
$pinger->hires();

while (1) {
my $ping_begin = time;
if (!$config{'pause'}) {
{
lock(%resolved_hosts);
Expand Down Expand Up @@ -94,7 +97,15 @@ sub ping_host {
}
}

sleep(1);
if ($config{'sync_ping'}) {
lock($sync_ping_cnt);
$sync_ping_cnt++;
cond_wait($sync_ping_cnt) until $sync_ping_cnt == 0;
}

# sleep till one second from ping beginning elapsed.
my $sleep_time = 1 - (time - $ping_begin);
sleep($sleep_time) if ($sleep_time > 0);
}

#$pinger->close(); // we are exiting anyway
Expand Down Expand Up @@ -434,6 +445,22 @@ sub print_ping_data {
}
}
sub trigger_sync_ping {
if (!$config{'sync_ping'}) {
return;
}
lock($sync_ping_cnt);
lock(%ping_data);
if ($sync_ping_cnt < keys %ping_data) {
return ;
}
$sync_ping_cnt = 0;
cond_broadcast($sync_ping_cnt);
}
sub print_main_headers() {
my $line;
my $fmt;
Expand Down Expand Up @@ -480,8 +507,11 @@ sub print_main_headers() {

sub usage() {
die(
"Usage: ".basename($0)." [-f FILE_LIST] [HOST]...\n\n".
"Ping all hosts from FILE_LIST and HOSTs.\n"
"Usage: ".basename($0)." [-S] [-f FILE] [HOST]...\n".
"Ping all hosts from FILE and HOSTs.\n\n".
" -f, --file=FILE read list of hosts from file\n".
" -h, --help display this help and exit\n".
" -S, --sync-ping send for all hosts same ammount of ping requests\n"
);
}

Expand All @@ -492,6 +522,7 @@ sub main() {
GetOptions(
'file|f=s' => \$file,
'help|h' => \$help,
'sync-ping|S' => \$config{'sync_ping'},
);

if ($help) {
Expand Down Expand Up @@ -523,6 +554,7 @@ sub main() {
print_main_headers();
print_host_headers();
print_ping_data();
trigger_sync_ping();

if (scalar threads->list(threads::running) != scalar keys %{$host_meta}) {
die("A worker thread exited unexpectedly.");
Expand Down

0 comments on commit ae225e2

Please sign in to comment.