#!/usr/bin/perl -w
##########################################################################
#      Pingu is a simple ping monitoring program - looking at ping times
#
# This especially annotated copy (PC Plus issue 234 on the SuperDisc)
# is Copyright (C) 2005 Future Publishing but is based upon the GPL version
# which is Copyright (C) 2005 Paul Grosse with permission.
#
##########################################################################

# pingu consists of a ping monitoring/ data collection program and a
# program that collates the ping time data

# make a note of the time in Unix time
$t = time;

# use the absolute path to the configuration file and the log file
# we will need these if we are running from somewhere other than
# the local directory
$path = "D:\\Program Files\\Apache Group\\Apache2\\cgi-bin\\pingu\\";

# open config file and read it in...
open CFG, "<".$path."pingu.conf";
  # all in one go.
  @conf = <CFG>;
close CFG;

# open log files and store the data in them
foreach $w (@conf) {
  # get rid of any line endings
  chomp $w;
  # if this line starts with an IP address, use it otherwise ignore it
  if ($w =~ m/^(\d+.\d+.\d+.\d+)/) {
    # the regular expression above collects any set of 4 numbers
    # separated by any other characters (you could use spaces if you want)
    # it is up to you when you write the configuration file to make sure that
    # they are actually IP addresses otherwise a line starting with 1,234,5678-9
    # would get through. You generated the config file so you make sure it is right.

    # next, we put the IP address into two variables - one for the IP address and the
    # other to be converted into a file name (Windows machines don't like dots
    # everywhere so we are going to convert them to dashes.
    # Whilst this particular version of pingucoll is for a UNIX-like system,
    # we can be consistent with the Windows version for the file names
    $adrfn = $adr = $1;
    ## change dots for dashes
    $adrfn =~ s/\./-/g;
    #the following is a debug line. You can use lines like this when you write
    #your programs. Just remark it out (using a hash mark) when you have done
    # with it and when you are tidying up, just delete them
#    print "$adr $adrfn\n";
    open LOG, ">>".$path."pingu".$adrfn.".log";
      # ping the destination using the Windows ping command.
      # You can add anything you like to this or mess around
      # with it in any way - creating tests and so on
      $x = "ping -n 3 $adr";
      @y = readpipe ($x);
      # take the last line as this has our results on it
      $r = $y[$#y];
      # debug again, just checking that it picks things up all right
#      print "$r\n";
      # check that it was okay
      if ($r =~ /Average = (\d+)ms/i) {
        # got the average time now - note that in the case of Windows, we use
        # a different regular expression
        print LOG "$t $1\n";
        # debug, checking that the right one was picked up
#        print "$1\n";
      } else {
        # no numbers there so this is a 100 percent fail
#        print "oops\n";
        print LOG "$t -1\n";
      };
    # close that log file and on with the next
    close LOG;
  };
};
