check_ide-smart.c 11 KB

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