Code

Fix long options parsing in check_disk, check_dns, check_mrtg and check_mrtgtraf...
[nagiosplug.git] / plugins / check_ide_smart.c
1 /*****************************************************************************
2
3 * Nagios check_ide_smart plugin
4 * ide-smart 1.3 - IDE S.M.A.R.T. checking tool
5
6 * License: GPL
7 * Copyright (C) 1998-1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>
8 *               1998      Gadi Oxman <gadio@netvision.net.il>
9 * Copyright (c) 2000 Robert Dale <rdale@digital-mission.com>
10 * Copyright (c) 2000-2007 Nagios Plugins Development Team
11
12 * Last Modified: $Date$
13
14 * Description:
15
16 * This file contains the check_ide_smart plugin
17
18 * This plugin checks a local hard drive with the (Linux specific) SMART
19 * interface
20
21
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
26
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30 * GNU General Public License for more details.
31
32 * You should have received a copy of the GNU General Public License
33 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34
35 * $Id$
36
37 *****************************************************************************/
39 const char *progname = "check_ide_smart";
40 const char *revision = "$Revision$";
41 const char *copyright = "1998-2007";
42 const char *email = "nagiosplug-devel@lists.sourceforge.net";
43         
44 #include "common.h"
45 #include "utils.h"
47 void print_help (void);
48 void print_usage (void);
50 #include <sys/stat.h>
51 #include <sys/ioctl.h>
52 #include <fcntl.h>
53 #include <linux/hdreg.h>
54 #include <linux/types.h>
55 #include <errno.h>
56         
57 #define NR_ATTRIBUTES   30
58         
59 #ifndef TRUE
60 #define TRUE 1
61 #endif  /*  */
62         
63 #define PREFAILURE 2
64 #define ADVISORY 1
65 #define OPERATIONAL 0
66 #define UNKNOWN -1
68 typedef struct threshold_s
69 {
70         __u8 id;
71         __u8 threshold;
72         __u8 reserved[10];
73 }
74 __attribute__ ((packed)) threshold_t;
76 typedef struct thresholds_s
77 {
78         __u16 revision;
79         threshold_t thresholds[NR_ATTRIBUTES];
80         __u8 reserved[18];
81         __u8 vendor[131];
82         __u8 checksum;
83 }
84 __attribute__ ((packed)) thresholds_t;
86 typedef struct value_s
87 {
88         __u8 id;
89         __u16 status;
90         __u8 value;
91         __u8 vendor[8];
92 }
93 __attribute__ ((packed)) value_t;
95 typedef struct values_s
96 {
97         __u16 revision;
98         value_t values[NR_ATTRIBUTES];
99         __u8 offline_status;
100         __u8 vendor1;
101         __u16 offline_timeout;
102         __u8 vendor2;
103         __u8 offline_capability;
104         __u16 smart_capability;
105         __u8 reserved[16];
106         __u8 vendor[125];
107         __u8 checksum;
109 __attribute__ ((packed)) values_t;
111 struct
113         __u8 value;
114         char *text;
117 offline_status_text[] =
118         {
119                 {0x00, "NeverStarted"},
120                 {0x02, "Completed"},
121                 {0x04, "Suspended"},
122                 {0x05, "Aborted"},
123                 {0x06, "Failed"},
124                 {0, 0}
125         };
127 struct
129         __u8 value;
130         char *text;
133 smart_command[] =
134         {
135                 {SMART_ENABLE, "SMART_ENABLE"},
136                 {SMART_DISABLE, "SMART_DISABLE"},
137                 {SMART_IMMEDIATE_OFFLINE, "SMART_IMMEDIATE_OFFLINE"},
138                 {SMART_AUTO_OFFLINE, "SMART_AUTO_OFFLINE"}
139         };
142 /* Index to smart_command table, keep in order */ 
143 enum SmartCommand 
144         { SMART_CMD_ENABLE,
145                 SMART_CMD_DISABLE,
146                 SMART_CMD_IMMEDIATE_OFFLINE,
147                 SMART_CMD_AUTO_OFFLINE 
148         };
150 void print_values (values_t * p, thresholds_t * t);
151 int smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0, char show_error); 
153 int
154 main (int argc, char *argv[]) 
156         char *device = NULL;
157         int command = -1;
158         int o, longindex;
159         int retval = 0;
161         thresholds_t thresholds;
162         values_t values;
163         int fd;
165         /* Parse extra opts if any */
166         argv=np_extra_opts (&argc, argv, progname);
168         static struct option longopts[] = { 
169                 {"device", required_argument, 0, 'd'}, 
170                 {"immediate", no_argument, 0, 'i'}, 
171                 {"quiet-check", no_argument, 0, 'q'}, 
172                 {"auto-on", no_argument, 0, '1'}, 
173                 {"auto-off", no_argument, 0, '0'}, 
174                 {"nagios", no_argument, 0, 'n'}, 
175                 {"help", no_argument, 0, 'h'}, 
176                 {"version", no_argument, 0, 'V'},
177                 {0, 0, 0, 0}
178         };
180         setlocale (LC_ALL, "");
181         bindtextdomain (PACKAGE, LOCALEDIR);
182         textdomain (PACKAGE);
184         while (1) {
185                 
186                 o = getopt_long (argc, argv, "+d:iq10nhV", longopts, &longindex);
188                 if (o == -1 || o == EOF || o == 1)
189                         break;
191                 switch (o) {
192                 case 'd':
193                         device = optarg;
194                         break;
195                 case 'q':
196                         command = 3;
197                         break;
198                 case 'i':
199                         command = 2;
200                         break;
201                 case '1':
202                         command = 1;
203                         break;
204                 case '0':
205                         command = 0;
206                         break;
207                 case 'n':
208                         command = 4;
209                         break;
210                 case 'h':
211                         print_help ();
212                         return STATE_OK;
213                 case 'V':
214                         print_revision (progname, revision);
215                         return STATE_OK;
216                 default:
217                         usage5 ();
218                 }
219         }
221         if (optind < argc) {
222                 device = argv[optind];
223         }
225         if (!device) {
226                 print_help ();
227                 return STATE_OK;
228         }
230         fd = open (device, O_RDONLY);
232         if (fd < 0) {
233                 printf (_("CRITICAL - Couldn't open device %s: %s\n"), device, strerror (errno));
234                 return STATE_CRITICAL;
235         }
237         if (smart_cmd_simple (fd, SMART_CMD_ENABLE, 0, TRUE)) {
238                 printf (_("CRITICAL - SMART_CMD_ENABLE\n"));
239                 return STATE_CRITICAL;
240         }
242         switch (command) {
243         case 0:
244                 retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0, TRUE);
245                 break;
246         case 1:
247                 retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0xF8, TRUE);
248                 break;
249         case 2:
250                 retval = smart_cmd_simple (fd, SMART_CMD_IMMEDIATE_OFFLINE, 0, TRUE);
251                 break;
252         case 3:
253                 smart_read_values (fd, &values);
254                 smart_read_thresholds (fd, &thresholds);
255                 retval = values_not_passed (&values, &thresholds);
256                 break;
257         case 4:
258                 smart_read_values (fd, &values);
259                 smart_read_thresholds (fd, &thresholds);
260                 retval = nagios (&values, &thresholds);
261                 break;
262         default:
263                 smart_read_values (fd, &values);
264                 smart_read_thresholds (fd, &thresholds);
265                 print_values (&values, &thresholds);
266                 break;
267         }
268         close (fd);
269         return retval;
274 char *
275 get_offline_text (int status) 
277         int i;
278         for (i = 0; offline_status_text[i].text; i++) {
279                 if (offline_status_text[i].value == status) {
280                         return offline_status_text[i].text;
281                 }
282         }
283         return "UNKNOW";
288 int
289 smart_read_values (int fd, values_t * values) 
291         int e;
292         __u8 args[4 + 512];
293         args[0] = WIN_SMART;
294         args[1] = 0;
295         args[2] = SMART_READ_VALUES;
296         args[3] = 1;
297         if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
298                 e = errno;
299                 printf (_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror (errno));
300                 return e;
301         }
302         memcpy (values, args + 4, 512);
303         return 0;
308 int
309 values_not_passed (values_t * p, thresholds_t * t) 
311         value_t * value = p->values;
312         threshold_t * threshold = t->thresholds;
313         int failed = 0;
314         int passed = 0;
315         int i;
316         for (i = 0; i < NR_ATTRIBUTES; i++) {
317                 if (value->id && threshold->id && value->id == threshold->id) {
318                         if (value->value <= threshold->threshold) {
319                                 ++failed;
320                         }
321                         else {
322                                 ++passed;
323                         }
324                 }
325                 ++value;
326                 ++threshold;
327         }
328         return (passed ? -failed : 2);
333 int
334 nagios (values_t * p, thresholds_t * t) 
336         value_t * value = p->values;
337         threshold_t * threshold = t->thresholds;
338         int status = OPERATIONAL;
339         int prefailure = 0;
340         int advisory = 0;
341         int failed = 0;
342         int passed = 0;
343         int total = 0;
344         int i;
345         for (i = 0; i < NR_ATTRIBUTES; i++) {
346                 if (value->id && threshold->id && value->id == threshold->id) {
347                         if (value->value <= threshold->threshold) {
348                                 ++failed;
349                                 if (value->status & 1) {
350                                         status = PREFAILURE;
351                                         ++prefailure;
352                                 }
353                                 else {
354                                         status = ADVISORY;
355                                         ++advisory;
356                                 }
357                         }
358                         else {
359                                 ++passed;
360                         }
361                         ++total;
362                 }
363                 ++value;
364                 ++threshold;
365         }
366         switch (status) {
367         case PREFAILURE:
368                 printf (_("CRITICAL - %d Harddrive PreFailure%cDetected! %d/%d tests failed.\n"),
369                         prefailure,
370                         prefailure > 1 ? 's' : ' ',
371                         failed,
372                   total);
373                 status=STATE_CRITICAL;
374                 break;
375         case ADVISORY:
376                 printf (_("WARNING - %d Harddrive Advisor%s Detected. %d/%d tests failed.\n"),
377                         advisory,
378                         advisory > 1 ? "ies" : "y",
379                         failed,
380                         total);
381                 status=STATE_WARNING;
382                 break;
383         case OPERATIONAL:
384                 printf (_("OK - Operational (%d/%d tests passed)\n"), passed, total);
385                 status=STATE_OK;
386                 break;
387         default:
388                 printf (_("ERROR - Status '%d' unkown. %d/%d tests passed\n"), status,
389                                                 passed, total);
390                 status = STATE_UNKNOWN;
391                 break;
392         }
393         return status;
398 void
399 print_value (value_t * p, threshold_t * t) 
401         printf ("Id=%3d, Status=%2d {%s , %s}, Value=%3d, Threshold=%3d, %s\n",
402                                         p->id, p->status, p->status & 1 ? "PreFailure" : "Advisory   ",
403                                         p->status & 2 ? "OnLine " : "OffLine", p->value, t->threshold,
404                                         p->value > t->threshold ? "Passed" : "Failed");
409 void
410 print_values (values_t * p, thresholds_t * t)
412         value_t * value = p->values;
413         threshold_t * threshold = t->thresholds;
414         int i;
415         for (i = 0; i < NR_ATTRIBUTES; i++) {
416                 if (value->id && threshold->id && value->id == threshold->id) {
417                         print_value (value++, threshold++);
418                 }
419         }
420         printf
421                 (_("OffLineStatus=%d {%s}, AutoOffLine=%s, OffLineTimeout=%d minutes\n"),
422                  p->offline_status,
423                  get_offline_text (p->offline_status & 0x7f),
424                  (p->offline_status & 0x80 ? "Yes" : "No"),
425                  p->offline_timeout / 60);
426         printf
427                 (_("OffLineCapability=%d {%s %s %s}\n"),
428                  p->offline_capability,
429                  p->offline_capability & 1 ? "Immediate" : "",
430                  p->offline_capability & 2 ? "Auto" : "",
431                  p->offline_capability & 4 ? "AbortOnCmd" : "SuspendOnCmd");
432         printf
433                 (_("SmartRevision=%d, CheckSum=%d, SmartCapability=%d {%s %s}\n"),
434                  p->revision,
435                  p->checksum,
436                  p->smart_capability,
437                  p->smart_capability & 1 ? "SaveOnStandBy" : "",
438                  p->smart_capability & 2 ? "AutoSave" : "");
442 int
443 smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0, char show_error) 
445         int e = 0;
446         __u8 args[4];
447         args[0] = WIN_SMART;
448         args[1] = val0;
449         args[2] = smart_command[command].value;
450         args[3] = 0;
451         if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
452                 e = errno;
453                 if (show_error) {
454                         printf (_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror (errno));
455                 }
456         }
457         return e;
462 int
463 smart_read_thresholds (int fd, thresholds_t * thresholds) 
465         int e;
466         __u8 args[4 + 512];
467         args[0] = WIN_SMART;
468   args[1] = 0;
469   args[2] = SMART_READ_THRESHOLDS;
470   args[3] = 1;
471         if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
472                 e = errno;
473                 printf (_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror (errno));
474                 return e;
475         }
476         memcpy (thresholds, args + 4, 512);
477         return 0;
481 void
482 print_help (void)
484         print_revision (progname, revision);
486         printf ("Nagios feature - 1999 Robert Dale <rdale@digital-mission.com>\n");
487         printf ("(C) 1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>\n");
488         printf (COPYRIGHT, copyright, email);
490         printf (_("This plugin checks a local hard drive with the (Linux specific) SMART interface [http://smartlinux.sourceforge.net/smart/index.php]."));
492   printf ("\n\n");
494   print_usage ();
496   printf (_(UT_HELP_VRSN));
497   printf (_(UT_EXTRA_OPTS));
499   printf (" %s\n", "-d, --device=DEVICE");
500   printf ("    %s\n", _("Select device DEVICE"));
501   printf ("    %s\n", _("Note: if the device is selected with this option, _no_ other options are accepted"));
502   printf (" %s\n", "-i, --immediate");
503   printf ("    %s\n", _("Perform immediately offline tests"));
504   printf (" %s\n", "-q, --quiet-check");
505   printf ("    %s\n", _("Returns the number of failed tests"));
506   printf (" %s\n", "-1, --auto-on");
507   printf ("    %s\n", _("Turn on automatic offline tests"));
508   printf (" %s\n", "-0, --auto-off");
509   printf ("    %s\n", _("Turn off automatic offline tests"));
510   printf (" %s\n", "-n, --nagios");
511   printf ("    %s\n", _("Output suitable for Nagios"));
513 #ifdef NP_EXTRA_OPTS
514   printf ("\n");
515   printf ("%s\n", _("Notes:"));
516   printf (_(UT_EXTRA_OPTS_NOTES));
517 #endif
519   printf (_(UT_SUPPORT));
522  /* todo : add to the long nanual as example
523  *
524  *     Run with:  check_ide-smart --nagios [-d] <DRIVE>
525  *     Where DRIVE is an IDE drive, ie. /dev/hda, /dev/hdb, /dev/hdc
526  *
527  *       - Returns 0 on no errors
528  *       - Returns 1 on advisories
529  *       - Returns 2 on prefailure
530  *       - Returns -1 not too often
531  */
534 void
535 print_usage (void)
537   printf (_("Usage:"));
538   printf ("%s [-d <device>] [-i <immediate>] [-q quiet] [-1 <auto-on>]",progname);
539   printf (" [-O <auto-off>] [-n <nagios>]\n");