4
0

check_ide_smart.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. * 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 = "nagiosplug-devel@lists.sourceforge.net";
  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 values_not_passed (values_t *, thresholds_t *);
  149. int nagios (values_t *, thresholds_t *);
  150. void print_value (value_t *, threshold_t *);
  151. void print_values (values_t *, thresholds_t *);
  152. int smart_cmd_simple (int, enum SmartCommand, __u8, char);
  153. int smart_read_thresholds (int, thresholds_t *);
  154. int
  155. main (int argc, char *argv[])
  156. {
  157. char *device = NULL;
  158. int command = -1;
  159. int o, longindex;
  160. int retval = 0;
  161. thresholds_t thresholds;
  162. values_t values;
  163. int fd;
  164. static struct option longopts[] = {
  165. {"device", required_argument, 0, 'd'},
  166. {"immediate", no_argument, 0, 'i'},
  167. {"quiet-check", no_argument, 0, 'q'},
  168. {"auto-on", no_argument, 0, '1'},
  169. {"auto-off", no_argument, 0, '0'},
  170. {"nagios", no_argument, 0, 'n'},
  171. {"help", no_argument, 0, 'h'},
  172. {"version", no_argument, 0, 'V'},
  173. {0, 0, 0, 0}
  174. };
  175. /* Parse extra opts if any */
  176. argv=np_extra_opts (&argc, argv, progname);
  177. setlocale (LC_ALL, "");
  178. bindtextdomain (PACKAGE, LOCALEDIR);
  179. textdomain (PACKAGE);
  180. while (1) {
  181. o = getopt_long (argc, argv, "+d:iq10nhV", longopts, &longindex);
  182. if (o == -1 || o == EOF || o == 1)
  183. break;
  184. switch (o) {
  185. case 'd':
  186. device = optarg;
  187. break;
  188. case 'q':
  189. command = 3;
  190. break;
  191. case 'i':
  192. command = 2;
  193. break;
  194. case '1':
  195. command = 1;
  196. break;
  197. case '0':
  198. command = 0;
  199. break;
  200. case 'n':
  201. command = 4;
  202. break;
  203. case 'h':
  204. print_help ();
  205. return STATE_OK;
  206. case 'V':
  207. print_revision (progname, NP_VERSION);
  208. return STATE_OK;
  209. default:
  210. usage5 ();
  211. }
  212. }
  213. if (optind < argc) {
  214. device = argv[optind];
  215. }
  216. if (!device) {
  217. print_help ();
  218. return STATE_OK;
  219. }
  220. fd = open (device, OPEN_MODE);
  221. if (fd < 0) {
  222. printf (_("CRITICAL - Couldn't open device %s: %s\n"), device, strerror (errno));
  223. return STATE_CRITICAL;
  224. }
  225. if (smart_cmd_simple (fd, SMART_CMD_ENABLE, 0, TRUE)) {
  226. printf (_("CRITICAL - SMART_CMD_ENABLE\n"));
  227. return STATE_CRITICAL;
  228. }
  229. switch (command) {
  230. case 0:
  231. retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0, TRUE);
  232. break;
  233. case 1:
  234. retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0xF8, TRUE);
  235. break;
  236. case 2:
  237. retval = smart_cmd_simple (fd, SMART_CMD_IMMEDIATE_OFFLINE, 0, TRUE);
  238. break;
  239. case 3:
  240. smart_read_values (fd, &values);
  241. smart_read_thresholds (fd, &thresholds);
  242. retval = values_not_passed (&values, &thresholds);
  243. break;
  244. case 4:
  245. smart_read_values (fd, &values);
  246. smart_read_thresholds (fd, &thresholds);
  247. retval = nagios (&values, &thresholds);
  248. break;
  249. default:
  250. smart_read_values (fd, &values);
  251. smart_read_thresholds (fd, &thresholds);
  252. print_values (&values, &thresholds);
  253. break;
  254. }
  255. close (fd);
  256. return retval;
  257. }
  258. char *
  259. get_offline_text (int status)
  260. {
  261. int i;
  262. for (i = 0; offline_status_text[i].text; i++) {
  263. if (offline_status_text[i].value == status) {
  264. return offline_status_text[i].text;
  265. }
  266. }
  267. return "UNKNOW";
  268. }
  269. int
  270. smart_read_values (int fd, values_t * values)
  271. {
  272. #ifdef __linux__
  273. int e;
  274. __u8 args[4 + 512];
  275. args[0] = WIN_SMART;
  276. args[1] = 0;
  277. args[2] = SMART_READ_VALUES;
  278. args[3] = 1;
  279. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  280. e = errno;
  281. printf (_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror (errno));
  282. return e;
  283. }
  284. memcpy (values, args + 4, 512);
  285. #endif /* __linux__ */
  286. #ifdef __NetBSD__
  287. struct atareq req;
  288. unsigned char inbuf[DEV_BSIZE];
  289. memset(&req, 0, sizeof(req));
  290. req.timeout = 1000;
  291. memset(&inbuf, 0, sizeof(inbuf));
  292. req.flags = ATACMD_READ;
  293. req.features = WDSM_RD_DATA;
  294. req.command = WDCC_SMART;
  295. req.databuf = (char *)inbuf;
  296. req.datalen = sizeof(inbuf);
  297. req.cylinder = WDSMART_CYL;
  298. if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) {
  299. if (req.retsts != ATACMD_OK)
  300. errno = ENODEV;
  301. }
  302. if (errno != 0) {
  303. int e = errno;
  304. printf (_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror (errno));
  305. return e;
  306. }
  307. (void)memcpy(values, inbuf, 512);
  308. #endif /* __NetBSD__ */
  309. return 0;
  310. }
  311. int
  312. values_not_passed (values_t * p, thresholds_t * t)
  313. {
  314. value_t * value = p->values;
  315. threshold_t * threshold = t->thresholds;
  316. int failed = 0;
  317. int passed = 0;
  318. int i;
  319. for (i = 0; i < NR_ATTRIBUTES; i++) {
  320. if (value->id && threshold->id && value->id == threshold->id) {
  321. if (value->value <= threshold->threshold) {
  322. ++failed;
  323. }
  324. else {
  325. ++passed;
  326. }
  327. }
  328. ++value;
  329. ++threshold;
  330. }
  331. return (passed ? -failed : 2);
  332. }
  333. int
  334. nagios (values_t * p, thresholds_t * t)
  335. {
  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;
  394. }
  395. void
  396. print_value (value_t * p, threshold_t * t)
  397. {
  398. printf ("Id=%3d, Status=%2d {%s , %s}, Value=%3d, Threshold=%3d, %s\n",
  399. p->id, p->status, p->status & 1 ? "PreFailure" : "Advisory ",
  400. p->status & 2 ? "OnLine " : "OffLine", p->value, t->threshold,
  401. p->value > t->threshold ? "Passed" : "Failed");
  402. }
  403. void
  404. print_values (values_t * p, thresholds_t * t)
  405. {
  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" : "");
  433. }
  434. int
  435. smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0, char show_error)
  436. {
  437. int e = 0;
  438. #ifdef __linux__
  439. __u8 args[4];
  440. args[0] = WIN_SMART;
  441. args[1] = val0;
  442. args[2] = smart_command[command].value;
  443. args[3] = 0;
  444. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  445. e = errno;
  446. if (show_error) {
  447. printf (_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror (errno));
  448. }
  449. }
  450. #endif /* __linux__ */
  451. #ifdef __NetBSD__
  452. struct atareq req;
  453. memset(&req, 0, sizeof(req));
  454. req.timeout = 1000;
  455. req.flags = ATACMD_READREG;
  456. req.features = smart_command[command].value;
  457. req.command = WDCC_SMART;
  458. req.cylinder = WDSMART_CYL;
  459. req.sec_count = val0;
  460. if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) {
  461. if (req.retsts != ATACMD_OK)
  462. errno = ENODEV;
  463. if (req.cylinder != WDSMART_CYL)
  464. errno = ENODEV;
  465. }
  466. if (errno != 0) {
  467. e = errno;
  468. printf (_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror (errno));
  469. return e;
  470. }
  471. #endif /* __NetBSD__ */
  472. return e;
  473. }
  474. int
  475. smart_read_thresholds (int fd, thresholds_t * thresholds)
  476. {
  477. #ifdef __linux__
  478. int e;
  479. __u8 args[4 + 512];
  480. args[0] = WIN_SMART;
  481. args[1] = 0;
  482. args[2] = SMART_READ_THRESHOLDS;
  483. args[3] = 1;
  484. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  485. e = errno;
  486. printf (_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror (errno));
  487. return e;
  488. }
  489. memcpy (thresholds, args + 4, 512);
  490. #endif /* __linux__ */
  491. #ifdef __NetBSD__
  492. struct atareq req;
  493. unsigned char inbuf[DEV_BSIZE];
  494. memset(&req, 0, sizeof(req));
  495. req.timeout = 1000;
  496. memset(&inbuf, 0, sizeof(inbuf));
  497. req.flags = ATACMD_READ;
  498. req.features = WDSM_RD_THRESHOLDS;
  499. req.command = WDCC_SMART;
  500. req.databuf = (char *)inbuf;
  501. req.datalen = sizeof(inbuf);
  502. req.cylinder = WDSMART_CYL;
  503. if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) {
  504. if (req.retsts != ATACMD_OK)
  505. errno = ENODEV;
  506. }
  507. if (errno != 0) {
  508. int e = errno;
  509. printf (_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror (errno));
  510. return e;
  511. }
  512. (void)memcpy(thresholds, inbuf, 512);
  513. #endif /* __NetBSD__ */
  514. return 0;
  515. }
  516. void
  517. print_help (void)
  518. {
  519. print_revision (progname, NP_VERSION);
  520. printf ("Nagios feature - 1999 Robert Dale <rdale@digital-mission.com>\n");
  521. printf ("(C) 1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>\n");
  522. printf (COPYRIGHT, copyright, email);
  523. printf (_("This plugin checks a local hard drive with the (Linux specific) SMART interface [http://smartlinux.sourceforge.net/smart/index.php]."));
  524. printf ("\n\n");
  525. print_usage ();
  526. printf (UT_HELP_VRSN);
  527. printf (UT_EXTRA_OPTS);
  528. printf (" %s\n", "-d, --device=DEVICE");
  529. printf (" %s\n", _("Select device DEVICE"));
  530. printf (" %s\n", _("Note: if the device is selected with this option, _no_ other options are accepted"));
  531. printf (" %s\n", "-i, --immediate");
  532. printf (" %s\n", _("Perform immediately offline tests"));
  533. printf (" %s\n", "-q, --quiet-check");
  534. printf (" %s\n", _("Returns the number of failed tests"));
  535. printf (" %s\n", "-1, --auto-on");
  536. printf (" %s\n", _("Turn on automatic offline tests"));
  537. printf (" %s\n", "-0, --auto-off");
  538. printf (" %s\n", _("Turn off automatic offline tests"));
  539. printf (" %s\n", "-n, --nagios");
  540. printf (" %s\n", _("Output suitable for Nagios"));
  541. printf (UT_SUPPORT);
  542. }
  543. /* todo : add to the long nanual as example
  544. *
  545. * Run with: check_ide-smart --nagios [-d] <DRIVE>
  546. * Where DRIVE is an IDE drive, ie. /dev/hda, /dev/hdb, /dev/hdc
  547. *
  548. * - Returns 0 on no errors
  549. * - Returns 1 on advisories
  550. * - Returns 2 on prefailure
  551. * - Returns -1 not too often
  552. */
  553. void
  554. print_usage (void)
  555. {
  556. printf ("%s\n", _("Usage:"));
  557. printf ("%s [-d <device>] [-i <immediate>] [-q quiet] [-1 <auto-on>]",progname);
  558. printf (" [-O <auto-off>] [-n <nagios>]\n");
  559. }