From 42f5f165787119a3c185f935ae2343de55736385 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 13 Dec 2023 09:23:48 +0100 Subject: check_swap: Change another fake boolen to a real one --- plugins/check_swap.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index fddd93fa..1a7011c0 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -52,7 +52,7 @@ const char *email = "devel@monitoring-plugins.org"; #endif typedef struct { - int is_percentage; + bool is_percentage; uint64_t value; } threshold_t; @@ -465,7 +465,7 @@ process_arguments (int argc, char **argv) if (optarg[length - 1] == '%') { /* It's percentage */ - warn.is_percentage = 1; + warn.is_percentage = true; optarg[length - 1] = '\0'; if (is_uint64(optarg, &warn.value)) { if (warn.value > 100) { @@ -475,7 +475,7 @@ process_arguments (int argc, char **argv) break; } else { /* It's Bytes */ - warn.is_percentage = 0; + warn.is_percentage = false; if (is_uint64(optarg, &warn.value)) { break; } else { @@ -495,7 +495,7 @@ process_arguments (int argc, char **argv) if (optarg[length - 1] == '%') { /* It's percentage */ - crit.is_percentage = 1; + crit.is_percentage = true; optarg[length - 1] = '\0'; if (is_uint64(optarg, &crit.value)) { if (crit.value> 100) { @@ -505,7 +505,7 @@ process_arguments (int argc, char **argv) break; } else { /* It's Bytes */ - crit.is_percentage = 0; + crit.is_percentage = false; if (is_uint64(optarg, &crit.value)) { break; } else { -- cgit v1.2.3-74-g34f1 From b32d7421ee81faaadb2729ee8b19cec4fd1b1899 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 13 Dec 2023 09:29:12 +0100 Subject: check_swap: Rename type since *_t is reserved for C standard types --- plugins/check_swap.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 1a7011c0..9630071a 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -54,7 +54,7 @@ const char *email = "devel@monitoring-plugins.org"; typedef struct { bool is_percentage; uint64_t value; -} threshold_t; +} threshold; int check_swap (float free_swap_mb, float total_swap_mb); int process_arguments (int argc, char **argv); @@ -62,8 +62,8 @@ int validate_arguments (void); void print_usage (void); void print_help (void); -threshold_t warn; -threshold_t crit; +threshold warn; +threshold crit; int verbose; bool allswaps = false; int no_swap_state = STATE_CRITICAL; -- cgit v1.2.3-74-g34f1 From 3a10773c0918a190c43d1508f9f572709fba25a8 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 13 Dec 2023 16:17:47 +0100 Subject: check_swap: Heavily refactored linux part --- plugins/check_swap.c | 776 +++++++++++++++++++++++++++------------------------ 1 file changed, 404 insertions(+), 372 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 9630071a..60309a76 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -56,70 +56,335 @@ typedef struct { uint64_t value; } threshold; -int check_swap (float free_swap_mb, float total_swap_mb); -int process_arguments (int argc, char **argv); -int validate_arguments (void); -void print_usage (void); -void print_help (void); - -threshold warn; -threshold crit; -int verbose; -bool allswaps = false; -int no_swap_state = STATE_CRITICAL; - -int -main (int argc, char **argv) -{ - unsigned int percent_used, percent; - uint64_t total_swap_mb = 0, used_swap_mb = 0, free_swap_mb = 0; - uint64_t dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0; - uint64_t tmp_KB = 0; - int result = STATE_UNKNOWN; - char input_buffer[MAX_INPUT_BUFFER]; -#ifdef HAVE_PROC_MEMINFO - FILE *fp; -#else - int conv_factor = SWAP_CONVERSION; -# ifdef HAVE_SWAP - char *temp_buffer; - char *swap_command; - char *swap_format; -# else -# ifdef HAVE_DECL_SWAPCTL - int i=0, nswaps=0, swapctl_res=0; -# ifdef CHECK_SWAP_SWAPCTL_SVR4 - swaptbl_t *tbl=NULL; - swapent_t *ent=NULL; -# else -# ifdef CHECK_SWAP_SWAPCTL_BSD - struct swapent *ent; -# endif /* CHECK_SWAP_SWAPCTL_BSD */ -# endif /* CHECK_SWAP_SWAPCTL_SVR4 */ -# endif /* HAVE_DECL_SWAPCTL */ -# endif -#endif - char str[32]; - char *status; +typedef struct { + unsigned long long free; // Free swap in Bytes! + unsigned long long used; // Used swap in Bytes! + unsigned long long total; // Total swap size, you guessed it, in Bytes! +} swap_metrics; + +typedef struct { + int errorcode; + int statusCode; + swap_metrics metrics; +} swap_result; + +typedef struct { + int verbose; + bool allswaps; + int no_swap_state; + threshold warn; + threshold crit; +} swap_config; + +typedef struct { + int errorcode; + swap_config config; +} swap_config_wrapper; + +swap_config_wrapper process_arguments (swap_config_wrapper config, int argc, char **argv); +void print_usage (); +void print_help (swap_config); + +swap_result getSwapFromProcMeminfo(swap_config config); +swap_result getSwapFromSwapCommand(swap_config config); +swap_result getSwapFromSwapctl_BSD(swap_config config); +swap_result getSwapFromSwap_SRV4(swap_config config); + +swap_config swap_config_init() { + swap_config tmp = { 0 }; + tmp.allswaps = false; + tmp.no_swap_state = STATE_CRITICAL; + tmp.verbose = 0; + + return tmp; +} + +int main (int argc, char **argv) { setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); + char *status; status = strdup (""); /* Parse extra opts if any */ argv=np_extra_opts (&argc, argv, progname); - if (process_arguments (argc, argv) == ERROR) + swap_config_wrapper tmp = { + .errorcode = OK + }; + + tmp.config = swap_config_init(); + + tmp = process_arguments (tmp, argc, argv); + + if (tmp.errorcode != OK) { usage4 (_("Could not parse arguments")); + } + + swap_config config = tmp.config; + +#ifdef HAVE_PROC_MEMINFO + swap_result data = getSwapFromProcMeminfo(config); +#else +# ifdef HAVE_SWAP + swap_result data = getSwapFromSwapCommand(); +# else +# ifdef CHECK_SWAP_SWAPCTL_SVR4 + swap_result data = getSwapFromSwapctl_SRV4(); +# else +# ifdef CHECK_SWAP_SWAPCTL_BSD + swap_result data = getSwapFromSwapctl_BSD(); +# else + #error No now found to retrieve swap +# endif /* CHECK_SWAP_SWAPCTL_BSD */ +# endif /* CHECK_SWAP_SWAPCTL_SVR4 */ +# endif /* HAVE_SWAP */ +#endif /* HAVE_PROC_MEMINFO */ + + double percent_used; + + /* if total_swap_mb == 0, let's not divide by 0 */ + if(data.metrics.total != 0) { + percent_used = 100 * ((double) data.metrics.used) / ((double) data.metrics.total); + } else { + printf (_("SWAP %s - Swap is either disabled, not present, or of zero size."), + state_text (data.statusCode)); + exit(config.no_swap_state); + } + + uint64_t warn_print = config.warn.value; + if (config.warn.is_percentage) { + warn_print = config.warn.value * (data.metrics.total*1024 *1024/100); + } + + uint64_t crit_print = config.crit.value; + if (config.crit.is_percentage) { + crit_print = config.crit.value * (data.metrics.total*1024 *1024/100); + } + + char *perfdata = perfdata_uint64 ("swap", data.metrics.free *1024 *1024, "B", + true, warn_print, + true, crit_print, + true, 0, + true, (long) data.metrics.total* 1024 * 1024); + + if ((config.warn.is_percentage && (percent_used >= (100 - config.warn.value))) || + config.warn.value >= data.metrics.free) { + data.statusCode = max_state (data.statusCode, STATE_WARNING); + } + + if ((config.crit.is_percentage && (percent_used >= (100 - config.crit.value))) || + config.crit.value >= data.metrics.free) { + data.statusCode = max_state (data.statusCode, STATE_CRITICAL); + } + + printf (_("SWAP %s - %g%% free (%lluMB out of %lluMB) %s|%s\n"), + state_text (data.statusCode), + (100 - percent_used), data.metrics.free, data.metrics.total, status, + perfdata); + + exit(data.statusCode); +} + + +/* process command-line arguments */ +swap_config_wrapper process_arguments (swap_config_wrapper conf_wrapper, int argc, char **argv) { + if (argc < 2) { + conf_wrapper.errorcode = ERROR; + return conf_wrapper; + } + + int option = 0; + static struct option longopts[] = { + {"warning", required_argument, 0, 'w'}, + {"critical", required_argument, 0, 'c'}, + {"allswaps", no_argument, 0, 'a'}, + {"no-swap", required_argument, 0, 'n'}, + {"verbose", no_argument, 0, 'v'}, + {"version", no_argument, 0, 'V'}, + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0} + }; + + int c = 0; /* option character */ + while (true) { + c = getopt_long (argc, argv, "+?Vvhac:w:n:", longopts, &option); + + if (c == -1 || c == EOF) + break; + + switch (c) { + case 'w': /* warning size threshold */ + { + /* + * We expect either a positive integer value without a unit, which means + * the unit is Bytes or a positive integer value and a percentage sign (%), + * which means the value must be with 0 and 100 and is relative to the total swap + */ + size_t length; + length = strlen(optarg); + + if (optarg[length - 1] == '%') { + /* It's percentage */ + conf_wrapper.config.warn.is_percentage = true; + optarg[length - 1] = '\0'; + if (is_uint64(optarg, &conf_wrapper.config.warn.value)) { + if (conf_wrapper.config.warn.value > 100) { + usage4 (_("Warning threshold percentage must be <= 100!")); + } + } + break; + } else { + /* It's Bytes */ + conf_wrapper.config.warn.is_percentage = false; + if (is_uint64(optarg, &conf_wrapper.config.warn.value)) { + break; + } else { + usage4 (_("Warning threshold be positive integer or percentage!")); + } + } + } + case 'c': /* critical size threshold */ + { + /* + * We expect either a positive integer value without a unit, which means + * the unit is Bytes or a positive integer value and a percentage sign (%), + * which means the value must be with 0 and 100 and is relative to the total swap + */ + size_t length; + length = strlen(optarg); + + if (optarg[length - 1] == '%') { + /* It's percentage */ + conf_wrapper.config.crit.is_percentage = true; + optarg[length - 1] = '\0'; + if (is_uint64(optarg, &conf_wrapper.config.crit.value)) { + if (conf_wrapper.config.crit.value> 100) { + usage4 (_("Critical threshold percentage must be <= 100!")); + } + } + break; + } else { + /* It's Bytes */ + conf_wrapper.config.crit.is_percentage = false; + if (is_uint64(optarg, &conf_wrapper.config.crit.value)) { + break; + } else { + usage4 (_("Critical threshold be positive integer or percentage!")); + } + } + } + case 'a': /* all swap */ + conf_wrapper.config.allswaps = true; + break; + case 'n': + if ((conf_wrapper.config.no_swap_state = mp_translate_state(optarg)) == ERROR) { + usage4 (_("no-swap result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); + } + break; + case 'v': /* verbose */ + conf_wrapper.config.verbose++; + break; + case 'V': /* version */ + print_revision (progname, NP_VERSION); + exit (STATE_UNKNOWN); + case 'h': /* help */ + print_help (conf_wrapper.config); + exit (STATE_UNKNOWN); + case '?': /* error */ + usage5 (); + } + } + + c = optind; + + if (conf_wrapper.config.warn.value == 0 && conf_wrapper.config.crit.value == 0) { + conf_wrapper.errorcode = ERROR; + return conf_wrapper; + } else if ((conf_wrapper.config.warn.is_percentage == conf_wrapper.config.crit.is_percentage) && + (conf_wrapper.config.warn.value < conf_wrapper.config.crit.value)) { + /* This is NOT triggered if warn and crit are different units, e.g warn is percentage + * and crit is absolute. We cannot determine the condition at this point since we + * dont know the value of total swap yet + */ + usage4(_("Warning should be more than critical")); + } + + return conf_wrapper; +} + + +void +print_help (swap_config config) +{ + print_revision (progname, NP_VERSION); + + printf (_(COPYRIGHT), copyright, email); + + printf ("%s\n", _("Check swap space on local machine.")); + + printf ("\n\n"); + + print_usage(); + + printf (UT_HELP_VRSN); + printf (UT_EXTRA_OPTS); + + printf (" %s\n", "-w, --warning=INTEGER"); + printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of swap space are free")); + printf (" %s\n", "-w, --warning=PERCENT%"); + printf (" %s\n", _("Exit with WARNING status if less than PERCENT of swap space is free")); + printf (" %s\n", "-c, --critical=INTEGER"); + printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of swap space are free")); + printf (" %s\n", "-c, --critical=PERCENT%"); + printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of swap space is free")); + printf (" %s\n", "-a, --allswaps"); + printf (" %s\n", _("Conduct comparisons for all swap partitions, one by one")); + printf (" %s\n", "-n, --no-swap="); + printf (" %s %s\n", _("Resulting state when there is no swap regardless of thresholds. Default:"), state_text(config.no_swap_state)); + printf (UT_VERBOSE); + + printf ("\n"); + printf ("%s\n", _("Notes:")); + printf (" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, they are all checked.")); + printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.")); + + printf (UT_SUPPORT); +} + + +void +print_usage () +{ + printf ("%s\n", _("Usage:")); + printf (" %s [-av] -w %% -c %%\n",progname); + printf (" -w -c [-n ]\n"); +} #ifdef HAVE_PROC_MEMINFO - if (verbose >= 3) { +swap_result getSwapFromProcMeminfo(swap_config config) { + + if (config.verbose >= 3) { printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO); } + + FILE *fp; fp = fopen (PROC_MEMINFO, "r"); + + + swap_result result = { 0 }; + result.statusCode = STATE_OK; + + uint64_t dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0; + + char input_buffer[MAX_INPUT_BUFFER]; + char str[32]; + while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { + uint64_t tmp_KB = 0; + /* * The following sscanf call looks for a line looking like: "Swap: 123 123 123" * On which kind of system this format exists, I can not say, but I wanted to @@ -129,59 +394,63 @@ main (int argc, char **argv) dsktotal_mb = dsktotal_mb / (1024 * 1024); /* Apply conversion */ dskused_mb = dskused_mb / (1024 * 1024); dskfree_mb = dskfree_mb / (1024 * 1024); - total_swap_mb += dsktotal_mb; - used_swap_mb += dskused_mb; - free_swap_mb += dskfree_mb; - if (allswaps) { - if (dsktotal_mb == 0) - percent=100.0; - else - percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); - result = max_state (result, check_swap (dskfree_mb, dsktotal_mb)); - if (verbose) - xasprintf (&status, "%s [%lu (%d%%)]", status, dskfree_mb, 100 - percent); - } - } + + result.metrics.total += dsktotal_mb; + result.metrics.used+= dskused_mb; + result.metrics.free += dskfree_mb; + + /* * The following sscanf call looks for lines looking like: "SwapTotal: 123" and "SwapFree: 123" * This format exists at least on Debian Linux with a 5.* kernel */ - else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu %*[k]%*[B]", str, &tmp_KB)) { - if (verbose >= 3) { + } else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu %*[k]%*[B]", str, &tmp_KB)) { + if (config.verbose >= 3) { printf("Got %s with %lu\n", str, tmp_KB); } /* I think this part is always in Kb, so convert to mb */ if (strcmp ("Total", str) == 0) { dsktotal_mb = tmp_KB / 1024; - } - else if (strcmp ("Free", str) == 0) { + } else if (strcmp ("Free", str) == 0) { dskfree_mb = dskfree_mb + tmp_KB / 1024; - } - else if (strcmp ("Cached", str) == 0) { + } else if (strcmp ("Cached", str) == 0) { dskfree_mb = dskfree_mb + tmp_KB / 1024; } } } + fclose(fp); - dskused_mb = dsktotal_mb - dskfree_mb; - total_swap_mb = dsktotal_mb; - used_swap_mb = dskused_mb; - free_swap_mb = dskfree_mb; -#else -# ifdef HAVE_SWAP + + result.metrics.total = dsktotal_mb; + result.metrics.used = dsktotal_mb - dskfree_mb; + result.metrics.free = dskfree_mb; + + return result; +} +#endif + +#ifdef HAVE_SWAP +swap_result getSwapFromSwapCommand() { + swap_result result = { 0 }; + + char *temp_buffer; + char *swap_command; + char *swap_format; + int conv_factor = SWAP_CONVERSION; + xasprintf(&swap_command, "%s", SWAP_COMMAND); xasprintf(&swap_format, "%s", SWAP_FORMAT); /* These override the command used if a summary (and thus ! allswaps) is required */ /* The summary flag returns more accurate information about swap usage on these OSes */ -# ifdef _AIX +# ifdef _AIX if (!allswaps) { xasprintf(&swap_command, "%s", "/usr/sbin/lsps -s"); xasprintf(&swap_format, "%s", "%lu%*s %lu"); conv_factor = 1; } -# endif +# endif if (verbose >= 2) printf (_("Command: %s\n"), swap_command); @@ -215,7 +484,7 @@ main (int argc, char **argv) } /* If different swap command is used for summary switch, need to read format differently */ -# ifdef _AIX +# ifdef _AIX if (!allswaps) { fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */ sscanf (input_buffer, swap_format, &total_swap_mb, &used_swap_mb); @@ -224,7 +493,7 @@ main (int argc, char **argv) if (verbose >= 3) printf (_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb, free_swap_mb); } else { -# endif +# endif while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { sscanf (input_buffer, swap_format, &dsktotal_mb, &dskfree_mb); @@ -251,21 +520,68 @@ main (int argc, char **argv) } # ifdef _AIX } -# endif +# endif + + /* If we get anything on STDERR, at least set warning */ + while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) + result = max_state (result, STATE_WARNING); + + /* close stderr */ + (void) fclose (child_stderr); + + /* close the pipe */ + if (spclose (child_process)) + result = max_state (result, STATE_WARNING); +} +#endif // HAVE_SWAP + +#ifdef CHECK_SWAP_SWAPCTL_BSD +swap_result getSwapFromSwapctl_BSD() { + int i=0, nswaps=0, swapctl_res=0; + struct swapent *ent; + int conv_factor = SWAP_CONVERSION; + + /* get the number of active swap devices */ + nswaps=swapctl(SWAP_NSWAP, NULL, 0); + + /* initialize swap table + entries */ + ent=(struct swapent*)malloc(sizeof(struct swapent)*nswaps); + + /* and now, tally 'em up */ + swapctl_res=swapctl(SWAP_STATS, ent, nswaps); + if(swapctl_res < 0){ + perror(_("swapctl failed: ")); + die(STATE_UNKNOWN, _("Error in swapctl call\n")); + } - /* If we get anything on STDERR, at least set warning */ - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) - result = max_state (result, STATE_WARNING); + for(i=0;i 0){ + percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); + result = max_state (result, check_swap(dskfree_mb, dsktotal_mb)); + if (verbose) { + xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); + } + } - /* close the pipe */ - if (spclose (child_process)) - result = max_state (result, STATE_WARNING); -# else -# ifdef CHECK_SWAP_SWAPCTL_SVR4 + total_swap_mb += dsktotal_mb; + free_swap_mb += dskfree_mb; + used_swap_mb += dskused_mb; + } + + /* and clean up after ourselves */ + free(ent); +} +#endif // CHECK_SWAP_SWAPCTL_BSD +#ifdef CHECK_SWAP_SWAPCTL_SVR4 +swap_result getSwapFromSwap_SRV4() { + int i=0, nswaps=0, swapctl_res=0; + swaptbl_t *tbl=NULL; + swapent_t *ent=NULL; /* get the number of active swap devices */ if((nswaps=swapctl(SC_GETNSWP, NULL))== -1) die(STATE_UNKNOWN, _("Error getting swap devices\n") ); @@ -322,289 +638,5 @@ main (int argc, char **argv) free(tbl->swt_ent[i].ste_path); } free(tbl); -# else -# ifdef CHECK_SWAP_SWAPCTL_BSD - - /* get the number of active swap devices */ - nswaps=swapctl(SWAP_NSWAP, NULL, 0); - - /* initialize swap table + entries */ - ent=(struct swapent*)malloc(sizeof(struct swapent)*nswaps); - - /* and now, tally 'em up */ - swapctl_res=swapctl(SWAP_STATS, ent, nswaps); - if(swapctl_res < 0){ - perror(_("swapctl failed: ")); - die(STATE_UNKNOWN, _("Error in swapctl call\n")); - } - - for(i=0;i 0){ - percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); - result = max_state (result, check_swap(dskfree_mb, dsktotal_mb)); - if (verbose) { - xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); - } - } - - total_swap_mb += dsktotal_mb; - free_swap_mb += dskfree_mb; - used_swap_mb += dskused_mb; - } - - /* and clean up after ourselves */ - free(ent); - -# endif /* CHECK_SWAP_SWAPCTL_BSD */ -# endif /* CHECK_SWAP_SWAPCTL_SVR4 */ -# endif /* HAVE_SWAP */ -#endif /* HAVE_PROC_MEMINFO */ - - /* if total_swap_mb == 0, let's not divide by 0 */ - if(total_swap_mb) { - percent_used = 100 * ((double) used_swap_mb) / ((double) total_swap_mb); - } else { - percent_used = 100; - status = "- Swap is either disabled, not present, or of zero size. "; - } - - result = max_state (result, check_swap(free_swap_mb, total_swap_mb)); - printf (_("SWAP %s - %d%% free (%dMB out of %dMB) %s|"), - state_text (result), - (100 - percent_used), (int) free_swap_mb, (int) total_swap_mb, status); - - uint64_t warn_print = warn.value; - if (warn.is_percentage) warn_print = warn.value * (total_swap_mb *1024 *1024/100); - uint64_t crit_print = crit.value; - if (crit.is_percentage) crit_print = crit.value * (total_swap_mb *1024 *1024/100); - - puts (perfdata_uint64 ("swap", free_swap_mb *1024 *1024, "B", - true, warn_print, - true, crit_print, - true, 0, - true, (long) total_swap_mb * 1024 * 1024)); - - return result; -} - - -int -check_swap(float free_swap_mb, float total_swap_mb) -{ - - if (!total_swap_mb) return no_swap_state; - - uint64_t free_swap = free_swap_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */ - - if (!crit.is_percentage && crit.value >= free_swap) return STATE_CRITICAL; - if (!warn.is_percentage && warn.value >= free_swap) return STATE_WARNING; - - - uint64_t usage_percentage = ((total_swap_mb - free_swap_mb) / total_swap_mb) * 100; - - if (crit.is_percentage && - crit.value != 0 && - usage_percentage >= (100 - crit.value)) - { - return STATE_CRITICAL; - } - - if (warn.is_percentage && - warn.value != 0 && - usage_percentage >= (100 - warn.value)) - { - return STATE_WARNING; - } - - return STATE_OK; -} - - - -/* process command-line arguments */ -int -process_arguments (int argc, char **argv) -{ - int c = 0; /* option character */ - - int option = 0; - static struct option longopts[] = { - {"warning", required_argument, 0, 'w'}, - {"critical", required_argument, 0, 'c'}, - {"allswaps", no_argument, 0, 'a'}, - {"no-swap", required_argument, 0, 'n'}, - {"verbose", no_argument, 0, 'v'}, - {"version", no_argument, 0, 'V'}, - {"help", no_argument, 0, 'h'}, - {0, 0, 0, 0} - }; - - if (argc < 2) - return ERROR; - - while (1) { - c = getopt_long (argc, argv, "+?Vvhac:w:n:", longopts, &option); - - if (c == -1 || c == EOF) - break; - - switch (c) { - case 'w': /* warning size threshold */ - { - /* - * We expect either a positive integer value without a unit, which means - * the unit is Bytes or a positive integer value and a percentage sign (%), - * which means the value must be with 0 and 100 and is relative to the total swap - */ - size_t length; - length = strlen(optarg); - - if (optarg[length - 1] == '%') { - /* It's percentage */ - warn.is_percentage = true; - optarg[length - 1] = '\0'; - if (is_uint64(optarg, &warn.value)) { - if (warn.value > 100) { - usage4 (_("Warning threshold percentage must be <= 100!")); - } - } - break; - } else { - /* It's Bytes */ - warn.is_percentage = false; - if (is_uint64(optarg, &warn.value)) { - break; - } else { - usage4 (_("Warning threshold be positive integer or percentage!")); - } - } - } - case 'c': /* critical size threshold */ - { - /* - * We expect either a positive integer value without a unit, which means - * the unit is Bytes or a positive integer value and a percentage sign (%), - * which means the value must be with 0 and 100 and is relative to the total swap - */ - size_t length; - length = strlen(optarg); - - if (optarg[length - 1] == '%') { - /* It's percentage */ - crit.is_percentage = true; - optarg[length - 1] = '\0'; - if (is_uint64(optarg, &crit.value)) { - if (crit.value> 100) { - usage4 (_("Critical threshold percentage must be <= 100!")); - } - } - break; - } else { - /* It's Bytes */ - crit.is_percentage = false; - if (is_uint64(optarg, &crit.value)) { - break; - } else { - usage4 (_("Critical threshold be positive integer or percentage!")); - } - } - } - case 'a': /* all swap */ - allswaps = true; - break; - case 'n': - if ((no_swap_state = mp_translate_state(optarg)) == ERROR) { - usage4 (_("no-swap result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); - } - break; - case 'v': /* verbose */ - verbose++; - break; - case 'V': /* version */ - print_revision (progname, NP_VERSION); - exit (STATE_UNKNOWN); - case 'h': /* help */ - print_help (); - exit (STATE_UNKNOWN); - case '?': /* error */ - usage5 (); - } - } - - c = optind; - if (c == argc) - return validate_arguments (); - - return validate_arguments (); -} - - - -int -validate_arguments (void) -{ - if (warn.value == 0 && crit.value == 0) { - return ERROR; - } - else if ((warn.is_percentage == crit.is_percentage) && (warn.value < crit.value)) { - /* This is NOT triggered if warn and crit are different units, e.g warn is percentage - * and crit is absolute. We cannot determine the condition at this point since we - * dont know the value of total swap yet - */ - usage4(_("Warning should be more than critical")); - } - return OK; -} - - - -void -print_help (void) -{ - print_revision (progname, NP_VERSION); - - printf (_(COPYRIGHT), copyright, email); - - printf ("%s\n", _("Check swap space on local machine.")); - - printf ("\n\n"); - - print_usage (); - - printf (UT_HELP_VRSN); - printf (UT_EXTRA_OPTS); - - printf (" %s\n", "-w, --warning=INTEGER"); - printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of swap space are free")); - printf (" %s\n", "-w, --warning=PERCENT%"); - printf (" %s\n", _("Exit with WARNING status if less than PERCENT of swap space is free")); - printf (" %s\n", "-c, --critical=INTEGER"); - printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of swap space are free")); - printf (" %s\n", "-c, --critical=PERCENT%"); - printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of swap space is free")); - printf (" %s\n", "-a, --allswaps"); - printf (" %s\n", _("Conduct comparisons for all swap partitions, one by one")); - printf (" %s\n", "-n, --no-swap="); - printf (" %s %s\n", _("Resulting state when there is no swap regardless of thresholds. Default:"), state_text(no_swap_state)); - printf (UT_VERBOSE); - - printf ("\n"); - printf ("%s\n", _("Notes:")); - printf (" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, they are all checked.")); - printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.")); - - printf (UT_SUPPORT); -} - - -void -print_usage (void) -{ - printf ("%s\n", _("Usage:")); - printf (" %s [-av] -w %% -c %%\n",progname); - printf (" -w -c [-n ]\n"); } +#endif // CHECK_SWAP_SWAPCTL_SVR4 -- cgit v1.2.3-74-g34f1 From 22a423ad5d2cc1d117c5fd933d2fd6901f4434c6 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 13 Dec 2023 17:13:23 +0100 Subject: check_swap: Return byte number (in linux) and simplify code after that --- plugins/check_swap.c | 76 +++++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 34 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 60309a76..6e995156 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -156,19 +156,28 @@ int main (int argc, char **argv) { uint64_t warn_print = config.warn.value; if (config.warn.is_percentage) { - warn_print = config.warn.value * (data.metrics.total*1024 *1024/100); + warn_print = + config.warn.value * (data.metrics.total / 100); } uint64_t crit_print = config.crit.value; if (config.crit.is_percentage) { - crit_print = config.crit.value * (data.metrics.total*1024 *1024/100); + crit_print = + config.crit.value * (data.metrics.total / 100); } - char *perfdata = perfdata_uint64 ("swap", data.metrics.free *1024 *1024, "B", - true, warn_print, - true, crit_print, - true, 0, - true, (long) data.metrics.total* 1024 * 1024); + char *perfdata = perfdata_uint64( + "swap", + data.metrics.free, + "B", + true, warn_print, + true, crit_print, + true, 0, + true, (long)data.metrics.total); + + if (config.verbose > 1) { + printf("Warn threshold value: %"PRIu64"\n", config.warn.value); + } if ((config.warn.is_percentage && (percent_used >= (100 - config.warn.value))) || config.warn.value >= data.metrics.free) { @@ -188,7 +197,6 @@ int main (int argc, char **argv) { exit(data.statusCode); } - /* process command-line arguments */ swap_config_wrapper process_arguments (swap_config_wrapper conf_wrapper, int argc, char **argv) { if (argc < 2) { @@ -377,7 +385,7 @@ swap_result getSwapFromProcMeminfo(swap_config config) { swap_result result = { 0 }; result.statusCode = STATE_OK; - uint64_t dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0; + uint64_t swap_total = 0, swap_used = 0, swap_free = 0; char input_buffer[MAX_INPUT_BUFFER]; char str[32]; @@ -390,41 +398,41 @@ swap_result getSwapFromProcMeminfo(swap_config config) { * On which kind of system this format exists, I can not say, but I wanted to * document this for people who are not adapt with sscanf anymore, like me */ - if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &dsktotal_mb, &dskused_mb, &dskfree_mb) == 3) { - dsktotal_mb = dsktotal_mb / (1024 * 1024); /* Apply conversion */ - dskused_mb = dskused_mb / (1024 * 1024); - dskfree_mb = dskfree_mb / (1024 * 1024); - - result.metrics.total += dsktotal_mb; - result.metrics.used+= dskused_mb; - result.metrics.free += dskfree_mb; - - - - /* - * The following sscanf call looks for lines looking like: "SwapTotal: 123" and "SwapFree: 123" - * This format exists at least on Debian Linux with a 5.* kernel - */ - } else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu %*[k]%*[B]", str, &tmp_KB)) { + if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", + &swap_total, &swap_used, &swap_free) == 3) { + + result.metrics.total += swap_total; + result.metrics.used += swap_used; + result.metrics.free += swap_free; + + /* + * The following sscanf call looks for lines looking like: + * "SwapTotal: 123" and "SwapFree: 123" This format exists at least + * on Debian Linux with a 5.* kernel + */ + } else if (sscanf(input_buffer, + "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " + "%*[k]%*[B]", + str, &tmp_KB)) { if (config.verbose >= 3) { printf("Got %s with %lu\n", str, tmp_KB); } /* I think this part is always in Kb, so convert to mb */ - if (strcmp ("Total", str) == 0) { - dsktotal_mb = tmp_KB / 1024; - } else if (strcmp ("Free", str) == 0) { - dskfree_mb = dskfree_mb + tmp_KB / 1024; - } else if (strcmp ("Cached", str) == 0) { - dskfree_mb = dskfree_mb + tmp_KB / 1024; + if (strcmp("Total", str) == 0) { + swap_total = tmp_KB * 1024; + } else if (strcmp("Free", str) == 0) { + swap_free = swap_free + tmp_KB * 1024; + } else if (strcmp("Cached", str) == 0) { + swap_free = swap_free + tmp_KB * 1024; } } } fclose(fp); - result.metrics.total = dsktotal_mb; - result.metrics.used = dsktotal_mb - dskfree_mb; - result.metrics.free = dskfree_mb; + result.metrics.total = swap_total; + result.metrics.used = swap_total - swap_free; + result.metrics.free = swap_free; return result; } -- cgit v1.2.3-74-g34f1 From 06c3393963c95573dd678cd13fa41d4b76438cc2 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 13 Dec 2023 17:14:06 +0100 Subject: check_swap: clang-format + minor cosmetics/debugging/etc --- plugins/check_swap.c | 633 +++++++++++++++++++++++++++------------------------ 1 file changed, 330 insertions(+), 303 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 6e995156..40ac2ed3 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -1,31 +1,31 @@ /***************************************************************************** -* -* Monitoring check_swap plugin -* -* License: GPL -* Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) -* Copyright (c) 2000-2007 Monitoring Plugins Development Team -* -* Description: -* -* This file contains the check_swap plugin -* -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -* -* -*****************************************************************************/ + * + * Monitoring check_swap plugin + * + * License: GPL + * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) + * Copyright (c) 2000-2007 Monitoring Plugins Development Team + * + * Description: + * + * This file contains the check_swap plugin + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + *****************************************************************************/ const char *progname = "check_swap"; const char *copyright = "2000-2007"; @@ -36,19 +36,19 @@ const char *email = "devel@monitoring-plugins.org"; #include "utils.h" #ifdef HAVE_DECL_SWAPCTL -# ifdef HAVE_SYS_PARAM_H -# include -# endif -# ifdef HAVE_SYS_SWAP_H -# include -# endif -# ifdef HAVE_SYS_STAT_H -# include -# endif +#ifdef HAVE_SYS_PARAM_H +#include +#endif +#ifdef HAVE_SYS_SWAP_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif #endif #ifndef SWAP_CONVERSION -# define SWAP_CONVERSION 1 +#define SWAP_CONVERSION 1 #endif typedef struct { @@ -57,8 +57,8 @@ typedef struct { } threshold; typedef struct { - unsigned long long free; // Free swap in Bytes! - unsigned long long used; // Used swap in Bytes! + unsigned long long free; // Free swap in Bytes! + unsigned long long used; // Used swap in Bytes! unsigned long long total; // Total swap size, you guessed it, in Bytes! } swap_metrics; @@ -81,9 +81,10 @@ typedef struct { swap_config config; } swap_config_wrapper; -swap_config_wrapper process_arguments (swap_config_wrapper config, int argc, char **argv); -void print_usage (); -void print_help (swap_config); +swap_config_wrapper process_arguments(swap_config_wrapper config, int argc, + char **argv); +void print_usage(); +void print_help(swap_config); swap_result getSwapFromProcMeminfo(swap_config config); swap_result getSwapFromSwapCommand(swap_config config); @@ -91,7 +92,7 @@ swap_result getSwapFromSwapctl_BSD(swap_config config); swap_result getSwapFromSwap_SRV4(swap_config config); swap_config swap_config_init() { - swap_config tmp = { 0 }; + swap_config tmp = {0}; tmp.allswaps = false; tmp.no_swap_state = STATE_CRITICAL; tmp.verbose = 0; @@ -99,28 +100,25 @@ swap_config swap_config_init() { return tmp; } - -int main (int argc, char **argv) { - setlocale (LC_ALL, ""); - bindtextdomain (PACKAGE, LOCALEDIR); - textdomain (PACKAGE); +int main(int argc, char **argv) { + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); char *status; - status = strdup (""); + status = strdup(""); /* Parse extra opts if any */ - argv=np_extra_opts (&argc, argv, progname); + argv = np_extra_opts(&argc, argv, progname); - swap_config_wrapper tmp = { - .errorcode = OK - }; + swap_config_wrapper tmp = {.errorcode = OK}; tmp.config = swap_config_init(); - tmp = process_arguments (tmp, argc, argv); + tmp = process_arguments(tmp, argc, argv); if (tmp.errorcode != OK) { - usage4 (_("Could not parse arguments")); + usage4(_("Could not parse arguments")); } swap_config config = tmp.config; @@ -128,194 +126,208 @@ int main (int argc, char **argv) { #ifdef HAVE_PROC_MEMINFO swap_result data = getSwapFromProcMeminfo(config); #else -# ifdef HAVE_SWAP +#ifdef HAVE_SWAP swap_result data = getSwapFromSwapCommand(); -# else -# ifdef CHECK_SWAP_SWAPCTL_SVR4 +#else +#ifdef CHECK_SWAP_SWAPCTL_SVR4 swap_result data = getSwapFromSwapctl_SRV4(); -# else -# ifdef CHECK_SWAP_SWAPCTL_BSD +#else +#ifdef CHECK_SWAP_SWAPCTL_BSD swap_result data = getSwapFromSwapctl_BSD(); -# else - #error No now found to retrieve swap -# endif /* CHECK_SWAP_SWAPCTL_BSD */ -# endif /* CHECK_SWAP_SWAPCTL_SVR4 */ -# endif /* HAVE_SWAP */ +#else +#error No way found to retrieve swap +#endif /* CHECK_SWAP_SWAPCTL_BSD */ +#endif /* CHECK_SWAP_SWAPCTL_SVR4 */ +#endif /* HAVE_SWAP */ #endif /* HAVE_PROC_MEMINFO */ double percent_used; /* if total_swap_mb == 0, let's not divide by 0 */ - if(data.metrics.total != 0) { - percent_used = 100 * ((double) data.metrics.used) / ((double) data.metrics.total); + if (data.metrics.total != 0) { + percent_used = + 100 * ((double)data.metrics.used) / ((double)data.metrics.total); } else { - printf (_("SWAP %s - Swap is either disabled, not present, or of zero size."), - state_text (data.statusCode)); + printf(_("SWAP %s - Swap is either disabled, not present, or of zero " + "size."), + state_text(data.statusCode)); exit(config.no_swap_state); } + if (config.verbose) { + printf("Computed usage percentage: %g\n", percent_used); + } + uint64_t warn_print = config.warn.value; if (config.warn.is_percentage) { - warn_print = - config.warn.value * (data.metrics.total / 100); + warn_print = config.warn.value * (data.metrics.total / 100); } uint64_t crit_print = config.crit.value; if (config.crit.is_percentage) { - crit_print = - config.crit.value * (data.metrics.total / 100); + crit_print = config.crit.value * (data.metrics.total / 100); } - char *perfdata = perfdata_uint64( - "swap", - data.metrics.free, - "B", - true, warn_print, - true, crit_print, - true, 0, - true, (long)data.metrics.total); + char *perfdata = + perfdata_uint64("swap", data.metrics.free, "B", true, warn_print, true, + crit_print, true, 0, true, (long)data.metrics.total); if (config.verbose > 1) { - printf("Warn threshold value: %"PRIu64"\n", config.warn.value); + printf("Warn threshold value: %" PRIu64 "\n", config.warn.value); } - if ((config.warn.is_percentage && (percent_used >= (100 - config.warn.value))) || - config.warn.value >= data.metrics.free) { - data.statusCode = max_state (data.statusCode, STATE_WARNING); - } + if ((config.warn.is_percentage && + (percent_used >= (100 - config.warn.value))) || + config.warn.value >= data.metrics.free) { + data.statusCode = max_state(data.statusCode, STATE_WARNING); + } - if ((config.crit.is_percentage && (percent_used >= (100 - config.crit.value))) || - config.crit.value >= data.metrics.free) { - data.statusCode = max_state (data.statusCode, STATE_CRITICAL); - } + if (config.verbose > 1) { + printf("Crit threshold value: %" PRIu64 "\n", config.crit.value); + } - printf (_("SWAP %s - %g%% free (%lluMB out of %lluMB) %s|%s\n"), - state_text (data.statusCode), - (100 - percent_used), data.metrics.free, data.metrics.total, status, - perfdata); + if ((config.crit.is_percentage && + (percent_used >= (100 - config.crit.value))) || + config.crit.value >= data.metrics.free) { + data.statusCode = max_state(data.statusCode, STATE_CRITICAL); + } + + printf(_("SWAP %s - %g%% free (%lluMB out of %lluMB) %s|%s\n"), + state_text(data.statusCode), (100 - percent_used), data.metrics.free, + data.metrics.total, status, perfdata); exit(data.statusCode); } /* process command-line arguments */ -swap_config_wrapper process_arguments (swap_config_wrapper conf_wrapper, int argc, char **argv) { +swap_config_wrapper process_arguments(swap_config_wrapper conf_wrapper, + int argc, char **argv) { if (argc < 2) { conf_wrapper.errorcode = ERROR; return conf_wrapper; } int option = 0; - static struct option longopts[] = { - {"warning", required_argument, 0, 'w'}, - {"critical", required_argument, 0, 'c'}, - {"allswaps", no_argument, 0, 'a'}, - {"no-swap", required_argument, 0, 'n'}, - {"verbose", no_argument, 0, 'v'}, - {"version", no_argument, 0, 'V'}, - {"help", no_argument, 0, 'h'}, - {0, 0, 0, 0} - }; - - int c = 0; /* option character */ + static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, + {"critical", required_argument, 0, 'c'}, + {"allswaps", no_argument, 0, 'a'}, + {"no-swap", required_argument, 0, 'n'}, + {"verbose", no_argument, 0, 'v'}, + {"version", no_argument, 0, 'V'}, + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0}}; + + int c = 0; /* option character */ while (true) { - c = getopt_long (argc, argv, "+?Vvhac:w:n:", longopts, &option); + c = getopt_long(argc, argv, "+?Vvhac:w:n:", longopts, &option); if (c == -1 || c == EOF) break; switch (c) { case 'w': /* warning size threshold */ - { - /* - * We expect either a positive integer value without a unit, which means - * the unit is Bytes or a positive integer value and a percentage sign (%), - * which means the value must be with 0 and 100 and is relative to the total swap - */ - size_t length; - length = strlen(optarg); - - if (optarg[length - 1] == '%') { - /* It's percentage */ - conf_wrapper.config.warn.is_percentage = true; - optarg[length - 1] = '\0'; - if (is_uint64(optarg, &conf_wrapper.config.warn.value)) { - if (conf_wrapper.config.warn.value > 100) { - usage4 (_("Warning threshold percentage must be <= 100!")); - } + { + /* + * We expect either a positive integer value without a unit, which + * means the unit is Bytes or a positive integer value and a + * percentage sign (%), which means the value must be with 0 and 100 + * and is relative to the total swap + */ + size_t length; + length = strlen(optarg); + + if (optarg[length - 1] == '%') { + /* It's percentage */ + conf_wrapper.config.warn.is_percentage = true; + optarg[length - 1] = '\0'; + if (is_uint64(optarg, &conf_wrapper.config.warn.value)) { + if (conf_wrapper.config.warn.value > 100) { + usage4( + _("Warning threshold percentage must be <= 100!")); } + } + break; + } else { + /* It's Bytes */ + conf_wrapper.config.warn.is_percentage = false; + if (is_uint64(optarg, &conf_wrapper.config.warn.value)) { break; } else { - /* It's Bytes */ - conf_wrapper.config.warn.is_percentage = false; - if (is_uint64(optarg, &conf_wrapper.config.warn.value)) { - break; - } else { - usage4 (_("Warning threshold be positive integer or percentage!")); - } + usage4(_("Warning threshold be positive integer or " + "percentage!")); } } + } case 'c': /* critical size threshold */ - { - /* - * We expect either a positive integer value without a unit, which means - * the unit is Bytes or a positive integer value and a percentage sign (%), - * which means the value must be with 0 and 100 and is relative to the total swap - */ - size_t length; - length = strlen(optarg); - - if (optarg[length - 1] == '%') { - /* It's percentage */ - conf_wrapper.config.crit.is_percentage = true; - optarg[length - 1] = '\0'; - if (is_uint64(optarg, &conf_wrapper.config.crit.value)) { - if (conf_wrapper.config.crit.value> 100) { - usage4 (_("Critical threshold percentage must be <= 100!")); - } + { + /* + * We expect either a positive integer value without a unit, which + * means the unit is Bytes or a positive integer value and a + * percentage sign (%), which means the value must be with 0 and 100 + * and is relative to the total swap + */ + size_t length; + length = strlen(optarg); + + if (optarg[length - 1] == '%') { + /* It's percentage */ + conf_wrapper.config.crit.is_percentage = true; + optarg[length - 1] = '\0'; + if (is_uint64(optarg, &conf_wrapper.config.crit.value)) { + if (conf_wrapper.config.crit.value > 100) { + usage4( + _("Critical threshold percentage must be <= 100!")); } + } + break; + } else { + /* It's Bytes */ + conf_wrapper.config.crit.is_percentage = false; + if (is_uint64(optarg, &conf_wrapper.config.crit.value)) { break; } else { - /* It's Bytes */ - conf_wrapper.config.crit.is_percentage = false; - if (is_uint64(optarg, &conf_wrapper.config.crit.value)) { - break; - } else { - usage4 (_("Critical threshold be positive integer or percentage!")); - } + usage4(_("Critical threshold be positive integer or " + "percentage!")); } - } - case 'a': /* all swap */ + } + } + case 'a': /* all swap */ conf_wrapper.config.allswaps = true; break; case 'n': - if ((conf_wrapper.config.no_swap_state = mp_translate_state(optarg)) == ERROR) { - usage4 (_("no-swap result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); + if ((conf_wrapper.config.no_swap_state = + mp_translate_state(optarg)) == ERROR) { + usage4(_("no-swap result must be a valid state name (OK, " + "WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); } break; - case 'v': /* verbose */ + case 'v': /* verbose */ conf_wrapper.config.verbose++; break; - case 'V': /* version */ - print_revision (progname, NP_VERSION); - exit (STATE_UNKNOWN); - case 'h': /* help */ - print_help (conf_wrapper.config); - exit (STATE_UNKNOWN); - case '?': /* error */ - usage5 (); + case 'V': /* version */ + print_revision(progname, NP_VERSION); + exit(STATE_UNKNOWN); + case 'h': /* help */ + print_help(conf_wrapper.config); + exit(STATE_UNKNOWN); + case '?': /* error */ + usage5(); } } c = optind; - if (conf_wrapper.config.warn.value == 0 && conf_wrapper.config.crit.value == 0) { + if (conf_wrapper.config.warn.value == 0 && + conf_wrapper.config.crit.value == 0) { conf_wrapper.errorcode = ERROR; return conf_wrapper; - } else if ((conf_wrapper.config.warn.is_percentage == conf_wrapper.config.crit.is_percentage) && - (conf_wrapper.config.warn.value < conf_wrapper.config.crit.value)) { - /* This is NOT triggered if warn and crit are different units, e.g warn is percentage - * and crit is absolute. We cannot determine the condition at this point since we - * dont know the value of total swap yet + } else if ((conf_wrapper.config.warn.is_percentage == + conf_wrapper.config.crit.is_percentage) && + (conf_wrapper.config.warn.value >= + conf_wrapper.config.crit.value)) { + /* This is NOT triggered if warn and crit are different units, e.g warn + * is percentage and crit is absolute. We cannot determine the condition + * at this point since we dont know the value of total swap yet */ usage4(_("Warning should be more than critical")); } @@ -323,52 +335,57 @@ swap_config_wrapper process_arguments (swap_config_wrapper conf_wrapper, int arg return conf_wrapper; } +void print_help(swap_config config) { + print_revision(progname, NP_VERSION); -void -print_help (swap_config config) -{ - print_revision (progname, NP_VERSION); + printf(_(COPYRIGHT), copyright, email); - printf (_(COPYRIGHT), copyright, email); + printf("%s\n", _("Check swap space on local machine.")); - printf ("%s\n", _("Check swap space on local machine.")); - - printf ("\n\n"); + printf("\n\n"); print_usage(); - printf (UT_HELP_VRSN); - printf (UT_EXTRA_OPTS); - - printf (" %s\n", "-w, --warning=INTEGER"); - printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of swap space are free")); - printf (" %s\n", "-w, --warning=PERCENT%"); - printf (" %s\n", _("Exit with WARNING status if less than PERCENT of swap space is free")); - printf (" %s\n", "-c, --critical=INTEGER"); - printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of swap space are free")); - printf (" %s\n", "-c, --critical=PERCENT%"); - printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of swap space is free")); - printf (" %s\n", "-a, --allswaps"); - printf (" %s\n", _("Conduct comparisons for all swap partitions, one by one")); - printf (" %s\n", "-n, --no-swap="); - printf (" %s %s\n", _("Resulting state when there is no swap regardless of thresholds. Default:"), state_text(config.no_swap_state)); - printf (UT_VERBOSE); - - printf ("\n"); - printf ("%s\n", _("Notes:")); - printf (" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, they are all checked.")); - printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.")); - - printf (UT_SUPPORT); + printf(UT_HELP_VRSN); + printf(UT_EXTRA_OPTS); + + printf(" %s\n", "-w, --warning=INTEGER"); + printf(" %s\n", _("Exit with WARNING status if less than INTEGER bytes " + "of swap space are free")); + printf(" %s\n", "-w, --warning=PERCENT%"); + printf(" %s\n", _("Exit with WARNING status if less than PERCENT of " + "swap space is free")); + printf(" %s\n", "-c, --critical=INTEGER"); + printf(" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes " + "of swap space are free")); + printf(" %s\n", "-c, --critical=PERCENT%"); + printf(" %s\n", _("Exit with CRITICAL status if less than PERCENT of " + "swap space is free")); + printf(" %s\n", "-a, --allswaps"); + printf(" %s\n", + _("Conduct comparisons for all swap partitions, one by one")); + printf(" %s\n", "-n, --no-swap="); + printf(" %s %s\n", + _("Resulting state when there is no swap regardless of thresholds. " + "Default:"), + state_text(config.no_swap_state)); + printf(UT_VERBOSE); + + printf("\n"); + printf("%s\n", _("Notes:")); + printf(" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, " + "they are all checked.")); + printf( + " %s\n", + _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.")); + + printf(UT_SUPPORT); } - -void -print_usage () -{ - printf ("%s\n", _("Usage:")); - printf (" %s [-av] -w %% -c %%\n",progname); - printf (" -w -c [-n ]\n"); +void print_usage() { + printf("%s\n", _("Usage:")); + printf(" %s [-av] -w %% -c %%\n", progname); + printf(" -w -c [-n ]\n"); } #ifdef HAVE_PROC_MEMINFO @@ -379,10 +396,9 @@ swap_result getSwapFromProcMeminfo(swap_config config) { } FILE *fp; - fp = fopen (PROC_MEMINFO, "r"); - + fp = fopen(PROC_MEMINFO, "r"); - swap_result result = { 0 }; + swap_result result = {0}; result.statusCode = STATE_OK; uint64_t swap_total = 0, swap_used = 0, swap_free = 0; @@ -390,13 +406,14 @@ swap_result getSwapFromProcMeminfo(swap_config config) { char input_buffer[MAX_INPUT_BUFFER]; char str[32]; - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { + while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) { uint64_t tmp_KB = 0; /* - * The following sscanf call looks for a line looking like: "Swap: 123 123 123" - * On which kind of system this format exists, I can not say, but I wanted to - * document this for people who are not adapt with sscanf anymore, like me + * The following sscanf call looks for a line looking like: "Swap: 123 + * 123 123" On which kind of system this format exists, I can not say, + * but I wanted to document this for people who are not adapt with + * sscanf anymore, like me */ if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &swap_total, &swap_used, &swap_free) == 3) { @@ -440,7 +457,7 @@ swap_result getSwapFromProcMeminfo(swap_config config) { #ifdef HAVE_SWAP swap_result getSwapFromSwapCommand() { - swap_result result = { 0 }; + swap_result result = {0}; char *temp_buffer; char *swap_command; @@ -450,128 +467,135 @@ swap_result getSwapFromSwapCommand() { xasprintf(&swap_command, "%s", SWAP_COMMAND); xasprintf(&swap_format, "%s", SWAP_FORMAT); -/* These override the command used if a summary (and thus ! allswaps) is required */ -/* The summary flag returns more accurate information about swap usage on these OSes */ -# ifdef _AIX +/* These override the command used if a summary (and thus ! allswaps) is + * required */ +/* The summary flag returns more accurate information about swap usage on these + * OSes */ +#ifdef _AIX if (!allswaps) { xasprintf(&swap_command, "%s", "/usr/sbin/lsps -s"); xasprintf(&swap_format, "%s", "%lu%*s %lu"); conv_factor = 1; } -# endif +#endif if (verbose >= 2) - printf (_("Command: %s\n"), swap_command); + printf(_("Command: %s\n"), swap_command); if (verbose >= 3) - printf (_("Format: %s\n"), swap_format); + printf(_("Format: %s\n"), swap_format); - child_process = spopen (swap_command); + child_process = spopen(swap_command); if (child_process == NULL) { - printf (_("Could not open pipe: %s\n"), swap_command); + printf(_("Could not open pipe: %s\n"), swap_command); return STATE_UNKNOWN; } - child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); + child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); if (child_stderr == NULL) - printf (_("Could not open stderr for %s\n"), swap_command); + printf(_("Could not open stderr for %s\n"), swap_command); - sprintf (str, "%s", ""); + sprintf(str, "%s", ""); /* read 1st line */ - fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); - if (strcmp (swap_format, "") == 0) { - temp_buffer = strtok (input_buffer, " \n"); + fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); + if (strcmp(swap_format, "") == 0) { + temp_buffer = strtok(input_buffer, " \n"); while (temp_buffer) { - if (strstr (temp_buffer, "blocks")) - sprintf (str, "%s %s", str, "%lu"); - else if (strstr (temp_buffer, "dskfree")) - sprintf (str, "%s %s", str, "%lu"); + if (strstr(temp_buffer, "blocks")) + sprintf(str, "%s %s", str, "%lu"); + else if (strstr(temp_buffer, "dskfree")) + sprintf(str, "%s %s", str, "%lu"); else - sprintf (str, "%s %s", str, "%*s"); - temp_buffer = strtok (NULL, " \n"); + sprintf(str, "%s %s", str, "%*s"); + temp_buffer = strtok(NULL, " \n"); } } -/* If different swap command is used for summary switch, need to read format differently */ -# ifdef _AIX +/* If different swap command is used for summary switch, need to read format + * differently */ +#ifdef _AIX if (!allswaps) { - fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */ - sscanf (input_buffer, swap_format, &total_swap_mb, &used_swap_mb); - free_swap_mb = total_swap_mb * (100 - used_swap_mb) /100; + fgets(input_buffer, MAX_INPUT_BUFFER - 1, + child_process); /* Ignore first line */ + sscanf(input_buffer, swap_format, &total_swap_mb, &used_swap_mb); + free_swap_mb = total_swap_mb * (100 - used_swap_mb) / 100; used_swap_mb = total_swap_mb - free_swap_mb; if (verbose >= 3) - printf (_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb, free_swap_mb); + printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, + used_swap_mb, free_swap_mb); } else { -# endif - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { - sscanf (input_buffer, swap_format, &dsktotal_mb, &dskfree_mb); +#endif + while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { + sscanf(input_buffer, swap_format, &dsktotal_mb, &dskfree_mb); dsktotal_mb = dsktotal_mb / conv_factor; /* AIX lists percent used, so this converts to dskfree in MBs */ -# ifdef _AIX +#ifdef _AIX dskfree_mb = dsktotal_mb * (100 - dskfree_mb) / 100; -# else - dskfree_mb = dskfree_mb / conv_factor; -# endif +#else + dskfree_mb = dskfree_mb / conv_factor; +#endif if (verbose >= 3) - printf (_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb); + printf(_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb); dskused_mb = dsktotal_mb - dskfree_mb; total_swap_mb += dsktotal_mb; used_swap_mb += dskused_mb; free_swap_mb += dskfree_mb; if (allswaps) { - percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); - result = max_state (result, check_swap (dskfree_mb, dsktotal_mb)); + percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); + result = max_state(result, check_swap(dskfree_mb, dsktotal_mb)); if (verbose) - xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); + xasprintf(&status, "%s [%.0f (%d%%)]", status, dskfree_mb, + 100 - percent); } } -# ifdef _AIX +#ifdef _AIX } -# endif +#endif /* If we get anything on STDERR, at least set warning */ - while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) - result = max_state (result, STATE_WARNING); + while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) + result = max_state(result, STATE_WARNING); /* close stderr */ - (void) fclose (child_stderr); + (void)fclose(child_stderr); /* close the pipe */ - if (spclose (child_process)) - result = max_state (result, STATE_WARNING); + if (spclose(child_process)) + result = max_state(result, STATE_WARNING); } #endif // HAVE_SWAP #ifdef CHECK_SWAP_SWAPCTL_BSD swap_result getSwapFromSwapctl_BSD() { - int i=0, nswaps=0, swapctl_res=0; + int i = 0, nswaps = 0, swapctl_res = 0; struct swapent *ent; int conv_factor = SWAP_CONVERSION; /* get the number of active swap devices */ - nswaps=swapctl(SWAP_NSWAP, NULL, 0); + nswaps = swapctl(SWAP_NSWAP, NULL, 0); /* initialize swap table + entries */ - ent=(struct swapent*)malloc(sizeof(struct swapent)*nswaps); + ent = (struct swapent *)malloc(sizeof(struct swapent) * nswaps); /* and now, tally 'em up */ - swapctl_res=swapctl(SWAP_STATS, ent, nswaps); - if(swapctl_res < 0){ + swapctl_res = swapctl(SWAP_STATS, ent, nswaps); + if (swapctl_res < 0) { perror(_("swapctl failed: ")); die(STATE_UNKNOWN, _("Error in swapctl call\n")); } - for(i=0;i 0){ - percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); - result = max_state (result, check_swap(dskfree_mb, dsktotal_mb)); + if (allswaps && dsktotal_mb > 0) { + percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); + result = max_state(result, check_swap(dskfree_mb, dsktotal_mb)); if (verbose) { - xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); + xasprintf(&status, "%s [%.0f (%d%%)]", status, dskfree_mb, + 100 - percent); } } @@ -587,52 +611,55 @@ swap_result getSwapFromSwapctl_BSD() { #ifdef CHECK_SWAP_SWAPCTL_SVR4 swap_result getSwapFromSwap_SRV4() { - int i=0, nswaps=0, swapctl_res=0; - swaptbl_t *tbl=NULL; - swapent_t *ent=NULL; + int i = 0, nswaps = 0, swapctl_res = 0; + swaptbl_t *tbl = NULL; + swapent_t *ent = NULL; /* get the number of active swap devices */ - if((nswaps=swapctl(SC_GETNSWP, NULL))== -1) - die(STATE_UNKNOWN, _("Error getting swap devices\n") ); + if ((nswaps = swapctl(SC_GETNSWP, NULL)) == -1) + die(STATE_UNKNOWN, _("Error getting swap devices\n")); - if(nswaps == 0) + if (nswaps == 0) die(STATE_OK, _("SWAP OK: No swap devices defined\n")); - if(verbose >= 3) + if (verbose >= 3) printf("Found %d swap device(s)\n", nswaps); /* initialize swap table + entries */ - tbl=(swaptbl_t*)malloc(sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps)); + tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); - if(tbl==NULL) + if (tbl == NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); - memset(tbl, 0, sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps)); - tbl->swt_n=nswaps; - for(i=0;iswt_ent[i].ste_path=(char*)malloc(sizeof(char)*MAXPATHLEN)) == NULL) + memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); + tbl->swt_n = nswaps; + for (i = 0; i < nswaps; i++) { + if ((tbl->swt_ent[i].ste_path = + (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); } /* and now, tally 'em up */ - swapctl_res=swapctl(SC_LIST, tbl); - if(swapctl_res < 0){ + swapctl_res = swapctl(SC_LIST, tbl); + if (swapctl_res < 0) { perror(_("swapctl failed: ")); die(STATE_UNKNOWN, _("Error in swapctl call\n")); } - for(i=0;iswt_ent[i].ste_pages / SWAP_CONVERSION; - dskfree_mb = (float) tbl->swt_ent[i].ste_free / SWAP_CONVERSION; - dskused_mb = ( dsktotal_mb - dskfree_mb ); + for (i = 0; i < nswaps; i++) { + dsktotal_mb = (float)tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; + dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION; + dskused_mb = (dsktotal_mb - dskfree_mb); if (verbose >= 3) - printf ("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb); + printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", + dsktotal_mb, dskfree_mb, dskused_mb); - if(allswaps && dsktotal_mb > 0){ - percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); - result = max_state (result, check_swap (dskfree_mb, dsktotal_mb)); + if (allswaps && dsktotal_mb > 0) { + percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); + result = max_state(result, check_swap(dskfree_mb, dsktotal_mb)); if (verbose) { - xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); + xasprintf(&status, "%s [%.0f (%d%%)]", status, dskfree_mb, + 100 - percent); } } @@ -642,7 +669,7 @@ swap_result getSwapFromSwap_SRV4() { } /* and clean up after ourselves */ - for(i=0;iswt_ent[i].ste_path); } free(tbl); -- cgit v1.2.3-74-g34f1 From 8cdcb1a8ad5d7ba13ca19493f333605dcca33287 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 13 Dec 2023 17:25:29 +0100 Subject: Update copyright --- plugins/check_swap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 40ac2ed3..4bad2100 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -4,7 +4,7 @@ * * License: GPL * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) - * Copyright (c) 2000-2007 Monitoring Plugins Development Team + * Copyright (c) 2000-2023 Monitoring Plugins Development Team * * Description: * @@ -28,7 +28,7 @@ *****************************************************************************/ const char *progname = "check_swap"; -const char *copyright = "2000-2007"; +const char *copyright = "2000-2023"; const char *email = "devel@monitoring-plugins.org"; #include "common.h" -- cgit v1.2.3-74-g34f1 From 6011cdf5547a815e30a2c4d69e53cabac9fcd10d Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 13 Dec 2023 17:40:18 +0100 Subject: check_swap: hopefully fix SRV4 stuff --- plugins/check_swap.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 4bad2100..921aaac5 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -610,10 +610,12 @@ swap_result getSwapFromSwapctl_BSD() { #endif // CHECK_SWAP_SWAPCTL_BSD #ifdef CHECK_SWAP_SWAPCTL_SVR4 -swap_result getSwapFromSwap_SRV4() { +swap_result getSwapFromSwap_SRV4(swap_config config) { int i = 0, nswaps = 0, swapctl_res = 0; - swaptbl_t *tbl = NULL; - swapent_t *ent = NULL; + //swaptbl_t *tbl = NULL; + void*tbl = NULL; + //swapent_t *ent = NULL; + void*ent = NULL; /* get the number of active swap devices */ if ((nswaps = swapctl(SC_GETNSWP, NULL)) == -1) die(STATE_UNKNOWN, _("Error getting swap devices\n")); @@ -621,7 +623,7 @@ swap_result getSwapFromSwap_SRV4() { if (nswaps == 0) die(STATE_OK, _("SWAP OK: No swap devices defined\n")); - if (verbose >= 3) + if (config.verbose >= 3) printf("Found %d swap device(s)\n", nswaps); /* initialize swap table + entries */ @@ -645,21 +647,23 @@ swap_result getSwapFromSwap_SRV4() { die(STATE_UNKNOWN, _("Error in swapctl call\n")); } + double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; + unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; + for (i = 0; i < nswaps; i++) { dsktotal_mb = (float)tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION; dskused_mb = (dsktotal_mb - dskfree_mb); - if (verbose >= 3) + if (config.verbose >= 3) printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb); - if (allswaps && dsktotal_mb > 0) { - percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); - result = max_state(result, check_swap(dskfree_mb, dsktotal_mb)); - if (verbose) { - xasprintf(&status, "%s [%.0f (%d%%)]", status, dskfree_mb, - 100 - percent); + if (config.allswaps && dsktotal_mb > 0) { + double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); + + if (config.verbose) { + printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); } } @@ -673,5 +677,13 @@ swap_result getSwapFromSwap_SRV4() { free(tbl->swt_ent[i].ste_path); } free(tbl); + + swap_result result = {0}; + result.errorcode = OK; + result.metrics.total = total_swap_mb * 1024 * 1024; + result.metrics.free = free_swap_mb * 1024 * 1024; + result.metrics.used = used_swap_mb * 1024 * 1024; + + return result; } #endif // CHECK_SWAP_SWAPCTL_SVR4 -- cgit v1.2.3-74-g34f1 From 2289d094ae00a52f96a038cb7ca8bb350aec483a Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 13 Dec 2023 17:45:13 +0100 Subject: check_swap: Hopefully fix stuff on BSD --- plugins/check_swap.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 921aaac5..ac74931a 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -567,7 +567,7 @@ swap_result getSwapFromSwapCommand() { #endif // HAVE_SWAP #ifdef CHECK_SWAP_SWAPCTL_BSD -swap_result getSwapFromSwapctl_BSD() { +swap_result getSwapFromSwapctl_BSD(swap_config config) { int i = 0, nswaps = 0, swapctl_res = 0; struct swapent *ent; int conv_factor = SWAP_CONVERSION; @@ -585,17 +585,20 @@ swap_result getSwapFromSwapctl_BSD() { die(STATE_UNKNOWN, _("Error in swapctl call\n")); } + + double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; + unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; + for (i = 0; i < nswaps; i++) { dsktotal_mb = (float)ent[i].se_nblks / conv_factor; dskused_mb = (float)ent[i].se_inuse / conv_factor; dskfree_mb = (dsktotal_mb - dskused_mb); - if (allswaps && dsktotal_mb > 0) { - percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); - result = max_state(result, check_swap(dskfree_mb, dsktotal_mb)); - if (verbose) { - xasprintf(&status, "%s [%.0f (%d%%)]", status, dskfree_mb, - 100 - percent); + if (config.allswaps && dsktotal_mb > 0) { + double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); + + if (config.verbose) { + printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); } } @@ -606,6 +609,17 @@ swap_result getSwapFromSwapctl_BSD() { /* and clean up after ourselves */ free(ent); + + swap_result result = {0}; + + result.statusCode = OK; + result.errorcode = OK; + + result.metrics.total = total_swap_mb * 1024 * 1024; + result.metrics.free = free_swap_mb * 1024 * 1024; + result.metrics.used = used_swap_mb * 1024 * 1024; + + return result; } #endif // CHECK_SWAP_SWAPCTL_BSD -- cgit v1.2.3-74-g34f1 From 2dec5182c508bd3cc286b4649836ead51aec50ef Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 19 Dec 2023 11:18:51 +0100 Subject: check_swap: refactor to improve readability --- .gitignore | 4 + configure.ac | 2 +- plugins/Makefile.am | 2 +- plugins/check_swap.c | 380 +------------------------------------- plugins/check_swap.d/check_swap.h | 45 +++++ plugins/check_swap.d/swap.c | 359 +++++++++++++++++++++++++++++++++++ 6 files changed, 414 insertions(+), 378 deletions(-) create mode 100644 plugins/check_swap.d/check_swap.h create mode 100644 plugins/check_swap.d/swap.c (limited to 'plugins') diff --git a/.gitignore b/.gitignore index 6f903d61..7e640a0e 100644 --- a/.gitignore +++ b/.gitignore @@ -223,6 +223,10 @@ NP-VERSION-FILE /plugins/tests/test_disk /plugins/tests/.deps +# /plugins/check_swap.d +/plugins/check_swap.d/.deps +/plugins/check_swap.d/.dirstamp + # /plugins-root/ /plugins-root/.deps /plugins-root/.libs diff --git a/configure.ac b/configure.ac index 17272e45..42fc0292 100644 --- a/configure.ac +++ b/configure.ac @@ -4,7 +4,7 @@ AC_INIT(monitoring-plugins,2.3git) AC_CONFIG_SRCDIR(NPTest.pm) AC_CONFIG_FILES([gl/Makefile]) AC_CONFIG_AUX_DIR(build-aux) -AM_INIT_AUTOMAKE([1.8.3]) +AM_INIT_AUTOMAKE([1.8.3 subdir-objects]) AM_SILENT_RULES([yes]) AM_MAINTAINER_MODE([enable]) AC_CONFIG_HEADERS([config.h]) diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 49086b7a..1ca7cf3f 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -107,7 +107,7 @@ check_real_LDADD = $(NETLIBS) check_snmp_LDADD = $(BASEOBJS) check_smtp_LDADD = $(SSLOBJS) check_ssh_LDADD = $(NETLIBS) -check_swap_LDADD = $(MATHLIBS) $(BASEOBJS) +check_swap_LDADD = $(MATHLIBS) $(BASEOBJS) check_swap.d/swap.o check_tcp_LDADD = $(SSLOBJS) check_time_LDADD = $(NETLIBS) check_ntp_time_LDADD = $(NETLIBS) $(MATHLIBS) 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"; const char *copyright = "2000-2023"; const char *email = "devel@monitoring-plugins.org"; -#include "common.h" -#include "popen.h" -#include "utils.h" #ifdef HAVE_DECL_SWAPCTL #ifdef HAVE_SYS_PARAM_H @@ -47,34 +44,8 @@ const char *email = "devel@monitoring-plugins.org"; #endif #endif -#ifndef SWAP_CONVERSION -#define SWAP_CONVERSION 1 -#endif - -typedef struct { - bool is_percentage; - uint64_t value; -} threshold; - -typedef struct { - unsigned long long free; // Free swap in Bytes! - unsigned long long used; // Used swap in Bytes! - unsigned long long total; // Total swap size, you guessed it, in Bytes! -} swap_metrics; - -typedef struct { - int errorcode; - int statusCode; - swap_metrics metrics; -} swap_result; - -typedef struct { - int verbose; - bool allswaps; - int no_swap_state; - threshold warn; - threshold crit; -} swap_config; +#include "./check_swap.d/check_swap.h" +#include "./utils.h" typedef struct { int errorcode; @@ -86,19 +57,6 @@ swap_config_wrapper process_arguments(swap_config_wrapper config, int argc, void print_usage(); void print_help(swap_config); -swap_result getSwapFromProcMeminfo(swap_config config); -swap_result getSwapFromSwapCommand(swap_config config); -swap_result getSwapFromSwapctl_BSD(swap_config config); -swap_result getSwapFromSwap_SRV4(swap_config config); - -swap_config swap_config_init() { - swap_config tmp = {0}; - tmp.allswaps = false; - tmp.no_swap_state = STATE_CRITICAL; - tmp.verbose = 0; - - return tmp; -} int main(int argc, char **argv) { setlocale(LC_ALL, ""); @@ -123,23 +81,7 @@ int main(int argc, char **argv) { swap_config config = tmp.config; -#ifdef HAVE_PROC_MEMINFO - swap_result data = getSwapFromProcMeminfo(config); -#else -#ifdef HAVE_SWAP - swap_result data = getSwapFromSwapCommand(); -#else -#ifdef CHECK_SWAP_SWAPCTL_SVR4 - swap_result data = getSwapFromSwapctl_SRV4(); -#else -#ifdef CHECK_SWAP_SWAPCTL_BSD - swap_result data = getSwapFromSwapctl_BSD(); -#else -#error No way found to retrieve swap -#endif /* CHECK_SWAP_SWAPCTL_BSD */ -#endif /* CHECK_SWAP_SWAPCTL_SVR4 */ -#endif /* HAVE_SWAP */ -#endif /* HAVE_PROC_MEMINFO */ + swap_result data = get_swap_data(config); double percent_used; @@ -323,7 +265,7 @@ swap_config_wrapper process_arguments(swap_config_wrapper conf_wrapper, return conf_wrapper; } else if ((conf_wrapper.config.warn.is_percentage == conf_wrapper.config.crit.is_percentage) && - (conf_wrapper.config.warn.value >= + (conf_wrapper.config.warn.value < conf_wrapper.config.crit.value)) { /* This is NOT triggered if warn and crit are different units, e.g warn * is percentage and crit is absolute. We cannot determine the condition @@ -387,317 +329,3 @@ void print_usage() { printf(" %s [-av] -w %% -c %%\n", progname); printf(" -w -c [-n ]\n"); } - -#ifdef HAVE_PROC_MEMINFO -swap_result getSwapFromProcMeminfo(swap_config config) { - - if (config.verbose >= 3) { - printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO); - } - - FILE *fp; - fp = fopen(PROC_MEMINFO, "r"); - - swap_result result = {0}; - result.statusCode = STATE_OK; - - uint64_t swap_total = 0, swap_used = 0, swap_free = 0; - - char input_buffer[MAX_INPUT_BUFFER]; - char str[32]; - - while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) { - uint64_t tmp_KB = 0; - - /* - * The following sscanf call looks for a line looking like: "Swap: 123 - * 123 123" On which kind of system this format exists, I can not say, - * but I wanted to document this for people who are not adapt with - * sscanf anymore, like me - */ - if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", - &swap_total, &swap_used, &swap_free) == 3) { - - result.metrics.total += swap_total; - result.metrics.used += swap_used; - result.metrics.free += swap_free; - - /* - * The following sscanf call looks for lines looking like: - * "SwapTotal: 123" and "SwapFree: 123" This format exists at least - * on Debian Linux with a 5.* kernel - */ - } else if (sscanf(input_buffer, - "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " - "%*[k]%*[B]", - str, &tmp_KB)) { - if (config.verbose >= 3) { - printf("Got %s with %lu\n", str, tmp_KB); - } - /* I think this part is always in Kb, so convert to mb */ - if (strcmp("Total", str) == 0) { - swap_total = tmp_KB * 1024; - } else if (strcmp("Free", str) == 0) { - swap_free = swap_free + tmp_KB * 1024; - } else if (strcmp("Cached", str) == 0) { - swap_free = swap_free + tmp_KB * 1024; - } - } - } - - fclose(fp); - - result.metrics.total = swap_total; - result.metrics.used = swap_total - swap_free; - result.metrics.free = swap_free; - - return result; -} -#endif - -#ifdef HAVE_SWAP -swap_result getSwapFromSwapCommand() { - swap_result result = {0}; - - char *temp_buffer; - char *swap_command; - char *swap_format; - int conv_factor = SWAP_CONVERSION; - - xasprintf(&swap_command, "%s", SWAP_COMMAND); - xasprintf(&swap_format, "%s", SWAP_FORMAT); - -/* These override the command used if a summary (and thus ! allswaps) is - * required */ -/* The summary flag returns more accurate information about swap usage on these - * OSes */ -#ifdef _AIX - if (!allswaps) { - xasprintf(&swap_command, "%s", "/usr/sbin/lsps -s"); - xasprintf(&swap_format, "%s", "%lu%*s %lu"); - conv_factor = 1; - } -#endif - - if (verbose >= 2) - printf(_("Command: %s\n"), swap_command); - if (verbose >= 3) - printf(_("Format: %s\n"), swap_format); - - child_process = spopen(swap_command); - if (child_process == NULL) { - printf(_("Could not open pipe: %s\n"), swap_command); - return STATE_UNKNOWN; - } - - child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); - if (child_stderr == NULL) - printf(_("Could not open stderr for %s\n"), swap_command); - - sprintf(str, "%s", ""); - /* read 1st line */ - fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); - if (strcmp(swap_format, "") == 0) { - temp_buffer = strtok(input_buffer, " \n"); - while (temp_buffer) { - if (strstr(temp_buffer, "blocks")) - sprintf(str, "%s %s", str, "%lu"); - else if (strstr(temp_buffer, "dskfree")) - sprintf(str, "%s %s", str, "%lu"); - else - sprintf(str, "%s %s", str, "%*s"); - temp_buffer = strtok(NULL, " \n"); - } - } - -/* If different swap command is used for summary switch, need to read format - * differently */ -#ifdef _AIX - if (!allswaps) { - fgets(input_buffer, MAX_INPUT_BUFFER - 1, - child_process); /* Ignore first line */ - sscanf(input_buffer, swap_format, &total_swap_mb, &used_swap_mb); - free_swap_mb = total_swap_mb * (100 - used_swap_mb) / 100; - used_swap_mb = total_swap_mb - free_swap_mb; - if (verbose >= 3) - printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, - used_swap_mb, free_swap_mb); - } else { -#endif - while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { - sscanf(input_buffer, swap_format, &dsktotal_mb, &dskfree_mb); - - dsktotal_mb = dsktotal_mb / conv_factor; - /* AIX lists percent used, so this converts to dskfree in MBs */ -#ifdef _AIX - dskfree_mb = dsktotal_mb * (100 - dskfree_mb) / 100; -#else - dskfree_mb = dskfree_mb / conv_factor; -#endif - if (verbose >= 3) - printf(_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb); - - dskused_mb = dsktotal_mb - dskfree_mb; - total_swap_mb += dsktotal_mb; - used_swap_mb += dskused_mb; - free_swap_mb += dskfree_mb; - if (allswaps) { - percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); - result = max_state(result, check_swap(dskfree_mb, dsktotal_mb)); - if (verbose) - xasprintf(&status, "%s [%.0f (%d%%)]", status, dskfree_mb, - 100 - percent); - } - } -#ifdef _AIX - } -#endif - - /* If we get anything on STDERR, at least set warning */ - while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) - result = max_state(result, STATE_WARNING); - - /* close stderr */ - (void)fclose(child_stderr); - - /* close the pipe */ - if (spclose(child_process)) - result = max_state(result, STATE_WARNING); -} -#endif // HAVE_SWAP - -#ifdef CHECK_SWAP_SWAPCTL_BSD -swap_result getSwapFromSwapctl_BSD(swap_config config) { - int i = 0, nswaps = 0, swapctl_res = 0; - struct swapent *ent; - int conv_factor = SWAP_CONVERSION; - - /* get the number of active swap devices */ - nswaps = swapctl(SWAP_NSWAP, NULL, 0); - - /* initialize swap table + entries */ - ent = (struct swapent *)malloc(sizeof(struct swapent) * nswaps); - - /* and now, tally 'em up */ - swapctl_res = swapctl(SWAP_STATS, ent, nswaps); - if (swapctl_res < 0) { - perror(_("swapctl failed: ")); - die(STATE_UNKNOWN, _("Error in swapctl call\n")); - } - - - double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; - unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; - - for (i = 0; i < nswaps; i++) { - dsktotal_mb = (float)ent[i].se_nblks / conv_factor; - dskused_mb = (float)ent[i].se_inuse / conv_factor; - dskfree_mb = (dsktotal_mb - dskused_mb); - - if (config.allswaps && dsktotal_mb > 0) { - double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); - - if (config.verbose) { - printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); - } - } - - total_swap_mb += dsktotal_mb; - free_swap_mb += dskfree_mb; - used_swap_mb += dskused_mb; - } - - /* and clean up after ourselves */ - free(ent); - - swap_result result = {0}; - - result.statusCode = OK; - result.errorcode = OK; - - result.metrics.total = total_swap_mb * 1024 * 1024; - result.metrics.free = free_swap_mb * 1024 * 1024; - result.metrics.used = used_swap_mb * 1024 * 1024; - - return result; -} -#endif // CHECK_SWAP_SWAPCTL_BSD - -#ifdef CHECK_SWAP_SWAPCTL_SVR4 -swap_result getSwapFromSwap_SRV4(swap_config config) { - int i = 0, nswaps = 0, swapctl_res = 0; - //swaptbl_t *tbl = NULL; - void*tbl = NULL; - //swapent_t *ent = NULL; - void*ent = NULL; - /* get the number of active swap devices */ - if ((nswaps = swapctl(SC_GETNSWP, NULL)) == -1) - die(STATE_UNKNOWN, _("Error getting swap devices\n")); - - if (nswaps == 0) - die(STATE_OK, _("SWAP OK: No swap devices defined\n")); - - if (config.verbose >= 3) - printf("Found %d swap device(s)\n", nswaps); - - /* initialize swap table + entries */ - tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); - - if (tbl == NULL) - die(STATE_UNKNOWN, _("malloc() failed!\n")); - - memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); - tbl->swt_n = nswaps; - for (i = 0; i < nswaps; i++) { - if ((tbl->swt_ent[i].ste_path = - (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL) - die(STATE_UNKNOWN, _("malloc() failed!\n")); - } - - /* and now, tally 'em up */ - swapctl_res = swapctl(SC_LIST, tbl); - if (swapctl_res < 0) { - perror(_("swapctl failed: ")); - die(STATE_UNKNOWN, _("Error in swapctl call\n")); - } - - double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; - unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; - - for (i = 0; i < nswaps; i++) { - dsktotal_mb = (float)tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; - dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION; - dskused_mb = (dsktotal_mb - dskfree_mb); - - if (config.verbose >= 3) - printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", - dsktotal_mb, dskfree_mb, dskused_mb); - - if (config.allswaps && dsktotal_mb > 0) { - double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); - - if (config.verbose) { - printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); - } - } - - total_swap_mb += dsktotal_mb; - free_swap_mb += dskfree_mb; - used_swap_mb += dskused_mb; - } - - /* and clean up after ourselves */ - for (i = 0; i < nswaps; i++) { - free(tbl->swt_ent[i].ste_path); - } - free(tbl); - - swap_result result = {0}; - result.errorcode = OK; - result.metrics.total = total_swap_mb * 1024 * 1024; - result.metrics.free = free_swap_mb * 1024 * 1024; - result.metrics.used = used_swap_mb * 1024 * 1024; - - return result; -} -#endif // CHECK_SWAP_SWAPCTL_SVR4 diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h new file mode 100644 index 00000000..bad52917 --- /dev/null +++ b/plugins/check_swap.d/check_swap.h @@ -0,0 +1,45 @@ +#ifndef CHECK_SWAP_H +#define CHECK_SWAP_H + +#include "../common.h" + +#ifndef SWAP_CONVERSION +#define SWAP_CONVERSION 1 +#endif + +typedef struct { + bool is_percentage; + uint64_t value; +} threshold; + +typedef struct { + unsigned long long free; // Free swap in Bytes! + unsigned long long used; // Used swap in Bytes! + unsigned long long total; // Total swap size, you guessed it, in Bytes! +} swap_metrics; + +typedef struct { + int errorcode; + int statusCode; + swap_metrics metrics; +} swap_result; + +typedef struct { + int verbose; + bool allswaps; + int no_swap_state; + threshold warn; + threshold crit; + bool on_aix; + int conversion_factor; +} swap_config; + +swap_config swap_config_init(); + +swap_result get_swap_data(swap_config config); +swap_result getSwapFromProcMeminfo(swap_config config, char path_to_proc_meminfo[]); +swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]); +swap_result getSwapFromSwapctl_BSD(swap_config config); +swap_result getSwapFromSwap_SRV4(swap_config config); + +#endif diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c new file mode 100644 index 00000000..ebfa840d --- /dev/null +++ b/plugins/check_swap.d/swap.c @@ -0,0 +1,359 @@ +#include "./check_swap.d/check_swap.h" + +swap_config swap_config_init() { + swap_config tmp = {0}; + tmp.allswaps = false; + tmp.no_swap_state = STATE_CRITICAL; + tmp.verbose = 0; + tmp.conversion_factor = SWAP_CONVERSION; + +#ifdef _AIX + tmp.on_aix = true; +#else + tmp.on_aix = false; +#endif + + return tmp; +} + +swap_result get_swap_data(swap_config config) { +#ifdef HAVE_PROC_MEMINFO + if (config.verbose >= 3) { + printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO); + } + + return getSwapFromProcMeminfo(config, PROC_MEMINFO); +#else +#ifdef HAVE_SWAP + if (config.verbose >= 3) { + printf("Using swap command %s with format: %s\n", SWAP_COMMAND, SWAP_FORMAT); + } + + /* These override the command used if a summary (and thus ! allswaps) is + * required + * The summary flag returns more accurate information about swap usage on these + * OSes */ + if (config.on_aix && !config.allswaps) { + + config.conversion_factor = 1; + + return getSwapFromSwapCommand(config, "/usr/sbin/lsps -s", "%lu%*s %lu"); + } else { + return getSwapFromSwapCommand(config, SWAP_COMMAND, SWAP_FORMAT); + } +#else +#ifdef CHECK_SWAP_SWAPCTL_SVR4 + return getSwapFromSwapctl_SRV4(); +#else +#ifdef CHECK_SWAP_SWAPCTL_BSD + return getSwapFromSwapctl_BSD(); +#else +#error No way found to retrieve swap +#endif /* CHECK_SWAP_SWAPCTL_BSD */ +#endif /* CHECK_SWAP_SWAPCTL_SVR4 */ +#endif /* HAVE_SWAP */ +#endif /* HAVE_PROC_MEMINFO */ +} + +swap_result getSwapFromProcMeminfo(swap_config config, char proc_meminfo[]) { + FILE *fp; + fp = fopen(proc_meminfo, "r"); + + swap_result result = {0}; + result.statusCode = STATE_OK; + + uint64_t swap_total = 0, swap_used = 0, swap_free = 0; + + char input_buffer[MAX_INPUT_BUFFER]; + char str[32]; + + while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) { + uint64_t tmp_KB = 0; + + /* + * The following sscanf call looks for a line looking like: "Swap: 123 + * 123 123" On which kind of system this format exists, I can not say, + * but I wanted to document this for people who are not adapt with + * sscanf anymore, like me + * Also the units used here are unclear and probably wrong + */ + if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", + &swap_total, &swap_used, &swap_free) == 3) { + + result.metrics.total += swap_total; + result.metrics.used += swap_used; + result.metrics.free += swap_free; + + /* + * The following sscanf call looks for lines looking like: + * "SwapTotal: 123" and "SwapFree: 123" This format exists at least + * on Debian Linux with a 5.* kernel + */ + } else if (sscanf(input_buffer, + "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " + "%*[k]%*[B]", + str, &tmp_KB)) { + + if (config.verbose >= 3) { + printf("Got %s with %lu\n", str, tmp_KB); + } + + /* I think this part is always in Kb, so convert to bytes */ + if (strcmp("Total", str) == 0) { + swap_total = tmp_KB * 1024; + } else if (strcmp("Free", str) == 0) { + swap_free = swap_free + tmp_KB * 1024; + } else if (strcmp("Cached", str) == 0) { + swap_free = swap_free + tmp_KB * 1024; + } + } + } + + fclose(fp); + + result.metrics.total = swap_total; + result.metrics.used = swap_total - swap_free; + result.metrics.free = swap_free; + + return result; +} + +swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]) { + swap_result result = {0}; + + char *temp_buffer; + + if (config.verbose >= 2) + printf(_("Command: %s\n"), swap_command); + if (config.verbose >= 3) + printf(_("Format: %s\n"), swap_format); + + child_process = spopen(swap_command); + if (child_process == NULL) { + printf(_("Could not open pipe: %s\n"), swap_command); + swap_result tmp = { + .errorcode = STATE_UNKNOWN, + }; + return tmp; + } + + child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); + if (child_stderr == NULL) { + printf(_("Could not open stderr for %s\n"), swap_command); + } + + char str[32] = {0}; + char input_buffer[MAX_INPUT_BUFFER]; + + /* read 1st line */ + fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); + if (strcmp(swap_format, "") == 0) { + temp_buffer = strtok(input_buffer, " \n"); + while (temp_buffer) { + if (strstr(temp_buffer, "blocks")) + sprintf(str, "%s %s", str, "%lu"); + else if (strstr(temp_buffer, "dskfree")) + sprintf(str, "%s %s", str, "%lu"); + else + sprintf(str, "%s %s", str, "%*s"); + temp_buffer = strtok(NULL, " \n"); + } + } + + double total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; + double dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0; + + /* + * If different swap command is used for summary switch, need to read format + * differently + */ + if (config.on_aix && !config.allswaps) { + fgets(input_buffer, MAX_INPUT_BUFFER - 1, + child_process); /* Ignore first line */ + sscanf(input_buffer, swap_format, &total_swap_mb, &used_swap_mb); + free_swap_mb = total_swap_mb * (100 - used_swap_mb) / 100; + used_swap_mb = total_swap_mb - free_swap_mb; + + if (config.verbose >= 3) { + printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, + used_swap_mb, free_swap_mb); + } + } else { + while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { + sscanf(input_buffer, swap_format, &dsktotal_mb, &dskfree_mb); + + dsktotal_mb = dsktotal_mb / config.conversion_factor; + /* AIX lists percent used, so this converts to dskfree in MBs */ + + if (config.on_aix) { + dskfree_mb = dsktotal_mb * (100 - dskfree_mb) / 100; + } else { + dskfree_mb = dskfree_mb / config.conversion_factor; + } + + if (config.verbose >= 3) + printf(_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb); + + dskused_mb = dsktotal_mb - dskfree_mb; + total_swap_mb += dsktotal_mb; + used_swap_mb += dskused_mb; + free_swap_mb += dskfree_mb; + } + } + + result.metrics.free = free_swap_mb * 1024 * 1024; + result.metrics.used = used_swap_mb * 1024 * 1024; + result.metrics.total = free_swap_mb * 1024 * 1024; + + /* If we get anything on STDERR, at least set warning */ + while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { + result.statusCode = max_state(result.statusCode, STATE_WARNING); + // TODO Set error here + } + + /* close stderr */ + (void)fclose(child_stderr); + + /* close the pipe */ + if (spclose(child_process)) { + result.statusCode = max_state(result.statusCode, STATE_WARNING); + // TODO set error here + } + + return result; +} + +#ifdef CHECK_SWAP_SWAPCTL_BSD +swap_result getSwapFromSwapctl_BSD(swap_config config) { + int i = 0, nswaps = 0, swapctl_res = 0; + struct swapent *ent; + + /* get the number of active swap devices */ + nswaps = swapctl(SWAP_NSWAP, NULL, 0); + + /* initialize swap table + entries */ + ent = (struct swapent *)malloc(sizeof(struct swapent) * nswaps); + + /* and now, tally 'em up */ + swapctl_res = swapctl(SWAP_STATS, ent, nswaps); + if (swapctl_res < 0) { + perror(_("swapctl failed: ")); + die(STATE_UNKNOWN, _("Error in swapctl call\n")); + } + + + double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; + unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; + + for (i = 0; i < nswaps; i++) { + dsktotal_mb = (float)ent[i].se_nblks / config.conversion_factor; + dskused_mb = (float)ent[i].se_inuse / config.conversion_factor; + dskfree_mb = (dsktotal_mb - dskused_mb); + + if (config.allswaps && dsktotal_mb > 0) { + double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); + + if (config.verbose) { + printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); + } + } + + total_swap_mb += dsktotal_mb; + free_swap_mb += dskfree_mb; + used_swap_mb += dskused_mb; + } + + /* and clean up after ourselves */ + free(ent); + + swap_result result = {0}; + + result.statusCode = OK; + result.errorcode = OK; + + result.metrics.total = total_swap_mb * 1024 * 1024; + result.metrics.free = free_swap_mb * 1024 * 1024; + result.metrics.used = used_swap_mb * 1024 * 1024; + + return result; +} +#endif // CHECK_SWAP_SWAPCTL_BSD + +#ifdef CHECK_SWAP_SWAPCTL_SVR4 +swap_result getSwapFromSwap_SRV4(swap_config config) { + int i = 0, nswaps = 0, swapctl_res = 0; + //swaptbl_t *tbl = NULL; + void*tbl = NULL; + //swapent_t *ent = NULL; + void*ent = NULL; + /* get the number of active swap devices */ + if ((nswaps = swapctl(SC_GETNSWP, NULL)) == -1) + die(STATE_UNKNOWN, _("Error getting swap devices\n")); + + if (nswaps == 0) + die(STATE_OK, _("SWAP OK: No swap devices defined\n")); + + if (config.verbose >= 3) + printf("Found %d swap device(s)\n", nswaps); + + /* initialize swap table + entries */ + tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); + + if (tbl == NULL) + die(STATE_UNKNOWN, _("malloc() failed!\n")); + + memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); + tbl->swt_n = nswaps; + for (i = 0; i < nswaps; i++) { + if ((tbl->swt_ent[i].ste_path = + (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL) + die(STATE_UNKNOWN, _("malloc() failed!\n")); + } + + /* and now, tally 'em up */ + swapctl_res = swapctl(SC_LIST, tbl); + if (swapctl_res < 0) { + perror(_("swapctl failed: ")); + die(STATE_UNKNOWN, _("Error in swapctl call\n")); + } + + double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; + unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; + + for (i = 0; i < nswaps; i++) { + dsktotal_mb = (float)tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; + dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION; + dskused_mb = (dsktotal_mb - dskfree_mb); + + if (config.verbose >= 3) + printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", + dsktotal_mb, dskfree_mb, dskused_mb); + + if (config.allswaps && dsktotal_mb > 0) { + double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); + + if (config.verbose) { + printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); + } + } + + total_swap_mb += dsktotal_mb; + free_swap_mb += dskfree_mb; + used_swap_mb += dskused_mb; + } + + /* and clean up after ourselves */ + for (i = 0; i < nswaps; i++) { + free(tbl->swt_ent[i].ste_path); + } + free(tbl); + + swap_result result = {0}; + result.errorcode = OK; + result.metrics.total = total_swap_mb * 1024 * 1024; + result.metrics.free = free_swap_mb * 1024 * 1024; + result.metrics.used = used_swap_mb * 1024 * 1024; + + return result; +} +#endif // CHECK_SWAP_SWAPCTL_SVR4 -- cgit v1.2.3-74-g34f1 From 6fcbbaafc4bae79d4e674a2cf7f1d87d5a471603 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 19 Dec 2023 11:28:35 +0100 Subject: Implement first unit test for check_swap --- .gitignore | 2 ++ configure.ac | 3 +++ plugins/Makefile.am | 19 ++++++++++++++++--- plugins/tests/test_check_swap.c | 21 +++++++++++++++++++++ plugins/tests/test_check_swap.t | 6 ++++++ 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 plugins/tests/test_check_swap.c create mode 100644 plugins/tests/test_check_swap.t (limited to 'plugins') diff --git a/.gitignore b/.gitignore index 7e640a0e..bbd7d844 100644 --- a/.gitignore +++ b/.gitignore @@ -221,7 +221,9 @@ NP-VERSION-FILE /plugins/tests/Makefile.in /plugins/tests/test_utils /plugins/tests/test_disk +/plugins/tests/test_check_swap /plugins/tests/.deps +/plugins/tests/.dirstamp # /plugins/check_swap.d /plugins/check_swap.d/.deps diff --git a/configure.ac b/configure.ac index 42fc0292..190b43ad 100644 --- a/configure.ac +++ b/configure.ac @@ -185,6 +185,9 @@ fi if test "$enable_libtap" = "yes" ; then EXTRA_TEST="test_utils test_disk test_tcp test_cmd test_base64" AC_SUBST(EXTRA_TEST) + + EXTRA_PLUGIN_TESTS="tests/test_check_swap" + AC_SUBST(EXTRA_PLUGIN_TESTS) fi dnl INI Parsing diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 1ca7cf3f..8ef2a246 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -38,19 +38,27 @@ check_tcp_programs = check_ftp check_imap check_nntp check_pop \ EXTRA_PROGRAMS = check_mysql check_radius check_pgsql check_snmp check_hpjd \ check_swap check_fping check_ldap check_game check_dig \ check_nagios check_by_ssh check_dns check_nt check_ide_smart \ - check_procs check_mysql_query check_apt check_dbi check_curl + check_procs check_mysql_query check_apt check_dbi check_curl \ + \ + tests/test_check_swap SUBDIRS = picohttpparser -EXTRA_DIST = t tests +np_test_scripts = tests/test_check_swap.t + +EXTRA_DIST = t tests $(np_test_scripts) PLUGINHDRS = common.h noinst_LIBRARIES = libnpcommon.a +noinst_PROGRAMS = @EXTRA_PLUGIN_TESTS@ +# These two lines support "make check", but we use "make test" +check_PROGRAMS = @EXTRA_PLUGIN_TESTS@ libnpcommon_a_SOURCES = utils.c netutils.c sslutils.c runcmd.c \ popen.c utils.h netutils.h popen.h common.h runcmd.c runcmd.h + BASEOBJS = libnpcommon.a ../lib/libmonitoringplug.a ../gl/libgnu.a $(LIB_CRYPTO) NETOBJS = $(BASEOBJS) $(EXTRA_NETOBLS) NETLIBS = $(NETOBJS) $(SOCKETLIBS) @@ -58,7 +66,10 @@ SSLOBJS = $(BASEOBJS) $(NETLIBS) $(SSLLIBS) $(LIB_CRYPTO) TESTS_ENVIRONMENT = perl -I $(top_builddir) -I $(top_srcdir) -TESTS = @PLUGIN_TEST@ +tap_ldflags = -L$(top_srcdir)/tap + +TESTS = @PLUGIN_TEST@ @EXTRA_PLUGIN_TESTS@ + test: perl -I $(top_builddir) -I $(top_srcdir) ../test.pl @@ -122,6 +133,8 @@ if !HAVE_UTMPX check_users_LDADD += popen.o endif +tests_test_check_swap_LDADD = $(BASEOBJS) check_swap.d/swap.o $(tap_ldflags) -ltap + ############################################################################## # secondary dependencies diff --git a/plugins/tests/test_check_swap.c b/plugins/tests/test_check_swap.c new file mode 100644 index 00000000..42ac0086 --- /dev/null +++ b/plugins/tests/test_check_swap.c @@ -0,0 +1,21 @@ + +#include "../check_swap.d/check_swap.h" +#include "../../tap/tap.h" + +void print_usage() {}; +void print_help(swap_config config) { + (void) config; +}; + +const char *progname = "test_check_swap"; + +int main() { + + swap_config config = swap_config_init(); + + swap_result test_data = get_swap_data(config); + + plan_tests(1); + + ok(test_data.errorcode == 0, "Test whether we manage to retrieve swap data"); +} diff --git a/plugins/tests/test_check_swap.t b/plugins/tests/test_check_swap.t new file mode 100644 index 00000000..97c651a8 --- /dev/null +++ b/plugins/tests/test_check_swap.t @@ -0,0 +1,6 @@ +#!/usr/bin/perl +use Test::More; +if (! -e "./test_check_swap") { + plan skip_all => "./test_swap not compiled - please enable libtap library to test"; +} +exec "./test_check_swap"; -- cgit v1.2.3-74-g34f1 From 442f50987cf8219856608d8d3775831e34e17994 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 19 Dec 2023 12:18:28 +0100 Subject: Add missing includes in plugins/check_swap.d/swap.c --- plugins/check_swap.d/swap.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'plugins') diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index ebfa840d..7703fb3e 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -1,4 +1,6 @@ #include "./check_swap.d/check_swap.h" +#include "../popen.h" +#include "../utils.h" swap_config swap_config_init() { swap_config tmp = {0}; -- cgit v1.2.3-74-g34f1 From fc9caba617cac02a205fb63b87b08fbc5c6a2955 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 19 Dec 2023 14:34:05 +0100 Subject: Hopefully fix build --- plugins/Makefile.am | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'plugins') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 8ef2a246..0a720b2d 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -118,7 +118,8 @@ check_real_LDADD = $(NETLIBS) check_snmp_LDADD = $(BASEOBJS) check_smtp_LDADD = $(SSLOBJS) check_ssh_LDADD = $(NETLIBS) -check_swap_LDADD = $(MATHLIBS) $(BASEOBJS) check_swap.d/swap.o +check_swap_SOURCES = check_swap.c check_swap.d/swap.c +check_swap_LDADD = $(MATHLIBS) $(BASEOBJS) check_tcp_LDADD = $(SSLOBJS) check_time_LDADD = $(NETLIBS) check_ntp_time_LDADD = $(NETLIBS) $(MATHLIBS) @@ -133,7 +134,8 @@ if !HAVE_UTMPX check_users_LDADD += popen.o endif -tests_test_check_swap_LDADD = $(BASEOBJS) check_swap.d/swap.o $(tap_ldflags) -ltap +tests_test_check_swap_LDADD = $(BASEOBJS) $(tap_ldflags) -ltap +tests_test_check_swap_SOURCES = check_swap.c check_swap.d/swap.c ############################################################################## # secondary dependencies -- cgit v1.2.3-74-g34f1 From 9083896d9c5f5a06f4f0c7c356879dc32330dab7 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 19 Dec 2023 14:48:01 +0100 Subject: Include new directory and contents into distribution --- plugins/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 0a720b2d..2310f107 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -46,7 +46,7 @@ SUBDIRS = picohttpparser np_test_scripts = tests/test_check_swap.t -EXTRA_DIST = t tests $(np_test_scripts) +EXTRA_DIST = t tests $(np_test_scripts) check_swap.d PLUGINHDRS = common.h -- cgit v1.2.3-74-g34f1 From 08c3f0f7379eabe51cd965d0dc35626d8939ae27 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 19 Dec 2023 15:06:11 +0100 Subject: Add test proc/meminfo file and corresponding test --- plugins/tests/test_check_swap.c | 7 ++++-- plugins/tests/var/proc_meminfo | 55 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 plugins/tests/var/proc_meminfo (limited to 'plugins') diff --git a/plugins/tests/test_check_swap.c b/plugins/tests/test_check_swap.c index 42ac0086..86ab188f 100644 --- a/plugins/tests/test_check_swap.c +++ b/plugins/tests/test_check_swap.c @@ -13,9 +13,12 @@ int main() { swap_config config = swap_config_init(); - swap_result test_data = get_swap_data(config); + swap_result test_data = getSwapFromProcMeminfo(config, "./var/proc_meminfo"); - plan_tests(1); + plan_tests(4); ok(test_data.errorcode == 0, "Test whether we manage to retrieve swap data"); + ok(test_data.metrics.total == 34233905152, "Is the total Swap correct"); + ok(test_data.metrics.free == 34231021568, "Is the free Swap correct"); + ok(test_data.metrics.used == 0, "Is the used Swap correct"); } diff --git a/plugins/tests/var/proc_meminfo b/plugins/tests/var/proc_meminfo new file mode 100644 index 00000000..8c94c4e5 --- /dev/null +++ b/plugins/tests/var/proc_meminfo @@ -0,0 +1,55 @@ +MemTotal: 32767776 kB +MemFree: 1693508 kB +MemAvailable: 23807480 kB +Buffers: 438456 kB +Cached: 19124976 kB +SwapCached: 136 kB +Active: 7860680 kB +Inactive: 18886776 kB +Active(anon): 6108756 kB +Inactive(anon): 1364500 kB +Active(file): 1751924 kB +Inactive(file): 17522276 kB +Unevictable: 8548 kB +Mlocked: 8548 kB +SwapTotal: 33431548 kB +SwapFree: 33428732 kB +Zswap: 0 kB +Zswapped: 0 kB +Dirty: 784 kB +Writeback: 0 kB +AnonPages: 7139968 kB +Mapped: 1094916 kB +Shmem: 284160 kB +KReclaimable: 3303788 kB +Slab: 3801908 kB +SReclaimable: 3303788 kB +SUnreclaim: 498120 kB +KernelStack: 32992 kB +PageTables: 68160 kB +SecPageTables: 0 kB +NFS_Unstable: 0 kB +Bounce: 0 kB +WritebackTmp: 0 kB +CommitLimit: 49815436 kB +Committed_AS: 16888536 kB +VmallocTotal: 34359738367 kB +VmallocUsed: 91200 kB +VmallocChunk: 0 kB +Percpu: 41472 kB +HardwareCorrupted: 0 kB +AnonHugePages: 1708032 kB +ShmemHugePages: 0 kB +ShmemPmdMapped: 0 kB +FileHugePages: 0 kB +FilePmdMapped: 0 kB +Unaccepted: 0 kB +HugePages_Total: 0 +HugePages_Free: 0 +HugePages_Rsvd: 0 +HugePages_Surp: 0 +Hugepagesize: 2048 kB +Hugetlb: 0 kB +DirectMap4k: 860468 kB +DirectMap2M: 20023296 kB +DirectMap1G: 12582912 kB -- cgit v1.2.3-74-g34f1 From 79d606abc137c9be288d2573984fc3cf7876cd79 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 4 Jan 2024 01:48:16 +0100 Subject: Implement stub functionality for BSD swapctl stuff --- plugins/check_swap.d/swap.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 7703fb3e..8dc7a4cc 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -225,19 +225,41 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[] return result; } -#ifdef CHECK_SWAP_SWAPCTL_BSD -swap_result getSwapFromSwapctl_BSD(swap_config config) { - int i = 0, nswaps = 0, swapctl_res = 0; - struct swapent *ent; +#ifndef CHECK_SWAP_SWAPCTL_BSD +#define CHECK_SWAP_SWAPCTL_BSD + +// Stub functionality for BSD stuff, so the compiler always sees the following BSD code + +#define SWAP_NSWAP 0 +#define SWAP_STATS 1 + +int swapctl(int cmd, const void *arg, int misc) { + (void) cmd; + (void) arg; + (void) misc; + return 512; +} + +struct swapent { + dev_t se_dev; /* device id */ + int se_flags; /* entry flags */ + int se_nblks; /* total blocks */ + int se_inuse; /* blocks in use */ + int se_priority; /* priority */ + char se_path[PATH_MAX]; /* path to entry */ +}; + +#endif +swap_result getSwapFromSwapctl_BSD(swap_config config) { /* get the number of active swap devices */ - nswaps = swapctl(SWAP_NSWAP, NULL, 0); + int nswaps = swapctl(SWAP_NSWAP, NULL, 0); /* initialize swap table + entries */ - ent = (struct swapent *)malloc(sizeof(struct swapent) * nswaps); + struct swapent *ent = (struct swapent *)malloc(sizeof(struct swapent) * nswaps); /* and now, tally 'em up */ - swapctl_res = swapctl(SWAP_STATS, ent, nswaps); + int swapctl_res = swapctl(SWAP_STATS, ent, nswaps); if (swapctl_res < 0) { perror(_("swapctl failed: ")); die(STATE_UNKNOWN, _("Error in swapctl call\n")); @@ -247,7 +269,7 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) { double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; - for (i = 0; i < nswaps; i++) { + for (int i = 0; i < nswaps; i++) { dsktotal_mb = (float)ent[i].se_nblks / config.conversion_factor; dskused_mb = (float)ent[i].se_inuse / config.conversion_factor; dskfree_mb = (dsktotal_mb - dskused_mb); @@ -279,7 +301,6 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) { return result; } -#endif // CHECK_SWAP_SWAPCTL_BSD #ifdef CHECK_SWAP_SWAPCTL_SVR4 swap_result getSwapFromSwap_SRV4(swap_config config) { -- cgit v1.2.3-74-g34f1 From 946d2f4b8fedfef30b8978d10823987cb6f88819 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 4 Jan 2024 02:11:48 +0100 Subject: Implement stub functionality for SRV4 swapctl stuff --- plugins/check_swap.d/swap.c | 65 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 16 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 8dc7a4cc..50920fa3 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -233,7 +233,7 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[] #define SWAP_NSWAP 0 #define SWAP_STATS 1 -int swapctl(int cmd, const void *arg, int misc) { +int bsd_swapctl(int cmd, const void *arg, int misc) { (void) cmd; (void) arg; (void) misc; @@ -249,17 +249,19 @@ struct swapent { char se_path[PATH_MAX]; /* path to entry */ }; +#else +#define bsd_swapctl swapctl #endif swap_result getSwapFromSwapctl_BSD(swap_config config) { /* get the number of active swap devices */ - int nswaps = swapctl(SWAP_NSWAP, NULL, 0); + int nswaps = bsd_swapctl(SWAP_NSWAP, NULL, 0); /* initialize swap table + entries */ struct swapent *ent = (struct swapent *)malloc(sizeof(struct swapent) * nswaps); /* and now, tally 'em up */ - int swapctl_res = swapctl(SWAP_STATS, ent, nswaps); + int swapctl_res = bsd_swapctl(SWAP_STATS, ent, nswaps); if (swapctl_res < 0) { perror(_("swapctl failed: ")); die(STATE_UNKNOWN, _("Error in swapctl call\n")); @@ -302,15 +304,46 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) { return result; } -#ifdef CHECK_SWAP_SWAPCTL_SVR4 + + +#ifndef CHECK_SWAP_SWAPCTL_SVR4 +int srv4_swapctl(int cmd, void *arg) { + (void) cmd; + (void) arg; + return 512; +} + +typedef struct srv4_swapent { + char *ste_path; /* name of the swap file */ + off_t ste_start; /* starting block for swapping */ + off_t ste_length; /* length of swap area */ + long ste_pages; /* number of pages for swapping */ + long ste_free; /* number of ste_pages free */ + long ste_flags; /* ST_INDEL bit set if swap file */ + /* is now being deleted */ +} swapent_t; + +typedef struct swaptbl { + int swt_n; /* number of swapents following */ + struct srv4_swapent swt_ent[]; /* array of swt_n swapents */ +} swaptbl_t; + +#define SC_LIST 2 +#define SC_GETNSWP 3 + +#ifndef MAXPATHLEN +#define MAXPATHLEN 2048 +#endif + +#else +#define srv4_swapctl swapctl +#endif + swap_result getSwapFromSwap_SRV4(swap_config config) { - int i = 0, nswaps = 0, swapctl_res = 0; - //swaptbl_t *tbl = NULL; - void*tbl = NULL; - //swapent_t *ent = NULL; - void*ent = NULL; + int nswaps = 0; + /* get the number of active swap devices */ - if ((nswaps = swapctl(SC_GETNSWP, NULL)) == -1) + if ((nswaps = srv4_swapctl(SC_GETNSWP, NULL)) == -1) die(STATE_UNKNOWN, _("Error getting swap devices\n")); if (nswaps == 0) @@ -320,21 +353,22 @@ swap_result getSwapFromSwap_SRV4(swap_config config) { printf("Found %d swap device(s)\n", nswaps); /* initialize swap table + entries */ - tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); + swaptbl_t *tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); if (tbl == NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); tbl->swt_n = nswaps; - for (i = 0; i < nswaps; i++) { + + for (int i = 0; i < nswaps; i++) { if ((tbl->swt_ent[i].ste_path = (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL) die(STATE_UNKNOWN, _("malloc() failed!\n")); } /* and now, tally 'em up */ - swapctl_res = swapctl(SC_LIST, tbl); + int swapctl_res = srv4_swapctl(SC_LIST, tbl); if (swapctl_res < 0) { perror(_("swapctl failed: ")); die(STATE_UNKNOWN, _("Error in swapctl call\n")); @@ -343,7 +377,7 @@ swap_result getSwapFromSwap_SRV4(swap_config config) { double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; - for (i = 0; i < nswaps; i++) { + for (int i = 0; i < nswaps; i++) { dsktotal_mb = (float)tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION; dskused_mb = (dsktotal_mb - dskfree_mb); @@ -366,7 +400,7 @@ swap_result getSwapFromSwap_SRV4(swap_config config) { } /* and clean up after ourselves */ - for (i = 0; i < nswaps; i++) { + for (int i = 0; i < nswaps; i++) { free(tbl->swt_ent[i].ste_path); } free(tbl); @@ -379,4 +413,3 @@ swap_result getSwapFromSwap_SRV4(swap_config config) { return result; } -#endif // CHECK_SWAP_SWAPCTL_SVR4 -- cgit v1.2.3-74-g34f1 From 1d669e47eb9e6971b266d451274c02c5203b1906 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 4 Jan 2024 02:36:50 +0100 Subject: Fix Makefile.am to fix tests --- plugins/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 2310f107..d43c1971 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -135,7 +135,7 @@ check_users_LDADD += popen.o endif tests_test_check_swap_LDADD = $(BASEOBJS) $(tap_ldflags) -ltap -tests_test_check_swap_SOURCES = check_swap.c check_swap.d/swap.c +tests_test_check_swap_SOURCES = tests/test_check_swap.c check_swap.d/swap.c ############################################################################## # secondary dependencies -- cgit v1.2.3-74-g34f1 From 735b04eff721a28e791714c0da4c8ac5726bfbcf Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 4 Jan 2024 02:37:26 +0100 Subject: Fix tests --- plugins/tests/test_check_swap.c | 3 +-- plugins/tests/test_check_swap.t | 2 +- plugins/tests/var/proc_meminfo | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) mode change 100644 => 100755 plugins/tests/test_check_swap.t (limited to 'plugins') diff --git a/plugins/tests/test_check_swap.c b/plugins/tests/test_check_swap.c index 86ab188f..319cadbf 100644 --- a/plugins/tests/test_check_swap.c +++ b/plugins/tests/test_check_swap.c @@ -10,7 +10,6 @@ void print_help(swap_config config) { const char *progname = "test_check_swap"; int main() { - swap_config config = swap_config_init(); swap_result test_data = getSwapFromProcMeminfo(config, "./var/proc_meminfo"); @@ -19,6 +18,6 @@ int main() { ok(test_data.errorcode == 0, "Test whether we manage to retrieve swap data"); ok(test_data.metrics.total == 34233905152, "Is the total Swap correct"); - ok(test_data.metrics.free == 34231021568, "Is the free Swap correct"); + ok(test_data.metrics.free == 34233905152, "Is the free Swap correct"); ok(test_data.metrics.used == 0, "Is the used Swap correct"); } diff --git a/plugins/tests/test_check_swap.t b/plugins/tests/test_check_swap.t old mode 100644 new mode 100755 index 97c651a8..826fae01 --- a/plugins/tests/test_check_swap.t +++ b/plugins/tests/test_check_swap.t @@ -1,6 +1,6 @@ #!/usr/bin/perl use Test::More; if (! -e "./test_check_swap") { - plan skip_all => "./test_swap not compiled - please enable libtap library to test"; + plan skip_all => "./test_check_swap not compiled - please enable libtap library to test"; } exec "./test_check_swap"; diff --git a/plugins/tests/var/proc_meminfo b/plugins/tests/var/proc_meminfo index 8c94c4e5..6c5a618d 100644 --- a/plugins/tests/var/proc_meminfo +++ b/plugins/tests/var/proc_meminfo @@ -3,7 +3,7 @@ MemFree: 1693508 kB MemAvailable: 23807480 kB Buffers: 438456 kB Cached: 19124976 kB -SwapCached: 136 kB +SwapCached: 0 kB Active: 7860680 kB Inactive: 18886776 kB Active(anon): 6108756 kB @@ -13,7 +13,7 @@ Inactive(file): 17522276 kB Unevictable: 8548 kB Mlocked: 8548 kB SwapTotal: 33431548 kB -SwapFree: 33428732 kB +SwapFree: 33431548 kB Zswap: 0 kB Zswapped: 0 kB Dirty: 784 kB -- cgit v1.2.3-74-g34f1 From ed01d534474cc640515f1d5155349f14090aafe9 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sat, 2 Nov 2024 13:53:33 +0100 Subject: Small fixes to check_swap stuff --- plugins/check_swap.c | 9 ++++--- plugins/check_swap.d/swap.c | 64 +++++++++++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 25 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 4e3471b6..c3199ab7 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -56,10 +56,13 @@ static swap_config_wrapper process_arguments(int argc, char **argv); void print_usage(void); static void print_help(swap_config /*config*/); -static int verbose; +int verbose; #define HUNDRED_PERCENT 100 +#define BYTES_TO_KiB(number) (number / 1024) +#define BYTES_TO_MiB(number) (BYTES_TO_KiB(number) / 1024) + int main(int argc, char **argv) { setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); @@ -127,8 +130,8 @@ int main(int argc, char **argv) { data.statusCode = max_state(data.statusCode, STATE_CRITICAL); } - printf(_("SWAP %s - %g%% free (%lluMB out of %lluMB) %s|%s\n"), state_text(data.statusCode), (HUNDRED_PERCENT - percent_used), - data.metrics.free, data.metrics.total, status, perfdata); + printf(_("SWAP %s - %g%% free (%lluMiB out of %lluMiB) %s|%s\n"), state_text(data.statusCode), (HUNDRED_PERCENT - percent_used), + BYTES_TO_MiB(data.metrics.free), BYTES_TO_MiB(data.metrics.total), status, perfdata); exit(data.statusCode); } diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index d437ba59..18db210c 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -59,18 +59,20 @@ swap_result get_swap_data(swap_config config) { } swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { - FILE *fp; - fp = fopen(proc_meminfo, "r"); + FILE *meminfo_file_ptr; + meminfo_file_ptr = fopen(proc_meminfo, "r"); swap_result result = {0}; result.statusCode = STATE_OK; - uint64_t swap_total = 0, swap_used = 0, swap_free = 0; + uint64_t swap_total = 0; + uint64_t swap_used = 0; + uint64_t swap_free = 0; char input_buffer[MAX_INPUT_BUFFER]; char str[32]; - while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) { + while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, meminfo_file_ptr)) { uint64_t tmp_KB = 0; /* @@ -111,7 +113,7 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { } } - fclose(fp); + fclose(meminfo_file_ptr); result.metrics.total = swap_total; result.metrics.used = swap_total - swap_free; @@ -125,10 +127,12 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[] char *temp_buffer; - if (verbose >= 2) + if (verbose >= 2) { printf(_("Command: %s\n"), swap_command); - if (verbose >= 3) + } + if (verbose >= 3) { printf(_("Format: %s\n"), swap_format); + } child_process = spopen(swap_command); if (child_process == NULL) { @@ -152,18 +156,23 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[] if (strcmp(swap_format, "") == 0) { temp_buffer = strtok(input_buffer, " \n"); while (temp_buffer) { - if (strstr(temp_buffer, "blocks")) + if (strstr(temp_buffer, "blocks")) { sprintf(str, "%s %s", str, "%lu"); - else if (strstr(temp_buffer, "dskfree")) + } else if (strstr(temp_buffer, "dskfree")) { sprintf(str, "%s %s", str, "%lu"); - else + } else { sprintf(str, "%s %s", str, "%*s"); + } temp_buffer = strtok(NULL, " \n"); } } - double total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; - double dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0; + double total_swap_mb = 0; + double free_swap_mb = 0; + double used_swap_mb = 0; + double dsktotal_mb = 0; + double dskused_mb = 0; + double dskfree_mb = 0; /* * If different swap command is used for summary switch, need to read format @@ -191,8 +200,9 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[] dskfree_mb = dskfree_mb / config.conversion_factor; } - if (verbose >= 3) + if (verbose >= 3) { printf(_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb); + } dskused_mb = dsktotal_mb - dskfree_mb; total_swap_mb += dsktotal_mb; @@ -256,7 +266,7 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) { int nswaps = bsd_swapctl(SWAP_NSWAP, NULL, 0); /* initialize swap table + entries */ - struct swapent *ent = (struct swapent *)malloc(sizeof(struct swapent) * nswaps); + struct swapent *ent = (struct swapent *)malloc(sizeof(struct swapent) * (unsigned long)nswaps); /* and now, tally 'em up */ int swapctl_res = bsd_swapctl(SWAP_STATS, ent, nswaps); @@ -265,8 +275,12 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) { die(STATE_UNKNOWN, _("Error in swapctl call\n")); } - double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; - unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; + double dsktotal_mb = 0.0; + double dskfree_mb = 0.0; + double dskused_mb = 0.0; + unsigned long long total_swap_mb = 0; + unsigned long long free_swap_mb = 0; + unsigned long long used_swap_mb = 0; for (int i = 0; i < nswaps; i++) { dsktotal_mb = (float)ent[i].se_nblks / config.conversion_factor; @@ -338,27 +352,32 @@ swap_result getSwapFromSwap_SRV4(swap_config config) { int nswaps = 0; /* get the number of active swap devices */ - if ((nswaps = srv4_swapctl(SC_GETNSWP, NULL)) == -1) + if ((nswaps = srv4_swapctl(SC_GETNSWP, NULL)) == -1) { die(STATE_UNKNOWN, _("Error getting swap devices\n")); + } - if (nswaps == 0) + if (nswaps == 0) { die(STATE_OK, _("SWAP OK: No swap devices defined\n")); + } - if (verbose >= 3) + if (verbose >= 3) { printf("Found %d swap device(s)\n", nswaps); + } /* initialize swap table + entries */ swaptbl_t *tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); - if (tbl == NULL) + if (tbl == NULL) { die(STATE_UNKNOWN, _("malloc() failed!\n")); + } memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); tbl->swt_n = nswaps; for (int i = 0; i < nswaps; i++) { - if ((tbl->swt_ent[i].ste_path = (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL) + if ((tbl->swt_ent[i].ste_path = (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL) { die(STATE_UNKNOWN, _("malloc() failed!\n")); + } } /* and now, tally 'em up */ @@ -380,8 +399,9 @@ swap_result getSwapFromSwap_SRV4(swap_config config) { dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION; dskused_mb = (dsktotal_mb - dskfree_mb); - if (verbose >= 3) + if (verbose >= 3) { printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb); + } if (config.allswaps && dsktotal_mb > 0) { double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); -- cgit v1.2.3-74-g34f1 From 14342ac87a26e3593b00dcb8482a280544b1667e Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 10 Nov 2024 00:19:03 +0100 Subject: check_swap: Small improvements --- plugins/check_swap.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index c3199ab7..ef60ce1b 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -27,11 +27,6 @@ * *****************************************************************************/ -#include -const char *progname = "check_swap"; -const char *copyright = "2000-2024"; -const char *email = "devel@monitoring-plugins.org"; - #ifdef HAVE_DECL_SWAPCTL # ifdef HAVE_SYS_PARAM_H # include @@ -44,6 +39,7 @@ const char *email = "devel@monitoring-plugins.org"; # endif #endif +#include #include "./check_swap.d/check_swap.h" #include "./utils.h" @@ -63,13 +59,16 @@ int verbose; #define BYTES_TO_KiB(number) (number / 1024) #define BYTES_TO_MiB(number) (BYTES_TO_KiB(number) / 1024) +const char *progname = "check_swap"; +const char *copyright = "2000-2024"; +const char *email = "devel@monitoring-plugins.org"; + int main(int argc, char **argv) { setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - char *status; - status = strdup(""); + char *status = strdup(""); /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); @@ -85,7 +84,6 @@ int main(int argc, char **argv) { swap_result data = get_swap_data(config); double percent_used; - /* if total_swap_mb == 0, let's not divide by 0 */ if (data.metrics.total != 0) { percent_used = HUNDRED_PERCENT * ((double)data.metrics.used) / ((double)data.metrics.total); -- cgit v1.2.3-74-g34f1 From 801d0faf457799e9772b5b97e821968f522778be Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 10 Nov 2024 01:28:52 +0100 Subject: test_check_swap: improvements and updates to unit test code --- plugins/tests/test_check_swap.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'plugins') diff --git a/plugins/tests/test_check_swap.c b/plugins/tests/test_check_swap.c index 319cadbf..d657c13a 100644 --- a/plugins/tests/test_check_swap.c +++ b/plugins/tests/test_check_swap.c @@ -2,17 +2,15 @@ #include "../check_swap.d/check_swap.h" #include "../../tap/tap.h" -void print_usage() {}; +void print_usage(void) {} void print_help(swap_config config) { (void) config; -}; +} const char *progname = "test_check_swap"; -int main() { - swap_config config = swap_config_init(); - - swap_result test_data = getSwapFromProcMeminfo(config, "./var/proc_meminfo"); +int main(void) { + swap_result test_data = getSwapFromProcMeminfo("./var/proc_meminfo"); plan_tests(4); -- cgit v1.2.3-74-g34f1 From ecbd9e9cb3bb4a1c497a19a29950583202ae4303 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 10 Nov 2024 01:30:40 +0100 Subject: test_check_swap: declare verbose in tests for now --- plugins/tests/test_check_swap.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'plugins') diff --git a/plugins/tests/test_check_swap.c b/plugins/tests/test_check_swap.c index d657c13a..b85fb4ad 100644 --- a/plugins/tests/test_check_swap.c +++ b/plugins/tests/test_check_swap.c @@ -2,6 +2,8 @@ #include "../check_swap.d/check_swap.h" #include "../../tap/tap.h" +int verbose = 0; + void print_usage(void) {} void print_help(swap_config config) { (void) config; -- cgit v1.2.3-74-g34f1 From 4b7977b25bda44a9a781d53011b73e71f7167c02 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 10 Nov 2024 01:40:29 +0100 Subject: check_swap: Fix text in old school tests --- plugins/t/check_swap.t | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'plugins') diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t index 18780386..eaa81083 100644 --- a/plugins/t/check_swap.t +++ b/plugins/t/check_swap.t @@ -8,9 +8,9 @@ use strict; use Test::More tests => 14; use NPTest; -my $successOutput = '/^SWAP OK - [0-9]+\% free \([0-9]+MB out of [0-9]+MB\)/'; -my $failureOutput = '/^SWAP CRITICAL - [0-9]+\% free \([0-9]+MB out of [0-9]+MB\)/'; -my $warnOutput = '/^SWAP WARNING - [0-9]+\% free \([0-9]+MB out of [0-9]+MB\)/'; +my $successOutput = '/^SWAP OK - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; +my $failureOutput = '/^SWAP CRITICAL - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; +my $warnOutput = '/^SWAP WARNING - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; my $result; -- cgit v1.2.3-74-g34f1 From 651925dffce258bf71a6717fd3d4e0969f29b6a6 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 10 Nov 2024 01:58:41 +0100 Subject: check_swap: Make check_swap work without thresholds --- plugins/check_swap.c | 11 ++--------- plugins/check_swap.d/check_swap.h | 7 ++++--- plugins/check_swap.d/swap.c | 3 +++ 3 files changed, 9 insertions(+), 12 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index ef60ce1b..94f41a55 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -166,11 +166,6 @@ swap_config_wrapper process_arguments(int argc, char **argv) { swap_config_wrapper conf_wrapper = {.errorcode = OK}; conf_wrapper.config = swap_config_init(); - if (argc < 2) { - conf_wrapper.errorcode = ERROR; - return conf_wrapper; - } - static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {"allswaps", no_argument, 0, 'a'}, {"no-swap", required_argument, 0, 'n'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, @@ -195,6 +190,7 @@ swap_config_wrapper process_arguments(int argc, char **argv) { */ size_t length; length = strlen(optarg); + conf_wrapper.config.warn.is_set = true; if (optarg[length - 1] == '%') { /* It's percentage */ @@ -224,6 +220,7 @@ swap_config_wrapper process_arguments(int argc, char **argv) { */ size_t length; length = strlen(optarg); + conf_wrapper.config.crit.is_set = true; if (optarg[length - 1] == '%') { /* It's percentage */ @@ -266,10 +263,6 @@ swap_config_wrapper process_arguments(int argc, char **argv) { } } - if (conf_wrapper.config.warn.value == 0 && conf_wrapper.config.crit.value == 0) { - conf_wrapper.errorcode = ERROR; - return conf_wrapper; - } if ((conf_wrapper.config.warn.is_percentage == conf_wrapper.config.crit.is_percentage) && (conf_wrapper.config.warn.value < conf_wrapper.config.crit.value)) { /* This is NOT triggered if warn and crit are different units, e.g warn diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h index 9e8be75f..e3e350c5 100644 --- a/plugins/check_swap.d/check_swap.h +++ b/plugins/check_swap.d/check_swap.h @@ -8,9 +8,10 @@ #endif typedef struct { + bool is_set; bool is_percentage; uint64_t value; -} threshold; +} check_swap_threshold; typedef struct { unsigned long long free; // Free swap in Bytes! @@ -27,8 +28,8 @@ typedef struct { typedef struct { bool allswaps; int no_swap_state; - threshold warn; - threshold crit; + check_swap_threshold warn; + check_swap_threshold crit; bool on_aix; int conversion_factor; } swap_config; diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 18db210c..293fdd71 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -10,6 +10,9 @@ swap_config swap_config_init(void) { tmp.no_swap_state = STATE_CRITICAL; tmp.conversion_factor = SWAP_CONVERSION; + tmp.warn.is_set = false; + tmp.crit.is_set = false; + #ifdef _AIX tmp.on_aix = true; #else -- cgit v1.2.3-74-g34f1 From 77c0913f7577d20fbb8a8ead522199cd079ea122 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 10 Nov 2024 10:36:46 +0100 Subject: check_swap: Use sscanf more precisely to avoid false matches --- plugins/check_swap.d/swap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 293fdd71..17c4f73b 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -99,7 +99,7 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { } else if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " "%*[k]%*[B]", - str, &tmp_KB)) { + str, &tmp_KB) == 2) { if (verbose >= 3) { printf("Got %s with %lu\n", str, tmp_KB); -- cgit v1.2.3-74-g34f1 From 9679551b20acdc8306a11e6c7d9dbc4f15e90967 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 10 Nov 2024 10:37:19 +0100 Subject: check_swap: stricter error handling --- plugins/check_swap.c | 6 ++++++ plugins/check_swap.d/swap.c | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 94f41a55..e0c246db 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -27,6 +27,7 @@ * *****************************************************************************/ +#include "common.h" #ifdef HAVE_DECL_SWAPCTL # ifdef HAVE_SYS_PARAM_H # include @@ -83,6 +84,11 @@ int main(int argc, char **argv) { swap_result data = get_swap_data(config); + if (data.errorcode != STATE_OK) { + puts("SWAP UNKNOWN - Failed to retrieve Swap usage"); + exit(STATE_UNKNOWN); + } + double percent_used; /* if total_swap_mb == 0, let's not divide by 0 */ if (data.metrics.total != 0) { diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 17c4f73b..6c94b33a 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -1,6 +1,7 @@ #include "./check_swap.d/check_swap.h" #include "../popen.h" #include "../utils.h" +#include "common.h" extern int verbose; @@ -66,7 +67,7 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { meminfo_file_ptr = fopen(proc_meminfo, "r"); swap_result result = {0}; - result.statusCode = STATE_OK; + result.errorcode = STATE_UNKNOWN; uint64_t swap_total = 0; uint64_t swap_used = 0; @@ -91,6 +92,9 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { result.metrics.used += swap_used; result.metrics.free += swap_free; + // Set error + result.errorcode = STATE_OK; + /* * The following sscanf call looks for lines looking like: * "SwapTotal: 123" and "SwapFree: 123" This format exists at least @@ -113,6 +117,8 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { } else if (strcmp("Cached", str) == 0) { swap_free = swap_free + tmp_KB * 1024; } + + result.errorcode = STATE_OK; } } -- cgit v1.2.3-74-g34f1 From 152cdcf3e425e11224b3c52cf0863b6825ae0874 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 10 Nov 2024 10:42:17 +0100 Subject: check_swap: change threshold handling again --- plugins/check_swap.c | 35 ++++++++++++++++++++--------------- plugins/check_swap.d/check_swap.h | 3 ++- plugins/check_swap.d/swap.c | 4 ++-- 3 files changed, 24 insertions(+), 18 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.c b/plugins/check_swap.c index e0c246db..bc90a90b 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -114,24 +114,29 @@ int main(int argc, char **argv) { crit_print = config.crit.value * (data.metrics.total / HUNDRED_PERCENT); } - char *perfdata = perfdata_uint64("swap", data.metrics.free, "B", true, warn_print, true, crit_print, true, 0, true, data.metrics.total); + char *perfdata = perfdata_uint64("swap", data.metrics.free, "B", config.warn_is_set, warn_print, config.crit_is_set, crit_print, true, + 0, true, data.metrics.total); - if (verbose > 1) { - printf("Warn threshold value: %" PRIu64 "\n", config.warn.value); - } + if (config.warn_is_set) { + if (verbose > 1) { + printf("Warn threshold value: %" PRIu64 "\n", config.warn.value); + } - if ((config.warn.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.warn.value))) || - config.warn.value >= data.metrics.free) { - data.statusCode = max_state(data.statusCode, STATE_WARNING); + if ((config.warn.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.warn.value))) || + config.warn.value >= data.metrics.free) { + data.statusCode = max_state(data.statusCode, STATE_WARNING); + } } - if (verbose > 1) { - printf("Crit threshold value: %" PRIu64 "\n", config.crit.value); - } + if (config.crit_is_set) { + if (verbose > 1) { + printf("Crit threshold value: %" PRIu64 "\n", config.crit.value); + } - if ((config.crit.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.crit.value))) || - config.crit.value >= data.metrics.free) { - data.statusCode = max_state(data.statusCode, STATE_CRITICAL); + if ((config.crit.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.crit.value))) || + config.crit.value >= data.metrics.free) { + data.statusCode = max_state(data.statusCode, STATE_CRITICAL); + } } printf(_("SWAP %s - %g%% free (%lluMiB out of %lluMiB) %s|%s\n"), state_text(data.statusCode), (HUNDRED_PERCENT - percent_used), @@ -196,7 +201,7 @@ swap_config_wrapper process_arguments(int argc, char **argv) { */ size_t length; length = strlen(optarg); - conf_wrapper.config.warn.is_set = true; + conf_wrapper.config.warn_is_set = true; if (optarg[length - 1] == '%') { /* It's percentage */ @@ -226,7 +231,7 @@ swap_config_wrapper process_arguments(int argc, char **argv) { */ size_t length; length = strlen(optarg); - conf_wrapper.config.crit.is_set = true; + conf_wrapper.config.crit_is_set = true; if (optarg[length - 1] == '%') { /* It's percentage */ diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h index e3e350c5..5d878989 100644 --- a/plugins/check_swap.d/check_swap.h +++ b/plugins/check_swap.d/check_swap.h @@ -8,7 +8,6 @@ #endif typedef struct { - bool is_set; bool is_percentage; uint64_t value; } check_swap_threshold; @@ -28,7 +27,9 @@ typedef struct { typedef struct { bool allswaps; int no_swap_state; + bool warn_is_set; check_swap_threshold warn; + bool crit_is_set; check_swap_threshold crit; bool on_aix; int conversion_factor; diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 6c94b33a..354efdbf 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -11,8 +11,8 @@ swap_config swap_config_init(void) { tmp.no_swap_state = STATE_CRITICAL; tmp.conversion_factor = SWAP_CONVERSION; - tmp.warn.is_set = false; - tmp.crit.is_set = false; + tmp.warn_is_set = false; + tmp.crit_is_set = false; #ifdef _AIX tmp.on_aix = true; -- cgit v1.2.3-74-g34f1 From a581465ca935d59d6627a537456517cef6334380 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:03:16 +0100 Subject: check_swap stuff: Use pragma once instead of include guard --- plugins/check_swap.d/check_swap.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h index 5d878989..99039b21 100644 --- a/plugins/check_swap.d/check_swap.h +++ b/plugins/check_swap.d/check_swap.h @@ -1,5 +1,4 @@ -#ifndef CHECK_SWAP_H -#define CHECK_SWAP_H +#pragma once #include "../common.h" @@ -42,5 +41,3 @@ swap_result getSwapFromProcMeminfo(char path_to_proc_meminfo[]); swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]); swap_result getSwapFromSwapctl_BSD(swap_config config); swap_result getSwapFromSwap_SRV4(swap_config config); - -#endif -- cgit v1.2.3-74-g34f1 From 23970766e71aa66c49a2a782dc94f5a58d4e4616 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:04:57 +0100 Subject: check_swap stuff: Add comments to ifdef stuff to improve readability --- plugins/check_swap.d/swap.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 354efdbf..ece05fd2 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -30,7 +30,7 @@ swap_result get_swap_data(swap_config config) { } return getSwapFromProcMeminfo(PROC_MEMINFO); -#else +#else // HAVE_PROC_MEMINFO # ifdef HAVE_SWAP if (verbose >= 3) { printf("Using swap command %s with format: %s\n", SWAP_COMMAND, SWAP_FORMAT); @@ -48,13 +48,13 @@ swap_result get_swap_data(swap_config config) { } else { return getSwapFromSwapCommand(config, SWAP_COMMAND, SWAP_FORMAT); } -# else +# else // HAVE_SWAP # ifdef CHECK_SWAP_SWAPCTL_SVR4 return getSwapFromSwapctl_SRV4(); -# else +# else // CHECK_SWAP_SWAPCTL_SVR4 # ifdef CHECK_SWAP_SWAPCTL_BSD return getSwapFromSwapctl_BSD(); -# else +# else // CHECK_SWAP_SWAPCTL_BSD # error No way found to retrieve swap # endif /* CHECK_SWAP_SWAPCTL_BSD */ # endif /* CHECK_SWAP_SWAPCTL_SVR4 */ -- cgit v1.2.3-74-g34f1 From 72676bdc14c22818f9154fbee9de54ec6ae61c89 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:05:28 +0100 Subject: check_swap stuff: improve error handling --- plugins/check_swap.d/swap.c | 59 ++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 19 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index ece05fd2..9133c4fe 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -73,6 +73,10 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { uint64_t swap_used = 0; uint64_t swap_free = 0; + bool found_total = false; + bool found_used = false; + bool found_free = false; + char input_buffer[MAX_INPUT_BUFFER]; char str[32]; @@ -92,7 +96,11 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { result.metrics.used += swap_used; result.metrics.free += swap_free; - // Set error + found_total = true; + found_free = true; + found_used = true; + + // Set error result.errorcode = STATE_OK; /* @@ -100,25 +108,34 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { * "SwapTotal: 123" and "SwapFree: 123" This format exists at least * on Debian Linux with a 5.* kernel */ - } else if (sscanf(input_buffer, - "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " - "%*[k]%*[B]", - str, &tmp_KB) == 2) { - - if (verbose >= 3) { - printf("Got %s with %lu\n", str, tmp_KB); - } - - /* I think this part is always in Kb, so convert to bytes */ - if (strcmp("Total", str) == 0) { - swap_total = tmp_KB * 1024; - } else if (strcmp("Free", str) == 0) { - swap_free = swap_free + tmp_KB * 1024; - } else if (strcmp("Cached", str) == 0) { - swap_free = swap_free + tmp_KB * 1024; + } else { + int sscanf_result = sscanf(input_buffer, + "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " + "%*[k]%*[B]", + str, &tmp_KB); + + if (sscanf_result == 2) { + + if (verbose >= 3) { + printf("Got %s with %lu\n", str, tmp_KB); + } + + /* I think this part is always in Kb, so convert to bytes */ + if (strcmp("Total", str) == 0) { + swap_total = tmp_KB * 1000; + found_total = true; + } else if (strcmp("Free", str) == 0) { + swap_free = swap_free + tmp_KB * 1000; + found_free = true; + found_used = true; // No explicit used metric available + } else if (strcmp("Cached", str) == 0) { + swap_free = swap_free + tmp_KB * 1000; + found_free = true; + found_used = true; // No explicit used metric available + } + + result.errorcode = STATE_OK; } - - result.errorcode = STATE_OK; } } @@ -128,6 +145,10 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { result.metrics.used = swap_total - swap_free; result.metrics.free = swap_free; + if (!found_free || !found_total || !found_used) { + result.errorcode = STATE_UNKNOWN; + } + return result; } -- cgit v1.2.3-74-g34f1 From 16075bd2854b2f450483cd3287738855992c18c7 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:05:52 +0100 Subject: check_swap stuff: make type casts explicit instead of implicit --- plugins/check_swap.d/swap.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'plugins') diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 9133c4fe..ba8c6439 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -313,8 +313,8 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) { unsigned long long used_swap_mb = 0; for (int i = 0; i < nswaps; i++) { - dsktotal_mb = (float)ent[i].se_nblks / config.conversion_factor; - dskused_mb = (float)ent[i].se_inuse / config.conversion_factor; + dsktotal_mb = (float)ent[i].se_nblks / (float)config.conversion_factor; + dskused_mb = (float)ent[i].se_inuse / (float)config.conversion_factor; dskfree_mb = (dsktotal_mb - dskused_mb); if (config.allswaps && dsktotal_mb > 0) { @@ -325,9 +325,9 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) { } } - total_swap_mb += dsktotal_mb; - free_swap_mb += dskfree_mb; - used_swap_mb += dskused_mb; + total_swap_mb += (unsigned long long)dsktotal_mb; + free_swap_mb += (unsigned long long)dskfree_mb; + used_swap_mb += (unsigned long long)dskused_mb; } /* and clean up after ourselves */ @@ -395,13 +395,13 @@ swap_result getSwapFromSwap_SRV4(swap_config config) { } /* initialize swap table + entries */ - swaptbl_t *tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); + swaptbl_t *tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * (unsigned long)nswaps)); if (tbl == NULL) { die(STATE_UNKNOWN, _("malloc() failed!\n")); } - memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); + memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * (unsigned long)nswaps)); tbl->swt_n = nswaps; for (int i = 0; i < nswaps; i++) { @@ -441,9 +441,9 @@ swap_result getSwapFromSwap_SRV4(swap_config config) { } } - total_swap_mb += dsktotal_mb; - free_swap_mb += dskfree_mb; - used_swap_mb += dskused_mb; + total_swap_mb += (unsigned long long)dsktotal_mb; + free_swap_mb += (unsigned long long)dskfree_mb; + used_swap_mb += (unsigned long long)dskused_mb; } /* and clean up after ourselves */ -- cgit v1.2.3-74-g34f1 From bb88e38a345c90465386d9eb746ea704c7343e80 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:10:50 +0100 Subject: check_swap stuff: Error out if meminfo file can not be opened --- plugins/check_swap.d/swap.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'plugins') diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index ba8c6439..2fe4544f 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c @@ -69,6 +69,13 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { swap_result result = {0}; result.errorcode = STATE_UNKNOWN; + if (meminfo_file_ptr == NULL) { + // failed to open meminfo file + // errno should contain an error + result.errorcode = STATE_UNKNOWN; + return result; + } + uint64_t swap_total = 0; uint64_t swap_used = 0; uint64_t swap_free = 0; -- cgit v1.2.3-74-g34f1