summaryrefslogtreecommitdiffstats
path: root/plugins/check_swap.d
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_swap.d')
-rw-r--r--plugins/check_swap.d/check_swap.h45
-rw-r--r--plugins/check_swap.d/swap.c415
2 files changed, 460 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..bad52917
--- /dev/null
+++ b/plugins/check_swap.d/check_swap.h
@@ -0,0 +1,45 @@
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
10typedef struct {
11 bool is_percentage;
12 uint64_t value;
13} threshold;
14
15typedef 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
21typedef struct {
22 int errorcode;
23 int statusCode;
24 swap_metrics metrics;
25} swap_result;
26
27typedef struct {
28 int verbose;
29 bool allswaps;
30 int no_swap_state;
31 threshold warn;
32 threshold crit;
33 bool on_aix;
34 int conversion_factor;
35} swap_config;
36
37swap_config swap_config_init();
38
39swap_result get_swap_data(swap_config config);
40swap_result getSwapFromProcMeminfo(swap_config config, char path_to_proc_meminfo[]);
41swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]);
42swap_result getSwapFromSwapctl_BSD(swap_config config);
43swap_result getSwapFromSwap_SRV4(swap_config config);
44
45#endif
diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c
new file mode 100644
index 00000000..50920fa3
--- /dev/null
+++ b/plugins/check_swap.d/swap.c
@@ -0,0 +1,415 @@
1#include "./check_swap.d/check_swap.h"
2#include "../popen.h"
3#include "../utils.h"
4
5swap_config swap_config_init() {
6 swap_config tmp = {0};
7 tmp.allswaps = false;
8 tmp.no_swap_state = STATE_CRITICAL;
9 tmp.verbose = 0;
10 tmp.conversion_factor = SWAP_CONVERSION;
11
12#ifdef _AIX
13 tmp.on_aix = true;
14#else
15 tmp.on_aix = false;
16#endif
17
18 return tmp;
19}
20
21swap_result get_swap_data(swap_config config) {
22#ifdef HAVE_PROC_MEMINFO
23 if (config.verbose >= 3) {
24 printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO);
25 }
26
27 return getSwapFromProcMeminfo(config, PROC_MEMINFO);
28#else
29#ifdef HAVE_SWAP
30 if (config.verbose >= 3) {
31 printf("Using swap command %s with format: %s\n", SWAP_COMMAND, SWAP_FORMAT);
32 }
33
34 /* These override the command used if a summary (and thus ! allswaps) is
35 * required
36 * The summary flag returns more accurate information about swap usage on these
37 * OSes */
38 if (config.on_aix && !config.allswaps) {
39
40 config.conversion_factor = 1;
41
42 return getSwapFromSwapCommand(config, "/usr/sbin/lsps -s", "%lu%*s %lu");
43 } else {
44 return getSwapFromSwapCommand(config, SWAP_COMMAND, SWAP_FORMAT);
45 }
46#else
47#ifdef CHECK_SWAP_SWAPCTL_SVR4
48 return getSwapFromSwapctl_SRV4();
49#else
50#ifdef CHECK_SWAP_SWAPCTL_BSD
51 return getSwapFromSwapctl_BSD();
52#else
53#error No way found to retrieve swap
54#endif /* CHECK_SWAP_SWAPCTL_BSD */
55#endif /* CHECK_SWAP_SWAPCTL_SVR4 */
56#endif /* HAVE_SWAP */
57#endif /* HAVE_PROC_MEMINFO */
58}
59
60swap_result getSwapFromProcMeminfo(swap_config config, char proc_meminfo[]) {
61 FILE *fp;
62 fp = fopen(proc_meminfo, "r");
63
64 swap_result result = {0};
65 result.statusCode = STATE_OK;
66
67 uint64_t swap_total = 0, swap_used = 0, swap_free = 0;
68
69 char input_buffer[MAX_INPUT_BUFFER];
70 char str[32];
71
72 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
73 uint64_t tmp_KB = 0;
74
75 /*
76 * The following sscanf call looks for a line looking like: "Swap: 123
77 * 123 123" On which kind of system this format exists, I can not say,
78 * but I wanted to document this for people who are not adapt with
79 * sscanf anymore, like me
80 * Also the units used here are unclear and probably wrong
81 */
82 if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu",
83 &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 (config.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
123swap_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 (config.verbose >= 2)
129 printf(_("Command: %s\n"), swap_command);
130 if (config.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,
174 child_process); /* Ignore first line */
175 sscanf(input_buffer, swap_format, &total_swap_mb, &used_swap_mb);
176 free_swap_mb = total_swap_mb * (100 - used_swap_mb) / 100;
177 used_swap_mb = total_swap_mb - free_swap_mb;
178
179 if (config.verbose >= 3) {
180 printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb,
181 used_swap_mb, free_swap_mb);
182 }
183 } else {
184 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
185 sscanf(input_buffer, swap_format, &dsktotal_mb, &dskfree_mb);
186
187 dsktotal_mb = dsktotal_mb / config.conversion_factor;
188 /* AIX lists percent used, so this converts to dskfree in MBs */
189
190 if (config.on_aix) {
191 dskfree_mb = dsktotal_mb * (100 - dskfree_mb) / 100;
192 } else {
193 dskfree_mb = dskfree_mb / config.conversion_factor;
194 }
195
196 if (config.verbose >= 3)
197 printf(_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb);
198
199 dskused_mb = dsktotal_mb - dskfree_mb;
200 total_swap_mb += dsktotal_mb;
201 used_swap_mb += dskused_mb;
202 free_swap_mb += dskfree_mb;
203 }
204 }
205
206 result.metrics.free = free_swap_mb * 1024 * 1024;
207 result.metrics.used = used_swap_mb * 1024 * 1024;
208 result.metrics.total = free_swap_mb * 1024 * 1024;
209
210 /* If we get anything on STDERR, at least set warning */
211 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
212 result.statusCode = max_state(result.statusCode, STATE_WARNING);
213 // TODO Set error here
214 }
215
216 /* close stderr */
217 (void)fclose(child_stderr);
218
219 /* close the pipe */
220 if (spclose(child_process)) {
221 result.statusCode = max_state(result.statusCode, STATE_WARNING);
222 // TODO set error here
223 }
224
225 return result;
226}
227
228#ifndef CHECK_SWAP_SWAPCTL_BSD
229#define CHECK_SWAP_SWAPCTL_BSD
230
231// Stub functionality for BSD stuff, so the compiler always sees the following BSD code
232
233#define SWAP_NSWAP 0
234#define SWAP_STATS 1
235
236int bsd_swapctl(int cmd, const void *arg, int misc) {
237 (void) cmd;
238 (void) arg;
239 (void) misc;
240 return 512;
241}
242
243struct swapent {
244 dev_t se_dev; /* device id */
245 int se_flags; /* entry flags */
246 int se_nblks; /* total blocks */
247 int se_inuse; /* blocks in use */
248 int se_priority; /* priority */
249 char se_path[PATH_MAX]; /* path to entry */
250};
251
252#else
253#define bsd_swapctl swapctl
254#endif
255
256swap_result getSwapFromSwapctl_BSD(swap_config config) {
257 /* get the number of active swap devices */
258 int nswaps = bsd_swapctl(SWAP_NSWAP, NULL, 0);
259
260 /* initialize swap table + entries */
261 struct swapent *ent = (struct swapent *)malloc(sizeof(struct swapent) * nswaps);
262
263 /* and now, tally 'em up */
264 int swapctl_res = bsd_swapctl(SWAP_STATS, ent, nswaps);
265 if (swapctl_res < 0) {
266 perror(_("swapctl failed: "));
267 die(STATE_UNKNOWN, _("Error in swapctl call\n"));
268 }
269
270
271 double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0;
272 unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0;
273
274 for (int i = 0; i < nswaps; i++) {
275 dsktotal_mb = (float)ent[i].se_nblks / config.conversion_factor;
276 dskused_mb = (float)ent[i].se_inuse / config.conversion_factor;
277 dskfree_mb = (dsktotal_mb - dskused_mb);
278
279 if (config.allswaps && dsktotal_mb > 0) {
280 double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb));
281
282 if (config.verbose) {
283 printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent);
284 }
285 }
286
287 total_swap_mb += dsktotal_mb;
288 free_swap_mb += dskfree_mb;
289 used_swap_mb += dskused_mb;
290 }
291
292 /* and clean up after ourselves */
293 free(ent);
294
295 swap_result result = {0};
296
297 result.statusCode = OK;
298 result.errorcode = OK;
299
300 result.metrics.total = total_swap_mb * 1024 * 1024;
301 result.metrics.free = free_swap_mb * 1024 * 1024;
302 result.metrics.used = used_swap_mb * 1024 * 1024;
303
304 return result;
305}
306
307
308
309#ifndef CHECK_SWAP_SWAPCTL_SVR4
310int srv4_swapctl(int cmd, void *arg) {
311 (void) cmd;
312 (void) arg;
313 return 512;
314}
315
316typedef struct srv4_swapent {
317 char *ste_path; /* name of the swap file */
318 off_t ste_start; /* starting block for swapping */
319 off_t ste_length; /* length of swap area */
320 long ste_pages; /* number of pages for swapping */
321 long ste_free; /* number of ste_pages free */
322 long ste_flags; /* ST_INDEL bit set if swap file */
323 /* is now being deleted */
324} swapent_t;
325
326typedef struct swaptbl {
327 int swt_n; /* number of swapents following */
328 struct srv4_swapent swt_ent[]; /* array of swt_n swapents */
329} swaptbl_t;
330
331#define SC_LIST 2
332#define SC_GETNSWP 3
333
334#ifndef MAXPATHLEN
335#define MAXPATHLEN 2048
336#endif
337
338#else
339#define srv4_swapctl swapctl
340#endif
341
342swap_result getSwapFromSwap_SRV4(swap_config config) {
343 int nswaps = 0;
344
345 /* get the number of active swap devices */
346 if ((nswaps = srv4_swapctl(SC_GETNSWP, NULL)) == -1)
347 die(STATE_UNKNOWN, _("Error getting swap devices\n"));
348
349 if (nswaps == 0)
350 die(STATE_OK, _("SWAP OK: No swap devices defined\n"));
351
352 if (config.verbose >= 3)
353 printf("Found %d swap device(s)\n", nswaps);
354
355 /* initialize swap table + entries */
356 swaptbl_t *tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps));
357
358 if (tbl == NULL)
359 die(STATE_UNKNOWN, _("malloc() failed!\n"));
360
361 memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps));
362 tbl->swt_n = nswaps;
363
364 for (int i = 0; i < nswaps; i++) {
365 if ((tbl->swt_ent[i].ste_path =
366 (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL)
367 die(STATE_UNKNOWN, _("malloc() failed!\n"));
368 }
369
370 /* and now, tally 'em up */
371 int swapctl_res = srv4_swapctl(SC_LIST, tbl);
372 if (swapctl_res < 0) {
373 perror(_("swapctl failed: "));
374 die(STATE_UNKNOWN, _("Error in swapctl call\n"));
375 }
376
377 double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0;
378 unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0;
379
380 for (int i = 0; i < nswaps; i++) {
381 dsktotal_mb = (float)tbl->swt_ent[i].ste_pages / SWAP_CONVERSION;
382 dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION;
383 dskused_mb = (dsktotal_mb - dskfree_mb);
384
385 if (config.verbose >= 3)
386 printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n",
387 dsktotal_mb, dskfree_mb, dskused_mb);
388
389 if (config.allswaps && dsktotal_mb > 0) {
390 double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb));
391
392 if (config.verbose) {
393 printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent);
394 }
395 }
396
397 total_swap_mb += dsktotal_mb;
398 free_swap_mb += dskfree_mb;
399 used_swap_mb += dskused_mb;
400 }
401
402 /* and clean up after ourselves */
403 for (int i = 0; i < nswaps; i++) {
404 free(tbl->swt_ent[i].ste_path);
405 }
406 free(tbl);
407
408 swap_result result = {0};
409 result.errorcode = OK;
410 result.metrics.total = total_swap_mb * 1024 * 1024;
411 result.metrics.free = free_swap_mb * 1024 * 1024;
412 result.metrics.used = used_swap_mb * 1024 * 1024;
413
414 return result;
415}