Cron: pinging hosts

Simple hacks like this one can be quite useful…

I realised a while ago that it would be a useful thing to check, occasionally, that all the machines I’m responsible for are still up. (This helps to minimise those embarrassing “Oh, I didn’t know there was anything wrong with it” conversations.).

Thus, the following pretty basic perl script, which I run from /etc/crontab on my own desktop every couple of hours:

#!/usr/bin/perl -w
#
# host_ping.pl - run from crontab
use strict;
use Net::Ping;
use Net::SMTP;
sub sendmail;
my $ping  = Net::Ping->new();
my $email = 'me@example.com';
my @host_array = qw/host1 host2 serverA serverB/;
my $hosts_down = "";
foreach my $host (@host_array) {
    unless ($ping->ping($host)) {
        $hosts_down .= "$host ";
    }
}
sendmail() if ($hosts_down ne "");
sub sendmail()
{
    # email to me
    my $s = Net::SMTP->new('mailserver.example.com');
    $s->mail($email);
    $s->to($email);
    $s->data("Subject: Host(s) down: $hosts_down","\n","\n");
    $s->quit;
}

Source: “Cron: pinging hosts” — Linux DevCenter

Other Recent Articles

  • “Project Managers”
    September 17th, 2007
  • Documents/HOWTOs Updated
    September 15th, 2007
  • Procrastination sucks ass
    September 2nd, 2007
  • Best resume/CV idea…ever
    August 28th, 2007
  • One year ago today: “The Great Northfield Hail Storm”
    August 24th, 2007
  • Cyndicate
    August 10th, 2007
  • Numbers and stuff
    August 7th, 2007

Browse the full archive of previous articles...

Recent Photos

View the rest of my photos...