Monitoring with GeekTool 3 and Perl on a Mac

I was running into issues where I was having power outages at my house.  My  IP address at home was changing a lot during due to the outages.  I wanted to keep track of the address changes as well as the outages.  I threw together a little script that keep track of things.

This script assumes a few things; that you have a dynamic DNS service running on your home router, as well as ping and host installed with the outputs structured as follows:

Me@io:~ $ ping -c1 www.reeleysoft.com
PING www.reeleysoft.com (69.50.219.52): 56 data bytes
64 bytes from 69.50.219.52: icmp_seq=0 ttl=46 time=66.705 ms
--- www.reeleysoft.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 66.705/66.705/66.705/0.000 ms
Me@io:~ $ host www.reeleysoft.com
www.reeleysoft.com has address 69.50.219.52

Here is a copy of the script.

#!/usr/bin/perl -w
use strict;
###############################################################################
# Written by: Josh Reeley (3/20/2010) reeley AT reeleysoft DOT com
# ipcheckup.pl  Version 1.0
# This checks to see if the IP address is the same and if it isn't then it
# tells me, and then checks to see if it gets an alive ping response.
###############################################################################

my $hostname = "www.reeleysoft.com"; # The Host to monitor
my $hostP = "/usr/bin/host";  # The path to host
my $hipFile = "/Users/Me/ipcheckup.txt";  # The location of the ip file
my $pingP = "/sbin/ping -c1"; # The path to ping
my $timestamp = `date +"%Y-%b-%d"`; # Grabbing current date
my $validDate;

#
# Get the old IP from a stored file
#
my $oldfile = `cat $hipFile`;
chomp($oldfile);
my ($oldhip, $olddate) = split(/,/,$oldfile);

#
# Get the current registered IP from dns
#
my $newhip = `$hostP $hostname`;
$newhip =~ s/$hostname\shas\saddress\s//;
chomp($newhip);

#
# Check to see if the IP address has been updated
#
if ($oldhip eq $newhip) {
        $validDate = $olddate;
} else {
        # Write out the updated up address with a timestamp
        open (IPFILE, ">$hipFile");
        print IPFILE "$newhip,$timestamp";
        close (IPFILE);
        $validDate = $timestamp;
}

#
# Run the ping test
#
my $isAlive = `$pingP $newhip`;

#
# Check to see how the ping response works out
#
if ($isAlive =~ m/.*\s0\.0%\spacket\sloss.*/g) {
        print "Success\n$validDate";
} else {
        print "Failure\n$validDate";
}

Here is what the script will output:

Me@io:~ $ ./ipcheckup.pl
Success
2011-Mar-05

The “Success” means that host was reachable (by ping) and the date states when the IP address last changed.  You can run this in a cron, but I think the way that I have chosen to do it seems a bit more interesting.  There is a program for the mac that lets you keep tabs on things by displaying information on your desktop.  It’s called GeekTool.

I setup GeekTool to run this script every 10 minutes and check on things.  Here is a screenshot of the settings:

Properties

Here is what the result looks like:

GeekTool Success

There are countless things you can monitor using GeekTool check out this interesting LifeHacker post for more ideas.

 

Be Sociable, Share!

Leave a Reply