summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore4
-rw-r--r--configure.ac2
-rw-r--r--plugins/Makefile.am2
-rw-r--r--plugins/check_swap.c380
-rw-r--r--plugins/check_swap.d/check_swap.h45
-rw-r--r--plugins/check_swap.d/swap.c359
6 files changed, 414 insertions, 378 deletions
diff --git a/.gitignore b/.gitignore
index 6f903d61..7e640a0e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -223,6 +223,10 @@ NP-VERSION-FILE
223/plugins/tests/test_disk 223/plugins/tests/test_disk
224/plugins/tests/.deps 224/plugins/tests/.deps
225 225
226# /plugins/check_swap.d
227/plugins/check_swap.d/.deps
228/plugins/check_swap.d/.dirstamp
229
226# /plugins-root/ 230# /plugins-root/
227/plugins-root/.deps 231/plugins-root/.deps
228/plugins-root/.libs 232/plugins-root/.libs
diff --git a/configure.ac b/configure.ac
index 17272e45..42fc0292 100644
--- a/configure.ac
+++ b/configure.ac
@@ -4,7 +4,7 @@ AC_INIT(monitoring-plugins,2.3git)
4AC_CONFIG_SRCDIR(NPTest.pm) 4AC_CONFIG_SRCDIR(NPTest.pm)
5AC_CONFIG_FILES([gl/Makefile]) 5AC_CONFIG_FILES([gl/Makefile])
6AC_CONFIG_AUX_DIR(build-aux) 6AC_CONFIG_AUX_DIR(build-aux)
7AM_INIT_AUTOMAKE([1.8.3]) 7AM_INIT_AUTOMAKE([1.8.3 subdir-objects])
8AM_SILENT_RULES([yes]) 8AM_SILENT_RULES([yes])
9AM_MAINTAINER_MODE([enable]) 9AM_MAINTAINER_MODE([enable])
10AC_CONFIG_HEADERS([config.h]) 10AC_CONFIG_HEADERS([config.h])
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 49086b7a..1ca7cf3f 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -107,7 +107,7 @@ check_real_LDADD = $(NETLIBS)
107check_snmp_LDADD = $(BASEOBJS) 107check_snmp_LDADD = $(BASEOBJS)
108check_smtp_LDADD = $(SSLOBJS) 108check_smtp_LDADD = $(SSLOBJS)
109check_ssh_LDADD = $(NETLIBS) 109check_ssh_LDADD = $(NETLIBS)
110check_swap_LDADD = $(MATHLIBS) $(BASEOBJS) 110check_swap_LDADD = $(MATHLIBS) $(BASEOBJS) check_swap.d/swap.o
111check_tcp_LDADD = $(SSLOBJS) 111check_tcp_LDADD = $(SSLOBJS)
112check_time_LDADD = $(NETLIBS) 112check_time_LDADD = $(NETLIBS)
113check_ntp_time_LDADD = $(NETLIBS) $(MATHLIBS) 113check_ntp_time_LDADD = $(NETLIBS) $(MATHLIBS)
diff --git a/plugins/check_swap.c b/plugins/check_swap.c
index ac74931a..f34b0514 100644
--- a/plugins/check_swap.c
+++ b/plugins/check_swap.c
@@ -31,9 +31,6 @@ const char *progname = "check_swap";
31const char *copyright = "2000-2023"; 31const char *copyright = "2000-2023";
32const char *email = "devel@monitoring-plugins.org"; 32const char *email = "devel@monitoring-plugins.org";
33 33
34#include "common.h"
35#include "popen.h"
36#include "utils.h"
37 34
38#ifdef HAVE_DECL_SWAPCTL 35#ifdef HAVE_DECL_SWAPCTL
39#ifdef HAVE_SYS_PARAM_H 36#ifdef HAVE_SYS_PARAM_H
@@ -47,34 +44,8 @@ const char *email = "devel@monitoring-plugins.org";
47#endif 44#endif
48#endif 45#endif
49 46
50#ifndef SWAP_CONVERSION 47#include "./check_swap.d/check_swap.h"
51#define SWAP_CONVERSION 1 48#include "./utils.h"
52#endif
53
54typedef struct {
55 bool is_percentage;
56 uint64_t value;
57} threshold;
58
59typedef struct {
60 unsigned long long free; // Free swap in Bytes!
61 unsigned long long used; // Used swap in Bytes!
62 unsigned long long total; // Total swap size, you guessed it, in Bytes!
63} swap_metrics;
64
65typedef struct {
66 int errorcode;
67 int statusCode;
68 swap_metrics metrics;
69} swap_result;
70
71typedef struct {
72 int verbose;
73 bool allswaps;
74 int no_swap_state;
75 threshold warn;
76 threshold crit;
77} swap_config;
78 49
79typedef struct { 50typedef struct {
80 int errorcode; 51 int errorcode;
@@ -86,19 +57,6 @@ swap_config_wrapper process_arguments(swap_config_wrapper config, int argc,
86void print_usage(); 57void print_usage();
87void print_help(swap_config); 58void print_help(swap_config);
88 59
89swap_result getSwapFromProcMeminfo(swap_config config);
90swap_result getSwapFromSwapCommand(swap_config config);
91swap_result getSwapFromSwapctl_BSD(swap_config config);
92swap_result getSwapFromSwap_SRV4(swap_config config);
93
94swap_config swap_config_init() {
95 swap_config tmp = {0};
96 tmp.allswaps = false;
97 tmp.no_swap_state = STATE_CRITICAL;
98 tmp.verbose = 0;
99
100 return tmp;
101}
102 60
103int main(int argc, char **argv) { 61int main(int argc, char **argv) {
104 setlocale(LC_ALL, ""); 62 setlocale(LC_ALL, "");
@@ -123,23 +81,7 @@ int main(int argc, char **argv) {
123 81
124 swap_config config = tmp.config; 82 swap_config config = tmp.config;
125 83
126#ifdef HAVE_PROC_MEMINFO 84 swap_result data = get_swap_data(config);
127 swap_result data = getSwapFromProcMeminfo(config);
128#else
129#ifdef HAVE_SWAP
130 swap_result data = getSwapFromSwapCommand();
131#else
132#ifdef CHECK_SWAP_SWAPCTL_SVR4
133 swap_result data = getSwapFromSwapctl_SRV4();
134#else
135#ifdef CHECK_SWAP_SWAPCTL_BSD
136 swap_result data = getSwapFromSwapctl_BSD();
137#else
138#error No way found to retrieve swap
139#endif /* CHECK_SWAP_SWAPCTL_BSD */
140#endif /* CHECK_SWAP_SWAPCTL_SVR4 */
141#endif /* HAVE_SWAP */
142#endif /* HAVE_PROC_MEMINFO */
143 85
144 double percent_used; 86 double percent_used;
145 87
@@ -323,7 +265,7 @@ swap_config_wrapper process_arguments(swap_config_wrapper conf_wrapper,
323 return conf_wrapper; 265 return conf_wrapper;
324 } else if ((conf_wrapper.config.warn.is_percentage == 266 } else if ((conf_wrapper.config.warn.is_percentage ==
325 conf_wrapper.config.crit.is_percentage) && 267 conf_wrapper.config.crit.is_percentage) &&
326 (conf_wrapper.config.warn.value >= 268 (conf_wrapper.config.warn.value <
327 conf_wrapper.config.crit.value)) { 269 conf_wrapper.config.crit.value)) {
328 /* This is NOT triggered if warn and crit are different units, e.g warn 270 /* This is NOT triggered if warn and crit are different units, e.g warn
329 * is percentage and crit is absolute. We cannot determine the condition 271 * is percentage and crit is absolute. We cannot determine the condition
@@ -387,317 +329,3 @@ void print_usage() {
387 printf(" %s [-av] -w <percent_free>%% -c <percent_free>%%\n", progname); 329 printf(" %s [-av] -w <percent_free>%% -c <percent_free>%%\n", progname);
388 printf(" -w <bytes_free> -c <bytes_free> [-n <state>]\n"); 330 printf(" -w <bytes_free> -c <bytes_free> [-n <state>]\n");
389} 331}
390
391#ifdef HAVE_PROC_MEMINFO
392swap_result getSwapFromProcMeminfo(swap_config config) {
393
394 if (config.verbose >= 3) {
395 printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO);
396 }
397
398 FILE *fp;
399 fp = fopen(PROC_MEMINFO, "r");
400
401 swap_result result = {0};
402 result.statusCode = STATE_OK;
403
404 uint64_t swap_total = 0, swap_used = 0, swap_free = 0;
405
406 char input_buffer[MAX_INPUT_BUFFER];
407 char str[32];
408
409 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
410 uint64_t tmp_KB = 0;
411
412 /*
413 * The following sscanf call looks for a line looking like: "Swap: 123
414 * 123 123" On which kind of system this format exists, I can not say,
415 * but I wanted to document this for people who are not adapt with
416 * sscanf anymore, like me
417 */
418 if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu",
419 &swap_total, &swap_used, &swap_free) == 3) {
420
421 result.metrics.total += swap_total;
422 result.metrics.used += swap_used;
423 result.metrics.free += swap_free;
424
425 /*
426 * The following sscanf call looks for lines looking like:
427 * "SwapTotal: 123" and "SwapFree: 123" This format exists at least
428 * on Debian Linux with a 5.* kernel
429 */
430 } else if (sscanf(input_buffer,
431 "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu "
432 "%*[k]%*[B]",
433 str, &tmp_KB)) {
434 if (config.verbose >= 3) {
435 printf("Got %s with %lu\n", str, tmp_KB);
436 }
437 /* I think this part is always in Kb, so convert to mb */
438 if (strcmp("Total", str) == 0) {
439 swap_total = tmp_KB * 1024;
440 } else if (strcmp("Free", str) == 0) {
441 swap_free = swap_free + tmp_KB * 1024;
442 } else if (strcmp("Cached", str) == 0) {
443 swap_free = swap_free + tmp_KB * 1024;
444 }
445 }
446 }
447
448 fclose(fp);
449
450 result.metrics.total = swap_total;
451 result.metrics.used = swap_total - swap_free;
452 result.metrics.free = swap_free;
453
454 return result;
455}
456#endif
457
458#ifdef HAVE_SWAP
459swap_result getSwapFromSwapCommand() {
460 swap_result result = {0};
461
462 char *temp_buffer;
463 char *swap_command;
464 char *swap_format;
465 int conv_factor = SWAP_CONVERSION;
466
467 xasprintf(&swap_command, "%s", SWAP_COMMAND);
468 xasprintf(&swap_format, "%s", SWAP_FORMAT);
469
470/* These override the command used if a summary (and thus ! allswaps) is
471 * required */
472/* The summary flag returns more accurate information about swap usage on these
473 * OSes */
474#ifdef _AIX
475 if (!allswaps) {
476 xasprintf(&swap_command, "%s", "/usr/sbin/lsps -s");
477 xasprintf(&swap_format, "%s", "%lu%*s %lu");
478 conv_factor = 1;
479 }
480#endif
481
482 if (verbose >= 2)
483 printf(_("Command: %s\n"), swap_command);
484 if (verbose >= 3)
485 printf(_("Format: %s\n"), swap_format);
486
487 child_process = spopen(swap_command);
488 if (child_process == NULL) {
489 printf(_("Could not open pipe: %s\n"), swap_command);
490 return STATE_UNKNOWN;
491 }
492
493 child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r");
494 if (child_stderr == NULL)
495 printf(_("Could not open stderr for %s\n"), swap_command);
496
497 sprintf(str, "%s", "");
498 /* read 1st line */
499 fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process);
500 if (strcmp(swap_format, "") == 0) {
501 temp_buffer = strtok(input_buffer, " \n");
502 while (temp_buffer) {
503 if (strstr(temp_buffer, "blocks"))
504 sprintf(str, "%s %s", str, "%lu");
505 else if (strstr(temp_buffer, "dskfree"))
506 sprintf(str, "%s %s", str, "%lu");
507 else
508 sprintf(str, "%s %s", str, "%*s");
509 temp_buffer = strtok(NULL, " \n");
510 }
511 }
512
513/* If different swap command is used for summary switch, need to read format
514 * differently */
515#ifdef _AIX
516 if (!allswaps) {
517 fgets(input_buffer, MAX_INPUT_BUFFER - 1,
518 child_process); /* Ignore first line */
519 sscanf(input_buffer, swap_format, &total_swap_mb, &used_swap_mb);
520 free_swap_mb = total_swap_mb * (100 - used_swap_mb) / 100;
521 used_swap_mb = total_swap_mb - free_swap_mb;
522 if (verbose >= 3)
523 printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb,
524 used_swap_mb, free_swap_mb);
525 } else {
526#endif
527 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
528 sscanf(input_buffer, swap_format, &dsktotal_mb, &dskfree_mb);
529
530 dsktotal_mb = dsktotal_mb / conv_factor;
531 /* AIX lists percent used, so this converts to dskfree in MBs */
532#ifdef _AIX
533 dskfree_mb = dsktotal_mb * (100 - dskfree_mb) / 100;
534#else
535 dskfree_mb = dskfree_mb / conv_factor;
536#endif
537 if (verbose >= 3)
538 printf(_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb);
539
540 dskused_mb = dsktotal_mb - dskfree_mb;
541 total_swap_mb += dsktotal_mb;
542 used_swap_mb += dskused_mb;
543 free_swap_mb += dskfree_mb;
544 if (allswaps) {
545 percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb));
546 result = max_state(result, check_swap(dskfree_mb, dsktotal_mb));
547 if (verbose)
548 xasprintf(&status, "%s [%.0f (%d%%)]", status, dskfree_mb,
549 100 - percent);
550 }
551 }
552#ifdef _AIX
553 }
554#endif
555
556 /* If we get anything on STDERR, at least set warning */
557 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
558 result = max_state(result, STATE_WARNING);
559
560 /* close stderr */
561 (void)fclose(child_stderr);
562
563 /* close the pipe */
564 if (spclose(child_process))
565 result = max_state(result, STATE_WARNING);
566}
567#endif // HAVE_SWAP
568
569#ifdef CHECK_SWAP_SWAPCTL_BSD
570swap_result getSwapFromSwapctl_BSD(swap_config config) {
571 int i = 0, nswaps = 0, swapctl_res = 0;
572 struct swapent *ent;
573 int conv_factor = SWAP_CONVERSION;
574
575 /* get the number of active swap devices */
576 nswaps = swapctl(SWAP_NSWAP, NULL, 0);
577
578 /* initialize swap table + entries */
579 ent = (struct swapent *)malloc(sizeof(struct swapent) * nswaps);
580
581 /* and now, tally 'em up */
582 swapctl_res = swapctl(SWAP_STATS, ent, nswaps);
583 if (swapctl_res < 0) {
584 perror(_("swapctl failed: "));
585 die(STATE_UNKNOWN, _("Error in swapctl call\n"));
586 }
587
588
589 double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0;
590 unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0;
591
592 for (i = 0; i < nswaps; i++) {
593 dsktotal_mb = (float)ent[i].se_nblks / conv_factor;
594 dskused_mb = (float)ent[i].se_inuse / conv_factor;
595 dskfree_mb = (dsktotal_mb - dskused_mb);
596
597 if (config.allswaps && dsktotal_mb > 0) {
598 double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb));
599
600 if (config.verbose) {
601 printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent);
602 }
603 }
604
605 total_swap_mb += dsktotal_mb;
606 free_swap_mb += dskfree_mb;
607 used_swap_mb += dskused_mb;
608 }
609
610 /* and clean up after ourselves */
611 free(ent);
612
613 swap_result result = {0};
614
615 result.statusCode = OK;
616 result.errorcode = OK;
617
618 result.metrics.total = total_swap_mb * 1024 * 1024;
619 result.metrics.free = free_swap_mb * 1024 * 1024;
620 result.metrics.used = used_swap_mb * 1024 * 1024;
621
622 return result;
623}
624#endif // CHECK_SWAP_SWAPCTL_BSD
625
626#ifdef CHECK_SWAP_SWAPCTL_SVR4
627swap_result getSwapFromSwap_SRV4(swap_config config) {
628 int i = 0, nswaps = 0, swapctl_res = 0;
629 //swaptbl_t *tbl = NULL;
630 void*tbl = NULL;
631 //swapent_t *ent = NULL;
632 void*ent = NULL;
633 /* get the number of active swap devices */
634 if ((nswaps = swapctl(SC_GETNSWP, NULL)) == -1)
635 die(STATE_UNKNOWN, _("Error getting swap devices\n"));
636
637 if (nswaps == 0)
638 die(STATE_OK, _("SWAP OK: No swap devices defined\n"));
639
640 if (config.verbose >= 3)
641 printf("Found %d swap device(s)\n", nswaps);
642
643 /* initialize swap table + entries */
644 tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps));
645
646 if (tbl == NULL)
647 die(STATE_UNKNOWN, _("malloc() failed!\n"));
648
649 memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps));
650 tbl->swt_n = nswaps;
651 for (i = 0; i < nswaps; i++) {
652 if ((tbl->swt_ent[i].ste_path =
653 (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL)
654 die(STATE_UNKNOWN, _("malloc() failed!\n"));
655 }
656
657 /* and now, tally 'em up */
658 swapctl_res = swapctl(SC_LIST, tbl);
659 if (swapctl_res < 0) {
660 perror(_("swapctl failed: "));
661 die(STATE_UNKNOWN, _("Error in swapctl call\n"));
662 }
663
664 double dsktotal_mb = 0.0, dskfree_mb = 0.0, dskused_mb = 0.0;
665 unsigned long long total_swap_mb = 0, free_swap_mb = 0, used_swap_mb = 0;
666
667 for (i = 0; i < nswaps; i++) {
668 dsktotal_mb = (float)tbl->swt_ent[i].ste_pages / SWAP_CONVERSION;
669 dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION;
670 dskused_mb = (dsktotal_mb - dskfree_mb);
671
672 if (config.verbose >= 3)
673 printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n",
674 dsktotal_mb, dskfree_mb, dskused_mb);
675
676 if (config.allswaps && dsktotal_mb > 0) {
677 double percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb));
678
679 if (config.verbose) {
680 printf("[%.0f (%g%%)]", dskfree_mb, 100 - percent);
681 }
682 }
683
684 total_swap_mb += dsktotal_mb;
685 free_swap_mb += dskfree_mb;
686 used_swap_mb += dskused_mb;
687 }
688
689 /* and clean up after ourselves */
690 for (i = 0; i < nswaps; i++) {
691 free(tbl->swt_ent[i].ste_path);
692 }
693 free(tbl);
694
695 swap_result result = {0};
696 result.errorcode = OK;
697 result.metrics.total = total_swap_mb * 1024 * 1024;
698 result.metrics.free = free_swap_mb * 1024 * 1024;
699 result.metrics.used = used_swap_mb * 1024 * 1024;
700
701 return result;
702}
703#endif // CHECK_SWAP_SWAPCTL_SVR4
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..ebfa840d
--- /dev/null
+++ b/plugins/check_swap.d/swap.c
@@ -0,0 +1,359 @@
1#include "./check_swap.d/check_swap.h"
2
3swap_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
19swap_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
58swap_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
121swap_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
227swap_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
283swap_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