diff options
| author | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2023-12-13 17:13:23 +0100 |
|---|---|---|
| committer | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2023-12-20 10:02:47 +0100 |
| commit | 22a423ad5d2cc1d117c5fd933d2fd6901f4434c6 (patch) | |
| tree | e9d4bcf242835b3c4099f57cc1a5f07b0de00155 | |
| parent | 3a10773c0918a190c43d1508f9f572709fba25a8 (diff) | |
| download | monitoring-plugins-22a423ad5d2cc1d117c5fd933d2fd6901f4434c6.tar.gz | |
check_swap: Return byte number (in linux) and simplify code after that
| -rw-r--r-- | plugins/check_swap.c | 76 |
1 files changed, 42 insertions, 34 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 60309a76..6e995156 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c | |||
| @@ -156,19 +156,28 @@ int main (int argc, char **argv) { | |||
| 156 | 156 | ||
| 157 | uint64_t warn_print = config.warn.value; | 157 | uint64_t warn_print = config.warn.value; |
| 158 | if (config.warn.is_percentage) { | 158 | if (config.warn.is_percentage) { |
| 159 | warn_print = config.warn.value * (data.metrics.total*1024 *1024/100); | 159 | warn_print = |
| 160 | config.warn.value * (data.metrics.total / 100); | ||
| 160 | } | 161 | } |
| 161 | 162 | ||
| 162 | uint64_t crit_print = config.crit.value; | 163 | uint64_t crit_print = config.crit.value; |
| 163 | if (config.crit.is_percentage) { | 164 | if (config.crit.is_percentage) { |
| 164 | crit_print = config.crit.value * (data.metrics.total*1024 *1024/100); | 165 | crit_print = |
| 166 | config.crit.value * (data.metrics.total / 100); | ||
| 165 | } | 167 | } |
| 166 | 168 | ||
| 167 | char *perfdata = perfdata_uint64 ("swap", data.metrics.free *1024 *1024, "B", | 169 | char *perfdata = perfdata_uint64( |
| 168 | true, warn_print, | 170 | "swap", |
| 169 | true, crit_print, | 171 | data.metrics.free, |
| 170 | true, 0, | 172 | "B", |
| 171 | true, (long) data.metrics.total* 1024 * 1024); | 173 | true, warn_print, |
| 174 | true, crit_print, | ||
| 175 | true, 0, | ||
| 176 | true, (long)data.metrics.total); | ||
| 177 | |||
| 178 | if (config.verbose > 1) { | ||
| 179 | printf("Warn threshold value: %"PRIu64"\n", config.warn.value); | ||
| 180 | } | ||
| 172 | 181 | ||
| 173 | if ((config.warn.is_percentage && (percent_used >= (100 - config.warn.value))) || | 182 | if ((config.warn.is_percentage && (percent_used >= (100 - config.warn.value))) || |
| 174 | config.warn.value >= data.metrics.free) { | 183 | config.warn.value >= data.metrics.free) { |
| @@ -188,7 +197,6 @@ int main (int argc, char **argv) { | |||
| 188 | exit(data.statusCode); | 197 | exit(data.statusCode); |
| 189 | } | 198 | } |
| 190 | 199 | ||
| 191 | |||
| 192 | /* process command-line arguments */ | 200 | /* process command-line arguments */ |
| 193 | swap_config_wrapper process_arguments (swap_config_wrapper conf_wrapper, int argc, char **argv) { | 201 | swap_config_wrapper process_arguments (swap_config_wrapper conf_wrapper, int argc, char **argv) { |
| 194 | if (argc < 2) { | 202 | if (argc < 2) { |
| @@ -377,7 +385,7 @@ swap_result getSwapFromProcMeminfo(swap_config config) { | |||
| 377 | swap_result result = { 0 }; | 385 | swap_result result = { 0 }; |
| 378 | result.statusCode = STATE_OK; | 386 | result.statusCode = STATE_OK; |
| 379 | 387 | ||
| 380 | uint64_t dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0; | 388 | uint64_t swap_total = 0, swap_used = 0, swap_free = 0; |
| 381 | 389 | ||
| 382 | char input_buffer[MAX_INPUT_BUFFER]; | 390 | char input_buffer[MAX_INPUT_BUFFER]; |
| 383 | char str[32]; | 391 | char str[32]; |
| @@ -390,41 +398,41 @@ swap_result getSwapFromProcMeminfo(swap_config config) { | |||
| 390 | * On which kind of system this format exists, I can not say, but I wanted to | 398 | * On which kind of system this format exists, I can not say, but I wanted to |
| 391 | * document this for people who are not adapt with sscanf anymore, like me | 399 | * document this for people who are not adapt with sscanf anymore, like me |
| 392 | */ | 400 | */ |
| 393 | if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &dsktotal_mb, &dskused_mb, &dskfree_mb) == 3) { | 401 | if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", |
| 394 | dsktotal_mb = dsktotal_mb / (1024 * 1024); /* Apply conversion */ | 402 | &swap_total, &swap_used, &swap_free) == 3) { |
| 395 | dskused_mb = dskused_mb / (1024 * 1024); | 403 | |
| 396 | dskfree_mb = dskfree_mb / (1024 * 1024); | 404 | result.metrics.total += swap_total; |
| 397 | 405 | result.metrics.used += swap_used; | |
| 398 | result.metrics.total += dsktotal_mb; | 406 | result.metrics.free += swap_free; |
| 399 | result.metrics.used+= dskused_mb; | 407 | |
| 400 | result.metrics.free += dskfree_mb; | 408 | /* |
| 401 | 409 | * The following sscanf call looks for lines looking like: | |
| 402 | 410 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least | |
| 403 | 411 | * on Debian Linux with a 5.* kernel | |
| 404 | /* | 412 | */ |
| 405 | * The following sscanf call looks for lines looking like: "SwapTotal: 123" and "SwapFree: 123" | 413 | } else if (sscanf(input_buffer, |
| 406 | * This format exists at least on Debian Linux with a 5.* kernel | 414 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " |
| 407 | */ | 415 | "%*[k]%*[B]", |
| 408 | } else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu %*[k]%*[B]", str, &tmp_KB)) { | 416 | str, &tmp_KB)) { |
| 409 | if (config.verbose >= 3) { | 417 | if (config.verbose >= 3) { |
| 410 | printf("Got %s with %lu\n", str, tmp_KB); | 418 | printf("Got %s with %lu\n", str, tmp_KB); |
| 411 | } | 419 | } |
| 412 | /* I think this part is always in Kb, so convert to mb */ | 420 | /* I think this part is always in Kb, so convert to mb */ |
| 413 | if (strcmp ("Total", str) == 0) { | 421 | if (strcmp("Total", str) == 0) { |
| 414 | dsktotal_mb = tmp_KB / 1024; | 422 | swap_total = tmp_KB * 1024; |
| 415 | } else if (strcmp ("Free", str) == 0) { | 423 | } else if (strcmp("Free", str) == 0) { |
| 416 | dskfree_mb = dskfree_mb + tmp_KB / 1024; | 424 | swap_free = swap_free + tmp_KB * 1024; |
| 417 | } else if (strcmp ("Cached", str) == 0) { | 425 | } else if (strcmp("Cached", str) == 0) { |
| 418 | dskfree_mb = dskfree_mb + tmp_KB / 1024; | 426 | swap_free = swap_free + tmp_KB * 1024; |
| 419 | } | 427 | } |
| 420 | } | 428 | } |
| 421 | } | 429 | } |
| 422 | 430 | ||
| 423 | fclose(fp); | 431 | fclose(fp); |
| 424 | 432 | ||
| 425 | result.metrics.total = dsktotal_mb; | 433 | result.metrics.total = swap_total; |
| 426 | result.metrics.used = dsktotal_mb - dskfree_mb; | 434 | result.metrics.used = swap_total - swap_free; |
| 427 | result.metrics.free = dskfree_mb; | 435 | result.metrics.free = swap_free; |
| 428 | 436 | ||
| 429 | return result; | 437 | return result; |
| 430 | } | 438 | } |
