File Coverage

blib/lib/AsyncPing.pm
Criterion Covered Total %
statement 5 78 6.4
branch 0 28 0.0
condition 0 6 0.0
subroutine 2 6 33.3
pod 0 2 0.0
total 7 120 5.8


line stmt bran cond sub pod time code
1             package AsyncPing;
2              
3 1     1   16161 use 5.006;
  1         3  
4 1     1   540 use IO::Socket;
  1         19722  
  1         4  
5              
6             =head1 NAME
7              
8             AsyncPing - ping a huge number of servers in several seconds
9              
10             =head1 VERSION
11              
12             Version 2016.1129
13              
14             =cut
15              
16             our $VERSION = '2016.1129';
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 $count=@$list;
74 0           my $expected;
75             my %resultmap;
76 0           my $revlistmap;
77 0           my @nlist;
78 0           my $got=0;
79 0           my $sent=0;
80 0           foreach my $h(@$list){
81 0           chomp($h);
82 0           my $n=inet_aton($h);
83 0           my $ip=inet_ntoa($n);
84 0           $expected->{$ip}=0;
85 0           $revlistmap->{$ip}=$h;
86 0           push @nlist,$n;
87             }
88 0           my $start=time();
89 0           my $endtime;
90 0   0       while(! $endtime || (time()<$endtime+$self->{timeout}) ){
91 0           my $bytesread=$self->{socket}->sysread(my $chunk, 4096, 0);
92 0 0         if($bytesread>0){
93 0           my $dest_ip=substr($chunk, 12,4);
94 0           my @ip=unpack('C*',$dest_ip);
95 0           my $ipstr=join('.',@ip);
96 0 0 0       if(exists $expected->{$ipstr} && $expected->{$ipstr}==0){
97 0           $got++;
98 0           $expected->{$ipstr}=1;
99             }
100 0 0         last if($count==$got);
101             }else{
102 0 0         if($sent<$count){
    0          
103 0           my $ip=$nlist[$sent];
104 0           my $checksum = 0x0000;
105 0           my $msg = pack($ICMP_PING, 0x08, 0x00, $checksum, $identifier, $sequence, $data);
106 0           $checksum = &_asyncping_checksum($msg);
107 0           $msg = pack($ICMP_PING, 0x08, 0x00, $checksum, $identifier, $sequence, $data);
108 0 0         $self->{socket}->send($msg, 0, scalar sockaddr_in(0, $ip)) or print "Error: on ip";
109 0           $sent++;
110             }elsif(! $endtime){
111 0           $endtime=time();
112             }
113             }
114             }
115              
116 0           foreach my $ip(sort keys %$expected){
117 0 0         if($expected->{$ip}==1){
118 0           $resultmap{$revlistmap->{$ip}}=1;
119             }else{
120 0           $resultmap{$revlistmap->{$ip}}=0;
121             }
122             }
123 0           return \%resultmap;
124             }
125              
126             sub ping{
127 0     0 0   my ($self,$list)=@_;
128 0           my $result=$self->_ping($list);
129 0           my $try=$self->{try};
130 0           my %failed;
131 0           while(--$try>0){
132 0           foreach my $h(keys %$result){
133 0 0         if($result->{$h}==0){
134 0           $failed{$h}=0;
135             }
136             }
137 0           my @failedservers=keys %failed;
138 0 0         if(@failedservers){
139 0           my $retryresult=$self->_ping(\@failedservers);
140 0           foreach my $h(keys %$retryresult){
141 0 0         if($retryresult->{$h}==1){
142 0           delete $failed{$h};
143 0           $result->{$h}=1;
144             }
145             }
146             }
147             }
148 0           return $result;
149             }
150              
151             sub _asyncping_checksum {
152 0     0     my ($msg) = @_;
153 0           my $res = 0;
154 0           foreach my $int (unpack "n*", $msg) {
155 0           $res += $int;
156             }
157 0 0         $res += unpack('C', substr($msg, -1, 1)) << 8 if length($msg) % 2;
158 0           $res = ($res >> 16) + ($res & 0xffff);
159 0           $res = ($res >> 16) + ($res & 0xffff);
160 0           return ~$res;
161             }
162              
163             1;
164              
165             =head1 AUTHOR
166              
167             Xinfeng Wang(xinfwang@ebay.com)
168              
169             =head1 BUGS
170              
171             Please report any bugs or feature requests to C, or through
172             the web interface at L. I will be notified, and then you'll
173             automatically be notified of progress on your bug as I make changes.
174              
175              
176              
177              
178             =head1 SUPPORT
179              
180             You can find documentation for this module with the perldoc command.
181              
182             perldoc AsyncPing
183              
184              
185             You can also look for information at:
186              
187             =over 4
188              
189             =item * RT: CPAN's request tracker (report bugs here)
190              
191             L
192              
193             =item * AnnoCPAN: Annotated CPAN documentation
194              
195             L
196              
197             =item * CPAN Ratings
198              
199             L
200              
201             =item * Search CPAN
202              
203             L
204              
205             =back
206              
207              
208             =head1 ACKNOWLEDGEMENTS
209              
210              
211             =head1 LICENSE AND COPYRIGHT
212              
213             Copyright 2016 Xinfeng Wang.
214              
215             This program is free software; you can redistribute it and/or modify it
216             under the terms of the the Artistic License (2.0). You may obtain a
217             copy of the full license at:
218              
219             L
220              
221             Any use, modification, and distribution of the Standard or Modified
222             Versions is governed by this Artistic License. By using, modifying or
223             distributing the Package, you accept this license. Do not use, modify,
224             or distribute the Package, if you do not accept this license.
225              
226             If your Modified Version has been derived from a Modified Version made
227             by someone other than you, you are nevertheless required to ensure that
228             your Modified Version complies with the requirements of this license.
229              
230             This license does not grant you the right to use any trademark, service
231             mark, tradename, or logo of the Copyright Holder.
232              
233             This license includes the non-exclusive, worldwide, free-of-charge
234             patent license to make, have made, use, offer to sell, sell, import and
235             otherwise transfer the Package with respect to any patent claims
236             licensable by the Copyright Holder that are necessarily infringed by the
237             Package. If you institute patent litigation (including a cross-claim or
238             counterclaim) against any party alleging that the Package constitutes
239             direct or contributory patent infringement, then this Artistic License
240             to you shall terminate on the date that such litigation is filed.
241              
242             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
243             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
244             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
245             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
246             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
247             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
248             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
249             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
250              
251              
252             =cut
253              
254             1; # End of AsyncPing