#!/usr/bin/perl -w

=pod

=head1 COPYRIGHT

 
This software is Copyright (c) 2010 NETWAYS GmbH, William Preston
                               <support@netways.de>

(Except where explicitly superseded by other copyright notices)

=head1 LICENSE

This work is made available to you under the terms of Version 2 of
the GNU General Public License. A copy of that license should have
been provided with this software, but in any event can be snarfed
from http://www.fsf.org.

This work 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.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 or visit their web page on the internet at
http://www.fsf.org.


CONTRIBUTION SUBMISSION POLICY:

(The following paragraph is not intended to limit the rights granted
to you to modify and distribute this software under the terms of
the GNU General Public License and is only of importance to you if
you choose to contribute your changes and enhancements to the
community by submitting them to NETWAYS GmbH.)

By intentionally submitting any modifications, corrections or
derivatives to this work, or any other work intended for use with
this Software, to NETWAYS GmbH, you confirm that
you are the copyright holder for those contributions and you grant
NETWAYS GmbH a nonexclusive, worldwide, irrevocable,
royalty-free, perpetual, license to use, copy, create derivative
works based on those contributions, and sublicense and distribute
those contributions and any derivatives thereof.

Nagios and the Nagios logo are registered trademarks of Ethan Galstad.

=head1 NAME

check_pcmeasure_combined

=head1 SYNOPSIS

Retrieves the status of a PCMeasure (MessPC) device

=head1 OPTIONS

check_pcmeasure_combined [options] <hostname> <SNMP community>

=over

=item   B<--temp-warning>

temperature warning level (default 30)

=item   B<--temp-critical>

temperature critical level (default 50)

=item   B<--hum-warning>

humidity max warning level (default 70)
e.g. 70:80 will warn if the level is outside this range

=item   B<--hum-critical>

humidity max critical level (default 90)

=item   B<--timeout>

how long to wait for the reply (default 30s)

=back

=head1 DESCRIPTION

This plugin checks the status of a MessPC Ethernetbox using SNMP

It doesn't require the BETTER-NETWORKS-ETHERNETBOX-MIB (the OIDs are
hardcoded into the script)

Note that ranges in the format x:y can also be given for the thresholds.

Also be aware that the text strings returned by the hardware do not always
exactly agree with the values i.e. 44.0 %RH vs. 43.9

=cut

use Getopt::Long;
use Pod::Usage;
use Net::SNMP;
use Data::Dumper;

my $tempwarning = 30;
my $tempcritical = 50;
my $humwarning = 70;
my $humcritical = 90;
my $tout = 30;
my $version;

my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3, 'DEPENDENT' => 4);

my %OIDS = (	'tempunit.0'			=>	'.1.3.6.1.4.1.14848.2.1.1.3.0',
		'sensorTable'			=>	'.1.3.6.1.4.1.14848.2.1.2',
		'sensorindex'			=>	'.1.3.6.1.4.1.14848.2.1.2.1.1',
		'name'				=>	'.1.3.6.1.4.1.14848.2.1.2.1.2',
		'sensortype'			=>	'.1.3.6.1.4.1.14848.2.1.2.1.3',
		'valueint'			=>	'.1.3.6.1.4.1.14848.2.1.2.1.4',
		'valueint10'			=>	'.1.3.6.1.4.1.14848.2.1.2.1.5',
		'valuestr'			=>	'.1.3.6.1.4.1.14848.2.1.2.1.6',
		'valid'				=>	'.1.3.6.1.4.1.14848.2.1.2.1.7',
		'lowlimit'			=>	'.1.3.6.1.4.1.14848.2.1.2.1.8',
		'highlimit'			=>	'.1.3.6.1.4.1.14848.2.1.2.1.9',
		'hysteresis'			=>	'.1.3.6.1.4.1.14848.2.1.2.1.10',
		'status'			=>	'.1.3.6.1.4.1.14848.2.1.2.1.11',
);

my %OIDS_rev = reverse %OIDS;


my %tempMap = (		0	=>	'C',
			1	=>	'F',
			2	=>	'K');


my %typeMap = (		0	=>	['no sensor', ''],
			1	=>	['temperature', 'X'],
			2	=>	['brightness', 'Lux'],
			3	=>	['humidity', '%RH'],
			4	=>	['switch contact', ''],
			5	=>	['voltage detector', ''],
			6	=>	['smoke sensor', '']);


# check the command line options
GetOptions('help|?' => \$help,
	   'V|version' => \$version,
           't|timeout=i' => \$tout,
           'w|tempwarn|temp-warning=s' => \$tempwarning,
           'c|tempcrit|temp-critical=s' => \$tempcritical,
           'humwarn|hum-warning=s' => \$humwarning,
           'humcrit|hum-critical=s' => \$humcritical);

