diff options
author | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2023-12-19 11:18:51 +0100 |
---|---|---|
committer | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2023-12-20 10:02:47 +0100 |
commit | 2dec5182c508bd3cc286b4649836ead51aec50ef (patch) | |
tree | 682dbf85ed7452f3101a200d925991c281b7d465 /plugins/check_swap.c | |
parent | 2289d094ae00a52f96a038cb7ca8bb350aec483a (diff) | |
download | monitoring-plugins-2dec5182c508bd3cc286b4649836ead51aec50ef.tar.gz |
check_swap: refactor to improve readability
Diffstat (limited to 'plugins/check_swap.c')
-rw-r--r-- | plugins/check_swap.c | 380 |
1 files changed, 4 insertions, 376 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c index ac74931a..f34b0514 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c | |||
@@ -31,9 +31,6 @@ const char *progname = "check_swap"; | |||
31 | const char *copyright = "2000-2023"; | 31 | const char *copyright = "2000-2023"; |
32 | const char *email = "devel@monitoring-plugins.org"; | 32 | const char *email = "devel@monitoring-plugins.org"; |
33 | 33 | ||
34 | #include "common.h" | ||
35 | #include "popen.h" | ||
36 | #include "utils.h" | ||
37 | 34 | ||
38 | #ifdef HAVE_DECL_SWAPCTL | 35 | #ifdef HAVE_DECL_SWAPCTL |
39 | #ifdef HAVE_SYS_PARAM_H | 36 | #ifdef HAVE_SYS_PARAM_H |
@@ -47,34 +44,8 @@ const char *email = "devel@monitoring-plugins.org"; | |||
47 | #endif | 44 | #endif |
48 | #endif | 45 | #endif |
49 | 46 | ||
50 | #ifndef SWAP_CONVERSION | 47 | #include "./check_swap.d/check_swap.h" |
51 | #define SWAP_CONVERSION 1 | 48 | #include "./utils.h" |
52 | #endif | ||
53 | |||
54 | typedef struct { | ||
55 | bool is_percentage; | ||
56 | uint64_t value; | ||
57 | } threshold; | ||
58 | |||
59 | typedef struct { | ||
60 | unsigned long long free; // Free swap in Bytes! | ||
61 | unsigned long long used; // Used swap in Bytes! | ||
62 | unsigned long long total; // Total swap size, you guessed it, in Bytes! | ||
63 | } swap_metrics; | ||
64 | |||
65 | typedef struct { | ||
66 | int errorcode; | ||
67 | int statusCode; | ||
68 | swap_metrics metrics; | ||
69 | } swap_result; | ||
70 | |||
71 | typedef struct { | ||
72 | int verbose; | ||
73 | bool allswaps; | ||
74 | int no_swap_state; | ||
75 | threshold warn; | ||
76 | threshold crit; | ||
77 | } swap_config; | ||
78 | 49 | ||
79 | typedef struct { | 50 | typedef struct { |
80 | int errorcode; | 51 | int errorcode; |
@@ -86,19 +57,6 @@ swap_config_wrapper process_arguments(swap_config_wrapper config, int argc, | |||
86 | void print_usage(); | 57 | void print_usage(); |
87 | void print_help(swap_config); | 58 | void print_help(swap_config); |
88 | 59 | ||
89 | swap_result getSwapFromProcMeminfo(swap_config config); | ||
90 | swap_result getSwapFromSwapCommand(swap_config config); | ||
91 | swap_result getSwapFromSwapctl_BSD(swap_config config); | ||
92 | swap_result getSwapFromSwap_SRV4(swap_config config); | ||
93 | |||
94 | swap_config swap_config_init() { | ||
95 | swap_config tmp = {0}; | ||
96 | tmp.allswaps = false; | ||
97 | tmp.no_swap_state = STATE_CRITICAL; | ||
98 | tmp.verbose = 0; | ||
99 | |||
100 | return tmp; | ||
101 | } | ||
102 | 60 | ||
103 | int main(int argc, char **argv) { | 61 | int main(int argc, char **argv) { |
104 | setlocale(LC_ALL, ""); | 62 | setlocale(LC_ALL, ""); |
@@ -123,23 +81,7 @@ int main(int argc, char **argv) { | |||
123 | 81 | ||
124 | swap_config config = tmp.config; | 82 | swap_config config = tmp.config; |
125 | 83 | ||
126 | #ifdef HAVE_PROC_MEMINFO | 84 | swap_result data = get_swap_data(config); |
127 | swap_result data = getSwapFromProcMeminfo(config); | ||
128 | #else | ||
129 | #ifdef HAVE_SWAP | ||
130 | swap_result data = getSwapFromSwapCommand(); | ||
131 | #else | ||
132 | #ifdef CHECK_SWAP_SWAPCTL_SVR4 | ||
133 | swap_result data = getSwapFromSwapctl_SRV4(); | ||
134 | #else | ||
135 | #ifdef CHECK_SWAP_SWAPCTL_BSD | ||
136 | swap_result data = getSwapFromSwapctl_BSD(); | ||
137 | #else | ||
138 | #error No way found to retrieve swap | ||
139 | #endif /* CHECK_SWAP_SWAPCTL_BSD */ | ||
140 | #endif /* CHECK_SWAP_SWAPCTL_SVR4 */ | ||
141 | #endif /* HAVE_SWAP */ | ||
142 | #endif /* HAVE_PROC_MEMINFO */ | ||
143 | 85 | ||
144 | double percent_used; | 86 | double percent_used; |
145 | 87 | ||
@@ -323,7 +265,7 @@ swap_config_wrapper process_arguments(swap_config_wrapper conf_wrapper, | |||
323 | return conf_wrapper; | 265 | return conf_wrapper; |
324 | } else if ((conf_wrapper.config.warn.is_percentage == | 266 | } else if ((conf_wrapper.config.warn.is_percentage == |
325 | conf_wrapper.config.crit.is_percentage) && | 267 | conf_wrapper.config.crit.is_percentage) && |
326 | (conf_wrapper.config.warn.value >= | 268 | (conf_wrapper.config.warn.value < |
327 | conf_wrapper.config.crit.value)) { | 269 | conf_wrapper.config.crit.value)) { |
328 | /* This is NOT triggered if warn and crit are different units, e.g warn | 270 | /* This is NOT triggered if warn and crit are different units, e.g warn |
329 | * is percentage and crit is absolute. We cannot determine the condition | 271 | * is percentage and crit is absolute. We cannot determine the condition |
@@ -387,317 +329,3 @@ void print_usage() { | |||
387 | printf(" %s [-av] -w <percent_free>%% -c <percent_free>%%\n", progname); | 329 | printf(" %s [-av] -w <percent_free>%% -c <percent_free>%%\n", progname); |
388 | printf(" -w <bytes_free> -c <bytes_free> [-n <state>]\n"); | 330 | printf(" -w <bytes_free> -c <bytes_free> [-n <state>]\n"); |
389 | } | 331 | } |
390 | |||
391 | #ifdef HAVE_PROC_MEMINFO | ||
392 | swap_result getSwapFromProcMeminfo(swap_config config) { | ||
393 | |||
394 | if (config.verbose >= 3) { | ||
395 | printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO); | ||
396 | } | ||
397 | |||
398 | FILE *fp; | ||
399 | fp = fopen(PROC_MEMINFO, "r"); | ||
400 | |||
401 | swap_result result = {0}; | ||
402 | result.statusCode = STATE_OK; | ||
403 | |||
404 | uint64_t swap_total = 0, swap_used = 0, swap_free = 0; | ||
405 | |||
406 | char input_buffer[MAX_INPUT_BUFFER]; | ||
407 | char str[32]; | ||
408 | |||
409 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) { | ||
410 | uint64_t tmp_KB = 0; | ||
411 | |||
412 | /* | ||
413 | * The following sscanf call looks for a line looking like: "Swap: 123 | ||
414 | * 123 123" On which kind of system this format exists, I can not say, | ||
415 | * but I wanted to document this for people who are not adapt with | ||
416 | * sscanf anymore, like me | ||
417 | */ | ||
418 | if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", | ||
419 | &swap_total, &swap_used, &swap_free) == 3) { | ||
420 | |||
421 | result.metrics.total += swap_total; | ||
422 | result.metrics.used += swap_used; | ||
423 | result.metrics.free += swap_free; | ||
424 | |||
425 | /* | ||
426 | * The following sscanf call looks for lines looking like: | ||
427 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least | ||
428 | * on Debian Linux with a 5.* kernel | ||
429 | */ | ||
430 | } else if (sscanf(input_buffer, | ||
431 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " | ||
432 | "%*[k]%*[B]", | ||
433 | str, &tmp_KB)) { | ||
434 | if (config.verbose >= 3) { | ||
435 | printf("Got %s with %lu\n", str, tmp_KB); | ||
436 | } | ||
437 | /* I think this part is always in Kb, so convert to mb */ | ||
438 | if (strcmp("Total", str) == 0) { | ||
439 | swap_total = tmp_KB * 1024; | ||
440 | } else if (strcmp("Free", str) == 0) { | ||
441 | swap_free = swap_free + tmp_KB * 1024; | ||
442 | } else if (strcmp("Cached", str) == 0) { | ||
443 | swap_free = swap_free + tmp_KB * 1024; | ||
444 | } | ||
445 | } | ||
446 | } | ||
447 | |||
448 | fclose(fp); | ||
449 | |||
450 | result.metrics.total = swap_total; | ||
451 | result.metrics.used = swap_total - swap_free; | ||
452 | result.metrics.free = swap_free; | ||
453 | |||
454 | return result; | ||
455 | } | ||
456 | #endif | ||
457 | |||
458 | #ifdef HAVE_SWAP | ||
459 | swap_result getSwapFromSwapCommand() { | ||
460 | swap_result result = {0}; | ||
461 | |||
462 | char *temp_buffer; | ||
463 | char *swap_command; | ||
464 | char *swap_format; | ||
465 | int conv_factor = SWAP_CONVERSION; | ||
466 | |||
467 | xasprintf(&swap_command, "%s", SWAP_COMMAND); | ||
468 | xasprintf(&swap_format, "%s", SWAP_FORMAT); | ||
469 | |||
470 | /* These override the command used if a summary (and thus ! allswaps) is | ||
471 | * required */ | ||
472 | /* The summary flag returns more accurate information about swap usage on these | ||
473 | * OSes */ | ||
474 | #ifdef _AIX | ||
475 | if (!allswaps) { | ||
476 | xasprintf(&swap_command, "%s", "/usr/sbin/lsps -s"); | ||
477 | xasprintf(&swap_format, "%s", "%lu%*s %lu"); | ||
478 | conv_factor = 1; | ||
479 | } | ||
480 | #endif | ||
481 | |||
482 | if (verbose >= 2) | ||
483 | printf(_("Command: %s\n"), swap_command); | ||
484 | if (verbose >= 3) | ||
485 | printf(_("Format: %s\n"), swap_format); | ||
486 | |||
487 | child_process = spopen(swap_command); | ||
488 | if (child_process == NULL) { | ||
489 | printf(_("Could not open pipe: %s\n"), swap_command); | ||
490 | return STATE_UNKNOWN; | ||
491 | } | ||
492 | |||
493 | child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); | ||
494 | if (child_stderr == NULL) | ||
495 | printf(_("Could not open stderr for %s\n"), swap_command); | ||
496 | |||
497 | sprintf(str, "%s", ""); | ||
498 | /* read 1st line */ | ||
499 | fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); | ||
500 | if (strcmp(swap_format, "") == 0) { | ||
501 | temp_buffer = strtok(input_buffer, " \n"); | ||
502 | while (temp_buffer) { | ||
503 | if (strstr(temp_buffer, "blocks")) | ||
504 | sprintf(str, "%s %s", str, "%lu"); | ||
505 | else if (strstr(temp_buffer, "dskfree")) | ||
506 | sprintf(str, "%s %s", str, "%lu"); | ||
507 | else | ||
508 | sprintf(str, "%s %s", str, "%*s"); | ||
509 | temp_buffer = strtok(NULL, " \n"); | ||
510 | } | ||
511 | } | ||
512 | |||
513 | /* If different swap command is used for summary switch, need to read format | ||
514 | * differently */ | ||
515 | #ifdef _AIX | ||
516 | if (!allswaps) { | ||
517 | fgets(input_buffer, MAX_INPUT_BUFFER - 1, | ||
518 | child_process); /* Ignore first line */ | ||
519 | sscanf(input_buffer, swap_format, &total_swap_mb, &used_swap_mb); | ||
520 | free_swap_mb = total_swap_mb * (100 - used_swap_mb) / 100; | ||
521 | used_swap_mb = total_swap_mb - free_swap_mb; | ||
522 | if (verbose >= 3) | ||
523 | printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, | ||
524 | used_swap_mb, free_swap_mb); | ||
525 | } else { | ||
526 | #endif | ||
527 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
528 | sscanf(input_buffer, swap_format, &dsktotal_mb, &dskfree_mb); | ||
529 | |||
530 | dsktotal_mb = dsktotal_mb / conv_factor; | ||
531 | /* AIX lists percent used, so this converts to dskfree in MBs */ | ||
532 | #ifdef _AIX | ||
533 | dskfree_mb = dsktotal_mb * (100 - dskfree_mb) / 100; | ||
534 | #else | ||
535 | dskfree_mb = dskfree_mb / conv_factor; | ||
536 | #endif | ||
537 | if (verbose >= 3) | ||
538 | printf(_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb); | ||
539 | |||
540 | dskused_mb = dsktotal_mb - dskfree_mb; | ||
541 | total_swap_mb += dsktotal_mb; | ||
542 | used_swap_mb += dskused_mb; | ||
543 | free_swap_mb += dskfree_mb; | ||
544 | if (allswaps) { | ||
545 | percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); | ||
546 | result = max_state(result, check_swap(dskfree_mb, dsktotal_mb)); | ||
547 | if (verbose) | ||
548 | xasprintf(&status, "%s [%.0f (%d%%)]", status, dskfree_mb, | ||
549 | 100 - percent); | ||
550 | } | ||
551 | } | ||
552 | #ifdef _AIX | ||
553 | } | ||
554 | #endif | ||
555 | |||
556 | /* If we get anything on STDERR, at least set warning */ | ||
557 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) | ||
558 | result = max_state(result, STATE_WARNING); | ||
559 | |||
560 | /* close stderr */ | ||
561 | (void)fclose(child_stderr); | ||
562 | |||
563 | /* close the pipe */ | ||
564 | if (spclose(child_process)) | ||
565 | result = max_state(result, STATE_WARNING); | ||
566 | } | ||
567 | #endif // HAVE_SWAP | ||
568 | |||
569 | #ifdef CHECK_SWAP_SWAPCTL_BSD | ||
570 | swap_result getSwapFromSwapctl_BSD(swap_config config) { | ||
571 | int i = 0, nswaps = 0, swapctl_res = 0; | ||
572 | struct swapent *ent; | ||
573 | int conv_factor = SWAP_CONVERSION; | ||
574 | |||
575 | /* get the number of active swap devices */ | ||
576 | nswaps = swapctl(SWAP_NSWAP, NULL, 0); | ||
577 | |||
578 | /* initialize swap table + entries */ | ||
579 | ent = (struct swapent *)malloc(sizeof(struct swapent) * nswaps); | ||
580 | |||
581 | /* and now, tally 'em up */ | ||
582 | swapctl_res = swapctl(SWAP_STATS, ent, nswaps); | ||
583 | if (swapctl_res < 0) { | ||
584 | perror(_("swapctl failed: ")); | ||
585 | die(STATE_UNKNOWN, _("Error in swapctl call\n")); | ||
586 | } | ||
587 | |||
588 | |||
589 | double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; | ||
590 | unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; | ||
591 | |||
592 | for (i = 0; i < nswaps; i++) { | ||
593 | dsktotal_mb = (float)ent[i].se_nblks / conv_factor; | ||
594 | dskused_mb = (float)ent[i].se_inuse / conv_factor; | ||
595 | dskfree_mb = (dsktotal_mb - dskused_mb); | ||
596 | |||
597 | if (config.allswaps && dsktotal_mb > 0) { | ||
598 | double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); | ||
599 | |||
600 | if (config.verbose) { | ||
601 | printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); | ||
602 | } | ||
603 | } | ||
604 | |||
605 | total_swap_mb += dsktotal_mb; | ||
606 | free_swap_mb += dskfree_mb; | ||
607 | used_swap_mb += dskused_mb; | ||
608 | } | ||
609 | |||
610 | /* and clean up after ourselves */ | ||
611 | free(ent); | ||
612 | |||
613 | swap_result result = {0}; | ||
614 | |||
615 | result.statusCode = OK; | ||
616 | result.errorcode = OK; | ||
617 | |||
618 | result.metrics.total = total_swap_mb * 1024 * 1024; | ||
619 | result.metrics.free = free_swap_mb * 1024 * 1024; | ||
620 | result.metrics.used = used_swap_mb * 1024 * 1024; | ||
621 | |||
622 | return result; | ||
623 | } | ||
624 | #endif // CHECK_SWAP_SWAPCTL_BSD | ||
625 | |||
626 | #ifdef CHECK_SWAP_SWAPCTL_SVR4 | ||
627 | swap_result getSwapFromSwap_SRV4(swap_config config) { | ||
628 | int i = 0, nswaps = 0, swapctl_res = 0; | ||
629 | //swaptbl_t *tbl = NULL; | ||
630 | void*tbl = NULL; | ||
631 | //swapent_t *ent = NULL; | ||
632 | void*ent = NULL; | ||
633 | /* get the number of active swap devices */ | ||
634 | if ((nswaps = swapctl(SC_GETNSWP, NULL)) == -1) | ||
635 | die(STATE_UNKNOWN, _("Error getting swap devices\n")); | ||
636 | |||
637 | if (nswaps == 0) | ||
638 | die(STATE_OK, _("SWAP OK: No swap devices defined\n")); | ||
639 | |||
640 | if (config.verbose >= 3) | ||
641 | printf("Found %d swap device(s)\n", nswaps); | ||
642 | |||
643 | /* initialize swap table + entries */ | ||
644 | tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); | ||
645 | |||
646 | if (tbl == NULL) | ||
647 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
648 | |||
649 | memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); | ||
650 | tbl->swt_n = nswaps; | ||
651 | for (i = 0; i < nswaps; i++) { | ||
652 | if ((tbl->swt_ent[i].ste_path = | ||
653 | (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL) | ||
654 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
655 | } | ||
656 | |||
657 | /* and now, tally 'em up */ | ||
658 | swapctl_res = swapctl(SC_LIST, tbl); | ||
659 | if (swapctl_res < 0) { | ||
660 | perror(_("swapctl failed: ")); | ||
661 | die(STATE_UNKNOWN, _("Error in swapctl call\n")); | ||
662 | } | ||
663 | |||
664 | double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; | ||
665 | unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; | ||
666 | |||
667 | for (i = 0; i < nswaps; i++) { | ||
668 | dsktotal_mb = (float)tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; | ||
669 | dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION; | ||
670 | dskused_mb = (dsktotal_mb - dskfree_mb); | ||
671 | |||
672 | if (config.verbose >= 3) | ||
673 | printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", | ||
674 | dsktotal_mb, dskfree_mb, dskused_mb); | ||
675 | |||
676 | if (config.allswaps && dsktotal_mb > 0) { | ||
677 | double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); | ||
678 | |||
679 | if (config.verbose) { | ||
680 | printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); | ||
681 | } | ||
682 | } | ||
683 | |||
684 | total_swap_mb += dsktotal_mb; | ||
685 | free_swap_mb += dskfree_mb; | ||
686 | used_swap_mb += dskused_mb; | ||
687 | } | ||
688 | |||
689 | /* and clean up after ourselves */ | ||
690 | for (i = 0; i < nswaps; i++) { | ||
691 | free(tbl->swt_ent[i].ste_path); | ||
692 | } | ||
693 | free(tbl); | ||
694 | |||
695 | swap_result result = {0}; | ||
696 | result.errorcode = OK; | ||
697 | result.metrics.total = total_swap_mb * 1024 * 1024; | ||
698 | result.metrics.free = free_swap_mb * 1024 * 1024; | ||
699 | result.metrics.used = used_swap_mb * 1024 * 1024; | ||
700 | |||
701 | return result; | ||
702 | } | ||
703 | #endif // CHECK_SWAP_SWAPCTL_SVR4 | ||