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 | 412 |
2 files changed, 456 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..d437ba59 --- /dev/null +++ b/plugins/check_swap.d/swap.c | |||
@@ -0,0 +1,412 @@ | |||
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 *fp; | ||
63 | fp = fopen(proc_meminfo, "r"); | ||
64 | |||
65 | swap_result result = {0}; | ||
66 | result.statusCode = STATE_OK; | ||
67 | |||
68 | uint64_t swap_total = 0, swap_used = 0, swap_free = 0; | ||
69 | |||
70 | char input_buffer[MAX_INPUT_BUFFER]; | ||
71 | char str[32]; | ||
72 | |||
73 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) { | ||
74 | uint64_t tmp_KB = 0; | ||
75 | |||
76 | /* | ||
77 | * The following sscanf call looks for a line looking like: "Swap: 123 | ||
78 | * 123 123" On which kind of system this format exists, I can not say, | ||
79 | * but I wanted to document this for people who are not adapt with | ||
80 | * sscanf anymore, like me | ||
81 | * Also the units used here are unclear and probably wrong | ||
82 | */ | ||
83 | if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &swap_total, &swap_used, &swap_free) == 3) { | ||
84 | |||
85 | result.metrics.total += swap_total; | ||
86 | result.metrics.used += swap_used; | ||
87 | result.metrics.free += swap_free; | ||
88 | |||
89 | /* | ||
90 | * The following sscanf call looks for lines looking like: | ||
91 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least | ||
92 | * on Debian Linux with a 5.* kernel | ||
93 | */ | ||
94 | } else if (sscanf(input_buffer, | ||
95 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " | ||
96 | "%*[k]%*[B]", | ||
97 | str, &tmp_KB)) { | ||
98 | |||
99 | if (verbose >= 3) { | ||
100 | printf("Got %s with %lu\n", str, tmp_KB); | ||
101 | } | ||
102 | |||
103 | /* I think this part is always in Kb, so convert to bytes */ | ||
104 | if (strcmp("Total", str) == 0) { | ||
105 | swap_total = tmp_KB * 1024; | ||
106 | } else if (strcmp("Free", str) == 0) { | ||
107 | swap_free = swap_free + tmp_KB * 1024; | ||
108 | } else if (strcmp("Cached", str) == 0) { | ||
109 | swap_free = swap_free + tmp_KB * 1024; | ||
110 | } | ||
111 | } | ||
112 | } | ||
113 | |||
114 | fclose(fp); | ||
115 | |||
116 | result.metrics.total = swap_total; | ||
117 | result.metrics.used = swap_total - swap_free; | ||
118 | result.metrics.free = swap_free; | ||
119 | |||
120 | return result; | ||
121 | } | ||
122 | |||
123 | swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]) { | ||
124 | swap_result result = {0}; | ||
125 | |||
126 | char *temp_buffer; | ||
127 | |||
128 | if (verbose >= 2) | ||
129 | printf(_("Command: %s\n"), swap_command); | ||
130 | if (verbose >= 3) | ||
131 | printf(_("Format: %s\n"), swap_format); | ||
132 | |||
133 | child_process = spopen(swap_command); | ||
134 | if (child_process == NULL) { | ||
135 | printf(_("Could not open pipe: %s\n"), swap_command); | ||
136 | swap_result tmp = { | ||
137 | .errorcode = STATE_UNKNOWN, | ||
138 | }; | ||
139 | return tmp; | ||
140 | } | ||
141 | |||
142 | child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); | ||
143 | if (child_stderr == NULL) { | ||
144 | printf(_("Could not open stderr for %s\n"), swap_command); | ||
145 | } | ||
146 | |||
147 | char str[32] = {0}; | ||
148 | char input_buffer[MAX_INPUT_BUFFER]; | ||
149 | |||
150 | /* read 1st line */ | ||
151 | fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); | ||
152 | if (strcmp(swap_format, "") == 0) { | ||
153 | temp_buffer = strtok(input_buffer, " \n"); | ||
154 | while (temp_buffer) { | ||
155 | if (strstr(temp_buffer, "blocks")) | ||
156 | sprintf(str, "%s %s", str, "%lu"); | ||
157 | else if (strstr(temp_buffer, "dskfree")) | ||
158 | sprintf(str, "%s %s", str, "%lu"); | ||
159 | else | ||
160 | sprintf(str, "%s %s", str, "%*s"); | ||
161 | temp_buffer = strtok(NULL, " \n"); | ||
162 | } | ||
163 | } | ||
164 | |||
165 | double total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; | ||
166 | double dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0; | ||
167 | |||
168 | /* | ||
169 | * If different swap command is used for summary switch, need to read format | ||
170 | * differently | ||
171 | */ | ||
172 | if (config.on_aix && !config.allswaps) { | ||
173 | fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */ | ||
174 | sscanf(input_buffer, swap_format, &total_swap_mb, &used_swap_mb); | ||
175 | free_swap_mb = total_swap_mb * (100 - used_swap_mb) / 100; | ||
176 | used_swap_mb = total_swap_mb - free_swap_mb; | ||
177 | |||
178 | if (verbose >= 3) { | ||
179 | printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, 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 (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 | #ifndef CHECK_SWAP_SWAPCTL_BSD | ||
227 | # define CHECK_SWAP_SWAPCTL_BSD | ||
228 | |||
229 | // Stub functionality for BSD stuff, so the compiler always sees the following BSD code | ||
230 | |||
231 | # define SWAP_NSWAP 0 | ||
232 | # define SWAP_STATS 1 | ||
233 | |||
234 | int bsd_swapctl(int cmd, const void *arg, int misc) { | ||
235 | (void)cmd; | ||
236 | (void)arg; | ||
237 | (void)misc; | ||
238 | return 512; | ||
239 | } | ||
240 | |||
241 | struct swapent { | ||
242 | dev_t se_dev; /* device id */ | ||
243 | int se_flags; /* entry flags */ | ||
244 | int se_nblks; /* total blocks */ | ||
245 | int se_inuse; /* blocks in use */ | ||
246 | int se_priority; /* priority */ | ||
247 | char se_path[PATH_MAX]; /* path to entry */ | ||
248 | }; | ||
249 | |||
250 | #else | ||
251 | # define bsd_swapctl swapctl | ||
252 | #endif | ||
253 | |||
254 | swap_result getSwapFromSwapctl_BSD(swap_config config) { | ||
255 | /* get the number of active swap devices */ | ||
256 | int nswaps = bsd_swapctl(SWAP_NSWAP, NULL, 0); | ||
257 | |||
258 | /* initialize swap table + entries */ | ||
259 | struct swapent *ent = (struct swapent *)malloc(sizeof(struct swapent) * nswaps); | ||
260 | |||
261 | /* and now, tally 'em up */ | ||
262 | int swapctl_res = bsd_swapctl(SWAP_STATS, ent, nswaps); | ||
263 | if (swapctl_res < 0) { | ||
264 | perror(_("swapctl failed: ")); | ||
265 | die(STATE_UNKNOWN, _("Error in swapctl call\n")); | ||
266 | } | ||
267 | |||
268 | double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; | ||
269 | unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0; | ||
270 | |||
271 | for (int i = 0; i < nswaps; i++) { | ||
272 | dsktotal_mb = (float)ent[i].se_nblks / config.conversion_factor; | ||
273 | dskused_mb = (float)ent[i].se_inuse / config.conversion_factor; | ||
274 | dskfree_mb = (dsktotal_mb - dskused_mb); | ||
275 | |||
276 | if (config.allswaps && dsktotal_mb > 0) { | ||
277 | double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); | ||
278 | |||
279 | if (verbose) { | ||
280 | printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); | ||
281 | } | ||
282 | } | ||
283 | |||
284 | total_swap_mb += dsktotal_mb; | ||
285 | free_swap_mb += dskfree_mb; | ||
286 | used_swap_mb += dskused_mb; | ||
287 | } | ||
288 | |||
289 | /* and clean up after ourselves */ | ||
290 | free(ent); | ||
291 | |||
292 | swap_result result = {0}; | ||
293 | |||
294 | result.statusCode = OK; | ||
295 | result.errorcode = OK; | ||
296 | |||
297 | result.metrics.total = total_swap_mb * 1024 * 1024; | ||
298 | result.metrics.free = free_swap_mb * 1024 * 1024; | ||
299 | result.metrics.used = used_swap_mb * 1024 * 1024; | ||
300 | |||
301 | return result; | ||
302 | } | ||
303 | |||
304 | #ifndef CHECK_SWAP_SWAPCTL_SVR4 | ||
305 | int srv4_swapctl(int cmd, void *arg) { | ||
306 | (void)cmd; | ||
307 | (void)arg; | ||
308 | return 512; | ||
309 | } | ||
310 | |||
311 | typedef struct srv4_swapent { | ||
312 | char *ste_path; /* name of the swap file */ | ||
313 | off_t ste_start; /* starting block for swapping */ | ||
314 | off_t ste_length; /* length of swap area */ | ||
315 | long ste_pages; /* number of pages for swapping */ | ||
316 | long ste_free; /* number of ste_pages free */ | ||
317 | long ste_flags; /* ST_INDEL bit set if swap file */ | ||
318 | /* is now being deleted */ | ||
319 | } swapent_t; | ||
320 | |||
321 | typedef struct swaptbl { | ||
322 | int swt_n; /* number of swapents following */ | ||
323 | struct srv4_swapent swt_ent[]; /* array of swt_n swapents */ | ||
324 | } swaptbl_t; | ||
325 | |||
326 | # define SC_LIST 2 | ||
327 | # define SC_GETNSWP 3 | ||
328 | |||
329 | # ifndef MAXPATHLEN | ||
330 | # define MAXPATHLEN 2048 | ||
331 | # endif | ||
332 | |||
333 | #else | ||
334 | # define srv4_swapctl swapctl | ||
335 | #endif | ||
336 | |||
337 | swap_result getSwapFromSwap_SRV4(swap_config config) { | ||
338 | int nswaps = 0; | ||
339 | |||
340 | /* get the number of active swap devices */ | ||
341 | if ((nswaps = srv4_swapctl(SC_GETNSWP, NULL)) == -1) | ||
342 | die(STATE_UNKNOWN, _("Error getting swap devices\n")); | ||
343 | |||
344 | if (nswaps == 0) | ||
345 | die(STATE_OK, _("SWAP OK: No swap devices defined\n")); | ||
346 | |||
347 | if (verbose >= 3) | ||
348 | printf("Found %d swap device(s)\n", nswaps); | ||
349 | |||
350 | /* initialize swap table + entries */ | ||
351 | swaptbl_t *tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); | ||
352 | |||
353 | if (tbl == NULL) | ||
354 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
355 | |||
356 | memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps)); | ||
357 | tbl->swt_n = nswaps; | ||
358 | |||
359 | for (int i = 0; i < nswaps; i++) { | ||
360 | if ((tbl->swt_ent[i].ste_path = (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL) | ||
361 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
362 | } | ||
363 | |||
364 | /* and now, tally 'em up */ | ||
365 | int swapctl_res = srv4_swapctl(SC_LIST, tbl); | ||
366 | if (swapctl_res < 0) { | ||
367 | perror(_("swapctl failed: ")); | ||
368 | die(STATE_UNKNOWN, _("Error in swapctl call\n")); | ||
369 | } | ||
370 | |||
371 | double dsktotal_mb = 0.0; | ||
372 | double dskfree_mb = 0.0; | ||
373 | double dskused_mb = 0.0; | ||
374 | unsigned long long total_swap_mb = 0; | ||
375 | unsigned long long free_swap_mb = 0; | ||
376 | unsigned long long used_swap_mb = 0; | ||
377 | |||
378 | for (int i = 0; i < nswaps; i++) { | ||
379 | dsktotal_mb = (float)tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; | ||
380 | dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION; | ||
381 | dskused_mb = (dsktotal_mb - dskfree_mb); | ||
382 | |||
383 | if (verbose >= 3) | ||
384 | printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb); | ||
385 | |||
386 | if (config.allswaps && dsktotal_mb > 0) { | ||
387 | double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); | ||
388 | |||
389 | if (verbose) { | ||
390 | printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); | ||
391 | } | ||
392 | } | ||
393 | |||
394 | total_swap_mb += dsktotal_mb; | ||
395 | free_swap_mb += dskfree_mb; | ||
396 | used_swap_mb += dskused_mb; | ||
397 | } | ||
398 | |||
399 | /* and clean up after ourselves */ | ||
400 | for (int i = 0; i < nswaps; i++) { | ||
401 | free(tbl->swt_ent[i].ste_path); | ||
402 | } | ||
403 | free(tbl); | ||
404 | |||
405 | swap_result result = {0}; | ||
406 | result.errorcode = OK; | ||
407 | result.metrics.total = total_swap_mb * 1024 * 1024; | ||
408 | result.metrics.free = free_swap_mb * 1024 * 1024; | ||
409 | result.metrics.used = used_swap_mb * 1024 * 1024; | ||
410 | |||
411 | return result; | ||
412 | } | ||