#!/usr/bin/perl -w

#
# This script checks the hard drives on a system for S.M.A.R.T. health
# indicators.  Only supports SCSI right now.
#
# 

use strict;

my $debug = 0;
my @disk_up;
my @disk_down;
my @disks;
my $scsi_disks = `/usr/bin/sudo /sbin/sfdisk -s |/usr/bin/grep -i sd[a-z] |/usr/bin/cut -f1 -d:`;

push @disks, split(' ', $scsi_disks);

unless ( scalar @disks ) {
    print "0 No disks to monitor\n";
    exit 0;
}

print "Monitoring: @disks\n" if $debug;

for ( @disks ) {
  my $drive = $_;
  if($drive =~ /\/dev\/sd/) {
    my $val;

    $val = `/usr/bin/sudo /usr/sbin/smartctl -s on $drive &> /dev/null || /bin/echo MISSING`;
    if ( $val eq "MISSING\n" ) {
        push @disk_down, $drive;
        next;
    }

    $val = `/usr/bin/sudo /usr/sbin/smartctl -H $drive`;
    if ( $val =~ /SMART Health Status\: OK/g ) {
        print "$_ is OK\n" if $debug;
        push @disk_up, $drive;
    } else {
        print "$_ is BAD\n" if $debug;
        push @disk_down, $drive;
    }
  }
}

my $ret = 0;	# OK
if ( scalar @disk_down ) {
    print "DOWN=(@disk_down)\n";
    exit 2;
    }
print "UP=(@disk_up) " if ( scalar @disk_up );
print "DOWN=(@disk_down) " if ( scalar @disk_down );
print "\n";

exit 0;
