Code

Trivial text changes (print_help, commentary & -n option)
[nagiosplug.git] / plugins / check_ide_smart.c
1 /*
2  *  check_ide-smart v.1 - hacked version of ide-smart for Nagios
3  *  Copyright (C) 2000 Robert Dale <rdale@digital-mission.com>
4  *
5  *  Nagios - http://www.nagios.org
6  *
7  *  Notes:
8  *         ide-smart has the same functionality as before. Some return
9  *         values were changed, otherwise the --nagios option was added.
10  *
11  *         Run with:  check_ide-smart --nagios [-d] <DRIVE>
12  *         Where DRIVE is an IDE drive, ie. /dev/hda, /dev/hdb, /dev/hdc
13  *
14  *           - Returns 0 on no errors
15  *           - Returns 1 on advisories
16  *           - Returns 2 on prefailure
17  *           - Returns -1 not too often
18  *
19  *  ide-smart 1.3 - IDE S.M.A.R.T. checking tool
20  *  Copyright (C) 1998-1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>
21  *                1998      Gadi Oxman <gadio@netvision.net.il>
22  *
23  *  This program is free software; you can redistribute it and/or modify
24  *  it under the terms of the GNU General Public License as published by
25  *  the Free Software Foundation; either version 2 of the License, or
26  *  (at your option) any later version.
27  *
28  *  This program is distributed in the hope that it will be useful,
29  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
30  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  *  GNU General Public License for more details.
32  *
33  *  You should have received a copy of the GNU General Public License
34  *  along with this program; if not, write to the Free Software
35  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36  *
37  * $Id$
38  */
40 const char *progname = "check_ide_smart";
41 const char *revision = "$Revision$";
42 const char *copyright = "2000-2004";
43 const char *email = "nagiosplug-devel@lists.sourceforge.net";
44         
45 #include "common.h"
46 #include "utils.h"
48 void print_help (void);
49 void print_usage (void);
51 #include <sys/stat.h>
52 #include <sys/ioctl.h>
53 #include <fcntl.h>
54 #include <linux/hdreg.h>
55 #include <linux/types.h>
56 #include <errno.h>
57         
58 #define NR_ATTRIBUTES   30
59         
60 #ifndef TRUE
61 #define TRUE 1
62 #endif  /*  */
63         
64 #define PREFAILURE 2
65 #define ADVISORY 1
66 #define OPERATIONAL 0
67 #define UNKNOWN -1
69 typedef struct threshold_s
70 {
71         __u8 id;
72         __u8 threshold;
73         __u8 reserved[10];
74 }
75 __attribute__ ((packed)) threshold_t;
77 typedef struct thresholds_s
78 {
79         __u16 revision;
80         threshold_t thresholds[NR_ATTRIBUTES];
81         __u8 reserved[18];
82         __u8 vendor[131];
83         __u8 checksum;
84 }
85 __attribute__ ((packed)) thresholds_t;
87 typedef struct value_s
88 {
89         __u8 id;
90         __u16 status;
91         __u8 value;
92         __u8 vendor[8];
93 }
94 __attribute__ ((packed)) value_t;
96 typedef struct values_s
97 {
98         __u16 revision;
99         value_t values[NR_ATTRIBUTES];
100         __u8 offline_status;
101         __u8 vendor1;
102         __u16 offline_timeout;
103         __u8 vendor2;
104         __u8 offline_capability;
105         __u16 smart_capability;
106         __u8 reserved[16];
107         __u8 vendor[125];
108         __u8 checksum;
110 __attribute__ ((packed)) values_t;
112 struct
114         __u8 value;
115         char *text;
118 offline_status_text[] =
119         {
120                 {0x00, "NeverStarted"},
121                 {0x02, "Completed"},
122                 {0x04, "Suspended"},
123                 {0x05, "Aborted"},
124                 {0x06, "Failed"},
125                 {0, 0}
126         };
128 struct
130         __u8 value;
131         char *text;
134 smart_command[] =
135         {
136                 {SMART_ENABLE, "SMART_ENABLE"},
137                 {SMART_DISABLE, "SMART_DISABLE"},
138                 {SMART_IMMEDIATE_OFFLINE, "SMART_IMMEDIATE_OFFLINE"},
139                 {SMART_AUTO_OFFLINE, "SMART_AUTO_OFFLINE"}
140         };
143 /* Index to smart_command table, keep in order */ 
144 enum SmartCommand 
145         { SMART_CMD_ENABLE,
146                 SMART_CMD_DISABLE,
147                 SMART_CMD_IMMEDIATE_OFFLINE,
148                 SMART_CMD_AUTO_OFFLINE 
149         };
151 void print_values (values_t * p, thresholds_t * t);
152 int smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0, char show_error); 
154 int
155 main (int argc, char *argv[]) 
157         char *device = NULL;
158         int command = -1;
159         int o, longindex;
160         int retval = 0;
162         thresholds_t thresholds;
163         values_t values;
164         int fd;
166         static struct option longopts[] = { 
167                 {"device", required_argument, 0, 'd'}, 
168                 {"immediate", no_argument, 0, 'i'}, 
169                 {"quiet-check", no_argument, 0, 'q'}, 
170                 {"auto-on", no_argument, 0, '1'}, 
171                 {"auto-off", no_argument, 0, '0'}, 
172                 {"nagios", no_argument, 0, 'n'}, 
173                 {"help", no_argument, 0, 'h'}, 
174                 {"version", no_argument, 0, 'V'}, {0, 0, 0, 0} 
175         };
177         setlocale (LC_ALL, "");
178         bindtextdomain (PACKAGE, LOCALEDIR);
179         textdomain (PACKAGE);
181         while (1) {
182                 
183                 o = getopt_long (argc, argv, "+d:iq10nhV", longopts, &longindex);
184                 
185                 if (o == -1 || o == EOF)
186                         break;
188                 switch (o) {
189                 case 'd':
190                         device = optarg;
191                         break;
192                 case 'q':
193                         command = 3;
194                         break;
195                 case 'i':
196                         command = 2;
197                         break;
198                 case '1':
199                         command = 1;
200                         break;
201                 case '0':
202                         command = 0;
203                         break;
204                 case 'n':
205                         command = 4;
206                         break;
207                 case 'h':
208                         print_help ();
209                         return STATE_OK;
210                 case 'V':
211                         print_revision (progname, revision);
212                         return STATE_OK;
213                 default:
214                         usage2 (_("Unknown argument"), optarg);
215                 }
217                 if (optind < argc) {
218                         device = argv[optind];
219                 }
221                 if (!device) {
222                         print_help ();
223                         return -1;
224                 }
226                 fd = open (device, O_RDONLY);
228                 if (fd < 0) {
229                         printf (_("CRITICAL - Couldn't open device: %s\n"), strerror (errno));
230                         return 2;
231                 }
233                 if (smart_cmd_simple (fd, SMART_CMD_ENABLE, 0, TRUE)) {
234                         printf (_("CRITICAL - SMART_CMD_ENABLE\n"));
235                         return 2;
236                 }
238                 switch (command) {
239                 case 0:
240                         retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0, TRUE);
241                         break;
242                 case 1:
243                         retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0xF8, TRUE);
244                         break;
245                 case 2:
246                         retval = smart_cmd_simple (fd, SMART_CMD_IMMEDIATE_OFFLINE, 0, TRUE);
247                         break;
248                 case 3:
249                         smart_read_values (fd, &values);
250                         smart_read_thresholds (fd, &thresholds);
251                         retval = values_not_passed (&values, &thresholds);
252                         break;
253                 case 4:
254                         smart_read_values (fd, &values);
255                         smart_read_thresholds (fd, &thresholds);
256                         retval = nagios (&values, &thresholds);
257                         break;
258                 default:
259                         smart_read_values (fd, &values);
260                         smart_read_thresholds (fd, &thresholds);
261                         print_values (&values, &thresholds);
262                         break;
263                 }
264                 close (fd);
265         }
266         return retval;
271 char *
272 get_offline_text (int status) 
274         int i;
275         for (i = 0; offline_status_text[i].text; i++) {
276                 if (offline_status_text[i].value == status) {
277                         return offline_status_text[i].text;
278                 }
279         }
280         return "UNKNOW";
285 int
286 smart_read_values (int fd, values_t * values) 
288         int e;
289         __u8 args[4 + 512];
290         args[0] = WIN_SMART;
291         args[1] = 0;
292         args[2] = SMART_READ_VALUES;
293         args[3] = 1;
294         if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
295                 e = errno;
296                 printf (_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror (errno));
297                 return e;
298         }
299         memcpy (values, args + 4, 512);
300         return 0;
305 int
306 values_not_passed (values_t * p, thresholds_t * t) 
308         value_t * value = p->values;
309         threshold_t * threshold = t->thresholds;
310         int failed = 0;
311         int passed = 0;
312         int i;
313         for (i = 0; i < NR_ATTRIBUTES; i++) {
314                 if (value->id && threshold->id && value->id == threshold->id) {
315                         if (value->value <= threshold->threshold) {
316                                 ++failed;
317                         }
318                         else {
319                                 ++passed;
320                         }
321                 }
322                 ++value;
323                 ++threshold;
324         }
325         return (passed ? -failed : 2);
330 int
331 nagios (values_t * p, thresholds_t * t) 
333         value_t * value = p->values;
334         threshold_t * threshold = t->thresholds;
335         int status = OPERATIONAL;
336         int prefailure = 0;
337         int advisory = 0;
338         int failed = 0;
339         int passed = 0;
340         int total = 0;
341         int i;
342         for (i = 0; i < NR_ATTRIBUTES; i++) {
343                 if (value->id && threshold->id && value->id == threshold->id) {
344                         if (value->value <= threshold->threshold) {
345                                 ++failed;
346                                 if (value->status & 1) {
347                                         status = PREFAILURE;
348                                         ++prefailure;
349                                 }
350                                 else {
351                                         status = ADVISORY;
352                                         ++advisory;
353                                 }
354                         }
355                         else {
356                                 ++passed;
357                         }
358                         ++total;
359                 }
360                 ++value;
361                 ++threshold;
362         }
363         switch (status) {
364         case PREFAILURE:
365                 printf (_("CRITICAL - %d Harddrive PreFailure%cDetected! %d/%d tests failed.\n"),
366                         prefailure,
367                         prefailure > 1 ? 's' : ' ',
368                         failed,
369                   total);
370                 break;
371         case ADVISORY:
372                 printf (_("WARNING - %d Harddrive Advisor%s Detected. %d/%d tests failed.\n"),
373                         advisory,
374                         advisory > 1 ? "ies" : "y",
375                         failed,
376                         total);
377                 break;
378         case OPERATIONAL:
379                 printf (_("OK - Operational (%d/%d tests passed)\n"), passed, total);
380                 break;
381         default:
382                 printf (_("ERROR - Status '%d' uknown. %d/%d tests passed\n"), status,
383                                                 passed, total);
384                 status = -1;
385                 break;
386         }
387         return status;
392 void
393 print_value (value_t * p, threshold_t * t) 
395         printf ("Id=%3d, Status=%2d {%s , %s}, Value=%3d, Threshold=%3d, %s\n",
396                                         p->id, p->status, p->status & 1 ? "PreFailure" : "Advisory   ",
397                                         p->status & 2 ? "OnLine " : "OffLine", p->value, t->threshold,
398                                         p->value > t->threshold ? "Passed" : "Failed");
403 void
404 print_values (values_t * p, thresholds_t * t)
406         value_t * value = p->values;
407         threshold_t * threshold = t->thresholds;
408         int i;
409         for (i = 0; i < NR_ATTRIBUTES; i++) {
410                 if (value->id && threshold->id && value->id == threshold->id) {
411                         print_value (value++, threshold++);
412                 }
413         }
414         printf
415                 (_("OffLineStatus=%d {%s}, AutoOffLine=%s, OffLineTimeout=%d minutes\n"),
416                  p->offline_status,
417                  get_offline_text (p->offline_status & 0x7f),
418                  (p->offline_status & 0x80 ? "Yes" : "No"),
419                  p->offline_timeout / 60);
420         printf
421                 (_("OffLineCapability=%d {%s %s %s}\n"),
422                  p->offline_capability,
423                  p->offline_capability & 1 ? "Immediate" : "",
424                  p->offline_capability & 2 ? "Auto" : "",
425                  p->offline_capability & 4 ? "AbortOnCmd" : "SuspendOnCmd");
426         printf
427                 (_("SmartRevision=%d, CheckSum=%d, SmartCapability=%d {%s %s}\n"),
428                  p->revision,
429                  p->checksum,
430                  p->smart_capability,
431                  p->smart_capability & 1 ? "SaveOnStandBy" : "",
432                  p->smart_capability & 2 ? "AutoSave" : "");
437 void
438 print_thresholds (thresholds_t * p) 
440         threshold_t * threshold = p->thresholds;
441         int i;
442         printf ("\n");
443         printf ("SmartRevision=%d\n", p->revision);
444         for (i = 0; i < NR_ATTRIBUTES; i++) {
445                 if (threshold->id) {
446                         printf ("Id=%3d, Threshold=%3d\n", threshold->id,
447                                                         threshold->threshold); }
448                 ++threshold;
449         }
450         printf ("CheckSum=%d\n", p->checksum);
453 int
454 smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0, char show_error) 
456         int e = 0;
457         __u8 args[4];
458         args[0] = WIN_SMART;
459         args[1] = val0;
460         args[2] = smart_command[command].value;
461         args[3] = 0;
462         if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
463                 e = errno;
464                 if (show_error) {
465                         printf (_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror (errno));
466                 }
467         }
468         return e;
473 int
474 smart_read_thresholds (int fd, thresholds_t * thresholds) 
476         int e;
477         __u8 args[4 + 512];
478         args[0] = WIN_SMART;
479   args[1] = 0;
480   args[2] = SMART_READ_THRESHOLDS;
481   args[3] = 1;
482         if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
483                 e = errno;
484                 printf (_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror (errno));
485                 return e;
486         }
487         memcpy (thresholds, args + 4, 512);
488         return 0;
492 void
493 print_help (void)
495         print_revision (progname, revision);
497         printf ("Nagios feature - 1999 Robert Dale <rdale@digital-mission.com>\n");
498         printf ("(C) 1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>\n");
499         printf (COPYRIGHT, copyright, email);
501         printf(_("This plugin checks this host's IDE hard drive through the (Linux specific) SMART command interface.\n\n"));
502         
503         printf ("\
504 Usage: %s [DEVICE] [OPTION]\n\
505  -d, --device=DEVICE\n\
506     Select device DEVICE\n\
507  -i, --immediate\n\
508     Perform immediately offline tests\n\
509  -q, --quiet-check\n\
510     Returns the number of failed tests\n\
511  -1, --auto-on\n\
512     Turn on automatic offline tests\n\
513  -0, --auto-off\n\
514     Turn off automatic offline tests\n\
515  -n, --nagios\n\
516     Output suitable for Nagios\n", progname);
520 void
521 print_usage (void)
523         printf ("\
524 Usage: %s [-d <device>] [-i <immediate>] [-q quiet] [-1 <auto-on>]\n\
525                         [-O <auto-off>] [-n <nagios>]\n", progname);