diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-06-28 09:53:59 +0200 | 
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-06-28 09:53:59 +0200 | 
| commit | 882ef5015f43856994be5a3766ca3fe2005908b0 (patch) | |
| tree | 3ed241a4f9ac3d1e01acd70a5df81b62a807a034 /plugins/check_swap.d | |
| parent | a746576b8cb72a3233caf5ac852b2679cc98d80c (diff) | |
| parent | f43c52194ce60b7a7fec463be0df7025705b8924 (diff) | |
| download | monitoring-plugins-882ef5015f43856994be5a3766ca3fe2005908b0.tar.gz | |
Merge branch 'master' into refactor/check_procs
Diffstat (limited to 'plugins/check_swap.d')
| -rw-r--r-- | plugins/check_swap.d/check_swap.h | 5 | ||||
| -rw-r--r-- | plugins/check_swap.d/swap.c | 102 | 
2 files changed, 51 insertions, 56 deletions
| diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h index 1000fc9e..da08d65a 100644 --- a/plugins/check_swap.d/check_swap.h +++ b/plugins/check_swap.d/check_swap.h | |||
| @@ -1,7 +1,8 @@ | |||
| 1 | #pragma once | 1 | #pragma once | 
| 2 | 2 | ||
| 3 | #include "../common.h" | 3 | #include "../common.h" | 
| 4 | #include "output.h" | 4 | #include "../../lib/output.h" | 
| 5 | #include "../../lib/states.h" | ||
| 5 | 6 | ||
| 6 | #ifndef SWAP_CONVERSION | 7 | #ifndef SWAP_CONVERSION | 
| 7 | # define SWAP_CONVERSION 1 | 8 | # define SWAP_CONVERSION 1 | 
| @@ -26,7 +27,7 @@ typedef struct { | |||
| 26 | 27 | ||
| 27 | typedef struct { | 28 | typedef struct { | 
| 28 | bool allswaps; | 29 | bool allswaps; | 
| 29 | int no_swap_state; | 30 | mp_state_enum no_swap_state; | 
| 30 | bool warn_is_set; | 31 | bool warn_is_set; | 
| 31 | check_swap_threshold warn; | 32 | check_swap_threshold warn; | 
| 32 | bool crit_is_set; | 33 | bool crit_is_set; | 
| diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 180d5037..634f80d9 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c | |||
| @@ -68,7 +68,7 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
| 68 | FILE *meminfo_file_ptr; | 68 | FILE *meminfo_file_ptr; | 
| 69 | meminfo_file_ptr = fopen(proc_meminfo, "r"); | 69 | meminfo_file_ptr = fopen(proc_meminfo, "r"); | 
| 70 | 70 | ||
| 71 | swap_result result = {0}; | 71 | swap_result result = {}; | 
| 72 | result.errorcode = STATE_UNKNOWN; | 72 | result.errorcode = STATE_UNKNOWN; | 
| 73 | 73 | ||
| 74 | if (meminfo_file_ptr == NULL) { | 74 | if (meminfo_file_ptr == NULL) { | 
| @@ -78,83 +78,71 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
| 78 | return result; | 78 | return result; | 
| 79 | } | 79 | } | 
| 80 | 80 | ||
| 81 | uint64_t swap_total = 0; | 81 | unsigned long swap_total = 0; | 
| 82 | uint64_t swap_used = 0; | 82 | unsigned long swap_used = 0; | 
| 83 | uint64_t swap_free = 0; | 83 | unsigned long swap_free = 0; | 
| 84 | 84 | ||
| 85 | bool found_total = false; | 85 | bool found_total = false; | 
| 86 | bool found_used = false; | ||
| 87 | bool found_free = false; | 86 | bool found_free = false; | 
| 88 | 87 | ||
| 89 | char input_buffer[MAX_INPUT_BUFFER]; | 88 | char input_buffer[MAX_INPUT_BUFFER]; | 
| 90 | char str[32]; | 89 | char str[32]; | 
| 91 | 90 | ||
| 92 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, meminfo_file_ptr)) { | 91 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, meminfo_file_ptr)) { | 
| 93 | uint64_t tmp_KB = 0; | ||
| 94 | 92 | ||
| 95 | /* | 93 | /* | 
| 96 | * The following sscanf call looks for a line looking like: "Swap: 123 | 94 | * The following sscanf call looks for a line looking like: "Swap: 123 | 
| 97 | * 123 123" On which kind of system this format exists, I can not say, | 95 | * 123 123" which exists on NetBSD (at least), | 
| 98 | * but I wanted to document this for people who are not adapt with | 96 | * The unit should be Bytes | 
| 99 | * sscanf anymore, like me | ||
| 100 | * Also the units used here are unclear and probably wrong | ||
| 101 | */ | 97 | */ | 
| 102 | if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &swap_total, &swap_used, &swap_free) == 3) { | 98 | if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &swap_total, &swap_used, &swap_free) == 3) { | 
| 103 | |||
| 104 | result.metrics.total += swap_total; | ||
| 105 | result.metrics.used += swap_used; | ||
| 106 | result.metrics.free += swap_free; | ||
| 107 | |||
| 108 | found_total = true; | 99 | found_total = true; | 
| 109 | found_free = true; | 100 | found_free = true; | 
| 110 | found_used = true; | ||
| 111 | |||
| 112 | // Set error | 101 | // Set error | 
| 113 | result.errorcode = STATE_OK; | 102 | result.errorcode = STATE_OK; | 
| 103 | // Break out of fgets here, since both scanf expressions might match (NetBSD for example) | ||
| 104 | break; | ||
| 105 | } | ||
| 106 | |||
| 107 | /* | ||
| 108 | * The following sscanf call looks for lines looking like: | ||
| 109 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least | ||
| 110 | * on Debian Linux with a 5.* kernel | ||
| 111 | */ | ||
| 112 | unsigned long tmp_KB = 0; | ||
| 113 | int sscanf_result = sscanf(input_buffer, | ||
| 114 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " | ||
| 115 | "%*[k]%*[B]", | ||
| 116 | str, &tmp_KB); | ||
| 117 | |||
| 118 | if (sscanf_result == 2) { | ||
| 119 | |||
| 120 | if (verbose >= 3) { | ||
| 121 | printf("Got %s with %lu\n", str, tmp_KB); | ||
| 122 | } | ||
| 114 | 123 | ||
| 115 | /* | 124 | /* I think this part is always in Kb, so convert to bytes */ | 
| 116 | * The following sscanf call looks for lines looking like: | 125 | if (strcmp("Total", str) == 0) { | 
| 117 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least | 126 | swap_total = tmp_KB * 1000; | 
| 118 | * on Debian Linux with a 5.* kernel | 127 | found_total = true; | 
| 119 | */ | 128 | } else if (strcmp("Free", str) == 0) { | 
| 120 | } else { | 129 | swap_free += tmp_KB * 1000; | 
| 121 | int sscanf_result = sscanf(input_buffer, | 130 | found_free = true; | 
| 122 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " | 131 | } else if (strcmp("Cached", str) == 0) { | 
| 123 | "%*[k]%*[B]", | 132 | swap_free += tmp_KB * 1000; | 
| 124 | str, &tmp_KB); | ||
| 125 | |||
| 126 | if (sscanf_result == 2) { | ||
| 127 | |||
| 128 | if (verbose >= 3) { | ||
| 129 | printf("Got %s with %lu\n", str, tmp_KB); | ||
| 130 | } | ||
| 131 | |||
| 132 | /* I think this part is always in Kb, so convert to bytes */ | ||
| 133 | if (strcmp("Total", str) == 0) { | ||
| 134 | swap_total = tmp_KB * 1000; | ||
| 135 | found_total = true; | ||
| 136 | } else if (strcmp("Free", str) == 0) { | ||
| 137 | swap_free = swap_free + tmp_KB * 1000; | ||
| 138 | found_free = true; | ||
| 139 | found_used = true; // No explicit used metric available | ||
| 140 | } else if (strcmp("Cached", str) == 0) { | ||
| 141 | swap_free = swap_free + tmp_KB * 1000; | ||
| 142 | found_free = true; | ||
| 143 | found_used = true; // No explicit used metric available | ||
| 144 | } | ||
| 145 | |||
| 146 | result.errorcode = STATE_OK; | ||
| 147 | } | 133 | } | 
| 134 | |||
| 135 | result.errorcode = STATE_OK; | ||
| 148 | } | 136 | } | 
| 149 | } | 137 | } | 
| 150 | 138 | ||
| 151 | fclose(meminfo_file_ptr); | 139 | fclose(meminfo_file_ptr); | 
| 152 | 140 | ||
| 153 | result.metrics.total = swap_total; | 141 | result.metrics.total = swap_total; | 
| 154 | result.metrics.used = swap_total - swap_free; | ||
| 155 | result.metrics.free = swap_free; | 142 | result.metrics.free = swap_free; | 
| 143 | result.metrics.used = swap_total - swap_free; | ||
| 156 | 144 | ||
| 157 | if (!found_free || !found_total || !found_used) { | 145 | if (!found_free || !found_total) { | 
| 158 | result.errorcode = STATE_UNKNOWN; | 146 | result.errorcode = STATE_UNKNOWN; | 
| 159 | } | 147 | } | 
| 160 | 148 | ||
| @@ -297,8 +285,14 @@ struct swapent { | |||
| 297 | }; | 285 | }; | 
| 298 | 286 | ||
| 299 | #else | 287 | #else | 
| 288 | |||
| 289 | // Includes for NetBSD | ||
| 290 | # include <unistd.h> | ||
| 291 | # include <sys/swap.h> | ||
| 292 | |||
| 300 | # define bsd_swapctl swapctl | 293 | # define bsd_swapctl swapctl | 
| 301 | #endif | 294 | |
| 295 | #endif // CHECK_SWAP_SWAPCTL_BSD | ||
| 302 | 296 | ||
| 303 | swap_result getSwapFromSwapctl_BSD(swap_config config) { | 297 | swap_result getSwapFromSwapctl_BSD(swap_config config) { | 
| 304 | /* get the number of active swap devices */ | 298 | /* get the number of active swap devices */ | 
| @@ -322,8 +316,8 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) { | |||
| 322 | unsigned long long used_swap_mb = 0; | 316 | unsigned long long used_swap_mb = 0; | 
| 323 | 317 | ||
| 324 | for (int i = 0; i < nswaps; i++) { | 318 | for (int i = 0; i < nswaps; i++) { | 
| 325 | dsktotal_mb = (float)ent[i].se_nblks / (float)config.conversion_factor; | 319 | dsktotal_mb = (double)ent[i].se_nblks / (double)config.conversion_factor; | 
| 326 | dskused_mb = (float)ent[i].se_inuse / (float)config.conversion_factor; | 320 | dskused_mb = (double)ent[i].se_inuse / (double)config.conversion_factor; | 
| 327 | dskfree_mb = (dsktotal_mb - dskused_mb); | 321 | dskfree_mb = (dsktotal_mb - dskused_mb); | 
| 328 | 322 | ||
| 329 | if (config.allswaps && dsktotal_mb > 0) { | 323 | if (config.allswaps && dsktotal_mb > 0) { | 
