jablonka.czprosek.czf

hotsanic

Subversion Repositories:
[/] [branches/] [HotSaNIC-0.5.0-pre6/] [modules/] [ping/] [platform/] [common.pm] - Rev 1

Compare with Previous - Blame - Download


package HotSaNICmod::common;

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

sub configure {
  %MODCONF=HotSaNICparser::get_moduleconfig(".",(INTERVAL=>"var",TYPE=>"var",PARALLEL=>"var",METHOD=>"var",HOST=>"array",SCALE=>"var",UPPER=>"var",SYSPING=>"var",PROTOCOL=>"var"));

# convert old settings entries
#
  if ($MODCONF{SCALE} ne "") { $MODCONF{GRAPH_STYLE}=$MODCONF{SCALE}; }
  if ($MODCONF{UPPER} ne "") { $MODCONF{GRAPH_MAX}=$MODCONF{UPPER}; }
  if ($MODCONF{SYSPING} eq "1") { $MODCONF{METHOD}="ping"; }
  else {
    if ($MODCONF{PROTOCOL} eq "tcp") { $MODCONF{METHOD}="perl-tcp"; }
    else { $MODCONF{METHOD}="perl-icmp"; }
    }

# check requirements
#
  if (index($MODCONF{METHOD},"perl") >= 0) { 
    eval { require Net::Ping; };
    if ($@) {
      HotSaNIClog::warn("Perlmodule Net::Ping not installed - falling back to system's ping command.");
      $MODCONF{METHOD}="ping";
      }
    }
  return %MODCONF;
  }

sub get_names {
  my $entry=shift || ",";
  my $host="";
  my $community="";
                
  my ($item,$description,$method)=split /,/,$entry;
  if (! defined $method) { $method=""; }
  if( index($item, "SNMP:") >= 0) { (undef,$host,$community,$item) = split /:/, $item; }
  if ($host ne "") { $name="$host:$item"; }
  else { $name="$item"; }
  ($dbname=$name) =~ s/:/_/g;
  $file=$name;

  if ($method ne "") {
    $name="$name ($method)";
    $dbname="$dbname-$method";
    $file="$file-$method";
    }


  return ($host,$community,$item,$dbname,$name,$file,$description);
  }

####  Usage:
####     ping ("host or IP",timeout (ms), wait (ms), count, protocol);
####
####  Return:
####     min/avg/max (ms)
sub ping {
  my $TIMING="";
  
  eval { require Time::HiRes; };
  if ($@) { HotSaNIClog::warn("Time::HiRes not found!"); }
  else { $TIMING="Time::HiRes"; require Time::HiRes; }
  
  if ($TIMING eq "") {
    eval { require 'sys/syscall.ph'; };
    if ($@) { HotSaNIClog::warn("syscall.ph not found!"); } 
    else { $TIMING="syscall"; require 'sys/syscall.ph'; }
    }
  
  if ($TIMING eq "") {
    HotSaNIClog::error("No suitable timing method found!");
    HotSaNIClog::info("Please consider to install the Time::HiRes module from CPAN.");
    HotSaNIClog::info("nYou can get it at http://www.cpan.org/");
    return (0,0,0);
    }

  use Net::Ping;

   my $HOST=shift || "127.0.0.1";
   my $TIMEOUT=shift || 50;
   my $WAIT=shift || 1000;
   my $COUNT=shift || 10;
   my $PROTOCOL=shift || "icmp";

  if ($TIMEOUT<50) { $TIMEOUT=50; }
  $TIMEOUT/=1000;

  $WAIT/=1000;

  if ($COUNT<5) { $COUNT=5; }

  $min=10000000; $max=0; $add=0;

  $p = Net::Ping->new($PROTOCOL);
  $TIMEVAL_T = "LL";
  $done = $start = pack($TIMEVAL_T, ());

  $replies=0;
  #  $first_reply=$p->ping($HOST, $TIMEOUT);

  for ($i=0; $i < $COUNT ; $i++) {

    if ($TIMING eq "syscall") { syscall(&SYS_gettimeofday, $start, 0) != -1 or dupe_control("die",$MODNAME,": gettimeofday: $!"); }
    if ($TIMING eq "Time::HiRes") { $start=Time::HiRes::time(); }
    $reply=$p->ping($HOST, $TIMEOUT);
    if ($TIMING eq "syscall") {
      syscall(&SYS_gettimeofday, $done,  0) != -1 or dupe_control("die",$MODNAME,": gettimeofday: $!");
      @start = unpack($TIMEVAL_T, $start);
      @done  = unpack($TIMEVAL_T, $done);
      $time=($done[0]-$start[0])*1000 + ($done[1]-$start[1])/1000;
      }
    if ($TIMING eq "Time::HiRes") {
      $done=Time::HiRes::time();
      $time=$done-$start;
      }
      
    if ( $reply ) {
      push @timearr,$time;
      $replies++;
      $add+=$time;
      }
    select (undef, undef,undef,$WAIT);
    }
      
  $p->close();
      
  $avg=$add/$replies if $replies>0;
  $replies=0;$add=0;
  foreach (@timearr) {
    if ($_ < 3*$avg) {
      $add+=$_;
      $replies++;
      $min=$_ if $_<$min;
      $max=$_ if $_>$max;
      }
    }
  if ($replies eq 0) { $replies++; $min=0; $max=0; $add=0; }
  $avg=$add/$replies;
  return($min,$avg,$max);
  }


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);
  }


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

  my $prt="";
  if    ($PROTO eq "udp")  { $prt="-u "; }
  elsif ($PROTO eq "icp")  { $prt="-i / "; }
  elsif ($PROTO eq "http") { $prt="-h / "; }
  elsif ($PROTO eq "smtp") { $prt="-S "; }
  elsif ($PROTO eq "disc") { $prt="-d "; }
  elsif ($PROTO eq "cgen") { $prt="-c "; }

  my $command="echoping $prt$HOST -w $WAIT -t $TIMEOUT -n $COUNT";

  my ($min,$avg,$max)=(0,0,0);

  open FILE,"$command |" || HotSaNIClog::error("unable to run `$command': $!");
  while (<FILE>) {
    chomp;
    @items=split;
    if ($items[0] eq "Minimum") { $min=$items[2]; }
    if ($items[0] eq "Maximum") { $max=$items[2]; }
    if ($items[0] eq "Average") { $avg=$items[2]; }
    }
  close FILE;
# Minimum time: 0.009705 seconds (26378 bytes per sec.)
# Maximum time: 0.012159 seconds (21054 bytes per sec.)
# Average time: 0.010741 seconds (23834 bytes per sec.)
# Standard deviation: 0.000632
# Median time: 0.010499 seconds (24383 bytes per sec.)

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

1;


Powered by WebSVN 2.2.1