summaryrefslogtreecommitdiffstats
path: root/plugins/check_swap.c
diff options
context:
space:
mode:
authorRincewindsHat <12514511+RincewindsHat@users.noreply.github.com>2023-12-13 17:14:06 +0100
committerRincewindsHat <12514511+RincewindsHat@users.noreply.github.com>2023-12-20 10:02:47 +0100
commit06c3393963c95573dd678cd13fa41d4b76438cc2 (patch)
tree296ae87ee5607e13b58be8e176c0c75d05441878 /plugins/check_swap.c
parent22a423ad5d2cc1d117c5fd933d2fd6901f4434c6 (diff)
downloadmonitoring-plugins-06c3393963c95573dd678cd13fa41d4b76438cc2.tar.gz
check_swap: clang-format + minor cosmetics/debugging/etc
Diffstat (limited to 'plugins/check_swap.c')
-rw-r--r--plugins/check_swap.c633
1 files changed, 330 insertions, 303 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c
index 6e995156..40ac2ed3 100644
--- a/plugins/check_swap.c
+++ b/plugins/check_swap.c
@@ -1,31 +1,31 @@
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-2007 Monitoring Plugins Development Team 7 * Copyright (c) 2000-2007 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 29
30const char *progname = "check_swap"; 30const char *progname = "check_swap";
31const char *copyright = "2000-2007"; 31const char *copyright = "2000-2007";
@@ -36,19 +36,19 @@ const char *email = "devel@monitoring-plugins.org";
36#include "utils.h" 36#include "utils.h"
37 37
38#ifdef HAVE_DECL_SWAPCTL 38#ifdef HAVE_DECL_SWAPCTL
39# ifdef HAVE_SYS_PARAM_H 39#ifdef HAVE_SYS_PARAM_H
40# include <sys/param.h> 40#include <sys/param.h>
41# endif 41#endif
42# ifdef HAVE_SYS_SWAP_H 42#ifdef HAVE_SYS_SWAP_H
43# include <sys/swap.h> 43#include <sys/swap.h>
44# endif 44#endif
45# ifdef HAVE_SYS_STAT_H 45#ifdef HAVE_SYS_STAT_H
46# include <sys/stat.h> 46#include <sys/stat.h>
47# endif 47#endif
48#endif 48#endif
49 49
50#ifndef SWAP_CONVERSION 50#ifndef SWAP_CONVERSION
51# define SWAP_CONVERSION 1 51#define SWAP_CONVERSION 1
52#endif 52#endif
53 53
54typedef struct { 54typedef struct {
@@ -57,8 +57,8 @@ typedef struct {
57} threshold; 57} threshold;
58 58
59typedef struct { 59typedef struct {
60 unsigned long long free; // Free swap in Bytes! 60 unsigned long long free; // Free swap in Bytes!
61 unsigned long long used; // Used 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! 62 unsigned long long total; // Total swap size, you guessed it, in Bytes!
63} swap_metrics; 63} swap_metrics;
64 64
@@ -81,9 +81,10 @@ typedef struct {
81 swap_config config; 81 swap_config config;
82} swap_config_wrapper; 82} swap_config_wrapper;
83 83
84swap_config_wrapper process_arguments (swap_config_wrapper config, int argc, char **argv); 84swap_config_wrapper process_arguments(swap_config_wrapper config, int argc,
85void print_usage (); 85 char **argv);
86void print_help (swap_config); 86void print_usage();
87void print_help(swap_config);
87 88
88swap_result getSwapFromProcMeminfo(swap_config config); 89swap_result getSwapFromProcMeminfo(swap_config config);
89swap_result getSwapFromSwapCommand(swap_config config); 90swap_result getSwapFromSwapCommand(swap_config config);
@@ -91,7 +92,7 @@ swap_result getSwapFromSwapctl_BSD(swap_config config);
91swap_result getSwapFromSwap_SRV4(swap_config config); 92swap_result getSwapFromSwap_SRV4(swap_config config);
92 93
93swap_config swap_config_init() { 94swap_config swap_config_init() {
94 swap_config tmp = { 0 }; 95 swap_config tmp = {0};
95 tmp.allswaps = false; 96 tmp.allswaps = false;
96 tmp.no_swap_state = STATE_CRITICAL; 97 tmp.no_swap_state = STATE_CRITICAL;
97 tmp.verbose = 0; 98 tmp.verbose = 0;
@@ -99,28 +100,25 @@ swap_config swap_config_init() {
99 return tmp; 100 return tmp;
100} 101}
101 102
102 103int main(int argc, char **argv) {
103int main (int argc, char **argv) { 104 setlocale(LC_ALL, "");
104 setlocale (LC_ALL, ""); 105 bindtextdomain(PACKAGE, LOCALEDIR);
105 bindtextdomain (PACKAGE, LOCALEDIR); 106 textdomain(PACKAGE);
106 textdomain (PACKAGE);
107 107
108 char *status; 108 char *status;
109 status = strdup (""); 109 status = strdup("");
110 110
111 /* Parse extra opts if any */ 111 /* Parse extra opts if any */
112 argv=np_extra_opts (&argc, argv, progname); 112 argv = np_extra_opts(&argc, argv, progname);
113 113
114 swap_config_wrapper tmp = { 114 swap_config_wrapper tmp = {.errorcode = OK};
115 .errorcode = OK
116 };
117 115
118 tmp.config = swap_config_init(); 116 tmp.config = swap_config_init();
119 117
120 tmp = process_arguments (tmp, argc, argv); 118 tmp = process_arguments(tmp, argc, argv);
121 119
122 if (tmp.errorcode != OK) { 120 if (tmp.errorcode != OK) {
123 usage4 (_("Could not parse arguments")); 121 usage4(_("Could not parse arguments"));
124 } 122 }
125 123
126 swap_config config = tmp.config; 124 swap_config config = tmp.config;
@@ -128,194 +126,208 @@ int main (int argc, char **argv) {
128#ifdef HAVE_PROC_MEMINFO 126#ifdef HAVE_PROC_MEMINFO
129 swap_result data = getSwapFromProcMeminfo(config); 127 swap_result data = getSwapFromProcMeminfo(config);
130#else 128#else
131# ifdef HAVE_SWAP 129#ifdef HAVE_SWAP
132 swap_result data = getSwapFromSwapCommand(); 130 swap_result data = getSwapFromSwapCommand();
133# else 131#else
134# ifdef CHECK_SWAP_SWAPCTL_SVR4 132#ifdef CHECK_SWAP_SWAPCTL_SVR4
135 swap_result data = getSwapFromSwapctl_SRV4(); 133 swap_result data = getSwapFromSwapctl_SRV4();
136# else 134#else
137# ifdef CHECK_SWAP_SWAPCTL_BSD 135#ifdef CHECK_SWAP_SWAPCTL_BSD
138 swap_result data = getSwapFromSwapctl_BSD(); 136 swap_result data = getSwapFromSwapctl_BSD();
139# else 137#else
140 #error No now found to retrieve swap 138#error No way found to retrieve swap
141# endif /* CHECK_SWAP_SWAPCTL_BSD */ 139#endif /* CHECK_SWAP_SWAPCTL_BSD */
142# endif /* CHECK_SWAP_SWAPCTL_SVR4 */ 140#endif /* CHECK_SWAP_SWAPCTL_SVR4 */
143# endif /* HAVE_SWAP */ 141#endif /* HAVE_SWAP */
144#endif /* HAVE_PROC_MEMINFO */ 142#endif /* HAVE_PROC_MEMINFO */
145 143
146 double percent_used; 144 double percent_used;
147 145
148 /* if total_swap_mb == 0, let's not divide by 0 */ 146 /* if total_swap_mb == 0, let's not divide by 0 */
149 if(data.metrics.total != 0) { 147 if (data.metrics.total != 0) {
150 percent_used = 100 * ((double) data.metrics.used) / ((double) data.metrics.total); 148 percent_used =
149 100 * ((double)data.metrics.used) / ((double)data.metrics.total);
151 } else { 150 } else {
152 printf (_("SWAP %s - Swap is either disabled, not present, or of zero size."), 151 printf(_("SWAP %s - Swap is either disabled, not present, or of zero "
153 state_text (data.statusCode)); 152 "size."),
153 state_text(data.statusCode));
154 exit(config.no_swap_state); 154 exit(config.no_swap_state);
155 } 155 }
156 156
157 if (config.verbose) {
158 printf("Computed usage percentage: %g\n", percent_used);
159 }
160
157 uint64_t warn_print = config.warn.value; 161 uint64_t warn_print = config.warn.value;
158 if (config.warn.is_percentage) { 162 if (config.warn.is_percentage) {
159 warn_print = 163 warn_print = config.warn.value * (data.metrics.total / 100);
160 config.warn.value * (data.metrics.total / 100);
161 } 164 }
162 165
163 uint64_t crit_print = config.crit.value; 166 uint64_t crit_print = config.crit.value;
164 if (config.crit.is_percentage) { 167 if (config.crit.is_percentage) {
165 crit_print = 168 crit_print = config.crit.value * (data.metrics.total / 100);
166 config.crit.value * (data.metrics.total / 100);
167 } 169 }
168 170
169 char *perfdata = perfdata_uint64( 171 char *perfdata =
170 "swap", 172 perfdata_uint64("swap", data.metrics.free, "B", true, warn_print, true,
171 data.metrics.free, 173 crit_print, true, 0, true, (long)data.metrics.total);
172 "B",
173 true, warn_print,
174 true, crit_print,
175 true, 0,
176 true, (long)data.metrics.total);
177 174
178 if (config.verbose > 1) { 175 if (config.verbose > 1) {
179 printf("Warn threshold value: %"PRIu64"\n", config.warn.value); 176 printf("Warn threshold value: %" PRIu64 "\n", config.warn.value);
180 } 177 }
181 178
182 if ((config.warn.is_percentage && (percent_used >= (100 - config.warn.value))) || 179 if ((config.warn.is_percentage &&
183 config.warn.value >= data.metrics.free) { 180 (percent_used >= (100 - config.warn.value))) ||
184 data.statusCode = max_state (data.statusCode, STATE_WARNING); 181 config.warn.value >= data.metrics.free) {
185 } 182 data.statusCode = max_state(data.statusCode, STATE_WARNING);
183 }
186 184
187 if ((config.crit.is_percentage && (percent_used >= (100 - config.crit.value))) || 185 if (config.verbose > 1) {
188 config.crit.value >= data.metrics.free) { 186 printf("Crit threshold value: %" PRIu64 "\n", config.crit.value);
189 data.statusCode = max_state (data.statusCode, STATE_CRITICAL); 187 }
190 }
191 188
192 printf (_("SWAP %s - %g%% free (%lluMB out of %lluMB) %s|%s\n"), 189 if ((config.crit.is_percentage &&
193 state_text (data.statusCode), 190 (percent_used >= (100 - config.crit.value))) ||
194 (100 - percent_used), data.metrics.free, data.metrics.total, status, 191 config.crit.value >= data.metrics.free) {
195 perfdata); 192 data.statusCode = max_state(data.statusCode, STATE_CRITICAL);
193 }
194
195 printf(_("SWAP %s - %g%% free (%lluMB out of %lluMB) %s|%s\n"),
196 state_text(data.statusCode), (100 - percent_used), data.metrics.free,
197 data.metrics.total, status, perfdata);
196 198
197 exit(data.statusCode); 199 exit(data.statusCode);
198} 200}
199 201
200/* process command-line arguments */ 202/* process command-line arguments */
201swap_config_wrapper process_arguments (swap_config_wrapper conf_wrapper, int argc, char **argv) { 203swap_config_wrapper process_arguments(swap_config_wrapper conf_wrapper,
204 int argc, char **argv) {
202 if (argc < 2) { 205 if (argc < 2) {
203 conf_wrapper.errorcode = ERROR; 206 conf_wrapper.errorcode = ERROR;
204 return conf_wrapper; 207 return conf_wrapper;
205 } 208 }
206 209
207 int option = 0; 210 int option = 0;
208 static struct option longopts[] = { 211 static struct option longopts[] = {{"warning", required_argument, 0, 'w'},
209 {"warning", required_argument, 0, 'w'}, 212 {"critical", required_argument, 0, 'c'},
210 {"critical", required_argument, 0, 'c'}, 213 {"allswaps", no_argument, 0, 'a'},
211 {"allswaps", no_argument, 0, 'a'}, 214 {"no-swap", required_argument, 0, 'n'},
212 {"no-swap", required_argument, 0, 'n'}, 215 {"verbose", no_argument, 0, 'v'},
213 {"verbose", no_argument, 0, 'v'}, 216 {"version", no_argument, 0, 'V'},
214 {"version", no_argument, 0, 'V'}, 217 {"help", no_argument, 0, 'h'},
215 {"help", no_argument, 0, 'h'}, 218 {0, 0, 0, 0}};
216 {0, 0, 0, 0} 219
217 }; 220 int c = 0; /* option character */
218
219 int c = 0; /* option character */
220 while (true) { 221 while (true) {
221 c = getopt_long (argc, argv, "+?Vvhac:w:n:", longopts, &option); 222 c = getopt_long(argc, argv, "+?Vvhac:w:n:", longopts, &option);
222 223
223 if (c == -1 || c == EOF) 224 if (c == -1 || c == EOF)
224 break; 225 break;
225 226
226 switch (c) { 227 switch (c) {
227 case 'w': /* warning size threshold */ 228 case 'w': /* warning size threshold */
228 { 229 {
229 /* 230 /*
230 * We expect either a positive integer value without a unit, which means 231 * We expect either a positive integer value without a unit, which
231 * the unit is Bytes or a positive integer value and a percentage sign (%), 232 * means the unit is Bytes or a positive integer value and a
232 * which means the value must be with 0 and 100 and is relative to the total swap 233 * percentage sign (%), which means the value must be with 0 and 100
233 */ 234 * and is relative to the total swap
234 size_t length; 235 */
235 length = strlen(optarg); 236 size_t length;
236 237 length = strlen(optarg);
237 if (optarg[length - 1] == '%') { 238
238 /* It's percentage */ 239 if (optarg[length - 1] == '%') {
239 conf_wrapper.config.warn.is_percentage = true; 240 /* It's percentage */
240 optarg[length - 1] = '\0'; 241 conf_wrapper.config.warn.is_percentage = true;
241 if (is_uint64(optarg, &conf_wrapper.config.warn.value)) { 242 optarg[length - 1] = '\0';
242 if (conf_wrapper.config.warn.value > 100) { 243 if (is_uint64(optarg, &conf_wrapper.config.warn.value)) {
243 usage4 (_("Warning threshold percentage must be <= 100!")); 244 if (conf_wrapper.config.warn.value > 100) {
244 } 245 usage4(
246 _("Warning threshold percentage must be <= 100!"));
245 } 247 }
248 }
249 break;
250 } else {
251 /* It's Bytes */
252 conf_wrapper.config.warn.is_percentage = false;
253 if (is_uint64(optarg, &conf_wrapper.config.warn.value)) {
246 break; 254 break;
247 } else { 255 } else {
248 /* It's Bytes */ 256 usage4(_("Warning threshold be positive integer or "
249 conf_wrapper.config.warn.is_percentage = false; 257 "percentage!"));
250 if (is_uint64(optarg, &conf_wrapper.config.warn.value)) {
251 break;
252 } else {
253 usage4 (_("Warning threshold be positive integer or percentage!"));
254 }
255 } 258 }
256 } 259 }
260 }
257 case 'c': /* critical size threshold */ 261 case 'c': /* critical size threshold */
258 { 262 {
259 /* 263 /*
260 * We expect either a positive integer value without a unit, which means 264 * We expect either a positive integer value without a unit, which
261 * the unit is Bytes or a positive integer value and a percentage sign (%), 265 * means the unit is Bytes or a positive integer value and a
262 * which means the value must be with 0 and 100 and is relative to the total swap 266 * percentage sign (%), which means the value must be with 0 and 100
263 */ 267 * and is relative to the total swap
264 size_t length; 268 */
265 length = strlen(optarg); 269 size_t length;
266 270 length = strlen(optarg);
267 if (optarg[length - 1] == '%') { 271
268 /* It's percentage */ 272 if (optarg[length - 1] == '%') {
269 conf_wrapper.config.crit.is_percentage = true; 273 /* It's percentage */
270 optarg[length - 1] = '\0'; 274 conf_wrapper.config.crit.is_percentage = true;
271 if (is_uint64(optarg, &conf_wrapper.config.crit.value)) { 275 optarg[length - 1] = '\0';
272 if (conf_wrapper.config.crit.value> 100) { 276 if (is_uint64(optarg, &conf_wrapper.config.crit.value)) {
273 usage4 (_("Critical threshold percentage must be <= 100!")); 277 if (conf_wrapper.config.crit.value > 100) {
274 } 278 usage4(
279 _("Critical threshold percentage must be <= 100!"));
275 } 280 }
281 }
282 break;
283 } else {
284 /* It's Bytes */
285 conf_wrapper.config.crit.is_percentage = false;
286 if (is_uint64(optarg, &conf_wrapper.config.crit.value)) {
276 break; 287 break;
277 } else { 288 } else {
278 /* It's Bytes */ 289 usage4(_("Critical threshold be positive integer or "
279 conf_wrapper.config.crit.is_percentage = false; 290 "percentage!"));
280 if (is_uint64(optarg, &conf_wrapper.config.crit.value)) {
281 break;
282 } else {
283 usage4 (_("Critical threshold be positive integer or percentage!"));
284 }
285 } 291 }
286 } 292 }
287 case 'a': /* all swap */ 293 }
294 case 'a': /* all swap */
288 conf_wrapper.config.allswaps = true; 295 conf_wrapper.config.allswaps = true;
289 break; 296 break;
290 case 'n': 297 case 'n':
291 if ((conf_wrapper.config.no_swap_state = mp_translate_state(optarg)) == ERROR) { 298 if ((conf_wrapper.config.no_swap_state =
292 usage4 (_("no-swap result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); 299 mp_translate_state(optarg)) == ERROR) {
300 usage4(_("no-swap result must be a valid state name (OK, "
301 "WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
293 } 302 }
294 break; 303 break;
295 case 'v': /* verbose */ 304 case 'v': /* verbose */
296 conf_wrapper.config.verbose++; 305 conf_wrapper.config.verbose++;
297 break; 306 break;
298 case 'V': /* version */ 307 case 'V': /* version */
299 print_revision (progname, NP_VERSION); 308 print_revision(progname, NP_VERSION);
300 exit (STATE_UNKNOWN); 309 exit(STATE_UNKNOWN);
301 case 'h': /* help */ 310 case 'h': /* help */
302 print_help (conf_wrapper.config); 311 print_help(conf_wrapper.config);
303 exit (STATE_UNKNOWN); 312 exit(STATE_UNKNOWN);
304 case '?': /* error */ 313 case '?': /* error */
305 usage5 (); 314 usage5();
306 } 315 }
307 } 316 }
308 317
309 c = optind; 318 c = optind;
310 319
311 if (conf_wrapper.config.warn.value == 0 && conf_wrapper.config.crit.value == 0) { 320 if (conf_wrapper.config.warn.value == 0 &&
321 conf_wrapper.config.crit.value == 0) {
312 conf_wrapper.errorcode = ERROR; 322 conf_wrapper.errorcode = ERROR;
313 return conf_wrapper; 323 return conf_wrapper;
314 } else if ((conf_wrapper.config.warn.is_percentage == conf_wrapper.config.crit.is_percentage) && 324 } else if ((conf_wrapper.config.warn.is_percentage ==
315 (conf_wrapper.config.warn.value < conf_wrapper.config.crit.value)) { 325 conf_wrapper.config.crit.is_percentage) &&
316 /* This is NOT triggered if warn and crit are different units, e.g warn is percentage 326 (conf_wrapper.config.warn.value >=
317 * and crit is absolute. We cannot determine the condition at this point since we 327 conf_wrapper.config.crit.value)) {
318 * dont know the value of total swap yet 328 /* 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
330 * at this point since we dont know the value of total swap yet
319 */ 331 */
320 usage4(_("Warning should be more than critical")); 332 usage4(_("Warning should be more than critical"));
321 } 333 }
@@ -323,52 +335,57 @@ swap_config_wrapper process_arguments (swap_config_wrapper conf_wrapper, int arg
323 return conf_wrapper; 335 return conf_wrapper;
324} 336}
325 337
338void print_help(swap_config config) {
339 print_revision(progname, NP_VERSION);
326 340
327void 341 printf(_(COPYRIGHT), copyright, email);
328print_help (swap_config config)
329{
330 print_revision (progname, NP_VERSION);
331 342
332 printf (_(COPYRIGHT), copyright, email); 343 printf("%s\n", _("Check swap space on local machine."));
333 344
334 printf ("%s\n", _("Check swap space on local machine.")); 345 printf("\n\n");
335
336 printf ("\n\n");
337 346
338 print_usage(); 347 print_usage();
339 348
340 printf (UT_HELP_VRSN); 349 printf(UT_HELP_VRSN);
341 printf (UT_EXTRA_OPTS); 350 printf(UT_EXTRA_OPTS);
342 351
343 printf (" %s\n", "-w, --warning=INTEGER"); 352 printf(" %s\n", "-w, --warning=INTEGER");
344 printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of swap space are free")); 353 printf(" %s\n", _("Exit with WARNING status if less than INTEGER bytes "
345 printf (" %s\n", "-w, --warning=PERCENT%"); 354 "of swap space are free"));
346 printf (" %s\n", _("Exit with WARNING status if less than PERCENT of swap space is free")); 355 printf(" %s\n", "-w, --warning=PERCENT%");
347 printf (" %s\n", "-c, --critical=INTEGER"); 356 printf(" %s\n", _("Exit with WARNING status if less than PERCENT of "
348 printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of swap space are free")); 357 "swap space is free"));
349 printf (" %s\n", "-c, --critical=PERCENT%"); 358 printf(" %s\n", "-c, --critical=INTEGER");
350 printf (" %s\n", _("Exit with CRITICAL status if less than PERCENT of swap space is free")); 359 printf(" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes "
351 printf (" %s\n", "-a, --allswaps"); 360 "of swap space are free"));
352 printf (" %s\n", _("Conduct comparisons for all swap partitions, one by one")); 361 printf(" %s\n", "-c, --critical=PERCENT%");
353 printf (" %s\n", "-n, --no-swap=<ok|warning|critical|unknown>"); 362 printf(" %s\n", _("Exit with CRITICAL status if less than PERCENT of "
354 printf (" %s %s\n", _("Resulting state when there is no swap regardless of thresholds. Default:"), state_text(config.no_swap_state)); 363 "swap space is free"));
355 printf (UT_VERBOSE); 364 printf(" %s\n", "-a, --allswaps");
356 365 printf(" %s\n",
357 printf ("\n"); 366 _("Conduct comparisons for all swap partitions, one by one"));
358 printf ("%s\n", _("Notes:")); 367 printf(" %s\n", "-n, --no-swap=<ok|warning|critical|unknown>");
359 printf (" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, they are all checked.")); 368 printf(" %s %s\n",
360 printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s.")); 369 _("Resulting state when there is no swap regardless of thresholds. "
361 370 "Default:"),
362 printf (UT_SUPPORT); 371 state_text(config.no_swap_state));
372 printf(UT_VERBOSE);
373
374 printf("\n");
375 printf("%s\n", _("Notes:"));
376 printf(" %s\n", _("Both INTEGER and PERCENT thresholds can be specified, "
377 "they are all checked."));
378 printf(
379 " %s\n",
380 _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s."));
381
382 printf(UT_SUPPORT);
363} 383}
364 384
365 385void print_usage() {
366void 386 printf("%s\n", _("Usage:"));
367print_usage () 387 printf(" %s [-av] -w <percent_free>%% -c <percent_free>%%\n", progname);
368{ 388 printf(" -w <bytes_free> -c <bytes_free> [-n <state>]\n");
369 printf ("%s\n", _("Usage:"));
370 printf (" %s [-av] -w <percent_free>%% -c <percent_free>%%\n",progname);
371 printf (" -w <bytes_free> -c <bytes_free> [-n <state>]\n");
372} 389}
373 390
374#ifdef HAVE_PROC_MEMINFO 391#ifdef HAVE_PROC_MEMINFO
@@ -379,10 +396,9 @@ swap_result getSwapFromProcMeminfo(swap_config config) {
379 } 396 }
380 397
381 FILE *fp; 398 FILE *fp;
382 fp = fopen (PROC_MEMINFO, "r"); 399 fp = fopen(PROC_MEMINFO, "r");
383
384 400
385 swap_result result = { 0 }; 401 swap_result result = {0};
386 result.statusCode = STATE_OK; 402 result.statusCode = STATE_OK;
387 403
388 uint64_t swap_total = 0, swap_used = 0, swap_free = 0; 404 uint64_t swap_total = 0, swap_used = 0, swap_free = 0;
@@ -390,13 +406,14 @@ swap_result getSwapFromProcMeminfo(swap_config config) {
390 char input_buffer[MAX_INPUT_BUFFER]; 406 char input_buffer[MAX_INPUT_BUFFER];
391 char str[32]; 407 char str[32];
392 408
393 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { 409 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
394 uint64_t tmp_KB = 0; 410 uint64_t tmp_KB = 0;
395 411
396 /* 412 /*
397 * The following sscanf call looks for a line looking like: "Swap: 123 123 123" 413 * The following sscanf call looks for a line looking like: "Swap: 123
398 * On which kind of system this format exists, I can not say, but I wanted to 414 * 123 123" On which kind of system this format exists, I can not say,
399 * document this for people who are not adapt with sscanf anymore, like me 415 * but I wanted to document this for people who are not adapt with
416 * sscanf anymore, like me
400 */ 417 */
401 if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu", 418 if (sscanf(input_buffer, "%*[S]%*[w]%*[a]%*[p]%*[:] %lu %lu %lu",
402 &swap_total, &swap_used, &swap_free) == 3) { 419 &swap_total, &swap_used, &swap_free) == 3) {
@@ -440,7 +457,7 @@ swap_result getSwapFromProcMeminfo(swap_config config) {
440 457
441#ifdef HAVE_SWAP 458#ifdef HAVE_SWAP
442swap_result getSwapFromSwapCommand() { 459swap_result getSwapFromSwapCommand() {
443 swap_result result = { 0 }; 460 swap_result result = {0};
444 461
445 char *temp_buffer; 462 char *temp_buffer;
446 char *swap_command; 463 char *swap_command;
@@ -450,128 +467,135 @@ swap_result getSwapFromSwapCommand() {
450 xasprintf(&swap_command, "%s", SWAP_COMMAND); 467 xasprintf(&swap_command, "%s", SWAP_COMMAND);
451 xasprintf(&swap_format, "%s", SWAP_FORMAT); 468 xasprintf(&swap_format, "%s", SWAP_FORMAT);
452 469
453/* These override the command used if a summary (and thus ! allswaps) is required */ 470/* These override the command used if a summary (and thus ! allswaps) is
454/* The summary flag returns more accurate information about swap usage on these OSes */ 471 * required */
455# ifdef _AIX 472/* The summary flag returns more accurate information about swap usage on these
473 * OSes */
474#ifdef _AIX
456 if (!allswaps) { 475 if (!allswaps) {
457 xasprintf(&swap_command, "%s", "/usr/sbin/lsps -s"); 476 xasprintf(&swap_command, "%s", "/usr/sbin/lsps -s");
458 xasprintf(&swap_format, "%s", "%lu%*s %lu"); 477 xasprintf(&swap_format, "%s", "%lu%*s %lu");
459 conv_factor = 1; 478 conv_factor = 1;
460 } 479 }
461# endif 480#endif
462 481
463 if (verbose >= 2) 482 if (verbose >= 2)
464 printf (_("Command: %s\n"), swap_command); 483 printf(_("Command: %s\n"), swap_command);
465 if (verbose >= 3) 484 if (verbose >= 3)
466 printf (_("Format: %s\n"), swap_format); 485 printf(_("Format: %s\n"), swap_format);
467 486
468 child_process = spopen (swap_command); 487 child_process = spopen(swap_command);
469 if (child_process == NULL) { 488 if (child_process == NULL) {
470 printf (_("Could not open pipe: %s\n"), swap_command); 489 printf(_("Could not open pipe: %s\n"), swap_command);
471 return STATE_UNKNOWN; 490 return STATE_UNKNOWN;
472 } 491 }
473 492
474 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); 493 child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r");
475 if (child_stderr == NULL) 494 if (child_stderr == NULL)
476 printf (_("Could not open stderr for %s\n"), swap_command); 495 printf(_("Could not open stderr for %s\n"), swap_command);
477 496
478 sprintf (str, "%s", ""); 497 sprintf(str, "%s", "");
479 /* read 1st line */ 498 /* read 1st line */
480 fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); 499 fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process);
481 if (strcmp (swap_format, "") == 0) { 500 if (strcmp(swap_format, "") == 0) {
482 temp_buffer = strtok (input_buffer, " \n"); 501 temp_buffer = strtok(input_buffer, " \n");
483 while (temp_buffer) { 502 while (temp_buffer) {
484 if (strstr (temp_buffer, "blocks")) 503 if (strstr(temp_buffer, "blocks"))
485 sprintf (str, "%s %s", str, "%lu"); 504 sprintf(str, "%s %s", str, "%lu");
486 else if (strstr (temp_buffer, "dskfree")) 505 else if (strstr(temp_buffer, "dskfree"))
487 sprintf (str, "%s %s", str, "%lu"); 506 sprintf(str, "%s %s", str, "%lu");
488 else 507 else
489 sprintf (str, "%s %s", str, "%*s"); 508 sprintf(str, "%s %s", str, "%*s");
490 temp_buffer = strtok (NULL, " \n"); 509 temp_buffer = strtok(NULL, " \n");
491 } 510 }
492 } 511 }
493 512
494/* If different swap command is used for summary switch, need to read format differently */ 513/* If different swap command is used for summary switch, need to read format
495# ifdef _AIX 514 * differently */
515#ifdef _AIX
496 if (!allswaps) { 516 if (!allswaps) {
497 fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process); /* Ignore first line */ 517 fgets(input_buffer, MAX_INPUT_BUFFER - 1,
498 sscanf (input_buffer, swap_format, &total_swap_mb, &used_swap_mb); 518 child_process); /* Ignore first line */
499 free_swap_mb = total_swap_mb * (100 - used_swap_mb) /100; 519 sscanf(input_buffer, swap_format, &total_swap_mb, &used_swap_mb);
520 free_swap_mb = total_swap_mb * (100 - used_swap_mb) / 100;
500 used_swap_mb = total_swap_mb - free_swap_mb; 521 used_swap_mb = total_swap_mb - free_swap_mb;
501 if (verbose >= 3) 522 if (verbose >= 3)
502 printf (_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb, used_swap_mb, free_swap_mb); 523 printf(_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb,
524 used_swap_mb, free_swap_mb);
503 } else { 525 } else {
504# endif 526#endif
505 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 527 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
506 sscanf (input_buffer, swap_format, &dsktotal_mb, &dskfree_mb); 528 sscanf(input_buffer, swap_format, &dsktotal_mb, &dskfree_mb);
507 529
508 dsktotal_mb = dsktotal_mb / conv_factor; 530 dsktotal_mb = dsktotal_mb / conv_factor;
509 /* AIX lists percent used, so this converts to dskfree in MBs */ 531 /* AIX lists percent used, so this converts to dskfree in MBs */
510# ifdef _AIX 532#ifdef _AIX
511 dskfree_mb = dsktotal_mb * (100 - dskfree_mb) / 100; 533 dskfree_mb = dsktotal_mb * (100 - dskfree_mb) / 100;
512# else 534#else
513 dskfree_mb = dskfree_mb / conv_factor; 535 dskfree_mb = dskfree_mb / conv_factor;
514# endif 536#endif
515 if (verbose >= 3) 537 if (verbose >= 3)
516 printf (_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb); 538 printf(_("total=%.0f, free=%.0f\n"), dsktotal_mb, dskfree_mb);
517 539
518 dskused_mb = dsktotal_mb - dskfree_mb; 540 dskused_mb = dsktotal_mb - dskfree_mb;
519 total_swap_mb += dsktotal_mb; 541 total_swap_mb += dsktotal_mb;
520 used_swap_mb += dskused_mb; 542 used_swap_mb += dskused_mb;
521 free_swap_mb += dskfree_mb; 543 free_swap_mb += dskfree_mb;
522 if (allswaps) { 544 if (allswaps) {
523 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); 545 percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb));
524 result = max_state (result, check_swap (dskfree_mb, dsktotal_mb)); 546 result = max_state(result, check_swap(dskfree_mb, dsktotal_mb));
525 if (verbose) 547 if (verbose)
526 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); 548 xasprintf(&status, "%s [%.0f (%d%%)]", status, dskfree_mb,
549 100 - percent);
527 } 550 }
528 } 551 }
529# ifdef _AIX 552#ifdef _AIX
530 } 553 }
531# endif 554#endif
532 555
533 /* If we get anything on STDERR, at least set warning */ 556 /* If we get anything on STDERR, at least set warning */
534 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) 557 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
535 result = max_state (result, STATE_WARNING); 558 result = max_state(result, STATE_WARNING);
536 559
537 /* close stderr */ 560 /* close stderr */
538 (void) fclose (child_stderr); 561 (void)fclose(child_stderr);
539 562
540 /* close the pipe */ 563 /* close the pipe */
541 if (spclose (child_process)) 564 if (spclose(child_process))
542 result = max_state (result, STATE_WARNING); 565 result = max_state(result, STATE_WARNING);
543} 566}
544#endif // HAVE_SWAP 567#endif // HAVE_SWAP
545 568
546#ifdef CHECK_SWAP_SWAPCTL_BSD 569#ifdef CHECK_SWAP_SWAPCTL_BSD
547swap_result getSwapFromSwapctl_BSD() { 570swap_result getSwapFromSwapctl_BSD() {
548 int i=0, nswaps=0, swapctl_res=0; 571 int i = 0, nswaps = 0, swapctl_res = 0;
549 struct swapent *ent; 572 struct swapent *ent;
550 int conv_factor = SWAP_CONVERSION; 573 int conv_factor = SWAP_CONVERSION;
551 574
552 /* get the number of active swap devices */ 575 /* get the number of active swap devices */
553 nswaps=swapctl(SWAP_NSWAP, NULL, 0); 576 nswaps = swapctl(SWAP_NSWAP, NULL, 0);
554 577
555 /* initialize swap table + entries */ 578 /* initialize swap table + entries */
556 ent=(struct swapent*)malloc(sizeof(struct swapent)*nswaps); 579 ent = (struct swapent *)malloc(sizeof(struct swapent) * nswaps);
557 580
558 /* and now, tally 'em up */ 581 /* and now, tally 'em up */
559 swapctl_res=swapctl(SWAP_STATS, ent, nswaps); 582 swapctl_res = swapctl(SWAP_STATS, ent, nswaps);
560 if(swapctl_res < 0){ 583 if (swapctl_res < 0) {
561 perror(_("swapctl failed: ")); 584 perror(_("swapctl failed: "));
562 die(STATE_UNKNOWN, _("Error in swapctl call\n")); 585 die(STATE_UNKNOWN, _("Error in swapctl call\n"));
563 } 586 }
564 587
565 for(i=0;i<nswaps;i++){ 588 for (i = 0; i < nswaps; i++) {
566 dsktotal_mb = (float) ent[i].se_nblks / conv_factor; 589 dsktotal_mb = (float)ent[i].se_nblks / conv_factor;
567 dskused_mb = (float) ent[i].se_inuse / conv_factor; 590 dskused_mb = (float)ent[i].se_inuse / conv_factor;
568 dskfree_mb = ( dsktotal_mb - dskused_mb ); 591 dskfree_mb = (dsktotal_mb - dskused_mb);
569 592
570 if(allswaps && dsktotal_mb > 0){ 593 if (allswaps && dsktotal_mb > 0) {
571 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); 594 percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb));
572 result = max_state (result, check_swap(dskfree_mb, dsktotal_mb)); 595 result = max_state(result, check_swap(dskfree_mb, dsktotal_mb));
573 if (verbose) { 596 if (verbose) {
574 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); 597 xasprintf(&status, "%s [%.0f (%d%%)]", status, dskfree_mb,
598 100 - percent);
575 } 599 }
576 } 600 }
577 601
@@ -587,52 +611,55 @@ swap_result getSwapFromSwapctl_BSD() {
587 611
588#ifdef CHECK_SWAP_SWAPCTL_SVR4 612#ifdef CHECK_SWAP_SWAPCTL_SVR4
589swap_result getSwapFromSwap_SRV4() { 613swap_result getSwapFromSwap_SRV4() {
590 int i=0, nswaps=0, swapctl_res=0; 614 int i = 0, nswaps = 0, swapctl_res = 0;
591 swaptbl_t *tbl=NULL; 615 swaptbl_t *tbl = NULL;
592 swapent_t *ent=NULL; 616 swapent_t *ent = NULL;
593 /* get the number of active swap devices */ 617 /* get the number of active swap devices */
594 if((nswaps=swapctl(SC_GETNSWP, NULL))== -1) 618 if ((nswaps = swapctl(SC_GETNSWP, NULL)) == -1)
595 die(STATE_UNKNOWN, _("Error getting swap devices\n") ); 619 die(STATE_UNKNOWN, _("Error getting swap devices\n"));
596 620
597 if(nswaps == 0) 621 if (nswaps == 0)
598 die(STATE_OK, _("SWAP OK: No swap devices defined\n")); 622 die(STATE_OK, _("SWAP OK: No swap devices defined\n"));
599 623
600 if(verbose >= 3) 624 if (verbose >= 3)
601 printf("Found %d swap device(s)\n", nswaps); 625 printf("Found %d swap device(s)\n", nswaps);
602 626
603 /* initialize swap table + entries */ 627 /* initialize swap table + entries */
604 tbl=(swaptbl_t*)malloc(sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps)); 628 tbl = (swaptbl_t *)malloc(sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps));
605 629
606 if(tbl==NULL) 630 if (tbl == NULL)
607 die(STATE_UNKNOWN, _("malloc() failed!\n")); 631 die(STATE_UNKNOWN, _("malloc() failed!\n"));
608 632
609 memset(tbl, 0, sizeof(swaptbl_t)+(sizeof(swapent_t)*nswaps)); 633 memset(tbl, 0, sizeof(swaptbl_t) + (sizeof(swapent_t) * nswaps));
610 tbl->swt_n=nswaps; 634 tbl->swt_n = nswaps;
611 for(i=0;i<nswaps;i++){ 635 for (i = 0; i < nswaps; i++) {
612 if((tbl->swt_ent[i].ste_path=(char*)malloc(sizeof(char)*MAXPATHLEN)) == NULL) 636 if ((tbl->swt_ent[i].ste_path =
637 (char *)malloc(sizeof(char) * MAXPATHLEN)) == NULL)
613 die(STATE_UNKNOWN, _("malloc() failed!\n")); 638 die(STATE_UNKNOWN, _("malloc() failed!\n"));
614 } 639 }
615 640
616 /* and now, tally 'em up */ 641 /* and now, tally 'em up */
617 swapctl_res=swapctl(SC_LIST, tbl); 642 swapctl_res = swapctl(SC_LIST, tbl);
618 if(swapctl_res < 0){ 643 if (swapctl_res < 0) {
619 perror(_("swapctl failed: ")); 644 perror(_("swapctl failed: "));
620 die(STATE_UNKNOWN, _("Error in swapctl call\n")); 645 die(STATE_UNKNOWN, _("Error in swapctl call\n"));
621 } 646 }
622 647
623 for(i=0;i<nswaps;i++){ 648 for (i = 0; i < nswaps; i++) {
624 dsktotal_mb = (float) tbl->swt_ent[i].ste_pages / SWAP_CONVERSION; 649 dsktotal_mb = (float)tbl->swt_ent[i].ste_pages / SWAP_CONVERSION;
625 dskfree_mb = (float) tbl->swt_ent[i].ste_free / SWAP_CONVERSION; 650 dskfree_mb = (float)tbl->swt_ent[i].ste_free / SWAP_CONVERSION;
626 dskused_mb = ( dsktotal_mb - dskfree_mb ); 651 dskused_mb = (dsktotal_mb - dskfree_mb);
627 652
628 if (verbose >= 3) 653 if (verbose >= 3)
629 printf ("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb, dskfree_mb, dskused_mb); 654 printf("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n",
655 dsktotal_mb, dskfree_mb, dskused_mb);
630 656
631 if(allswaps && dsktotal_mb > 0){ 657 if (allswaps && dsktotal_mb > 0) {
632 percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); 658 percent = 100 * (((double)dskused_mb) / ((double)dsktotal_mb));
633 result = max_state (result, check_swap (dskfree_mb, dsktotal_mb)); 659 result = max_state(result, check_swap(dskfree_mb, dsktotal_mb));
634 if (verbose) { 660 if (verbose) {
635 xasprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); 661 xasprintf(&status, "%s [%.0f (%d%%)]", status, dskfree_mb,
662 100 - percent);
636 } 663 }
637 } 664 }
638 665
@@ -642,7 +669,7 @@ swap_result getSwapFromSwap_SRV4() {
642 } 669 }
643 670
644 /* and clean up after ourselves */ 671 /* and clean up after ourselves */
645 for(i=0;i<nswaps;i++){ 672 for (i = 0; i < nswaps; i++) {
646 free(tbl->swt_ent[i].ste_path); 673 free(tbl->swt_ent[i].ste_path);
647 } 674 }
648 free(tbl); 675 free(tbl);