check_ide_smart.c 11 KB

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