summaryrefslogtreecommitdiffstats
path: root/plugins/check_ide_smart.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_ide_smart.c')
-rw-r--r--plugins/check_ide_smart.c134
1 files changed, 70 insertions, 64 deletions
diff --git a/plugins/check_ide_smart.c b/plugins/check_ide_smart.c
index dd88e124..c1325cf9 100644
--- a/plugins/check_ide_smart.c
+++ b/plugins/check_ide_smart.c
@@ -80,30 +80,30 @@ void print_usage(void);
80#define OPERATIONAL 0 80#define OPERATIONAL 0
81#define UNKNOWN -1 81#define UNKNOWN -1
82 82
83typedef struct threshold_s { 83typedef struct {
84 uint8_t id; 84 uint8_t id;
85 uint8_t threshold; 85 uint8_t threshold;
86 uint8_t reserved[10]; 86 uint8_t reserved[10];
87} __attribute__((packed)) threshold_t; 87} __attribute__((packed)) smart_threshold;
88 88
89typedef struct thresholds_s { 89typedef struct {
90 uint16_t revision; 90 uint16_t revision;
91 threshold_t thresholds[NR_ATTRIBUTES]; 91 smart_threshold thresholds[NR_ATTRIBUTES];
92 uint8_t reserved[18]; 92 uint8_t reserved[18];
93 uint8_t vendor[131]; 93 uint8_t vendor[131];
94 uint8_t checksum; 94 uint8_t checksum;
95} __attribute__((packed)) thresholds_t; 95} __attribute__((packed)) smart_thresholds;
96 96
97typedef struct value_s { 97typedef struct {
98 uint8_t id; 98 uint8_t id;
99 uint16_t status; 99 uint16_t status;
100 uint8_t value; 100 uint8_t value;
101 uint8_t vendor[8]; 101 uint8_t vendor[8];
102} __attribute__((packed)) value_t; 102} __attribute__((packed)) smart_value;
103 103
104typedef struct values_s { 104typedef struct {
105 uint16_t revision; 105 uint16_t revision;
106 value_t values[NR_ATTRIBUTES]; 106 smart_value values[NR_ATTRIBUTES];
107 uint8_t offline_status; 107 uint8_t offline_status;
108 uint8_t vendor1; 108 uint8_t vendor1;
109 uint16_t offline_timeout; 109 uint16_t offline_timeout;
@@ -113,7 +113,7 @@ typedef struct values_s {
113 uint8_t reserved[16]; 113 uint8_t reserved[16];
114 uint8_t vendor[125]; 114 uint8_t vendor[125];
115 uint8_t checksum; 115 uint8_t checksum;
116} __attribute__((packed)) values_t; 116} __attribute__((packed)) smart_values;
117 117
118static struct { 118static struct {
119 uint8_t value; 119 uint8_t value;
@@ -137,12 +137,12 @@ enum SmartCommand {
137}; 137};
138 138
139static char *get_offline_text(int /*status*/); 139static char *get_offline_text(int /*status*/);
140static int smart_read_values(int /*fd*/, values_t * /*values*/); 140static int smart_read_values(int /*fd*/, smart_values * /*values*/);
141static mp_state_enum compare_values_and_thresholds(values_t * /*p*/, thresholds_t * /*t*/); 141static mp_state_enum compare_values_and_thresholds(smart_values * /*p*/, smart_thresholds * /*t*/);
142static void print_value(value_t * /*p*/, threshold_t * /*t*/); 142static void print_value(smart_value * /*p*/, smart_threshold * /*t*/);
143static void print_values(values_t * /*p*/, thresholds_t * /*t*/); 143static void print_values(smart_values * /*p*/, smart_thresholds * /*t*/);
144static mp_state_enum smart_cmd_simple(int /*fd*/, enum SmartCommand /*command*/, uint8_t /*val0*/, bool /*show_error*/); 144static mp_state_enum smart_cmd_simple(int /*fd*/, enum SmartCommand /*command*/, uint8_t /*val0*/, bool /*show_error*/);
145static int smart_read_thresholds(int /*fd*/, thresholds_t * /*thresholds*/); 145static int smart_read_thresholds(int /*fd*/, smart_thresholds * /*thresholds*/);
146static int verbose = 0; 146static int verbose = 0;
147 147
148typedef struct { 148typedef struct {
@@ -246,9 +246,9 @@ int main(int argc, char *argv[]) {
246 return STATE_CRITICAL; 246 return STATE_CRITICAL;
247 } 247 }
248 248
249 values_t values; 249 smart_values values;
250 smart_read_values(device_file_descriptor, &values); 250 smart_read_values(device_file_descriptor, &values);
251 thresholds_t thresholds; 251 smart_thresholds thresholds;
252 smart_read_thresholds(device_file_descriptor, &thresholds); 252 smart_read_thresholds(device_file_descriptor, &thresholds);
253 mp_state_enum retval = compare_values_and_thresholds(&values, &thresholds); 253 mp_state_enum retval = compare_values_and_thresholds(&values, &thresholds);
254 if (verbose) { 254 if (verbose) {
@@ -268,22 +268,20 @@ char *get_offline_text(int status) {
268 return "UNKNOWN"; 268 return "UNKNOWN";
269} 269}
270 270
271int smart_read_values(int fd, values_t *values) { 271int smart_read_values(int file_descriptor, smart_values *values) {
272#ifdef __linux__ 272#ifdef __linux__
273 int errno_storage;
274 uint8_t args[4 + 512]; 273 uint8_t args[4 + 512];
275 args[0] = WIN_SMART; 274 args[0] = WIN_SMART;
276 args[1] = 0; 275 args[1] = 0;
277 args[2] = SMART_READ_VALUES; 276 args[2] = SMART_READ_VALUES;
278 args[3] = 1; 277 args[3] = 1;
279 if (ioctl(fd, HDIO_DRIVE_CMD, &args)) { 278 if (ioctl(file_descriptor, HDIO_DRIVE_CMD, &args)) {
280 errno_storage = errno; 279 int errno_storage = errno;
281 printf(_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror(errno)); 280 printf(_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror(errno));
282 return errno_storage; 281 return errno_storage;
283 } 282 }
284 memcpy(values, args + 4, 512); 283 memcpy(values, args + 4, 512);
285#endif /* __linux__ */ 284#elif defined __NetBSD__
286#ifdef __NetBSD__
287 struct atareq req; 285 struct atareq req;
288 unsigned char inbuf[DEV_BSIZE]; 286 unsigned char inbuf[DEV_BSIZE];
289 287
@@ -298,26 +296,30 @@ int smart_read_values(int fd, values_t *values) {
298 req.datalen = sizeof(inbuf); 296 req.datalen = sizeof(inbuf);
299 req.cylinder = WDSMART_CYL; 297 req.cylinder = WDSMART_CYL;
300 298
301 if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) { 299 if (ioctl(file_descriptor, ATAIOCCOMMAND, &req) == 0) {
302 if (req.retsts != ATACMD_OK) { 300 if (req.retsts != ATACMD_OK) {
303 errno = ENODEV; 301 errno = ENODEV;
304 } 302 }
305 } 303 }
306 304
307 if (errno != 0) { 305 if (errno != 0) {
308 int e = errno; 306 int errno_storage = errno;
309 printf(_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror(errno)); 307 printf(_("CRITICAL - SMART_READ_VALUES: %s\n"), strerror(errno));
310 return e; 308 return errno_storage;
311 } 309 }
312 310
313 (void)memcpy(values, inbuf, 512); 311 (void)memcpy(values, inbuf, 512);
314#endif /* __NetBSD__ */ 312#else // __linux__ || __NetBSD__
313# error Not implemented for this OS
314#endif
315
315 return 0; 316 return 0;
316} 317}
317 318
318mp_state_enum compare_values_and_thresholds(values_t *p, thresholds_t *t) { 319mp_state_enum compare_values_and_thresholds(smart_values *values, smart_thresholds *thresholds) {
319 value_t *value = p->values; 320 smart_value *value = values->values;
320 threshold_t *threshold = t->thresholds; 321 smart_threshold *threshold = thresholds->thresholds;
322
321 int status = OPERATIONAL; 323 int status = OPERATIONAL;
322 int prefailure = 0; 324 int prefailure = 0;
323 int advisory = 0; 325 int advisory = 0;
@@ -366,37 +368,39 @@ mp_state_enum compare_values_and_thresholds(values_t *p, thresholds_t *t) {
366 return status; 368 return status;
367} 369}
368 370
369void print_value(value_t *p, threshold_t *t) { 371void print_value(smart_value *value_pointer, smart_threshold *threshold_pointer) {
370 printf("Id=%3d, Status=%2d {%s , %s}, Value=%3d, Threshold=%3d, %s\n", p->id, p->status, p->status & 1 ? "PreFailure" : "Advisory ", 372 printf("Id=%3d, Status=%2d {%s , %s}, Value=%3d, Threshold=%3d, %s\n", value_pointer->id, value_pointer->status,
371 p->status & 2 ? "OnLine " : "OffLine", p->value, t->threshold, p->value >= t->threshold ? "Passed" : "Failed"); 373 value_pointer->status & 1 ? "PreFailure" : "Advisory ", value_pointer->status & 2 ? "OnLine " : "OffLine",
374 value_pointer->value, threshold_pointer->threshold, value_pointer->value >= threshold_pointer->threshold ? "Passed" : "Failed");
372} 375}
373 376
374void print_values(values_t *p, thresholds_t *t) { 377void print_values(smart_values *values, smart_thresholds *thresholds) {
375 value_t *value = p->values; 378 smart_value *value = values->values;
376 threshold_t *threshold = t->thresholds; 379 smart_threshold *threshold = thresholds->thresholds;
377 int i; 380 for (int i = 0; i < NR_ATTRIBUTES; i++) {
378 for (i = 0; i < NR_ATTRIBUTES; i++) {
379 if (value->id && threshold->id && value->id == threshold->id) { 381 if (value->id && threshold->id && value->id == threshold->id) {
380 print_value(value++, threshold++); 382 print_value(value++, threshold++);
381 } 383 }
382 } 384 }
383 printf(_("OffLineStatus=%d {%s}, AutoOffLine=%s, OffLineTimeout=%d minutes\n"), p->offline_status, 385 printf(_("OffLineStatus=%d {%s}, AutoOffLine=%s, OffLineTimeout=%d minutes\n"), values->offline_status,
384 get_offline_text(p->offline_status & 0x7f), (p->offline_status & 0x80 ? "Yes" : "No"), p->offline_timeout / 60); 386 get_offline_text(values->offline_status & 0x7f), (values->offline_status & 0x80 ? "Yes" : "No"), values->offline_timeout / 60);
385 printf(_("OffLineCapability=%d {%s %s %s}\n"), p->offline_capability, p->offline_capability & 1 ? "Immediate" : "", 387 printf(_("OffLineCapability=%d {%s %s %s}\n"), values->offline_capability, values->offline_capability & 1 ? "Immediate" : "",
386 p->offline_capability & 2 ? "Auto" : "", p->offline_capability & 4 ? "AbortOnCmd" : "SuspendOnCmd"); 388 values->offline_capability & 2 ? "Auto" : "", values->offline_capability & 4 ? "AbortOnCmd" : "SuspendOnCmd");
387 printf(_("SmartRevision=%d, CheckSum=%d, SmartCapability=%d {%s %s}\n"), p->revision, p->checksum, p->smart_capability, 389 printf(_("SmartRevision=%d, CheckSum=%d, SmartCapability=%d {%s %s}\n"), values->revision, values->checksum, values->smart_capability,
388 p->smart_capability & 1 ? "SaveOnStandBy" : "", p->smart_capability & 2 ? "AutoSave" : ""); 390 values->smart_capability & 1 ? "SaveOnStandBy" : "", values->smart_capability & 2 ? "AutoSave" : "");
389} 391}
390 392
391mp_state_enum smart_cmd_simple(int fd, enum SmartCommand command, uint8_t val0, bool show_error) { 393mp_state_enum smart_cmd_simple(int file_descriptor, enum SmartCommand command, uint8_t val0, bool show_error) {
392 mp_state_enum result = STATE_UNKNOWN; 394 mp_state_enum result = STATE_UNKNOWN;
393#ifdef __linux__ 395#ifdef __linux__
394 uint8_t args[4]; 396 uint8_t args[4] = {
395 args[0] = WIN_SMART; 397 WIN_SMART,
396 args[1] = val0; 398 val0,
397 args[2] = smart_command[command].value; 399 smart_command[command].value,
398 args[3] = 0; 400 0,
399 if (ioctl(fd, HDIO_DRIVE_CMD, &args)) { 401 };
402
403 if (ioctl(file_descriptor, HDIO_DRIVE_CMD, &args)) {
400 result = STATE_CRITICAL; 404 result = STATE_CRITICAL;
401 if (show_error) { 405 if (show_error) {
402 printf(_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror(errno)); 406 printf(_("CRITICAL - %s: %s\n"), smart_command[command].text, strerror(errno));
@@ -408,8 +412,7 @@ mp_state_enum smart_cmd_simple(int fd, enum SmartCommand command, uint8_t val0,
408 } 412 }
409 } 413 }
410 414
411#endif /* __linux__ */ 415#elif defined __NetBSD__
412#ifdef __NetBSD__
413 struct atareq req; 416 struct atareq req;
414 417
415 memset(&req, 0, sizeof(req)); 418 memset(&req, 0, sizeof(req));
@@ -420,7 +423,7 @@ mp_state_enum smart_cmd_simple(int fd, enum SmartCommand command, uint8_t val0,
420 req.cylinder = WDSMART_CYL; 423 req.cylinder = WDSMART_CYL;
421 req.sec_count = val0; 424 req.sec_count = val0;
422 425
423 if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) { 426 if (ioctl(file_descriptor, ATAIOCCOMMAND, &req) == 0) {
424 if (req.retsts != ATACMD_OK) { 427 if (req.retsts != ATACMD_OK) {
425 errno = ENODEV; 428 errno = ENODEV;
426 } 429 }
@@ -440,32 +443,32 @@ mp_state_enum smart_cmd_simple(int fd, enum SmartCommand command, uint8_t val0,
440 printf(_("OK - Command sent (%s)\n"), smart_command[command].text); 443 printf(_("OK - Command sent (%s)\n"), smart_command[command].text);
441 } 444 }
442 } 445 }
443 446#else
447# error Not implemented for this OS
444#endif /* __NetBSD__ */ 448#endif /* __NetBSD__ */
449
445 return result; 450 return result;
446} 451}
447 452
448int smart_read_thresholds(int fd, thresholds_t *thresholds) { 453int smart_read_thresholds(int file_descriptor, smart_thresholds *thresholds) {
449#ifdef __linux__ 454#ifdef __linux__
450 int errno_storage;
451 uint8_t args[4 + 512]; 455 uint8_t args[4 + 512];
452 args[0] = WIN_SMART; 456 args[0] = WIN_SMART;
453 args[1] = 0; 457 args[1] = 0;
454 args[2] = SMART_READ_THRESHOLDS; 458 args[2] = SMART_READ_THRESHOLDS;
455 args[3] = 1; 459 args[3] = 1;
456 if (ioctl(fd, HDIO_DRIVE_CMD, &args)) { 460 if (ioctl(file_descriptor, HDIO_DRIVE_CMD, &args)) {
457 errno_storage = errno; 461 int errno_storage = errno;
458 printf(_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror(errno)); 462 printf(_("CRITICAL - SMART_READ_THRESHOLDS: %s\n"), strerror(errno));
459 return errno_storage; 463 return errno_storage;
460 } 464 }
461 memcpy(thresholds, args + 4, 512); 465 memcpy(thresholds, args + 4, 512);
462#endif /* __linux__ */ 466#elif defined __NetBSD__
463#ifdef __NetBSD__
464 struct atareq req; 467 struct atareq req;
465 unsigned char inbuf[DEV_BSIZE];
466
467 memset(&req, 0, sizeof(req)); 468 memset(&req, 0, sizeof(req));
468 req.timeout = 1000; 469 req.timeout = 1000;
470
471 unsigned char inbuf[DEV_BSIZE];
469 memset(&inbuf, 0, sizeof(inbuf)); 472 memset(&inbuf, 0, sizeof(inbuf));
470 473
471 req.flags = ATACMD_READ; 474 req.flags = ATACMD_READ;
@@ -475,7 +478,7 @@ int smart_read_thresholds(int fd, thresholds_t *thresholds) {
475 req.datalen = sizeof(inbuf); 478 req.datalen = sizeof(inbuf);
476 req.cylinder = WDSMART_CYL; 479 req.cylinder = WDSMART_CYL;
477 480
478 if (ioctl(fd, ATAIOCCOMMAND, &req) == 0) { 481 if (ioctl(file_descriptor, ATAIOCCOMMAND, &req) == 0) {
479 if (req.retsts != ATACMD_OK) { 482 if (req.retsts != ATACMD_OK) {
480 errno = ENODEV; 483 errno = ENODEV;
481 } 484 }
@@ -488,7 +491,10 @@ int smart_read_thresholds(int fd, thresholds_t *thresholds) {
488 } 491 }
489 492
490 (void)memcpy(thresholds, inbuf, 512); 493 (void)memcpy(thresholds, inbuf, 512);
494#else
495# error Not implemented for this OS
491#endif /* __NetBSD__ */ 496#endif /* __NetBSD__ */
497
492 return 0; 498 return 0;
493} 499}
494 500