diff options
Diffstat (limited to 'plugins/check_swap.d')
-rw-r--r-- | plugins/check_swap.d/check_swap.h | 44 | ||||
-rw-r--r-- | plugins/check_swap.d/swap.c | 432 |
2 files changed, 476 insertions, 0 deletions
diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h new file mode 100644 index 00000000..9e8be75f --- /dev/null +++ b/plugins/check_swap.d/check_swap.h | |||
@@ -0,0 +1,44 @@ | |||
1 | #ifndef CHECK_SWAP_H | ||
2 | #define CHECK_SWAP_H | ||
3 | |||
4 | #include "../common.h" | ||
5 | |||
6 | #ifndef SWAP_CONVERSION | ||
7 | # define SWAP_CONVERSION 1 | ||
8 | #endif | ||
9 | |||
10 | typedef struct { | ||
11 | bool is_percentage; | ||
12 | uint64_t value; | ||
13 | } threshold; | ||
14 | |||
15 | typedef struct { | ||
16 | unsigned long long free; // Free swap in Bytes! | ||
17 | unsigned long long used; // Used swap in Bytes! | ||
18 | unsigned long long total; // Total swap size, you guessed it, in Bytes! | ||
19 | } swap_metrics; | ||
20 | |||
21 | typedef struct { | ||
22 | int errorcode; | ||
23 | int statusCode; | ||
24 | swap_metrics metrics; | ||
25 | } swap_result; | ||
26 | |||
27 | typedef struct { | ||
28 | bool allswaps; | ||
29 | int no_swap_state; | ||
30 | threshold warn; | ||
31 | threshold crit; | ||
32 | bool on_aix; | ||
33 | int conversion_factor; | ||
34 | } swap_config; | ||
35 | |||
36 | swap_config swap_config_init(void); | ||
37 | |||
38 | swap_result get_swap_data(swap_config config); | ||
39 | swap_result getSwapFromProcMeminfo(char path_to_proc_meminfo[]); | ||
40 | swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]); | ||
41 | swap_result getSwapFromSwapctl_BSD(swap_config config); | ||
42 | swap_result getSwapFromSwap_SRV4(swap_config config); | ||
43 | |||
44 | #endif | ||
diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c new file mode 100644 index 00000000..18db210c --- /dev/null +++ b/plugins/check_swap.d/swap.c | |||
@@ -0,0 +1,432 @@ | |||
1 | #include "./check_swap.d/check_swap.h" | ||
2 | #include "../popen.h" | ||
3 | #include "../utils.h" | ||
4 | |||
5 | extern int verbose; | ||
6 | |||
7 | swap_config swap_config_init(void) { | ||
8 | swap_config tmp = {0}; | ||
9 | tmp.allswaps = false; | ||
10 | tmp.no_swap_state = STATE_CRITICAL; | ||
11 | tmp.conversion_factor = SWAP_CONVERSION; | ||
12 | |||
13 | #ifdef _AIX | ||
14 | tmp.on_aix = true; | ||
15 | #else | ||
16 | tmp.on_aix = false; | ||
17 | #endif | ||
18 | |||
19 | return tmp; | ||
20 | } | ||
21 | |||
22 | swap_result get_swap_data(swap_config config) { | ||
23 | #ifdef HAVE_PROC_MEMINFO | ||
24 | if (verbose >= 3) { | ||
25 | printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO); | ||
26 | } | ||
27 | |||
28 | return getSwapFromProcMeminfo(PROC_MEMINFO); | ||
29 | #else | ||
30 | # ifdef HAVE_SWAP | ||
31 | if (verbose >= 3) { | ||
32 | printf("Using swap command %s with format: %s\n", SWAP_COMMAND, SWAP_FORMAT); | ||
33 | } | ||
34 | |||
35 | /* These override the command used if a summary (and thus ! allswaps) is | ||
36 | * required | ||
37 | * The summary flag returns more accurate information about swap usage on these | ||
38 | * OSes */ | ||
39 | if (config.on_aix && !config.allswaps) { | ||
40 | |||
41 | config.conversion_factor = 1; | ||
42 | |||
43 | return getSwapFromSwapCommand(config, "/usr/sbin/lsps -s", "%lu%*s %lu"); | ||
44 | } else { | ||
45 | return getSwapFromSwapCommand(config, SWAP_COMMAND, SWAP_FORMAT); | ||
46 | } | ||
47 | # else | ||
48 | # ifdef CHECK_SWAP_SWAPCTL_SVR4 | ||
49 | return getSwapFromSwapctl_SRV4(); | ||
50 | # else | ||
51 | # ifdef CHECK_SWAP_SWAPCTL_BSD | ||
52 | return getSwapFromSwapctl_BSD(); | ||
53 | # else | ||
54 | # error No way found to retrieve swap | ||
55 | # endif /* CHECK_SWAP_SWAPCTL_BSD */ | ||
56 | # endif /* CHECK_SWAP_SWAPCTL_SVR4 */ | ||
57 | # endif /* HAVE_SWAP */ | ||
58 | #endif /* HAVE_PROC_MEMINFO */ | ||
59 | } | ||
60 | |||
61 | swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | ||
62 | FILE *meminfo_file_ptr; | ||
63 | meminfo_file_ptr = fopen(proc_meminfo, "r"); | ||
64 | |||
65 | swap_result result = {0}; | ||
66 | result.statusCode = STATE_OK; | ||
67 | |||
68 | uint64_t swap_total = 0; | ||
69 | uint64_t swap_used = 0; | ||
70 | uint64_t swap_free = 0; | ||
71 | |||
72 | char input_buffer[MAX_INPUT_BUFFER]; | ||
73 | char str[32]; | ||
74 | |||
75 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, meminfo_file_ptr)) { | ||
76 | uint64_t tmp_KB = 0; | ||
77 | |||
78 | /* | ||
79 | * The following sscanf call looks for a line looking like: "Swap: 123 | ||
80 | * 123 123" On which kind of system this format exists, I can not say, | ||
81 | * but I wanted to document this for people who are not adapt with | ||
82 | * sscanf anymore, like me | ||
83 | * Also the units used here are unclear and probably wrong | ||
84 | */ | ||
85 | if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &swap_total, &swap_used, &swap_free) == 3) { | ||
86 | |||
87 | result.metrics.total += swap_total; | ||
88 | result.metrics.used += swap_used; | ||
89 | result.metrics.free += swap_free; | ||
90 | |||
91 | /* | ||
92 | * The following sscanf call looks for lines looking like: | ||
93 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least | ||
94 | * on Debian Linux with a 5.* kernel | ||
95 | */ | ||
96 | } else if (sscanf(input_buffer, | ||
97 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " | ||
98 | "%*[k]%*[B]", | ||
99 | str, &tmp_KB)) { | ||
100 | |||
101 | if (verbose >= 3) { | ||
102 | printf("Got %s with %lu\n", str, tmp_KB); | ||
103 | } | ||
104 | |||
105 | /* I think this part is always in Kb, so convert to bytes */ | ||
106 | if (strcmp("Total", str) == 0) { | ||
107 | swap_total = tmp_KB * 1024; | ||
108 | } else if (strcmp("Free", str) == 0) { | ||
109 | swap_free = swap_free + tmp_KB * 1024; | ||
110 | } else if (strcmp("Cached", str) == 0) { | ||
111 | swap_free = swap_free + tmp_KB * 1024; | ||
112 | } | ||
113 | } | ||
114 | } | ||
115 | |||
116 | fclose(meminfo_file_ptr); | ||
117 | |||
118 | result.metrics.total = swap_total; | ||
119 | result.metrics.used = swap_total - swap_free; | ||
120 | result.metrics.free = swap_free; | ||
121 | |||
122 | return result; | ||
123 | } | ||
124 | |||
125 | swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]) { | ||
126 | swap_result result = {0}; | ||
127 | |||
128 | char *temp_buffer; | ||
129 | |||
130 | if (verbose >= 2) { | ||
131 | printf(_("Command: %s\n"), swap_command); | ||
132 | } | ||
133 | if (verbose >= 3) { | ||
134 | printf(_("Format: %s\n"), swap_format); | ||
135 | } | ||
136 | |||
137 | child_process = spopen(swap_command); | ||
138 | if (child_process == NULL) { | ||
139 | printf(_("Could not open pipe: %s\n"), swap_command); | ||
140 | swap_result tmp = { | ||
141 | .errorcode = STATE_UNKNOWN, | ||
142 | }; | ||
143 | return tmp; | ||
144 | } | ||
145 | |||
146 | child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); | ||
147 | if (child_stderr == NULL) { | ||
148 | printf(_("Could not open stderr for %s\n"), swap_command); | ||
149 | } | ||
150 | |||
151 | char str[32] = {0}; | ||
152 | char input_buffer[MAX_INPUT_BUFFER]; | ||
153 | |||
154 | /* read 1st line */ | ||
155 | fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); | ||
156 | if (strcmp(swap_format, "") == 0) { | ||
157 | temp_buffer = strtok(input_buffer, " \n"); | ||
158 | while (temp_buffer) { | ||
159 | if (strstr(temp_buffer, "blocks")) { | ||
160 | sprintf(str, "%s %s", str, "%lu"); | ||
161 | } else if (strstr(temp_buffer, "dskfree")) { | ||
162 | sprintf(str, "%s %s", str, "%lu"); | ||
163 | } else { | ||
164 | sprintf(str, "%s %s", str, "%*s"); | ||
165 | } | ||
166 | temp_buffer = strtok(NULL, " \n"); | ||
167 | } | ||
168 | } | ||
169 | |||
170 | double total_swap_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; | ||
176 | |||
177 | /* | ||
178 | * If different swap command is used for summary switch, need to read format | ||
179 | * differently | ||
180 | */ | ||
181 | if (config.on_aix && !config.allswaps) { | ||
182 | fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */ | ||
183 | sscanf(input_buffer, swap_format, &total_swap_mb, &used_swap_mb); | ||
184 | free_swap_mb = total_swap_mb * (100 - used_swap_mb) / 100; | ||
185 | used_swap_mb = total_swap_mb - free_swap_mb; | ||
186 | |||
187 | if (verbose >= 3) { | ||
188 | printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb, free_swap_mb); | ||
189 | } | ||
190 | } else { | ||
191 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
192 | sscanf(input_buffer, swap_format, &dsktotal_mb, &dskfree_mb); | ||
193 | |||
194 | dsktotal_mb = dsktotal_mb / config.conversion_factor; | ||
195 | /* AIX lists percent used, so this converts to dskfree in MBs */ | ||
196 | |||
197 | if (config.on_aix) { | ||
198 | dskfree_mb = dsktotal_mb * (100 - dskfree_mb) / 100; | ||
199 | } else { | ||
200 | dskfree_mb = dskfree_mb / config.conversion_factor; | ||
201 | } | ||
202 | |||
203 | if (verbose >= 3) { | ||
204 | printf(_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb); | ||
205 | } | ||
206 | |||
207 | dskused_mb = dsktotal_mb - dskfree_mb; | ||
208 | total_swap_mb += dsktotal_mb; | ||
209 | used_swap_mb += dskused_mb; | ||
210 | free_swap_mb += dskfree_mb; | ||
211 | } | ||
212 | } | ||
213 | |||
214 | result.metrics.free = free_swap_mb * 1024 * 1024; | ||
215 | result.metrics.used = used_swap_mb * 1024 * 1024; | ||
216 | result.metrics.total = free_swap_mb * 1024 * 1024; | ||
217 | |||
218 | /* If we get anything on STDERR, at least set warning */ | ||
219 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | ||
220 | result.statusCode = max_state(result.statusCode, STATE_WARNING); | ||
221 | // TODO Set error here | ||
222 | } | ||
223 | |||
224 | /* close stderr */ | ||
225 | (void)fclose(child_stderr); | ||
226 | |||
227 | /* close the pipe */ | ||
228 | if (spclose(child_process)) { | ||
229 | result.statusCode = max_state(result.statusCode, STATE_WARNING); | ||
230 | // TODO set error here | ||
231 | } | ||
232 | |||
233 | return result; | ||
234 | } | ||
235 | |||
236 | #ifndef CHECK_SWAP_SWAPCTL_BSD | ||
237 | # define CHECK_SWAP_SWAPCTL_BSD | ||
238 | |||
239 | // Stub functionality for BSD stuff, so the compiler always sees the following BSD code | ||
240 | |||
241 | # define SWAP_NSWAP 0 | ||
242 | # define SWAP_STATS 1 | ||
243 | |||
244 | int bsd_swapctl(int cmd, const void *arg, int misc) { | ||
245 | (void)cmd; | ||
246 | (void)arg; | ||
247 | (void)misc; | ||
248 | return 512; | ||
249 | } | ||
250 | |||
251 | struct swapent { | ||
252 | dev_t se_dev; /* device id */ | ||
253 | int se_flags; /* entry flags */ | ||
254 | int se_nblks; /* total blocks */ | ||
255 | int se_inuse; /* blocks in use */ | ||
256 | int se_priority; /* priority */ | ||
257 | char se_path[PATH_MAX]; /* path to entry */ | ||
258 | }; | ||
259 | |||
260 | #else | ||
261 | # define bsd_swapctl swapctl | ||
262 | #endif | ||
263 | |||
264 | swap_result getSwapFromSwapctl_BSD(swap_config config) { | ||
265 | /* get the number of active swap devices */ | ||
266 | int nswaps = bsd_swapctl(SWAP_NSWAP, NULL, 0); | ||
267 | |||
268 | /* initialize swap table + entries */ | ||
269 | struct swapent *ent = (struct swapent *)malloc(sizeof(struct swapent) * (unsigned long)nswaps); | ||
270 | |||
271 | /* and now, tally 'em up */ | ||
272 | int swapctl_res = bsd_swapctl(SWAP_STATS, ent, nswaps); | ||
273 | if (swapctl_res < 0) { | ||
274 | perror(_("swapctl failed: ")); | ||
275 | die(STATE_UNKNOWN, _("Error in swapctl call\n")); | ||
276 | } | ||
277 | |||
278 | double dsktotal_mb = 0.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; | ||
284 | |||
285 | for (int i = 0; i < nswaps; i++) { | ||
286 | dsktotal_mb = (float)ent[i].se_nblks / config.conversion_factor; | ||
287 | dskused_mb = (float)ent[i].se_inuse / config.conversion_factor; | ||
288 | dskfree_mb = (dsktotal_mb - dskused_mb); | ||
289 | |||
290 | if (config.allswaps && dsktotal_mb > 0) { | ||
291 | double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); | ||
292 | |||
293 | if (verbose) { | ||
294 | printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); | ||
295 | } | ||
296 | } | ||
297 | |||
298 | total_swap_mb += dsktotal_mb; | ||
299 | free_swap_mb += dskfree_mb; | ||
300 | used_swap_mb += dskused_mb; | ||
301 | } | ||
302 | |||
303 | /* and clean up after ourselves */ | ||
304 | free(ent); | ||
305 | |||
306 | swap_result result = {0}; | ||
307 | |||
308 | result.statusCode = OK; | ||
309 | result.errorcode = OK; | ||
310 | |||
311 | result.metrics.total = total_swap_mb * 1024 * 1024; | ||
312 | result.metrics.free = free_swap_mb * 1024 * 1024; | ||
313 | result.metrics.used = used_swap_mb * 1024 * 1024; | ||
314 | |||
315 | return result; | ||
316 | } | ||
317 | |||
318 | #ifndef CHECK_SWAP_SWAPCTL_SVR4 | ||
319 | int srv4_swapctl(int cmd, void *arg) { | ||
320 | (void)cmd; | ||
321 | (void)arg; | ||
322 | return 512; | ||
323 | } | ||
324 | |||
325 | typedef struct srv4_swapent { | ||
326 | char *ste_path; /* name of the swap file */ | ||
327 | off_t ste_start; /* starting block for swapping */ | ||
328 | off_t ste_length; /* length of swap area */ | ||
329 | long ste_pages; /* number of pages for swapping */ | ||
330 | long ste_free; /* number of ste_pages free */ | ||
331 | long ste_flags; /* ST_INDEL bit set if swap file */ | ||
332 | /* is now being deleted */ | ||
333 | } swapent_t; | ||
334 | |||
335 | typedef struct swaptbl { | ||
336 | int swt_n; /* number of swapents following */ | ||
337 | struct srv4_swapent swt_ent[]; /* array of swt_n swapents */ | ||
338 | } swaptbl_t; | ||
339 | |||
340 | # define SC_LIST 2 | ||
341 | # define SC_GETNSWP 3 | ||
342 | |||
343 | # ifndef MAXPATHLEN | ||
344 | # define MAXPATHLEN 2048 | ||
345 | # endif | ||
346 | |||
347 | #else | ||
348 | # define srv4_swapctl swapctl | ||
349 | #endif | ||
350 | |||
351 | swap_result getSwapFromSwap_SRV4(swap_config config) { | ||
352 | int nswaps = 0; | ||
353 | |||
354 | /* get the number of active swap devices */ | ||
355 | if ((nswaps = srv4_swapctl(SC_GETNSWP, NULL)) == -1) { | ||
356 | die(STATE_UNKNOWN, _("Error getting swap devices\n")); | ||
357 | } | ||
358 | |||
359 | if (nswaps == 0) { | ||
360 | die(STATE_OK, _("SWAP OK: No swap devices defined\n")); | ||
361 | } | ||
362 | |||
363 | if (verbose >= 3) { | ||
364 | printf("Found %d swap device(s)\n", nswaps); | ||
365 | } | ||
366 | |||
367 | /* initialize swap table + entries */ | ||
368 | swaptbl_t *tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); | ||
369 | |||
370 | if (tbl == NULL) { | ||
371 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
372 | } | ||
373 | |||
374 | memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); | ||
375 | tbl->swt_n = nswaps; | ||
376 | |||
377 | for (int i = 0; i < nswaps; i++) { | ||
378 | if ((tbl->swt_ent[i].ste_path = (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL) { | ||
379 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
380 | } | ||
381 | } | ||
382 | |||
383 | /* and now, tally 'em up */ | ||
384 | int swapctl_res = srv4_swapctl(SC_LIST, tbl); | ||
385 | if (swapctl_res < 0) { | ||
386 | perror(_("swapctl failed: ")); | ||
387 | die(STATE_UNKNOWN, _("Error in swapctl call\n")); | ||
388 | } | ||
389 | |||
390 | double dsktotal_mb = 0.0; | ||
391 | double dskfree_mb = 0.0; | ||
392 | double dskused_mb = 0.0; | ||
393 | unsigned long long total_swap_mb = 0; | ||
394 | unsigned long long free_swap_mb = 0; | ||
395 | unsigned long long used_swap_mb = 0; | ||
396 | |||
397 | for (int i = 0; i < nswaps; i++) { | ||
398 | dsktotal_mb = (float)tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; | ||
399 | dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION; | ||
400 | dskused_mb = (dsktotal_mb - dskfree_mb); | ||
401 | |||
402 | if (verbose >= 3) { | ||
403 | printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb); | ||
404 | } | ||
405 | |||
406 | if (config.allswaps && dsktotal_mb > 0) { | ||
407 | double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); | ||
408 | |||
409 | if (verbose) { | ||
410 | printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); | ||
411 | } | ||
412 | } | ||
413 | |||
414 | total_swap_mb += dsktotal_mb; | ||
415 | free_swap_mb += dskfree_mb; | ||
416 | used_swap_mb += dskused_mb; | ||
417 | } | ||
418 | |||
419 | /* and clean up after ourselves */ | ||
420 | for (int i = 0; i < nswaps; i++) { | ||
421 | free(tbl->swt_ent[i].ste_path); | ||
422 | } | ||
423 | free(tbl); | ||
424 | |||
425 | swap_result result = {0}; | ||
426 | result.errorcode = OK; | ||
427 | result.metrics.total = total_swap_mb * 1024 * 1024; | ||
428 | result.metrics.free = free_swap_mb * 1024 * 1024; | ||
429 | result.metrics.used = used_swap_mb * 1024 * 1024; | ||
430 | |||
431 | return result; | ||
432 | } | ||