jablonka.czprosek.czf

hotsanic

Subversion Repositories:
[/] [branches/] [HotSaNIC-0.5.0-jablonecka/] [modules/] [ping/] [platform/] [default.pm] - Blame information for rev 2

 

Line No. Rev Author Line
11simandlpackage HotSaNICmod::OSdep;
2 
3use RRDs;
4 
5sub version {
6 ($VERSION = '$Revision: 1.11 $') =~ s/.*(\d+\.\d+).*/$1/;
7 return "default.pm $VERSION";
8 }
9 
10sub sample {
11 my %args=@_;
12 
13 $mtime=(stat("rrd"))[9];
14 $now=time;
15 
16# if ((($mtime+$interval) > $now) && ($mtime+5 < $now)) { dupe_control("stop",$ARGS{"MODNAME"},""); }
17 if (($mtime+$args{INTERVAL}) <= $now) {
18 utime $now,$now,"rrd";
19 
20 $processes=0;
21 
22 sub wait_for_child {
23 my $pid = wait;
24 return 0 if $pid < 0;
25 $processes--;
26 if ($processes<0) {$processes=0;}
27 }
28 
29 
30 my @HOSTS=();
31 
32 foreach (keys(%args)) {
33 if (index($_,"HOST:") >=0 ) {
34 (undef,$host)=split /:/;
35 push @HOSTS,$host;
36 }
37 }
38 
39 foreach $host (@HOSTS) {
40 
41 wait_for_child() if $processes >= $args{PARALLEL};
42 
43 $processes++;
44 $return=fork;
45 
46 # child process
47 if ($return == 0) {
48 if (defined($args{DEBUGLEVEL}) && $args{DEBUGLEVEL}>100) { print " > $host\n"; }
49 if ($args{SYSPING} == 0) { ($min,$avg,$max)=ping($host,250,250,10,$args{PROTOCOL}); }
50 else { ($min,$avg,$max)=sysping($host,250,250,10); }
51 
52 if ( ! -e "rrd/$host.rrd" ) { system("./makerrd","$host","U") }
53 RRDs::update "rrd/$host.rrd",time.":".$min.":".$avg.":".$max;
54 if ($ERROR = RRDs::error) { print time," ",$MODNAME,": unable to update `$host.rrd': $ERROR"; }
55# if ($args{DEBUGLEVEL}>101) { printf " < %-15s (%3.2f/%3.2f/%3.2f)\n",$host,$min,$avg,$max; }
56 exit 0;
57 }
58 }
59 }
60 while($processes) { wait_for_child(); }
61 }
62 
63#### Usage:
64#### ping ("host or IP",timeout (ms), wait (ms), count, protocol);
65####
66#### Return:
67#### min/avg/max (ms)
68sub ping {
69 my $TIMING="";
70 
71 eval { require Time::HiRes; };
72 if ($@) { print "Time::HiRes not found!\n"; }
73 else { $TIMING="Time::HiRes"; require Time::HiRes; }
74 
75 if ($TIMING eq "") {
76 eval { require 'sys/syscall.ph'; };
77 if ($@) { print "syscall.ph not found!\n"; }
78 else { $TIMING="syscall"; require 'sys/syscall.ph'; }
79 }
80 
81 if ($TIMING eq "") {
82 print "No suitable timing method found!\nPlease consider to install the Time::HiRes module from CPAN.\nYou can get it at http://www.cpan.org/\n";
83 return (0,0,0);
84 }
85 
86 use Net::Ping;
87 
88 my $HOST=shift || "127.0.0.1";
89 my $TIMEOUT=shift || 50;
90 my $WAIT=shift || 1000;
91 my $COUNT=shift || 10;
92 my $PROTOCOL=shift || "icmp";
93 
94 if ($TIMEOUT<50) { $TIMEOUT=50; }
95 $TIMEOUT/=1000;
96 
97 $WAIT/=1000;
98 
99 if ($COUNT<5) { $COUNT=5; }
100 
101 $min=10000000; $max=0; $add=0;
102 
103 $p = Net::Ping->new($PROTOCOL);
104 $TIMEVAL_T = "LL";
105 $done = $start = pack($TIMEVAL_T, ());
106 
107 $replies=0;
108 # $first_reply=$p->ping($HOST, $TIMEOUT);
109 
110 for ($i=0; $i < $COUNT ; $i++) {
111 
112 if ($TIMING eq "syscall") { syscall(&SYS_gettimeofday, $start, 0) != -1 or dupe_control("die",$MODNAME,": gettimeofday: $!"); }
113 if ($TIMING eq "Time::HiRes") { $start=Time::HiRes::time(); }
114 $reply=$p->ping($HOST, $TIMEOUT);
115 if ($TIMING eq "syscall") {
116 syscall(&SYS_gettimeofday, $done, 0) != -1 or dupe_control("die",$MODNAME,": gettimeofday: $!");
117 @start = unpack($TIMEVAL_T, $start);
118 @done = unpack($TIMEVAL_T, $done);
119 $time=($done[0]-$start[0])*1000 + ($done[1]-$start[1])/1000;
120 }
121 if ($TIMING eq "Time::HiRes") {
122 $done=Time::HiRes::time();
123 $time=$done-$start;
124 }
125 
126 if ( $reply ) {
127 push @timearr,$time;
128 $replies++;
129 $add+=$time;
130 }
131 select (undef, undef,undef,$WAIT);
132 }
133 
134 $p->close();
135 
136 $avg=$add/$replies if $replies>0;
137 $replies=0;$add=0;
138 foreach (@timearr) {
139 if ($_ < 3*$avg) {
140 $add+=$_;
141 $replies++;
142 $min=$_ if $_<$min;
143 $max=$_ if $_>$max;
144 }
145 }
146 if ($replies eq 0) { $replies++; $min=0; $max=0; $add=0; }
147 $avg=$add/$replies;
148 return($min,$avg,$max);
149 }
150 
151sub sysping {
152 my($HOST,$TIMEOUT,$WAIT,$COUNT)=@_;
153 $TIMEOUT/=1000;
154 $WAIT/=1000;
155 
156 my $command="ping $HOST -c $COUNT -w 1 -i 0.2";
157 my $line = "";
158 open FILE,"$command |" || print time," ",$MODNAME,": unable to run `$command': $!\n";
159 while (<FILE>) { $line=$_; }
160 close FILE;
161 
162# for testing purposes...
163#
164# $line="round-trip min/avg/max = 0.4/0.4/0.4 ms";
165# $line="round-trip min/avg/max/mdev = 0.285/0.304/0.324/0.026 ms";
166# $line="rtt min/avg/max/mdev = 0.160/0.194/0.238/0.035 ms";
167 
168 my $factor=1000;
169 
170 chomp $line;
171 if ( (index($line,"round-trip") >=0) || (index($line,"rtt") >=0) ) {
172 (undef,$times)=split /= */,$line;
173 ($min,$avg,$max)=split /[\/ ]/,$times;
174 if ($times =~ /us/) { $factor=1000000; }
175 }
176 else { ($min,$avg,$max)=(0,0,0); }
177 
178 return($min/$factor,$avg/$factor,$max/$factor);
179 }
180 
1811;
182 

Powered by WebSVN 2.2.1