| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package AsyncPing; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
16662
|
use 5.006; |
|
|
1
|
|
|
|
|
3
|
|
|
4
|
1
|
|
|
1
|
|
539
|
use IO::Socket; |
|
|
1
|
|
|
|
|
20164
|
|
|
|
1
|
|
|
|
|
3
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
AsyncPing - ping a huge number of servers in several seconds |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version 2016.1207 |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '2016.1207'; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use AsyncPing; |
|
22
|
|
|
|
|
|
|
use Data::Dumper; |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $asyncping=new AsyncPing(timeout=>3,try=>2); |
|
25
|
|
|
|
|
|
|
my @servers=("host1","host2","host3"); |
|
26
|
|
|
|
|
|
|
my $result=$asyncping->ping(\@servers); |
|
27
|
|
|
|
|
|
|
print Dumper $result; |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
First of all, I tried some of the Async Ping modules on cpan, none of them really worked when I tried to ping 10,000 servers. |
|
32
|
|
|
|
|
|
|
This AsyncPing is designed to ping a huge number of servers. As I tested, it can send out ICMP request to 25,000 servers per second on a very common server. |
|
33
|
|
|
|
|
|
|
Also I tested if I fork a seperate process handling the recieving work, it can be improved to about 45,000 ping per second. |
|
34
|
|
|
|
|
|
|
The timeout value start to work after this module sends out all the requests. |
|
35
|
|
|
|
|
|
|
The retry will only work on the failed ones. |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Please notice that ICMP is not TCP connection, there is no guarantee that if you send a request to a server, you'll get a response. So you may want to set the try to 2. |
|
38
|
|
|
|
|
|
|
So if you have a million servers to ping(10% of them are down) and you set the timeout to 3 and retry to 2, I can estimate the time to be about (1M/25k+3)+(100k/25k+3)=50 seconds. |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Please also notice that since ICMP can only be sent by root, if you want to use this library, you'll have to run your program as root. |
|
41
|
|
|
|
|
|
|
If the ping requests are going through firewall, your ping requests could possibly be discarded by firewall, don't blame the library. |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
since every process share same network interface and usually there is only 1 network interface on a server, I think it doens't really help if you make it parallel |
|
44
|
|
|
|
|
|
|
or multi-threaded to increase speed. Just like you don't get much benefit if you make more threads while you have only 1 CPU. But you can test on your own, good luck! |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my $ICMP_PING = 'ccnnna*'; |
|
49
|
|
|
|
|
|
|
my $identifier = 1; |
|
50
|
|
|
|
|
|
|
my $sequence = 2; |
|
51
|
|
|
|
|
|
|
my $data = 'abcdefghijklmn'; |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub new{ |
|
54
|
0
|
|
|
0
|
0
|
|
my ($class,%arg)=@_; |
|
55
|
0
|
|
|
|
|
|
my $timeout=3; |
|
56
|
0
|
|
|
|
|
|
my $try=1; |
|
57
|
0
|
0
|
|
|
|
|
my $socket = IO::Socket::INET->new( |
|
58
|
|
|
|
|
|
|
Proto => 'icmp', |
|
59
|
|
|
|
|
|
|
Type => SOCK_RAW, |
|
60
|
|
|
|
|
|
|
Blocking => 0 |
|
61
|
|
|
|
|
|
|
) or Carp::croak "Unable to create icmp socket : $!"; |
|
62
|
0
|
0
|
|
|
|
|
if($arg{timeout}){ |
|
63
|
0
|
|
|
|
|
|
$timeout=$arg{timeout}; |
|
64
|
|
|
|
|
|
|
} |
|
65
|
0
|
0
|
|
|
|
|
if($arg{try}>0){ |
|
66
|
0
|
|
|
|
|
|
$try=$arg{try}; |
|
67
|
|
|
|
|
|
|
} |
|
68
|
0
|
|
|
|
|
|
return bless {socket=>$socket, timeout=>$timeout, try=>$try}, $class; |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub _ping{ |
|
72
|
0
|
|
|
0
|
|
|
my ($self,$list)=@_; |
|
73
|
0
|
|
|
|
|
|
my $expected; |
|
74
|
|
|
|
|
|
|
my %resultmap; |
|
75
|
0
|
|
|
|
|
|
my $revlistmap; |
|
76
|
0
|
|
|
|
|
|
my @nlist; |
|
77
|
0
|
|
|
|
|
|
my $got=0; |
|
78
|
0
|
|
|
|
|
|
my $sent=0; |
|
79
|
0
|
|
|
|
|
|
my %tmp; |
|
80
|
0
|
|
|
|
|
|
foreach my $h(@$list){ |
|
81
|
0
|
|
|
|
|
|
chomp($h); |
|
82
|
0
|
|
|
|
|
|
my $n; |
|
83
|
|
|
|
|
|
|
my $ip; |
|
84
|
0
|
|
|
|
|
|
eval{ |
|
85
|
0
|
|
|
|
|
|
$n=inet_aton($h); |
|
86
|
0
|
|
|
|
|
|
$ip=inet_ntoa($n); |
|
87
|
|
|
|
|
|
|
}; |
|
88
|
0
|
0
|
|
|
|
|
if($@){ |
|
89
|
0
|
|
|
|
|
|
$resultmap{$h}=0; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
0
|
0
|
0
|
|
|
|
if($n && $ip){ |
|
92
|
0
|
|
|
|
|
|
$expected->{$ip}=0; |
|
93
|
0
|
|
|
|
|
|
$revlistmap->{$ip}=$h; |
|
94
|
0
|
|
|
|
|
|
push @nlist,$n; |
|
95
|
0
|
|
|
|
|
|
$tmp{$n}=$ip; |
|
96
|
|
|
|
|
|
|
}else{ |
|
97
|
0
|
|
|
|
|
|
$resultmap{$h}=0; |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
} |
|
100
|
0
|
|
|
|
|
|
my $count=@nlist; |
|
101
|
0
|
|
|
|
|
|
my $start=time(); |
|
102
|
0
|
|
|
|
|
|
my $endtime; |
|
103
|
0
|
|
0
|
|
|
|
while(! $endtime || (time()<$endtime+$self->{timeout}) ){ |
|
104
|
0
|
|
|
|
|
|
my $bytesread=$self->{socket}->sysread(my $chunk, 4096, 0); |
|
105
|
0
|
0
|
|
|
|
|
if($bytesread>0){ |
|
106
|
0
|
|
|
|
|
|
my $dest_ip=substr($chunk, 12,4); |
|
107
|
0
|
|
|
|
|
|
my @ip=unpack('C*',$dest_ip); |
|
108
|
0
|
|
|
|
|
|
my $ipstr=join('.',@ip); |
|
109
|
0
|
0
|
0
|
|
|
|
if(exists $expected->{$ipstr} && $expected->{$ipstr}==0){ |
|
110
|
0
|
|
|
|
|
|
$got++; |
|
111
|
0
|
|
|
|
|
|
$expected->{$ipstr}=1; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
0
|
0
|
|
|
|
|
last if($count==$got); |
|
114
|
|
|
|
|
|
|
}else{ |
|
115
|
0
|
0
|
|
|
|
|
if($sent<$count){ |
|
|
|
0
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
my $ip=$nlist[$sent]; |
|
117
|
0
|
|
|
|
|
|
my $checksum = 0x0000; |
|
118
|
0
|
|
|
|
|
|
my $msg = pack($ICMP_PING, 0x08, 0x00, $checksum, $identifier, $sequence, $data); |
|
119
|
0
|
|
|
|
|
|
$checksum = &_asyncping_checksum($msg); |
|
120
|
0
|
|
|
|
|
|
$msg = pack($ICMP_PING, 0x08, 0x00, $checksum, $identifier, $sequence, $data); |
|
121
|
0
|
0
|
|
|
|
|
$self->{socket}->send($msg, 0, scalar sockaddr_in(0, $ip)) or print "Error: on ip"; |
|
122
|
0
|
|
|
|
|
|
$sent++; |
|
123
|
|
|
|
|
|
|
}elsif(! $endtime){ |
|
124
|
0
|
|
|
|
|
|
$endtime=time(); |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
foreach my $ip(sort keys %$expected){ |
|
130
|
0
|
0
|
|
|
|
|
if($expected->{$ip}==1){ |
|
131
|
0
|
|
|
|
|
|
$resultmap{$revlistmap->{$ip}}=1; |
|
132
|
|
|
|
|
|
|
}else{ |
|
133
|
0
|
|
|
|
|
|
$resultmap{$revlistmap->{$ip}}=0; |
|
134
|
|
|
|
|
|
|
} |
|
135
|
|
|
|
|
|
|
} |
|
136
|
0
|
|
|
|
|
|
return \%resultmap; |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub ping{ |
|
140
|
0
|
|
|
0
|
0
|
|
my ($self,$list)=@_; |
|
141
|
0
|
|
|
|
|
|
my $result=$self->_ping($list); |
|
142
|
0
|
|
|
|
|
|
my $try=$self->{try}; |
|
143
|
0
|
|
|
|
|
|
my %failed; |
|
144
|
0
|
|
|
|
|
|
while(--$try>0){ |
|
145
|
0
|
|
|
|
|
|
foreach my $h(keys %$result){ |
|
146
|
0
|
0
|
|
|
|
|
if($result->{$h}==0){ |
|
147
|
0
|
|
|
|
|
|
$failed{$h}=0; |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
} |
|
150
|
0
|
|
|
|
|
|
my @failedservers=keys %failed; |
|
151
|
0
|
0
|
|
|
|
|
if(@failedservers){ |
|
152
|
0
|
|
|
|
|
|
my $retryresult=$self->_ping(\@failedservers); |
|
153
|
0
|
|
|
|
|
|
foreach my $h(keys %$retryresult){ |
|
154
|
0
|
0
|
|
|
|
|
if($retryresult->{$h}==1){ |
|
155
|
0
|
|
|
|
|
|
delete $failed{$h}; |
|
156
|
0
|
|
|
|
|
|
$result->{$h}=1; |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
} |
|
160
|
|
|
|
|
|
|
} |
|
161
|
0
|
|
|
|
|
|
return $result; |
|
162
|
|
|
|
|
|
|
} |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub _asyncping_checksum { |
|
165
|
0
|
|
|
0
|
|
|
my ($msg) = @_; |
|
166
|
0
|
|
|
|
|
|
my $res = 0; |
|
167
|
0
|
|
|
|
|
|
foreach my $int (unpack "n*", $msg) { |
|
168
|
0
|
|
|
|
|
|
$res += $int; |
|
169
|
|
|
|
|
|
|
} |
|
170
|
0
|
0
|
|
|
|
|
$res += unpack('C', substr($msg, -1, 1)) << 8 if length($msg) % 2; |
|
171
|
0
|
|
|
|
|
|
$res = ($res >> 16) + ($res & 0xffff); |
|
172
|
0
|
|
|
|
|
|
$res = ($res >> 16) + ($res & 0xffff); |
|
173
|
0
|
|
|
|
|
|
return ~$res; |
|
174
|
|
|
|
|
|
|
} |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
1; |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 AUTHOR |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Xinfeng Wang(xinfwang@ebay.com) |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 BUGS |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
|
185
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
|
186
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 SUPPORT |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
perldoc AsyncPing |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
You can also look for information at: |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=over 4 |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
L |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
L |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
L |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=item * Search CPAN |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
L |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=back |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
Copyright 2016 Xinfeng Wang. |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
229
|
|
|
|
|
|
|
under the terms of the the Artistic License (2.0). You may obtain a |
|
230
|
|
|
|
|
|
|
copy of the full license at: |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
L |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
Any use, modification, and distribution of the Standard or Modified |
|
235
|
|
|
|
|
|
|
Versions is governed by this Artistic License. By using, modifying or |
|
236
|
|
|
|
|
|
|
distributing the Package, you accept this license. Do not use, modify, |
|
237
|
|
|
|
|
|
|
or distribute the Package, if you do not accept this license. |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
If your Modified Version has been derived from a Modified Version made |
|
240
|
|
|
|
|
|
|
by someone other than you, you are nevertheless required to ensure that |
|
241
|
|
|
|
|
|
|
your Modified Version complies with the requirements of this license. |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
This license does not grant you the right to use any trademark, service |
|
244
|
|
|
|
|
|
|
mark, tradename, or logo of the Copyright Holder. |
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
This license includes the non-exclusive, worldwide, free-of-charge |
|
247
|
|
|
|
|
|
|
patent license to make, have made, use, offer to sell, sell, import and |
|
248
|
|
|
|
|
|
|
otherwise transfer the Package with respect to any patent claims |
|
249
|
|
|
|
|
|
|
licensable by the Copyright Holder that are necessarily infringed by the |
|
250
|
|
|
|
|
|
|
Package. If you institute patent litigation (including a cross-claim or |
|
251
|
|
|
|
|
|
|
counterclaim) against any party alleging that the Package constitutes |
|
252
|
|
|
|
|
|
|
direct or contributory patent infringement, then this Artistic License |
|
253
|
|
|
|
|
|
|
to you shall terminate on the date that such litigation is filed. |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER |
|
256
|
|
|
|
|
|
|
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. |
|
257
|
|
|
|
|
|
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
|
258
|
|
|
|
|
|
|
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY |
|
259
|
|
|
|
|
|
|
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR |
|
260
|
|
|
|
|
|
|
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR |
|
261
|
|
|
|
|
|
|
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, |
|
262
|
|
|
|
|
|
|
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
263
|
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=cut |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
1; # End of AsyncPing |