Script - check_smart_osx

Revised: 2009-06-03 richard

Download Script – -File, 1.4 KB

#!/usr/bin/perl -w

use strict;
use Getopt::Std;

my $smartcmd = `/usr/sbin/diskutil list | grep /dev/`;
my $version = `sw_vers | grep ProductVersion:`;
my @results = split /\n/, $smartcmd;
my $i = 0;
my $exitcrit = 1;
my $exitok = 1;
my $opt_h;

sub trim($)
{
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}

getopts("h");

if ($opt_h)
{
    print_usage();
    exit 0;
}

while ($results[$i])
    {
        $results[$i] = trim($results[$i]);
        my $smartstatus = trim(`/usr/sbin/diskutil info $results[$i] | grep SMART`);
        if ($smartstatus =~ m/Verified/)
        {
            print "OK: $smartstatus on $results[$i].<br>";
            $exitok = 0;
        }
        elsif ($smartstatus =~ m/Not Supported/)  #Comment out if you do not want to see unsupported disks
        {
            print "NOT SUPPORTED:$smartstatus on $results[$i]<br>";
            $exitok = 0;
        }
        elsif (!$smartstatus)    #Comment out if you do not want to see unsupported disks
        {
            print "NOT SUPPORTED:$smartstatus on $results[$i]<br>";
            $exitok = 0;
        }
        else
        {
            print "CRITICAL: $smartstatus on $results[$i].<br>";
            $exitcrit = 0;
        }
    $i++;     
}

if ($exitcrit =~ 0)
{
        exit 2;
}
elsif ($exitok =~ 0)
{
        exit 0;
}

sub print_usage
{
 print << "USAGE";

-----------------------------------------------------------------
Monitors SMART status on OSX

This script monitors all Volumes SMART status mounted in /dev/.

Usage: check_smart_osx

Example: check_smart_osx


USAGE
     exit 0;
}