#!/usr/bin/perl -w
#
# 2006 (C) Andrei Warkentin, Data Armor. 
# Read the GNU Copyright stuff for all the legalese.
# 
# Check HP array health. Uses the HP hpacucli utility.
# This means this should work for arrays which use the cpqarray 
# and cciss drivers. 
# This is based of check_3ware.pl plugin, by Andrei Warkentin.

use strict;
use Getopt::Long;

# Note that we don't use the critical, timeout or warning flags for anything. Or the hostname.
use vars qw($state $state_message $PROGNAME $opt_controller $opt_version $opt_verbose $opt_help $opt_hostname $opt_warning $opt_critical $opt_timeout);
use lib "/usr/lib/nagios/plugins";
use utils qw($TIMEOUT %ERRORS &print_revision &support);

# Execute command for running the HP ACU CLI.
my $hpacucli = "/usr/sbin/hpacucli";

# Name of this plugin.
$PROGNAME = "check_hparray";

sub print_help();
sub print_usage();

# Clear the environment.
$ENV{'PATH'} = '';
$ENV{'BASH_ENV'} = '';
$ENV{'ENV'} = '';

Getopt::Long::Configure('bundling');
GetOptions
    (
     "V" => \$opt_version, "version" => \$opt_version,
     "h" => \$opt_help, "help" => \$opt_help,
     "v" => \$opt_verbose, "verbose" => \$opt_verbose,
     "w=s" => \$opt_warning, "warning=s" => \$opt_warning,
     "c=s" => \$opt_critical, "critical=s" => \$opt_critical,
     "t=s" => \$opt_timeout, "timeout=s" => \$opt_timeout,
     "H=s" => \$opt_hostname, "hostname=s" => \$opt_hostname,
     "d=s" => \$opt_controller, "controller=s" => \$opt_controller);

if($opt_version) {
    print_revision($PROGNAME, 'Version: 1.0');
    exit $ERRORS{'OK'};
}
     
if($opt_help) {
    print_help();
    exit $ERRORS{'OK'};
}

if(! $opt_controller) {
    print "ERROR Missing controller slot number - did you pass anything to -d?\n";
    exit $ERRORS{"UNKNOWN"};
}

# Check for hpacucli presence.
if(! -x $hpacucli) {
    print "ERROR No executable hpacucli at $hpacucli.\n";
    exit $ERRORS{"UNKNOWN"};
}

# Parse logical drive info.
if(!open(HPACUCLI, $hpacucli . " controller slot=$opt_controller ld all show 2>&1 |")) {
    print "ERROR Could not open $hpacucli: $!\n";
    exit $ERRORS{"UNKNOWN"};
}
$state = 'UNKNOWN';
$state_message = 'No understandable output from hpacucli';
my $out;

# There is some reasoning behind the ordering of the checks. 
while(<HPACUCLI>)
{
    $out .= "$_ ";
    if(/OK\)/)
    {
        $state = 'OK';
        $state_message = "OK Arrays on Controller in slot=$opt_controller";       
    }
    if(/Rebuilding\)/) {
	$state = 'WARNING';
	$state_message = "Rebuilding Arrays on Controller in slot=$opt_controller";
    }       
    if(/Failed\)/) {
        $state = 'CRITICAL';
        $state_message = "Failed Arrays on Controller in slot=$opt_controller";
    }     
    if(/Interim Recovery Mode\)/)
    {
        $state = 'CRITICAL'; 
        $state_message = "Degraded Arrays on Controller in slot=$opt_controller";
    }
}
close(HPACUCLI) || 
    die $! ? "$out - Error close $hpacucli pipe: $!"
    : "$out - Exit status: $? from $hpacucli\n";

print "$state_message.\n";
if($opt_verbose)
{
    print $out;
}
exit $ERRORS{$state};

sub print_usage()
{
    print "Usage: $PROGNAME [-d array slot number] [-v verbose]\n";
}
 
sub print_help() 
{
    print_revision($PROGNAME, "Version: 1.0");
    print "Copyright (C) 2006 Andrei Warkentin\n";
    print "\n";
    print_usage();
    print "Checks the status of the HP array controller in the slot number (like 1,2,4,5) passed with -d.\n\n";
    support();
}
