#!/usr/bin/perl -w

# check enclosure status

# Copyright (c) 2008,2009,2010 Peter Palfrader <peter@palfrader.org>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

use strict;
use English;
use Getopt::Long;

# nagios exit codes
my %CODE = (
	'OK'            => 0,
	'WARNING'       => 1,
	'CRITICAL'      => 2,
	'UNKNOWN'       => 3
);

my $EXITCODE = 'OK';

$SIG{'__DIE__'} = sub {
	print STDERR @_;
	exit $CODE{'UNKNOWN'};
};

sub runcmd($) {
	my ($cmd) = @_;
	$cmd = "sudo hpacucli $cmd";
	open(FH, $cmd."|") or die ("Cannot run $cmd: $!");
	my @lines = <FH>;
	close FH;
	die ("no results from $cmd\n") if (scalar @lines == 0);
	return \@lines;
}

sub record($) {
	my ($newexit) = @_;
	die "code $newexit not defined\n" unless defined $CODE{$newexit};

	if ($CODE{$newexit} > $CODE{$EXITCODE}) {
		$EXITCODE = $newexit;
	};
}

my $usage = "$PROGRAM_NAME: Usage: $PROGRAM_NAME <controller slot> <enclosure>\n";
my $params;
Getopt::Long::Configure('bundling');
if (!GetOptions (
	'--help'                      => \$params->{'help'},
	)) {
	die ($usage);
};
if ($params->{'help'}) {
	print $usage;
	exit (0);
};
die ($usage) unless (scalar @ARGV == 2);
my $slot = shift;
my $enc = shift;

my @resultstr;
my %status;
my $status = runcmd("controller slot=$slot enclosure $enc show detail");
for (@$status) {
	chomp;
	next if /^$/;
	next if (/^\S.*in Slot $slot/);
	next if (/^   \S.*at Port/);
	last if (/^   \S/);

	if (m/^      (Fan Status|Temperature Status):\s*(.*?)\s*$/) {
			my $system = $1;
			my $status = $2;
			push @{$status{$status}}, $system;
			if ($status ne 'OK') {
				record('WARNING');
			};
	} elsif (m/^      (Power Supply Status):\s*(.*?)\s*$/) {
			my $system = $1;
			my $status = $2;
			push @{$status{$status}}, $system;
			if ($status ne 'Redundant') {
				record('WARNING');
			};
	} elsif (m/^      (Active Path|Standby Path):\s*(.*?),\s*(.*?)\s*$/) {
			my $system = $1;
			my $detail = $2;
			my $status = $3;
			push @{$status{$status}}, $system."($detail)";
			if ($status ne 'OK') {
				record('WARNING');
			};
	} elsif (m/^(Error): (The specified device does not have a Storage Enclosure identified by.*)/) {
			my $status = $1;
			my $detail = "$enc: Unidentified Storage Enclosure";
			push @{$status{$status}}, $detail;
			record('CRITICAL');
	} elsif (m/^(Error): (.*)/) {
			my $status = $1;
			my $detail = $2;
			push @{$status{$status}}, $detail;
			record('CRITICAL');
	}

};
$status = join(" - ", (map { $_.": ".join(", ", @{$status{$_}}) } keys %status));
push @resultstr, "Slot $slot: $status";

print "$EXITCODE: ", join(" --- ", @resultstr), "\n";
exit $CODE{$EXITCODE};
