| 1 | 1 | simandl | package HotSaNICmod::common; |
| 2 | | | |
| 3 | | | sub version { |
| 4 | | | ($VERSION = '$Revision: 1.9 $') =~ s/.*(\d+\.\d+).*/$1/; |
| 5 | | | return "common.pm $VERSION"; |
| 6 | | | } |
| 7 | | | |
| 8 | | | sub configure { |
| 9 | | | %MODCONF=HotSaNICparser::get_moduleconfig(".",(INTERVAL=>"var",TYPE=>"var",PARALLEL=>"var",METHOD=>"var",HOST=>"array",SCALE=>"var",UPPER=>"var",SYSPING=>"var",PROTOCOL=>"var")); |
| 10 | | | |
| 11 | | | # convert old settings entries |
| 12 | | | # |
| 13 | | | if ($MODCONF{SCALE} ne "") { $MODCONF{GRAPH_STYLE}=$MODCONF{SCALE}; } |
| 14 | | | if ($MODCONF{UPPER} ne "") { $MODCONF{GRAPH_MAX}=$MODCONF{UPPER}; } |
| 15 | | | if ($MODCONF{SYSPING} eq "1") { $MODCONF{METHOD}="ping"; } |
| 16 | | | else { |
| 17 | | | if ($MODCONF{PROTOCOL} eq "tcp") { $MODCONF{METHOD}="perl-tcp"; } |
| 18 | | | else { $MODCONF{METHOD}="perl-icmp"; } |
| 19 | | | } |
| 20 | | | |
| 21 | | | # check requirements |
| 22 | | | # |
| 23 | | | if (index($MODCONF{METHOD},"perl") >= 0) { |
| 24 | | | eval { require Net::Ping; }; |
| 25 | | | if ($@) { |
| 26 | | | HotSaNIClog::warn("Perlmodule Net::Ping not installed - falling back to system's ping command."); |
| 27 | | | $MODCONF{METHOD}="ping"; |
| 28 | | | } |
| 29 | | | } |
| 30 | | | return %MODCONF; |
| 31 | | | } |
| 32 | | | |
| 33 | | | sub get_names { |
| 34 | | | my $entry=shift || ","; |
| 35 | | | my $host=""; |
| 36 | | | my $community=""; |
| 37 | | | |
| 38 | | | my ($item,$description,$method)=split /,/,$entry; |
| 39 | | | if (! defined $method) { $method=""; } |
| 40 | | | if( index($item, "SNMP:") >= 0) { (undef,$host,$community,$item) = split /:/, $item; } |
| 41 | | | if ($host ne "") { $name="$host:$item"; } |
| 42 | | | else { $name="$item"; } |
| 43 | | | ($dbname=$name) =~ s/:/_/g; |
| 44 | | | $file=$name; |
| 45 | | | |
| 46 | | | if ($method ne "") { |
| 47 | | | $name="$name ($method)"; |
| 48 | | | $dbname="$dbname-$method"; |
| 49 | | | $file="$file-$method"; |
| 50 | | | } |
| 51 | | | |
| 52 | | | |
| 53 | | | return ($host,$community,$item,$dbname,$name,$file,$description); |
| 54 | | | } |
| 55 | | | |
| 56 | | | #### Usage: |
| 57 | | | #### ping ("host or IP",timeout (ms), wait (ms), count, protocol); |
| 58 | | | #### |
| 59 | | | #### Return: |
| 60 | | | #### min/avg/max (ms) |
| 61 | | | sub ping { |
| 62 | | | my $TIMING=""; |
| 63 | | | |
| 64 | | | eval { require Time::HiRes; }; |
| 65 | | | if ($@) { HotSaNIClog::warn("Time::HiRes not found!"); } |
| 66 | | | else { $TIMING="Time::HiRes"; require Time::HiRes; } |
| 67 | | | |
| 68 | | | if ($TIMING eq "") { |
| 69 | | | eval { require 'sys/syscall.ph'; }; |
| 70 | | | if ($@) { HotSaNIClog::warn("syscall.ph not found!"); } |
| 71 | | | else { $TIMING="syscall"; require 'sys/syscall.ph'; } |
| 72 | | | } |
| 73 | | | |
| 74 | | | if ($TIMING eq "") { |
| 75 | | | HotSaNIClog::error("No suitable timing method found!"); |
| 76 | | | HotSaNIClog::info("Please consider to install the Time::HiRes module from CPAN."); |
| 77 | | | HotSaNIClog::info("nYou can get it at http://www.cpan.org/"); |
| 78 | | | return (0,0,0); |
| 79 | | | } |
| 80 | | | |
| 81 | | | use Net::Ping; |
| 82 | | | |
| 83 | | | my $HOST=shift || "127.0.0.1"; |
| 84 | | | my $TIMEOUT=shift || 50; |
| 85 | | | my $WAIT=shift || 1000; |
| 86 | | | my $COUNT=shift || 10; |
| 87 | | | my $PROTOCOL=shift || "icmp"; |
| 88 | | | |
| 89 | | | if ($TIMEOUT<50) { $TIMEOUT=50; } |
| 90 | | | $TIMEOUT/=1000; |
| 91 | | | |
| 92 | | | $WAIT/=1000; |
| 93 | | | |
| 94 | | | if ($COUNT<5) { $COUNT=5; } |
| 95 | | | |
| 96 | | | $min=10000000; $max=0; $add=0; |
| 97 | | | |
| 98 | | | $p = Net::Ping->new($PROTOCOL); |
| 99 | | | $TIMEVAL_T = "LL"; |
| 100 | | | $done = $start = pack($TIMEVAL_T, ()); |
| 101 | | | |
| 102 | | | $replies=0; |
| 103 | | | # $first_reply=$p->ping($HOST, $TIMEOUT); |
| 104 | | | |
| 105 | | | for ($i=0; $i < $COUNT ; $i++) { |
| 106 | | | |
| 107 | | | if ($TIMING eq "syscall") { syscall(&SYS_gettimeofday, $start, 0) != -1 or dupe_control("die",$MODNAME,": gettimeofday: $!"); } |
| 108 | | | if ($TIMING eq "Time::HiRes") { $start=Time::HiRes::time(); } |
| 109 | | | $reply=$p->ping($HOST, $TIMEOUT); |
| 110 | | | if ($TIMING eq "syscall") { |
| 111 | | | syscall(&SYS_gettimeofday, $done, 0) != -1 or dupe_control("die",$MODNAME,": gettimeofday: $!"); |
| 112 | | | @start = unpack($TIMEVAL_T, $start); |
| 113 | | | @done = unpack($TIMEVAL_T, $done); |
| 114 | | | $time=($done[0]-$start[0])*1000 + ($done[1]-$start[1])/1000; |
| 115 | | | } |
| 116 | | | if ($TIMING eq "Time::HiRes") { |
| 117 | | | $done=Time::HiRes::time(); |
| 118 | | | $time=$done-$start; |
| 119 | | | } |
| 120 | | | |
| 121 | | | if ( $reply ) { |
| 122 | | | push @timearr,$time; |
| 123 | | | $replies++; |
| 124 | | | $add+=$time; |
| 125 | | | } |
| 126 | | | select (undef, undef,undef,$WAIT); |
| 127 | | | } |
| 128 | | | |
| 129 | | | $p->close(); |
| 130 | | | |
| 131 | | | $avg=$add/$replies if $replies>0; |
| 132 | | | $replies=0;$add=0; |
| 133 | | | foreach (@timearr) { |
| 134 | | | if ($_ < 3*$avg) { |
| 135 | | | $add+=$_; |
| 136 | | | $replies++; |
| 137 | | | $min=$_ if $_<$min; |
| 138 | | | $max=$_ if $_>$max; |
| 139 | | | } |
| 140 | | | } |
| 141 | | | if ($replies eq 0) { $replies++; $min=0; $max=0; $add=0; } |
| 142 | | | $avg=$add/$replies; |
| 143 | | | return($min,$avg,$max); |
| 144 | | | } |
| 145 | | | |
| 146 | | | |
| 147 | | | sub sysping { |
| 148 | | | my($HOST,$TIMEOUT,$WAIT,$COUNT)=@_; |
| 149 | | | $TIMEOUT/=1000; |
| 150 | | | $WAIT/=1000; |
| 151 | | | |
| 152 | | | my $command="ping $HOST -c $COUNT -w 1 -i 0.2"; |
| 153 | | | |
| 154 | | | open FILE,"$command |" || HotSaNIClog::error("unable to run `$command': $!"); |
| 155 | | | while (<FILE>) { $line=$_; } |
| 156 | | | close FILE; |
| 157 | | | |
| 158 | | | # for testing purposes... |
| 159 | | | # |
| 160 | | | # $line="round-trip min/avg/max = 0.4/0.4/0.4 ms"; |
| 161 | | | # $line="round-trip min/avg/max/mdev = 0.285/0.304/0.324/0.026 ms"; |
| 162 | | | # $line="rtt min/avg/max/mdev = 0.160/0.194/0.238/0.035 ms"; |
| 163 | | | |
| 164 | | | my $factor=1000; |
| 165 | | | |
| 166 | | | chomp $line; |
| 167 | | | if ( (index($line,"round-trip") >=0) || (index($line,"rtt") >=0) ) { |
| 168 | | | (undef,$times)=split /= */,$line; |
| 169 | | | ($min,$avg,$max)=split /[\/ ]/,$times; |
| 170 | | | if ($times =~ /us/) { $factor=1000000; } |
| 171 | | | } |
| 172 | | | else { ($min,$avg,$max)=(0,0,0); } |
| 173 | | | |
| 174 | | | return($min/$factor,$avg/$factor,$max/$factor); |
| 175 | | | } |
| 176 | | | |
| 177 | | | |
| 178 | | | sub echoping { |
| 179 | | | my($HOST,$TIMEOUT,$WAIT,$COUNT,$PROTO)=@_; |
| 180 | | | $TIMEOUT/=1000; |
| 181 | | | $WAIT/=1000; |
| 182 | | | |
| 183 | | | my $prt=""; |
| 184 | | | if ($PROTO eq "udp") { $prt="-u "; } |
| 185 | | | elsif ($PROTO eq "icp") { $prt="-i / "; } |
| 186 | | | elsif ($PROTO eq "http") { $prt="-h / "; } |
| 187 | | | elsif ($PROTO eq "smtp") { $prt="-S "; } |
| 188 | | | elsif ($PROTO eq "disc") { $prt="-d "; } |
| 189 | | | elsif ($PROTO eq "cgen") { $prt="-c "; } |
| 190 | | | |
| 191 | | | my $command="echoping $prt$HOST -w $WAIT -t $TIMEOUT -n $COUNT"; |
| 192 | | | |
| 193 | | | my ($min,$avg,$max)=(0,0,0); |
| 194 | | | |
| 195 | | | open FILE,"$command |" || HotSaNIClog::error("unable to run `$command': $!"); |
| 196 | | | while (<FILE>) { |
| 197 | | | chomp; |
| 198 | | | @items=split; |
| 199 | | | if ($items[0] eq "Minimum") { $min=$items[2]; } |
| 200 | | | if ($items[0] eq "Maximum") { $max=$items[2]; } |
| 201 | | | if ($items[0] eq "Average") { $avg=$items[2]; } |
| 202 | | | } |
| 203 | | | close FILE; |
| 204 | | | # Minimum time: 0.009705 seconds (26378 bytes per sec.) |
| 205 | | | # Maximum time: 0.012159 seconds (21054 bytes per sec.) |
| 206 | | | # Average time: 0.010741 seconds (23834 bytes per sec.) |
| 207 | | | # Standard deviation: 0.000632 |
| 208 | | | # Median time: 0.010499 seconds (24383 bytes per sec.) |
| 209 | | | |
| 210 | | | return($min,$avg,$max); |
| 211 | | | } |
| 212 | | | |
| 213 | | | 1; |
| 214 | | | |