4 # Check S.M.A.R.T. enabled disks status.
6 # This uses smartmontools to check for disk status.
7 # This is NOT compatible with smartsuite
8 # Please use smartctl --smart=on --offlineauto=on --saveauto=on /dev/something
9 # or similar to enable automatic data collection, and RTFM about it.
11 # this uses sudo to access the smartctl program, so please add a line in sudoers
12 # nagios computername = NOPASSWD:/usr/sbin/smartctl
14 # CopyLeft Roy Sigurd Karlsbakk <roy@karlsbakk.net>
15 # Developed under Debian/SID
16 # No warranties what so ever. If this toasts your PC, or your wife
17 # runs away with your girlfriend, or even me, don't blame me.
26 $s, $i, $out, $retcode, $errtxt, $exitcode,
27 $device, $text_mode, $exitcode_mode, $help, $verbose, $type,
29 my $smartctl = "sudo /usr/sbin/smartctl";
30 my $e_commandline = 0;
33 my $e_disk_failing = 0;
37 my $e_selftestlog = 0;
51 $s = "OUT OF RANGE: $s";
58 $s = shift or $s = 'Unknown';
59 printf STDERR
("Error: $s\n") unless ($help);
60 printf STDERR
("Syntax: %s (-t|-e) -d <device> [-vh]\n", $0);
61 printf STDERR
(" --text-mode -t check by parsing smartctl's output\n");
62 printf STDERR
(" --exitcode-mode -e check smartctl's exitcode (only works on IDE)\n");
63 printf STDERR
(" --device -d disk device to check\n");
64 printf STDERR
(" --type -T drive type. See the -d flag in the smartctl manual\n");
65 printf STDERR
(" --verbose -v verbose\n");
66 printf STDERR
(" --help -h this help\n");
71 Getopt
::Long
::Configure
('bundling');
73 "d=s" => \
$device, "device=s" => \
$device,
74 "T=s" => \
$type, "type=s" => \
$type,
75 "t" => \
$text_mode, "text-mode" => \
$text_mode,
76 "e" => \
$exitcode_mode, "exitcode-mode" => \
$exitcode_mode,
77 "h" => \
$help, "help" => \
$help,
78 "v" => \
$verbose, "verbose" => \
$verbose
82 syntax
("Need device to check") unless ($device);
83 syntax
("Conflicting modes") if ($text_mode && $exitcode_mode);
84 syntax
("Need test mode") unless ($text_mode || $exitcode_mode);
85 syntax
("Exitcode mode only works on ATA drives") if ($exitcode_mode && ! ($device =~ /\/dev\
/hd./));
88 $type =~ s/[\r\n]*?//g;
89 print "type: '$type'\n" if ($verbose);
90 syntax
("Valid --type entries include ata, scsi and 3ware,n")
91 unless (($type =~ /^ata$/) || ($type =~ /^scsi$/) || ($type =~ /^3ware,\d+$/));
94 $type = "--device=$type";
100 print "running $smartctl $type -H $device" if ($verbose);
101 unless (open SMARTCTL
,"$smartctl $type -H $device|") {
102 print STDERR
"Can't execute $smartctl: $!\n";
106 last if (/=== START OF READ SMART DATA SECTION ===/);
110 exit(0) if ($out =~ /PASSED/);
111 exit(2) if ($out =~ /SAVE ALL DATA/ || $out =~ /FAILED/);
113 } elsif ($exitcode_mode) {
114 print "Running $smartctl $type -q silent $device\n" if ($verbose);
115 system("$smartctl $type -q silent $device");
117 $e_commandline = 1 if ($retcode & 0x0100);
118 $e_devopen = 1 if ($retcode & 0x0200);
119 $e_chksum = 1 if ($retcode & 0x0400);
120 $e_disk_failing = 1 if ($retcode & 0x0800);
121 $e_prefail = 1 if ($retcode & 0x1000);
122 $e_mayprefail = 1 if ($retcode & 0x2000);
123 $e_errlog = 1 if ($retcode & 0x4000);
124 $e_selftestlog = 1 if ($retcode & 0x8000);
126 print "$e_commandline $e_devopen $e_chksum $e_disk_failing $e_prefail $e_mayprefail $e_errlog $e_selftestlog\n"
132 if ($e_commandline) {
133 $errtxt .= "Commandline didn't parse, ";
134 $exitcode = 3 if ($exitcode == 0);
137 $errtxt .= "Device could not be opened, ";
138 $exitcode = 3 if ($exitcode == 0);
141 $errtxt .= "Checksum failure somewhere, ";
142 $exitcode = 1 if ($exitcode != 2);
144 if ($e_disk_failing) {
145 $errtxt .= "Disk is failing!, ";
149 $errtxt .= "Disk is in prefail, ";
150 $exitcode = 1 if ($exitcode != 2);
153 $errtxt .= "Disk is close to prefail. Please check manually, ";
154 $exitcode = 1 if ($exitcode != 2);
157 $errtxt .= "The device error log contains records of errors, ";
158 $exitcode = 1 if ($exitcode != 2);
160 if ($e_selftestlog) {
161 $errtxt .= "The device self-test log contains records of errors, ";
162 $exitcode = 1 if ($exitcode != 2);
164 if ($exitcode == 1) {
165 $errtxt = "WARNNG: $errtxt";
166 } elsif ($exitcode == 2) {
167 $errtxt = "CRITICAL: $errtxt";
169 $errtxt = "UNKNOWN: $errtxt";
177 print STDERR
"Something's strange is going on :~|\n";
180 # vim:ts=4:sw=4:cindent