diff options
author | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2023-12-19 11:18:51 +0100 |
---|---|---|
committer | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2023-12-20 10:02:47 +0100 |
commit | 2dec5182c508bd3cc286b4649836ead51aec50ef (patch) | |
tree | 682dbf85ed7452f3101a200d925991c281b7d465 /plugins/check_swap.d/swap.c | |
parent | 2289d094ae00a52f96a038cb7ca8bb350aec483a (diff) | |
download | monitoring-plugins-2dec5182c508bd3cc286b4649836ead51aec50ef.tar.gz |
check_swap: refactor to improve readability
Diffstat (limited to 'plugins/check_swap.d/swap.c')
-rw-r--r-- | plugins/check_swap.d/swap.c | 359 |
1 files changed, 359 insertions, 0 deletions
diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c new file mode 100644 index 00000000..ebfa840d --- /dev/null +++ b/plugins/check_swap.d/swap.c | |||
@@ -0,0 +1,359 @@ | |||
1 | #include "./check_swap.d/check_swap.h" | ||
2 | |||
3 | swap_config swap_config_init() { | ||
4 | swap_config tmp = {0}; | ||
5 | tmp.allswaps = false; | ||
6 | tmp.no_swap_state = STATE_CRITICAL; | ||
7 | tmp.verbose = 0; | ||
8 | tmp.conversion_factor = SWAP_CONVERSION; | ||
9 | |||
10 | #ifdef _AIX | ||
11 | tmp.on_aix = true; | ||
12 | #else | ||
13 | tmp.on_aix = false; | ||
14 | #endif | ||
15 | |||
16 | return tmp; | ||
17 | } | ||
18 | |||
19 | swap_result get_swap_data(swap_config config) { | ||
20 | #ifdef HAVE_PROC_MEMINFO | ||
21 | if (config.verbose >= 3) { | ||
22 | printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO); | ||
23 | } | ||
24 | |||
25 | return getSwapFromProcMeminfo(config, PROC_MEMINFO); | ||
26 | #else | ||
27 | #ifdef HAVE_SWAP | ||
28 | if (config.verbose >= 3) { | ||
29 | printf("Using swap command %s with format: %s\n", SWAP_COMMAND, SWAP_FORMAT); | ||
30 | } | ||
31 | |||
32 | /* These override the command used if a summary (and thus ! allswaps) is | ||
33 | * required | ||
34 | * The summary flag returns more accurate information about swap usage on these | ||
35 | * OSes */ | ||
36 | if (config.on_aix && !config.allswaps) { | ||
37 | |||
38 | config.conversion_factor = 1; | ||
39 | |||
40 | return getSwapFromSwapCommand(config, "/usr/sbin/lsps -s", "%lu%*s %lu"); | ||
41 | } else { | ||
42 | return getSwapFromSwapCommand(config, SWAP_COMMAND, SWAP_FORMAT); | ||
43 | } | ||
44 | #else | ||
45 | #ifdef CHECK_SWAP_SWAPCTL_SVR4 | ||
46 | return getSwapFromSwapctl_SRV4(); | ||
47 | #else | ||
48 | #ifdef CHECK_SWAP_SWAPCTL_BSD | ||
49 | return getSwapFromSwapctl_BSD(); | ||
50 | #else | ||
51 | #error No way found to retrieve swap | ||
52 | #endif /* CHECK_SWAP_SWAPCTL_BSD */ | ||
53 | #endif /* CHECK_SWAP_SWAPCTL_SVR4 */ | ||
54 | #endif /* HAVE_SWAP */ | ||
55 | #endif /* HAVE_PROC_MEMINFO */ | ||
56 | } | ||
57 | |||
58 | swap_result getSwapFromProcMeminfo(swap_config config, char proc_meminfo[]) { | ||
59 | FILE *fp; | ||
60 | fp = fopen(proc_meminfo, "r"); | ||
61 | |||
62 | swap_result result = {0}; | ||
63 | result.statusCode = STATE_OK; | ||
64 | |||
65 | uint64_t swap_total = 0, swap_used = 0, swap_free = 0; | ||
66 | |||
67 | char input_buffer[MAX_INPUT_BUFFER]; | ||
68 | char str[32]; | ||
69 | |||
70 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) { | ||
71 | uint64_t tmp_KB = 0; | ||
72 | |||
73 | /* | ||
74 | * The following sscanf call looks for a line looking like: "Swap: 123 | ||
75 | * 123 123" On which kind of system this format exists, I can not say, | ||
76 | * but I wanted to document this for people who are not adapt with | ||
77 | * sscanf anymore, like me | ||
78 | * Also the units used here are unclear and probably wrong | ||
79 | */ | ||
80 | if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", | ||
81 | &swap_total, &swap_used, &swap_free) == 3) { | ||
82 | |||
83 | result.metrics.total += swap_total; | ||
84 | result.metrics.used += swap_used; | ||
85 | result.metrics.free += swap_free; | ||
86 | |||
87 | /* | ||
88 | * The following sscanf call looks for lines looking like: | ||
89 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least | ||
90 | * on Debian Linux with a 5.* kernel | ||
91 | */ | ||
92 | } else if (sscanf(input_buffer, | ||
93 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " | ||
94 | "%*[k]%*[B]", | ||
95 | str, &tmp_KB)) { | ||
96 | |||
97 | if (config.verbose >= 3) { | ||
98 | printf("Got %s with %lu\n", str, tmp_KB); | ||
99 | } | ||
100 | |||
101 | /* I think this part is always in Kb, so convert to bytes */ | ||
102 | if (strcmp("Total", str) == 0) { | ||
103 | swap_total = tmp_KB * 1024; | ||
104 | } else if (strcmp("Free", str) == 0) { | ||
105 | swap_free = swap_free + tmp_KB * 1024; | ||
106 | } else if (strcmp("Cached", str) == 0) { | ||
107 | swap_free = swap_free + tmp_KB * 1024; | ||
108 | } | ||
109 | } | ||
110 | } | ||
111 | |||
112 | fclose(fp); | ||
113 | |||
114 | result.metrics.total = swap_total; | ||
115 | result.metrics.used = swap_total - swap_free; | ||
116 | result.metrics.free = swap_free; | ||
117 | |||
118 | return result; | ||
119 | } | ||
120 | |||
121 | swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]) { | ||
122 | swap_result result = {0}; | ||
123 | |||
124 | char *temp_buffer; | ||
125 | |||
126 | if (config.verbose >= 2) | ||
127 | printf(_("Command: %s\n"), swap_command); | ||
128 | if (config.verbose >= 3) | ||
129 | printf(_("Format: %s\n"), swap_format); | ||
130 | |||
131 | child_process = spopen(swap_command); | ||
132 | if (child_process == NULL) { | ||
133 | printf(_("Could not open pipe: %s\n"), swap_command); | ||
134 | swap_result tmp = { | ||
135 | .errorcode = STATE_UNKNOWN, | ||
136 | }; | ||
137 | return tmp; | ||
138 | } | ||
139 | |||
140 | child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); | ||
141 | if (child_stderr == NULL) { | ||
142 | printf(_("Could not open stderr for %s\n"), swap_command); | ||
143 | } | ||
144 | |||
145 | char str[32] = {0}; | ||
146 | char input_buffer[MAX_INPUT_BUFFER]; | ||
147 | |||
148 | /* read 1st line */ | ||
149 | fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); | ||
150 | if (strcmp(swap_format, "") == 0) { | ||
151 | temp_buffer = strtok(input_buffer, " \n"); | ||
152 | while (temp_buffer) { | ||
153 | if (strstr(temp_buffer, "blocks")) | ||
154 | sprintf(str, "%s %s", str, "%lu"); | ||
155 | else if (strstr(temp_buffer, "dskfree")) | ||
156 | sprintf(str, "%s %s", str, "%lu"); | ||
157 | else | ||
158 | sprintf(str, "%s %s", str, "%*s"); | ||
159 | temp_buffer = strtok(NULL, " \n"); | ||
160 | } | ||
161 | } | ||
162 | |||
163 | double total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; | ||
164 | double dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0; | ||
165 | |||
166 | /* | ||
167 | * If different swap command is used for summary switch, need to read format | ||
168 | * differently | ||
169 | */ | ||
170 | if (config.on_aix && !config.allswaps) { | ||
171 | fgets(input_buffer, MAX_INPUT_BUFFER - 1, | ||
172 | child_process); /* Ignore first line */ | ||
173 | sscanf(input_buffer, swap_format, &total_swap_mb, &used_swap_mb); | ||
174 | free_swap_mb = total_swap_mb * (100 - used_swap_mb) / 100; | ||
175 | used_swap_mb = total_swap_mb - free_swap_mb; | ||
176 | |||
177 | if (config.verbose >= 3) { | ||
178 | printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, | ||
179 | used_swap_mb, free_swap_mb); | ||
180 | } | ||
181 | } else { | ||
182 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
183 | sscanf(input_buffer, swap_format, &dsktotal_mb, &dskfree_mb); | ||
184 | |||
185 | dsktotal_mb = dsktotal_mb / config.conversion_factor; | ||
186 | /* AIX lists percent used, so this converts to dskfree in MBs */ | ||
187 | |||
188 | if (config.on_aix) { | ||
189 | dskfree_mb = dsktotal_mb * (100 - dskfree_mb) / 100; | ||
190 | } else { | ||
191 | dskfree_mb = dskfree_mb / config.conversion_factor; | ||
192 | } | ||
193 | |||
194 | if (config.verbose >= 3) | ||
195 | printf(_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb); | ||
196 | |||
197 | dskused_mb = dsktotal_mb - dskfree_mb; | ||
198 | total_swap_mb += dsktotal_mb; | ||
199 | used_swap_mb += dskused_mb; | ||
200 | free_swap_mb += dskfree_mb; | ||
201 | } | ||
202 | } | ||
203 | |||
204 | result.metrics.free = free_swap_mb * 1024 * 1024; | ||
205 | result.metrics.used = used_swap_mb * 1024 * 1024; | ||
206 | result.metrics.total = free_swap_mb * 1024 * 1024; | ||
207 | |||
208 | /* If we get anything on STDERR, at least set warning */ | ||
209 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | ||
210 | result.statusCode = max_state(result.statusCode, STATE_WARNING); | ||
211 | // TODO Set error here | ||
212 | } | ||
213 | |||
214 | /* close stderr */ | ||
215 | (void)fclose(child_stderr); | ||
216 | |||
217 | /* close the pipe */ | ||
218 | if (spclose(child_process)) { | ||
219 | result.statusCode = max_state(result.statusCode, STATE_WARNING); | ||
220 | // TODO set error here | ||
221 | } | ||
222 | |||
223 | return result; | ||
224 | } | ||
225 | |||
226 | #ifdef CHECK_SWAP_SWAPCTL_BSD | ||
227 | swap_result getSwapFromSwapctl_BSD(swap_config config) { | ||
228 | int i = 0, nswaps = 0, swapctl_res = 0; | ||
229 | struct swapent *ent; | ||
230 | |||
231 | /* get the number of active swap devices */ | ||
232 | nswaps = swapctl(SWAP_NSWAP, NULL, 0); | ||
233 | |||
234 | /* initialize swap table + entries */ | ||
235 | ent = (struct swapent *)malloc(sizeof(struct swapent) * nswaps); | ||
236 | |||
237 | /* and now, tally 'em up */ | ||
238 | swapctl_res = swapctl(SWAP_STATS, ent, nswaps); | ||
239 | if (swapctl_res < 0) { | ||
240 | perror(_("swapctl failed: ")); | ||
241 | die(STATE_UNKNOWN, _("Error in swapctl call\n")); | ||
242 | } | ||
243 | |||
244 | |||
245 | double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; | ||
246 | unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; | ||
247 | |||
248 | for (i = 0; i < nswaps; i++) { | ||
249 | dsktotal_mb = (float)ent[i].se_nblks / config.conversion_factor; | ||
250 | dskused_mb = (float)ent[i].se_inuse / config.conversion_factor; | ||
251 | dskfree_mb = (dsktotal_mb - dskused_mb); | ||
252 | |||
253 | if (config.allswaps && dsktotal_mb > 0) { | ||
254 | double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); | ||
255 | |||
256 | if (config.verbose) { | ||
257 | printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); | ||
258 | } | ||
259 | } | ||
260 | |||
261 | total_swap_mb += dsktotal_mb; | ||
262 | free_swap_mb += dskfree_mb; | ||
263 | used_swap_mb += dskused_mb; | ||
264 | } | ||
265 | |||
266 | /* and clean up after ourselves */ | ||
267 | free(ent); | ||
268 | |||
269 | swap_result result = {0}; | ||
270 | |||
271 | result.statusCode = OK; | ||
272 | result.errorcode = OK; | ||
273 | |||
274 | result.metrics.total = total_swap_mb * 1024 * 1024; | ||
275 | result.metrics.free = free_swap_mb * 1024 * 1024; | ||
276 | result.metrics.used = used_swap_mb * 1024 * 1024; | ||
277 | |||
278 | return result; | ||
279 | } | ||
280 | #endif // CHECK_SWAP_SWAPCTL_BSD | ||
281 | |||
282 | #ifdef CHECK_SWAP_SWAPCTL_SVR4 | ||
283 | swap_result getSwapFromSwap_SRV4(swap_config config) { | ||
284 | int i = 0, nswaps = 0, swapctl_res = 0; | ||
285 | //swaptbl_t *tbl = NULL; | ||
286 | void*tbl = NULL; | ||
287 | //swapent_t *ent = NULL; | ||
288 | void*ent = NULL; | ||
289 | /* get the number of active swap devices */ | ||
290 | if ((nswaps = swapctl(SC_GETNSWP, NULL)) == -1) | ||
291 | die(STATE_UNKNOWN, _("Error getting swap devices\n")); | ||
292 | |||
293 | if (nswaps == 0) | ||
294 | die(STATE_OK, _("SWAP OK: No swap devices defined\n")); | ||
295 | |||
296 | if (config.verbose >= 3) | ||
297 | printf("Found %d swap device(s)\n", nswaps); | ||
298 | |||
299 | /* initialize swap table + entries */ | ||
300 | tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); | ||
301 | |||
302 | if (tbl == NULL) | ||
303 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
304 | |||
305 | memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); | ||
306 | tbl->swt_n = nswaps; | ||
307 | for (i = 0; i < nswaps; i++) { | ||
308 | if ((tbl->swt_ent[i].ste_path = | ||
309 | (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL) | ||
310 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
311 | } | ||
312 | |||
313 | /* and now, tally 'em up */ | ||
314 | swapctl_res = swapctl(SC_LIST, tbl); | ||
315 | if (swapctl_res < 0) { | ||
316 | perror(_("swapctl failed: ")); | ||
317 | die(STATE_UNKNOWN, _("Error in swapctl call\n")); | ||
318 | } | ||
319 | |||
320 | double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; | ||
321 | unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; | ||
322 | |||
323 | for (i = 0; i < nswaps; i++) { | ||
324 | dsktotal_mb = (float)tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; | ||
325 | dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION; | ||
326 | dskused_mb = (dsktotal_mb - dskfree_mb); | ||
327 | |||
328 | if (config.verbose >= 3) | ||
329 | printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", | ||
330 | dsktotal_mb, dskfree_mb, dskused_mb); | ||
331 | |||
332 | if (config.allswaps && dsktotal_mb > 0) { | ||
333 | double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); | ||
334 | |||
335 | if (config.verbose) { | ||
336 | printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); | ||
337 | } | ||
338 | } | ||
339 | |||
340 | total_swap_mb += dsktotal_mb; | ||
341 | free_swap_mb += dskfree_mb; | ||
342 | used_swap_mb += dskused_mb; | ||
343 | } | ||
344 | |||
345 | /* and clean up after ourselves */ | ||
346 | for (i = 0; i < nswaps; i++) { | ||
347 | free(tbl->swt_ent[i].ste_path); | ||
348 | } | ||
349 | free(tbl); | ||
350 | |||
351 | swap_result result = {0}; | ||
352 | result.errorcode = OK; | ||
353 | result.metrics.total = total_swap_mb * 1024 * 1024; | ||
354 | result.metrics.free = free_swap_mb * 1024 * 1024; | ||
355 | result.metrics.used = used_swap_mb * 1024 * 1024; | ||
356 | |||
357 | return result; | ||
358 | } | ||
359 | #endif // CHECK_SWAP_SWAPCTL_SVR4 | ||