#!/usr/bin/perl -wT
#
# NAGIOS Plugin: check_3ware
# Check the status of the 3ware raid controler with tw_cli
# Author: Marius Hein <mhein@netways.de>
# NETWAYS GmbH, www.netways.de, info@netways.de

# angepasst von Matthias Geerdsen <matthias@atw.goe.net>

$ENV{PATH}="/sbin:/usr/sbin:/bin:/usr/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};


use strict;
use File::Basename;
use Getopt::Long;
use vars qw(
	$opt_unit
	$opt_control
	$opt_help
	$opt_usage
	$opt_warnings
	$opt_oks
	$progname
	@state_ok
	@state_warning
	$return
	$command
	%conf
	$tmp1
	$state_out
	$desc_out
	$exit_out
	);

sub print_help();
sub print_usage();
sub print_warnings();
sub print_okays();

$progname = basename($0);

@state_ok=("OK");
@state_warning=("DEGRADED","OFFLINE");

$conf{'path'} = "sudo /usr/sbin/";
$conf{'bin'} = "tw_cli";
$conf{'cmd_part_first'} = "info";
$conf{'cmd_part_last'} = "status";

Getopt::Long::Configure('bundling');
GetOptions
	(
	"controller=i"	=>	\$opt_control,
	"C=i"		=>	\$opt_control,
	
	"unit=i"	=>	\$opt_unit,
	"U=i"		=>	\$opt_unit,

	"warnings"	=>	\$opt_warnings,
	"W"		=>	\$opt_warnings,

	"okays"		=>	\$opt_oks,
	"O"		=>	\$opt_oks,
	
	"h"		=>	\$opt_help,
	"help"		=>	\$opt_help,
	"usage"		=>	\$opt_usage
	) || die "try '$progname --help' for informations.\n";

if (!$opt_control) { $opt_control=0; }
if (!$opt_unit) { $opt_unit=0; }
if ($opt_control !~ /^(\d+)$/) {
	exit 3
	} 
else {
	$opt_control = $1
	}
	
if ($opt_unit !~ /^(\d+)$/) { 
	exit 3
	}
else {
	$opt_unit = $1
	}


sub print_help() {
 print "\n";
 print "check 3ware >>HELP<<\n";
 print "\n";
 print "\t --help, -h\t\t\t help screen.\n";
 print "\t --usage\t\t\t little usage\n";
 print "\n";
 print "\t --controller, -C\t\t Controller ID\n";
 print "\t --unit, -U\t\t\t Unit ID\n";
 print "\n";
 print "\t --warnings, -W\t\t\t Displays warning Keywords\n";
 print "\t --okays, -O\t\t\t Displays ok Keywords\n";
 print "\n";
}

sub print_usage() {
 print "\n";
 print "check 3ware >>USAGE<<\n";
 print "\n";
 print "\t$progname -C 0 -U 0 (checks Controller 0 Unit 0 for status)\n";
 print "\t$progname -W (Displays all Warning keywords)\n";
 print "\t$progname -O (Displays all OK keywords)\n";
 print "\t$progname --help (Displays the Helpmessage)\n";
 print "\n";
}

sub print_warnings() {
 print "Print Warning-Keywords:\n";
 print "\n";
 foreach (@state_warning) {
  print "$_\n";
 }
}

sub print_okays() {
 print "Print OKAY-Keywords:\n";
 print "\n";
 foreach (@state_ok) {
 print "$_\n";
 }
}

if ($opt_help) {
 print_help();
 exit;
}

if ($opt_usage) {
 print_usage();
 exit;
}

if ($opt_warnings) {
 print_warnings();
 exit;
}

if ($opt_oks) {
 print_okays();
 exit;
}
if ($opt_control >= 0 && $opt_unit >= 0)
{
 $command = $conf{'path'}.$conf{'bin'}." ".$conf{'cmd_part_first'}." c$opt_control u$opt_unit ".$conf{'cmd_part_last'};
 $return = qx ( $command );
$return =~ /.*\s=\s(.*)/;
$tmp1 = $1;
if ($tmp1) {

 foreach (@state_ok) {
  if ($tmp1 eq $_) {
   $state_out = "OK";
   $desc_out = "Unit $opt_unit at Controller $opt_control is $tmp1";
   $exit_out = 0;
   }
 }
 if (!$state_out) {
  foreach (@state_warning) {
   if ($tmp1 eq $_) {
    $state_out = "WARNING";
    $desc_out = "Unit $opt_unit at Controller $opt_control is $tmp1";
    $exit_out = 1;
    }
  }
 }

}

 if (!$state_out) {
  $state_out = "CRITICAL";
  $desc_out = "Unit $opt_unit at Controller $opt_control is $tmp1";
  $exit_out = 2;
 }

print "$progname: $state_out ($desc_out)\n";
exit $exit_out;

}
 else
 {
  print_help();
  exit;
 }
