#!/usr/bin/perl -w

# COPYRIGHT:
#
# This software is Copyright (c) 2009 NETWAYS GmbH, Tobias Redel
#                                
#
# (Except where explicitly superseded by other copyright notices)
#
#
# 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_rittal_cmc.pl - a nagios plugin for rittal cmd unit

=head1 SYNOPSIS

check_rittal_cmc.pl -H <host>
		    -C <community>
                    [-w <warning temperature>]
                    [-c <critical temperature>]
                    [-v] []
                    [-h]
                    [-V]

a nagios plugin for rittal cmd unit

=head1 OPTIONS

=over

=item -H|--host 

IP address or DNS name of target host

=item -C|--community

SNMP community

=item -w|--warning <warning temperature>

Warning, if temperature is higher than <warning temperature>

=item -c|--critical <critical temperature>

Critical, if temperature is higher than <critical temperature>

=item -v|--verbose 

Verbose mode. If no logfile is specified, verbose output will be printend to STDOUT

=item -h|--help

print help page

=item -V|--version

print plugin version

=cut

use strict;
use Getopt::Long qw(:config no_ignore_case bundling);
use Pod::Usage;
use Net::SNMP;

# version string
my $version = '0.1';

# define states
our @state = ('OK', 'WARNING', 'CRITICAL', 'UNKNOWN');

# get command-line parameters
my ($optHostname, $optCommunity, $optWarning, $optCritical, $optVerbose, $optHelp, $optVersion);

GetOptions(
	"H|host=s"	=> \$optHostname,
	"C|community=s" => \$optCommunity,
	"w|warning=s"	=> \$optWarning,
	"c|critical=s"	=> \$optCritical,
        "v|verbose:s"   => \$optVerbose,
        "h|help"        => \$optHelp,
        "V|version"     => \$optVersion,
);



# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# help and version page
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #

# should print version?
if (defined $optVersion) { print $version."\n"; exit 0; }

# should print help?
if ($optHelp || !$optHostname || !$optCommunity) { pod2usage(1); }



# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# let's go!
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #

# define vars
my $MIBS;
my @UNITS = (3, 4, 5, 6);
my $OID;
my $result;
my $perfdata;
my $output = 'Sensors: ';
my $exitCode = 0;

# define MIBS
$MIBS->{STATE} = '.1.3.6.1.4.1.2606.4.2.1.0';
$MIBS->{UNIT}  = '.1.3.6.1.4.1.2606.4.2';

# auto fill
$optWarning  = 100 if !defined $optWarning;
$optCritical = 100 if !defined $optCritical;

# create snmp connection
my $session = SNMPcreateSession($optHostname, $optCommunity, 1);

# first of all... get cmc's state
$result = $session->get_request(-varbindlist => [$MIBS->{STATE}]);
LeavePlugin(2, 'Device is not ready') if $result->{$MIBS->{STATE}} == 1;

# get units
foreach my $unit (@UNITS) {
	$OID = $MIBS->{UNIT}.'.'.$unit.'.2.0';

	# get only active units
	$result = $session->get_request(-varbindlist => [$OID]);
	if ($result->{$OID} !~ /not avail/) {
		# set unit name
		my $unit_name = $result->{$OID};
		$unit_name =~ s/ //g;

		# get number of unit's slots
		$OID = $MIBS->{UNIT}.'.'.$unit.'.5.1.0';
		$result = $session->get_request(-varbindlist => [$OID]);
		my $max_slots = $result->{$OID};

		# get sonsor type of each slot
		my $counter = 1;
		while($counter <= $max_slots) {
			$OID = $MIBS->{UNIT}.'.'.$unit.'.5.2.1.3.'.$counter;
			$result = $session->get_request(-varbindlist => [$OID]);

			# proceed only temperature sensors
			if ($result->{$OID} =~ /Temperatur/) {
				# get temperature
				$OID = $MIBS->{UNIT}.'.'.$unit.'.5.2.1.5.'.$counter;
				$result = $session->get_request(-varbindlist => [$OID]);

				# output
				$output .= "$unit_name $counter = $result->{$OID}; ";	

				# perfdata
				$perfdata .= "'$unit_name $counter'=$result->{$OID};$optWarning;$optCritical;0;100 ";

				# decide exit code
				$exitCode = 1 if $result->{$OID} >= $optWarning;
				$exitCode = 2 if $result->{$OID} >= $optCritical;
			}

			$counter++;
		}
	}
}

# exit
LeavePlugin($exitCode, $output."|".$perfdata);



# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# functions...
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #

# verbose mode
sub beVerbose {
        my $type = shift;
        my $text = shift;

        if (defined $optVerbose) {
                # generate message
                my $message = localtime(time)." | Verbose: $type: $text\n";

                # should write log to file or STDOUT?
                if ($optVerbose ne "") {
                        open(LOGF,">>$optVerbose") || die $!;
                        print LOGF $message;
                        close(LOGF);
                } else {
                        print $message;
                }
        }
}

sub LeavePlugin {
        my $exitCode = $_[0];
        my $comment  = $_[1];

        print $state[$exitCode]." - $comment\n";
        exit $exitCode;
}

sub SNMPcreateSession {
        my $hostname  = shift;
        my $community = shift;
        my $version   = shift;
        my $timeout   = 10;
        my ($session, $error) = Net::SNMP->session(
                                -hostname      => $hostname,
                                -community     => $community,
                                -version       => $version,
                                -timeout       => $timeout
                                );

        return $session;
}
