#!/usr/bin/perl
# Version 2.0 - Original Script by: Ton Voon, 2006
# See http://www.nagioscommunity.org for more details
#
# Version 2.1 by b.rosner@wurzelwerk.it on 10052012
# Changed to use Weather::Google
#
# Version 2.2 by b.rosner@wurzelwerk.it on 12052012
# Added support for Weather condition checks
#
# This script is available under GPLv2
#
# You will need to set the permissions of the script so it is executable
use strict;
use encoding 'utf8';

# Get these plugins from CPAN
use Nagios::Plugin;
use Weather::Google;


my $np = Nagios::Plugin->new( 
	shortname => "FORECAST",
	usage => "Usage: %s [-m] [-v] [-w] [-c] [-l]",
	 );

$np->add_arg(
        spec => "mode|m=s",
        help => "-m, --mode=temp|cond. Can be either temp for Temparature or cond for rain/storm warning",
        );

$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 in Googles Weather-API",
	required => 1,
	);

$np->add_arg(
        spec => "days|d=f",
        help => "-d, --days=REAL_NUMBER. How many days in the future - Maximum=3",
        default => 3,
        required => 1,
        );

$np->getopts;

#
# Google Weather API only supports 3 days upfront
#
my $days;
if ($np->opts->days gt "3") {
	$np->nagios_die(
        	message => "Error, days must be in Range 1..3",
        );
} else {
	$days = $np->opts->days;
}

#
# Start Weather Plugin and dexlare german localization
#
my $weather = new Weather::Google($np->opts->location,{language => 'de', encoding => 'latin1'});

#
# Decide, which mode to use
#
my $mode = $np->opts->mode;
if ($mode eq "temp") {
	run_temp();
} elsif ($mode eq "cond") {
	run_cond();
} else {
        $np->nagios_die(
                message => "Error, mode must be either \"temp\" or \"cond\".",
        );

}

sub run_cond {
	#
	# Experimental stuff -> Rainforecast 
	#
	my $icon = $weather->forecast($days,'icon');
	my $condition = $weather->forecast($days,'condition');

	#
	# uncomment for debug
	#
	#print "icon \= $icon \& cond \= $condition";
	
	#
	# Check for Rain / Storm / Snow and exit
	#
        if ( ($icon =~ (/chance_of_rain/)) || ($icon =~ (/chance_of_storm/)) || ($icon =~ (/chance_of_snow/)) ) {
                $np->nagios_exit(
                        return_code => "1",
                        message => "\"".$condition."\" in ".$days." days in ".$np->opts->location." ",
                );
	} elsif ( ($icon =~ (/rain/)) || ($icon =~ (/storm/)) || ($icon =~ (/snow/)) || ($icon =~ (/thunderstorm/)) ) {
	        $np->nagios_exit(
	                return_code => "2",
	                message => "\"".$condition."\" in ".$days." days in ".$np->opts->location." ",
        	);
	} else {
                $np->nagios_exit(
                        return_code => "0",
                        message => "\"".$condition."\" in ".$days." days in ".$np->opts->location." ",
                );

	};
}

sub run_temp {
	#
	# Check if Weather is OK
	#
	my $threshold = $np->set_thresholds( warning => $np->opts->warning, critical => $np->opts->critical );

	#
	# Get the Forecast
	#
	my $celsius = $weather->forecast($days,'high');

	#
	# Add Perfdata
	#
	$np->add_perfdata(
		label => "celsius",
		value => $celsius,
		threshold => $threshold,
		uom => "", # Bug in Nagios::Plugin version 0.15
	);

	#
	# Construct output
	#
	$np->nagios_exit( 
		return_code => $np->check_threshold($celsius),
		message => "Temperature will be ".$celsius." degrees Celsius in ".$np->opts->days." days in ".$np->opts->location,
	);

}
