#!/usr/bin/perl -w


# check_intel_raid.pl   --  Nagios Plugin
# By Andrey A Sidorenko - sidor <at> maa <dot> spb <dot> ru
# This script - modified version of check_promise_chassis.pl
# by Barry O'Donovan - http://www.barryodonovan.com/ - barry <at> opensolutions <dot> ie
#
# Designed specifically for the Intel S5000PAL motherboard but should be work with other
# models, which have controllers, supported by Intel’s RAID Web Console 2.
# List of supported RAID controllers can be found here:
# http://downloadmirror.intel.com/16416/ENG/ir3_Linux_RWC2_2.64-00_readme.txt
# Intel’s RAID Web Console 2 must be installed on Intel's server in server-mode
# http://downloadmirror.intel.com/16416/ENG/ir3_Linux_RWC2_2.64-00_readme.txt
# Distribution of Intel’s RAID Web Console 2 can be found here:
# http://downloadcenter.intel.com/default.aspx
#
# Usage: ./check_intel_raid.pl <community> <host>
#
# Copyright (c) 2008 Andrey A Sidorenko
# Copyright (c) 2007 Barry O'Donovan - http://www.barryodonovan.com/
# Copyright (c) 2007 Open Source Solutions Ltd - http://www.opensolutions.ie/
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of 
# this software and associated documentation files (the "Software"), to deal in 
# the Software without restriction, including without limitation the rights to 
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
# of the Software, and to permit persons to whom the Software is furnished to do 
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all 
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


use strict;
use Net::SNMP;

my $status;
my $TIMEOUT = 20;

my %ERRORS = ('UNKNOWN' , '-1',
              'OK' , '0',
              'WARNING', '1',
              'CRITICAL', '2');

my $state = "OK";
my $answer = "";
my $snmpkey;
my $key;
my $community = "public";
my $port = 161;

my $snmpVirtualDiskStatusTable = '1.3.6.1.4.1.3582.4.1.4.3.1.2.1.5';

my $hostname;
my $session;
my $error;
my $response = undef;
my $usage;

my $itn;
my @virtual_disk_stats = undef;


sub setstate 
{
    my $newstate = shift( @_ );

    if( $ERRORS{$newstate} > $ERRORS{$state} ) 
    {
        $state = $newstate;
        return();
    }
    
    if( $newstate eq 'UNKNOWN' && $state eq 'OK' ) 
    {
        $state = $newstate;
        return();
    }
}

# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
     print ("ERROR: No snmp response from $hostname\n");
     exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);

$community = shift;
$hostname  = shift;

( $session, $error ) = Net::SNMP->session(
 -hostname  => $hostname,
 -community => $community,
 -port      => $port
);

if( !defined( $session ) ) 
{
    $state='UNKNOWN';
    $answer=$error;
    print ("$state: $answer");
    exit $ERRORS{$state};
}
####################################################################################
# Query the Virtual Disk States
####################################################################################

if( !defined( $response = $session->get_table( $snmpVirtualDiskStatusTable ) ) ) 
{
    if( $session->error_status == 2 ) 
    {
        $answer = "Virtual Disk State OID not supported.";
    } 
    else 
    { 
        $answer=$session->error; 
    }
    $session->close;
    $state = 'CRITICAL';
    print ( "$state: $answer" );
    exit $ERRORS{$state};
}

$itn = 0;

foreach $snmpkey ( keys %{$response} ) 
{
    $virtual_disk_stats[$itn] = $response->{$snmpkey};
    if ($virtual_disk_stats[$itn]==0) {
	$virtual_disk_stats[$itn]="offline";
    } elsif ($virtual_disk_stats[$itn]==1) {
	$virtual_disk_stats[$itn]="partially degraded";
    } elsif ($virtual_disk_stats[$itn]==2) {
	$virtual_disk_stats[$itn]="degraded";
    } elsif ($virtual_disk_stats[$itn]==3) {
	$virtual_disk_stats[$itn]="optimal";
    }
    
    if( !( $virtual_disk_stats[$itn] =~ "optimal" ) )
    {
        &setstate('CRITICAL');
        $answer .= "Virtual Disk " .  $itn  . " in state " . $virtual_disk_stats[$itn] . ". ";
    } else {
        $answer .= "Virtual Disk " .  $itn  . " in state " . $virtual_disk_stats[$itn] . ". ";
    }
    $itn++;
}

####################################################################################
####################################################################################

$session->close;

if($state eq 'OK') 
{
    print "Virtual Disks OK. $answer\n";
} 
else 
{
    print "Virtual Disks CRITICAL. $answer\n"; 
}

exit $ERRORS{$state};

