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(-) 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