#! /usr/bin/perl
# anders@aftenposten.no, 2006-08-22
# check status of COMPAQ RAID volumes in FreeBSD

%modelist=();
$okstatus="OK";
$arraytxt="COMPAQ RAID";
$ENV{PATH} = "/usr/local/bin:/usr/local/sbin:$ENV{PATH}:/sbin";

$volumes = 0;

if (!open(CAM, "sudo -u root /sbin/camcontrol devlist |")) {
	print "ERROR, could not open sudo -u root /sbin/camcontrol.\n";
	exit(3);
}
while(<CAM>) {
	next if ($_ !~ /$arraytxt/);
	$volumes++;
	$mode = $_;
	chomp($mode);
	$mode =~ s@<COMPAQ RAID \d+\s+VOLUME @@;
	$mode =~ s@>.*@@;
#	print "Mode: $mode\n";
	if (defined $modelist{"$mode"}) {
		$modelist{"$mode"}++;
	} else {
		$modelist{"$mode"}=1;
	}
}
close(CAM);

if ($volumes == 0) {
	print "No $arraytxt arrays found. Sudo problem?\n";
	exit(3);
} elsif ($volumes == $modelist{"$okstatus"}) {
	# All volumes are OK
	print $modelist{"$okstatus"} . " of " . $volumes . " volumes OK\n";
	exit(0);
} else {
	# Not all volumes are OK
	print "ERROR, $volumes volumes:";
	foreach $key (keys %modelist) {
		print " " . $modelist{"$key"} . " $key"
	}
	print "\n";
	# This is critical
	exit(2);
}