if ($#ARGV!=1) {$help=1;} # wrong number of command line options
# pod2usage( -verbose => 99, -sections => "NAME|COPYRIGHT|SYNOPSIS|OPTIONS") if $help;
pod2usage(1) if $help;

my $host = shift;
my $community = shift;

$SIG{'ALRM'} = sub { nagexit('CRITICAL', "Timeout trying to reach device $host") };

alarm($tout);

my ($session, $error) = Net::SNMP->session(
	-hostname	=>	$host,
	-community	=>	$community);
if (!defined($session)) { nagexit('CRITICAL', "Failed to reach device $host") };

my $result = $session->get_request(
	-varbindlist	=>	[$OIDS{'tempunit.0'}]);
if (!defined($result)) { nagexit('CRITICAL', "Failed to query device $host") };


$typeMap{1}[1] = $tempMap{$result->{$OIDS{'tempunit.0'}}};

$result = $session->get_table(
	-baseoid	=>	$OIDS{'sensorTable'});
if (!defined($result)) { nagexit('CRITICAL', "Failed to query device $host") };


my @tablearr;

# put the table into an array for easier lookup
foreach my $oid (keys %$result) {

	# remove the index from oid
	my @tmparr = split(/\./, $oid);
	my $index = pop(@tmparr);
	my $base = join('.', @tmparr);

	if (exists($OIDS_rev{$base}))
	{
		$tablearr[$index]{ $OIDS_rev{$base} }  = $result->{$oid};
		if (($OIDS_rev{$base} eq 'valuestr') and ($result->{$oid} =~ /^0x/))
		{
			# latin1 hex-encoded?
			$tablearr[$index]{ $OIDS_rev{$base} }  = pack 'H*', $result->{$oid};
		}
	}

}

my $totalcrit = 0;
my $totalwarn = 0;
my @messages;
my @extended_output;
my @perfdata;


# print the values
foreach my $index (@tablearr) {

	my $outline;
	my $crit = 0; 
	my $warn = 0;
	my ($warning, $critical);

	next unless (defined($index->{valid}) and ($index->{valid} == 1));


	my $value = 0;

	if (defined($index->{valueint10}))
	{
		$value = $index->{valueint10} / 10;
	} elsif (defined($index->{valueint})) {
		$value = $index->{valueint};
	}

	if ($index->{sensortype} eq 1)
	{
		$warning = $tempwarning;
		$critical = $tempcritical;
	}
	elsif ($index->{sensortype} eq 3)
	{
		$warning = $humwarning;
		$critical = $humcritical;
	}
	else
	{
		$warning = undef;
		$critical = undef;
	}

	# TODO: check against lowlimit / highlimit / hysteresis
	if (defined($index->{status}) and ($index->{status} != 0 and $index->{status} != 2))
	{
		$totalcrit++;
		$crit = 1;
		push @messages, $index->{name}." has reached an error level";
	} elsif (outside($value, $critical)) {
		$totalcrit++;
		$crit = 1;
		push @messages, $index->{name}." value of $value exceeds $tempcritical";
	} elsif (outside($value, $warning)) {
		$totalwarn++;
		$warn = 1;
		push @messages, $index->{name}." value of $value exceeds $tempwarning";
	};

	if ($crit) {
		$outline = "[CRITICAL] ";
	} elsif ($warn) {
		$outline = "[WARNING] ";
	} else {
		$outline = "[OK] ";
	}

	$outline .= $index->{name}."(".$typeMap{$index->{sensortype}}[0]."): ".$index->{valuestr};
	push @extended_output, $outline;


	# add the perfdata
	my $perfdata = "sensor_".$index->{sensorindex}."=".$value.$typeMap{$index->{sensortype}}[1].";";
	$perfdata .= $warning.";";
	$perfdata .= $critical.";";
	$perfdata .= "0;";


	push @perfdata, $perfdata;

}


if ($#messages > -1) {
	print join(", ", @messages);
} else {
	print "No problems found";
}
print " | ";
print join(" ", @perfdata);
print "\n";
print join("\n", @extended_output);
print "\n";


if ($totalcrit) {
	exit 2;
} elsif ($totalwarn) {
	exit 1;
} else {
	exit 0;
}

sub testRange
{
	my ($test, $rangewarn, $rangecrit) = @_;

	return 2 if (outside($test, $rangecrit));
	return 1 if (outside($test, $rangewarn));
	return 0;
}


sub outside
{
	my ($value, $range) = @_;

	return 0 if (!defined($range) or !defined($value));

	if ($range =~ /(\d*)[:-](\d*)/)
	{
		# outside the range a:b is an error
		return 1 if (($value < $1) or ($value > $2))
	}
	else
	{
		# above the value x is an error
		return 1 if ($value > $range);
	}
	return 0;
	
}

sub nagexit
{
	my $errlevel = shift;
	my $string = shift;

	if (defined($session)) { $session->close };

	print "$errlevel: $string\n";
	exit $ERRORS{$errlevel};
}

