check_ide_smart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * check_ide-smart v.1 - hacked version of ide-smart for Nagios
  3. * Copyright (C) 2000 Robert Dale <rdale@digital-mission.com>
  4. *
  5. * Nagios - http://www.nagios.org
  6. *
  7. * Notes:
  8. * ide-smart has the same functionality as before. Some return
  9. * values were changed, otherwise the --nagios option was added.
  10. *
  11. * Run with: check_ide-smart --nagios [-d] <DRIVE>
  12. * Where DRIVE is an IDE drive, ie. /dev/hda, /dev/hdb, /dev/hdc
  13. *
  14. * - Returns 0 on no errors
  15. * - Returns 1 on advisories
  16. * - Returns 2 on prefailure
  17. * - Returns -1 not too often
  18. *
  19. * ide-smart 1.3 - IDE S.M.A.R.T. checking tool
  20. * Copyright (C) 1998-1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>
  21. * 1998 Gadi Oxman <gadio@netvision.net.il>
  22. *
  23. * This program is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License as published by
  25. * the Free Software Foundation; either version 2 of the License, or
  26. * (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  36. *
  37. * $Id$
  38. */
  39. const char *progname = "check_ide_smart";
  40. const char *revision = "$Revision$";
  41. const char *copyright = "2000-2004";
  42. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  43. #include "common.h"
  44. #include "utils.h"
  45. void print_help (void);
  46. void print_usage (void);
  47. #include <sys/stat.h>
  48. #include <sys/ioctl.h>
  49. #include <fcntl.h>
  50. #include <linux/hdreg.h>
  51. #include <linux/types.h>
  52. #include <errno.h>
  53. #define NR_ATTRIBUTES 30
  54. #ifndef TRUE
  55. #define TRUE 1
  56. #endif /* */
  57. #define PREFAILURE 2
  58. #define ADVISORY 1
  59. #define OPERATIONAL 0
  60. #define UNKNOWN -1
  61. typedef struct threshold_s
  62. {
  63. __u8 id;
  64. __u8 threshold;
  65. __u8 reserved[10];
  66. }
  67. __attribute__ ((packed)) threshold_t;
  68. typedef struct thresholds_s
  69. {
  70. __u16 revision;
  71. threshold_t thresholds[NR_ATTRIBUTES];
  72. __u8 reserved[18];
  73. __u8 vendor[131];
  74. __u8 checksum;
  75. }
  76. __attribute__ ((packed)) thresholds_t;
  77. typedef struct value_s
  78. {
  79. __u8 id;
  80. __u16 status;
  81. __u8 value;
  82. __u8 vendor[8];
  83. }
  84. __attribute__ ((packed)) value_t;
  85. typedef struct values_s
  86. {
  87. __u16 revision;
  88. value_t values[NR_ATTRIBUTES];
  89. __u8 offline_status;
  90. __u8 vendor1;
  91. __u16 offline_timeout;
  92. __u8 vendor2;
  93. __u8 offline_capability;
  94. __u16 smart_capability;
  95. __u8 reserved[16];
  96. __u8 vendor[125];
  97. __u8 checksum;
  98. }
  99. __attribute__ ((packed)) values_t;
  100. struct
  101. {
  102. __u8 value;
  103. char *text;
  104. }
  105. offline_status_text[] =
  106. {
  107. {0x00, "NeverStarted"},
  108. {0x02, "Completed"},
  109. {0x04, "Suspended"},
  110. {0x05, "Aborted"},
  111. {0x06, "Failed"},
  112. {0, 0}
  113. };
  114. struct
  115. {
  116. __u8 value;
  117. char *text;
  118. }
  119. smart_command[] =
  120. {
  121. {SMART_ENABLE, "SMART_ENABLE"},
  122. {SMART_DISABLE, "SMART_DISABLE"},
  123. {SMART_IMMEDIATE_OFFLINE, "SMART_IMMEDIATE_OFFLINE"},
  124. {SMART_AUTO_OFFLINE, "SMART_AUTO_OFFLINE"}
  125. };
  126. /* Index to smart_command table, keep in order */
  127. enum SmartCommand
  128. { SMART_CMD_ENABLE,
  129. SMART_CMD_DISABLE,
  130. SMART_CMD_IMMEDIATE_OFFLINE,
  131. SMART_CMD_AUTO_OFFLINE
  132. };
  133. void print_values (values_t * p, thresholds_t * t);
  134. int smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0, char show_error);
  135. int
  136. main (int argc, char *argv[])
  137. {
  138. char *device = NULL;
  139. int command = -1;
  140. int o, longindex;
  141. int retval = 0;
  142. thresholds_t thresholds;
  143. values_t values;
  144. int fd;
  145. static struct option longopts[] = {
  146. {"device", required_argument, 0, 'd'},
  147. {"immediate", no_argument, 0, 'i'},
  148. {"quiet-check", no_argument, 0, 'q'},
  149. {"auto-on", no_argument, 0, '1'},
  150. {"auto-off", no_argument, 0, '0'},
  151. {"nagios", no_argument, 0, 'n'},
  152. {"help", no_argument, 0, 'h'},
  153. {"version", no_argument, 0, 'V'}, {0, 0, 0, 0}
  154. };
  155. setlocale (LC_ALL, "");
  156. bindtextdomain (PACKAGE, LOCALEDIR);
  157. textdomain (PACKAGE);
  158. while (1) {
  159. o = getopt_long (argc, argv, "+d:iq10nhV", longopts, &longindex);
  160. switch (o) {
  161. case -1:
  162. /*
  163. * bail out of the switch but not the loop, so
  164. * that device can be extracted from argv.
  165. */
  166. break;
  167. case 'd':
  168. device = optarg;
  169. break;
  170. case 'q':
  171. command = 3;
  172. break;
  173. case 'i':
  174. command = 2;
  175. break;
  176. case '1':
  177. command = 1;
  178. break;
  179. case '0':
  180. command = 0;
  181. break;
  182. case 'n':
  183. command = 4;
  184. break;
  185. case 'h':
  186. print_help ();
  187. return STATE_OK;
  188. case 'V':
  189. print_revision (progname, revision);
  190. return STATE_OK;
  191. default:
  192. usage2 (_("Unknown argument"), optarg);
  193. }
  194. if (optind < argc) {
  195. device = argv[optind];
  196. }
  197. if (!device) {
  198. print_help ();
  199. return -1;
  200. }
  201. fd = open (device, O_RDONLY);
  202. if (fd < 0) {
  203. printf (_("CRITICAL - Couldn't open device %s: %s\n"), device, strerror (errno));
  204. return 2;
  205. }
  206. if (smart_cmd_simple (fd, SMART_CMD_ENABLE, 0, TRUE)) {
  207. printf (_("CRITICAL - SMART_CMD_ENABLE\n"));
  208. return 2;
  209. }
  210. switch (command) {
  211. case 0:
  212. retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0, TRUE);
  213. break;
  214. case 1:
  215. retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0xF8, TRUE);
  216. break;
  217. case 2:
  218. retval = smart_cmd_simple (fd, SMART_CMD_IMMEDIATE_OFFLINE, 0, TRUE);
  219. break;
  220. case 3:
  221. smart_read_values (fd, &values);
  222. smart_read_thresholds (fd, &thresholds);
  223. retval = values_not_passed (&values, &thresholds);
  224. break;
  225. case 4:
  226. smart_read_values (fd, &values);
  227. smart_read_thresholds (fd, &thresholds);
  228. retval = nagios (&values, &thresholds);
  229. break;
  230. default:
  231. smart_read_values (fd, &values);
  232. smart_read_thresholds (fd, &thresholds);
  233. print_values (&values, &thresholds);
  234. break;
  235. }
  236. close (fd);
  237. }
  238. return retval;
  239. }
  240. char *
  241. get_offline_text (int status)
  242. {
  243. int i;
  244. for (i = 0; offline_status_text[i].text; i++) {
  245. if (offline_status_text[i].value == status) {
  246. return offline_status_text[i].text;
  247. }
  248. }
  249. return "UNKNOW";
  250. }
  251. int
  252. smart_read_values (int fd, values_t * values)
  253. {
  254. int e;
  255. __u8 args[4 + 512];
  256. args[0] = WIN_SMART;
  257. args[1] = 0;
  258. args[2] = SMART_READ_VALUES;
  259. args[3] = 1;
  260. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  261. e = errno;
  262. printf (_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror (errno));
  263. return e;
  264. }
  265. memcpy (values, args + 4, 512);
  266. return 0;
  267. }
  268. int
  269. values_not_passed (values_t * p, thresholds_t * t)
  270. {
  271. value_t * value = p->values;
  272. threshold_t * threshold = t->thresholds;
  273. int failed = 0;
  274. int passed = 0;
  275. int i;
  276. for (i = 0; i < NR_ATTRIBUTES; i++) {
  277. if (value->id && threshold->id && value->id == threshold->id) {
  278. if (value->value <= threshold->threshold) {
  279. ++failed;
  280. }
  281. else {
  282. ++passed;
  283. }
  284. }
  285. ++value;
  286. ++threshold;
  287. }
  288. return (passed ? -failed : 2);
  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. break;
  331. case ADVISORY:
  332. printf (_("WARNING - %d Harddrive Advisor%s Detected. %d/%d tests failed.\n"),
  333. advisory,
  334. advisory > 1 ? "ies" : "y",
  335. failed,
  336. total);
  337. break;
  338. case OPERATIONAL:
  339. printf (_("OK - Operational (%d/%d tests passed)\n"), passed, total);
  340. break;
  341. default:
  342. printf (_("ERROR - Status '%d' uknown. %d/%d tests passed\n"), status,
  343. passed, total);
  344. status = -1;
  345. break;
  346. }
  347. return status;
  348. }
  349. void
  350. print_value (value_t * p, threshold_t * t)
  351. {
  352. printf ("Id=%3d, Status=%2d {%s , %s}, Value=%3d, Threshold=%3d, %s\n",
  353. p->id, p->status, p->status & 1 ? "PreFailure" : "Advisory ",
  354. p->status & 2 ? "OnLine " : "OffLine", p->value, t->threshold,
  355. p->value > t->threshold ? "Passed" : "Failed");
  356. }
  357. void
  358. print_values (values_t * p, thresholds_t * t)
  359. {
  360. value_t * value = p->values;
  361. threshold_t * threshold = t->thresholds;
  362. int i;
  363. for (i = 0; i < NR_ATTRIBUTES; i++) {
  364. if (value->id && threshold->id && value->id == threshold->id) {
  365. print_value (value++, threshold++);
  366. }
  367. }
  368. printf
  369. (_("OffLineStatus=%d {%s}, AutoOffLine=%s, OffLineTimeout=%d minutes\n"),
  370. p->offline_status,
  371. get_offline_text (p->offline_status & 0x7f),
  372. (p->offline_status & 0x80 ? "Yes" : "No"),
  373. p->offline_timeout / 60);
  374. printf
  375. (_("OffLineCapability=%d {%s %s %s}\n"),
  376. p->offline_capability,
  377. p->offline_capability & 1 ? "Immediate" : "",
  378. p->offline_capability & 2 ? "Auto" : "",
  379. p->offline_capability & 4 ? "AbortOnCmd" : "SuspendOnCmd");
  380. printf
  381. (_("SmartRevision=%d, CheckSum=%d, SmartCapability=%d {%s %s}\n"),
  382. p->revision,
  383. p->checksum,
  384. p->smart_capability,
  385. p->smart_capability & 1 ? "SaveOnStandBy" : "",
  386. p->smart_capability & 2 ? "AutoSave" : "");
  387. }
  388. void
  389. print_thresholds (thresholds_t * p)
  390. {
  391. threshold_t * threshold = p->thresholds;
  392. int i;
  393. printf ("\n");
  394. printf ("SmartRevision=%d\n", p->revision);
  395. for (i = 0; i < NR_ATTRIBUTES; i++) {
  396. if (threshold->id) {
  397. printf ("Id=%3d, Threshold=%3d\n", threshold->id,
  398. threshold->threshold); }
  399. ++threshold;
  400. }
  401. printf ("CheckSum=%d\n", p->checksum);
  402. }
  403. int
  404. smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0, char show_error)
  405. {
  406. int e = 0;
  407. __u8 args[4];
  408. args[0] = WIN_SMART;
  409. args[1] = val0;
  410. args[2] = smart_command[command].value;
  411. args[3] = 0;
  412. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  413. e = errno;
  414. if (show_error) {
  415. printf (_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror (errno));
  416. }
  417. }
  418. return e;
  419. }
  420. int
  421. smart_read_thresholds (int fd, thresholds_t * thresholds)
  422. {
  423. int e;
  424. __u8 args[4 + 512];
  425. args[0] = WIN_SMART;
  426. args[1] = 0;
  427. args[2] = SMART_READ_THRESHOLDS;
  428. args[3] = 1;
  429. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  430. e = errno;
  431. printf (_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror (errno));
  432. return e;
  433. }
  434. memcpy (thresholds, args + 4, 512);
  435. return 0;
  436. }
  437. void
  438. print_help (void)
  439. {
  440. print_revision (progname, revision);
  441. printf ("Nagios feature - 1999 Robert Dale <rdale@digital-mission.com>\n");
  442. printf ("(C) 1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>\n");
  443. printf (COPYRIGHT, copyright, email);
  444. printf(_("This plugin checks a local hard drive with the (Linux specific) SMART interface [http://smartlinux.sourceforge.net/smart/index.php].\n\n"));
  445. printf ("\
  446. Usage: %s [OPTION] [DEVICE]\n\
  447. -d, --device=DEVICE\n\
  448. Select device DEVICE\n\
  449. Note: if the device is selected with this option, _no_ other options are accepted\n\
  450. -i, --immediate\n\
  451. Perform immediately offline tests\n\
  452. -q, --quiet-check\n\
  453. Returns the number of failed tests\n\
  454. -1, --auto-on\n\
  455. Turn on automatic offline tests\n\
  456. -0, --auto-off\n\
  457. Turn off automatic offline tests\n\
  458. -n, --nagios\n\
  459. Output suitable for Nagios\n", progname);
  460. }
  461. void
  462. print_usage (void)
  463. {
  464. printf ("\
  465. Usage: %s [-d <device>] [-i <immediate>] [-q quiet] [-1 <auto-on>]\n\
  466. [-O <auto-off>] [-n <nagios>]\n", progname);
  467. }