#!/usr/bin/perl -w

$SILENT = "YES"; # change to NO if you want reports even if no bad news

$RECIPS = "hmangala\@uci.edu"; #,aebrenne\@uci.edu,jfarran\@uci.edu";
$HOST = `hostname -s`; chomp $HOST;
$TWCLI = "/opt/3ware/CLI/tw_cli";

# the following shouldn't be changed if the controller is a recent 3ware.
# tested on 9570 and 9650SE
# if it does fail to detect the right controller, you'll have to make local 
# corrections

# set the controller # 
$CN = `$TWCLI show | grep ^c |cut -f1 -d' '`; chomp $CN;

# processed output of the tw_cli cmd to provide a string with all the ports occupied
$RAW_TW = `$TWCLI /$CN show all |grep ^p | cut -f1 -d' '|tr '\n' ' '`;
@PORTS = split(/ /, $RAW_TW);  # and put them in an array for easy iteration
$BODY = "Host\tPort\t#Sects_Realloc\n";

$ERRCNT = 0; # will record the number of disks with sector errors
foreach $disk (@PORTS) {
	print STDERR "$disk ";  # debugging assist
	$str =  `$TWCLI /$CN/$disk show rasect`;  # get the port info
#	print STDERR "$TWCLI /${CN}/${disk} show rasect\n";
	chomp $str;
	@l = split (/ = /, $str);
	if ($l[1] != 0) {$ERRCNT++; $BODY .= "$HOST\t$disk\t$l[1]\n";}
}

# and send off the error report to the $RECIPS
if ($ERRCNT > 0) {
	$SUBJ = "$HOST: !!WARN!![$ERRCNT] DISK w/ REALLOC SECTORS DETECTED";
	system ("echo \"$BODY\" |mutt -s \"$SUBJ\" $RECIPS");
} elsif ($SILENT eq "NO") {
	$SUBJ = "$HOST: INFO: NO DISKS w/ REALLOC SECTORS DETECTED";
	system ("echo \"$BODY\" |mutt -s \"$SUBJ\" $RECIPS");
}
	print STDERR "\n\nSummary Info [$ERRCNT] disks found with sector problems\n$BODY\n\n"; # for interactive use
