check_ide_smart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. static struct option longopts[] = {
  145. {"device", required_argument, 0, 'd'},
  146. {"immediate", no_argument, 0, 'i'},
  147. {"quiet-check", no_argument, 0, 'q'},
  148. {"auto-on", no_argument, 0, '1'},
  149. {"auto-off", no_argument, 0, '0'},
  150. {"nagios", no_argument, 0, 'n'},
  151. {"help", no_argument, 0, 'h'},
  152. {"version", no_argument, 0, 'V'}, {0, 0, 0, 0}
  153. };
  154. setlocale (LC_ALL, "");
  155. bindtextdomain (PACKAGE, LOCALEDIR);
  156. textdomain (PACKAGE);
  157. while (1) {
  158. o = getopt_long (argc, argv, "+d:iq10nhV", longopts, &longindex);
  159. if (o == -1 || o == EOF || o == 1)
  160. break;
  161. switch (o) {
  162. case 'd':
  163. device = optarg;
  164. break;
  165. case 'q':
  166. command = 3;
  167. break;
  168. case 'i':
  169. command = 2;
  170. break;
  171. case '1':
  172. command = 1;
  173. break;
  174. case '0':
  175. command = 0;
  176. break;
  177. case 'n':
  178. command = 4;
  179. break;
  180. case 'h':
  181. print_help ();
  182. return STATE_OK;
  183. case 'V':
  184. print_revision (progname, revision);
  185. return STATE_OK;
  186. default:
  187. usage5 ();
  188. }
  189. }
  190. if (optind < argc) {
  191. device = argv[optind];
  192. }
  193. if (!device) {
  194. print_help ();
  195. return STATE_OK;
  196. }
  197. fd = open (device, O_RDONLY);
  198. if (fd < 0) {
  199. printf (_("CRITICAL - Couldn't open device %s: %s\n"), device, strerror (errno));
  200. return STATE_CRITICAL;
  201. }
  202. if (smart_cmd_simple (fd, SMART_CMD_ENABLE, 0, TRUE)) {
  203. printf (_("CRITICAL - SMART_CMD_ENABLE\n"));
  204. return STATE_CRITICAL;
  205. }
  206. switch (command) {
  207. case 0:
  208. retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0, TRUE);
  209. break;
  210. case 1:
  211. retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0xF8, TRUE);
  212. break;
  213. case 2:
  214. retval = smart_cmd_simple (fd, SMART_CMD_IMMEDIATE_OFFLINE, 0, TRUE);
  215. break;
  216. case 3:
  217. smart_read_values (fd, &values);
  218. smart_read_thresholds (fd, &thresholds);
  219. retval = values_not_passed (&values, &thresholds);
  220. break;
  221. case 4:
  222. smart_read_values (fd, &values);
  223. smart_read_thresholds (fd, &thresholds);
  224. retval = nagios (&values, &thresholds);
  225. break;
  226. default:
  227. smart_read_values (fd, &values);
  228. smart_read_thresholds (fd, &thresholds);
  229. print_values (&values, &thresholds);
  230. break;
  231. }
  232. close (fd);
  233. return retval;
  234. }
  235. char *
  236. get_offline_text (int status)
  237. {
  238. int i;
  239. for (i = 0; offline_status_text[i].text; i++) {
  240. if (offline_status_text[i].value == status) {
  241. return offline_status_text[i].text;
  242. }
  243. }
  244. return "UNKNOW";
  245. }
  246. int
  247. smart_read_values (int fd, values_t * values)
  248. {
  249. int e;
  250. __u8 args[4 + 512];
  251. args[0] = WIN_SMART;
  252. args[1] = 0;
  253. args[2] = SMART_READ_VALUES;
  254. args[3] = 1;
  255. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  256. e = errno;
  257. printf (_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror (errno));
  258. return e;
  259. }
  260. memcpy (values, args + 4, 512);
  261. return 0;
  262. }
  263. int
  264. values_not_passed (values_t * p, thresholds_t * t)
  265. {
  266. value_t * value = p->values;
  267. threshold_t * threshold = t->thresholds;
  268. int failed = 0;
  269. int passed = 0;
  270. int i;
  271. for (i = 0; i < NR_ATTRIBUTES; i++) {
  272. if (value->id && threshold->id && value->id == threshold->id) {
  273. if (value->value <= threshold->threshold) {
  274. ++failed;
  275. }
  276. else {
  277. ++passed;
  278. }
  279. }
  280. ++value;
  281. ++threshold;
  282. }
  283. return (passed ? -failed : 2);
  284. }
  285. int
  286. nagios (values_t * p, thresholds_t * t)
  287. {
  288. value_t * value = p->values;
  289. threshold_t * threshold = t->thresholds;
  290. int status = OPERATIONAL;
  291. int prefailure = 0;
  292. int advisory = 0;
  293. int failed = 0;
  294. int passed = 0;
  295. int total = 0;
  296. int i;
  297. for (i = 0; i < NR_ATTRIBUTES; i++) {
  298. if (value->id && threshold->id && value->id == threshold->id) {
  299. if (value->value <= threshold->threshold) {
  300. ++failed;
  301. if (value->status & 1) {
  302. status = PREFAILURE;
  303. ++prefailure;
  304. }
  305. else {
  306. status = ADVISORY;
  307. ++advisory;
  308. }
  309. }
  310. else {
  311. ++passed;
  312. }
  313. ++total;
  314. }
  315. ++value;
  316. ++threshold;
  317. }
  318. switch (status) {
  319. case PREFAILURE:
  320. printf (_("CRITICAL - %d Harddrive PreFailure%cDetected! %d/%d tests failed.\n"),
  321. prefailure,
  322. prefailure > 1 ? 's' : ' ',
  323. failed,
  324. total);
  325. status=STATE_CRITICAL;
  326. break;
  327. case ADVISORY:
  328. printf (_("WARNING - %d Harddrive Advisor%s Detected. %d/%d tests failed.\n"),
  329. advisory,
  330. advisory > 1 ? "ies" : "y",
  331. failed,
  332. total);
  333. status=STATE_WARNING;
  334. break;
  335. case OPERATIONAL:
  336. printf (_("OK - Operational (%d/%d tests passed)\n"), passed, total);
  337. status=STATE_OK;
  338. break;
  339. default:
  340. printf (_("ERROR - Status '%d' unkown. %d/%d tests passed\n"), status,
  341. passed, total);
  342. status = STATE_UNKNOWN;
  343. break;
  344. }
  345. return status;
  346. }
  347. void
  348. print_value (value_t * p, threshold_t * t)
  349. {
  350. printf ("Id=%3d, Status=%2d {%s , %s}, Value=%3d, Threshold=%3d, %s\n",
  351. p->id, p->status, p->status & 1 ? "PreFailure" : "Advisory ",
  352. p->status & 2 ? "OnLine " : "OffLine", p->value, t->threshold,
  353. p->value > t->threshold ? "Passed" : "Failed");
  354. }
  355. void
  356. print_values (values_t * p, thresholds_t * t)
  357. {
  358. value_t * value = p->values;
  359. threshold_t * threshold = t->thresholds;
  360. int i;
  361. for (i = 0; i < NR_ATTRIBUTES; i++) {
  362. if (value->id && threshold->id && value->id == threshold->id) {
  363. print_value (value++, threshold++);
  364. }
  365. }
  366. printf
  367. (_("OffLineStatus=%d {%s}, AutoOffLine=%s, OffLineTimeout=%d minutes\n"),
  368. p->offline_status,
  369. get_offline_text (p->offline_status & 0x7f),
  370. (p->offline_status & 0x80 ? "Yes" : "No"),
  371. p->offline_timeout / 60);
  372. printf
  373. (_("OffLineCapability=%d {%s %s %s}\n"),
  374. p->offline_capability,
  375. p->offline_capability & 1 ? "Immediate" : "",
  376. p->offline_capability & 2 ? "Auto" : "",
  377. p->offline_capability & 4 ? "AbortOnCmd" : "SuspendOnCmd");
  378. printf
  379. (_("SmartRevision=%d, CheckSum=%d, SmartCapability=%d {%s %s}\n"),
  380. p->revision,
  381. p->checksum,
  382. p->smart_capability,
  383. p->smart_capability & 1 ? "SaveOnStandBy" : "",
  384. p->smart_capability & 2 ? "AutoSave" : "");
  385. }
  386. int
  387. smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0, char show_error)
  388. {
  389. int e = 0;
  390. __u8 args[4];
  391. args[0] = WIN_SMART;
  392. args[1] = val0;
  393. args[2] = smart_command[command].value;
  394. args[3] = 0;
  395. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  396. e = errno;
  397. if (show_error) {
  398. printf (_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror (errno));
  399. }
  400. }
  401. return e;
  402. }
  403. int
  404. smart_read_thresholds (int fd, thresholds_t * thresholds)
  405. {
  406. int e;
  407. __u8 args[4 + 512];
  408. args[0] = WIN_SMART;
  409. args[1] = 0;
  410. args[2] = SMART_READ_THRESHOLDS;
  411. args[3] = 1;
  412. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  413. e = errno;
  414. printf (_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror (errno));
  415. return e;
  416. }
  417. memcpy (thresholds, args + 4, 512);
  418. return 0;
  419. }
  420. void
  421. print_help (void)
  422. {
  423. print_revision (progname, revision);
  424. printf ("Nagios feature - 1999 Robert Dale <rdale@digital-mission.com>\n");
  425. printf ("(C) 1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>\n");
  426. printf (COPYRIGHT, copyright, email);
  427. printf (_("This plugin checks a local hard drive with the (Linux specific) SMART interface [http://smartlinux.sourceforge.net/smart/index.php]."));
  428. printf ("\n\n");
  429. print_usage ();
  430. printf (_(UT_HELP_VRSN));
  431. printf (" %s\n", "-d, --device=DEVICE");
  432. printf (" %s\n", _("Select device DEVICE"));
  433. printf (" %s\n", _("Note: if the device is selected with this option, _no_ other options are accepted"));
  434. printf (" %s\n", "-i, --immediate");
  435. printf (" %s\n", _("Perform immediately offline tests"));
  436. printf (" %s\n", "-q, --quiet-check");
  437. printf (" %s\n", _("Returns the number of failed tests"));
  438. printf (" %s\n", "-1, --auto-on");
  439. printf (" %s\n", _("Turn on automatic offline tests"));
  440. printf (" %s\n", "-0, --auto-off");
  441. printf (" %s\n", _("Turn off automatic offline tests"));
  442. printf (" %s\n", "-n, --nagios");
  443. printf (" %s\n\n", _("Output suitable for Nagios"));
  444. printf (_(UT_SUPPORT));
  445. }
  446. /* todo : add to the long nanual as example
  447. *
  448. * Run with: check_ide-smart --nagios [-d] <DRIVE>
  449. * Where DRIVE is an IDE drive, ie. /dev/hda, /dev/hdb, /dev/hdc
  450. *
  451. * - Returns 0 on no errors
  452. * - Returns 1 on advisories
  453. * - Returns 2 on prefailure
  454. * - Returns -1 not too often
  455. */
  456. void
  457. print_usage (void)
  458. {
  459. printf (_("Usage:"));
  460. printf ("%s [-d <device>] [-i <immediate>] [-q quiet] [-1 <auto-on>]",progname);
  461. printf (" [-O <auto-off>] [-n <nagios>]\n");
  462. }