#!/usr/bin/perl
#
# Josh Yost
# written: 07.14.06
# updated: 12.19.06
#
#       Use the output from SNMP walks to create warning/critical
# signals to Nagios w/ Dell's Array/Storage Manager information.
#
# LICENSE
#       Licensed under the GPL w/ absolutely no warranty of any kind.
#
# TODO
#       Use Net::SNMP
#
# BUGS &/OR PATCHES
#       mailto: joshyost@gmail.com

use warnings;
use strict;
use Getopt::Std;
use File::Basename;
use lib '/usr/nagios/libexec';
use utils qw ( %ERRORS );

our $vers = '1.0.1';
our $exe  = basename $0;

sub usage{
    print "Usage: $exe [-hvV] -D (glob|phys|log|con) -T (sm|am) -H <host> -C <string>\n";
    exit $ERRORS{'OK'};
}

sub VERS{
    print "$exe\t\t$vers\n";
    exit $ERRORS{'OK'};
}

sub HELP{
  print "$exe\t\t$vers\n",
        "\n\tThis script will check your Dell hardware for problems. It\n",
        "can check the Controller State, Global State, Logical Disk State,\n",
        "and Physical Disk State.\n",
        "\tIt currently uses snmpwalk, so you must have it in your path. You\n",
        "may need to change the lib path to utils.pm since I use Gentoo,\n",
        "which installs the Nagios plugins at /usr/nagios/libexec.\n",
        "\nOPTIONS\n",
        "  -C <arg>\t\tSet the SNMP community string\n",
        "  -D glob|phys|log|con\tSet the device type that you want to check\n",
        "  -h \t\t\tShow this help information\n",
        "  -H <arg>\t\tSet the target host\n",
        "  -T am|sm\t\tSet the OpenManage type (Array or Storage Manager)\n",
        "  -v \t\t\tverbose (debugging output)\n",
  "  -V \t\t\tShow version information\n";
  exit $ERRORS{'OK'};
}

#### Variables
my %opts;
getopts("vVhH:C:T:D:", \%opts);
my $verbose = defined ($opts{v});
my $host = $opts{H} if defined ($opts{H});
my $pass = $opts{C} if defined ($opts{C});
my $type = $opts{T} if defined ($opts{T});
my $dev  = $opts{D} if defined ($opts{D});
my ($num,$exit,$id) = (0,$ERRORS{'OK'},1);
my ($out,$oid);

&HELP()  if defined($opts{h});
&VERS()  if defined($opts{V});
&usage() if !(defined($host) && defined($pass) && defined($type) && defined($dev));
&usage() if !($type eq 'sm'  || $type eq 'am');
&usage() if !($dev eq 'phys' || $dev eq 'log' || $dev eq 'con' || $dev eq 'glob');

#### initialize SNMP output hash
my %perc = ('log-0'  => 'UNKNOWN',        'phys-0'  => 'UNKNOWN',    'con-0'  => 'UNKNOWN',
            'log-1'  => 'READY',          'phys-1'  => 'READY',      'con-1'  => 'READY',
            'log-2'  => 'FAILED',         'phys-2'  => 'FAILED',     'con-2'  => 'FAILED',
            'log-3'  => 'ONLINE',         'phys-3'  => 'READY',      'con-3'  => 'ONLINE',
            'log-4'  => 'OFFLINE',        'phys-4'  => 'OFFLINE',    'con-4'  => 'OFFLINE',
            'log-6'  => 'DEGRADED',       'phys-6'  => 'DEGRADED',   'con-6'  => 'DEGRADED',
            'log-7'  => 'VERIFYING',      'phys-7'  => 'RECOVERING', 'glob-0' => 'UNKNOWN',
            'log-15' => 'RESYNCHING',     'phys-11' => 'REMOVED',    'glob-1' => 'CRITICAL',
            'log-24' => 'REBUILDING',     'phys-15' => 'RESYNCHING', 'glob-2' => 'WARNING',
            'log-26' => 'FORMATTING',     'phys-24' => 'REBUILDING', 'glob-3' => 'NORMAL',
            'log-32' => 'RECONSTRUCTING', 'phys-25' => 'NO MEDIA',   'glob-4' => 'UNKNOWN',
            'log-35' => 'INITIALIZING',   'phys-26' => 'FORMATTING',
            'log-36' => 'BKGRND INIT.',   'phys-28' => 'DIAGNOSTICS',
            'phys-35' => 'INITIALZING' );

# set oid param
$id = 20 if $type eq 'sm';

# set out and oid string
if ($dev eq 'phys'){
    $out = 'Physical Disks -';
    $oid = ".1.3.6.1.4.1.674.10893.1.${id}.130.4.1.4";
}
elsif ($dev eq 'log'){
    $out = 'Logical Disks -';
    $oid = ".1.3.6.1.4.1.674.10893.1.${id}.140.1.1.4";
}
elsif ($dev eq 'con') {
    $out = 'Controller(s) -';
    $oid = ".1.3.6.1.4.1.674.10893.1.${id}.130.1.1.5";
}
else{
    $out = 'PERC Global State -';
    $oid = ".1.3.6.1.4.1.674.10893.1.${id}.2";
}

my @output = `snmpwalk -v2c $host -c $pass $oid`;
my $state  = $?;

if ($state == 2 || $state > 2){
    print $output[0];
    exit 2;
}
elsif ($state == 1){
    print $output[0];
    exit 1;
}
elsif ($state == 0){
    for (@output){
        $num++;
        print "output:|$_|,state:$state\n" if $verbose;

        /INTEGER:\s+(\d+)/;
        my ($s) = ($1);

        print "s: |$s|\n" if ($verbose && defined($s));
        if (!defined($s)){
            print $_;
            exit $ERRORS{'UNKNOWN'};
        }

    # handle global status separately
        if ($dev eq 'glob'){
            $out .= ' ' . $perc{"$dev-$s"};
            if    ($s == 1) { $exit = $ERRORS{'CRITICAL'} }
            elsif ($s == 2) { $exit = $ERRORS{'WARNING'}  }
            elsif ($s != 3) { $exit = $ERRORS{'UNKNOWN'}  }
            next;
        }

    # handle other devices
        if (($dev eq 'phys' && !($s == 1 || $s == 3)) || $dev ne 'phys'){
      $out .= ' #' . $num . ': ';
          (defined($perc{"$dev-$s"})) ? ($out .= $perc{"$dev-$s"})
          : ($out .= 'UNKNOWN VALUE');
  }

    # set exit status; make sure you don't downgrade status
        if (!defined($perc{"$dev-$s"}) || $perc{"$dev-$s"} eq 'UNKNOWN'){
            $exit = $ERRORS{'UNKNOWN'} if ($exit != $ERRORS{'CRITICAL'})
            }
        elsif ($perc{"$dev-$s"} eq 'FAILED' || $perc{"$dev-$s"} eq 'OFFLINE'){
            $exit = $ERRORS{'CRITICAL'}
        }
        elsif (!($perc{"$dev-$s"} eq 'READY' || $perc{"$dev-$s"} eq 'ONLINE')){
            $exit = $ERRORS{'WARNING'} if ($exit != $ERRORS{'CRITICAL'})
            }
    }
  #### output
  # check physical disk output
    if ($dev eq 'phys' && $out eq 'Physical Disks -'){
        $out .= ' OK';
    }
    print $out,"\n";
    exit $exit;
}
elsif ($state < 0){
    print $output[0];
    exit $state;
}
