check_ide-smart.c 11 KB

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