#!/usr/bin/perl
# Copyright: Ton Voon, 2006
#
# This is the script used in the Nagios Plugin lightning talk
# given by Ton Voon at FOSDEM 2007.
# See http://www.nagioscommunity.org for more details
#
# This script is available under GPLv2, though the ideas are
# public domain :)
#
# You will need to set the permissions of the script so it is executable

# Get these plugins from CPAN
use Nagios::Plugin;
use Weather::Com::Simple;


chdir "/tmp";	# For Weather::Com's cache files. Nagios is run in /

$np = Nagios::Plugin->new( 
	shortname => "WEATHER",
	usage => "Usage: %s [-v] [-w] [-c] [-l]",
	 );

$np->add_arg(
	spec => "warning|w=s",
	help => "-w, --warning=INTEGER:INTEGER. Warning if celsius temperature outside INTEGER:INTEGER",
	);

$np->add_arg(
	spec => "critical|c=s",
	help => "-c, --critical=INTEGER:INTEGER. Critical if celsius temperature outside INTEGER:INTEGER",
	);

$np->add_arg(
	spec => "location|l=s",
	help => "-l, --location=place. Tries to find this location at weather.com",
	required => 1,
	);

$np->getopts;

# You will need to register with weather.com to get a 
# partner_id and license key, then replace values below
# See http://www.weather.com/services/xmloap.html
my $weather = Weather::Com::Simple->new(
	partner_id => "$ENV{PARTNER_ID}",
	license => "$ENV{LICENSE}",
	place => $np->opts->location,
	language => "en",
	);
$info = $weather->get_weather();
# To see complete data, uncomment next two lines
# use Data::Dumper;
# print Dumper $info;
$celsius = $info->[0]->{celsius};
$place = $info->[0]->{place};

$threshold = $np->set_thresholds( warning => $np->opts->warning, critical => $np->opts->critical );
$np->add_perfdata(
	label => "celsius",
	value => $celsius,
	threshold => $threshold,
	uom => "", # Bug in Nagios::Plugin version 0.15
	);

$np->nagios_exit( 
	return_code => $np->check_threshold($celsius),
	message => "Temperature $celsius degrees celsius at ".$place,
	);
