Megaraid

Megaraid Raid Controller

Nagios Plugin

#!/usr/bin/perl -w

# check_megacli nagios plugin
# Copyright (c) Michael Mende 
#
# Checks the health state of a LSI Megaraid
#
# !!!You may need to change the path to the MegaCli binary!!!
#
# !!!You may need to add the following line to your /etc/sudoers:!!!
# nagios ALL=NOPASSWD: /usr/local/MegaCli/MegaCli -LDInfo -L0  -aALL

use strict;
use FindBin;
FindBin::again();
use lib $FindBin::Bin;
use utils qw($TIMEOUT %ERRORS);

my $sudo        = '/usr/bin/sudo';
my $megacli     = '/usr/local/MegaCli/MegaCli';
my $megacliargs = '-LDInfo -L0  -aALL';



#### $megacli OUTPUT EXAMPLE
#Adapter 0 -- Virtual Drive Information:
#Virtual Disk: 0 (Target Id: 0)
#Name:
#RAID Level: Primary-1, Secondary-0, RAID Level Qualifier-0
#Size:556944MB
#State: Optimal
#Stripe Size: 64kB
#Number Of Drives per span:2
#Span Depth:4
#Default Cache Policy: WriteBack, ReadAdaptive, Cached, No Write Cache if Bad BBU
#Current Cache Policy: WriteBack, ReadAdaptive, Cached, No Write Cache if Bad BBU
#Access Policy: Read/Write
#Disk Cache Policy: Enabled
#Encryption Type: None
#### END $megacli OUTPUT EXAMPLE

$treshold_error = ""
$treshold_error .= " no $sudo found" unless (-x $sudo );
$treshold_error .= " no $megacli found" unless (-x $megacli );

if ($treshold_error) {
    print "$treshold_error";
    exit $ERROR{'UNKNOWN'};
}

open (MEGACLI, "$sudo $megacli $megacliargs|") || die "error: Could not execute ".$megacli.": ".$!;

my $status = 'UNKNOWN';
my $state;
my ($threshold_output_level,$threshold_output_size,$threshold_output_state);

while () {
    if ( m/RAID\s+Level:/ ) {
        $threshold_output_level = $_;
    } elsif ( m/Size:/ ) {
        $threshold_output_size = $_;
    } elsif ( m/State:\s*(\w+)/ ) {
        $state = $1;
        $threshold_output_state = $_;
        if ( $state ne 'Optimal' ) {
            $status = 'CRITICAL';
        } else {
            $status = 'OK';
        }
    }
}

foreach ($threshold_output_level,$threshold_output_size,$threshold_output_state) {
    s/[\r\n]//g;
}

if ( $status eq 'OK' ) {
    print $status.": (".$threshold_output_state.", ".$threshold_output_size.", ".$threshold_output_level.")\n";
} else {
    print $status.": (".$threshold_output_state.", ".$threshold_output_size.", ".$threshold_output_level.")\n";
}
exit $ERRORS{$status}