1 | 1 | simandl | package HotSaNICmod::OSdep; |
2 | | | |
3 | | | sub version { |
4 | | | ($VERSION = '$Revision: 1.8 $') =~ s/.*(\d+\.\d+).*/$1/; |
5 | | | return "default.pm $VERSION"; |
6 | | | } |
7 | | | |
8 | | | sub sample { |
9 | | | my %args=@_; |
10 | | | |
11 | | | $mtime=(stat("rrd"))[9]; |
12 | | | $now=time; |
13 | | | |
14 | | | if (($mtime+$args{INTERVAL}) <= $now) { |
15 | | | utime $now,$now,"rrd"; |
16 | | | |
17 | | | $processes=0; |
18 | | | |
19 | | | sub wait_for_child { |
20 | | | my $pid = wait; |
21 | | | return 0 if $pid < 0; |
22 | | | $processes--; |
23 | | | if ($processes<0) {$processes=0;} |
24 | | | } |
25 | | | |
26 | | | foreach (@{$args{HOST}}) { |
27 | | | my ($host,$descr,$method)=split /,/; |
28 | | | |
29 | | | if ( (!defined $method) || ($method eq "") || ($method eq "default") ) { $method=$args{METHOD}; } |
30 | | | |
31 | | | wait_for_child() if $processes >= $args{PARALLEL}; |
32 | | | |
33 | | | $processes++; |
34 | | | $return=fork; |
35 | | | |
36 | | | # child process |
37 | | | if ($return == 0) { |
38 | | | if (HotSaNIClog::check_debuglevel("MODULE_VERBOSE")) { HotSaNIClog::info("> $host using $method"); } |
39 | | | my ($meth,$proto)=split /-/,$method; |
40 | | | if ($meth eq "ping") { ($min,$avg,$max)=sysping($host,250,250,10); } |
41 | | | elsif ($meth eq "echo") { ($min,$avg,$max)=HotSaNICmod::common::echoping($host,250,250,10,$proto); } |
42 | | | elsif ($meth eq "perl") { ($min,$avg,$max)=HotSaNICmod::common::ping($host,250,250,10,$proto); } |
43 | | | else { HotSaNIClog::error("Unknown ping method!"); } |
44 | | | |
45 | | | if ($method ne $args{METHOD}) { HotSaNICmod::do_rrd("$host-$method","U",time,$min,$avg,$max); } |
46 | | | else { HotSaNICmod::do_rrd($host,"U",time,$min,$avg,$max); } |
47 | | | exit 0; |
48 | | | } |
49 | | | } |
50 | | | } |
51 | | | while($processes) { wait_for_child(); } |
52 | | | } |
53 | | | |
54 | | | |
55 | | | sub sysping { |
56 | | | my($HOST,$TIMEOUT,$WAIT,$COUNT)=@_; |
57 | | | $TIMEOUT/=1000; |
58 | | | $WAIT/=1000; |
59 | | | |
60 | | | my $command="ping -s $HOST 56 $COUNT"; |
61 | | | |
62 | | | open FILE,"$command |" || HotSaNIClog::error("unable to run `$command': $!"); |
63 | | | while (<FILE>) { $line=$_; } |
64 | | | close FILE; |
65 | | | |
66 | | | # for testing purposes... |
67 | | | # |
68 | | | # $line="round-trip min/avg/max = 0.4/0.4/0.4 ms"; |
69 | | | # $line="round-trip min/avg/max/mdev = 0.285/0.304/0.324/0.026 ms"; |
70 | | | # $line="rtt min/avg/max/mdev = 0.160/0.194/0.238/0.035 ms"; |
71 | | | |
72 | | | my $factor=1000; |
73 | | | |
74 | | | chomp $line; |
75 | | | if ( (index($line,"round-trip") >=0) || (index($line,"rtt") >=0) ) { |
76 | | | (undef,$times)=split /= */,$line; |
77 | | | ($min,$avg,$max)=split /[\/ ]/,$times; |
78 | | | if ($times =~ /us/) { $factor=1000000; } |
79 | | | } |
80 | | | else { ($min,$avg,$max)=(0,0,0); } |
81 | | | |
82 | | | return($min/$factor,$avg/$factor,$max/$factor); |
83 | | | } |
84 | | | |
85 | | | |
86 | | | 1; |
87 | | | |