Fix Debian bug #534604: Checking md10 and above
[monitoring-plugins.git] / contrib / check_smart.pl
blob3f9104d8e751a73249257d2bf986ded60c0c20d5
1 #!/usr/bin/perl -w
3 # chec_smart
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.
19 # Licenced under GPL
22 use strict;
23 use Getopt::Long;
25 my (
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;
31 my $e_devopen = 0;
32 my $e_chksum = 0;
33 my $e_disk_failing = 0;
34 my $e_prefail = 0;
35 my $e_mayprefail = 0;
36 my $e_errlog = 0;
37 my $e_selftestlog = 0;
39 sub end {
40 $s = shift;
41 $i = shift;
42 if ($i == 0) {
43 $s = "OK: $s";
44 } elsif ($i == 1) {
45 $s = "WARNNG: $s";
46 } elsif ($i == 2) {
47 $s = "CRITICAL: $s";
48 } elsif ($i == 3) {
49 $s = "UNKNOWN: $s";
50 } else {
51 $s = "OUT OF RANGE: $s";
53 print "$s\n";
54 exit($i);
57 sub syntax {
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");
67 exit(0) if $help;
68 exit(3);
71 Getopt::Long::Configure('bundling');
72 GetOptions(
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
79 ) || syntax("RTFM!");
81 syntax if ($help);
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./));
87 if ($type) {
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+$/));
93 if (defined($type)) {
94 $type = "--device=$type";
95 } else {
96 $type = "";
99 if ($text_mode) {
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";
103 exit(3);
105 while (<SMARTCTL>) {
106 last if (/=== START OF READ SMART DATA SECTION ===/);
108 $out = <SMARTCTL>;
109 print $out;
110 exit(0) if ($out =~ /PASSED/);
111 exit(2) if ($out =~ /SAVE ALL DATA/ || $out =~ /FAILED/);
112 exit(3);
113 } elsif ($exitcode_mode) {
114 print "Running $smartctl $type -q silent $device\n" if ($verbose);
115 system("$smartctl $type -q silent $device");
116 $retcode = $?;
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"
127 if ($verbose);
129 $exitcode = 0;
130 $errtxt = "";
131 if ($exitcode) {
132 if ($e_commandline) {
133 $errtxt .= "Commandline didn't parse, ";
134 $exitcode = 3 if ($exitcode == 0);
136 if ($e_devopen) {
137 $errtxt .= "Device could not be opened, ";
138 $exitcode = 3 if ($exitcode == 0);
140 if ($e_chksum) {
141 $errtxt .= "Checksum failure somewhere, ";
142 $exitcode = 1 if ($exitcode != 2);
144 if ($e_disk_failing) {
145 $errtxt .= "Disk is failing!, ";
146 $exitcode = 2;
148 if ($e_prefail) {
149 $errtxt .= "Disk is in prefail, ";
150 $exitcode = 1 if ($exitcode != 2);
152 if ($e_mayprefail) {
153 $errtxt .= "Disk is close to prefail. Please check manually, ";
154 $exitcode = 1 if ($exitcode != 2);
156 if ($e_errlog) {
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";
168 } else {
169 $errtxt = "UNKNOWN: $errtxt";
171 } else {
172 $errtxt = "OK";
174 print "$errtxt\n";
175 exit($exitcode);
176 } else {
177 print STDERR "Something's strange is going on :~|\n";
178 exit(3);
180 # vim:ts=4:sw=4:cindent