#!/usr/bin/perl -w

$DEBUG = 0;

my ( $column, $color, $status, $desc, $mr_cmd, $mr_args );

$color  = 'green';
$column = 'hwraid';
$status = 'status ';

$mr_cmd  = "sudo /opt/ArcRAID/bin/arcconf";
$mr_args = "getconfig 1 AL";

my @output = `$mr_cmd $mr_args`;

$desc = "Adaptec (ServeRAID) Controller Status<br />\n";

foreach my $line ( @output )
{
	if ( $line =~ /^\s+Logical\sdevice\sname\s+\:\s(.*)/ )
	{
		$desc .= "Logical device: $1\n";	
	}
	
	if ( $line =~ /^\s+RAID\slevel\s+\:\s(.*)/ )
	{
		$desc .= "RAID level: $1\n";
	}

	if ( $line =~ /^\s+Status\sof\slogical\sdevice\s+\:\s(.*)/ )
	{
		$desc .= "Logical device status: $1\n";
	}

	if ( $line =~ /^\s+Size\s+\:\s(.*)/ )
	{
		$desc .= "Logical device size: $1<br />\n";
	}

	last if ( $line =~ /^Physical\sDevice\sinformation/ )
}

my @statuses = grep(/Status of logical device/, @output);
foreach my $status ( @statuses )
{
	if ( $status !~ /Optimal/ )
	{
		$color = 'red';
	}
}

if ( $color eq 'red' )
{
	$desc .= "There appears to be a problem with a logical device.<br /><br /><br />";
}
else
{
	$desc .= "All RAID devices are functioning normally.<br /><br /><br />";
}

$desc .= "@output";

xymon_submit($desc, $color, $column);

sub xymon_submit
{
        my ($xymon, $xymon_srv, $machine);
        my ($mesg, $color, $column) = @_;

        if ( $DEBUG != 1 )
        {
                $xymon     = $ENV{'BB'};
                $xymon_srv = $ENV{'BBDISP'};
                $machine   = $ENV{'MACHINE'};
	
        	system("$xymon $xymon_srv 'status $machine.$column $color $desc'\n");
	}
}

