#!/usr/bin/perl -w

# Copyright (c) 2008,2009 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 File::Temp qw/ tempdir /;

# 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 arcconf $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;
	};
}

# arcconf puts crap into a $PWD/UcliEvt.log file.
my $dir = tempdir( "/tmp/check-aacraid-XXXXXXX", CLEANUP => 1 );
chdir ($dir) or die ("Cannot chdir $dir: $!\n");

my $ctrl = 1;
my @resultstr;
my $numcontrollers = 1;
while ($ctrl <= $numcontrollers) {
	my $lds = runcmd("GETCONFIG $ctrl LD");
	my %status;
	my $ld = "unknown";
	my @extrastatus = ();
	for (@$lds) {
		chomp;
		if (/^Controllers found: *([0-9]+)/) {
			$numcontrollers = $1;
		} elsif (/^Logical device number (.*)/) {
			$ld = "LD$1";
		} elsif (/^ *Status of logical device *: *(.*)/) {
			my $status = $1;
			if ($status ne 'Optimal') {
				record('WARNING');
			};
			push @{$status{$status}}, $ld;
		};
	};

	my $adi = runcmd("GETCONFIG $ctrl AD");
	for (@$adi) {
		chomp;
		if (/^ *Controller Status *: *(.*)/) {
			my $status = $1;
			if ($status ne 'Optimal') {
				record('WARNING');
			};
			push @{$status{$status}}, "controller";
		} elsif (/^ *Defunct disk drive count *: *(.*)/) {
			my $count  = $1;
			if ($count > 0) {
				record('WARNING');
				push @extrastatus, "$count defunct drives";
			}
		} elsif (m#^ *Logical devices/Failed/Degraded *: (.*)/(.*)/(.*)#) {
			my $total = $1;
			my $failed = $2;
			my $degraded = $3;
			push @extrastatus, "$total/$failed/$degraded LDs total/failed/degraded";
			if ($failed > 0) {
				record('CRITICAL');
			} elsif ($degraded > 0) {
				record('WARNING');
			}
		} elsif (/.*Controller Battery Information/) {
			last;
		};
	};
	# in Controller Battery Information
	for (@$adi) {
		chomp;
		if (/^ *Status *: *(.*)/) {
			my $status = $1;
			if ($status eq 'Not Installed') {
				next;
			} elsif ($status ne 'Optimal' && $status ne 'ZMM Optimal') {
				record('WARNING');
			};
			push @{$status{$status}}, 'Battery';
		}
	}

	my $status = join(" - ", (map { $_.": ".join(", ", @{$status{$_}}) } keys %status));
	if (scalar @extrastatus > 0) {
		$status .= ", ".join(", ", @extrastatus);
	}
	push @resultstr, "Ctrl $ctrl: $status";
	$ctrl++;
};

if ( -e 'UcliEvt.log' ) {
	unlink('UcliEvt.log') or die ("Cannot unlink UcliEvt.log: $!\n");
}

# Need to chdir out of tempdir in order to rm it in new perl
chdir ('/') or die ("Cannot chdir /: $!\n");

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