#!/usr/bin/perl

my $STATE_OK = 0;
my $STATE_WARNING = 1;
my $STATE_CRITICAL = 2;
my $STATE_UNKNOWN = 3;

use warnings;
use strict;

my $HRCONF = "C:\\hrconf\\hrconf.exe";
my $output =`$HRCONF GETCONFIG 1`;
my @lines = split(/\n/, $output);

# Array status
my $state;

# Number of drives
my $active_drives = 0;
my $failed_drives = 0;

# Arrays to store the drives names according to their status
my @failed_drives_tab;
my @active_drives_tab;

# Temp variable to save the no. of the drive
my @drivers_temp;
foreach my $line (@lines) {
# Is the RAID array OK or degraded ? 
 if ($line =~ /Status of logical drive/) {
   if ($line =~ /Optimal/) {
     $state = 1;
   }
   else {
     $state = 0;
   }
 }

# Which drive are we checking ? 
 if ($line =~ /Device \#/){
   @drivers_temp = split(/\#/, $line);
 }

# Is this drive OK ? 
 if ($line =~ /State/) {
 # Online
   if ($line =~ /Online/) {
     push(@active_drives_tab,$drivers_temp[1]);
     $active_drives++;
   }
 # Other
   else
   {
     push(@failed_drives_tab,$drivers_temp[1]);
     $failed_drives++;
   }
 }
}

# Output
if ($state == 1)
 {
   print "OK - $active_drives active drives : @active_drives_tab";
   exit $STATE_OK;
 }
else
 {
   print "CRITICAL - $failed_drives failed drive(s) : @failed_drives_tab";
   exit $STATE_CRITICAL;
 }
