check_ide_smart.c 11 KB

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