1 /******************************************************************************
2 * Nagios check_ide_smart plugin
6 * ide-smart 1.3 - IDE S.M.A.R.T. checking tool
7 * Copyright (C) 1998-1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>
8 * 1998 Gadi Oxman <gadio@netvision.net.il>
10 * Copyright (c) 2000 Robert Dale <rdale@digital-mission.com>
11 * Copyright (c) 2000-2006 nagios-plugins team
13 * Last Modified: $Date$
17 * This file contains the check_ide_smart plugin
19 * This plugin checks a local hard drive with the (Linux specific) SMART interface
22 * License Information:
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
41 const char *progname
= "check_ide_smart";
42 const char *revision
= "$Revision$";
43 const char *copyright
= "2000-2006";
44 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
49 void print_help (void);
50 void print_usage (void);
53 #include <sys/ioctl.h>
55 #include <linux/hdreg.h>
56 #include <linux/types.h>
59 #define NR_ATTRIBUTES 30
70 typedef struct threshold_s
76 __attribute__ ((packed
)) threshold_t
;
78 typedef struct thresholds_s
81 threshold_t thresholds
[NR_ATTRIBUTES
];
86 __attribute__ ((packed
)) thresholds_t
;
88 typedef struct value_s
95 __attribute__ ((packed
)) value_t
;
97 typedef struct values_s
100 value_t values
[NR_ATTRIBUTES
];
103 __u16 offline_timeout
;
105 __u8 offline_capability
;
106 __u16 smart_capability
;
111 __attribute__ ((packed
)) values_t
;
119 offline_status_text
[] =
121 {0x00, "NeverStarted"},
137 {SMART_ENABLE
, "SMART_ENABLE"},
138 {SMART_DISABLE
, "SMART_DISABLE"},
139 {SMART_IMMEDIATE_OFFLINE
, "SMART_IMMEDIATE_OFFLINE"},
140 {SMART_AUTO_OFFLINE
, "SMART_AUTO_OFFLINE"}
144 /* Index to smart_command table, keep in order */
148 SMART_CMD_IMMEDIATE_OFFLINE
,
149 SMART_CMD_AUTO_OFFLINE
152 void print_values (values_t
* p
, thresholds_t
* t
);
153 int smart_cmd_simple (int fd
, enum SmartCommand command
, __u8 val0
, char show_error
);
156 main (int argc
, char *argv
[])
163 thresholds_t thresholds
;
167 static struct option longopts
[] = {
168 {"device", required_argument
, 0, 'd'},
169 {"immediate", no_argument
, 0, 'i'},
170 {"quiet-check", no_argument
, 0, 'q'},
171 {"auto-on", no_argument
, 0, '1'},
172 {"auto-off", no_argument
, 0, '0'},
173 {"nagios", no_argument
, 0, 'n'},
174 {"help", no_argument
, 0, 'h'},
175 {"version", no_argument
, 0, 'V'}, {0, 0, 0, 0}
178 setlocale (LC_ALL
, "");
179 bindtextdomain (PACKAGE
, LOCALEDIR
);
180 textdomain (PACKAGE
);
184 o
= getopt_long (argc
, argv
, "+d:iq10nhV", longopts
, &longindex
);
186 if (o
== -1 || o
== EOF
|| o
== 1)
212 print_revision (progname
, revision
);
220 device
= argv
[optind
];
228 fd
= open (device
, O_RDONLY
);
231 printf (_("CRITICAL - Couldn't open device %s: %s\n"), device
, strerror (errno
));
232 return STATE_CRITICAL
;
235 if (smart_cmd_simple (fd
, SMART_CMD_ENABLE
, 0, TRUE
)) {
236 printf (_("CRITICAL - SMART_CMD_ENABLE\n"));
237 return STATE_CRITICAL
;
242 retval
= smart_cmd_simple (fd
, SMART_CMD_AUTO_OFFLINE
, 0, TRUE
);
245 retval
= smart_cmd_simple (fd
, SMART_CMD_AUTO_OFFLINE
, 0xF8, TRUE
);
248 retval
= smart_cmd_simple (fd
, SMART_CMD_IMMEDIATE_OFFLINE
, 0, TRUE
);
251 smart_read_values (fd
, &values
);
252 smart_read_thresholds (fd
, &thresholds
);
253 retval
= values_not_passed (&values
, &thresholds
);
256 smart_read_values (fd
, &values
);
257 smart_read_thresholds (fd
, &thresholds
);
258 retval
= nagios (&values
, &thresholds
);
261 smart_read_values (fd
, &values
);
262 smart_read_thresholds (fd
, &thresholds
);
263 print_values (&values
, &thresholds
);
273 get_offline_text (int status
)
276 for (i
= 0; offline_status_text
[i
].text
; i
++) {
277 if (offline_status_text
[i
].value
== status
) {
278 return offline_status_text
[i
].text
;
287 smart_read_values (int fd
, values_t
* values
)
293 args
[2] = SMART_READ_VALUES
;
295 if (ioctl (fd
, HDIO_DRIVE_CMD
, &args
)) {
297 printf (_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror (errno
));
300 memcpy (values
, args
+ 4, 512);
307 values_not_passed (values_t
* p
, thresholds_t
* t
)
309 value_t
* value
= p
->values
;
310 threshold_t
* threshold
= t
->thresholds
;
314 for (i
= 0; i
< NR_ATTRIBUTES
; i
++) {
315 if (value
->id
&& threshold
->id
&& value
->id
== threshold
->id
) {
316 if (value
->value
<= threshold
->threshold
) {
326 return (passed
? -failed
: 2);
332 nagios (values_t
* p
, thresholds_t
* t
)
334 value_t
* value
= p
->values
;
335 threshold_t
* threshold
= t
->thresholds
;
336 int status
= OPERATIONAL
;
343 for (i
= 0; i
< NR_ATTRIBUTES
; i
++) {
344 if (value
->id
&& threshold
->id
&& value
->id
== threshold
->id
) {
345 if (value
->value
<= threshold
->threshold
) {
347 if (value
->status
& 1) {
366 printf (_("CRITICAL - %d Harddrive PreFailure%cDetected! %d/%d tests failed.\n"),
368 prefailure
> 1 ? 's' : ' ',
371 status
=STATE_CRITICAL
;
374 printf (_("WARNING - %d Harddrive Advisor%s Detected. %d/%d tests failed.\n"),
376 advisory
> 1 ? "ies" : "y",
379 status
=STATE_WARNING
;
382 printf (_("OK - Operational (%d/%d tests passed)\n"), passed
, total
);
386 printf (_("ERROR - Status '%d' unkown. %d/%d tests passed\n"), status
,
388 status
= STATE_UNKNOWN
;
397 print_value (value_t
* p
, threshold_t
* t
)
399 printf ("Id=%3d, Status=%2d {%s , %s}, Value=%3d, Threshold=%3d, %s\n",
400 p
->id
, p
->status
, p
->status
& 1 ? "PreFailure" : "Advisory ",
401 p
->status
& 2 ? "OnLine " : "OffLine", p
->value
, t
->threshold
,
402 p
->value
> t
->threshold
? "Passed" : "Failed");
408 print_values (values_t
* p
, thresholds_t
* t
)
410 value_t
* value
= p
->values
;
411 threshold_t
* threshold
= t
->thresholds
;
413 for (i
= 0; i
< NR_ATTRIBUTES
; i
++) {
414 if (value
->id
&& threshold
->id
&& value
->id
== threshold
->id
) {
415 print_value (value
++, threshold
++);
419 (_("OffLineStatus=%d {%s}, AutoOffLine=%s, OffLineTimeout=%d minutes\n"),
421 get_offline_text (p
->offline_status
& 0x7f),
422 (p
->offline_status
& 0x80 ? "Yes" : "No"),
423 p
->offline_timeout
/ 60);
425 (_("OffLineCapability=%d {%s %s %s}\n"),
426 p
->offline_capability
,
427 p
->offline_capability
& 1 ? "Immediate" : "",
428 p
->offline_capability
& 2 ? "Auto" : "",
429 p
->offline_capability
& 4 ? "AbortOnCmd" : "SuspendOnCmd");
431 (_("SmartRevision=%d, CheckSum=%d, SmartCapability=%d {%s %s}\n"),
435 p
->smart_capability
& 1 ? "SaveOnStandBy" : "",
436 p
->smart_capability
& 2 ? "AutoSave" : "");
441 smart_cmd_simple (int fd
, enum SmartCommand command
, __u8 val0
, char show_error
)
447 args
[2] = smart_command
[command
].value
;
449 if (ioctl (fd
, HDIO_DRIVE_CMD
, &args
)) {
452 printf (_("CRITICAL - %s: %s\n"), smart_command
[command
].text
, strerror (errno
));
461 smart_read_thresholds (int fd
, thresholds_t
* thresholds
)
467 args
[2] = SMART_READ_THRESHOLDS
;
469 if (ioctl (fd
, HDIO_DRIVE_CMD
, &args
)) {
471 printf (_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror (errno
));
474 memcpy (thresholds
, args
+ 4, 512);
482 print_revision (progname
, revision
);
484 printf ("Nagios feature - 1999 Robert Dale <rdale@digital-mission.com>\n");
485 printf ("(C) 1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>\n");
486 printf (COPYRIGHT
, copyright
, email
);
488 printf (_("This plugin checks a local hard drive with the (Linux specific) SMART interface [http://smartlinux.sourceforge.net/smart/index.php]."));
494 printf (_(UT_HELP_VRSN
));
496 printf (" %s\n", "-d, --device=DEVICE");
497 printf (" %s\n", _("Select device DEVICE"));
498 printf (" %s\n", _("Note: if the device is selected with this option, _no_ other options are accepted"));
499 printf (" %s\n", "-i, --immediate");
500 printf (" %s\n", _("Perform immediately offline tests"));
501 printf (" %s\n", "-q, --quiet-check");
502 printf (" %s\n", _("Returns the number of failed tests"));
503 printf (" %s\n", "-1, --auto-on");
504 printf (" %s\n", _("Turn on automatic offline tests"));
505 printf (" %s\n", "-0, --auto-off");
506 printf (" %s\n", _("Turn off automatic offline tests"));
507 printf (" %s\n", "-n, --nagios");
508 printf (" %s\n\n", _("Output suitable for Nagios"));
509 printf (_(UT_SUPPORT
));
512 /* todo : add to the long nanual as example
514 * Run with: check_ide-smart --nagios [-d] <DRIVE>
515 * Where DRIVE is an IDE drive, ie. /dev/hda, /dev/hdb, /dev/hdc
517 * - Returns 0 on no errors
518 * - Returns 1 on advisories
519 * - Returns 2 on prefailure
520 * - Returns -1 not too often
527 printf (_("Usage:"));
528 printf ("%s [-d <device>] [-i <immediate>] [-q quiet] [-1 <auto-on>]",progname
);
529 printf (" [-O <auto-off>] [-n <nagios>]\n");