summaryrefslogtreecommitdiffstats
path: root/plugins/check_swap.d
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2024-11-02 13:37:39 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2024-11-02 13:37:39 +0100
commit3faeed07c4825d5c3ceb323e814e703d9262cd82 (patch)
treeb6a61e24ffa0fbb4af5f145c137f1f584aaba1bc /plugins/check_swap.d
parent735b04eff721a28e791714c0da4c8ac5726bfbcf (diff)
parent6d1d1dac32841d5ca6ee51bb09b30a6c604b17e2 (diff)
downloadmonitoring-plugins-3faeed07c4825d5c3ceb323e814e703d9262cd82.tar.gz
Merge branch 'master' into check_swap_again
Diffstat (limited to 'plugins/check_swap.d')
-rw-r--r--plugins/check_swap.d/check_swap.h7
-rw-r--r--plugins/check_swap.d/swap.c151
2 files changed, 77 insertions, 81 deletions
diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h
index bad52917..9e8be75f 100644
--- a/plugins/check_swap.d/check_swap.h
+++ b/plugins/check_swap.d/check_swap.h
@@ -4,7 +4,7 @@
4#include "../common.h" 4#include "../common.h"
5 5
6#ifndef SWAP_CONVERSION 6#ifndef SWAP_CONVERSION
7#define SWAP_CONVERSION 1 7# define SWAP_CONVERSION 1
8#endif 8#endif
9 9
10typedef struct { 10typedef struct {
@@ -25,7 +25,6 @@ typedef struct {
25} swap_result; 25} swap_result;
26 26
27typedef struct { 27typedef struct {
28 int verbose;
29 bool allswaps; 28 bool allswaps;
30 int no_swap_state; 29 int no_swap_state;
31 threshold warn; 30 threshold warn;
@@ -34,10 +33,10 @@ typedef struct {
34 int conversion_factor; 33 int conversion_factor;
35} swap_config; 34} swap_config;
36 35
37swap_config swap_config_init(); 36swap_config swap_config_init(void);
38 37
39swap_result get_swap_data(swap_config config); 38swap_result get_swap_data(swap_config config);
40swap_result getSwapFromProcMeminfo(swap_config config, char path_to_proc_meminfo[]); 39swap_result getSwapFromProcMeminfo(char path_to_proc_meminfo[]);
41swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]); 40swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]);
42swap_result getSwapFromSwapctl_BSD(swap_config config); 41swap_result getSwapFromSwapctl_BSD(swap_config config);
43swap_result getSwapFromSwap_SRV4(swap_config config); 42swap_result getSwapFromSwap_SRV4(swap_config config);
diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c
index 50920fa3..d437ba59 100644
--- a/plugins/check_swap.d/swap.c
+++ b/plugins/check_swap.d/swap.c
@@ -2,11 +2,12 @@
2#include "../popen.h" 2#include "../popen.h"
3#include "../utils.h" 3#include "../utils.h"
4 4
5swap_config swap_config_init() { 5extern int verbose;
6
7swap_config swap_config_init(void) {
6 swap_config tmp = {0}; 8 swap_config tmp = {0};
7 tmp.allswaps = false; 9 tmp.allswaps = false;
8 tmp.no_swap_state = STATE_CRITICAL; 10 tmp.no_swap_state = STATE_CRITICAL;
9 tmp.verbose = 0;
10 tmp.conversion_factor = SWAP_CONVERSION; 11 tmp.conversion_factor = SWAP_CONVERSION;
11 12
12#ifdef _AIX 13#ifdef _AIX
@@ -20,14 +21,14 @@ swap_config swap_config_init() {
20 21
21swap_result get_swap_data(swap_config config) { 22swap_result get_swap_data(swap_config config) {
22#ifdef HAVE_PROC_MEMINFO 23#ifdef HAVE_PROC_MEMINFO
23 if (config.verbose >= 3) { 24 if (verbose >= 3) {
24 printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO); 25 printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO);
25 } 26 }
26 27
27 return getSwapFromProcMeminfo(config, PROC_MEMINFO); 28 return getSwapFromProcMeminfo(PROC_MEMINFO);
28#else 29#else
29#ifdef HAVE_SWAP 30# ifdef HAVE_SWAP
30 if (config.verbose >= 3) { 31 if (verbose >= 3) {
31 printf("Using swap command %s with format: %s\n", SWAP_COMMAND, SWAP_FORMAT); 32 printf("Using swap command %s with format: %s\n", SWAP_COMMAND, SWAP_FORMAT);
32 } 33 }
33 34
@@ -43,21 +44,21 @@ swap_result get_swap_data(swap_config config) {
43 } else { 44 } else {
44 return getSwapFromSwapCommand(config, SWAP_COMMAND, SWAP_FORMAT); 45 return getSwapFromSwapCommand(config, SWAP_COMMAND, SWAP_FORMAT);
45 } 46 }
46#else 47# else
47#ifdef CHECK_SWAP_SWAPCTL_SVR4 48# ifdef CHECK_SWAP_SWAPCTL_SVR4
48 return getSwapFromSwapctl_SRV4(); 49 return getSwapFromSwapctl_SRV4();
49#else 50# else
50#ifdef CHECK_SWAP_SWAPCTL_BSD 51# ifdef CHECK_SWAP_SWAPCTL_BSD
51 return getSwapFromSwapctl_BSD(); 52 return getSwapFromSwapctl_BSD();
52#else 53# else
53#error No way found to retrieve swap 54# error No way found to retrieve swap
54#endif /* CHECK_SWAP_SWAPCTL_BSD */ 55# endif /* CHECK_SWAP_SWAPCTL_BSD */
55#endif /* CHECK_SWAP_SWAPCTL_SVR4 */ 56# endif /* CHECK_SWAP_SWAPCTL_SVR4 */
56#endif /* HAVE_SWAP */ 57# endif /* HAVE_SWAP */
57#endif /* HAVE_PROC_MEMINFO */ 58#endif /* HAVE_PROC_MEMINFO */
58} 59}
59 60
60swap_result getSwapFromProcMeminfo(swap_config config, char proc_meminfo[]) { 61swap_result getSwapFromProcMeminfo(char proc_meminfo[]) {
61 FILE *fp; 62 FILE *fp;
62 fp = fopen(proc_meminfo, "r"); 63 fp = fopen(proc_meminfo, "r");
63 64
@@ -79,8 +80,7 @@ swap_result getSwapFromProcMeminfo(swap_config config, char proc_meminfo[]) {
79 * sscanf anymore, like me 80 * sscanf anymore, like me
80 * Also the units used here are unclear and probably wrong 81 * Also the units used here are unclear and probably wrong
81 */ 82 */
82 if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", 83 if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &swap_total, &swap_used, &swap_free) == 3) {
83 &swap_total, &swap_used, &swap_free) == 3) {
84 84
85 result.metrics.total += swap_total; 85 result.metrics.total += swap_total;
86 result.metrics.used += swap_used; 86 result.metrics.used += swap_used;
@@ -96,7 +96,7 @@ swap_result getSwapFromProcMeminfo(swap_config config, char proc_meminfo[]) {
96 "%*[k]%*[B]", 96 "%*[k]%*[B]",
97 str, &tmp_KB)) { 97 str, &tmp_KB)) {
98 98
99 if (config.verbose >= 3) { 99 if (verbose >= 3) {
100 printf("Got %s with %lu\n", str, tmp_KB); 100 printf("Got %s with %lu\n", str, tmp_KB);
101 } 101 }
102 102
@@ -125,9 +125,9 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[]
125 125
126 char *temp_buffer; 126 char *temp_buffer;
127 127
128 if (config.verbose >= 2) 128 if (verbose >= 2)
129 printf(_("Command: %s\n"), swap_command); 129 printf(_("Command: %s\n"), swap_command);
130 if (config.verbose >= 3) 130 if (verbose >= 3)
131 printf(_("Format: %s\n"), swap_format); 131 printf(_("Format: %s\n"), swap_format);
132 132
133 child_process = spopen(swap_command); 133 child_process = spopen(swap_command);
@@ -169,16 +169,14 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[]
169 * If different swap command is used for summary switch, need to read format 169 * If different swap command is used for summary switch, need to read format
170 * differently 170 * differently
171 */ 171 */
172 if (config.on_aix && !config.allswaps) { 172 if (config.on_aix && !config.allswaps) {
173 fgets(input_buffer, MAX_INPUT_BUFFER - 1, 173 fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */
174 child_process); /* Ignore first line */
175 sscanf(input_buffer, swap_format, &total_swap_mb, &used_swap_mb); 174 sscanf(input_buffer, swap_format, &total_swap_mb, &used_swap_mb);
176 free_swap_mb = total_swap_mb * (100 - used_swap_mb) / 100; 175 free_swap_mb = total_swap_mb * (100 - used_swap_mb) / 100;
177 used_swap_mb = total_swap_mb - free_swap_mb; 176 used_swap_mb = total_swap_mb - free_swap_mb;
178 177
179 if (config.verbose >= 3) { 178 if (verbose >= 3) {
180 printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, 179 printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb, free_swap_mb);
181 used_swap_mb, free_swap_mb);
182 } 180 }
183 } else { 181 } else {
184 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 182 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
@@ -193,7 +191,7 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[]
193 dskfree_mb = dskfree_mb / config.conversion_factor; 191 dskfree_mb = dskfree_mb / config.conversion_factor;
194 } 192 }
195 193
196 if (config.verbose >= 3) 194 if (verbose >= 3)
197 printf(_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb); 195 printf(_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb);
198 196
199 dskused_mb = dsktotal_mb - dskfree_mb; 197 dskused_mb = dsktotal_mb - dskfree_mb;
@@ -204,8 +202,8 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[]
204 } 202 }
205 203
206 result.metrics.free = free_swap_mb * 1024 * 1024; 204 result.metrics.free = free_swap_mb * 1024 * 1024;
207 result.metrics.used = used_swap_mb * 1024 * 1024; 205 result.metrics.used = used_swap_mb * 1024 * 1024;
208 result.metrics.total = free_swap_mb * 1024 * 1024; 206 result.metrics.total = free_swap_mb * 1024 * 1024;
209 207
210 /* If we get anything on STDERR, at least set warning */ 208 /* If we get anything on STDERR, at least set warning */
211 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { 209 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
@@ -226,31 +224,31 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[]
226} 224}
227 225
228#ifndef CHECK_SWAP_SWAPCTL_BSD 226#ifndef CHECK_SWAP_SWAPCTL_BSD
229#define CHECK_SWAP_SWAPCTL_BSD 227# define CHECK_SWAP_SWAPCTL_BSD
230 228
231// Stub functionality for BSD stuff, so the compiler always sees the following BSD code 229// Stub functionality for BSD stuff, so the compiler always sees the following BSD code
232 230
233#define SWAP_NSWAP 0 231# define SWAP_NSWAP 0
234#define SWAP_STATS 1 232# define SWAP_STATS 1
235 233
236int bsd_swapctl(int cmd, const void *arg, int misc) { 234int bsd_swapctl(int cmd, const void *arg, int misc) {
237 (void) cmd; 235 (void)cmd;
238 (void) arg; 236 (void)arg;
239 (void) misc; 237 (void)misc;
240 return 512; 238 return 512;
241} 239}
242 240
243struct swapent { 241struct swapent {
244 dev_t se_dev; /* device id */ 242 dev_t se_dev; /* device id */
245 int se_flags; /* entry flags */ 243 int se_flags; /* entry flags */
246 int se_nblks; /* total blocks */ 244 int se_nblks; /* total blocks */
247 int se_inuse; /* blocks in use */ 245 int se_inuse; /* blocks in use */
248 int se_priority; /* priority */ 246 int se_priority; /* priority */
249 char se_path[PATH_MAX]; /* path to entry */ 247 char se_path[PATH_MAX]; /* path to entry */
250}; 248};
251 249
252#else 250#else
253#define bsd_swapctl swapctl 251# define bsd_swapctl swapctl
254#endif 252#endif
255 253
256swap_result getSwapFromSwapctl_BSD(swap_config config) { 254swap_result getSwapFromSwapctl_BSD(swap_config config) {
@@ -267,7 +265,6 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) {
267 die(STATE_UNKNOWN, _("Error in swapctl call\n")); 265 die(STATE_UNKNOWN, _("Error in swapctl call\n"));
268 } 266 }
269 267
270
271 double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; 268 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; 269 unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0;
273 270
@@ -279,7 +276,7 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) {
279 if (config.allswaps && dsktotal_mb > 0) { 276 if (config.allswaps && dsktotal_mb > 0) {
280 double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); 277 double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb));
281 278
282 if (config.verbose) { 279 if (verbose) {
283 printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); 280 printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent);
284 } 281 }
285 } 282 }
@@ -297,46 +294,44 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) {
297 result.statusCode = OK; 294 result.statusCode = OK;
298 result.errorcode = OK; 295 result.errorcode = OK;
299 296
300 result.metrics.total = total_swap_mb * 1024 * 1024; 297 result.metrics.total = total_swap_mb * 1024 * 1024;
301 result.metrics.free = free_swap_mb * 1024 * 1024; 298 result.metrics.free = free_swap_mb * 1024 * 1024;
302 result.metrics.used = used_swap_mb * 1024 * 1024; 299 result.metrics.used = used_swap_mb * 1024 * 1024;
303 300
304 return result; 301 return result;
305} 302}
306 303
307
308
309#ifndef CHECK_SWAP_SWAPCTL_SVR4 304#ifndef CHECK_SWAP_SWAPCTL_SVR4
310int srv4_swapctl(int cmd, void *arg) { 305int srv4_swapctl(int cmd, void *arg) {
311 (void) cmd; 306 (void)cmd;
312 (void) arg; 307 (void)arg;
313 return 512; 308 return 512;
314} 309}
315 310
316typedef struct srv4_swapent { 311typedef struct srv4_swapent {
317 char *ste_path; /* name of the swap file */ 312 char *ste_path; /* name of the swap file */
318 off_t ste_start; /* starting block for swapping */ 313 off_t ste_start; /* starting block for swapping */
319 off_t ste_length; /* length of swap area */ 314 off_t ste_length; /* length of swap area */
320 long ste_pages; /* number of pages for swapping */ 315 long ste_pages; /* number of pages for swapping */
321 long ste_free; /* number of ste_pages free */ 316 long ste_free; /* number of ste_pages free */
322 long ste_flags; /* ST_INDEL bit set if swap file */ 317 long ste_flags; /* ST_INDEL bit set if swap file */
323 /* is now being deleted */ 318 /* is now being deleted */
324} swapent_t; 319} swapent_t;
325 320
326typedef struct swaptbl { 321typedef struct swaptbl {
327 int swt_n; /* number of swapents following */ 322 int swt_n; /* number of swapents following */
328 struct srv4_swapent swt_ent[]; /* array of swt_n swapents */ 323 struct srv4_swapent swt_ent[]; /* array of swt_n swapents */
329} swaptbl_t; 324} swaptbl_t;
330 325
331#define SC_LIST 2 326# define SC_LIST 2
332#define SC_GETNSWP 3 327# define SC_GETNSWP 3
333 328
334#ifndef MAXPATHLEN 329# ifndef MAXPATHLEN
335#define MAXPATHLEN 2048 330# define MAXPATHLEN 2048
336#endif 331# endif
337 332
338#else 333#else
339#define srv4_swapctl swapctl 334# define srv4_swapctl swapctl
340#endif 335#endif
341 336
342swap_result getSwapFromSwap_SRV4(swap_config config) { 337swap_result getSwapFromSwap_SRV4(swap_config config) {
@@ -349,7 +344,7 @@ swap_result getSwapFromSwap_SRV4(swap_config config) {
349 if (nswaps == 0) 344 if (nswaps == 0)
350 die(STATE_OK, _("SWAP OK: No swap devices defined\n")); 345 die(STATE_OK, _("SWAP OK: No swap devices defined\n"));
351 346
352 if (config.verbose >= 3) 347 if (verbose >= 3)
353 printf("Found %d swap device(s)\n", nswaps); 348 printf("Found %d swap device(s)\n", nswaps);
354 349
355 /* initialize swap table + entries */ 350 /* initialize swap table + entries */
@@ -362,8 +357,7 @@ swap_result getSwapFromSwap_SRV4(swap_config config) {
362 tbl->swt_n = nswaps; 357 tbl->swt_n = nswaps;
363 358
364 for (int i = 0; i < nswaps; i++) { 359 for (int i = 0; i < nswaps; i++) {
365 if ((tbl->swt_ent[i].ste_path = 360 if ((tbl->swt_ent[i].ste_path = (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL)
366 (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL)
367 die(STATE_UNKNOWN, _("malloc() failed!\n")); 361 die(STATE_UNKNOWN, _("malloc() failed!\n"));
368 } 362 }
369 363
@@ -374,22 +368,25 @@ swap_result getSwapFromSwap_SRV4(swap_config config) {
374 die(STATE_UNKNOWN, _("Error in swapctl call\n")); 368 die(STATE_UNKNOWN, _("Error in swapctl call\n"));
375 } 369 }
376 370
377 double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0; 371 double dsktotal_mb = 0.0;
378 unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 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;
379 377
380 for (int i = 0; i < nswaps; i++) { 378 for (int i = 0; i < nswaps; i++) {
381 dsktotal_mb = (float)tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; 379 dsktotal_mb = (float)tbl->swt_ent[i].ste_pages / SWAP_CONVERSION;
382 dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION; 380 dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION;
383 dskused_mb = (dsktotal_mb - dskfree_mb); 381 dskused_mb = (dsktotal_mb - dskfree_mb);
384 382
385 if (config.verbose >= 3) 383 if (verbose >= 3)
386 printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", 384 printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb);
387 dsktotal_mb, dskfree_mb, dskused_mb);
388 385
389 if (config.allswaps && dsktotal_mb > 0) { 386 if (config.allswaps && dsktotal_mb > 0) {
390 double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb)); 387 double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb));
391 388
392 if (config.verbose) { 389 if (verbose) {
393 printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent); 390 printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent);
394 } 391 }
395 } 392 }