#!/usr/bin/perl -w
#########################################################################
# Script:       check_eternus_lt_tape.pl				#
# Author:       Heino Walther - www.beardmann.dk			#
# Thanks:	This script is based on Claudio Kuenzler's script for	#
#		IBM Library monitoring.					#
# Purpose:      Monitor Fujitsu Eternus LT Tape Libraries		#
# Compatible:	LT40 S2 (Tested) should work on other models		#
# License: 	GPLv2							#
# History:                                                              #
# 20142210	First version 						#
#########################################################################
my $version = '20142210';
#########################################################################
use strict;
use Getopt::Long;
use Net::SNMP;
use Switch;
#########################################################################
# Variable Declaration
my $hostname = '';
my $community = '';
my $model = '';
my $type = '';
my $help = '';
my $oid_base = '';
my $session = '';
my $error = '';
#########################################################################
# User Input
if ( @ARGV > 0 ) {
	GetOptions(
	'H=s' => \$hostname,
	'C:s' => \$community,
	'help' => \$help,
	);
} else { help(); }
#########################################################################
# Check if user asks for help
if ( $help ne '' ) {
	help();
}

# Set community if not set by user
if ( $community eq '' ) {
	$community = "public";
}

#########################################################################
# OID Definition
$oid_base = ".1.3.6.1.4.1.20884";
my $oid_hostname = ".1.3.6.1.2.1.1.5.0"; #
my $oid_uptime = ".1.3.6.1.2.1.1.3.0"; #
my $oid_productname = "$oid_base.10893.2.101.1.2.0"; #  
my $oid_vendorname = "$oid_base.10893.2.101.1.3.0"; #
my $oid_serialnumber = "$oid_base.10893.2.101.4.1.0"; #
my $oid_firmware = "$oid_base.10893.2.101.1.4.0"; #
my $oid_globalstatus = "$oid_base.10893.2.101.2.2.0"; #
my $oid_faulterror = "$oid_base.10893.2.101.2.5.0"; #
my $oid_faultdesc = "$oid_base.10893.2.101.2.5.0"; #
#########################################################################
# Subs
sub help {
print "check_eternus_lt_tape.pl (c) 2014 Heino Walther (published under GPL License)
Version: $version\n
Usage: ./check_eternus_lt_tape.pl -H host [-C community] -t checktype\n
Options: 
-H\tHostname or IP address of tape library.
-C\tSNMP community name (if not set, public will be used).
--help\tShow this help/usage.\n";
exit 0;
}
#########################################################################
# SNMP Connection
($session,$error) = Net::SNMP->session(
			-hostname => $hostname,
			-community => $community,
			-version => 1,
			);
#########################################################################
# Plugin Checks
	my @oidlist = ($oid_hostname, $oid_productname, $oid_vendorname, $oid_serialnumber, $oid_firmware);
	my $result = $session->get_request(-varbindlist => \@oidlist);
	
	if (!defined($result)) {
		printf("ERROR: Description table : %s.\n", $session->error);
		$session->close;
		exit 2;
	}

	$hostname = $$result{$oid_hostname};
	my $vendorname = $$result{$oid_vendorname};
	$vendorname =~ s/\s+$//;
	my $productname = $$result{$oid_productname};
	my $serialnumber = $$result{$oid_serialnumber};
	my $firmware = $$result{$oid_firmware};

#	print "$hostname ($vendorname $productname) - S/N: $serialnumber - running on Firmware $firmware\n";

	@oidlist = ($oid_globalstatus, $oid_faulterror, $oid_faultdesc);
	$result = $session->get_request(-varbindlist => \@oidlist);
        
	if (!defined($result)) {
                printf("ERROR: Description table : %s.\n", $session->error);
                $session->close;
                exit 2;
        }

	my $globalstatus = $$result{$oid_globalstatus};
	my $faulterror = $$result{$oid_faulterror};
	my $faultdesc = $$result{$oid_faultdesc};

	switch ($globalstatus) {
		case 1 { print "WARNING - Current status is: other - $faultdesc (error code: $faulterror)\n"; print "$hostname ($vendorname $productname) - S/N: $serialnumber - running on Firmware $firmware\n"; exit 1; }
		case 2 { print "WARNING - Current status is: unknown - $faultdesc (error code: $faulterror)\n"; print "$hostname ($vendorname $productname) - S/N: $serialnumber - running on Firmware $firmware\n"; exit 1; }
		case 3 { print "OK - Current status is: ok\n"; print "$hostname ($vendorname $productname) - S/N: $serialnumber - running on Firmware $firmware\n"; exit 0; }
		case 4 { print "WARNING - Current status is: non-critical - $faultdesc (error code: $faulterror)\n"; print "$hostname ($vendorname $productname) - S/N: $serialnumber - running on Firmware $firmware\n"; exit 1; }
		case 5 { print "CRITICAL - Current status is: critical - $faultdesc (error code: $faulterror)\n"; print "$hostname ($vendorname $productname) - S/N: $serialnumber - running on Firmware $firmware\n"; exit 2; }
		case 6 { print "CRITICAL - Current status is: non-recoverable - $faultdesc (error code: $faulterror)\n"; print "$hostname ($vendorname $productname) - S/N: $serialnumber - running on Firmware $firmware\n"; exit 2; }
		else { print "UNKNOWN - Unknown status code\n"; exit 3; }
	}
#########################################################################
# Close SNMP Session
$session->close(); 

print "UNKNOWN - The script should have exited before this point\n";
exit 3

