Net::Ping help

Steve Smythe ssmythe at docent.com
Thu Nov 21 11:46:20 CST 2002


Hiya Gang!
 
It's been a year since I moved to CA and I've really missed you folks.
I wish I could have lunches with you guys.  :-)
 
I'm having an interesting problem with a hosts monitoring script.  I'm
just now putting it together, and I'm coming up with some strange
results.
 
I'm using the Net::Ping module on win32, and I don't get pings back
from the same machine when I use the windows ping command
that says the host is up.  Here's the script...
 
---------------------------
#!c:/perl/bin/perl -w
############################################################################
##
# monitor - Monitor host ping status and email failures
#
#  author: Steve Smythe
#   email:  <mailto:ssmythe at docent.com> ssmythe at docent.com
# version: 1.0
#    date: 2002.11.21
############################################################################
##
 
use Net::SMTP;
use Net::Ping;
 

############################################################################
##
# Init
############################################################################
##
sub Init {
 # mail globals
 our $mailHost="mail.docent.com";
 our $mailFrom="ssmythe\@docent.com";
 
 # host globals
 our $hostList="monitor_hosts.txt";
 our %hosts=();
 
 # message globals
 our $msg="Ping Results:\n\n";
}
 

############################################################################
##
# GetHostList
############################################################################
##
sub GetHostList {
 my $file="";
 
 $file="$hostList";
 open(FILE, "$file") || die "cannot open file \"$file\" for input: $!";
 while(<FILE>) {
  chomp;
  s/\#.*$//g;  # strip comments at end of line
  s/^[\s]+//g; # strip leading whitespace
  s/[\s]+$//g; # strip trailing whitespace
  if (/^\#/ || /^[\s]*$/) { next; } # skip comments and blank lines
  $hosts{$_}='';
 }
 close(FILE) || die "cannot close file \"$file\" for input: $!";
}
 

############################################################################
##
# DumpHostList
############################################################################
##
sub DumpHostList {
 my $host="";
 
 foreach $host (sort keys %hosts) {
  print "[$host]\n";
 }
}
 

############################################################################
##
# PingHost
############################################################################
##
sub PingHost {
 my ($host)=@_;
 my $rc=0;
 
 my $p="";
 
 $p = Net::Ping->new();
 if ($p->ping($host, 10)) {
  $rc=1;
 }
 $p->close();
 
 printf ("%-3d ", $rc);
 
 return($rc);
}
 

############################################################################
##
# PingHosts
############################################################################
##
sub PingHosts {
 my $host="";
 
 foreach $host (sort keys %hosts) {
  printf("%-20s", $host);
  $msg.=sprintf("%-20s", $host);
  if (PingHost($host) == 1) {
   print "ok\n";
   $msg.="ok\n";
  } else {
   print "failed\n";
   $msg.="failed\n";
  }
 }
}
 

############################################################################
##
# Email
############################################################################
##
sub Email {
 my ($mailTo, $subject, $msg)=@_;
 
 my $smtp="";
 
 $smtp = Net::SMTP->new($mailHost) || die "can't open mailhost: $mailHost";
 $smtp->mail($mailFrom)    || die "can't set mail from: $mailFrom";
 $smtp->to($mailTo)     || die "can't set mail to  : $mailTo";
 $smtp->data("From: $mailFrom\nTo: $mailTo\nSubject: $subject\n\n$msg");
 $smtp->quit;
}
 

############################################################################
##
# Process
############################################################################
##
sub Process {
 GetHostList();
 # DumpHostList();
 PingHosts();
 # Email("$mailFrom", "Ping Stats", "$msg");
}
 

############################################################################
##
# MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN
#
############################################################################
##
Init();
Process();
---------------------------
 
Here's the example data file:
 
---------------------------
# monitor_hosts.txt - host list to monitor
 
slv-eng-p4  # Perforce Server
slv-eng-stor2  # CD-ROM, build image server
hydra   # Solaris 5.x, 6.x build server
sol   # Solaris 4.x build server
cmbuild   # Win32 5.x build server
qabuild   # Win32 4.x build server
gbuild   # Win32 Exchange 6.x build server

---------------------------
 
Here's the script output:
 
D:\dev_perforce\cmtools\monitor>monitor.pl
cmbuild             1   ok
gbuild              0   failed
hydra               1   ok
qabuild             0   failed
slv-eng-p4          0   failed
slv-eng-stor2       1   ok
sol                 1   ok
 
Here's when I manually ping each box...
 
D:\dev_perforce\cmtools\monitor>ping cmbuild
 
Pinging cmbuild.docent.com [172.25.2.227] with 32 bytes of data:
 
Reply from 172.25.2.227: bytes=32 time<10ms TTL=128
Reply from 172.25.2.227: bytes=32 time<10ms TTL=128
Reply from 172.25.2.227: bytes=32 time<10ms TTL=128
Reply from 172.25.2.227: bytes=32 time<10ms TTL=128
 
Ping statistics for 172.25.2.227:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum =  0ms, Average =  0ms
 
D:\dev_perforce\cmtools\monitor>ping gbuild
 
Pinging gbuild.docent.com [172.25.2.229] with 32 bytes of data:
 
Reply from 172.25.2.229: bytes=32 time<10ms TTL=128
Reply from 172.25.2.229: bytes=32 time<10ms TTL=128
Reply from 172.25.2.229: bytes=32 time<10ms TTL=128
Reply from 172.25.2.229: bytes=32 time<10ms TTL=128
 
Ping statistics for 172.25.2.229:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum =  0ms, Average =  0ms
 
D:\dev_perforce\cmtools\monitor>ping hydra
 
Pinging hydra.docent.com [172.25.2.82] with 32 bytes of data:
 
Reply from 172.25.2.82: bytes=32 time<10ms TTL=255
Reply from 172.25.2.82: bytes=32 time<10ms TTL=255
Reply from 172.25.2.82: bytes=32 time<10ms TTL=255
Reply from 172.25.2.82: bytes=32 time<10ms TTL=255
 
Ping statistics for 172.25.2.82:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum =  0ms, Average =  0ms
 
D:\dev_perforce\cmtools\monitor>ping qabuild
 
Pinging qabuild.docent.com [172.25.2.163] with 32 bytes of data:
 
Reply from 172.25.2.163: bytes=32 time<10ms TTL=128
Reply from 172.25.2.163: bytes=32 time<10ms TTL=128
Reply from 172.25.2.163: bytes=32 time<10ms TTL=128
Reply from 172.25.2.163: bytes=32 time<10ms TTL=128
 
Ping statistics for 172.25.2.163:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum =  0ms, Average =  0ms
 
...and so on...
 
So does the windows ping command have a more relaxed
standard for failed packets?  I don't get it.  Anyone have an
idea on this?
 
Steve
 
 

Steve Smythe
Sr. CM Engineer
ssmythe at docent.com <mailto:ssmythe at docent.com> 
W:650-934-9546
C:925-699-6822 
Docent, Inc.
2444 Charleston Road
Mountain View, CA 94043-1622
http://www.docent.com <http://www.docent.com/> 

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/archives/pikes-peak-pm/attachments/20021121/eb76648f/attachment.htm


More information about the Pikes-peak-pm mailing list