check_ide-smart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. * Net Saint - 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. #include "common.h"
  38. #include "utils.h"
  39. #include <sys/stat.h>
  40. #include <sys/ioctl.h>
  41. #include <fcntl.h>
  42. #include <linux/hdreg.h>
  43. #include <linux/types.h>
  44. #include <errno.h>
  45. #define NR_ATTRIBUTES 30
  46. #ifndef TRUE
  47. #define TRUE 1
  48. #endif /* */
  49. #define PREFAILURE 2
  50. #define ADVISORY 1
  51. #define OPERATIONAL 0
  52. #define UNKNOWN -1
  53. typedef struct threshold_s
  54. {
  55. __u8 id;
  56. __u8 threshold;
  57. __u8 reserved[10];
  58. }
  59. __attribute__ ((packed)) threshold_t;
  60. typedef struct thresholds_s
  61. {
  62. __u16 revision;
  63. threshold_t thresholds[NR_ATTRIBUTES];
  64. __u8 reserved[18];
  65. __u8 vendor[131];
  66. __u8 checksum;
  67. }
  68. __attribute__ ((packed)) thresholds_t;
  69. typedef struct value_s
  70. {
  71. __u8 id;
  72. __u16 status;
  73. __u8 value;
  74. __u8 vendor[8];
  75. }
  76. __attribute__ ((packed)) value_t;
  77. typedef struct values_s
  78. {
  79. __u16 revision;
  80. value_t values[NR_ATTRIBUTES];
  81. __u8 offline_status;
  82. __u8 vendor1;
  83. __u16 offline_timeout;
  84. __u8 vendor2;
  85. __u8 offline_capability;
  86. __u16 smart_capability;
  87. __u8 reserved[16];
  88. __u8 vendor[125];
  89. __u8 checksum;
  90. }
  91. __attribute__ ((packed)) values_t;
  92. struct
  93. {
  94. __u8 value;
  95. char *text;
  96. }
  97. offline_status_text[] =
  98. {
  99. {0x00, "NeverStarted"},
  100. {0x02, "Completed"},
  101. {0x04, "Suspended"},
  102. {0x05, "Aborted"},
  103. {0x06, "Failed"},
  104. {0, 0}
  105. };
  106. struct
  107. {
  108. __u8 value;
  109. char *text;
  110. }
  111. smart_command[] =
  112. {
  113. {SMART_ENABLE, "SMART_ENABLE"},
  114. {SMART_DISABLE, "SMART_DISABLE"},
  115. {SMART_IMMEDIATE_OFFLINE, "SMART_IMMEDIATE_OFFLINE"},
  116. {SMART_AUTO_OFFLINE, "SMART_AUTO_OFFLINE"}
  117. };
  118. /* Index to smart_command table, keep in order */
  119. enum SmartCommand
  120. { SMART_CMD_ENABLE,
  121. SMART_CMD_DISABLE,
  122. SMART_CMD_IMMEDIATE_OFFLINE,
  123. SMART_CMD_AUTO_OFFLINE
  124. };
  125. char *
  126. get_offline_text (int status)
  127. {
  128. int i;
  129. for (i = 0; offline_status_text[i].text; i++) {
  130. if (offline_status_text[i].value == status) {
  131. return offline_status_text[i].text;
  132. }
  133. }
  134. return "unknown";
  135. }
  136. int
  137. smart_read_values (int fd, values_t * values)
  138. {
  139. int e;
  140. __u8 args[4 + 512];
  141. args[0] = WIN_SMART;
  142. args[1] = 0;
  143. args[2] = SMART_READ_VALUES;
  144. args[3] = 1;
  145. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  146. e = errno;
  147. printf (_("Critical: SMART_READ_VALUES: %s\n"), strerror (errno));
  148. return e;
  149. }
  150. memcpy (values, args + 4, 512);
  151. return 0;
  152. }
  153. int
  154. values_not_passed (values_t * p, thresholds_t * t)
  155. {
  156. value_t * value = p->values;
  157. threshold_t * threshold = t->thresholds;
  158. int failed = 0;
  159. int passed = 0;
  160. int i;
  161. for (i = 0; i < NR_ATTRIBUTES; i++) {
  162. if (value->id && threshold->id && value->id == threshold->id) {
  163. if (value->value <= threshold->threshold) {
  164. ++failed;
  165. }
  166. else {
  167. ++passed;
  168. }
  169. }
  170. ++value;
  171. ++threshold;
  172. }
  173. return (passed ? -failed : 2);
  174. }
  175. int
  176. net_saint (values_t * p, thresholds_t * t)
  177. {
  178. value_t * value = p->values;
  179. threshold_t * threshold = t->thresholds;
  180. int status = OPERATIONAL;
  181. int prefailure = 0;
  182. int advisory = 0;
  183. int failed = 0;
  184. int passed = 0;
  185. int total = 0;
  186. int i;
  187. for (i = 0; i < NR_ATTRIBUTES; i++) {
  188. if (value->id && threshold->id && value->id == threshold->id) {
  189. if (value->value <= threshold->threshold) {
  190. ++failed;
  191. if (value->status & 1) {
  192. status = PREFAILURE;
  193. ++prefailure;
  194. }
  195. else {
  196. status = ADVISORY;
  197. ++advisory;
  198. }
  199. }
  200. else {
  201. ++passed;
  202. }
  203. ++total;
  204. }
  205. ++value;
  206. ++threshold;
  207. }
  208. switch (status) {
  209. case PREFAILURE:
  210. printf (_("Critical: %d Harddrive PreFailure%cDetected! %d/%d tests failed.\n"),
  211. prefailure,
  212. prefailure > 1 ? 's' : ' ',
  213. failed,
  214. total);
  215. break;
  216. case ADVISORY:
  217. printf (_("Warning: %d Harddrive Advisor%s Detected. %d/%d tests failed.\n"),
  218. advisory,
  219. advisory > 1 ? "ies" : "y",
  220. failed,
  221. total);
  222. break;
  223. case OPERATIONAL:
  224. printf (_("Status: Operational (%d/%d tests passed)\n"), passed, total);
  225. break;
  226. default:
  227. printf (_("Error: Status '%d' uknown. %d/%d tests passed\n"), status,
  228. passed, total);
  229. status = -1;
  230. break;
  231. }
  232. return status;
  233. }
  234. void
  235. print_value (value_t * p, threshold_t * t)
  236. {
  237. printf ("Id=%3d, Status=%2d {%s , %s}, Value=%3d, Threshold=%3d, %s\n",
  238. p->id, p->status, p->status & 1 ? "PreFailure" : "Advisory ",
  239. p->status & 2 ? "OnLine " : "OffLine", p->value, t->threshold,
  240. p->value > t->threshold ? "Passed" : "Failed");
  241. }
  242. void
  243. print_values (values_t * p, thresholds_t * t)
  244. {
  245. value_t * value = p->values;
  246. threshold_t * threshold = t->thresholds;
  247. int i;
  248. for (i = 0; i < NR_ATTRIBUTES; i++) {
  249. if (value->id && threshold->id && value->id == threshold->id) {
  250. print_value (value++, threshold++);
  251. }
  252. }
  253. printf
  254. (_("OffLineStatus=%d {%s}, AutoOffLine=%s, OffLineTimeout=%d minutes\n"),
  255. p->offline_status,
  256. get_offline_text (p->offline_status & 0x7f),
  257. (p->offline_status & 0x80 ? "Yes" : "No"),
  258. p->offline_timeout / 60);
  259. printf
  260. (_("OffLineCapability=%d {%s %s %s}\n"),
  261. p->offline_capability,
  262. p->offline_capability & 1 ? "Immediate" : "",
  263. p->offline_capability & 2 ? "Auto" : "",
  264. p->offline_capability & 4 ? "AbortOnCmd" : "SuspendOnCmd");
  265. printf
  266. (_("SmartRevision=%d, CheckSum=%d, SmartCapability=%d {%s %s}\n"),
  267. p->revision,
  268. p->checksum,
  269. p->smart_capability,
  270. p->smart_capability & 1 ? "SaveOnStandBy" : "",
  271. p->smart_capability & 2 ? "AutoSave" : "");
  272. }
  273. void
  274. print_thresholds (thresholds_t * p)
  275. {
  276. threshold_t * threshold = p->thresholds;
  277. int i;
  278. printf ("\n");
  279. printf ("SmartRevision=%d\n", p->revision);
  280. for (i = 0; i < NR_ATTRIBUTES; i++) {
  281. if (threshold->id) {
  282. printf ("Id=%3d, Threshold=%3d\n", threshold->id,
  283. threshold->threshold); }
  284. ++threshold;
  285. }
  286. printf ("CheckSum=%d\n", p->checksum);
  287. }
  288. int
  289. smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0,
  290. char show_error)
  291. {
  292. int e = 0;
  293. __u8 args[4];
  294. args[0] = WIN_SMART;
  295. args[1] = val0;
  296. args[2] = smart_command[command].value;
  297. args[3] = 0;
  298. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  299. e = errno;
  300. if (show_error) {
  301. printf (_("Critical: %s: %s\n"), smart_command[command].text, strerror (errno));
  302. }
  303. }
  304. return e;
  305. }
  306. int
  307. smart_read_thresholds (int fd, thresholds_t * thresholds)
  308. {
  309. int e;
  310. __u8 args[4 + 512];
  311. args[0] = WIN_SMART;
  312. args[1] = 0;
  313. args[2] = SMART_READ_THRESHOLDS;
  314. args[3] = 1;
  315. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  316. e = errno;
  317. printf (_("Critical: SMART_READ_THRESHOLDS: %s\n"), strerror (errno));
  318. return e;
  319. }
  320. memcpy (thresholds, args + 4, 512);
  321. return 0;
  322. }
  323. void
  324. show_version ()
  325. {
  326. printf ("check_ide-smart v.1 - FREE Software with NO WARRANTY\n");
  327. printf ("Nagios feature - Robert Dale <rdale@digital-mission.com>\n");
  328. printf ("(C) 1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>\n");
  329. }
  330. void
  331. show_help ()
  332. {
  333. printf (_("\
  334. Usage: check_ide-smart [DEVICE] [OPTION]\n\
  335. -d, --device=DEVICE\n\
  336. Select device DEVICE\n\
  337. -i, --immediate\n\
  338. Perform immediately offline tests\n\
  339. -q, --quiet-check\n\
  340. Returns the number of failed tests\n\
  341. -1, --auto-on\n\
  342. Turn on automatic offline tests\n\
  343. -0, --auto-off\n\
  344. Turn off automatic offline tests\n\
  345. -n, --net-saint\n\
  346. Output suitable for Net Saint\n\
  347. -h, --help\n\
  348. -V, --version\n"));
  349. }
  350. int
  351. main (int argc, char *argv[])
  352. {
  353. char *device = NULL;
  354. int command = -1;
  355. int o, longindex;
  356. int retval = 0;
  357. thresholds_t thresholds;
  358. values_t values;
  359. int fd;
  360. static struct option longopts[] = {
  361. {"device", required_argument, 0, 'd'},
  362. {"immediate", no_argument, 0, 'i'},
  363. {"quiet-check", no_argument, 0, 'q'},
  364. {"auto-on", no_argument, 0, '1'},
  365. {"auto-off", no_argument, 0, '0'},
  366. {"net-saint", no_argument, 0, 'n'},
  367. {"help", no_argument, 0, 'h'},
  368. {"version", no_argument, 0, 'V'}, {0, 0, 0, 0}
  369. };
  370. while (1) {
  371. o = getopt_long (argc, argv, "+d:iq10nhV", longopts, &longindex);
  372. if (o == -1 || o == EOF)
  373. break;
  374. switch (o) {
  375. case 'd':
  376. device = optarg;
  377. break;
  378. case 'q':
  379. command = 3;
  380. break;
  381. case 'i':
  382. command = 2;
  383. break;
  384. case '1':
  385. command = 1;
  386. break;
  387. case '0':
  388. command = 0;
  389. break;
  390. case 'n':
  391. command = 4;
  392. break;
  393. case 'h':
  394. show_help ();
  395. return 0;
  396. case 'V':
  397. show_version ();
  398. return 0;
  399. default:
  400. printf (_("Try `%s --help' for more information.\n"), argv[0]);
  401. return 1;
  402. }
  403. if (optind < argc) {
  404. device = argv[optind];
  405. }
  406. if (!device) {
  407. show_help ();
  408. show_version ();
  409. return -1;
  410. }
  411. fd = open (device, O_RDONLY);
  412. if (fd < 0) {
  413. printf (_("Critical: Couldn't open device: %s\n"), strerror (errno));
  414. return 2;
  415. }
  416. if (smart_cmd_simple (fd, SMART_CMD_ENABLE, 0, TRUE)) {
  417. printf (_("Critical: SMART_CMD_ENABLE\n"));
  418. return 2;
  419. }
  420. switch (command) {
  421. case 0:
  422. retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0, TRUE);
  423. break;
  424. case 1:
  425. retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0xF8, TRUE);
  426. break;
  427. case 2:
  428. retval = smart_cmd_simple (fd, SMART_CMD_IMMEDIATE_OFFLINE, 0, TRUE);
  429. break;
  430. case 3:
  431. smart_read_values (fd, &values);
  432. smart_read_thresholds (fd, &thresholds);
  433. retval = values_not_passed (&values, &thresholds);
  434. break;
  435. case 4:
  436. smart_read_values (fd, &values);
  437. smart_read_thresholds (fd, &thresholds);
  438. retval = net_saint (&values, &thresholds);
  439. break;
  440. default:
  441. smart_read_values (fd, &values);
  442. smart_read_thresholds (fd, &thresholds);
  443. print_values (&values, &thresholds);
  444. break;
  445. }
  446. close (fd);
  447. }
  448. return retval;
  449. }