Automated MegaRaid monitoring on ESXi 5

I have a couple of MegaRaid controllers (Intel RS2BL040 & LSI 9260-4i)
I’m not thrilled with the monitoring software so I wrote this script to email me in the event of a raid problem:
There are a couple other pieces but this is the meat of it;
Runs from crontab every 5 minutes, named “process.pl”

#!/usr/bin/perl

use strict;
use warnings;

use MIME::Lite::TT;

# Process the current log file in

my $logs_dir = '/home/megaraid/logs/';

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);

$year += 1900;
$mon = sprintf('%.2d',$mon+1);
$mday = sprintf('%.2d',$mday);
$hour = sprintf('%.2d',$hour);

while ( $min % 5 ) {
$min--;
}
$min = sprintf('%.2d',$min);

#print "year: $year Month: $mon Day $mday hour $hour : $min \n";

my $filename = $year.$mon.$mday.$hour.$min.'.txt';

print "Getting filename $filename \n";

my $text_file = '/home/megaraid/logs/'.$filename;

my @cmd = ( 'scp root@192.168.0.175:/tmp/megaraid/'.$filename.' /home/megaraid/logs/' );

system( @cmd );

unless ( -f '/home/megaraid/logs/'.$filename ) { die ( $filename . " could not be retrieved, exiting now.\n\n" ); }

print "Successfully retrieved, now processing $text_file \n...\n";

my $capture;
my $counter = 0;
my %alerts;
my $line;
my %params;
my %options;
$options{INCLUDE_PATH} = '/home/megaraid';
$options{anything} = 'nothing';

open ( TXT, $text_file ) || die "Couldn't open Text file $text_file!\n";

while ($line = ) {

if ( $line =~ m/Device Present/gi ) { #We want the next 5 lines, start capture

$counter++;

}

if ( $counter > 0 && $counter < 11 ) { $capture .= $line; $counter++; if ( $line =~ m/Degraded\s*:\s(\d+)\s/ && $1 > 0 ) {
print "Degraded: $1 \n";
$alerts{'degraded'} = $1;
}

if ( $line =~ m/Offline\s*:\s(\d+)\s/ && $1 > 0 ) {
print "Offline: $1 \n";
$alerts{'offline'} = $1;
}

if ( $line =~ m/Critical Disks\s*:\s(\d+)\s/ && $1 > 0 ) {
print "Critical: $1 \n";
$alerts{'critical'} = $1;
}

if ( $line =~ m/Failed Disks\s*:\s(\d+)\s/ && $1 > 0 ) {
print "Failed: $1 \n";
$alerts{'failed'} = $1;
}

} # End of 10 rows we need to look at (physical drive status)

}

close( TXT );

print $capture . "\n";

my $alert_file = '/home/megaraid/status.txt';
my $alert_text = "";

if ( (scalar keys %alerts) > 0 ){

print "ALERTS! \n Check alert file and possibly send email if new alert status.\n";
foreach my $alert ( keys %alerts ) {

my $val = $alerts{ $alert };
print "$alert => $val\n";

$alert_text = "$alert $val\n";
open ( ALERT, "$alert_file" );

$counter = 0;

while ($line = ) {
if ( $line eq $alert_text ) {
print "found match $line $alert_text \n";
$counter++;
}
}

close ( ALERT );

if ( $counter == 0 ) {

print "THIS IS A NEW ALERT, send email $alert_text \n";
open ( ALERT, ">>$alert_file" );
print ALERT $alert_text;
close ( ALERT );

$params{'first_name'} = "Bob";
$params{'alert_text'} = $alert_text;

my $msg = MIME::Lite::TT->new(
From => 'ESXI@omniweb.com',
To => 'ESXI@omniweb.com,my_cell_number',
Subject => 'RAID Alert',
Template => 'email.txt.tt',
TmplOptions => \%options,
TmplParams => \%params,
);

$msg->send;

}

}

} else {

# check if there were previous alerts, notify when cleared
open ( ALERT, "$alert_file" );

$counter = 0;
while ($line = ) {
if ( $line ne "Good" ) {
print "Status was $line now Good\n";
$alert_text .= "Status was $line now Good\n";
$counter++;
}
}
close ( ALERT );
if ( $counter > 0 ){
open ( ALERT, ">$alert_file" );
print ALERT "Good";
close ( ALERT );
print "Email $alert_text \n";

$params{alert_text} = $alert_text;
my $msg = MIME::Lite::TT->new(
From => 'ESXI@omniweb.com',
To => 'ESXI@omniweb.com,my_cell_number',
Subject => 'RAID Good',
Template => 'email.txt.tt',
TmplOptions => \%options,
TmplParams => \%params,
);

$msg->send;

}
}

exit();

Leave a comment if you want details on how to get the rest of it working. Or if there is a better way to do it, please let me know!

This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Automated MegaRaid monitoring on ESXi 5

Leave a Reply

Your email address will not be published. Required fields are marked *