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 --- plugins/check_swap.d/check_swap.h | 45 +++++ plugins/check_swap.d/swap.c | 359 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 404 insertions(+) create mode 100644 plugins/check_swap.d/check_swap.h create mode 100644 plugins/check_swap.d/swap.c (limited to 'plugins/check_swap.d') 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 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/check_swap.d') 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 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/check_swap.d') 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/check_swap.d') 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 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/check_swap.d') 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 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/check_swap.d') 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/check_swap.d') 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/check_swap.d') 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/check_swap.d') 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/check_swap.d') 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/check_swap.d') 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/check_swap.d') 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/check_swap.d') 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/check_swap.d') 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