blib/lib/AsyncPing.pm

filter: Statementstmt 12.9% 12 / 93 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 33.3% 2 / 6 Podpod 0.0% 0 / 2 totaltotal 9.2% 14 / 152 CC 27 SCAR 50.2
CC
27
Cov
9%
_ping
272.0
ping
56.0
new
20.0
CRAP 151.9
↑ Summary
1 package AsyncPing;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 1 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 1 my $ICMP_PING = 'ccnnna*';
49 1 my $identifier = 1;
50 1 my $sequence = 2;
51 1 my $data = 'abcdefghijklmn';
52  
53 0 ▶sub new{
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
54 0 my ($class,%arg)=@_;
55 0 my $timeout=3;
56 0 my $try=1;
57 0 ▶ my $socket = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $socket = "IO::Socket::INET"->new("Proto", "icmp", "Type", 3, "Blocking", 0) or Carp::croak("Unable to create icmp socket : $!") 0 0 0
58 Proto => 'icmp',
59 Type => SOCK_RAW,
60 Blocking => 0
61 ) or Carp::croak "Unable to create icmp socket : $!";
62 0 ▶ if($arg{timeout}){
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"timeout"}) 0 0 0
63 0 $timeout=$arg{timeout};
64 }
65 0 ▶ if($arg{try}>0){
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"try"} > 0) 0 0 0
66 0 $try=$arg{try};
67 }
68 0 return bless {socket=>$socket, timeout=>$timeout, try=>$try}, $class;
69 }
70  
71 0 ▶sub _ping{
Subroutine _ping: not called CC=16 SCAR=56.1
72 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 ▶ if($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
89 0 $resultmap{$h}=0;
90 }
91 0 ▶ if($n && $ip){
Branch: 1 branch
BranchTrueFalseTotal
if ($n and $ip) { } 0 0 0
Condition0%0/3
$n and $ip
!ll&&!rl&&r
000
MC/DC0%0/2
$n and $ip
$n $ip
Truth table0%0/3
A and B
A= $nB= $ip
ABresult
0X0
100
111
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 ▶ while(! $endtime || (time()<$endtime+$self->{timeout}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $endtime or time < $endtime + $self->{"timeout"}) 0 0 0
Condition0%0/3
not $endtime or time < $endtime + $self->{"timeout"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $endtime or time < $endtime + $self->{"timeout"}
not $endtime time < $endtime + $self->{"timeout"}
Truth table0%0/3
not(A) or B
A= not $endtimeB= time < $endtime + $self->{"timeout"}
ABresult
1X1
011
000
104 0 my $bytesread=$self->{socket}->sysread(my $chunk, 4096, 0);
105 0 ▶ if($bytesread>0){
Branch: 1 branch
BranchTrueFalseTotal
if ($bytesread > 0) { } 0 0 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 ▶ if(exists $expected->{$ipstr} && $expected->{$ipstr}==0){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $expected->{$ipstr} and $expected->{$ipstr} == 0) 0 0 0
Condition0%0/3
exists $expected->{$ipstr} and $expected->{$ipstr} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
exists $expected->{$ipstr} and $expected->{$ipstr} == 0
exists $expected->{$ipstr} $expected->{$ipstr} == 0
Truth table0%0/3
A and B
A= exists $expected->{$ipstr}B= $expected->{$ipstr} == 0
ABresult
0X0
100
111
110 0 $got++;
111 0 $expected->{$ipstr}=1;
112 }
113 0 ▶ last if($count==$got);
Branch: 1 branch
BranchTrueFalseTotal
if $count == $got 0 0 0
114 }else{
115 0 ▶ if($sent<$count){
Branch: 2 branches
BranchTrueFalseTotal
if ($sent < $count) { } 0 0 0
elsif (not $endtime) { } 0 0 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 ▶ $self->{socket}->send($msg, 0, scalar sockaddr_in(0, $ip)) or print "Error: on ip";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"socket"}->send($msg, 0, scalar sockaddr_in(0, $ip)) or print "Error: on ip" 0 0 0
122 0 $sent++;
123 }elsif(! $endtime){
124 0 $endtime=time();
125 }
126 }
127 }
128  
129 0 foreach my $ip(sort keys %$expected){
130 0 ▶ if($expected->{$ip}==1){
Branch: 1 branch
BranchTrueFalseTotal
if ($expected->{$ip} == 1) { } 0 0 0
131 0 $resultmap{$revlistmap->{$ip}}=1;
132 }else{
133 0 $resultmap{$revlistmap->{$ip}}=0;
134 }
135 }
136 0 return \%resultmap;
137 }
138  
139 0 ▶sub ping{
Subroutine ping: not called CC=7 SCAR=40.3
Pod undocumented
140 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){
Branch: 1 branch
BranchTrueFalseTotal
if (--$try > 0) 0 0 0
145 0 foreach my $h(keys %$result){
146 0 ▶ if($result->{$h}==0){
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{$h} == 0) 0 0 0
147 0 $failed{$h}=0;
148 }
149 }
150 0 my @failedservers=keys %failed;
151 0 ▶ if(@failedservers){
Branch: 1 branch
BranchTrueFalseTotal
if (@failedservers) 0 0 0
152 0 my $retryresult=$self->_ping(\@failedservers);
153 0 foreach my $h(keys %$retryresult){
154 0 ▶ if($retryresult->{$h}==1){
Branch: 1 branch
BranchTrueFalseTotal
if ($retryresult->{$h} == 1) 0 0 0
155 0 delete $failed{$h};
156 0 $result->{$h}=1;
157 }
158 }
159 }
160 }
161 0 return $result;
162 }
163  
164 0 ▶sub _asyncping_checksum {
Subroutine _asyncping_checksum: not called CC=3 SCAR=24.8
165 0 my ($msg) = @_;
166 0 my $res = 0;
167 0 foreach my $int (unpack "n*", $msg) {
168 0 $res += $int;
169 }
170 0 ▶ $res += unpack('C', substr($msg, -1, 1)) << 8 if length($msg) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if length($msg) % 2 0 0 0
171 0 $res = ($res >> 16) + ($res & 0xffff);
172 0 $res = ($res >> 16) + ($res & 0xffff);
173 0 return ~$res;
174 }
175  
176 1 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<bug-asyncping at rt.cpan.org>, or through
185 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AsyncPing>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=AsyncPing>
205  
206 =item * AnnoCPAN: Annotated CPAN documentation
207  
208 L<http://annocpan.org/dist/AsyncPing>
209  
210 =item * CPAN Ratings
211  
212 L<http://cpanratings.perl.org/d/AsyncPing>
213  
214 =item * Search CPAN
215  
216 L<http://search.cpan.org/dist/AsyncPing/>
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<http://www.perlfoundation.org/artistic_license_2_0>
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 1; # End of AsyncPing
↑ Summary