diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-11-15 15:05:28 +0100 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-11-15 15:05:28 +0100 |
commit | 72676bdc14c22818f9154fbee9de54ec6ae61c89 (patch) | |
tree | 85cc59c875968e51fb9bd29fa5d4d1a2295321fa | |
parent | 23970766e71aa66c49a2a782dc94f5a58d4e4616 (diff) | |
download | monitoring-plugins-72676bdc14c22818f9154fbee9de54ec6ae61c89.tar.gz |
check_swap stuff: improve error handling
-rw-r--r-- | plugins/check_swap.d/swap.c | 59 |
1 files changed, 40 insertions, 19 deletions
diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index ece05fd2..9133c4fe 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c | |||
@@ -73,6 +73,10 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
73 | uint64_t swap_used = 0; | 73 | uint64_t swap_used = 0; |
74 | uint64_t swap_free = 0; | 74 | uint64_t swap_free = 0; |
75 | 75 | ||
76 | bool found_total = false; | ||
77 | bool found_used = false; | ||
78 | bool found_free = false; | ||
79 | |||
76 | char input_buffer[MAX_INPUT_BUFFER]; | 80 | char input_buffer[MAX_INPUT_BUFFER]; |
77 | char str[32]; | 81 | char str[32]; |
78 | 82 | ||
@@ -92,7 +96,11 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
92 | result.metrics.used += swap_used; | 96 | result.metrics.used += swap_used; |
93 | result.metrics.free += swap_free; | 97 | result.metrics.free += swap_free; |
94 | 98 | ||
95 | // Set error | 99 | found_total = true; |
100 | found_free = true; | ||
101 | found_used = true; | ||
102 | |||
103 | // Set error | ||
96 | result.errorcode = STATE_OK; | 104 | result.errorcode = STATE_OK; |
97 | 105 | ||
98 | /* | 106 | /* |
@@ -100,25 +108,34 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
100 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least | 108 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least |
101 | * on Debian Linux with a 5.* kernel | 109 | * on Debian Linux with a 5.* kernel |
102 | */ | 110 | */ |
103 | } else if (sscanf(input_buffer, | 111 | } else { |
104 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " | 112 | int sscanf_result = sscanf(input_buffer, |
105 | "%*[k]%*[B]", | 113 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " |
106 | str, &tmp_KB) == 2) { | 114 | "%*[k]%*[B]", |
107 | 115 | str, &tmp_KB); | |
108 | if (verbose >= 3) { | 116 | |
109 | printf("Got %s with %lu\n", str, tmp_KB); | 117 | if (sscanf_result == 2) { |
110 | } | 118 | |
111 | 119 | if (verbose >= 3) { | |
112 | /* I think this part is always in Kb, so convert to bytes */ | 120 | printf("Got %s with %lu\n", str, tmp_KB); |
113 | if (strcmp("Total", str) == 0) { | 121 | } |
114 | swap_total = tmp_KB * 1024; | 122 | |
115 | } else if (strcmp("Free", str) == 0) { | 123 | /* I think this part is always in Kb, so convert to bytes */ |
116 | swap_free = swap_free + tmp_KB * 1024; | 124 | if (strcmp("Total", str) == 0) { |
117 | } else if (strcmp("Cached", str) == 0) { | 125 | swap_total = tmp_KB * 1000; |
118 | swap_free = swap_free + tmp_KB * 1024; | 126 | found_total = true; |
127 | } else if (strcmp("Free", str) == 0) { | ||
128 | swap_free = swap_free + tmp_KB * 1000; | ||
129 | found_free = true; | ||
130 | found_used = true; // No explicit used metric available | ||
131 | } else if (strcmp("Cached", str) == 0) { | ||
132 | swap_free = swap_free + tmp_KB * 1000; | ||
133 | found_free = true; | ||
134 | found_used = true; // No explicit used metric available | ||
135 | } | ||
136 | |||
137 | result.errorcode = STATE_OK; | ||
119 | } | 138 | } |
120 | |||
121 | result.errorcode = STATE_OK; | ||
122 | } | 139 | } |
123 | } | 140 | } |
124 | 141 | ||
@@ -128,6 +145,10 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
128 | result.metrics.used = swap_total - swap_free; | 145 | result.metrics.used = swap_total - swap_free; |
129 | result.metrics.free = swap_free; | 146 | result.metrics.free = swap_free; |
130 | 147 | ||
148 | if (!found_free || !found_total || !found_used) { | ||
149 | result.errorcode = STATE_UNKNOWN; | ||
150 | } | ||
151 | |||
131 | return result; | 152 | return result; |
132 | } | 153 | } |
133 | 154 | ||