diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-11-10 10:37:19 +0100 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-11-10 10:37:19 +0100 |
| commit | 9679551b20acdc8306a11e6c7d9dbc4f15e90967 (patch) | |
| tree | 2d8a340dd0df04284bc4d3dcb6e27cb4a7461de7 | |
| parent | 77c0913f7577d20fbb8a8ead522199cd079ea122 (diff) | |
| download | monitoring-plugins-9679551b20acdc8306a11e6c7d9dbc4f15e90967.tar.gz | |
check_swap: stricter error handling
| -rw-r--r-- | plugins/check_swap.c | 6 | ||||
| -rw-r--r-- | plugins/check_swap.d/swap.c | 8 |
2 files changed, 13 insertions, 1 deletions
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 @@ | |||
| 27 | * | 27 | * |
| 28 | *****************************************************************************/ | 28 | *****************************************************************************/ |
| 29 | 29 | ||
| 30 | #include "common.h" | ||
| 30 | #ifdef HAVE_DECL_SWAPCTL | 31 | #ifdef HAVE_DECL_SWAPCTL |
| 31 | # ifdef HAVE_SYS_PARAM_H | 32 | # ifdef HAVE_SYS_PARAM_H |
| 32 | # include <sys/param.h> | 33 | # include <sys/param.h> |
| @@ -83,6 +84,11 @@ int main(int argc, char **argv) { | |||
| 83 | 84 | ||
| 84 | swap_result data = get_swap_data(config); | 85 | swap_result data = get_swap_data(config); |
| 85 | 86 | ||
| 87 | if (data.errorcode != STATE_OK) { | ||
| 88 | puts("SWAP UNKNOWN - Failed to retrieve Swap usage"); | ||
| 89 | exit(STATE_UNKNOWN); | ||
| 90 | } | ||
| 91 | |||
| 86 | double percent_used; | 92 | double percent_used; |
| 87 | /* if total_swap_mb == 0, let's not divide by 0 */ | 93 | /* if total_swap_mb == 0, let's not divide by 0 */ |
| 88 | if (data.metrics.total != 0) { | 94 | 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 @@ | |||
| 1 | #include "./check_swap.d/check_swap.h" | 1 | #include "./check_swap.d/check_swap.h" |
| 2 | #include "../popen.h" | 2 | #include "../popen.h" |
| 3 | #include "../utils.h" | 3 | #include "../utils.h" |
| 4 | #include "common.h" | ||
| 4 | 5 | ||
| 5 | extern int verbose; | 6 | extern int verbose; |
| 6 | 7 | ||
| @@ -66,7 +67,7 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
| 66 | meminfo_file_ptr = fopen(proc_meminfo, "r"); | 67 | meminfo_file_ptr = fopen(proc_meminfo, "r"); |
| 67 | 68 | ||
| 68 | swap_result result = {0}; | 69 | swap_result result = {0}; |
| 69 | result.statusCode = STATE_OK; | 70 | result.errorcode = STATE_UNKNOWN; |
| 70 | 71 | ||
| 71 | uint64_t swap_total = 0; | 72 | uint64_t swap_total = 0; |
| 72 | uint64_t swap_used = 0; | 73 | uint64_t swap_used = 0; |
| @@ -91,6 +92,9 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
| 91 | result.metrics.used += swap_used; | 92 | result.metrics.used += swap_used; |
| 92 | result.metrics.free += swap_free; | 93 | result.metrics.free += swap_free; |
| 93 | 94 | ||
| 95 | // Set error | ||
| 96 | result.errorcode = STATE_OK; | ||
| 97 | |||
| 94 | /* | 98 | /* |
| 95 | * The following sscanf call looks for lines looking like: | 99 | * The following sscanf call looks for lines looking like: |
| 96 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least | 100 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least |
| @@ -113,6 +117,8 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
| 113 | } else if (strcmp("Cached", str) == 0) { | 117 | } else if (strcmp("Cached", str) == 0) { |
| 114 | swap_free = swap_free + tmp_KB * 1024; | 118 | swap_free = swap_free + tmp_KB * 1024; |
| 115 | } | 119 | } |
| 120 | |||
| 121 | result.errorcode = STATE_OK; | ||
| 116 | } | 122 | } |
| 117 | } | 123 | } |
| 118 | 124 | ||
