#!/usr/bin/perl
# Stanislav Lechev (c) 2000/2001 ... GPL'ed

# ako nqkoj module ti lipswa -> eto ot kyde movesh da gi izteglish

use Net::SMTP;
# http://www.cpan.org/authors/id/G/GB/GBARR/libnet-1.0703.tar.gz


use Net::Ping;
# http://www.cpan.org/authors/id/G/GS/GSAR/perl-5.6.1.tar.gz


use Socket;

$DEBUG=0;									# 0=off / 1=on
# debuga e za da printwa na stdout-a (ne e dobre za crond)

$MailHost = "localhost";					# mail servera
$MailResult = "35988xxxxxx\@sms.mtel.net";	# mail addressa za sms ili pager
#$MailResult = "xxxxxx\@mobipage.bg";		# mobipage


# only IP's are allowed
@IPZ =	(
	"127.0.0.1",
	"192.168.0.1",
	"212.5.138.1",
	"149.174.214.132",
	"62.176.79.1");



#-----------------------------------------------------------------------------
foreach $IP(@IPZ) {

	$p = Net::Ping->new("icmp");	# move da bude ("tcp","udp","icmp")
									# spored mene taka e naj dobre...
									# inache mnogo se bawi

									
	if (! $p->ping($IP,1)) {
#	                  ^^^
# tazi 1-ca e timeout-a ako imash lagnati hostowe movesh da q uwelichish :)


		$host = gethostbyaddr(inet_aton($IP), AF_INET);

		
# towa napraktika e msg-to :)
		$result="$host($IP) is down";

		
# towa movesh da go iztriesh ako ne go shtesh :)
		`logger "$result"`;


		print "$result\n" if $DEBUG;
# i da sendnem mail-a :)
		$smtp = Net::SMTP->new($MailHost);
		$smtp->mail("host\@checker.com");
		$smtp->to($MailResult);
		$smtp->data();
		$smtp->datasend("To: $MailResult\n");
		$smtp->datasend("Subject: $host($IP)\n");
		$smtp->datasend("\n");
		$smtp->datasend("$host($IP) is down\n");
		$smtp->dataend();
    }
}

