#!/usr/bin/perl -w
#
# 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
#
# Additions by Ianaré Sévi <ianare@gmail.com> :
# SSH addon-on, newer style report, more keywords

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

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

$progname = basename($0);

@state_ok=("OK","VERIFYING");
@state_warning=("DEGRADED","OFFLINE","INITIALIZING","REBUILDING","MIGRATING");

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

Getopt::Long::Configure('bundling');
GetOptions
	(
	"hostname=s"	=>	\$opt_host,
	"H=s"		=>	\$opt_host,
	
	"user=s"	=>	\$opt_usr,
	"u=s"		=>	\$opt_usr,

	"controller=s"	=>	\$opt_control,
	"C=s"		=>	\$opt_control,
	
	"unit=s"	=>	\$opt_unit,
	"U=s"		=>	\$opt_unit,
	
	"port=s"        =>      \$opt_port,
        "p=s"           =>      \$opt_port,

	"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; }

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 --hostname, -H\t\t\t Hostname\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 -C 0 -U 0 -H 192.168.1.15 -u nagios (same as above on remote: nagios\@192.168.1.15)\n\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)
{
 # use ssh
 if ($opt_host && $opt_usr) {
   if ($opt_port) {
     $conf{'fullbin'} = "/usr/bin/ssh -oProtocol=2 -p$opt_port $opt_usr\@$opt_host $conf{'bin'}";}
   else{
     $conf{'fullbin'} = "/usr/bin/ssh -oProtocol=2 -p22 $opt_usr\@$opt_host $conf{'bin'}";}
 }
  
 $command = "sudo ".$conf{'fullbin'}." ".$conf{'cmd_part_first'}." c$opt_control u$opt_unit ".$conf{'cmd_part_last'}; 
 $return = qx ( $command );
 #print $return;
 
 # newer style report
 if ($return =~ /=/){ 
  ($tmp1,$tmp2) = split(/= /,$return);
  $tmp2 =~ s/\s+$//;
  $stat = $tmp2;
 }
 # older style
 else{
 ($tmp1,$tmp2) = split(/\s.*?/,$return);
  $stat = $tmp1;
 }

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

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

#print "$progname: $state_out ($desc_out)|users = 0;0;0;0\n";
print "$progname: $state_out ($desc_out)|status = $exit_out\n";
exit $exit_out;
}

else
 {
  print_help();
  exit;
 }
