Our server has a 3Ware 9550SX SATA-RAID card in it that controlls our raid system. The command-line interface to this card is the tw_cli program, provided by 3ware. I wrote a script that runs every hour that checks the status of the raid and sends mail to be if a disk comes up as not optimal or if any new alarm messages appear.

#!/usr/bin/perl

#
# check_raid – Check for any alarms from the raid card, using tw_cli
#
# Syntax: check_raid
#
# If any errors are listed, email them to the sysadmin
#
# 13 October 2006

$day=`date +%a`;
$month=`date +%b`;
$date=`date +%d`;
$hostname=`hostname`;

chop($day);
chop($month);
chop($date);
chop($hostname);

# Location of tw_cli program
$TW_HOME=”/net/sw/edg/bin”;

# Person to receive email
$TO=”admin\@uchicago.edu”;
$FROM=”root”;
$SUBJECT=”RAID DISK PROBLEM ON $hostname”;

# Determine how many raid controllers are in the machine
@raids = `$TW_HOME/tw_cli show| grep ^c`;

# Output of above command will look something like this:
# c0 9550SX-8LP 8 8 1 0 4 4 –
# c1 9550SX-8LP 8 8 1 0 4 4 –

# Run the tw_cli program, looking for errors in each raid
foreach $raid(@raids)
{
# Split the line to get the controller
($controller, $model, $ports, $drives, $units, $notopt, $rrate, $vrate, $bbu)=split(‘ ‘,$raid, 9);

# If any are in the Not Optimal state, email the admin
if ( $notopt != 0 )
{
$notopt_message = “$notopt disk(s) on controller $controller are not in optimal state.\n\n”;
}

# Check for any disk alarms
@alarms=`$TW_HOME/tw_cli show alarms| grep $controller`;
foreach $alarm(@alarms)
{

#Check the month and date on the alarm and only send messages from the current day
($acontroller,$aday,$amonth,$adate,$atime,$ayear,$aerror,$anotes,$junk,$amessage)=split(‘ ‘,$alarm, 10);

# Check that the alarm message is from today
if ($date == $adate && $month == $amonth)
{
# Concatenate the messages together and send mail out
$message = $notopt_message . $alarm;
sendEmail(“$TO”,”$FROM”,”$SUBJECT”,”$message”);
}
}

}

sub sendEmail
{
my ($to, $from, $subject, $message) = @_;
my $sendmail = ‘/usr/sbin/sendmail’;
open (MAIL, “|$sendmail -oi -t”);
print MAIL “From: $from\n”;
print MAIL “To: $to\n”;
print MAIL “Subject: $subject\n\n”;
print MAIL “$message\n”;
close (MAIL);
}