#!/usr/bin/perl -w
#
# Check status of RAID on PERC 6/i using MegaCli.
###################################################
# Last updated 2011/01/11
#
BEGIN {
$ENV{'PATH'} = '/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_, "/usr/local/sbin/MegaCli64 -AdpAllinfo -a0|");
while (<TMP_>) {
 chomp;
 if ($_ =~ /^\h*Product Name\h*:\h*/) {
  $Adp_Name = substr($_,18);
  next;
 }
 if ($_ =~ /^\h*FW Package Build\h*:\h*/) {
  $Adp_FW = substr($_,18);
  next;
 }
 if ($_ =~ /^\h*Virtual Drives\h*:\h*(\d+)/) {
  $VD_count = $1;
  next;
 }
 if ($_ =~ /^\h*Degraded\h*:\h*(\d+)/) {
  if ($1 != 0) {
    $error_count = $error_count +1;
  }
  $VD_degraded = $1;
  next;
 }
 if ($_ =~ /^\h*Offline\h*:\h*(\d+)/) {
  if ($1 != 0) {
    $error_count = $error_count +1;
  }
  $VD_offline = $1;
  next;
 }
 if ($_ =~ /^\h*Disks\h*:\h*(\d+)/) {
  $PDisk_count = $1;
  next;
 }
 if ($_ =~ /^\h*Critical Disks\h*:\h*(\d+)/) {
  if ($1 != 0) {
    $error_count = $error_count +1;
  }
  $PD_critical = $1;
  next;
 }
 if ($_ =~ /^\h*Failed Disks\h*:\h*(\d+)/) {
  if ($1 != 0) {
    $error_count = $error_count +1;
  }
  $PD_failed = $1;
  next;
 }
 if ($_ =~ /^\h*Memory Uncorrectable Errors\h*:\h*(\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);
