#!/usr/bin/perl -w
#
# check_senturion.pl - Nagios(r) sensatronics senturion plugin
#
# Copyright (c) 2008 NETWAYS GmbH, < http://www.netways.de >
# Author: Bernd Loehlein < bernd.loehlein(at)netways.de >
#
# 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.
#
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307

# Main Stuff...
use strict;
use Net::SNMP;
use vars qw (
    $VERSION
    $COMMUNITY
    $host_address
    $opt_help
    $opt_version
    $warn_level
    $crit_level
    $value
    $type
    $PROGNAME
    $OUT
);


sub print_usage();
sub print_version();
use File::Basename;
use Getopt::Long;

# Main configuration
$VERSION      = "0.01";
$PROGNAME = basename($0);

my $session;
my $error;
my $snmp_version = 1;
$COMMUNITY    = "public";

# Nagios Status Values
my %STATUS_CODE = (
                    'UNKNOWN'  => '-1',
                    'OK'       => '0',
                    'WARNING'  => '1',
                    'CRITICAL' => '2'
);

my @snmpoids;

# SNMP OIDs for Sensatronics Senturion
my $snmpTemperature  = '.1.3.6.1.4.1.16174.1.1.5.2.2.1.0';
my $snmpHumidity = '.1.3.6.1.4.1.16174.1.1.5.2.2.2.0';
my $snmpLight = '.1.3.6.1.4.1.16174.1.1.5.2.2.3.0';
my $snmpAirflow = '.1.3.6.1.4.1.16174.1.1.5.2.2.4.0';

my $response;

# Help/Usage sub...
sub print_usage()
{
 print "Usage: $PROGNAME -H host [ -C community ] -t type -w warn -c crit \n\n";
 print "Options:\n";
 print " -H, --host STRING or IPADDRESS\n";
 print " -C, --community STRING\n";
 print " -t, --type STRING (temperature,humidity,light,airflow)\n";
 print " -w, --warning INTEGER\n";
 print " -c, --critical INTEGER\n";
 print " -V, --version\n";
 print " -h, --help\n";
 print "    Display this screen.\n\n";
 exit($STATUS_CODE{"UNKNOWN"});
}

sub print_version()
{
	print $VERSION."\n";
	exit($STATUS_CODE{"UNKNOWN"});
}

# - Initial arguments parsing
Getopt::Long::Configure('bundling');
GetOptions (
                "H=s"           =>  \$host_address,
                "host=s"          =>  \$host_address,
                
                "C=s"           =>  \$COMMUNITY,
                "community=s"   =>  \$COMMUNITY,
                
                "w=s"           =>  \$warn_level,
                "warning=s"     =>  \$warn_level,
                
                "t=s"		=>  \$type,
                "type=s"	=>  \$type,
  
                "c=s"           =>  \$crit_level,
                "critical=s"    =>  \$crit_level,
                
                "h"             =>  \$opt_help,
                "help"          =>  \$opt_help,

		"V"		=>  \$opt_version,
		"version"	=>  \$opt_version,
           ) or die "Too few arguments. Try '$PROGNAME --help' for more information\n";

# HelpArgument
if ($opt_help) {
    print_usage();
}

if ($opt_version) {
    print_version();
}

# Checking the Arguments
if((!$host_address) or (!$crit_level>0) or (!$warn_level>0) or (!$type) ) 
{ 
    print "Too few arguments. Try '$PROGNAME --help' for more information\n";
    exit($STATUS_CODE{"UNKNOWN"});
}

if ( $snmp_version =~ /[12]/ ) {
	( $session, $error ) = Net::SNMP->session(
		-hostname  => $host_address,
		-community => $COMMUNITY,
		-port      => 161,
		-version   => $snmp_version
	);

	if ( !defined($session) ) {
		print("UNKNOWN: $error");
		exit $STATUS_CODE{'UNKNOWN'};
	}
}
elsif ( $snmp_version =~ /3/ ) {
	my $state = 'UNKNOWN';
	print("$state: No support for SNMP v3 yet\n");
	exit $STATUS_CODE{$state};
}
else {
	my $state = 'UNKNOWN';
	print("$state: No support for SNMP v$snmp_version yet\n");
	exit $STATUS_CODE{$state};
}

push( @snmpoids, $snmpTemperature );
push( @snmpoids, $snmpHumidity );
push( @snmpoids, $snmpLight );
push( @snmpoids, $snmpAirflow );

if ( !defined( $response = $session->get_request(@snmpoids) ) ) {
	my $answer = $session->error;
	$session->close;

	print("WARNING: SNMP error: $answer\n");
	exit $STATUS_CODE{'WARNING'};
}

my $temperature = $response->{$snmpTemperature} / 10;
my $humidity = $response->{$snmpHumidity} / 10;
my $light =  $response->{$snmpLight} / 10;
my $airflow =  $response->{$snmpAirflow} / 10;

if ($type eq 'temperature') {
	$value = $temperature;
} elsif ($type eq 'humidity') {
	$value = $humidity;
} elsif ($type eq 'light') {
	$value = $light;
} elsif ($type eq 'airflow' ) {
	$value = $airflow;
} else {
	print("WARNING: unknown type: $type requested\n");
        exit $STATUS_CODE{'WARNING'};
}

# Making the Pluginoutput
if ($value >= $crit_level) {
    $OUT = "CRITICAL";
} elsif ($value >= $warn_level) {
    $OUT = "WARNING";
} else {
    $OUT = "OK";
}

print $OUT . " $type: " . $value . "\n";

exit ($STATUS_CODE{$OUT});
