diff options
Diffstat (limited to 'plugins/check_swap.c')
-rw-r--r-- | plugins/check_swap.c | 831 |
1 files changed, 312 insertions, 519 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c index e7ee785d..4d3b6099 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c | |||
@@ -1,607 +1,400 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring check_swap plugin | 3 | * Monitoring check_swap plugin |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) | 6 | * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) |
7 | * Copyright (c) 2000-2024 Monitoring Plugins Development Team | 7 | * Copyright (c) 2000-2024 Monitoring Plugins Development Team |
8 | * | 8 | * |
9 | * Description: | 9 | * Description: |
10 | * | 10 | * |
11 | * This file contains the check_swap plugin | 11 | * This file contains the check_swap plugin |
12 | * | 12 | * |
13 | * | 13 | * |
14 | * This program is free software: you can redistribute it and/or modify | 14 | * This program is free software: you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by | 15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation, either version 3 of the License, or | 16 | * the Free Software Foundation, either version 3 of the License, or |
17 | * (at your option) any later version. | 17 | * (at your option) any later version. |
18 | * | 18 | * |
19 | * This program is distributed in the hope that it will be useful, | 19 | * This program is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. | 22 | * GNU General Public License for more details. |
23 | * | 23 | * |
24 | * You should have received a copy of the GNU General Public License | 24 | * You should have received a copy of the GNU General Public License |
25 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 25 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
26 | * | 26 | * |
27 | * | 27 | * |
28 | *****************************************************************************/ | 28 | *****************************************************************************/ |
29 | |||
30 | const char *progname = "check_swap"; | ||
31 | const char *copyright = "2000-2024"; | ||
32 | const char *email = "devel@monitoring-plugins.org"; | ||
33 | 29 | ||
34 | #include "common.h" | 30 | #include "common.h" |
35 | #include "popen.h" | 31 | #include "output.h" |
36 | #include "utils.h" | 32 | #include "states.h" |
37 | 33 | #include <limits.h> | |
38 | #ifdef HAVE_DECL_SWAPCTL | 34 | #ifdef HAVE_DECL_SWAPCTL |
39 | # ifdef HAVE_SYS_PARAM_H | 35 | # ifdef HAVE_SYS_PARAM_H |
40 | # include <sys/param.h> | 36 | # include <sys/param.h> |
41 | # endif | 37 | # endif |
42 | # ifdef HAVE_SYS_SWAP_H | 38 | # ifdef HAVE_SYS_SWAP_H |
43 | # include <sys/swap.h> | 39 | # include <sys/swap.h> |
44 | # endif | 40 | # endif |
45 | # ifdef HAVE_SYS_STAT_H | 41 | # ifdef HAVE_SYS_STAT_H |
46 | # include <sys/stat.h> | 42 | # include <sys/stat.h> |
47 | # endif | 43 | # endif |
48 | #endif | 44 | #endif |
49 | 45 | ||
50 | #ifndef SWAP_CONVERSION | 46 | #include <stdint.h> |
51 | # define SWAP_CONVERSION 1 | 47 | #include "./check_swap.d/check_swap.h" |
52 | #endif | 48 | #include "./utils.h" |
53 | 49 | ||
54 | typedef struct { | 50 | typedef struct { |
55 | bool is_percentage; | 51 | int errorcode; |
56 | uint64_t value; | 52 | swap_config config; |
57 | } threshold; | 53 | } swap_config_wrapper; |
58 | 54 | ||
59 | int check_swap (float free_swap_mb, float total_swap_mb); | 55 | static swap_config_wrapper process_arguments(int argc, char **argv); |
60 | int process_arguments (int argc, char **argv); | 56 | void print_usage(void); |
61 | int validate_arguments (void); | 57 | static void print_help(swap_config /*config*/); |
62 | void print_usage (void); | 58 | |
63 | void print_help (void); | ||
64 | |||
65 | threshold warn; | ||
66 | threshold crit; | ||
67 | int verbose; | 59 | int verbose; |
68 | bool allswaps = false; | ||
69 | int no_swap_state = STATE_CRITICAL; | ||
70 | |||
71 | int | ||
72 | main (int argc, char **argv) | ||
73 | { | ||
74 | unsigned int percent_used, percent; | ||
75 | uint64_t total_swap_mb = 0, used_swap_mb = 0, free_swap_mb = 0; | ||
76 | uint64_t dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0; | ||
77 | uint64_t tmp_KB = 0; | ||
78 | int result = STATE_UNKNOWN; | ||
79 | char input_buffer[MAX_INPUT_BUFFER]; | ||
80 | #ifdef HAVE_PROC_MEMINFO | ||
81 | FILE *fp; | ||
82 | #else | ||
83 | int conv_factor = SWAP_CONVERSION; | ||
84 | # ifdef HAVE_SWAP | ||
85 | char *temp_buffer; | ||
86 | char *swap_command; | ||
87 | char *swap_format; | ||
88 | # else | ||
89 | # ifdef HAVE_DECL_SWAPCTL | ||
90 | int i=0, nswaps=0, swapctl_res=0; | ||
91 | # ifdef CHECK_SWAP_SWAPCTL_SVR4 | ||
92 | swaptbl_t *tbl=NULL; | ||
93 | swapent_t *ent=NULL; | ||
94 | # else | ||
95 | # ifdef CHECK_SWAP_SWAPCTL_BSD | ||
96 | struct swapent *ent; | ||
97 | # endif /* CHECK_SWAP_SWAPCTL_BSD */ | ||
98 | # endif /* CHECK_SWAP_SWAPCTL_SVR4 */ | ||
99 | # endif /* HAVE_DECL_SWAPCTL */ | ||
100 | # endif | ||
101 | #endif | ||
102 | char str[32]; | ||
103 | char *status; | ||
104 | 60 | ||
105 | setlocale (LC_ALL, ""); | 61 | #define HUNDRED_PERCENT 100 |
106 | bindtextdomain (PACKAGE, LOCALEDIR); | 62 | |
107 | textdomain (PACKAGE); | 63 | #define BYTES_TO_KiB(number) (number / 1024) |
64 | #define BYTES_TO_MiB(number) (BYTES_TO_KiB(number) / 1024) | ||
65 | |||
66 | const char *progname = "check_swap"; | ||
67 | const char *copyright = "2000-2024"; | ||
68 | const char *email = "devel@monitoring-plugins.org"; | ||
108 | 69 | ||
109 | status = strdup (""); | 70 | int main(int argc, char **argv) { |
71 | setlocale(LC_ALL, ""); | ||
72 | bindtextdomain(PACKAGE, LOCALEDIR); | ||
73 | textdomain(PACKAGE); | ||
110 | 74 | ||
111 | /* Parse extra opts if any */ | 75 | /* Parse extra opts if any */ |
112 | argv=np_extra_opts (&argc, argv, progname); | 76 | argv = np_extra_opts(&argc, argv, progname); |
113 | 77 | ||
114 | if (process_arguments (argc, argv) == ERROR) | 78 | swap_config_wrapper tmp = process_arguments(argc, argv); |
115 | usage4 (_("Could not parse arguments")); | ||
116 | 79 | ||
117 | #ifdef HAVE_PROC_MEMINFO | 80 | if (tmp.errorcode != OK) { |
118 | if (verbose >= 3) { | 81 | usage4(_("Could not parse arguments")); |
119 | printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO); | ||
120 | } | 82 | } |
121 | fp = fopen (PROC_MEMINFO, "r"); | ||
122 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { | ||
123 | /* | ||
124 | * The following sscanf call looks for a line looking like: "Swap: 123 123 123" | ||
125 | * On which kind of system this format exists, I can not say, but I wanted to | ||
126 | * document this for people who are not adapt with sscanf anymore, like me | ||
127 | */ | ||
128 | if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", &dsktotal_mb, &dskused_mb, &dskfree_mb) == 3) { | ||
129 | dsktotal_mb = dsktotal_mb / (1024 * 1024); /* Apply conversion */ | ||
130 | dskused_mb = dskused_mb / (1024 * 1024); | ||
131 | dskfree_mb = dskfree_mb / (1024 * 1024); | ||
132 | total_swap_mb += dsktotal_mb; | ||
133 | used_swap_mb += dskused_mb; | ||
134 | free_swap_mb += dskfree_mb; | ||
135 | if (allswaps) { | ||
136 | if (dsktotal_mb == 0) | ||
137 | percent=100.0; | ||
138 | else | ||
139 | percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); | ||
140 | result = max_state (result, check_swap (dskfree_mb, dsktotal_mb)); | ||
141 | if (verbose) | ||
142 | xasprintf (&status, "%s [%lu (%d%%)]", status, dskfree_mb, 100 - percent); | ||
143 | } | ||
144 | } | ||
145 | 83 | ||
146 | /* | 84 | swap_config config = tmp.config; |
147 | * The following sscanf call looks for lines looking like: "SwapTotal: 123" and "SwapFree: 123" | ||
148 | * This format exists at least on Debian Linux with a 5.* kernel | ||
149 | */ | ||
150 | else if (sscanf (input_buffer, "%*[S]%*[w]%*[a]%*[p]%[TotalFreCchd]%*[:] %lu %*[k]%*[B]", str, &tmp_KB)) { | ||
151 | if (verbose >= 3) { | ||
152 | printf("Got %s with %lu\n", str, tmp_KB); | ||
153 | } | ||
154 | /* I think this part is always in Kb, so convert to mb */ | ||
155 | if (strcmp ("Total", str) == 0) { | ||
156 | dsktotal_mb = tmp_KB / 1024; | ||
157 | } | ||
158 | else if (strcmp ("Free", str) == 0) { | ||
159 | dskfree_mb = dskfree_mb + tmp_KB / 1024; | ||
160 | } | ||
161 | else if (strcmp ("Cached", str) == 0) { | ||
162 | dskfree_mb = dskfree_mb + tmp_KB / 1024; | ||
163 | } | ||
164 | } | ||
165 | } | ||
166 | fclose(fp); | ||
167 | dskused_mb = dsktotal_mb - dskfree_mb; | ||
168 | total_swap_mb = dsktotal_mb; | ||
169 | used_swap_mb = dskused_mb; | ||
170 | free_swap_mb = dskfree_mb; | ||
171 | #else | ||
172 | # ifdef HAVE_SWAP | ||
173 | xasprintf(&swap_command, "%s", SWAP_COMMAND); | ||
174 | xasprintf(&swap_format, "%s", SWAP_FORMAT); | ||
175 | |||
176 | /* These override the command used if a summary (and thus ! allswaps) is required */ | ||
177 | /* The summary flag returns more accurate information about swap usage on these OSes */ | ||
178 | # ifdef _AIX | ||
179 | if (!allswaps) { | ||
180 | xasprintf(&swap_command, "%s", "/usr/sbin/lsps -s"); | ||
181 | xasprintf(&swap_format, "%s", "%lu%*s %lu"); | ||
182 | conv_factor = 1; | ||
183 | } | ||
184 | # endif | ||
185 | 85 | ||
186 | if (verbose >= 2) | 86 | swap_result data = get_swap_data(config); |
187 | printf (_("Command: %s\n"), swap_command); | ||
188 | if (verbose >= 3) | ||
189 | printf (_("Format: %s\n"), swap_format); | ||
190 | 87 | ||
191 | child_process = spopen (swap_command); | 88 | if (data.errorcode != STATE_OK) { |
192 | if (child_process == NULL) { | 89 | puts("SWAP UNKNOWN - Failed to retrieve Swap usage"); |
193 | printf (_("Could not open pipe: %s\n"), swap_command); | 90 | exit(STATE_UNKNOWN); |
194 | return STATE_UNKNOWN; | ||
195 | } | 91 | } |
196 | 92 | ||
197 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | 93 | double percent_used; |
198 | if (child_stderr == NULL) | 94 | mp_check overall = mp_check_init(); |
199 | printf (_("Could not open stderr for %s\n"), swap_command); | 95 | if (config.output_format_is_set) { |
200 | 96 | overall.format = config.output_format; | |
201 | sprintf (str, "%s", ""); | ||
202 | /* read 1st line */ | ||
203 | fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); | ||
204 | if (strcmp (swap_format, "") == 0) { | ||
205 | temp_buffer = strtok (input_buffer, " \n"); | ||
206 | while (temp_buffer) { | ||
207 | if (strstr (temp_buffer, "blocks")) | ||
208 | sprintf (str, "%s %s", str, "%lu"); | ||
209 | else if (strstr (temp_buffer, "dskfree")) | ||
210 | sprintf (str, "%s %s", str, "%lu"); | ||
211 | else | ||
212 | sprintf (str, "%s %s", str, "%*s"); | ||
213 | temp_buffer = strtok (NULL, " \n"); | ||
214 | } | ||
215 | } | 97 | } |
98 | mp_subcheck sc1 = mp_subcheck_init(); | ||
99 | sc1 = mp_set_subcheck_default_state(sc1, STATE_OK); | ||
216 | 100 | ||
217 | /* If different swap command is used for summary switch, need to read format differently */ | 101 | /* if total_swap_mb == 0, let's not divide by 0 */ |
218 | # ifdef _AIX | 102 | if (data.metrics.total != 0) { |
219 | if (!allswaps) { | 103 | percent_used = HUNDRED_PERCENT * ((double)data.metrics.used) / ((double)data.metrics.total); |
220 | fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */ | ||
221 | sscanf (input_buffer, swap_format, &total_swap_mb, &used_swap_mb); | ||
222 | free_swap_mb = total_swap_mb * (100 - used_swap_mb) /100; | ||
223 | used_swap_mb = total_swap_mb - free_swap_mb; | ||
224 | if (verbose >= 3) | ||
225 | printf (_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb, free_swap_mb); | ||
226 | } else { | 104 | } else { |
227 | # endif | 105 | sc1 = mp_set_subcheck_state(sc1, config.no_swap_state); |
228 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | 106 | sc1.output = (char *)_("Swap is either disabled, not present, or of zero size."); |
229 | sscanf (input_buffer, swap_format, &dsktotal_mb, &dskfree_mb); | ||
230 | |||
231 | dsktotal_mb = dsktotal_mb / conv_factor; | ||
232 | /* AIX lists percent used, so this converts to dskfree in MBs */ | ||
233 | # ifdef _AIX | ||
234 | dskfree_mb = dsktotal_mb * (100 - dskfree_mb) / 100; | ||
235 | # else | ||
236 | dskfree_mb = dskfree_mb / conv_factor; | ||
237 | # endif | ||
238 | if (verbose >= 3) | ||
239 | printf (_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb); | ||
240 | |||
241 | dskused_mb = dsktotal_mb - dskfree_mb; | ||
242 | total_swap_mb += dsktotal_mb; | ||
243 | used_swap_mb += dskused_mb; | ||
244 | free_swap_mb += dskfree_mb; | ||
245 | if (allswaps) { | ||
246 | percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); | ||
247 | result = max_state (result, check_swap (dskfree_mb, dsktotal_mb)); | ||
248 | if (verbose) | ||
249 | xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); | ||
250 | } | ||
251 | } | ||
252 | # ifdef _AIX | ||
253 | } | ||
254 | # endif | ||
255 | |||
256 | /* If we get anything on STDERR, at least set warning */ | ||
257 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) | ||
258 | result = max_state (result, STATE_WARNING); | ||
259 | |||
260 | /* close stderr */ | ||
261 | (void) fclose (child_stderr); | ||
262 | |||
263 | /* close the pipe */ | ||
264 | if (spclose (child_process)) | ||
265 | result = max_state (result, STATE_WARNING); | ||
266 | # else | ||
267 | # ifdef CHECK_SWAP_SWAPCTL_SVR4 | ||
268 | 107 | ||
269 | /* get the number of active swap devices */ | 108 | mp_add_subcheck_to_check(&overall, sc1); |
270 | if((nswaps=swapctl(SC_GETNSWP, NULL))== -1) | 109 | mp_exit(overall); |
271 | die(STATE_UNKNOWN, _("Error getting swap devices\n") ); | 110 | } |
272 | 111 | ||
273 | if(nswaps == 0) | 112 | if (verbose) { |
274 | die(STATE_OK, _("SWAP OK: No swap devices defined\n")); | 113 | printf("Computed usage percentage: %g\n", percent_used); |
114 | } | ||
275 | 115 | ||
276 | if(verbose >= 3) | 116 | mp_perfdata pd = perfdata_init(); |
277 | printf("Found %d swap device(s)\n", nswaps); | 117 | pd.label = "swap"; |
118 | pd = mp_set_pd_value(pd, data.metrics.free); | ||
119 | pd.uom = "B"; | ||
278 | 120 | ||
279 | /* initialize swap table + entries */ | 121 | if (config.warn_is_set) { |
280 | tbl=(swaptbl_t*)malloc(sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps)); | 122 | uint64_t warn_print = config.warn.value; |
123 | if (config.warn.is_percentage) { | ||
124 | warn_print = config.warn.value * (data.metrics.total / HUNDRED_PERCENT); | ||
125 | } | ||
281 | 126 | ||
282 | if(tbl==NULL) | 127 | mp_perfdata_value warn_pd = mp_create_pd_value(warn_print); |
283 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
284 | 128 | ||
285 | memset(tbl, 0, sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps)); | 129 | mp_range warn_range = mp_range_init(); |
286 | tbl->swt_n=nswaps; | 130 | warn_range.end_infinity = false; |
287 | for(i=0;i<nswaps;i++){ | 131 | warn_range.end = warn_pd; |
288 | if((tbl->swt_ent[i].ste_path=(char*)malloc(sizeof(char)*MAXPATHLEN)) == NULL) | ||
289 | die(STATE_UNKNOWN, _("malloc() failed!\n")); | ||
290 | } | ||
291 | 132 | ||
292 | /* and now, tally 'em up */ | 133 | pd.warn = warn_range; |
293 | swapctl_res=swapctl(SC_LIST, tbl); | 134 | pd.warn_present = true; |
294 | if(swapctl_res < 0){ | ||
295 | perror(_("swapctl failed: ")); | ||
296 | die(STATE_UNKNOWN, _("Error in swapctl call\n")); | ||
297 | } | 135 | } |
298 | 136 | ||
299 | for(i=0;i<nswaps;i++){ | 137 | if (config.crit_is_set) { |
300 | dsktotal_mb = (float) tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; | 138 | uint64_t crit_print = config.crit.value; |
301 | dskfree_mb = (float) tbl->swt_ent[i].ste_free / SWAP_CONVERSION; | 139 | if (config.crit.is_percentage) { |
302 | dskused_mb = ( dsktotal_mb - dskfree_mb ); | 140 | crit_print = config.crit.value * (data.metrics.total / HUNDRED_PERCENT); |
303 | |||
304 | if (verbose >= 3) | ||
305 | printf ("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb); | ||
306 | |||
307 | if(allswaps && dsktotal_mb > 0){ | ||
308 | percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); | ||
309 | result = max_state (result, check_swap (dskfree_mb, dsktotal_mb)); | ||
310 | if (verbose) { | ||
311 | xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); | ||
312 | } | ||
313 | } | 141 | } |
314 | 142 | ||
315 | total_swap_mb += dsktotal_mb; | 143 | mp_perfdata_value crit_pd = mp_create_pd_value(crit_print); |
316 | free_swap_mb += dskfree_mb; | ||
317 | used_swap_mb += dskused_mb; | ||
318 | } | ||
319 | 144 | ||
320 | /* and clean up after ourselves */ | 145 | mp_range crit_range = mp_range_init(); |
321 | for(i=0;i<nswaps;i++){ | 146 | crit_range.end_infinity = false; |
322 | free(tbl->swt_ent[i].ste_path); | 147 | crit_range.end = crit_pd; |
148 | |||
149 | pd.crit = crit_range; | ||
150 | pd.crit_present = true; | ||
323 | } | 151 | } |
324 | free(tbl); | ||
325 | # else | ||
326 | # ifdef CHECK_SWAP_SWAPCTL_BSD | ||
327 | 152 | ||
328 | /* get the number of active swap devices */ | 153 | mp_perfdata_value max = mp_create_pd_value(data.metrics.total); |
329 | nswaps=swapctl(SWAP_NSWAP, NULL, 0); | 154 | pd.max = max; |
155 | pd.max_present = true; | ||
330 | 156 | ||
331 | /* initialize swap table + entries */ | 157 | mp_perfdata_value min = mp_create_pd_value(0); |
332 | ent=(struct swapent*)malloc(sizeof(struct swapent)*nswaps); | 158 | pd.min = min; |
159 | pd.min_present = true; | ||
333 | 160 | ||
334 | /* and now, tally 'em up */ | 161 | mp_add_perfdata_to_subcheck(&sc1, pd); |
335 | swapctl_res=swapctl(SWAP_STATS, ent, nswaps); | 162 | if (verbose > 1) { |
336 | if(swapctl_res < 0){ | 163 | printf("Warn threshold value: %" PRIu64 "\n", config.warn.value); |
337 | perror(_("swapctl failed: ")); | ||
338 | die(STATE_UNKNOWN, _("Error in swapctl call\n")); | ||
339 | } | 164 | } |
340 | 165 | ||
341 | for(i=0;i<nswaps;i++){ | 166 | if (config.warn_is_set) { |
342 | dsktotal_mb = (float) ent[i].se_nblks / conv_factor; | 167 | if ((config.warn.is_percentage && (percent_used >= (100 - (double)config.warn.value))) || config.warn.value >= data.metrics.free) { |
343 | dskused_mb = (float) ent[i].se_inuse / conv_factor; | 168 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); |
344 | dskfree_mb = ( dsktotal_mb - dskused_mb ); | ||
345 | |||
346 | if(allswaps && dsktotal_mb > 0){ | ||
347 | percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); | ||
348 | result = max_state (result, check_swap(dskfree_mb, dsktotal_mb)); | ||
349 | if (verbose) { | ||
350 | xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); | ||
351 | } | ||
352 | } | 169 | } |
353 | |||
354 | total_swap_mb += dsktotal_mb; | ||
355 | free_swap_mb += dskfree_mb; | ||
356 | used_swap_mb += dskused_mb; | ||
357 | } | 170 | } |
358 | 171 | ||
359 | /* and clean up after ourselves */ | 172 | if (verbose > 1) { |
360 | free(ent); | 173 | printf("Crit threshold value: %" PRIu64 "\n", config.crit.value); |
361 | |||
362 | # endif /* CHECK_SWAP_SWAPCTL_BSD */ | ||
363 | # endif /* CHECK_SWAP_SWAPCTL_SVR4 */ | ||
364 | # endif /* HAVE_SWAP */ | ||
365 | #endif /* HAVE_PROC_MEMINFO */ | ||
366 | |||
367 | /* if total_swap_mb == 0, let's not divide by 0 */ | ||
368 | if(total_swap_mb) { | ||
369 | percent_used = 100 * ((double) used_swap_mb) / ((double) total_swap_mb); | ||
370 | } else { | ||
371 | percent_used = 100; | ||
372 | status = "- Swap is either disabled, not present, or of zero size. "; | ||
373 | } | 174 | } |
374 | 175 | ||
375 | result = max_state (result, check_swap(free_swap_mb, total_swap_mb)); | 176 | if (config.crit_is_set) { |
376 | printf (_("SWAP %s - %d%% free (%dMB out of %dMB) %s|"), | 177 | if ((config.crit.is_percentage && (percent_used >= (100 - (double)config.crit.value))) || config.crit.value >= data.metrics.free) { |
377 | state_text (result), | 178 | sc1 = mp_set_subcheck_state(sc1, STATE_CRITICAL); |
378 | (100 - percent_used), (int) free_swap_mb, (int) total_swap_mb, status); | 179 | } |
180 | } | ||
379 | 181 | ||
380 | uint64_t warn_print = warn.value; | 182 | xasprintf(&sc1.output, _("%g%% free (%lluMiB out of %lluMiB)"), (100 - percent_used), data.metrics.free >> 20, |
381 | if (warn.is_percentage) warn_print = warn.value * (total_swap_mb *1024 *1024/100); | 183 | data.metrics.total >> 20); |
382 | uint64_t crit_print = crit.value; | ||
383 | if (crit.is_percentage) crit_print = crit.value * (total_swap_mb *1024 *1024/100); | ||
384 | 184 | ||
385 | puts (perfdata_uint64 ("swap", free_swap_mb *1024 *1024, "B", | 185 | overall.summary = "Swap"; |
386 | true, warn_print, | 186 | mp_add_subcheck_to_check(&overall, sc1); |
387 | true, crit_print, | ||
388 | true, 0, | ||
389 | true, (long) total_swap_mb * 1024 * 1024)); | ||
390 | 187 | ||
391 | return result; | 188 | mp_exit(overall); |
392 | } | 189 | } |
393 | 190 | ||
191 | int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) { | ||
192 | if (total_swap_mb == 0) { | ||
193 | return config.no_swap_state; | ||
194 | } | ||
394 | 195 | ||
395 | int | 196 | uint64_t free_swap = (uint64_t)(free_swap_mb * (1024 * 1024)); /* Convert back to bytes as warn and crit specified in bytes */ |
396 | check_swap(float free_swap_mb, float total_swap_mb) | ||
397 | { | ||
398 | |||
399 | if (!total_swap_mb) return no_swap_state; | ||
400 | 197 | ||
401 | uint64_t free_swap = free_swap_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */ | 198 | if (!config.crit.is_percentage && config.crit.value >= free_swap) { |
402 | uint64_t usage_percentage = ((total_swap_mb - free_swap_mb) / total_swap_mb) * 100; | 199 | return STATE_CRITICAL; |
200 | } | ||
201 | if (!config.warn.is_percentage && config.warn.value >= free_swap) { | ||
202 | return STATE_WARNING; | ||
203 | } | ||
403 | 204 | ||
404 | if (warn.value || crit.value) { /* Thresholds defined */ | 205 | uint64_t usage_percentage = (uint64_t)((total_swap_mb - free_swap_mb) / total_swap_mb) * HUNDRED_PERCENT; |
405 | if (!crit.is_percentage && crit.value >= free_swap) return STATE_CRITICAL; | ||
406 | if (!warn.is_percentage && warn.value >= free_swap) return STATE_WARNING; | ||
407 | 206 | ||
408 | if (crit.is_percentage && | 207 | if (config.crit.is_percentage && config.crit.value != 0 && usage_percentage >= (HUNDRED_PERCENT - config.crit.value)) { |
409 | crit.value != 0 && | 208 | return STATE_CRITICAL; |
410 | usage_percentage >= (100 - crit.value)) | 209 | } |
411 | { | ||
412 | return STATE_CRITICAL; | ||
413 | } | ||
414 | 210 | ||
415 | if (warn.is_percentage && | 211 | if (config.warn.is_percentage && config.warn.value != 0 && usage_percentage >= (HUNDRED_PERCENT - config.warn.value)) { |
416 | warn.value != 0 && | 212 | return STATE_WARNING; |
417 | usage_percentage >= (100 - warn.value)) | 213 | } |
418 | { | ||
419 | return STATE_WARNING; | ||
420 | } | ||
421 | 214 | ||
422 | return STATE_OK; | 215 | return STATE_OK; |
423 | } else { /* Without thresholds */ | ||
424 | return STATE_OK; | ||
425 | } | ||
426 | } | 216 | } |
427 | 217 | ||
428 | 218 | #define output_format_index CHAR_MAX + 1 | |
429 | 219 | ||
430 | /* process command-line arguments */ | 220 | /* process command-line arguments */ |
431 | int | 221 | swap_config_wrapper process_arguments(int argc, char **argv) { |
432 | process_arguments (int argc, char **argv) | 222 | swap_config_wrapper conf_wrapper = {.errorcode = OK}; |
433 | { | 223 | conf_wrapper.config = swap_config_init(); |
434 | int c = 0; /* option character */ | 224 | |
435 | 225 | static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, | |
436 | int option = 0; | 226 | {"critical", required_argument, 0, 'c'}, |
437 | static struct option longopts[] = { | 227 | {"allswaps", no_argument, 0, 'a'}, |
438 | {"warning", required_argument, 0, 'w'}, | 228 | {"no-swap", required_argument, 0, 'n'}, |
439 | {"critical", required_argument, 0, 'c'}, | 229 | {"verbose", no_argument, 0, 'v'}, |
440 | {"allswaps", no_argument, 0, 'a'}, | 230 | {"version", no_argument, 0, 'V'}, |
441 | {"no-swap", required_argument, 0, 'n'}, | 231 | {"help", no_argument, 0, 'h'}, |
442 | {"verbose", no_argument, 0, 'v'}, | 232 | {"output-format", required_argument, 0, output_format_index}, |
443 | {"version", no_argument, 0, 'V'}, | 233 | {0, 0, 0, 0}}; |
444 | {"help", no_argument, 0, 'h'}, | 234 | |
445 | {0, 0, 0, 0} | 235 | while (true) { |
446 | }; | 236 | int option = 0; |
447 | 237 | int option_char = getopt_long(argc, argv, "+?Vvhac:w:n:", longopts, &option); | |
448 | while (1) { | 238 | |
449 | c = getopt_long (argc, argv, "+?Vvhac:w:n:", longopts, &option); | 239 | if (option_char == -1 || option_char == EOF) { |
450 | |||
451 | if (c == -1 || c == EOF) | ||
452 | break; | 240 | break; |
241 | } | ||
453 | 242 | ||
454 | switch (c) { | 243 | switch (option_char) { |
455 | case 'w': /* warning size threshold */ | 244 | case 'w': /* warning size threshold */ |
456 | { | 245 | { |
457 | /* | 246 | /* |
458 | * We expect either a positive integer value without a unit, which means | 247 | * We expect either a positive integer value without a unit, which |
459 | * the unit is Bytes or a positive integer value and a percentage sign (%), | 248 | * means the unit is Bytes or a positive integer value and a |
460 | * which means the value must be with 0 and 100 and is relative to the total swap | 249 | * percentage sign (%), which means the value must be with 0 and 100 |
461 | */ | 250 | * and is relative to the total swap |
462 | size_t length; | 251 | */ |
463 | length = strlen(optarg); | 252 | size_t length; |
464 | 253 | length = strlen(optarg); | |
465 | if (optarg[length - 1] == '%') { | 254 | conf_wrapper.config.warn_is_set = true; |
466 | /* It's percentage */ | 255 | |
467 | warn.is_percentage = true; | 256 | if (optarg[length - 1] == '%') { |
468 | optarg[length - 1] = '\0'; | 257 | /* It's percentage */ |
469 | if (is_uint64(optarg, &warn.value)) { | 258 | conf_wrapper.config.warn.is_percentage = true; |
470 | if (warn.value > 100) { | 259 | optarg[length - 1] = '\0'; |
471 | usage4 (_("Warning threshold percentage must be <= 100!")); | 260 | if (is_uint64(optarg, &conf_wrapper.config.warn.value)) { |
472 | } | 261 | if (conf_wrapper.config.warn.value > HUNDRED_PERCENT) { |
473 | } | 262 | usage4(_("Warning threshold percentage must be <= 100!")); |
474 | break; | ||
475 | } else { | ||
476 | /* It's Bytes */ | ||
477 | warn.is_percentage = false; | ||
478 | if (is_uint64(optarg, &warn.value)) { | ||
479 | break; | ||
480 | } else { | ||
481 | usage4 (_("Warning threshold be positive integer or percentage!")); | ||
482 | } | 263 | } |
483 | } | 264 | } |
265 | break; | ||
266 | } /* It's Bytes */ | ||
267 | conf_wrapper.config.warn.is_percentage = false; | ||
268 | if (is_uint64(optarg, &conf_wrapper.config.warn.value)) { | ||
269 | break; | ||
484 | } | 270 | } |
271 | usage4(_("Warning threshold be positive integer or " | ||
272 | "percentage!")); | ||
273 | } | ||
485 | case 'c': /* critical size threshold */ | 274 | case 'c': /* critical size threshold */ |
486 | { | 275 | { |
487 | /* | 276 | /* |
488 | * We expect either a positive integer value without a unit, which means | 277 | * We expect either a positive integer value without a unit, which |
489 | * the unit is Bytes or a positive integer value and a percentage sign (%), | 278 | * means the unit is Bytes or a positive integer value and a |
490 | * which means the value must be with 0 and 100 and is relative to the total swap | 279 | * percentage sign (%), which means the value must be with 0 and 100 |
491 | */ | 280 | * and is relative to the total swap |
492 | size_t length; | 281 | */ |
493 | length = strlen(optarg); | 282 | size_t length; |
494 | 283 | length = strlen(optarg); | |
495 | if (optarg[length - 1] == '%') { | 284 | conf_wrapper.config.crit_is_set = true; |
496 | /* It's percentage */ | 285 | |
497 | crit.is_percentage = true; | 286 | if (optarg[length - 1] == '%') { |
498 | optarg[length - 1] = '\0'; | 287 | /* It's percentage */ |
499 | if (is_uint64(optarg, &crit.value)) { | 288 | conf_wrapper.config.crit.is_percentage = true; |
500 | if (crit.value> 100) { | 289 | optarg[length - 1] = '\0'; |
501 | usage4 (_("Critical threshold percentage must be <= 100!")); | 290 | if (is_uint64(optarg, &conf_wrapper.config.crit.value)) { |
502 | } | 291 | if (conf_wrapper.config.crit.value > HUNDRED_PERCENT) { |
503 | } | 292 | usage4(_("Critical threshold percentage must be <= 100!")); |
504 | break; | ||
505 | } else { | ||
506 | /* It's Bytes */ | ||
507 | crit.is_percentage = false; | ||
508 | if (is_uint64(optarg, &crit.value)) { | ||
509 | break; | ||
510 | } else { | ||
511 | usage4 (_("Critical threshold be positive integer or percentage!")); | ||
512 | } | 293 | } |
513 | } | 294 | } |
514 | } | 295 | break; |
515 | case 'a': /* all swap */ | 296 | } /* It's Bytes */ |
516 | allswaps = true; | 297 | conf_wrapper.config.crit.is_percentage = false; |
298 | if (is_uint64(optarg, &conf_wrapper.config.crit.value)) { | ||
299 | break; | ||
300 | } | ||
301 | usage4(_("Critical threshold be positive integer or " | ||
302 | "percentage!")); | ||
303 | } | ||
304 | case 'a': /* all swap */ | ||
305 | conf_wrapper.config.allswaps = true; | ||
517 | break; | 306 | break; |
518 | case 'n': | 307 | case 'n': |
519 | if ((no_swap_state = mp_translate_state(optarg)) == ERROR) { | 308 | if ((conf_wrapper.config.no_swap_state = mp_translate_state(optarg)) == ERROR) { |
520 | usage4 (_("no-swap result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 309 | usage4(_("no-swap result must be a valid state name (OK, " |
310 | "WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | ||
521 | } | 311 | } |
522 | break; | 312 | break; |
523 | case 'v': /* verbose */ | 313 | case 'v': /* verbose */ |
524 | verbose++; | 314 | verbose++; |
525 | break; | 315 | break; |
526 | case 'V': /* version */ | 316 | case output_format_index: { |
527 | print_revision (progname, NP_VERSION); | 317 | parsed_output_format parser = mp_parse_output_format(optarg); |
528 | exit (STATE_UNKNOWN); | 318 | if (!parser.parsing_success) { |
529 | case 'h': /* help */ | 319 | // TODO List all available formats here, maybe add anothoer usage function |
530 | print_help (); | 320 | printf("Invalid output format: %s\n", optarg); |
531 | exit (STATE_UNKNOWN); | 321 | exit(STATE_UNKNOWN); |
532 | case '?': /* error */ | 322 | } |
533 | usage5 (); | 323 | |
324 | conf_wrapper.config.output_format_is_set = true; | ||
325 | conf_wrapper.config.output_format = parser.output_format; | ||
326 | break; | ||
327 | } | ||
328 | case 'V': /* version */ | ||
329 | print_revision(progname, NP_VERSION); | ||
330 | exit(STATE_UNKNOWN); | ||
331 | case 'h': /* help */ | ||
332 | print_help(conf_wrapper.config); | ||
333 | exit(STATE_UNKNOWN); | ||
334 | case '?': /* error */ | ||
335 | usage5(); | ||
534 | } | 336 | } |
535 | } | 337 | } |
536 | 338 | ||
537 | c = optind; | 339 | if ((conf_wrapper.config.warn.is_percentage == conf_wrapper.config.crit.is_percentage) && |
538 | if (c == argc) | 340 | (conf_wrapper.config.warn.value < conf_wrapper.config.crit.value)) { |
539 | return validate_arguments (); | 341 | /* This is NOT triggered if warn and crit are different units, e.g warn |
540 | 342 | * is percentage and crit is absolute. We cannot determine the condition | |
541 | return validate_arguments (); | 343 | * at this point since we dont know the value of total swap yet |
542 | } | ||
543 | |||
544 | |||
545 | |||
546 | int | ||
547 | validate_arguments (void) | ||
548 | { | ||
549 | if ((warn.is_percentage == crit.is_percentage) && (warn.value < crit.value)) { | ||
550 | /* This is NOT triggered if warn and crit are different units, e.g warn is percentage | ||
551 | * and crit is absolute. We cannot determine the condition at this point since we | ||
552 | * dont know the value of total swap yet | ||
553 | */ | 344 | */ |
554 | usage4(_("Warning should be more than critical")); | 345 | usage4(_("Warning should be more than critical")); |
555 | } | 346 | } |
556 | return OK; | ||
557 | } | ||
558 | |||
559 | 347 | ||
560 | 348 | return conf_wrapper; | |
561 | void | ||
562 | print_help (void) | ||
563 | { | ||
564 | print_revision (progname, NP_VERSION); | ||
565 | |||
566 | printf (_(COPYRIGHT), copyright, email); | ||
567 | |||
568 | printf ("%s\n", _("Check swap space on local machine.")); | ||
569 | |||
570 | printf ("\n\n"); | ||
571 | |||
572 | print_usage (); | ||
573 | |||
574 | printf (UT_HELP_VRSN); | ||
575 | printf (UT_EXTRA_OPTS); | ||
576 | |||
577 | printf (" %s\n", "-w, --warning=INTEGER"); | ||
578 | printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of swap space are free")); | ||
579 | printf (" %s\n", "-w, --warning=PERCENT%"); | ||
580 | printf (" %s\n", _("Exit with WARNING status if less than PERCENT of swap space is free")); | ||
581 | printf (" %s\n", "-c, --critical=INTEGER"); | ||
582 | printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of swap space are free")); | ||
583 | printf (" %s\n", "-c, --critical=PERCENT%"); | ||
584 | printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of swap space is free")); | ||
585 | printf (" %s\n", "-a, --allswaps"); | ||
586 | printf (" %s\n", _("Conduct comparisons for all swap partitions, one by one")); | ||
587 | printf (" %s\n", "-n, --no-swap=<ok|warning|critical|unknown>"); | ||
588 | printf (" %s %s\n", _("Resulting state when there is no swap regardless of thresholds. Default:"), state_text(no_swap_state)); | ||
589 | printf (UT_VERBOSE); | ||
590 | |||
591 | printf ("\n"); | ||
592 | printf ("%s\n", _("Notes:")); | ||
593 | printf (" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, they are all checked.")); | ||
594 | printf (" %s\n", _("Without thresholds, the plugin shows free swap space and performance data, but always returns OK.")); | ||
595 | printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.")); | ||
596 | |||
597 | printf (UT_SUPPORT); | ||
598 | } | 349 | } |
599 | 350 | ||
351 | void print_help(swap_config config) { | ||
352 | print_revision(progname, NP_VERSION); | ||
353 | |||
354 | printf(_(COPYRIGHT), copyright, email); | ||
355 | |||
356 | printf("%s\n", _("Check swap space on local machine.")); | ||
357 | |||
358 | printf("\n\n"); | ||
359 | |||
360 | print_usage(); | ||
361 | |||
362 | printf(UT_HELP_VRSN); | ||
363 | printf(UT_EXTRA_OPTS); | ||
364 | |||
365 | printf(" %s\n", "-w, --warning=INTEGER"); | ||
366 | printf(" %s\n", _("Exit with WARNING status if less than INTEGER bytes " | ||
367 | "of swap space are free")); | ||
368 | printf(" %s\n", "-w, --warning=PERCENT%"); | ||
369 | printf(" %s\n", _("Exit with WARNING status if less than PERCENT of " | ||
370 | "swap space is free")); | ||
371 | printf(" %s\n", "-c, --critical=INTEGER"); | ||
372 | printf(" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes " | ||
373 | "of swap space are free")); | ||
374 | printf(" %s\n", "-c, --critical=PERCENT%"); | ||
375 | printf(" %s\n", _("Exit with CRITICAL status if less than PERCENT of " | ||
376 | "swap space is free")); | ||
377 | printf(" %s\n", "-a, --allswaps"); | ||
378 | printf(" %s\n", _("Conduct comparisons for all swap partitions, one by one")); | ||
379 | printf(" %s\n", "-n, --no-swap=<ok|warning|critical|unknown>"); | ||
380 | printf(" %s %s\n", | ||
381 | _("Resulting state when there is no swap regardless of thresholds. " | ||
382 | "Default:"), | ||
383 | state_text(config.no_swap_state)); | ||
384 | printf(UT_OUTPUT_FORMAT); | ||
385 | printf(UT_VERBOSE); | ||
386 | |||
387 | printf("\n"); | ||
388 | printf("%s\n", _("Notes:")); | ||
389 | printf(" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, " | ||
390 | "they are all checked.")); | ||
391 | printf(" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.")); | ||
392 | |||
393 | printf(UT_SUPPORT); | ||
394 | } | ||
600 | 395 | ||
601 | void | 396 | void print_usage(void) { |
602 | print_usage (void) | 397 | printf("%s\n", _("Usage:")); |
603 | { | 398 | printf(" %s [-av] -w <percent_free>%% -c <percent_free>%%\n", progname); |
604 | printf ("%s\n", _("Usage:")); | 399 | printf(" -w <bytes_free> -c <bytes_free> [-n <state>]\n"); |
605 | printf (" %s [-av] [-w <percent_free>%%] [-c <percent_free>%%]\n",progname); | ||
606 | printf (" [-w <bytes_free>] [-c <bytes_free>] [-n <state>]\n"); | ||
607 | } | 400 | } |