check_ide_smart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. int
  132. main (int argc, char *argv[])
  133. {
  134. char *device = NULL;
  135. int command = -1;
  136. int o, longindex;
  137. int retval = 0;
  138. thresholds_t thresholds;
  139. values_t values;
  140. int fd;
  141. static struct option longopts[] = {
  142. {"device", required_argument, 0, 'd'},
  143. {"immediate", no_argument, 0, 'i'},
  144. {"quiet-check", no_argument, 0, 'q'},
  145. {"auto-on", no_argument, 0, '1'},
  146. {"auto-off", no_argument, 0, '0'},
  147. {"net-saint", no_argument, 0, 'n'},
  148. {"help", no_argument, 0, 'h'},
  149. {"version", no_argument, 0, 'V'}, {0, 0, 0, 0}
  150. };
  151. setlocale (LC_ALL, "");
  152. bindtextdomain (PACKAGE, LOCALEDIR);
  153. textdomain (PACKAGE);
  154. while (1) {
  155. o = getopt_long (argc, argv, "+d:iq10nhV", longopts, &longindex);
  156. if (o == -1 || o == EOF)
  157. break;
  158. switch (o) {
  159. case 'd':
  160. device = optarg;
  161. break;
  162. case 'q':
  163. command = 3;
  164. break;
  165. case 'i':
  166. command = 2;
  167. break;
  168. case '1':
  169. command = 1;
  170. break;
  171. case '0':
  172. command = 0;
  173. break;
  174. case 'n':
  175. command = 4;
  176. break;
  177. case 'h':
  178. print_help ();
  179. return STATE_OK;
  180. case 'V':
  181. print_revision (progname, revision);
  182. return STATE_OK;
  183. default:
  184. usage2 (_("Unknown argument"), optarg);
  185. }
  186. if (optind < argc) {
  187. device = argv[optind];
  188. }
  189. if (!device) {
  190. show_help ();
  191. show_version ();
  192. return -1;
  193. }
  194. fd = open (device, O_RDONLY);
  195. if (fd < 0) {
  196. printf (_("CRITICAL - Couldn't open device: %s\n"), strerror (errno));
  197. return 2;
  198. }
  199. if (smart_cmd_simple (fd, SMART_CMD_ENABLE, 0, TRUE)) {
  200. printf (_("CRITICAL - SMART_CMD_ENABLE\n"));
  201. return 2;
  202. }
  203. switch (command) {
  204. case 0:
  205. retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0, TRUE);
  206. break;
  207. case 1:
  208. retval = smart_cmd_simple (fd, SMART_CMD_AUTO_OFFLINE, 0xF8, TRUE);
  209. break;
  210. case 2:
  211. retval = smart_cmd_simple (fd, SMART_CMD_IMMEDIATE_OFFLINE, 0, TRUE);
  212. break;
  213. case 3:
  214. smart_read_values (fd, &values);
  215. smart_read_thresholds (fd, &thresholds);
  216. retval = values_not_passed (&values, &thresholds);
  217. break;
  218. case 4:
  219. smart_read_values (fd, &values);
  220. smart_read_thresholds (fd, &thresholds);
  221. retval = net_saint (&values, &thresholds);
  222. break;
  223. default:
  224. smart_read_values (fd, &values);
  225. smart_read_thresholds (fd, &thresholds);
  226. print_values (&values, &thresholds);
  227. break;
  228. }
  229. close (fd);
  230. }
  231. return retval;
  232. }
  233. char *
  234. get_offline_text (int status)
  235. {
  236. int i;
  237. for (i = 0; offline_status_text[i].text; i++) {
  238. if (offline_status_text[i].value == status) {
  239. return offline_status_text[i].text;
  240. }
  241. }
  242. return "UNKNOW";
  243. }
  244. int
  245. smart_read_values (int fd, values_t * values)
  246. {
  247. int e;
  248. __u8 args[4 + 512];
  249. args[0] = WIN_SMART;
  250. args[1] = 0;
  251. args[2] = SMART_READ_VALUES;
  252. args[3] = 1;
  253. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  254. e = errno;
  255. printf (_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror (errno));
  256. return e;
  257. }
  258. memcpy (values, args + 4, 512);
  259. return 0;
  260. }
  261. int
  262. values_not_passed (values_t * p, thresholds_t * t)
  263. {
  264. value_t * value = p->values;
  265. threshold_t * threshold = t->thresholds;
  266. int failed = 0;
  267. int passed = 0;
  268. int i;
  269. for (i = 0; i < NR_ATTRIBUTES; i++) {
  270. if (value->id && threshold->id && value->id == threshold->id) {
  271. if (value->value <= threshold->threshold) {
  272. ++failed;
  273. }
  274. else {
  275. ++passed;
  276. }
  277. }
  278. ++value;
  279. ++threshold;
  280. }
  281. return (passed ? -failed : 2);
  282. }
  283. int
  284. net_saint (values_t * p, thresholds_t * t)
  285. {
  286. value_t * value = p->values;
  287. threshold_t * threshold = t->thresholds;
  288. int status = OPERATIONAL;
  289. int prefailure = 0;
  290. int advisory = 0;
  291. int failed = 0;
  292. int passed = 0;
  293. int total = 0;
  294. int i;
  295. for (i = 0; i < NR_ATTRIBUTES; i++) {
  296. if (value->id && threshold->id && value->id == threshold->id) {
  297. if (value->value <= threshold->threshold) {
  298. ++failed;
  299. if (value->status & 1) {
  300. status = PREFAILURE;
  301. ++prefailure;
  302. }
  303. else {
  304. status = ADVISORY;
  305. ++advisory;
  306. }
  307. }
  308. else {
  309. ++passed;
  310. }
  311. ++total;
  312. }
  313. ++value;
  314. ++threshold;
  315. }
  316. switch (status) {
  317. case PREFAILURE:
  318. printf (_("CRITICAL - %d Harddrive PreFailure%cDetected! %d/%d tests failed.\n"),
  319. prefailure,
  320. prefailure > 1 ? 's' : ' ',
  321. failed,
  322. total);
  323. break;
  324. case ADVISORY:
  325. printf (_("WARNING - %d Harddrive Advisor%s Detected. %d/%d tests failed.\n"),
  326. advisory,
  327. advisory > 1 ? "ies" : "y",
  328. failed,
  329. total);
  330. break;
  331. case OPERATIONAL:
  332. printf (_("OK - Operational (%d/%d tests passed)\n"), passed, total);
  333. break;
  334. default:
  335. printf (_("ERROR - Status '%d' uknown. %d/%d tests passed\n"), status,
  336. passed, total);
  337. status = -1;
  338. break;
  339. }
  340. return status;
  341. }
  342. void
  343. print_value (value_t * p, threshold_t * t)
  344. {
  345. printf ("Id=%3d, Status=%2d {%s , %s}, Value=%3d, Threshold=%3d, %s\n",
  346. p->id, p->status, p->status & 1 ? "PreFailure" : "Advisory ",
  347. p->status & 2 ? "OnLine " : "OffLine", p->value, t->threshold,
  348. p->value > t->threshold ? "Passed" : "Failed");
  349. }
  350. void
  351. print_values (values_t * p, thresholds_t * t)
  352. {
  353. value_t * value = p->values;
  354. threshold_t * threshold = t->thresholds;
  355. int i;
  356. for (i = 0; i < NR_ATTRIBUTES; i++) {
  357. if (value->id && threshold->id && value->id == threshold->id) {
  358. print_value (value++, threshold++);
  359. }
  360. }
  361. printf
  362. (_("OffLineStatus=%d {%s}, AutoOffLine=%s, OffLineTimeout=%d minutes\n"),
  363. p->offline_status,
  364. get_offline_text (p->offline_status & 0x7f),
  365. (p->offline_status & 0x80 ? "Yes" : "No"),
  366. p->offline_timeout / 60);
  367. printf
  368. (_("OffLineCapability=%d {%s %s %s}\n"),
  369. p->offline_capability,
  370. p->offline_capability & 1 ? "Immediate" : "",
  371. p->offline_capability & 2 ? "Auto" : "",
  372. p->offline_capability & 4 ? "AbortOnCmd" : "SuspendOnCmd");
  373. printf
  374. (_("SmartRevision=%d, CheckSum=%d, SmartCapability=%d {%s %s}\n"),
  375. p->revision,
  376. p->checksum,
  377. p->smart_capability,
  378. p->smart_capability & 1 ? "SaveOnStandBy" : "",
  379. p->smart_capability & 2 ? "AutoSave" : "");
  380. }
  381. void
  382. print_thresholds (thresholds_t * p)
  383. {
  384. threshold_t * threshold = p->thresholds;
  385. int i;
  386. printf ("\n");
  387. printf ("SmartRevision=%d\n", p->revision);
  388. for (i = 0; i < NR_ATTRIBUTES; i++) {
  389. if (threshold->id) {
  390. printf ("Id=%3d, Threshold=%3d\n", threshold->id,
  391. threshold->threshold); }
  392. ++threshold;
  393. }
  394. printf ("CheckSum=%d\n", p->checksum);
  395. }
  396. int
  397. smart_cmd_simple (int fd, enum SmartCommand command, __u8 val0,
  398. char show_error)
  399. {
  400. int e = 0;
  401. __u8 args[4];
  402. args[0] = WIN_SMART;
  403. args[1] = val0;
  404. args[2] = smart_command[command].value;
  405. args[3] = 0;
  406. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  407. e = errno;
  408. if (show_error) {
  409. printf (_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror (errno));
  410. }
  411. }
  412. return e;
  413. }
  414. int
  415. smart_read_thresholds (int fd, thresholds_t * thresholds)
  416. {
  417. int e;
  418. __u8 args[4 + 512];
  419. args[0] = WIN_SMART;
  420. args[1] = 0;
  421. args[2] = SMART_READ_THRESHOLDS;
  422. args[3] = 1;
  423. if (ioctl (fd, HDIO_DRIVE_CMD, &args)) {
  424. e = errno;
  425. printf (_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror (errno));
  426. return e;
  427. }
  428. memcpy (thresholds, args + 4, 512);
  429. return 0;
  430. }
  431. void
  432. print_help ()
  433. {
  434. print_revision (progname, revision);
  435. printf ("Nagios feature - 1999 Robert Dale <rdale@digital-mission.com>\n");
  436. printf ("(C) 1999 Ragnar Hojland Espinosa <ragnar@lightside.dhis.org>\n");
  437. printf (COPYRIGHT, copyright, email);
  438. printf ("\
  439. Usage: %s [DEVICE] [OPTION]\n\
  440. -d, --device=DEVICE\n\
  441. Select device DEVICE\n\
  442. -i, --immediate\n\
  443. Perform immediately offline tests\n\
  444. -q, --quiet-check\n\
  445. Returns the number of failed tests\n\
  446. -1, --auto-on\n\
  447. Turn on automatic offline tests\n\
  448. -0, --auto-off\n\
  449. Turn off automatic offline tests\n\
  450. -n, --net-saint\n\
  451. Output suitable for Net Saint\n", progname);
  452. }
  453. void
  454. print_usage (void)
  455. {
  456. printf ("Usage: %s \n"), progname);
  457. }