diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-11-02 13:53:33 +0100 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-11-02 13:53:33 +0100 |
commit | ed01d534474cc640515f1d5155349f14090aafe9 (patch) | |
tree | 1a23878ddffc675b9fce6598d51a2bf376caa871 | |
parent | 3faeed07c4825d5c3ceb323e814e703d9262cd82 (diff) | |
download | monitoring-plugins-ed01d534474cc640515f1d5155349f14090aafe9.tar.gz |
Small fixes to check_swap stuff
-rw-r--r-- | plugins/check_swap.c | 9 | ||||
-rw-r--r-- | plugins/check_swap.d/swap.c | 64 |
2 files changed, 48 insertions, 25 deletions
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); | |||
56 | void print_usage(void); | 56 | void print_usage(void); |
57 | static void print_help(swap_config /*config*/); | 57 | static void print_help(swap_config /*config*/); |
58 | 58 | ||
59 | static int verbose; | 59 | int verbose; |
60 | 60 | ||
61 | #define HUNDRED_PERCENT 100 | 61 | #define HUNDRED_PERCENT 100 |
62 | 62 | ||
63 | #define BYTES_TO_KiB(number) (number / 1024) | ||
64 | #define BYTES_TO_MiB(number) (BYTES_TO_KiB(number) / 1024) | ||
65 | |||
63 | int main(int argc, char **argv) { | 66 | int main(int argc, char **argv) { |
64 | setlocale(LC_ALL, ""); | 67 | setlocale(LC_ALL, ""); |
65 | bindtextdomain(PACKAGE, LOCALEDIR); | 68 | bindtextdomain(PACKAGE, LOCALEDIR); |
@@ -127,8 +130,8 @@ int main(int argc, char **argv) { | |||
127 | data.statusCode = max_state(data.statusCode, STATE_CRITICAL); | 130 | data.statusCode = max_state(data.statusCode, STATE_CRITICAL); |
128 | } | 131 | } |
129 | 132 | ||
130 | printf(_("SWAP %s - %g%% free (%lluMB out of %lluMB) %s|%s\n"), state_text(data.statusCode), (HUNDRED_PERCENT - percent_used), | 133 | printf(_("SWAP %s - %g%% free (%lluMiB out of %lluMiB) %s|%s\n"), state_text(data.statusCode), (HUNDRED_PERCENT - percent_used), |
131 | data.metrics.free, data.metrics.total, status, perfdata); | 134 | BYTES_TO_MiB(data.metrics.free), BYTES_TO_MiB(data.metrics.total), status, perfdata); |
132 | 135 | ||
133 | exit(data.statusCode); | 136 | exit(data.statusCode); |
134 | } | 137 | } |
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) { | |||
59 | } | 59 | } |
60 | 60 | ||
61 | swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | 61 | swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { |
62 | FILE *fp; | 62 | FILE *meminfo_file_ptr; |
63 | fp = fopen(proc_meminfo, "r"); | 63 | meminfo_file_ptr = fopen(proc_meminfo, "r"); |
64 | 64 | ||
65 | swap_result result = {0}; | 65 | swap_result result = {0}; |
66 | result.statusCode = STATE_OK; | 66 | result.statusCode = STATE_OK; |
67 | 67 | ||
68 | uint64_t swap_total = 0, swap_used = 0, swap_free = 0; | 68 | uint64_t swap_total = 0; |
69 | uint64_t swap_used = 0; | ||
70 | uint64_t swap_free = 0; | ||
69 | 71 | ||
70 | char input_buffer[MAX_INPUT_BUFFER]; | 72 | char input_buffer[MAX_INPUT_BUFFER]; |
71 | char str[32]; | 73 | char str[32]; |
72 | 74 | ||
73 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) { | 75 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, meminfo_file_ptr)) { |
74 | uint64_t tmp_KB = 0; | 76 | uint64_t tmp_KB = 0; |
75 | 77 | ||
76 | /* | 78 | /* |
@@ -111,7 +113,7 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
111 | } | 113 | } |
112 | } | 114 | } |
113 | 115 | ||
114 | fclose(fp); | 116 | fclose(meminfo_file_ptr); |
115 | 117 | ||
116 | result.metrics.total = swap_total; | 118 | result.metrics.total = swap_total; |
117 | result.metrics.used = swap_total - swap_free; | 119 | result.metrics.used = swap_total - swap_free; |
@@ -125,10 +127,12 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[] | |||
125 | 127 | ||
126 | char *temp_buffer; | 128 | char *temp_buffer; |
127 | 129 | ||
128 | if (verbose >= 2) | 130 | if (verbose >= 2) { |
129 | printf(_("Command: %s\n"), swap_command); | 131 | printf(_("Command: %s\n"), swap_command); |
130 | if (verbose >= 3) | 132 | } |
133 | if (verbose >= 3) { | ||
131 | printf(_("Format: %s\n"), swap_format); | 134 | printf(_("Format: %s\n"), swap_format); |
135 | } | ||
132 | 136 | ||
133 | child_process = spopen(swap_command); | 137 | child_process = spopen(swap_command); |
134 | if (child_process == NULL) { | 138 | if (child_process == NULL) { |
@@ -152,18 +156,23 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[] | |||
152 | if (strcmp(swap_format, "") == 0) { | 156 | if (strcmp(swap_format, "") == 0) { |
153 | temp_buffer = strtok(input_buffer, " \n"); | 157 | temp_buffer = strtok(input_buffer, " \n"); |
154 | while (temp_buffer) { | 158 | while (temp_buffer) { |
155 | if (strstr(temp_buffer, "blocks")) | 159 | if (strstr(temp_buffer, "blocks")) { |
156 | sprintf(str, "%s %s", str, "%lu"); | 160 | sprintf(str, "%s %s", str, "%lu"); |
157 | else if (strstr(temp_buffer, "dskfree")) | 161 | } else if (strstr(temp_buffer, "dskfree")) { |
158 | sprintf(str, "%s %s", str, "%lu"); | 162 | sprintf(str, "%s %s", str, "%lu"); |
159 | else | 163 | } else { |
160 | sprintf(str, "%s %s", str, "%*s"); | 164 | sprintf(str, "%s %s", str, "%*s"); |
165 | } | ||
161 | temp_buffer = strtok(NULL, " \n"); | 166 | temp_buffer = strtok(NULL, " \n"); |
162 | } | 167 | } |
163 | } | 168 | } |
164 | 169 | ||
165 | double total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; | 170 | double total_swap_mb = 0; |
166 | double dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0; | 171 | double free_swap_mb = 0; |
172 | double used_swap_mb = 0; | ||
173 | double dsktotal_mb = 0; | ||
174 | double dskused_mb = 0; | ||
175 | double dskfree_mb = 0; | ||
167 | 176 | ||
168 | /* | 177 | /* |
169 | * If different swap command is used for summary switch, need to read format | 178 | * 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[] | |||
191 | dskfree_mb = dskfree_mb / config.conversion_factor; | 200 | dskfree_mb = dskfree_mb / config.conversion_factor; |
192 | } | 201 | } |
193 | 202 | ||
194 | if (verbose >= 3) | 203 | if (verbose >= 3) { |
195 | printf(_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb); | 204 | printf(_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb); |
205 | } | ||
196 | 206 | ||
197 | dskused_mb = dsktotal_mb - dskfree_mb; | 207 | dskused_mb = dsktotal_mb - dskfree_mb; |
198 | total_swap_mb += dsktotal_mb; | 208 | total_swap_mb += dsktotal_mb; |
@@ -256,7 +266,7 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) { | |||
256 | int nswaps = bsd_swapctl(SWAP_NSWAP, NULL, 0); | 266 | int nswaps = bsd_swapctl(SWAP_NSWAP, NULL, 0); |
257 | 267 | ||
258 | /* initialize swap table + entries */ | 268 | /* initialize swap table + entries */ |
259 | struct swapent *ent = (struct swapent *)malloc(sizeof(struct swapent) * nswaps); | 269 | struct swapent *ent = (struct swapent *)malloc(sizeof(struct swapent) * (unsigned long)nswaps); |
260 | 270 | ||
261 | /* and now, tally 'em up */ | 271 | /* and now, tally 'em up */ |
262 | int swapctl_res = bsd_swapctl(SWAP_STATS, ent, nswaps); | 272 | int swapctl_res = bsd_swapctl(SWAP_STATS, ent, nswaps); |
@@ -265,8 +275,12 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) { | |||
265 | die(STATE_UNKNOWN, _("Error in swapctl call\n")); | 275 | die(STATE_UNKNOWN, _("Error in swapctl call\n")); |
266 | } | 276 | } |
267 | 277 | ||
268 | double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; | 278 | double dsktotal_mb = 0.0; |
269 | unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; | 279 | double dskfree_mb = 0.0; |
280 | double dskused_mb = 0.0; | ||
281 | unsigned long long total_swap_mb = 0; | ||
282 | unsigned long long free_swap_mb = 0; | ||
283 | unsigned long long used_swap_mb = 0; | ||
270 | 284 | ||
271 | for (int i = 0; i < nswaps; i++) { | 285 | for (int i = 0; i < nswaps; i++) { |
272 | dsktotal_mb = (float)ent[i].se_nblks / config.conversion_factor; | 286 | dsktotal_mb = (float)ent[i].se_nblks / config.conversion_factor; |
@@ -338,27 +352,32 @@ swap_result getSwapFromSwap_SRV4(swap_config config) { | |||
338 | int nswaps = 0; | 352 | int nswaps = 0; |
339 | 353 | ||
340 | /* get the number of active swap devices */ | 354 | /* get the number of active swap devices */ |
341 | if ((nswaps = srv4_swapctl(SC_GETNSWP, NULL)) == -1) | 355 | if ((nswaps = srv4_swapctl(SC_GETNSWP, NULL)) == -1) { |
342 | die(STATE_UNKNOWN, _("Error getting swap devices\n")); | 356 | die(STATE_UNKNOWN, _("Error getting swap devices\n")); |
357 | } | ||
343 | 358 | ||
344 | if (nswaps == 0) | 359 | if (nswaps == 0) { |
345 | die(STATE_OK, _("SWAP OK: No swap devices defined\n")); | 360 | die(STATE_OK, _("SWAP OK: No swap devices defined\n")); |
361 | } | ||
346 | 362 | ||
347 | if (verbose >= 3) | 363 | if (verbose >= 3) { |
348 | printf("Found %d swap device(s)\n", nswaps); | 364 | printf("Found %d swap device(s)\n", nswaps); |
365 | } | ||
349 | 366 | ||
350 | /* initialize swap table + entries */ | 367 | /* initialize swap table + entries */ |
351 | swaptbl_t *tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); | 368 | swaptbl_t *tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); |
352 | 369 | ||
353 | if (tbl == NULL) | 370 | if (tbl == NULL) { |
354 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | 371 | die(STATE_UNKNOWN, _("malloc() failed!\n")); |
372 | } | ||
355 | 373 | ||
356 | memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); | 374 | memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); |
357 | tbl->swt_n = nswaps; | 375 | tbl->swt_n = nswaps; |
358 | 376 | ||
359 | for (int i = 0; i < nswaps; i++) { | 377 | for (int i = 0; i < nswaps; i++) { |
360 | if ((tbl->swt_ent[i].ste_path = (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL) | 378 | if ((tbl->swt_ent[i].ste_path = (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL) { |
361 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | 379 | die(STATE_UNKNOWN, _("malloc() failed!\n")); |
380 | } | ||
362 | } | 381 | } |
363 | 382 | ||
364 | /* and now, tally 'em up */ | 383 | /* and now, tally 'em up */ |
@@ -380,8 +399,9 @@ swap_result getSwapFromSwap_SRV4(swap_config config) { | |||
380 | dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION; | 399 | dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION; |
381 | dskused_mb = (dsktotal_mb - dskfree_mb); | 400 | dskused_mb = (dsktotal_mb - dskfree_mb); |
382 | 401 | ||
383 | if (verbose >= 3) | 402 | if (verbose >= 3) { |
384 | printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb); | 403 | printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb); |
404 | } | ||
385 | 405 | ||
386 | if (config.allswaps && dsktotal_mb > 0) { | 406 | if (config.allswaps && dsktotal_mb > 0) { |
387 | double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); | 407 | double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); |