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"; }

In reply to Line monitor by DrHyde

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.