#!/usr/bin/perl -w

$DEBUG = 0;

my ( $column, $color, $status, $desc, $mr_cmd, $mr_args );
my ( $ncrit, $nfail, $ndegr, $noffl );

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

$mr_cmd  = "sudo /opt/MegaRAID/MegaCli/MegaCli64";
$mr_args = "-AdpAllinfo -aALL";

my @output = `$mr_cmd $mr_args`;

$desc = "LSI MegaRAID/MegaSAS Controller Status<br />\n";

foreach my $line ( @output )
{
	#print "OUTPUT::: $line";
	#chomp $line;
	if ( $line =~ /^\s+Disks\s+\:\s(\d{1})/ )
	{
		$desc .= "Number of physical disks: $1\n";	
	}
	
	if ( $line =~ /^\s+Critical\sDisks\s+\:\s(\d{1})/ )
	{
		$ncrit = $1;
		$desc .= "Number of critical physical disks: $1\n";
	}
	
	if ( $line =~ /^\s+Failed\sDisks\s+\:\s(\d{1})/ )
	{
		$nfail = $1;
		$desc .= "Number of failed physical disks: $1<br />\n";
	}

	if ( $line =~ /^Virtual\sDrives\s+\:\s(\d{1})/ )
	{
		$desc .= "Number of virtual drives: $1\n";
	}
	
	if ( $line =~ /^\s+Degraded\s+\:\s(\d{1})/ )
	{
		$ndegr = $1;
		$desc .= "Number of degraded virtual disks: $1\n";
	}

	if ( $line =~ /^\s+Offline\s+\:\s(\d{1})/ )
	{
		$noffl = $1;
		$desc .= "Number of offline virtual disks: $1<br />\n";
	}
}

if ( (( $ndegr != 0 ) && ( $nfail != 0)) || ( $noffl != 0) )
{
	$color = 'red';
	$desc .= "There are failed disks or virtual drives.";
}
elsif ( ( $ndegr != 0 ) && ( $nfail == 0 ) && ( $ncrit != 0 ) )
{
	$color = 'yellow';
	$desc .= "There are critical disks.";
}
elsif ( ( $ndegr == 0 ) && ( $nfail == 0 ) && ( $ncrit == 0 ) && ( $noffl == 0 ) )
{
	$color = 'green';
	$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");
	}
	else
	{
		printf("COLUMN::: %s\n, COLOR::: %s\n, DESCRIPTION::: %s", $column, $color, $desc);
	}
}

