diff options
Diffstat (limited to 'plugins/check_swap.d')
-rw-r--r-- | plugins/check_swap.d/check_swap.h | 10 | ||||
-rw-r--r-- | plugins/check_swap.d/swap.c | 124 |
2 files changed, 71 insertions, 63 deletions
diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h index 99039b21..8d3c7fcf 100644 --- a/plugins/check_swap.d/check_swap.h +++ b/plugins/check_swap.d/check_swap.h | |||
@@ -1,6 +1,8 @@ | |||
1 | #pragma once | 1 | #pragma once |
2 | 2 | ||
3 | #include "../common.h" | 3 | #include "../common.h" |
4 | #include "../../lib/output.h" | ||
5 | #include "../../lib/states.h" | ||
4 | 6 | ||
5 | #ifndef SWAP_CONVERSION | 7 | #ifndef SWAP_CONVERSION |
6 | # define SWAP_CONVERSION 1 | 8 | # define SWAP_CONVERSION 1 |
@@ -25,19 +27,23 @@ typedef struct { | |||
25 | 27 | ||
26 | typedef struct { | 28 | typedef struct { |
27 | bool allswaps; | 29 | bool allswaps; |
28 | int no_swap_state; | 30 | mp_state_enum no_swap_state; |
29 | bool warn_is_set; | 31 | bool warn_is_set; |
30 | check_swap_threshold warn; | 32 | check_swap_threshold warn; |
31 | bool crit_is_set; | 33 | bool crit_is_set; |
32 | check_swap_threshold crit; | 34 | check_swap_threshold crit; |
33 | bool on_aix; | 35 | bool on_aix; |
34 | int conversion_factor; | 36 | int conversion_factor; |
37 | |||
38 | bool output_format_is_set; | ||
39 | mp_output_format output_format; | ||
35 | } swap_config; | 40 | } swap_config; |
36 | 41 | ||
37 | swap_config swap_config_init(void); | 42 | swap_config swap_config_init(void); |
38 | 43 | ||
39 | swap_result get_swap_data(swap_config config); | 44 | swap_result get_swap_data(swap_config config); |
40 | swap_result getSwapFromProcMeminfo(char path_to_proc_meminfo[]); | 45 | swap_result getSwapFromProcMeminfo(char path_to_proc_meminfo[]); |
41 | swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]); | 46 | swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], |
47 | const char swap_format[]); | ||
42 | swap_result getSwapFromSwapctl_BSD(swap_config config); | 48 | swap_result getSwapFromSwapctl_BSD(swap_config config); |
43 | swap_result getSwapFromSwap_SRV4(swap_config config); | 49 | swap_result getSwapFromSwap_SRV4(swap_config config); |
diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 2fe4544f..58213a3c 100644 --- a/plugins/check_swap.d/swap.c +++ b/plugins/check_swap.d/swap.c | |||
@@ -14,6 +14,8 @@ swap_config swap_config_init(void) { | |||
14 | tmp.warn_is_set = false; | 14 | tmp.warn_is_set = false; |
15 | tmp.crit_is_set = false; | 15 | tmp.crit_is_set = false; |
16 | 16 | ||
17 | tmp.output_format_is_set = false; | ||
18 | |||
17 | #ifdef _AIX | 19 | #ifdef _AIX |
18 | tmp.on_aix = true; | 20 | tmp.on_aix = true; |
19 | #else | 21 | #else |
@@ -50,10 +52,10 @@ swap_result get_swap_data(swap_config config) { | |||
50 | } | 52 | } |
51 | # else // HAVE_SWAP | 53 | # else // HAVE_SWAP |
52 | # ifdef CHECK_SWAP_SWAPCTL_SVR4 | 54 | # ifdef CHECK_SWAP_SWAPCTL_SVR4 |
53 | return getSwapFromSwapctl_SRV4(); | 55 | return getSwapFromSwapctl_SRV4(config); |
54 | # else // CHECK_SWAP_SWAPCTL_SVR4 | 56 | # else // CHECK_SWAP_SWAPCTL_SVR4 |
55 | # ifdef CHECK_SWAP_SWAPCTL_BSD | 57 | # ifdef CHECK_SWAP_SWAPCTL_BSD |
56 | return getSwapFromSwapctl_BSD(); | 58 | return getSwapFromSwapctl_BSD(config); |
57 | # else // CHECK_SWAP_SWAPCTL_BSD | 59 | # else // CHECK_SWAP_SWAPCTL_BSD |
58 | # error No way found to retrieve swap | 60 | # error No way found to retrieve swap |
59 | # endif /* CHECK_SWAP_SWAPCTL_BSD */ | 61 | # endif /* CHECK_SWAP_SWAPCTL_BSD */ |
@@ -66,7 +68,7 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
66 | FILE *meminfo_file_ptr; | 68 | FILE *meminfo_file_ptr; |
67 | meminfo_file_ptr = fopen(proc_meminfo, "r"); | 69 | meminfo_file_ptr = fopen(proc_meminfo, "r"); |
68 | 70 | ||
69 | swap_result result = {0}; | 71 | swap_result result = {}; |
70 | result.errorcode = STATE_UNKNOWN; | 72 | result.errorcode = STATE_UNKNOWN; |
71 | 73 | ||
72 | if (meminfo_file_ptr == NULL) { | 74 | if (meminfo_file_ptr == NULL) { |
@@ -76,90 +78,81 @@ swap_result getSwapFromProcMeminfo(char proc_meminfo[]) { | |||
76 | return result; | 78 | return result; |
77 | } | 79 | } |
78 | 80 | ||
79 | uint64_t swap_total = 0; | 81 | unsigned long swap_total = 0; |
80 | uint64_t swap_used = 0; | 82 | unsigned long swap_used = 0; |
81 | uint64_t swap_free = 0; | 83 | unsigned long swap_free = 0; |
82 | 84 | ||
83 | bool found_total = false; | 85 | bool found_total = false; |
84 | bool found_used = false; | ||
85 | bool found_free = false; | 86 | bool found_free = false; |
86 | 87 | ||
87 | char input_buffer[MAX_INPUT_BUFFER]; | 88 | char input_buffer[MAX_INPUT_BUFFER]; |
88 | char str[32]; | 89 | char str[32]; |
89 | 90 | ||
90 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, meminfo_file_ptr)) { | 91 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, meminfo_file_ptr)) { |
91 | uint64_t tmp_KB = 0; | ||
92 | 92 | ||
93 | /* | 93 | /* |
94 | * The following sscanf call looks for a line looking like: "Swap: 123 | 94 | * The following sscanf call looks for a line looking like: "Swap: 123 |
95 | * 123 123" On which kind of system this format exists, I can not say, | 95 | * 123 123" which exists on NetBSD (at least), |
96 | * but I wanted to document this for people who are not adapt with | 96 | * The unit should be Bytes |
97 | * sscanf anymore, like me | ||
98 | * Also the units used here are unclear and probably wrong | ||
99 | */ | 97 | */ |
100 | if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &swap_total, &swap_used, &swap_free) == 3) { | 98 | if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &swap_total, &swap_used, |
101 | 99 | &swap_free) == 3) { | |
102 | result.metrics.total += swap_total; | ||
103 | result.metrics.used += swap_used; | ||
104 | result.metrics.free += swap_free; | ||
105 | |||
106 | found_total = true; | 100 | found_total = true; |
107 | found_free = true; | 101 | found_free = true; |
108 | found_used = true; | ||
109 | |||
110 | // Set error | 102 | // Set error |
111 | result.errorcode = STATE_OK; | 103 | result.errorcode = STATE_OK; |
104 | // Break out of fgets here, since both scanf expressions might match (NetBSD for | ||
105 | // example) | ||
106 | break; | ||
107 | } | ||
112 | 108 | ||
113 | /* | 109 | /* |
114 | * The following sscanf call looks for lines looking like: | 110 | * The following sscanf call looks for lines looking like: |
115 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least | 111 | * "SwapTotal: 123" and "SwapFree: 123" This format exists at least |
116 | * on Debian Linux with a 5.* kernel | 112 | * on Debian Linux with a 5.* kernel |
117 | */ | 113 | */ |
118 | } else { | 114 | unsigned long tmp_KB = 0; |
119 | int sscanf_result = sscanf(input_buffer, | 115 | int sscanf_result = sscanf(input_buffer, |
120 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " | 116 | "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu " |
121 | "%*[k]%*[B]", | 117 | "%*[k]%*[B]", |
122 | str, &tmp_KB); | 118 | str, &tmp_KB); |
123 | 119 | ||
124 | if (sscanf_result == 2) { | 120 | if (sscanf_result == 2) { |
125 | 121 | ||
126 | if (verbose >= 3) { | 122 | if (verbose >= 3) { |
127 | printf("Got %s with %lu\n", str, tmp_KB); | 123 | printf("Got %s with %lu\n", str, tmp_KB); |
128 | } | ||
129 | |||
130 | /* I think this part is always in Kb, so convert to bytes */ | ||
131 | if (strcmp("Total", str) == 0) { | ||
132 | swap_total = tmp_KB * 1000; | ||
133 | found_total = true; | ||
134 | } else if (strcmp("Free", str) == 0) { | ||
135 | swap_free = swap_free + tmp_KB * 1000; | ||
136 | found_free = true; | ||
137 | found_used = true; // No explicit used metric available | ||
138 | } else if (strcmp("Cached", str) == 0) { | ||
139 | swap_free = swap_free + tmp_KB * 1000; | ||
140 | found_free = true; | ||
141 | found_used = true; // No explicit used metric available | ||
142 | } | ||
143 | |||
144 | result.errorcode = STATE_OK; | ||
145 | } | 124 | } |
125 | |||
126 | /* I think this part is always in Kb, so convert to bytes */ | ||
127 | if (strcmp("Total", str) == 0) { | ||
128 | swap_total = tmp_KB * 1000; | ||
129 | found_total = true; | ||
130 | } else if (strcmp("Free", str) == 0) { | ||
131 | swap_free += tmp_KB * 1000; | ||
132 | found_free = true; | ||
133 | } else if (strcmp("Cached", str) == 0) { | ||
134 | swap_free += tmp_KB * 1000; | ||
135 | } | ||
136 | |||
137 | result.errorcode = STATE_OK; | ||
146 | } | 138 | } |
147 | } | 139 | } |
148 | 140 | ||
149 | fclose(meminfo_file_ptr); | 141 | fclose(meminfo_file_ptr); |
150 | 142 | ||
151 | result.metrics.total = swap_total; | 143 | result.metrics.total = swap_total; |
152 | result.metrics.used = swap_total - swap_free; | ||
153 | result.metrics.free = swap_free; | 144 | result.metrics.free = swap_free; |
145 | result.metrics.used = swap_total - swap_free; | ||
154 | 146 | ||
155 | if (!found_free || !found_total || !found_used) { | 147 | if (!found_free || !found_total) { |
156 | result.errorcode = STATE_UNKNOWN; | 148 | result.errorcode = STATE_UNKNOWN; |
157 | } | 149 | } |
158 | 150 | ||
159 | return result; | 151 | return result; |
160 | } | 152 | } |
161 | 153 | ||
162 | swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], const char swap_format[]) { | 154 | swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[], |
155 | const char swap_format[]) { | ||
163 | swap_result result = {0}; | 156 | swap_result result = {0}; |
164 | 157 | ||
165 | char *temp_buffer; | 158 | char *temp_buffer; |
@@ -222,7 +215,8 @@ swap_result getSwapFromSwapCommand(swap_config config, const char swap_command[] | |||
222 | used_swap_mb = total_swap_mb - free_swap_mb; | 215 | used_swap_mb = total_swap_mb - free_swap_mb; |
223 | 216 | ||
224 | if (verbose >= 3) { | 217 | if (verbose >= 3) { |
225 | printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb, free_swap_mb); | 218 | printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb, |
219 | free_swap_mb); | ||
226 | } | 220 | } |
227 | } else { | 221 | } else { |
228 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | 222 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { |
@@ -295,8 +289,14 @@ struct swapent { | |||
295 | }; | 289 | }; |
296 | 290 | ||
297 | #else | 291 | #else |
292 | |||
293 | // Includes for NetBSD | ||
294 | # include <unistd.h> | ||
295 | # include <sys/swap.h> | ||
296 | |||
298 | # define bsd_swapctl swapctl | 297 | # define bsd_swapctl swapctl |
299 | #endif | 298 | |
299 | #endif // CHECK_SWAP_SWAPCTL_BSD | ||
300 | 300 | ||
301 | swap_result getSwapFromSwapctl_BSD(swap_config config) { | 301 | swap_result getSwapFromSwapctl_BSD(swap_config config) { |
302 | /* get the number of active swap devices */ | 302 | /* get the number of active swap devices */ |
@@ -320,8 +320,8 @@ swap_result getSwapFromSwapctl_BSD(swap_config config) { | |||
320 | unsigned long long used_swap_mb = 0; | 320 | unsigned long long used_swap_mb = 0; |
321 | 321 | ||
322 | for (int i = 0; i < nswaps; i++) { | 322 | for (int i = 0; i < nswaps; i++) { |
323 | dsktotal_mb = (float)ent[i].se_nblks / (float)config.conversion_factor; | 323 | dsktotal_mb = (double)ent[i].se_nblks / (double)config.conversion_factor; |
324 | dskused_mb = (float)ent[i].se_inuse / (float)config.conversion_factor; | 324 | dskused_mb = (double)ent[i].se_inuse / (double)config.conversion_factor; |
325 | dskfree_mb = (dsktotal_mb - dskused_mb); | 325 | dskfree_mb = (dsktotal_mb - dskused_mb); |
326 | 326 | ||
327 | if (config.allswaps && dsktotal_mb > 0) { | 327 | if (config.allswaps && dsktotal_mb > 0) { |
@@ -402,7 +402,8 @@ swap_result getSwapFromSwap_SRV4(swap_config config) { | |||
402 | } | 402 | } |
403 | 403 | ||
404 | /* initialize swap table + entries */ | 404 | /* initialize swap table + entries */ |
405 | swaptbl_t *tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * (unsigned long)nswaps)); | 405 | swaptbl_t *tbl = |
406 | (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * (unsigned long)nswaps)); | ||
406 | 407 | ||
407 | if (tbl == NULL) { | 408 | if (tbl == NULL) { |
408 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | 409 | die(STATE_UNKNOWN, _("malloc() failed!\n")); |
@@ -437,7 +438,8 @@ swap_result getSwapFromSwap_SRV4(swap_config config) { | |||
437 | dskused_mb = (dsktotal_mb - dskfree_mb); | 438 | dskused_mb = (dsktotal_mb - dskfree_mb); |
438 | 439 | ||
439 | if (verbose >= 3) { | 440 | if (verbose >= 3) { |
440 | printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb); | 441 | printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, |
442 | dskused_mb); | ||
441 | } | 443 | } |
442 | 444 | ||
443 | if (config.allswaps && dsktotal_mb > 0) { | 445 | if (config.allswaps && dsktotal_mb > 0) { |