#!/usr/bin/perl

###################################################################
#
# VIM SETTINGS
# ts=3
# sw=3
#
# #################################################################

###################################################################
#	check_senturion - check the SensaTromics SenTurions Sensors
#
#	License Information:
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	To get a copy of the GNU General Public License, write to the Free
#	Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
####################################################################

use strict;
use LWP::UserAgent;
use XML::Simple;
use Data::Dumper;
use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS);
use Getopt::Long;

####################################################################
# var declaration

my $ua = LWP::UserAgent->new;
my $response;
my $host;
my $version="20080919";
my $help;
my $warning;
my $critical;
my $probe;
my $dump;
my $internal_tmp;
my $internal_hum;
my $internal;
my $value;
my $unit="Grad Celsus";
my $verbose;

####################################################################
# get options
#

GetOptions(
	"h"		=>	\$help,				"help"			=>	\$help,
	"w:i"		=>	\$warning,			"warning"		=>	\$warning,
	"c:i"		=>	\$critical,			"critical"		=>	\$critical,
	"p:i"		=>	\$probe,				"probe"			=>	\$probe,
	"d"		=>	\$dump,				"dump"			=>	\$dump,
	"i:i"		=>	\$internal,			"internal"		=>	\$internal,
	"H:s"		=>	\$host,				"host"			=>	\$host,
	"v"		=>	\$verbose,			"verbose"		=>	\$verbose);

####################################################################
# action
#
$response = $ua->get("http://$host/xmldata");
if ($dump){
	print_dump();
}
elsif (($internal || $probe) && ($warning && $critical)){
	check_probe();
}
else{
	print_help();
}

###################################################################
# funktions
#

sub print_help(){
	print "\n\nVersion: $version\n";
	print "Autor: Daniel Bierstedt [d.bierstedt\@crh-group.com]\n\n";
	print "This plugin checks the sensors of SensaTronics SenTurion\n";
	print "by analyzing the xml output.\n";
	print "\n\n";
	print "options:\n";
	print "-d|--dump:\t\tprint all xml data\n";
	print "-i|--internal:\t\tcheck internal sensors\n";
	print "\ttemp: 1\n";
	print "\tlux: 3\n";
	print "\thumidity: 2\n";
	print "-p|--probe:\t\tcheck external sensors\n";
	print "-w|--warning:\t\twarning value\n";
	print "-c|--critical:\t\tcritical value\n";
	print "-v|--verbose:\t\tprint verbose output\n";
	print "-h|--help:\t\tprint this help message\n\n";
	print "Examples:\n";
	print "check internal temp:\n";
	print "./check_senturion -i 1 -w 20 -c 30\n\n";
	print "check internal light sensor:\n";
	print "./check_senturion -i 3 -w 10 -c 30\n";
	print "(does warning and critical make sense here?)\n\n";
	print "check internal humidity sensor:\n";
	print "./check_senturion -i 2 -w 40 -c 60\n\n";
	print "check external sensor:\n";
	print "./check_senturion -p 2 -w 20 -c 30\n\n";
	print "Note: Ive got only temp sensors on external ports...\n\n";
}

sub print_dump(){
	if($response->is_success){
		my $xml = new XML::Simple;
		my $data = $xml -> XMLin($response -> content);
		print Dumper($data)
			or die "nope...";
	}
}

sub check_probe(){
	if($response->is_success){
		my $xml = new XML::Simple;
		my $data = $xml -> XMLin($response -> content)
			or die $response->status_line;

		if($verbose)
		{
			print "verbose:\n\n";
			print "internal: $internal\n";
			print "probe: $probe\n";
			print "warning: $warning\n";
			print "critical: $critical\n";
		}

		if($internal){

			if($internal==2){
				$unit="% rel. Luftfeuchte";
				if($verbose)
				{
					print "internal hum: $internal\n";
				}
			}

			elsif($internal==3){
				$unit="Lux";
				if($verbose)
				{
					print "internal lux: $internal\n";
				}
			}

			else{
				$unit="Grad Celsus";
				if($verbose)
				{
					print "internal tmp: $internal\n";
				}
			}

###########################################################################
# query data from sensors
#

			$value = $data->{Group}->{'9'}->{Probe}->{$internal}->{Value};
			if($verbose)
			{
				print "value: $value\n";
			}
		}

		if($probe){
			$value = $data->{Group}->{$probe}->{Probe}->{Value};

			if($verbose)
			{
				print "value: $value\n";
			}
		}
#
###########################################################################

	}

	if($value>=$critical){
		print "Critical: $value $unit|$unit=$value;$warning;$critical";
		exit $ERRORS{'CRITICAL'};
	}

	elsif($value>=$warning){
		print "Warning: $value $unit|$unit=$value;$warning;$critical";
		exit $ERRORS{'WARNING'};
	}

	elsif($value<$warning){
		print "OK: $value $unit|$unit=$value;$warning;$critical";
		exit $ERRORS{'OK'};
	}

	else{
		print "\noutput unknown. DO SOMETHING!\n\n";
		exit $ERRORS{'UNKNOWN'};
	}
}

