check_ide-smart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. #include "common.h"
  40. #include "utils.h"
  41. #include <sys/stat.h>
  42. #include <sys/ioctl.h>
  43. #include <fcntl.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. {0x00, "NeverStarted"},
  102. {0x02, "Completed"},
  103. {0x04, "Suspended"},
  104. {0x05, "Aborted"},
  105. {0x06, "Failed"},
  106. {0, 0}
  107. };
  108. struct
  109. {
  110. __u8 value;
  111. char *text;
  112. }
  113. smart_command[] =
  114. {
  115. {SMART_ENABLE, "SMART_ENABLE"},
  116. {SMART_DISABLE, "SMART_DISABLE"},
  117. {SMART_IMMEDIATE_OFFLINE, "SMART_IMMEDIATE_OFFLINE"},
  118. {SMART_AUTO_OFFLINE, "SMART_AUTO_OFFLINE"}
  119. };
  120. /* Index to smart_command table, keep in order */
  121. enum SmartCommand
  122. { SMART_CMD_ENABLE,
  123. SMART_CMD_DISABLE,
  124. SMART_CMD_IMMEDIATE_OFFLINE,
  125. SMART_CMD_AUTO_OFFLINE
  126. };
  127. char *
  128. get_offline_text (int status)
  129. {
  130. int i;
  131. for (i = 0; offline_status_text[i].text; i++) {
  132. if (offline_status_text[i].value == status) {
  133. return offline_status_text[i].text;
  134. }
  135. }
  136. return "UNKNOW";
  137. }
  138. int
  139. smart_read_values (int fd, values_t * values)
  140. {
  141. int e;
  142. __u8 args[4 + 512];
  143. args[0] = WIN_SMART;
  144. args[1] = 0;
  145. args[2] = SMART_READ_VALUES;
  146. args[3] = 1;
  147. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  148. e = errno;
  149. printf (_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror (errno));
  150. return e;
  151. }
  152. memcpy (values, args + 4, 512);
  153. return 0;
  154. }
  155. int
  156. values_not_passed (values_t * p, thresholds_t * t)
  157. {
  158. value_t * value = p->values;
  159. threshold_t * threshold = t->thresholds;
  160. int failed = 0;
  161. int passed = 0;
  162. int i;
  163. for (i = 0; i < NR_ATTRIBUTES; i++) {
  164. if (value->id && threshold->id && value->id == threshold->id) {
  165. if (value->value <= threshold->threshold) {
  166. ++failed;
  167. }
  168. else {
  169. ++passed;
  170. }
  171. }
  172. ++value;
  173. ++threshold;
  174. }
  175. return (passed ? -failed : 2);
  176. }
  177. int
  178. net_saint (values_t * p, thresholds_t * t)
  179. {
  180. value_t * value = p->values;
  181. threshold_t * threshold = t->thresholds;
  182. int status = OPERATIONAL;
  183. int prefailure = 0;
  184. int advisory = 0;
  185. int failed = 0;
  186. int passed = 0;
  187. int total = 0;
  188. int i;
  189. for (i = 0; i < NR_ATTRIBUTES; i++) {
  190. if (value->id && threshold->id && value->id == threshold->id) {
  191. if (value->value <= threshold->threshold) {
  192. ++failed;
  193. if (value->status & 1) {
  194. status = PREFAILURE;
  195. ++prefailure;
  196. }
  197. else {
  198. status = ADVISORY;
  199. ++advisory;
  200. }
  201. }
  202. else {
  203. ++passed;
  204. }
  205. ++total;
  206. }
  207. ++value;
  208. ++threshold;
  209. }
  210. switch (status) {
  211. case PREFAILURE:
  212. printf (_("CRITICAL - %d Harddrive PreFailure%cDetected! %d/%d tests failed.\n"),
  213. prefailure,
  214. prefailure > 1 ? 's' : ' ',
  215. failed,
  216. total);
  217. break;
  218. case ADVISORY:
  219. printf (_("WARNING - %d Harddrive Advisor%s Detected. %d/%d tests failed.\n"),
  220. advisory,
  221. advisory > 1 ? "ies" : "y",
  222. failed,
  223. 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,
  258. get_offline_text (p->offline_status & 0x7f),
  259. (p->offline_status & 0x80 ? "Yes" : "No"),
  260. p->offline_timeout / 60);
  261. printf
  262. (_("OffLineCapability=%d {%s %s %s}\n"),
  263. p->offline_capability,
  264. p->offline_capability & 1 ? "Immediate" : "",
  265. p->offline_capability & 2 ? "Auto" : "",
  266. p->offline_capability & 4 ? "AbortOnCmd" : "SuspendOnCmd");
  267. printf
  268. (_("SmartRevision=%d, CheckSum=%d, SmartCapability=%d {%s %s}\n"),
  269. p->revision,
  270. p->checksum,
  271. p->smart_capability,
  272. p->smart_capability & 1 ? "SaveOnStandBy" : "",
  273. p->smart_capability & 2 ? "AutoSave" : "");
  274. }
  275. void
  276. print_thresholds (thresholds_t * p)
  277. {
  278. threshold_t * threshold = p->thresholds;
  279. int i;
  280. printf ("\n");
  281. printf ("SmartRevision=%d\n", p->revision);
  282. for (i = 0; i < NR_ATTRIBUTES; i++) {
  283. if (threshold->id) {
  284. printf ("Id=%3d, Threshold=%3d\n", threshold->id,
  285. threshold->threshold); }
  286. ++threshold;
  287. }
  288. printf ("CheckSum=%d\n", p->checksum);
  289. }
  290. int
  291. smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0,
  292. char show_error)
  293. {
  294. int e = 0;
  295. __u8 args[4];
  296. args[0] = WIN_SMART;
  297. args[1] = val0;
  298. args[2] = smart_command[command].value;
  299. args[3] = 0;
  300. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  301. e = errno;
  302. if (show_error) {
  303. printf (_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror (errno));
  304. }
  305. }
  306. return e;
  307. }
  308. int
  309. smart_read_thresholds (int fd, thresholds_t * thresholds)
  310. {
  311. int e;
  312. __u8 args[4 + 512];
  313. args[0] = WIN_SMART;
  314. args[1] = 0;
  315. args[2] = SMART_READ_THRESHOLDS;
  316. args[3] = 1;
  317. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  318. e = errno;
  319. printf (_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror (errno));
  320. return e;
  321. }
  322. memcpy (thresholds, args + 4, 512);
  323. return 0;
  324. }
  325. void
  326. show_version ()
  327. {
  328. printf ("check_ide-smart v.1 - FREE Software with NO WARRANTY\n");
  329. printf ("Nagios feature - Robert Dale <rdale@digital-mission.com>\n");
  330. printf ("(C) 1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>\n");
  331. }
  332. void
  333. show_help ()
  334. {
  335. printf (_("\
  336. Usage: check_ide-smart [DEVICE] [OPTION]\n\
  337. -d, --device=DEVICE\n\
  338. Select device DEVICE\n\
  339. -i, --immediate\n\
  340. Perform immediately offline tests\n\
  341. -q, --quiet-check\n\
  342. Returns the number of failed tests\n\
  343. -1, --auto-on\n\
  344. Turn on automatic offline tests\n\
  345. -0, --auto-off\n\
  346. Turn off automatic offline tests\n\
  347. -n, --net-saint\n\
  348. Output suitable for Net Saint\n\
  349. -h, --help\n\
  350. -V, --version\n"));
  351. }
  352. int
  353. main (int argc, char *argv[])
  354. {
  355. char *device = NULL;
  356. int command = -1;
  357. int o, longindex;
  358. int retval = 0;
  359. thresholds_t thresholds;
  360. values_t values;
  361. int fd;
  362. static struct option longopts[] = {
  363. {"device", required_argument, 0, 'd'},
  364. {"immediate", no_argument, 0, 'i'},
  365. {"quiet-check", no_argument, 0, 'q'},
  366. {"auto-on", no_argument, 0, '1'},
  367. {"auto-off", no_argument, 0, '0'},
  368. {"net-saint", no_argument, 0, 'n'},
  369. {"help", no_argument, 0, 'h'},
  370. {"version", no_argument, 0, 'V'}, {0, 0, 0, 0}
  371. };
  372. setlocale (LC_ALL, "");
  373. bindtextdomain (PACKAGE, LOCALEDIR);
  374. textdomain (PACKAGE);
  375. while (1) {
  376. o = getopt_long (argc, argv, "+d:iq10nhV", longopts, &longindex);
  377. if (o == -1 || o == EOF)
  378. break;
  379. switch (o) {
  380. case 'd':
  381. device = optarg;
  382. break;
  383. case 'q':
  384. command = 3;
  385. break;
  386. case 'i':
  387. command = 2;
  388. break;
  389. case '1':
  390. command = 1;
  391. break;
  392. case '0':
  393. command = 0;
  394. break;
  395. case 'n':
  396. command = 4;
  397. break;
  398. case 'h':
  399. show_help ();
  400. return 0;
  401. case 'V':
  402. show_version ();
  403. return 0;
  404. default:
  405. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  406. print_usage ();
  407. exit (STATE_UNKNOWN);
  408. }
  409. if (optind < argc) {
  410. device = argv[optind];
  411. }
  412. if (!device) {
  413. show_help ();
  414. show_version ();
  415. return -1;
  416. }
  417. fd = open (device, O_RDONLY);
  418. if (fd < 0) {
  419. printf (_("CRITICAL - Couldn't open device: %s\n"), strerror (errno));
  420. return 2;
  421. }
  422. if (smart_cmd_simple (fd, SMART_CMD_ENABLE, 0, TRUE)) {
  423. printf (_("CRITICAL - SMART_CMD_ENABLE\n"));
  424. return 2;
  425. }
  426. switch (command) {
  427. case 0:
  428. retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0, TRUE);
  429. break;
  430. case 1:
  431. retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0xF8, TRUE);
  432. break;
  433. case 2:
  434. retval = smart_cmd_simple (fd, SMART_CMD_IMMEDIATE_OFFLINE, 0, TRUE);
  435. break;
  436. case 3:
  437. smart_read_values (fd, &values);
  438. smart_read_thresholds (fd, &thresholds);
  439. retval = values_not_passed (&values, &thresholds);
  440. break;
  441. case 4:
  442. smart_read_values (fd, &values);
  443. smart_read_thresholds (fd, &thresholds);
  444. retval = net_saint (&values, &thresholds);
  445. break;
  446. default:
  447. smart_read_values (fd, &values);
  448. smart_read_thresholds (fd, &thresholds);
  449. print_values (&values, &thresholds);
  450. break;
  451. }
  452. close (fd);
  453. }
  454. return retval;
  455. }