check_ide-smart.c 11 KB

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