#!/usr/bin/perl

#This script requires sg3_utils
# Scott Nolin
# UW SSEC
# 6/1/2014 

#This script is for monitoring Dell MD1200 disk arrays attached via DIRECT SAS
# If you have a vendor hardware raid solution, this is likely not the answer you want.
# If you have a JBOD device other than an MD1200 it may be useful, but this has only been tested with the MD1200

#    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 3 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.
#    <http://www.gnu.org/licenses/>.

my %ses_hash = (
description => '0',
summary_status => '3',
);

my @enclosures;


my @enclosure_list = `/usr/bin/sg_map -i | /bin/grep MD1200`;
foreach my $line (@enclosure_list) {
        my @linearray = split(" ",$line);
        push(@enclosures, $linearray[0]);
}

foreach my $sgdev (@enclosures) {

        my @enclosure_info=`/usr/bin/sg_ses -f --page 2 ${sgdev}`;
        chomp @enclosure_info;

        my @ses_string=`/usr/bin/sg_ses -f --page 4 ${sgdev}`; # page 4 is string in/out = includes service tag
        my @servicetag_array = split(" ",$ses_string[5]);
        my $servicetag = $servicetag_array[$#servicetag_array];
        $servicetag =~ s/\.//g;
        $servicetag =~ s/\`//g;

        my @linearray = split(" ",$enclosure_info[$ses_hash{description}]);
        my $firmware = $linearray[2];

        #check summary status
        my $errorcount=0;
        @linearray = split(" ",$enclosure_info[$ses_hash{summary_status}]);
        foreach my $entry (@linearray) {
          next if $entry =~ m/^INFO/;  #don't warn on INFO
          $entry =~ s/,//g;
          $entry =~ s/.*=//;
          $errorcount=$errorcount + $entry;
        }
        my $checkname = join('_', "MD1200",$servicetag,"SUMMARY");
        if ($errorcount > 0) {
          print "2 $checkname - firmware $firmware $enclosure_info[$ses_hash{summary_status}]\n";
        } else {
          print "0 $checkname - firmware $firmware $enclosure_info[$ses_hash{summary_status}]\n";
        }

        sub status_check {
           my $status = $_[0];
           # Predicted failure=0, Disabled=0, Swap=0, status: OK
           my @statusarray = split(" ",$status);
           if ("$statusarray[5]" eq "OK") {
             return 0;
           } else {
             return 2;
           }
        }


#parse through our enclosure_info array

        my @element=();
        my @all_elements=(); #array of element arrays
        my $element_type; #name of the element type, for example cooling or power supply

        foreach my $linenum (6..$#enclosure_info) {
        #skip header.

           my $line = $enclosure_info[$linenum];
           $line  =~ s/^\s+//;
           if ($line =~ m/^Element type:/) {
             #start of new element type
             $line =~ s/Element type: //; #NEW ELEMENT type, strip label
             my @linearray = split(",",$line);
             $element_type = @linearray[0];
             next; # we have reset the element_type, next line should be an overall or individual element, so a new array.
          } elsif ($line =~ m/^Overall status:|^Individual element/) { #individual element
              push @all_elements, [ @element ]; #last element array is done, store it in the array of arrays
              @element=(); #start over
              push @element, $element_type; #name of new element type, first array item
          } else { #just data, add to array
              push @element, $line;
          }
        } #end FOREACH enclosure_info
        push @all_elements, [ @element ]; #add the last element array

# all_elements is complete, now just parse and output data for check_mk


        for $i (0 .. $#all_elements) {
          next if ($all_elements[$i][1] =~ m/Unsupported$/);
          next if ($all_elements[$i][1] =~ m/Unknown$/);
          next if ($all_elements[$i][0] =~ m/^\[0x80\]/);
          next if ($all_elements[$i][0] =~ m/^Language/);
          next if ($all_elements[$i][0] eq "");



          my $status = status_check($all_elements[$i][1]);

          my $value = $all_elements[$i][-1];
          my $checkitem = $all_elements[$i][0];
          $checkitem =~ s/\s/_/g;
          my $checkname = join('_', "MD1200",$servicetag,$checkitem);
          print "$status $checkname - $value\n";
        }



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




} #end foreach sg_dev

