jablonka.czprosek.czf

hotsanic

Subversion Repositories:
[/] [branches/] [HotSaNIC-0.5.0-pre6/] [modules/] [ping/] [platform/] [default.pm] - Rev 21 Go to most recent revision

Compare with Previous - Blame - Download


package HotSaNICmod::OSdep;

sub version {
  ($VERSION = '$Revision: 1.16 $') =~ s/.*(\d+\.\d+).*/$1/;
  return "default.pm $VERSION";
  }

sub sample {
  my %args=@_;

  $mtime=(stat("rrd"))[9];
  $now=time;

  if (($mtime+$args{INTERVAL}) <= $now) {
    utime $now,$now,"rrd";

    $processes=0;

    sub wait_for_child {
      my $pid = wait;
      return 0 if $pid < 0;
      $processes--;
      if ($processes<0) {$processes=0;}
      }
  
    foreach (@{$args{HOST}}) {
      my ($host,$descr,$method)=split /,/;

      if ( (!defined $method) || ($method eq "") || ($method eq "default") ) { $method=$args{METHOD}; }

      wait_for_child() if $processes >= $args{PARALLEL};

      $processes++;
      $return=fork;
    
      # child process
      if ($return == 0) {
        if (HotSaNIClog::check_debuglevel("MODULE_VERBOSE")) { HotSaNIClog::info("> $host using $method"); }
        my ($meth,$proto)=split /-/,$method;
        if    ($meth eq "ping") { ($min,$avg,$max)=sysping($host,250,250,10); }
        elsif ($meth eq "echo") { ($min,$avg,$max)=HotSaNICmod::common::echoping($host,250,250,10,$proto); }
        elsif ($meth eq "perl") { ($min,$avg,$max)=HotSaNICmod::common::ping($host,250,250,10,$proto); }
        else { HotSaNIClog::error("Unknown ping method!"); }

        if ($method ne $args{METHOD}) { HotSaNICmod::do_rrd("$host-$method","U",time,$min,$avg,$max); }
        else { HotSaNICmod::do_rrd($host,"U",time,$min,$avg,$max); }
        exit 0;
        }
      }
    }
  while($processes) { wait_for_child(); }
  }


sub sysping {
  my($HOST,$TIMEOUT,$WAIT,$COUNT)=@_;
  $TIMEOUT/=1000;
  $WAIT/=1000;

  my $command="ping $HOST -c $COUNT -w 1 -i 0.2";

  open FILE,"$command |" || HotSaNIClog::error("unable to run `$command': $!");
  while (<FILE>) { $line=$_; }
  close FILE;

# for testing purposes...
#
# $line="round-trip min/avg/max = 0.4/0.4/0.4 ms";
# $line="round-trip min/avg/max/mdev = 0.285/0.304/0.324/0.026 ms";
# $line="rtt min/avg/max/mdev = 0.160/0.194/0.238/0.035 ms";

  my $factor=1000;

  chomp $line;
  if ( (index($line,"round-trip") >=0) || (index($line,"rtt") >=0) ) {
    (undef,$times)=split /= */,$line;
    ($min,$avg,$max)=split /[\/ ]/,$times;
    if ($times =~ /us/) { $factor=1000000; }
    }
  else { ($min,$avg,$max)=(0,0,0); }

  return($min/$factor,$avg/$factor,$max/$factor);
  }


1;


Powered by WebSVN 2.2.1