-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, 16 Jul 2003, Jun Tanamal wrote:

Hi Jun,

I would like to share this ping script that i wrote long time ago
when i want to monitor our ISP  connection status. it was written in perl
though the script is not really in good work since im on the stages of
learning when i wrote it but it works, hope you'll find it useful. =)

its a daemon script and you can execute it by
./routerping start|stop|status. it sleeps every 5 minutes and it only send
2 emails in every incident (1 for down and 1 for up) and it doesn't flood
your mailbox.

you need a perl modules to use the script and you can get on CPAN

Simple
Net::Ping
Net:SMTP


HTH
- --glynn



> I want to monitor my connection to the ISP say, every 5 minutes. How do
> I make a script to ping a specific IP and make it send an email warning
> message when the connection is down?
> Thanks for the ideas..
>
> -Jun
>
>
> --
> Philippine Linux Users' Group (PLUG) Mailing List
> [EMAIL PROTECTED] (#PLUG @ irc.free.net.ph)
> Official Website: http://plug.linux.org.ph
> Searchable Archives: http://marc.free.net.ph
> .
> To leave, go to http://lists.q-linux.com/mailman/listinfo/plug
> .
> Are you a Linux newbie? To join the newbie list, go to
> http://lists.q-linux.com/mailman/listinfo/ph-linux-newbie
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE/FQ3lEmCTy9QHJy0RAihMAKCk/QGABmweT7LZAnLmsLTUi99llACfYODi
BNA7f2PW+zstqXXpf6H++FY=
=RBg9
-----END PGP SIGNATURE-----
#!/usr/bin/perl -w

use Simple;
use Net::Ping;

######################################################################
sub send_mail_down {

$to = "[EMAIL PROTECTED]";
$subject = "$ip down";
$from = "[EMAIL PROTECTED]";
$body = "Reason:\n\nHost unreachable at $var";

use Net::SMTP;

my $relay = "localhost";
my $smtp = Net::SMTP->new($relay) || die "Can't open mail connection: $!";

$smtp->mail($from);
$smtp->to($to);

$smtp->data();
$smtp->datasend("To: $to\n");
$smtp->datasend("From: $from\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("\n");
$smtp->datasend("$body\n");
$smtp->dataend();
$smtp->quit();
}

sub send_mail_up {
#my($to, $from, $subject, @body) = @_;
$to = "[EMAIL PROTECTED]";
$subject = "$ip up";
$from = "[EMAIL PROTECTED]";
$body = "Reason:\n\nHost reachable at $var";

use Net::SMTP;

my $relay = "localhost";
my $smtp = Net::SMTP->new($relay) || die "Can't open mail connection: $!";

    $smtp->mail($from);
    $smtp->to($to);

    $smtp->data();
    $smtp->datasend("To: $to\n");
    $smtp->datasend("From: $from\n");
    $smtp->datasend("Subject: $subject\n");
    $smtp->datasend("\n");
    $smtp->datasend("$body\n");
    $smtp->dataend();
    $smtp->quit();
}
##################################################################

open(STATS,">routerstat.log");
$routerstat = 1;
print STATS $routerstat;
close STATS;

open(STDERR,">>./routerping.err.log");

$myproc = Proc::Simple->new();
$timer = 300; 
$pid = 0;

$z = @ARGV;
if ($z == 0) [EMAIL PROTECTED] = ""};
if ($ARGV[0] eq "start") { #print "My pid is: $$\n"; 
$myproc->start(\&runonce)}
elsif ($ARGV[0] eq "status") { status() }
elsif ($ARGV[0] eq "stop") { killdemon() }
else {print "Use routerping {start,stop,status}\n";}

sub killdemon {
print "Stoping routerping daemon\n";
close OUT; 
$pid = "";
if (open (PID,"routerping.pid") == 0) {print "The program is not running\n";exit;} ;
$pid = <PID>;
close(PID);
print `rm routerping.pid`;
$myproc->start("kill",$pid);
print "                                  [done]\n";
}


sub status {
close OUT;

if (open (IN,"routerping.pid") == 0) { print "Programm is not running \n";exit;};

$pid = <IN> ;
print "Running in background with pid $pid\n" ;

close IN;
}


sub runonce {

open (PID,"routerping.pid");

while (<PID>) {
if ($_ ne "") {
print "Running in background with pid $_\n";
print "You can't run the program twice\n";
close (PID);
exit;}
}
print "Starting routerping daemon";
open (PID, ">routerping.pid");
print PID $$;
close (PID);
print "                                 [done]\n";

while (1) {


$loctime = scalar(localtime);
@loctime = split(/ +/,$loctime);

if (open (LINE,"routerping.lin.log")) {
while (<LINE>) {$lastline = $_;};
#$a = $lastline;

}

$myvar = scalar(localtime);
($dayn,$month,$day,$time,$year) = split(/ +/,$myvar);
%myhash = 
("Jan","01","Feb","02","Mar","03","Apr","04","May","05","Jun","06","Jul","07","Aug","08","Sep","09","Oct","10","Nov","11","Dec","12");

%myday = 
("1","01","2","02","3","03","4","04","5","05","6","06","7","07","8","08","9","09","10","10","11","11","12","12","13","13","14","14","15","15","16","16","17","17","18","18","19","19","20","20","21","21","22","22","23","23","24","24","25","25","26","26","27","27","28","28","29","29","30","30","31","31");

($hours,$minute,$sec) = split(/:/, $time);
if ($hours > 12) {
  $hours = $hours - 12
}

$var = "$myhash{$month}-$myday{$day}-$year $hours:$minute:$sec\n";

############ IP ADDRESS HERE ##############
$ip = "192.168.1.130";
###########################################

my $p = Net::Ping->new("icmp");
if ($p->ping($ip)) {
    print "$ip is alive.\n";

    open (LOG,"routerstat.log");
    while (<LOG>) {
    if (/0/) {
       close LOG;
       open (LOG,">routerstat.log");
       $hostinf = 1;
       print LOG $hostinf;
       &send_mail_up;
    }
    else {
       last;
    }
    }
    close LOG;

} else {
    print "$ip is not reachable.\n";

    open (LOG, "routerstat.log");
    while (<LOG>) {
    if (/0/) {
        $hostinf = 0;
        last;
    }
    else {

    $hostinf = 0;
    $hostdown = "routerstat.log";
    open (LOG,">$hostdown");
    print LOG $hostinf;
    close LOG;
    &send_mail_down;
    }
    }
}

sleep($timer);

}
}
--
Philippine Linux Users' Group (PLUG) Mailing List
[EMAIL PROTECTED] (#PLUG @ irc.free.net.ph)
Official Website: http://plug.linux.org.ph
Searchable Archives: http://marc.free.net.ph
.
To leave, go to http://lists.q-linux.com/mailman/listinfo/plug
.
Are you a Linux newbie? To join the newbie list, go to
http://lists.q-linux.com/mailman/listinfo/ph-linux-newbie

Reply via email to