I like to know when and for how long my ADSL line is down. Sadly, I can't do any SNMP goodness, so I wrote a little perl script which runs every five minutes from a cron job. It tries to ping my ISP's nameserver and records the result. It also emails me whenever the line goes down, when it comes back up, and whenever there's packet loss.
#!/usr/local/bin/perl use warnings; use strict; use GDBM_File; use Fcntl qw(O_CREAT O_RDWR); use Mail::Mailer; tie my %history, 'GDBM_File', "$ENV{HOME}/pinger/history", O_CREAT|O_R +DWR, 0600; my $ping=`ping -c 5 195.112.4.4|grep received`; $ping =~ /(\d+)%/; my $packet_loss = $1; chomp($packet_loss); if($packet_loss != 0) { $history{time()} = $packet_loss; } if(!defined($history{prev_state})) { $history{prev_state}=100; $history{most_recent} = time; } if($packet_loss == 100 && $history{prev_state} != 100) { # connection +gone down email( "Connection died at ".scalar(localtime) ); $history{prev_state} = $packet_loss; $history{most_recent} = time; } elsif($history{prev_state} == 100) { # previous was 10 +0% loss if($packet_loss != 100) { # ... and we're +back up email( "Connection came back up with $packet_loss% pa +cket\n". "loss at ".scalar(localtime).".\n\n". "It went down at ". scalar(localtime($history{most_recent})) ); $history{prev_state} = $packet_loss; $history{most_recent} = time; } } elsif($history{prev_state} == 0) { # previous was 0% + loss if($packet_loss != 0) { # ... now we hav +e lossage email("$packet_loss% packet loss on connection at ".sc +alar(local time)); $history{prev_state} = $packet_loss; $history{most_recent} = time; } } sub email { my $body=shift; my $mailer=new Mail::Mailer; $mailer->open({ To => 'david@cantrell.org.uk', Subject => 'LINEMONITOR report from '.`hostname` }); print $mailer $body; $mailer->close; }
There's also another script which shows me the log:
#!/usr/local/bin/perl use warnings; use strict; use GDBM_File; use Fcntl qw(O_CREAT O_RDWR); use Data::Dumper; tie my %history, 'GDBM_File', "$ENV{HOME}/pinger/history", O_CREAT|O_R +DWR, 0600; print "Loss%\tTime\n"; foreach my $key (sort { $a <=> $b } grep { /^\d+$/ } keys %history) { print "$history{$key}\t".scalar(localtime($key))."\n"; }

Replies are listed 'Best First'.
Re: Line monitor
by Mr_Person (Hermit) on Jun 20, 2003 at 14:04 UTC
    Nagios (previously NetSaint) is also good for this sort of thing. You can monitor (many) multiple devices and services on those devices, keep track of when they go down, make pretty graphs of their uptime and email, page, or whatever other plugin you want to write to notify you on different events.