check_ide_smart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. *****************************************************************************/
  38. const char *progname = "check_ide_smart";
  39. const char *revision = "$Revision$";
  40. const char *copyright = "1998-2007";
  41. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  42. #include "common.h"
  43. #include "utils.h"
  44. void print_help (void);
  45. void print_usage (void);
  46. #include <sys/stat.h>
  47. #include <sys/ioctl.h>
  48. #include <fcntl.h>
  49. #include <linux/hdreg.h>
  50. #include <linux/types.h>
  51. #include <errno.h>
  52. #define NR_ATTRIBUTES 30
  53. #ifndef TRUE
  54. #define TRUE 1
  55. #endif /* */
  56. #define PREFAILURE 2
  57. #define ADVISORY 1
  58. #define OPERATIONAL 0
  59. #define UNKNOWN -1
  60. typedef struct threshold_s
  61. {
  62. __u8 id;
  63. __u8 threshold;
  64. __u8 reserved[10];
  65. }
  66. __attribute__ ((packed)) threshold_t;
  67. typedef struct thresholds_s
  68. {
  69. __u16 revision;
  70. threshold_t thresholds[NR_ATTRIBUTES];
  71. __u8 reserved[18];
  72. __u8 vendor[131];
  73. __u8 checksum;
  74. }
  75. __attribute__ ((packed)) thresholds_t;
  76. typedef struct value_s
  77. {
  78. __u8 id;
  79. __u16 status;
  80. __u8 value;
  81. __u8 vendor[8];
  82. }
  83. __attribute__ ((packed)) value_t;
  84. typedef struct values_s
  85. {
  86. __u16 revision;
  87. value_t values[NR_ATTRIBUTES];
  88. __u8 offline_status;
  89. __u8 vendor1;
  90. __u16 offline_timeout;
  91. __u8 vendor2;
  92. __u8 offline_capability;
  93. __u16 smart_capability;
  94. __u8 reserved[16];
  95. __u8 vendor[125];
  96. __u8 checksum;
  97. }
  98. __attribute__ ((packed)) values_t;
  99. struct
  100. {
  101. __u8 value;
  102. char *text;
  103. }
  104. offline_status_text[] =
  105. {
  106. {0x00, "NeverStarted"},
  107. {0x02, "Completed"},
  108. {0x04, "Suspended"},
  109. {0x05, "Aborted"},
  110. {0x06, "Failed"},
  111. {0, 0}
  112. };
  113. struct
  114. {
  115. __u8 value;
  116. char *text;
  117. }
  118. smart_command[] =
  119. {
  120. {SMART_ENABLE, "SMART_ENABLE"},
  121. {SMART_DISABLE, "SMART_DISABLE"},
  122. {SMART_IMMEDIATE_OFFLINE, "SMART_IMMEDIATE_OFFLINE"},
  123. {SMART_AUTO_OFFLINE, "SMART_AUTO_OFFLINE"}
  124. };
  125. /* Index to smart_command table, keep in order */
  126. enum SmartCommand
  127. { SMART_CMD_ENABLE,
  128. SMART_CMD_DISABLE,
  129. SMART_CMD_IMMEDIATE_OFFLINE,
  130. SMART_CMD_AUTO_OFFLINE
  131. };
  132. void print_values (values_t * p, thresholds_t * t);
  133. int smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0, char show_error);
  134. int
  135. main (int argc, char *argv[])
  136. {
  137. char *device = NULL;
  138. int command = -1;
  139. int o, longindex;
  140. int retval = 0;
  141. thresholds_t thresholds;
  142. values_t values;
  143. int fd;
  144. /* Parse extra opts if any */
  145. argv=np_extra_opts (&argc, argv, progname);
  146. static struct option longopts[] = {
  147. {"device", required_argument, 0, 'd'},
  148. {"immediate", no_argument, 0, 'i'},
  149. {"quiet-check", no_argument, 0, 'q'},
  150. {"auto-on", no_argument, 0, '1'},
  151. {"auto-off", no_argument, 0, '0'},
  152. {"nagios", no_argument, 0, 'n'},
  153. {"help", no_argument, 0, 'h'},
  154. {"version", no_argument, 0, 'V'},
  155. {0, 0, 0, 0}
  156. };
  157. setlocale (LC_ALL, "");
  158. bindtextdomain (PACKAGE, LOCALEDIR);
  159. textdomain (PACKAGE);
  160. while (1) {
  161. o = getopt_long (argc, argv, "+d:iq10nhV", longopts, &longindex);
  162. if (o == -1 || o == EOF || o == 1)
  163. break;
  164. switch (o) {
  165. case 'd':
  166. device = optarg;
  167. break;
  168. case 'q':
  169. command = 3;
  170. break;
  171. case 'i':
  172. command = 2;
  173. break;
  174. case '1':
  175. command = 1;
  176. break;
  177. case '0':
  178. command = 0;
  179. break;
  180. case 'n':
  181. command = 4;
  182. break;
  183. case 'h':
  184. print_help ();
  185. return STATE_OK;
  186. case 'V':
  187. print_revision (progname, revision);
  188. return STATE_OK;
  189. default:
  190. usage5 ();
  191. }
  192. }
  193. if (optind < argc) {
  194. device = argv[optind];
  195. }
  196. if (!device) {
  197. print_help ();
  198. return STATE_OK;
  199. }
  200. fd = open (device, O_RDONLY);
  201. if (fd < 0) {
  202. printf (_("CRITICAL - Couldn't open device %s: %s\n"), device, strerror (errno));
  203. return STATE_CRITICAL;
  204. }
  205. if (smart_cmd_simple (fd, SMART_CMD_ENABLE, 0, TRUE)) {
  206. printf (_("CRITICAL - SMART_CMD_ENABLE\n"));
  207. return STATE_CRITICAL;
  208. }
  209. switch (command) {
  210. case 0:
  211. retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0, TRUE);
  212. break;
  213. case 1:
  214. retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0xF8, TRUE);
  215. break;
  216. case 2:
  217. retval = smart_cmd_simple (fd, SMART_CMD_IMMEDIATE_OFFLINE, 0, TRUE);
  218. break;
  219. case 3:
  220. smart_read_values (fd, &values);
  221. smart_read_thresholds (fd, &thresholds);
  222. retval = values_not_passed (&values, &thresholds);
  223. break;
  224. case 4:
  225. smart_read_values (fd, &values);
  226. smart_read_thresholds (fd, &thresholds);
  227. retval = nagios (&values, &thresholds);
  228. break;
  229. default:
  230. smart_read_values (fd, &values);
  231. smart_read_thresholds (fd, &thresholds);
  232. print_values (&values, &thresholds);
  233. break;
  234. }
  235. close (fd);
  236. return retval;
  237. }
  238. char *
  239. get_offline_text (int status)
  240. {
  241. int i;
  242. for (i = 0; offline_status_text[i].text; i++) {
  243. if (offline_status_text[i].value == status) {
  244. return offline_status_text[i].text;
  245. }
  246. }
  247. return "UNKNOW";
  248. }
  249. int
  250. smart_read_values (int fd, values_t * values)
  251. {
  252. int e;
  253. __u8 args[4 + 512];
  254. args[0] = WIN_SMART;
  255. args[1] = 0;
  256. args[2] = SMART_READ_VALUES;
  257. args[3] = 1;
  258. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  259. e = errno;
  260. printf (_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror (errno));
  261. return e;
  262. }
  263. memcpy (values, args + 4, 512);
  264. return 0;
  265. }
  266. int
  267. values_not_passed (values_t * p, thresholds_t * t)
  268. {
  269. value_t * value = p->values;
  270. threshold_t * threshold = t->thresholds;
  271. int failed = 0;
  272. int passed = 0;
  273. int i;
  274. for (i = 0; i < NR_ATTRIBUTES; i++) {
  275. if (value->id && threshold->id && value->id == threshold->id) {
  276. if (value->value <= threshold->threshold) {
  277. ++failed;
  278. }
  279. else {
  280. ++passed;
  281. }
  282. }
  283. ++value;
  284. ++threshold;
  285. }
  286. return (passed ? -failed : 2);
  287. }
  288. int
  289. nagios (values_t * p, thresholds_t * t)
  290. {
  291. value_t * value = p->values;
  292. threshold_t * threshold = t->thresholds;
  293. int status = OPERATIONAL;
  294. int prefailure = 0;
  295. int advisory = 0;
  296. int failed = 0;
  297. int passed = 0;
  298. int total = 0;
  299. int i;
  300. for (i = 0; i < NR_ATTRIBUTES; i++) {
  301. if (value->id && threshold->id && value->id == threshold->id) {
  302. if (value->value <= threshold->threshold) {
  303. ++failed;
  304. if (value->status & 1) {
  305. status = PREFAILURE;
  306. ++prefailure;
  307. }
  308. else {
  309. status = ADVISORY;
  310. ++advisory;
  311. }
  312. }
  313. else {
  314. ++passed;
  315. }
  316. ++total;
  317. }
  318. ++value;
  319. ++threshold;
  320. }
  321. switch (status) {
  322. case PREFAILURE:
  323. printf (_("CRITICAL - %d Harddrive PreFailure%cDetected! %d/%d tests failed.\n"),
  324. prefailure,
  325. prefailure > 1 ? 's' : ' ',
  326. failed,
  327. total);
  328. status=STATE_CRITICAL;
  329. break;
  330. case ADVISORY:
  331. printf (_("WARNING - %d Harddrive Advisor%s Detected. %d/%d tests failed.\n"),
  332. advisory,
  333. advisory > 1 ? "ies" : "y",
  334. failed,
  335. total);
  336. status=STATE_WARNING;
  337. break;
  338. case OPERATIONAL:
  339. printf (_("OK - Operational (%d/%d tests passed)\n"), passed, total);
  340. status=STATE_OK;
  341. break;
  342. default:
  343. printf (_("ERROR - Status '%d' unkown. %d/%d tests passed\n"), status,
  344. passed, total);
  345. status = STATE_UNKNOWN;
  346. break;
  347. }
  348. return status;
  349. }
  350. void
  351. print_value (value_t * p, threshold_t * t)
  352. {
  353. printf ("Id=%3d, Status=%2d {%s , %s}, Value=%3d, Threshold=%3d, %s\n",
  354. p->id, p->status, p->status & 1 ? "PreFailure" : "Advisory ",
  355. p->status & 2 ? "OnLine " : "OffLine", p->value, t->threshold,
  356. p->value > t->threshold ? "Passed" : "Failed");
  357. }
  358. void
  359. print_values (values_t * p, thresholds_t * t)
  360. {
  361. value_t * value = p->values;
  362. threshold_t * threshold = t->thresholds;
  363. int i;
  364. for (i = 0; i < NR_ATTRIBUTES; i++) {
  365. if (value->id && threshold->id && value->id == threshold->id) {
  366. print_value (value++, threshold++);
  367. }
  368. }
  369. printf
  370. (_("OffLineStatus=%d {%s}, AutoOffLine=%s, OffLineTimeout=%d minutes\n"),
  371. p->offline_status,
  372. get_offline_text (p->offline_status & 0x7f),
  373. (p->offline_status & 0x80 ? "Yes" : "No"),
  374. p->offline_timeout / 60);
  375. printf
  376. (_("OffLineCapability=%d {%s %s %s}\n"),
  377. p->offline_capability,
  378. p->offline_capability & 1 ? "Immediate" : "",
  379. p->offline_capability & 2 ? "Auto" : "",
  380. p->offline_capability & 4 ? "AbortOnCmd" : "SuspendOnCmd");
  381. printf
  382. (_("SmartRevision=%d, CheckSum=%d, SmartCapability=%d {%s %s}\n"),
  383. p->revision,
  384. p->checksum,
  385. p->smart_capability,
  386. p->smart_capability & 1 ? "SaveOnStandBy" : "",
  387. p->smart_capability & 2 ? "AutoSave" : "");
  388. }
  389. int
  390. smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0, char show_error)
  391. {
  392. int e = 0;
  393. __u8 args[4];
  394. args[0] = WIN_SMART;
  395. args[1] = val0;
  396. args[2] = smart_command[command].value;
  397. args[3] = 0;
  398. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  399. e = errno;
  400. if (show_error) {
  401. printf (_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror (errno));
  402. }
  403. }
  404. return e;
  405. }
  406. int
  407. smart_read_thresholds (int fd, thresholds_t * thresholds)
  408. {
  409. int e;
  410. __u8 args[4 + 512];
  411. args[0] = WIN_SMART;
  412. args[1] = 0;
  413. args[2] = SMART_READ_THRESHOLDS;
  414. args[3] = 1;
  415. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  416. e = errno;
  417. printf (_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror (errno));
  418. return e;
  419. }
  420. memcpy (thresholds, args + 4, 512);
  421. return 0;
  422. }
  423. void
  424. print_help (void)
  425. {
  426. print_revision (progname, revision);
  427. printf ("Nagios feature - 1999 Robert Dale <rdale@digital-mission.com>\n");
  428. printf ("(C) 1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>\n");
  429. printf (COPYRIGHT, copyright, email);
  430. printf (_("This plugin checks a local hard drive with the (Linux specific) SMART interface [http://smartlinux.sourceforge.net/smart/index.php]."));
  431. printf ("\n\n");
  432. print_usage ();
  433. printf (_(UT_HELP_VRSN));
  434. printf (_(UT_EXTRA_OPTS));
  435. printf (" %s\n", "-d, --device=DEVICE");
  436. printf (" %s\n", _("Select device DEVICE"));
  437. printf (" %s\n", _("Note: if the device is selected with this option, _no_ other options are accepted"));
  438. printf (" %s\n", "-i, --immediate");
  439. printf (" %s\n", _("Perform immediately offline tests"));
  440. printf (" %s\n", "-q, --quiet-check");
  441. printf (" %s\n", _("Returns the number of failed tests"));
  442. printf (" %s\n", "-1, --auto-on");
  443. printf (" %s\n", _("Turn on automatic offline tests"));
  444. printf (" %s\n", "-0, --auto-off");
  445. printf (" %s\n", _("Turn off automatic offline tests"));
  446. printf (" %s\n", "-n, --nagios");
  447. printf (" %s\n", _("Output suitable for Nagios"));
  448. #ifdef NP_EXTRA_OPTS
  449. printf ("\n");
  450. printf ("%s\n", _("Notes:"));
  451. printf (_(UT_EXTRA_OPTS_NOTES));
  452. #endif
  453. printf (_(UT_SUPPORT));
  454. }
  455. /* todo : add to the long nanual as example
  456. *
  457. * Run with: check_ide-smart --nagios [-d] <DRIVE>
  458. * Where DRIVE is an IDE drive, ie. /dev/hda, /dev/hdb, /dev/hdc
  459. *
  460. * - Returns 0 on no errors
  461. * - Returns 1 on advisories
  462. * - Returns 2 on prefailure
  463. * - Returns -1 not too often
  464. */
  465. void
  466. print_usage (void)
  467. {
  468. printf (_("Usage:"));
  469. printf ("%s [-d <device>] [-i <immediate>] [-q quiet] [-1 <auto-on>]",progname);
  470. printf (" [-O <auto-off>] [-n <nagios>]\n");
  471. }