#!/usr/bin/perl -w
#
# check_qtm-i6k.pl - nagios plugin for Scalar i2000/i6000 tape libraries
#
# Ralf Cremer <ralf@cremer-web.de>
#
# Based on check_adic.pl by
# Martin Niedworok (martin.niedworok@gmx.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, USA.
#

use strict;
use Getopt::Long;
use File::Basename;
use Net::SNMP;

my $PROGNAME = basename($0);
my $ARG_H = "";
my $ARG_C = "publicCmtyStr";
my $ARG_help;
my @status;

# Getting options
Getopt::Long::Configure('bundling');
GetOptions(
    "host|H=s" 		=> \$ARG_H,
    "community|C=s" 	=> \$ARG_C,
	"-help"        	=> \$ARG_help,
	"h"            	=> \$ARG_help
);

if ( !$ARG_H || $ARG_help ) {
	print_usage();
}

#######################################################
#
# 1.3.6.1.4.1.3764.1.1.200.20.90.10.1.2.8.0.0.0.0.0.1.0.59
# ADIC-MANAGEMENT-MIB::numLogicalLibraries.'........'.59 = INTEGER: 3
#
#######################################################


# Getting SNMP output
my($session, $error) = Net::SNMP->session(
	-hostname	=>	$ARG_H,
	-community	=>	$ARG_C
);

my %oids = (
	Connectivity	=> '.1.3.6.1.4.1.3764.1.1.200.20.100.10.1.2.8.0.0.0.0.0.1.0.59.1',
	Control		=> '.1.3.6.1.4.1.3764.1.1.200.20.100.10.1.2.8.0.0.0.0.0.1.0.59.2',
	Cooling		=> '.1.3.6.1.4.1.3764.1.1.200.20.100.10.1.2.8.0.0.0.0.0.1.0.59.3',
	Drives		=> '.1.3.6.1.4.1.3764.1.1.200.20.100.10.1.2.8.0.0.0.0.0.1.0.59.4',
	Power		=> '.1.3.6.1.4.1.3764.1.1.200.20.100.10.1.2.8.0.0.0.0.0.1.0.59.5',
	Robotics	=> '.1.3.6.1.4.1.3764.1.1.200.20.100.10.1.2.8.0.0.0.0.0.1.0.59.6',
	PhyOnline	=> '.1.3.6.1.4.1.3764.1.1.200.20.80.10.1.5.8.0.0.0.0.0.1.0.59',
	RobotReady	=> '.1.3.6.1.4.1.3764.1.1.200.20.80.10.1.6.8.0.0.0.0.0.1.0.59',
);
#rc#	numLogLib	=> '.1.3.6.1.4.1.3764.1.1.200.20.90.10.1.2.8.0.0.0.0.0.1.0.59',

my %subsystems = (
	1	=>	'Connectivity',
	2	=>	'Control',
	3	=>	'Cooling',
	4	=>	'Drives',
	5	=>	'Power',
	6	=>	'Robotics',
	7	=>	'PhyOnline',
	8	=>	'RobotReady',
);
#rc#	9	=>	'numLogLib',

my %prio = (
	2	=>	'1',
	7	=>	'2',
	3	=>	'3',
	4	=>	'4',
	6	=>	'5',
	5	=>	'6',
	1	=>	'7',
	8	=> 	'8',
);
#rc#	9	=>	'9',

my $result = $session->get_request(
	-varbindlist => [$oids{Connectivity},
			$oids{Control},
			$oids{Cooling},
			$oids{Drives},
			$oids{Power},
			$oids{Robotics},
			$oids{PhyOnline},
			$oids{RobotReady},
]);
#rc#			$oids{numLogLib}]

$session->close;

my %resulthash = %$result;

my $key;
my @keys = sort keys %resulthash;

for (my $j = 0; $j < 8; $j++) {$status[$j] = ""};

# Putting the checked systems in a status array
my $i = 1;
foreach $key (@keys) {
	$_ = $resulthash{$key};
	$status[$prio{$_}] .= $i; 
	$i++;
}

# Iterating over the status array and searching the highest prio
if($status[1] ne "") {
	myexit('CRITICAL', "failed subsystems: " . build_output($status[1]) . " | 'Library status'=0;;;0;1");
} elsif($status[2] ne "") {
	myexit('CRITICAL', "invalid subsystems: " . build_output($status[2]) . " | 'Library status'=0;;;0;1");
} elsif($status[3] ne "") {
	myexit('WARNING', "degraded subsystems: " . build_output($status[3]) . " | 'Library status'=0.5;;;0;1");
} elsif($status[4] ne "") {
	myexit('WARNING', "warning subsystems: " . build_output($status[4]) . " | 'Library status'=0.5;;;0;1");
} elsif($status[5] ne "") {
	myexit('UNKNOWN', "unknown subsystems: " . build_output($status[5]) . " | 'Library status'=0;;;0;1");
} elsif($status[6] ne "") {
	myexit('OK', "informational subsystems: " . build_output($status[6]) . " | 'Library status'=1;;;0;1");
} elsif($status[7] ne "") {
	myexit('OK', "all subsystems ok. | 'Library status'=1;;;0;1");
}

# Getting the subsystems names into an output string
sub build_output {
	my @input = split(//, $_[0]);
	my $output;
	while (@input) {
		$output .= $subsystems{shift(@input)};
		$output .= " ";
	}
	return $output;
}

sub print_usage {
	print <<EOU;
    The check will get the Overall System Status 
    of Scalar i2000 / i6000 tape libraries.
    Additionaly status of physical library and 
    robotics readiness is also checked.
 
    Usage: $PROGNAME [ -H host ] [ -C SNMP-community ]

    Options:

    -H --host
        Hostname 
    -C --community
        SNMP-community
EOU

	myexit( 'UNKNOWN', $PROGNAME );
}

sub myexit {

#$STATE_OK=0;
#$STATE_WARNING=1;
#$STATE_CRITICAL=2;
#$STATE_UNKNOWN=3;
#$STATE_DEPENDENT=4;

	my $time;
	my $date;

	my ( $state, $text ) = @_;

	my %STATUS_CODE =
	  ( 'UNKNOWN' => '-1', 'OK' => '0', 'WARNING' => '1', 'CRITICAL' => '2', 'DEPENDENT' => '4' );

	my $out = $state;
	$out .= " - ";
	$out .=  $text;
	$out .= chr(10);

	print $out;

	exit $STATUS_CODE{$state};
}

