check_ide_smart.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*****************************************************************************
  2. *
  3. * Monitoring 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 Monitoring Plugins Development Team
  11. *
  12. * Description:
  13. *
  14. * This file contains the check_ide_smart plugin
  15. *
  16. * This plugin checks a local hard drive with the (Linux specific) SMART
  17. * interface
  18. *
  19. *
  20. * This program is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation, either version 3 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. *
  33. *
  34. *****************************************************************************/
  35. const char *progname = "check_ide_smart";
  36. const char *copyright = "1998-2007";
  37. const char *email = "devel@monitoring-plugins.org";
  38. #include "common.h"
  39. #include "utils.h"
  40. void print_help (void);
  41. void print_usage (void);
  42. #include <sys/stat.h>
  43. #include <sys/ioctl.h>
  44. #include <fcntl.h>
  45. #ifdef __linux__
  46. #include <linux/hdreg.h>
  47. #include <linux/types.h>
  48. #define OPEN_MODE O_RDONLY
  49. #endif /* __linux__ */
  50. #ifdef __NetBSD__
  51. #include <sys/device.h>
  52. #include <sys/param.h>
  53. #include <sys/sysctl.h>
  54. #include <sys/videoio.h> /* for __u8 and friends */
  55. #include <sys/scsiio.h>
  56. #include <sys/ataio.h>
  57. #include <dev/ata/atareg.h>
  58. #include <dev/ic/wdcreg.h>
  59. #define SMART_ENABLE WDSM_ENABLE_OPS
  60. #define SMART_DISABLE WDSM_DISABLE_OPS
  61. #define SMART_IMMEDIATE_OFFLINE WDSM_EXEC_OFFL_IMM
  62. #define SMART_AUTO_OFFLINE 0xdb /* undefined in NetBSD headers */
  63. #define OPEN_MODE O_RDWR
  64. #endif /* __NetBSD__ */
  65. #include <errno.h>
  66. #define NR_ATTRIBUTES 30
  67. #ifndef TRUE
  68. #define TRUE 1
  69. #endif /* */
  70. #define PREFAILURE 2
  71. #define ADVISORY 1
  72. #define OPERATIONAL 0
  73. #define UNKNOWN -1
  74. typedef struct threshold_s
  75. {
  76. __u8 id;
  77. __u8 threshold;
  78. __u8 reserved[10];
  79. }
  80. __attribute__ ((packed)) threshold_t;
  81. typedef struct thresholds_s
  82. {
  83. __u16 revision;
  84. threshold_t thresholds[NR_ATTRIBUTES];
  85. __u8 reserved[18];
  86. __u8 vendor[131];
  87. __u8 checksum;
  88. }
  89. __attribute__ ((packed)) thresholds_t;
  90. typedef struct value_s
  91. {
  92. __u8 id;
  93. __u16 status;
  94. __u8 value;
  95. __u8 vendor[8];
  96. }
  97. __attribute__ ((packed)) value_t;
  98. typedef struct values_s
  99. {
  100. __u16 revision;
  101. value_t values[NR_ATTRIBUTES];
  102. __u8 offline_status;
  103. __u8 vendor1;
  104. __u16 offline_timeout;
  105. __u8 vendor2;
  106. __u8 offline_capability;
  107. __u16 smart_capability;
  108. __u8 reserved[16];
  109. __u8 vendor[125];
  110. __u8 checksum;
  111. }
  112. __attribute__ ((packed)) values_t;
  113. struct
  114. {
  115. __u8 value;
  116. char *text;
  117. }
  118. offline_status_text[] =
  119. {
  120. {0x00, "NeverStarted"},
  121. {0x02, "Completed"},
  122. {0x04, "Suspended"},
  123. {0x05, "Aborted"},
  124. {0x06, "Failed"},
  125. {0, 0}
  126. };
  127. struct
  128. {
  129. __u8 value;
  130. char *text;
  131. }
  132. smart_command[] =
  133. {
  134. {SMART_ENABLE, "SMART_ENABLE"},
  135. {SMART_DISABLE, "SMART_DISABLE"},
  136. {SMART_IMMEDIATE_OFFLINE, "SMART_IMMEDIATE_OFFLINE"},
  137. {SMART_AUTO_OFFLINE, "SMART_AUTO_OFFLINE"}
  138. };
  139. /* Index to smart_command table, keep in order */
  140. enum SmartCommand
  141. { SMART_CMD_ENABLE,
  142. SMART_CMD_DISABLE,
  143. SMART_CMD_IMMEDIATE_OFFLINE,
  144. SMART_CMD_AUTO_OFFLINE
  145. };
  146. char *get_offline_text (int);
  147. int smart_read_values (int, values_t *);
  148. int nagios (values_t *, thresholds_t *);
  149. void print_value (value_t *, threshold_t *);
  150. void print_values (values_t *, thresholds_t *);
  151. int smart_cmd_simple (int, enum SmartCommand, __u8, char);
  152. int smart_read_thresholds (int, thresholds_t *);
  153. int verbose = FALSE;
  154. int
  155. main (int argc, char *argv[])
  156. {
  157. char *device = NULL;
  158. int o, longindex;
  159. int retval = 0;
  160. thresholds_t thresholds;
  161. values_t values;
  162. int fd;
  163. static struct option longopts[] = {
  164. {"device", required_argument, 0, 'd'},
  165. {"immediate", no_argument, 0, 'i'},
  166. {"quiet-check", no_argument, 0, 'q'},
  167. {"auto-on", no_argument, 0, '1'},
  168. {"auto-off", no_argument, 0, '0'},
  169. {"nagios", no_argument, 0, 'n'}, /* DEPRECATED, but we still accept it */
  170. {"help", no_argument, 0, 'h'},
  171. {"version", no_argument, 0, 'V'},
  172. {0, 0, 0, 0}
  173. };
  174. /* Parse extra opts if any */
  175. argv=np_extra_opts (&argc, argv, progname);
  176. setlocale (LC_ALL, "");
  177. bindtextdomain (PACKAGE, LOCALEDIR);
  178. textdomain (PACKAGE);
  179. while (1) {
  180. o = getopt_long (argc, argv, "+d:iq10nhVv", longopts, &longindex);
  181. if (o == -1 || o == EOF || o == 1)
  182. break;
  183. switch (o) {
  184. case 'd':
  185. device = optarg;
  186. break;
  187. case 'q':
  188. fprintf (stderr, "%s\n", _("DEPRECATION WARNING: the -q switch (quiet output) is no longer \"quiet\"."));
  189. fprintf (stderr, "%s\n", _("Nagios-compatible output is now always returned."));
  190. break;
  191. case 'i':
  192. case '1':
  193. case '0':
  194. printf ("%s\n", _("SMART commands are broken and have been disabled (See Notes in --help)."));
  195. return STATE_CRITICAL;
  196. break;
  197. case 'n':
  198. fprintf (stderr, "%s\n", _("DEPRECATION WARNING: the -n switch (Nagios-compatible output) is now the"));
  199. fprintf (stderr, "%s\n", _("default and will be removed from future releases."));
  200. break;
  201. case 'v': /* verbose */
  202. verbose = TRUE;
  203. break;
  204. case 'h':
  205. print_help ();
  206. return STATE_UNKNOWN;
  207. case 'V':
  208. print_revision (progname, NP_VERSION);
  209. return STATE_UNKNOWN;
  210. default:
  211. usage5 ();
  212. }
  213. }
  214. if (optind < argc) {
  215. device = argv[optind];
  216. }
  217. if (!device) {
  218. print_help ();
  219. return STATE_UNKNOWN;
  220. }
  221. fd = open (device, OPEN_MODE);
  222. if (fd < 0) {
  223. printf (_("CRITICAL - Couldn't open device %s: %s\n"), device, strerror (errno));
  224. return STATE_CRITICAL;
  225. }
  226. if (smart_cmd_simple (fd, SMART_CMD_ENABLE, 0, FALSE)) {
  227. printf (_("CRITICAL - SMART_CMD_ENABLE\n"));
  228. return STATE_CRITICAL;
  229. }
  230. smart_read_values (fd, &values);
  231. smart_read_thresholds (fd, &thresholds);
  232. retval = nagios (&values, &thresholds);
  233. if (verbose) print_values (&values, &thresholds);
  234. close (fd);
  235. return retval;
  236. }
  237. char *
  238. get_offline_text (int status)
  239. {
  240. int i;
  241. for (i = 0; offline_status_text[i].text; i++) {
  242. if (offline_status_text[i].value == status) {
  243. return offline_status_text[i].text;
  244. }
  245. }
  246. return "UNKNOW";
  247. }
  248. int
  249. smart_read_values (int fd, values_t * values)
  250. {
  251. #ifdef __linux__
  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. #endif /* __linux__ */
  265. #ifdef __NetBSD__
  266. struct atareq req;
  267. unsigned char inbuf[DEV_BSIZE];
  268. memset(&req, 0, sizeof(req));
  269. req.timeout = 1000;
  270. memset(&inbuf, 0, sizeof(inbuf));
  271. req.flags = ATACMD_READ;
  272. req.features = WDSM_RD_DATA;
  273. req.command = WDCC_SMART;
  274. req.databuf = (char *)inbuf;
  275. req.datalen = sizeof(inbuf);
  276. req.cylinder = WDSMART_CYL;
  277. if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) {
  278. if (req.retsts != ATACMD_OK)
  279. errno = ENODEV;
  280. }
  281. if (errno != 0) {
  282. int e = errno;
  283. printf (_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror (errno));
  284. return e;
  285. }
  286. (void)memcpy(values, inbuf, 512);
  287. #endif /* __NetBSD__ */
  288. return 0;
  289. }
  290. int
  291. nagios (values_t * p, thresholds_t * t)
  292. {
  293. value_t * value = p->values;
  294. threshold_t * threshold = t->thresholds;
  295. int status = OPERATIONAL;
  296. int prefailure = 0;
  297. int advisory = 0;
  298. int failed = 0;
  299. int passed = 0;
  300. int total = 0;
  301. int i;
  302. for (i = 0; i < NR_ATTRIBUTES; i++) {
  303. if (value->id && threshold->id && value->id == threshold->id) {
  304. if (value->value < threshold->threshold) {
  305. ++failed;
  306. if (value->status & 1) {
  307. status = PREFAILURE;
  308. ++prefailure;
  309. }
  310. else {
  311. status = ADVISORY;
  312. ++advisory;
  313. }
  314. }
  315. else {
  316. ++passed;
  317. }
  318. ++total;
  319. }
  320. ++value;
  321. ++threshold;
  322. }
  323. switch (status) {
  324. case PREFAILURE:
  325. printf (_("CRITICAL - %d Harddrive PreFailure%cDetected! %d/%d tests failed.\n"),
  326. prefailure,
  327. prefailure > 1 ? 's' : ' ',
  328. failed,
  329. total);
  330. status=STATE_CRITICAL;
  331. break;
  332. case ADVISORY:
  333. printf (_("WARNING - %d Harddrive Advisor%s Detected. %d/%d tests failed.\n"),
  334. advisory,
  335. advisory > 1 ? "ies" : "y",
  336. failed,
  337. total);
  338. status=STATE_WARNING;
  339. break;
  340. case OPERATIONAL:
  341. printf (_("OK - Operational (%d/%d tests passed)\n"), passed, total);
  342. status=STATE_OK;
  343. break;
  344. default:
  345. printf (_("ERROR - Status '%d' unknown. %d/%d tests passed\n"), status,
  346. passed, total);
  347. status = STATE_UNKNOWN;
  348. break;
  349. }
  350. return status;
  351. }
  352. void
  353. print_value (value_t * p, threshold_t * t)
  354. {
  355. printf ("Id=%3d, Status=%2d {%s , %s}, Value=%3d, Threshold=%3d, %s\n",
  356. p->id, p->status, p->status & 1 ? "PreFailure" : "Advisory ",
  357. p->status & 2 ? "OnLine " : "OffLine", p->value, t->threshold,
  358. p->value >= t->threshold ? "Passed" : "Failed");
  359. }
  360. void
  361. print_values (values_t * p, thresholds_t * t)
  362. {
  363. value_t * value = p->values;
  364. threshold_t * threshold = t->thresholds;
  365. int i;
  366. for (i = 0; i < NR_ATTRIBUTES; i++) {
  367. if (value->id && threshold->id && value->id == threshold->id) {
  368. print_value (value++, threshold++);
  369. }
  370. }
  371. printf
  372. (_("OffLineStatus=%d {%s}, AutoOffLine=%s, OffLineTimeout=%d minutes\n"),
  373. p->offline_status,
  374. get_offline_text (p->offline_status & 0x7f),
  375. (p->offline_status & 0x80 ? "Yes" : "No"),
  376. p->offline_timeout / 60);
  377. printf
  378. (_("OffLineCapability=%d {%s %s %s}\n"),
  379. p->offline_capability,
  380. p->offline_capability & 1 ? "Immediate" : "",
  381. p->offline_capability & 2 ? "Auto" : "",
  382. p->offline_capability & 4 ? "AbortOnCmd" : "SuspendOnCmd");
  383. printf
  384. (_("SmartRevision=%d, CheckSum=%d, SmartCapability=%d {%s %s}\n"),
  385. p->revision,
  386. p->checksum,
  387. p->smart_capability,
  388. p->smart_capability & 1 ? "SaveOnStandBy" : "",
  389. p->smart_capability & 2 ? "AutoSave" : "");
  390. }
  391. int
  392. smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0, char show_error)
  393. {
  394. int e = STATE_UNKNOWN;
  395. #ifdef __linux__
  396. __u8 args[4];
  397. args[0] = WIN_SMART;
  398. args[1] = val0;
  399. args[2] = smart_command[command].value;
  400. args[3] = 0;
  401. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  402. e = STATE_CRITICAL;
  403. if (show_error)
  404. printf (_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror (errno));
  405. } else {
  406. e = STATE_OK;
  407. if (show_error)
  408. printf (_("OK - Command sent (%s)\n"), smart_command[command].text);
  409. }
  410. #endif /* __linux__ */
  411. #ifdef __NetBSD__
  412. struct atareq req;
  413. memset(&req, 0, sizeof(req));
  414. req.timeout = 1000;
  415. req.flags = ATACMD_READREG;
  416. req.features = smart_command[command].value;
  417. req.command = WDCC_SMART;
  418. req.cylinder = WDSMART_CYL;
  419. req.sec_count = val0;
  420. if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) {
  421. if (req.retsts != ATACMD_OK)
  422. errno = ENODEV;
  423. if (req.cylinder != WDSMART_CYL)
  424. errno = ENODEV;
  425. }
  426. if (errno != 0) {
  427. e = STATE_CRITICAL;
  428. if (show_error)
  429. printf (_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror (errno));
  430. } else {
  431. e = STATE_OK;
  432. if (show_error)
  433. printf (_("OK - Command sent (%s)\n"), smart_command[command].text);
  434. }
  435. #endif /* __NetBSD__ */
  436. return e;
  437. }
  438. int
  439. smart_read_thresholds (int fd, thresholds_t * thresholds)
  440. {
  441. #ifdef __linux__
  442. int e;
  443. __u8 args[4 + 512];
  444. args[0] = WIN_SMART;
  445. args[1] = 0;
  446. args[2] = SMART_READ_THRESHOLDS;
  447. args[3] = 1;
  448. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  449. e = errno;
  450. printf (_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror (errno));
  451. return e;
  452. }
  453. memcpy (thresholds, args + 4, 512);
  454. #endif /* __linux__ */
  455. #ifdef __NetBSD__
  456. struct atareq req;
  457. unsigned char inbuf[DEV_BSIZE];
  458. memset(&req, 0, sizeof(req));
  459. req.timeout = 1000;
  460. memset(&inbuf, 0, sizeof(inbuf));
  461. req.flags = ATACMD_READ;
  462. req.features = WDSM_RD_THRESHOLDS;
  463. req.command = WDCC_SMART;
  464. req.databuf = (char *)inbuf;
  465. req.datalen = sizeof(inbuf);
  466. req.cylinder = WDSMART_CYL;
  467. if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) {
  468. if (req.retsts != ATACMD_OK)
  469. errno = ENODEV;
  470. }
  471. if (errno != 0) {
  472. int e = errno;
  473. printf (_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror (errno));
  474. return e;
  475. }
  476. (void)memcpy(thresholds, inbuf, 512);
  477. #endif /* __NetBSD__ */
  478. return 0;
  479. }
  480. void
  481. print_help (void)
  482. {
  483. print_revision (progname, NP_VERSION);
  484. printf ("(C) 1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>\n");
  485. printf ("Plugin implementation - 1999 Robert Dale <rdale@digital-mission.com>\n");
  486. printf (COPYRIGHT, copyright, email);
  487. printf (_("This plugin checks a local hard drive with the (Linux specific) SMART interface [http://smartlinux.sourceforge.net/smart/index.php]."));
  488. printf ("\n\n");
  489. print_usage ();
  490. printf (UT_HELP_VRSN);
  491. printf (UT_EXTRA_OPTS);
  492. printf (" %s\n", "-d, --device=DEVICE");
  493. printf (" %s\n", _("Select device DEVICE"));
  494. printf (" %s\n", _("Note: if the device is specified without this option, any further option will"));
  495. printf (" %s\n", _("be ignored."));
  496. printf (UT_VERBOSE);
  497. printf ("\n");
  498. printf ("%s\n", _("Notes:"));
  499. printf (" %s\n", _("The SMART command modes (-i/--immediate, -0/--auto-off and -1/--auto-on) were"));
  500. printf (" %s\n", _("broken in an underhand manner and have been disabled. You can use smartctl"));
  501. printf (" %s\n", _("instead:"));
  502. printf (" %s\n", _("-0/--auto-off: use \"smartctl --offlineauto=off\""));
  503. printf (" %s\n", _("-1/--auto-on: use \"smartctl --offlineauto=on\""));
  504. printf (" %s\n", _("-i/--immediate: use \"smartctl --test=offline\""));
  505. printf (UT_SUPPORT);
  506. }
  507. /* todo : add to the long nanual as example
  508. *
  509. * Run with: check_ide-smart --nagios [-d] <DRIVE>
  510. * Where DRIVE is an IDE drive, ie. /dev/hda, /dev/hdb, /dev/hdc
  511. *
  512. * - Returns 0 on no errors
  513. * - Returns 1 on advisories
  514. * - Returns 2 on prefailure
  515. * - Returns -1 not too often
  516. */
  517. void
  518. print_usage (void)
  519. {
  520. printf ("%s\n", _("Usage:"));
  521. printf ("%s [-d <device>] [-v]", progname);
  522. }