#!/usr/bin/perl -w
#
# Nagios plugin to check raid status on Solaris.
# Copy to Nagios libexec directory (requires utils.pm from Nagios plugins).
#
# $Id: check_raidctl.pl,v 1.00 2010/03/09 kaunt Exp $
#
# Copyright (C) 2008-2010  Thomas Kaun / Telefonica o2 Germany GmbH & Co OHG
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

##################################################################################
### loading modules
##################################################################################

use strict;
use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS);

##################################################################################
### declarations
##################################################################################

my $warning = 0; my $critical = 0;
my @line; my @vline; my @dline;
my @controller; my @volume;
my $stat = ""; my $perf = ""; my $vol = "";
my $command = "/usr/local/bin/sudo /usr/sbin/raidctl";

##################################################################################
### main
##################################################################################

open (RAIDCTL, "$command |");
my @rc = <RAIDCTL>;
close RAIDCTL;

foreach (@rc) {
  if ($_ =~ /Volume/) {
    @line = split(/:/, $_);
    chomp $line[1];

    open (VOLCHECK, "$command -l $line[1]|");
    @volume = <VOLCHECK>;
    close VOLCHECK;

    foreach (@volume) {
      if ($_ =~ /^c/) {
	@vline = split(/\s/, $_);
	if ($vline[5] =~ /OPTIMAL/) {
	  $stat = ($stat."Volume ".$line[1]." ".$vline[5]."/".$vline[8].", ");
	}
	elsif ($vline[5] =~ /FAILED/) {
	  $stat = ($stat."Volume ".$line[1]." failed, ");
	  $critical++;
	}
	else {
	  $stat = ($stat."Volume ".$line[1]." ".$vline[5].", ");
	  $warning++;
	}
      }
      if ($_ =~ /^\s/ && !($_ =~ /[a-z]/)) {
	@dline = split(/\s/, $_);
	$perf = ($perf."Disk ".$dline[2]." ".$dline[5].", ");
      }
    }

  }
}

##################################################################################
### generating return code and message
##################################################################################

if ($critical > 0) {
	print "$critical CRITICAL's, $warning WARNING's ! $stat|$perf\n";
	exit $ERRORS{'CRITICAL'};
}
elsif ($warning > 0) {
	print "$warning WARNING's ! $stat|$perf\n";
	exit $ERRORS{'WARNING'};
}
print "$stat|$perf\n";
exit $ERRORS{'OK'};
