#!/usr/bin/perl
# Filename: wanmon.pl, Date: 17/12/2001
#
# Author: Frank Diercks, VRZ Koblenz, vrz@lasv.rlp.de
# 
# Dieses Skript überprueft anhand des Perlmoduls Ping die Verbindungen zu den im Array
# host aufgeführten Gegenstellen. 
#
use Net::Ping;

@host=("192.168.1.1", "192.168.1.2");

$logfile="//var//log//wanmon.log";
$host_count=scalar(@host) - 1;


for ($x=0; $x <= $host_count; $x++)
 {
 $time_out_flag[$x]=0;
 # print $time_out_flag[$x];
 }


while (1)
{
$p = Net::Ping->new("icmp");

  for ($x=0; $x <= $host_count; $x++)
  {
  # print $host[$x], " ", $time_out_flag[$x], "\n";

	if ($p->ping($host[$x], 3) == 0)
	{
		if ($p->ping($host[$x], 3) == 0)
		{

			if ($time_out_flag[$x] == 0)
			{
                	open (LF, ">>$logfile") || die "Cannot open $logfile for write :$!";
                	print LF scalar(localtime), " ", $host[$x], " timed out.\n";
                	close LF;
	
        		system 'echo '.$host[$x].' timed out ! | smbclient -M admin1 > /dev/null';
			};
	
		$time_out_flag[$x] = 1;
		};
	};
	# Default timeout to wait for a reply is 3 seconds
	if ($p->ping($host[$x], 3) == 1)
	{

		if ($time_out_flag[$x] == 1)
		{
		open (LF, ">>$logfile") or die "Cannot open $logfile for write :$!";
		print LF scalar(localtime), " ", $host[$x], " online again.\n";
		close LF;

        	system 'echo '.$host[$x].' online again. | smbclient -M admin1 > /dev/null';
		}

	$time_out_flag[$x] = 0;
	};

  };

$p->close();
sleep 900;
};

exit;

