#!/usr/bin/perl -w
#
# Check status of RAID on PERC 6/i using MegaCli.
###################################################
# Last updated 6/5/2008
#
BEGIN {
$ENV{'PATH'} = '/usr/local/bin:/usr/bin';
$ENV{'ENV'} = '';
}
use strict;
use warnings;
my $Adp_Name = "";
my $Adp_FW = "";
my $VD_count = "";
my $VD_degraded = 0;
my $VD_offline = 0;
my $PDisk_count = 0;
my $PD_critical = 0;
my $PD_failed = 0;
my $error_count = 0;
my $Mem_Uncorrectable = 0;
open(TMP_, "sudo MegaCli64 -AdpAllinfo -a0|");
while (<TMP_>) {
 chomp;
 if ($_ =~ /^Product Name    : /) {
  $Adp_Name = substr($_,18);
  next;
 }
 if ($_ =~ /^FW Package Build: /) {
  $Adp_FW = substr($_,18);
  next;
 }
 if ($_ =~ /^Virtual Drives    : (\d+)/) {
  $VD_count = $1;
  next;
 }
 if ($_ =~ /^\s+Degraded\s+: (\d+)/) {
  if ($1 != 0) {
    $error_count = $error_count +1;
  }
  $VD_degraded = $1;
  next;
 }
 if ($_ =~ /^\s+Offline\s+: (\d+)/) {
  if ($1 != 0) {
    $error_count = $error_count +1;
  }
  $VD_offline = $1;
  next;
 }
 if ($_ =~ /^\s+Disks\s+: (\d+)/) {
  $PDisk_count = $1;
  next;
 }
 if ($_ =~ /^\s+Critical Disks\s+: (\d+)/) {
  if ($1 != 0) {
    $error_count = $error_count +1;
  }
  $PD_critical = $1;
  next;
 }
 if ($_ =~ /^\s+Failed Disks\s+: (\d+)/) {
  if ($1 != 0) {
    $error_count = $error_count +1;
  }
  $PD_failed = $1;
  next;
 }
 if ($_ =~ /^Memory Uncorrectable Errors : (\d+)/) {
  if ($1 != 0) {
    $error_count = $error_count +1;
  }
  $Mem_Uncorrectable = $1;
  next;
 }
}
close(TMP_);
print("Checking status on $Adp_Name FW $Adp_FW \n");
print("Error count is $error_count \n");
if ($error_count > 0) {
  print("Virtual Disk count is $VD_count. Degraded = $VD_degraded and offline = $VD_offline.\n");
  print("Physical Disk count is $PDisk_count. Critical = $PD_critical and failed = $PD_failed.\n");
  print("Uncorrectable Memory Errors: $Mem_Uncorrectable \n");
 }
exit($error_count);

