Wednesday, March 16, 2011

Weather Threshold Monitoring Script

Handy script for spring:

#!/usr/bin/perl
#
# Weather Threshold
# Watches for a certain temperature outside, and sends email when met
# Set variable $thresh below to what you want to watch for
# Meant to be run by Cron
# Example Cron Entries:
# 55 8 * * * /bin/rm -f /tmp/tempthresh-met
# */15 9-14 * * * /home/arthur/bin/tempthresh.pl
# 10 14 * * * /bin/rm -f /tmp/tempthresh-met

use Weather::Google;

# set temp threshold
$thresh = 50;

if ( -w "/tmp/tempthresh-met" )
{
        # print "Temp Threshold met, exiting\n";
        exit 0;
}

my $gw;

$gw = new Weather::Google(48423); # Zip code
$gw->language('en');

my $temp_f = $gw->current_conditions('temp_f');
# print "It is $temp_f F degrees\n";

if ( $temp_f > $thresh )
        system("echo | mail -s \"tempthresh.pl reports: temp over $thresh F\" youremail\@domain.com &> /dev/null");
        system("touch /tmp/tempthresh-met");
}