diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/check_by_ssh.c | 99 | ||||
-rw-r--r-- | plugins/check_swap.c | 131 | ||||
-rw-r--r-- | plugins/check_swap.d/check_swap.h | 4 | ||||
-rw-r--r-- | plugins/check_swap.d/swap.c | 2 | ||||
-rw-r--r-- | plugins/common.h | 9 | ||||
-rw-r--r-- | plugins/popen.c | 1 | ||||
-rw-r--r-- | plugins/runcmd.c | 1 | ||||
-rw-r--r-- | plugins/sslutils.c | 1 | ||||
-rw-r--r-- | plugins/t/check_swap.t | 65 | ||||
-rw-r--r-- | plugins/utils.c | 48 | ||||
-rw-r--r-- | plugins/utils.h | 15 |
11 files changed, 217 insertions, 159 deletions
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c index 905b2393..2ac7805d 100644 --- a/plugins/check_by_ssh.c +++ b/plugins/check_by_ssh.c | |||
@@ -82,8 +82,9 @@ int main(int argc, char **argv) { | |||
82 | argv = np_extra_opts(&argc, argv, progname); | 82 | argv = np_extra_opts(&argc, argv, progname); |
83 | 83 | ||
84 | /* process arguments */ | 84 | /* process arguments */ |
85 | if (process_arguments(argc, argv) == ERROR) | 85 | if (process_arguments(argc, argv) == ERROR) { |
86 | usage_va(_("Could not parse arguments")); | 86 | usage_va(_("Could not parse arguments")); |
87 | } | ||
87 | 88 | ||
88 | /* Set signal handling and alarm timeout */ | 89 | /* Set signal handling and alarm timeout */ |
89 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { | 90 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
@@ -94,8 +95,9 @@ int main(int argc, char **argv) { | |||
94 | /* run the command */ | 95 | /* run the command */ |
95 | if (verbose) { | 96 | if (verbose) { |
96 | printf("Command: %s\n", commargv[0]); | 97 | printf("Command: %s\n", commargv[0]); |
97 | for (int i = 1; i < commargc; i++) | 98 | for (int i = 1; i < commargc; i++) { |
98 | printf("Argument %i: %s\n", i, commargv[i]); | 99 | printf("Argument %i: %s\n", i, commargv[i]); |
100 | } | ||
99 | } | 101 | } |
100 | 102 | ||
101 | result = cmd_run_array(commargv, &chld_out, &chld_err, 0); | 103 | result = cmd_run_array(commargv, &chld_out, &chld_err, 0); |
@@ -107,33 +109,40 @@ int main(int argc, char **argv) { | |||
107 | } | 109 | } |
108 | 110 | ||
109 | if (verbose) { | 111 | if (verbose) { |
110 | for (size_t i = 0; i < chld_out.lines; i++) | 112 | for (size_t i = 0; i < chld_out.lines; i++) { |
111 | printf("stdout: %s\n", chld_out.line[i]); | 113 | printf("stdout: %s\n", chld_out.line[i]); |
112 | for (size_t i = 0; i < chld_err.lines; i++) | 114 | } |
115 | for (size_t i = 0; i < chld_err.lines; i++) { | ||
113 | printf("stderr: %s\n", chld_err.line[i]); | 116 | printf("stderr: %s\n", chld_err.line[i]); |
117 | } | ||
114 | } | 118 | } |
115 | 119 | ||
116 | if (skip_stdout == -1) /* --skip-stdout specified without argument */ | 120 | if (skip_stdout == -1) { /* --skip-stdout specified without argument */ |
117 | skip_stdout = chld_out.lines; | 121 | skip_stdout = chld_out.lines; |
118 | if (skip_stderr == -1) /* --skip-stderr specified without argument */ | 122 | } |
123 | if (skip_stderr == -1) { /* --skip-stderr specified without argument */ | ||
119 | skip_stderr = chld_err.lines; | 124 | skip_stderr = chld_err.lines; |
125 | } | ||
120 | 126 | ||
121 | /* UNKNOWN or worse if (non-skipped) output found on stderr */ | 127 | /* UNKNOWN or worse if (non-skipped) output found on stderr */ |
122 | if (chld_err.lines > (size_t)skip_stderr) { | 128 | if (chld_err.lines > (size_t)skip_stderr) { |
123 | printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]); | 129 | printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]); |
124 | if (warn_on_stderr) | 130 | if (warn_on_stderr) { |
125 | return max_state_alt(result, STATE_WARNING); | 131 | return max_state_alt(result, STATE_WARNING); |
132 | } | ||
126 | return max_state_alt(result, STATE_UNKNOWN); | 133 | return max_state_alt(result, STATE_UNKNOWN); |
127 | } | 134 | } |
128 | 135 | ||
129 | /* this is simple if we're not supposed to be passive. | 136 | /* this is simple if we're not supposed to be passive. |
130 | * Wrap up quickly and keep the tricks below */ | 137 | * Wrap up quickly and keep the tricks below */ |
131 | if (!passive) { | 138 | if (!passive) { |
132 | if (chld_out.lines > (size_t)skip_stdout) | 139 | if (chld_out.lines > (size_t)skip_stdout) { |
133 | for (size_t i = skip_stdout; i < chld_out.lines; i++) | 140 | for (size_t i = skip_stdout; i < chld_out.lines; i++) { |
134 | puts(chld_out.line[i]); | 141 | puts(chld_out.line[i]); |
135 | else | 142 | } |
143 | } else { | ||
136 | printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), remotecmd, result); | 144 | printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), remotecmd, result); |
145 | } | ||
137 | return result; /* return error status from remote command */ | 146 | return result; /* return error status from remote command */ |
138 | } | 147 | } |
139 | 148 | ||
@@ -151,8 +160,9 @@ int main(int argc, char **argv) { | |||
151 | commands = 0; | 160 | commands = 0; |
152 | for (size_t i = skip_stdout; i < chld_out.lines; i++) { | 161 | for (size_t i = skip_stdout; i < chld_out.lines; i++) { |
153 | status_text = chld_out.line[i++]; | 162 | status_text = chld_out.line[i++]; |
154 | if (i == chld_out.lines || strstr(chld_out.line[i], "STATUS CODE: ") == NULL) | 163 | if (i == chld_out.lines || strstr(chld_out.line[i], "STATUS CODE: ") == NULL) { |
155 | die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); | 164 | die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); |
165 | } | ||
156 | 166 | ||
157 | if (service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { | 167 | if (service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { |
158 | fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, host_shortname, service[commands++], | 168 | fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, host_shortname, service[commands++], |
@@ -200,18 +210,22 @@ int process_arguments(int argc, char **argv) { | |||
200 | {"configfile", optional_argument, 0, 'F'}, | 210 | {"configfile", optional_argument, 0, 'F'}, |
201 | {0, 0, 0, 0}}; | 211 | {0, 0, 0, 0}}; |
202 | 212 | ||
203 | if (argc < 2) | 213 | if (argc < 2) { |
204 | return ERROR; | 214 | return ERROR; |
215 | } | ||
205 | 216 | ||
206 | for (c = 1; c < argc; c++) | 217 | for (c = 1; c < argc; c++) { |
207 | if (strcmp("-to", argv[c]) == 0) | 218 | if (strcmp("-to", argv[c]) == 0) { |
208 | strcpy(argv[c], "-t"); | 219 | strcpy(argv[c], "-t"); |
220 | } | ||
221 | } | ||
209 | 222 | ||
210 | while (1) { | 223 | while (1) { |
211 | c = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option); | 224 | c = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option); |
212 | 225 | ||
213 | if (c == -1 || c == EOF) | 226 | if (c == -1 || c == EOF) { |
214 | break; | 227 | break; |
228 | } | ||
215 | 229 | ||
216 | switch (c) { | 230 | switch (c) { |
217 | case 'V': /* version */ | 231 | case 'V': /* version */ |
@@ -224,10 +238,11 @@ int process_arguments(int argc, char **argv) { | |||
224 | verbose = true; | 238 | verbose = true; |
225 | break; | 239 | break; |
226 | case 't': /* timeout period */ | 240 | case 't': /* timeout period */ |
227 | if (!is_integer(optarg)) | 241 | if (!is_integer(optarg)) { |
228 | usage_va(_("Timeout interval must be a positive integer")); | 242 | usage_va(_("Timeout interval must be a positive integer")); |
229 | else | 243 | } else { |
230 | timeout_interval = atoi(optarg); | 244 | timeout_interval = atoi(optarg); |
245 | } | ||
231 | break; | 246 | break; |
232 | case 'U': | 247 | case 'U': |
233 | unknown_timeout = true; | 248 | unknown_timeout = true; |
@@ -236,8 +251,9 @@ int process_arguments(int argc, char **argv) { | |||
236 | hostname = optarg; | 251 | hostname = optarg; |
237 | break; | 252 | break; |
238 | case 'p': /* port number */ | 253 | case 'p': /* port number */ |
239 | if (!is_integer(optarg)) | 254 | if (!is_integer(optarg)) { |
240 | usage_va(_("Port must be a positive integer")); | 255 | usage_va(_("Port must be a positive integer")); |
256 | } | ||
241 | comm_append("-p"); | 257 | comm_append("-p"); |
242 | comm_append(optarg); | 258 | comm_append(optarg); |
243 | break; | 259 | break; |
@@ -290,25 +306,28 @@ int process_arguments(int argc, char **argv) { | |||
290 | break; | 306 | break; |
291 | case 'C': /* Command for remote machine */ | 307 | case 'C': /* Command for remote machine */ |
292 | commands++; | 308 | commands++; |
293 | if (commands > 1) | 309 | if (commands > 1) { |
294 | xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); | 310 | xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); |
311 | } | ||
295 | xasprintf(&remotecmd, "%s%s", remotecmd, optarg); | 312 | xasprintf(&remotecmd, "%s%s", remotecmd, optarg); |
296 | break; | 313 | break; |
297 | case 'S': /* skip n (or all) lines on stdout */ | 314 | case 'S': /* skip n (or all) lines on stdout */ |
298 | if (optarg == NULL) | 315 | if (optarg == NULL) { |
299 | skip_stdout = -1; /* skip all output on stdout */ | 316 | skip_stdout = -1; /* skip all output on stdout */ |
300 | else if (!is_integer(optarg)) | 317 | } else if (!is_integer(optarg)) { |
301 | usage_va(_("skip-stdout argument must be an integer")); | 318 | usage_va(_("skip-stdout argument must be an integer")); |
302 | else | 319 | } else { |
303 | skip_stdout = atoi(optarg); | 320 | skip_stdout = atoi(optarg); |
321 | } | ||
304 | break; | 322 | break; |
305 | case 'E': /* skip n (or all) lines on stderr */ | 323 | case 'E': /* skip n (or all) lines on stderr */ |
306 | if (optarg == NULL) | 324 | if (optarg == NULL) { |
307 | skip_stderr = -1; /* skip all output on stderr */ | 325 | skip_stderr = -1; /* skip all output on stderr */ |
308 | else if (!is_integer(optarg)) | 326 | } else if (!is_integer(optarg)) { |
309 | usage_va(_("skip-stderr argument must be an integer")); | 327 | usage_va(_("skip-stderr argument must be an integer")); |
310 | else | 328 | } else { |
311 | skip_stderr = atoi(optarg); | 329 | skip_stderr = atoi(optarg); |
330 | } | ||
312 | break; | 331 | break; |
313 | case 'W': /* exit with warning if there is an output on stderr */ | 332 | case 'W': /* exit with warning if there is an output on stderr */ |
314 | warn_on_stderr = 1; | 333 | warn_on_stderr = 1; |
@@ -338,18 +357,22 @@ int process_arguments(int argc, char **argv) { | |||
338 | } | 357 | } |
339 | 358 | ||
340 | if (strlen(remotecmd) == 0) { | 359 | if (strlen(remotecmd) == 0) { |
341 | for (; c < argc; c++) | 360 | for (; c < argc; c++) { |
342 | if (strlen(remotecmd) > 0) | 361 | if (strlen(remotecmd) > 0) { |
343 | xasprintf(&remotecmd, "%s %s", remotecmd, argv[c]); | 362 | xasprintf(&remotecmd, "%s %s", remotecmd, argv[c]); |
344 | else | 363 | } else { |
345 | xasprintf(&remotecmd, "%s", argv[c]); | 364 | xasprintf(&remotecmd, "%s", argv[c]); |
365 | } | ||
366 | } | ||
346 | } | 367 | } |
347 | 368 | ||
348 | if (commands > 1 || passive) | 369 | if (commands > 1 || passive) { |
349 | xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); | 370 | xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); |
371 | } | ||
350 | 372 | ||
351 | if (remotecmd == NULL || strlen(remotecmd) <= 1) | 373 | if (remotecmd == NULL || strlen(remotecmd) <= 1) { |
352 | usage_va(_("No remotecmd")); | 374 | usage_va(_("No remotecmd")); |
375 | } | ||
353 | 376 | ||
354 | comm_append(hostname); | 377 | comm_append(hostname); |
355 | comm_append(remotecmd); | 378 | comm_append(remotecmd); |
@@ -359,25 +382,30 @@ int process_arguments(int argc, char **argv) { | |||
359 | 382 | ||
360 | void comm_append(const char *str) { | 383 | void comm_append(const char *str) { |
361 | 384 | ||
362 | if (++commargc > NP_MAXARGS) | 385 | if (++commargc > NP_MAXARGS) { |
363 | die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); | 386 | die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); |
387 | } | ||
364 | 388 | ||
365 | if ((commargv = (char **)realloc(commargv, (commargc + 1) * sizeof(char *))) == NULL) | 389 | if ((commargv = (char **)realloc(commargv, (commargc + 1) * sizeof(char *))) == NULL) { |
366 | die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); | 390 | die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); |
391 | } | ||
367 | 392 | ||
368 | commargv[commargc - 1] = strdup(str); | 393 | commargv[commargc - 1] = strdup(str); |
369 | commargv[commargc] = NULL; | 394 | commargv[commargc] = NULL; |
370 | } | 395 | } |
371 | 396 | ||
372 | int validate_arguments(void) { | 397 | int validate_arguments(void) { |
373 | if (remotecmd == NULL || hostname == NULL) | 398 | if (remotecmd == NULL || hostname == NULL) { |
374 | return ERROR; | 399 | return ERROR; |
400 | } | ||
375 | 401 | ||
376 | if (passive && commands != services) | 402 | if (passive && commands != services) { |
377 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname); | 403 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname); |
404 | } | ||
378 | 405 | ||
379 | if (passive && host_shortname == NULL) | 406 | if (passive && host_shortname == NULL) { |
380 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname); | 407 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname); |
408 | } | ||
381 | 409 | ||
382 | return OK; | 410 | return OK; |
383 | } | 411 | } |
@@ -432,7 +460,6 @@ void print_help(void) { | |||
432 | printf(" %s\n", _("Tell ssh to use this configfile [optional]")); | 460 | printf(" %s\n", _("Tell ssh to use this configfile [optional]")); |
433 | printf(" %s\n", "-q, --quiet"); | 461 | printf(" %s\n", "-q, --quiet"); |
434 | printf(" %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]")); | 462 | printf(" %s\n", _("Tell ssh to suppress warning and diagnostic messages [optional]")); |
435 | printf(UT_WARN_CRIT); | ||
436 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 463 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
437 | printf(" %s\n", "-U, --unknown-timeout"); | 464 | printf(" %s\n", "-U, --unknown-timeout"); |
438 | printf(" %s\n", _("Make connection problems return UNKNOWN instead of CRITICAL")); | 465 | printf(" %s\n", _("Make connection problems return UNKNOWN instead of CRITICAL")); |
diff --git a/plugins/check_swap.c b/plugins/check_swap.c index bc90a90b..4d3b6099 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c | |||
@@ -28,6 +28,9 @@ | |||
28 | *****************************************************************************/ | 28 | *****************************************************************************/ |
29 | 29 | ||
30 | #include "common.h" | 30 | #include "common.h" |
31 | #include "output.h" | ||
32 | #include "states.h" | ||
33 | #include <limits.h> | ||
31 | #ifdef HAVE_DECL_SWAPCTL | 34 | #ifdef HAVE_DECL_SWAPCTL |
32 | # ifdef HAVE_SYS_PARAM_H | 35 | # ifdef HAVE_SYS_PARAM_H |
33 | # include <sys/param.h> | 36 | # include <sys/param.h> |
@@ -69,8 +72,6 @@ int main(int argc, char **argv) { | |||
69 | bindtextdomain(PACKAGE, LOCALEDIR); | 72 | bindtextdomain(PACKAGE, LOCALEDIR); |
70 | textdomain(PACKAGE); | 73 | textdomain(PACKAGE); |
71 | 74 | ||
72 | char *status = strdup(""); | ||
73 | |||
74 | /* Parse extra opts if any */ | 75 | /* Parse extra opts if any */ |
75 | argv = np_extra_opts(&argc, argv, progname); | 76 | argv = np_extra_opts(&argc, argv, progname); |
76 | 77 | ||
@@ -90,59 +91,101 @@ int main(int argc, char **argv) { | |||
90 | } | 91 | } |
91 | 92 | ||
92 | double percent_used; | 93 | double percent_used; |
94 | mp_check overall = mp_check_init(); | ||
95 | if (config.output_format_is_set) { | ||
96 | overall.format = config.output_format; | ||
97 | } | ||
98 | mp_subcheck sc1 = mp_subcheck_init(); | ||
99 | sc1 = mp_set_subcheck_default_state(sc1, STATE_OK); | ||
100 | |||
93 | /* if total_swap_mb == 0, let's not divide by 0 */ | 101 | /* if total_swap_mb == 0, let's not divide by 0 */ |
94 | if (data.metrics.total != 0) { | 102 | if (data.metrics.total != 0) { |
95 | percent_used = HUNDRED_PERCENT * ((double)data.metrics.used) / ((double)data.metrics.total); | 103 | percent_used = HUNDRED_PERCENT * ((double)data.metrics.used) / ((double)data.metrics.total); |
96 | } else { | 104 | } else { |
97 | printf(_("SWAP %s - Swap is either disabled, not present, or of zero " | 105 | sc1 = mp_set_subcheck_state(sc1, config.no_swap_state); |
98 | "size."), | 106 | sc1.output = (char *)_("Swap is either disabled, not present, or of zero size."); |
99 | state_text(data.statusCode)); | 107 | |
100 | exit(config.no_swap_state); | 108 | mp_add_subcheck_to_check(&overall, sc1); |
109 | mp_exit(overall); | ||
101 | } | 110 | } |
102 | 111 | ||
103 | if (verbose) { | 112 | if (verbose) { |
104 | printf("Computed usage percentage: %g\n", percent_used); | 113 | printf("Computed usage percentage: %g\n", percent_used); |
105 | } | 114 | } |
106 | 115 | ||
107 | uint64_t warn_print = config.warn.value; | 116 | mp_perfdata pd = perfdata_init(); |
108 | if (config.warn.is_percentage) { | 117 | pd.label = "swap"; |
109 | warn_print = config.warn.value * (data.metrics.total / HUNDRED_PERCENT); | 118 | pd = mp_set_pd_value(pd, data.metrics.free); |
119 | pd.uom = "B"; | ||
120 | |||
121 | if (config.warn_is_set) { | ||
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 | } | ||
126 | |||
127 | mp_perfdata_value warn_pd = mp_create_pd_value(warn_print); | ||
128 | |||
129 | mp_range warn_range = mp_range_init(); | ||
130 | warn_range.end_infinity = false; | ||
131 | warn_range.end = warn_pd; | ||
132 | |||
133 | pd.warn = warn_range; | ||
134 | pd.warn_present = true; | ||
110 | } | 135 | } |
111 | 136 | ||
112 | uint64_t crit_print = config.crit.value; | 137 | if (config.crit_is_set) { |
113 | if (config.crit.is_percentage) { | 138 | uint64_t crit_print = config.crit.value; |
114 | crit_print = config.crit.value * (data.metrics.total / HUNDRED_PERCENT); | 139 | if (config.crit.is_percentage) { |
140 | crit_print = config.crit.value * (data.metrics.total / HUNDRED_PERCENT); | ||
141 | } | ||
142 | |||
143 | mp_perfdata_value crit_pd = mp_create_pd_value(crit_print); | ||
144 | |||
145 | mp_range crit_range = mp_range_init(); | ||
146 | crit_range.end_infinity = false; | ||
147 | crit_range.end = crit_pd; | ||
148 | |||
149 | pd.crit = crit_range; | ||
150 | pd.crit_present = true; | ||
115 | } | 151 | } |
116 | 152 | ||
117 | char *perfdata = perfdata_uint64("swap", data.metrics.free, "B", config.warn_is_set, warn_print, config.crit_is_set, crit_print, true, | 153 | mp_perfdata_value max = mp_create_pd_value(data.metrics.total); |
118 | 0, true, data.metrics.total); | 154 | pd.max = max; |
155 | pd.max_present = true; | ||
156 | |||
157 | mp_perfdata_value min = mp_create_pd_value(0); | ||
158 | pd.min = min; | ||
159 | pd.min_present = true; | ||
160 | |||
161 | mp_add_perfdata_to_subcheck(&sc1, pd); | ||
162 | if (verbose > 1) { | ||
163 | printf("Warn threshold value: %" PRIu64 "\n", config.warn.value); | ||
164 | } | ||
119 | 165 | ||
120 | if (config.warn_is_set) { | 166 | if (config.warn_is_set) { |
121 | if (verbose > 1) { | 167 | if ((config.warn.is_percentage && (percent_used >= (100 - (double)config.warn.value))) || config.warn.value >= data.metrics.free) { |
122 | printf("Warn threshold value: %" PRIu64 "\n", config.warn.value); | 168 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); |
123 | } | 169 | } |
170 | } | ||
124 | 171 | ||
125 | if ((config.warn.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.warn.value))) || | 172 | if (verbose > 1) { |
126 | config.warn.value >= data.metrics.free) { | 173 | printf("Crit threshold value: %" PRIu64 "\n", config.crit.value); |
127 | data.statusCode = max_state(data.statusCode, STATE_WARNING); | ||
128 | } | ||
129 | } | 174 | } |
130 | 175 | ||
131 | if (config.crit_is_set) { | 176 | if (config.crit_is_set) { |
132 | if (verbose > 1) { | 177 | if ((config.crit.is_percentage && (percent_used >= (100 - (double)config.crit.value))) || config.crit.value >= data.metrics.free) { |
133 | printf("Crit threshold value: %" PRIu64 "\n", config.crit.value); | 178 | sc1 = mp_set_subcheck_state(sc1, STATE_CRITICAL); |
134 | } | ||
135 | |||
136 | if ((config.crit.is_percentage && (percent_used >= (double)(HUNDRED_PERCENT - config.crit.value))) || | ||
137 | config.crit.value >= data.metrics.free) { | ||
138 | data.statusCode = max_state(data.statusCode, STATE_CRITICAL); | ||
139 | } | 179 | } |
140 | } | 180 | } |
141 | 181 | ||
142 | printf(_("SWAP %s - %g%% free (%lluMiB out of %lluMiB) %s|%s\n"), state_text(data.statusCode), (HUNDRED_PERCENT - percent_used), | 182 | xasprintf(&sc1.output, _("%g%% free (%lluMiB out of %lluMiB)"), (100 - percent_used), data.metrics.free >> 20, |
143 | BYTES_TO_MiB(data.metrics.free), BYTES_TO_MiB(data.metrics.total), status, perfdata); | 183 | data.metrics.total >> 20); |
184 | |||
185 | overall.summary = "Swap"; | ||
186 | mp_add_subcheck_to_check(&overall, sc1); | ||
144 | 187 | ||
145 | exit(data.statusCode); | 188 | mp_exit(overall); |
146 | } | 189 | } |
147 | 190 | ||
148 | int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) { | 191 | int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) { |
@@ -172,15 +215,22 @@ int check_swap(float free_swap_mb, float total_swap_mb, swap_config config) { | |||
172 | return STATE_OK; | 215 | return STATE_OK; |
173 | } | 216 | } |
174 | 217 | ||
218 | #define output_format_index CHAR_MAX + 1 | ||
219 | |||
175 | /* process command-line arguments */ | 220 | /* process command-line arguments */ |
176 | swap_config_wrapper process_arguments(int argc, char **argv) { | 221 | swap_config_wrapper process_arguments(int argc, char **argv) { |
177 | swap_config_wrapper conf_wrapper = {.errorcode = OK}; | 222 | swap_config_wrapper conf_wrapper = {.errorcode = OK}; |
178 | conf_wrapper.config = swap_config_init(); | 223 | conf_wrapper.config = swap_config_init(); |
179 | 224 | ||
180 | static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, | 225 | static struct option longopts[] = {{"warning", required_argument, 0, 'w'}, |
181 | {"allswaps", no_argument, 0, 'a'}, {"no-swap", required_argument, 0, 'n'}, | 226 | {"critical", required_argument, 0, 'c'}, |
182 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, | 227 | {"allswaps", no_argument, 0, 'a'}, |
183 | {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; | 228 | {"no-swap", required_argument, 0, 'n'}, |
229 | {"verbose", no_argument, 0, 'v'}, | ||
230 | {"version", no_argument, 0, 'V'}, | ||
231 | {"help", no_argument, 0, 'h'}, | ||
232 | {"output-format", required_argument, 0, output_format_index}, | ||
233 | {0, 0, 0, 0}}; | ||
184 | 234 | ||
185 | while (true) { | 235 | while (true) { |
186 | int option = 0; | 236 | int option = 0; |
@@ -263,6 +313,18 @@ swap_config_wrapper process_arguments(int argc, char **argv) { | |||
263 | case 'v': /* verbose */ | 313 | case 'v': /* verbose */ |
264 | verbose++; | 314 | verbose++; |
265 | break; | 315 | break; |
316 | case output_format_index: { | ||
317 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
318 | if (!parser.parsing_success) { | ||
319 | // TODO List all available formats here, maybe add anothoer usage function | ||
320 | printf("Invalid output format: %s\n", optarg); | ||
321 | exit(STATE_UNKNOWN); | ||
322 | } | ||
323 | |||
324 | conf_wrapper.config.output_format_is_set = true; | ||
325 | conf_wrapper.config.output_format = parser.output_format; | ||
326 | break; | ||
327 | } | ||
266 | case 'V': /* version */ | 328 | case 'V': /* version */ |
267 | print_revision(progname, NP_VERSION); | 329 | print_revision(progname, NP_VERSION); |
268 | exit(STATE_UNKNOWN); | 330 | exit(STATE_UNKNOWN); |
@@ -319,6 +381,7 @@ void print_help(swap_config config) { | |||
319 | _("Resulting state when there is no swap regardless of thresholds. " | 381 | _("Resulting state when there is no swap regardless of thresholds. " |
320 | "Default:"), | 382 | "Default:"), |
321 | state_text(config.no_swap_state)); | 383 | state_text(config.no_swap_state)); |
384 | printf(UT_OUTPUT_FORMAT); | ||
322 | printf(UT_VERBOSE); | 385 | printf(UT_VERBOSE); |
323 | 386 | ||
324 | printf("\n"); | 387 | printf("\n"); |
diff --git a/plugins/check_swap.d/check_swap.h b/plugins/check_swap.d/check_swap.h index 99039b21..1000fc9e 100644 --- a/plugins/check_swap.d/check_swap.h +++ b/plugins/check_swap.d/check_swap.h | |||
@@ -1,6 +1,7 @@ | |||
1 | #pragma once | 1 | #pragma once |
2 | 2 | ||
3 | #include "../common.h" | 3 | #include "../common.h" |
4 | #include "output.h" | ||
4 | 5 | ||
5 | #ifndef SWAP_CONVERSION | 6 | #ifndef SWAP_CONVERSION |
6 | # define SWAP_CONVERSION 1 | 7 | # define SWAP_CONVERSION 1 |
@@ -32,6 +33,9 @@ typedef struct { | |||
32 | check_swap_threshold crit; | 33 | check_swap_threshold crit; |
33 | bool on_aix; | 34 | bool on_aix; |
34 | int conversion_factor; | 35 | int conversion_factor; |
36 | |||
37 | bool output_format_is_set; | ||
38 | mp_output_format output_format; | ||
35 | } swap_config; | 39 | } swap_config; |
36 | 40 | ||
37 | swap_config swap_config_init(void); | 41 | swap_config swap_config_init(void); |
diff --git a/plugins/check_swap.d/swap.c b/plugins/check_swap.d/swap.c index 2fe4544f..180d5037 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 |
diff --git a/plugins/common.h b/plugins/common.h index b7a7d59b..603bae55 100644 --- a/plugins/common.h +++ b/plugins/common.h | |||
@@ -32,6 +32,7 @@ | |||
32 | #define _COMMON_H_ | 32 | #define _COMMON_H_ |
33 | 33 | ||
34 | #include "config.h" | 34 | #include "config.h" |
35 | #include "../lib/monitoringplug.h" | ||
35 | 36 | ||
36 | #ifdef HAVE_FEATURES_H | 37 | #ifdef HAVE_FEATURES_H |
37 | #include <features.h> | 38 | #include <features.h> |
@@ -179,14 +180,6 @@ enum { | |||
179 | }; | 180 | }; |
180 | 181 | ||
181 | enum { | 182 | enum { |
182 | STATE_OK, | ||
183 | STATE_WARNING, | ||
184 | STATE_CRITICAL, | ||
185 | STATE_UNKNOWN, | ||
186 | STATE_DEPENDENT | ||
187 | }; | ||
188 | |||
189 | enum { | ||
190 | DEFAULT_SOCKET_TIMEOUT = 10, /* timeout after 10 seconds */ | 183 | DEFAULT_SOCKET_TIMEOUT = 10, /* timeout after 10 seconds */ |
191 | MAX_INPUT_BUFFER = 8192, /* max size of most buffers we use */ | 184 | MAX_INPUT_BUFFER = 8192, /* max size of most buffers we use */ |
192 | MAX_HOST_ADDRESS_LENGTH = 256 /* max size of a host address */ | 185 | MAX_HOST_ADDRESS_LENGTH = 256 /* max size of a host address */ |
diff --git a/plugins/popen.c b/plugins/popen.c index 2b9824bc..cfe930b6 100644 --- a/plugins/popen.c +++ b/plugins/popen.c | |||
@@ -40,7 +40,6 @@ | |||
40 | 40 | ||
41 | #include "./common.h" | 41 | #include "./common.h" |
42 | #include "./utils.h" | 42 | #include "./utils.h" |
43 | #include "../lib/maxfd.h" | ||
44 | 43 | ||
45 | /* extern so plugin has pid to kill exec'd process on timeouts */ | 44 | /* extern so plugin has pid to kill exec'd process on timeouts */ |
46 | extern pid_t *childpid; | 45 | extern pid_t *childpid; |
diff --git a/plugins/runcmd.c b/plugins/runcmd.c index 74843149..4429ceb0 100644 --- a/plugins/runcmd.c +++ b/plugins/runcmd.c | |||
@@ -40,6 +40,7 @@ | |||
40 | 40 | ||
41 | /** includes **/ | 41 | /** includes **/ |
42 | #include "runcmd.h" | 42 | #include "runcmd.h" |
43 | #include "../lib/monitoringplug.h" | ||
43 | #ifdef HAVE_SYS_WAIT_H | 44 | #ifdef HAVE_SYS_WAIT_H |
44 | # include <sys/wait.h> | 45 | # include <sys/wait.h> |
45 | #endif | 46 | #endif |
diff --git a/plugins/sslutils.c b/plugins/sslutils.c index 3c928413..719de575 100644 --- a/plugins/sslutils.c +++ b/plugins/sslutils.c | |||
@@ -29,6 +29,7 @@ | |||
29 | #define MAX_CN_LENGTH 256 | 29 | #define MAX_CN_LENGTH 256 |
30 | #include "common.h" | 30 | #include "common.h" |
31 | #include "netutils.h" | 31 | #include "netutils.h" |
32 | #include "../lib/monitoringplug.h" | ||
32 | 33 | ||
33 | #ifdef HAVE_SSL | 34 | #ifdef HAVE_SSL |
34 | static SSL_CTX *ctx = NULL; | 35 | static SSL_CTX *ctx = NULL; |
diff --git a/plugins/t/check_swap.t b/plugins/t/check_swap.t index eaa81083..7e61b766 100644 --- a/plugins/t/check_swap.t +++ b/plugins/t/check_swap.t | |||
@@ -5,39 +5,54 @@ | |||
5 | # | 5 | # |
6 | 6 | ||
7 | use strict; | 7 | use strict; |
8 | use Test::More tests => 14; | 8 | use warnings; |
9 | use Test::More tests => 21; | ||
9 | use NPTest; | 10 | use NPTest; |
10 | 11 | use JSON; | |
11 | my $successOutput = '/^SWAP OK - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; | ||
12 | my $failureOutput = '/^SWAP CRITICAL - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; | ||
13 | my $warnOutput = '/^SWAP WARNING - [0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; | ||
14 | 12 | ||
15 | my $result; | 13 | my $result; |
14 | my $outputFormat = '--output-format mp-test-json'; | ||
15 | my $output; | ||
16 | my $message = '/^[0-9]+\% free \([0-9]+MiB out of [0-9]+MiB\)/'; | ||
16 | 17 | ||
17 | $result = NPTest->testCmd( "./check_swap" ); # Always OK | 18 | $result = NPTest->testCmd( "./check_swap $outputFormat" ); # Always OK |
18 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); | 19 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
19 | like( $result->output, $successOutput, "Right output" ); | 20 | $output = decode_json($result->output); |
21 | is($output->{'state'}, "OK", "State was correct"); | ||
22 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
20 | 23 | ||
21 | $result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576" ); # 1 MB free | 24 | $result = NPTest->testCmd( "./check_swap -w 1048576 -c 1048576 $outputFormat" ); # 1 MB free |
22 | cmp_ok( $result->return_code, "==", 0, "At least 1MB free" ); | 25 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
23 | like( $result->output, $successOutput, "Right output" ); | 26 | $output = decode_json($result->output); |
27 | is($output->{'state'}, "OK", "State was correct"); | ||
28 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
24 | 29 | ||
25 | $result = NPTest->testCmd( "./check_swap -w 1% -c 1%" ); # 1% free | 30 | $result = NPTest->testCmd( "./check_swap -w 1% -c 1% $outputFormat" ); # 1% free |
26 | cmp_ok( $result->return_code, "==", 0, 'At least 1% free' ); | 31 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
27 | like( $result->output, $successOutput, "Right output" ); | 32 | $output = decode_json($result->output); |
33 | is($output->{'state'}, "OK", "State was correct"); | ||
34 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
28 | 35 | ||
29 | $result = NPTest->testCmd( "./check_swap -w 100% -c 100%" ); # 100% (always critical) | 36 | $result = NPTest->testCmd( "./check_swap -w 100% -c 100% $outputFormat" ); # 100% (always critical) |
30 | cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); | 37 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
31 | like( $result->output, $failureOutput, "Right output" ); | 38 | $output = decode_json($result->output); |
39 | is($output->{'state'}, "CRITICAL", "State was correct"); | ||
40 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
32 | 41 | ||
33 | $result = NPTest->testCmd( "./check_swap -w 100% -c 1%" ); # 100% (always warn) | 42 | $result = NPTest->testCmd( "./check_swap -w 100% -c 1% $outputFormat" ); # 100% (always warn) |
34 | cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' ); | 43 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
35 | like( $result->output, $warnOutput, "Right output" ); | 44 | $output = decode_json($result->output); |
45 | is($output->{'state'}, "WARNING", "State was correct"); | ||
46 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
36 | 47 | ||
37 | $result = NPTest->testCmd( "./check_swap -w 100%" ); # 100% (single threshold, always warn) | 48 | $result = NPTest->testCmd( "./check_swap -w 100% $outputFormat" ); # 100% (single threshold, always warn) |
38 | cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' ); | 49 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
39 | like( $result->output, $warnOutput, "Right output" ); | 50 | $output = decode_json($result->output); |
51 | is($output->{'state'}, "WARNING", "State was correct"); | ||
52 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
40 | 53 | ||
41 | $result = NPTest->testCmd( "./check_swap -c 100%" ); # 100% (single threshold, always critical) | 54 | $result = NPTest->testCmd( "./check_swap -c 100% $outputFormat" ); # 100% (single threshold, always critical) |
42 | cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); | 55 | cmp_ok( $result->return_code, "==", 0, "Always OK" ); |
43 | like( $result->output, $failureOutput, "Right output" ); | 56 | $output = decode_json($result->output); |
57 | is($output->{'state'}, "CRITICAL", "State was correct"); | ||
58 | like($output->{'checks'}->[0]->{'output'}, $message, "Output was correct"); | ||
diff --git a/plugins/utils.c b/plugins/utils.c index 6d366e3d..09649429 100644 --- a/plugins/utils.c +++ b/plugins/utils.c | |||
@@ -42,54 +42,6 @@ extern const char *progname; | |||
42 | 42 | ||
43 | time_t start_time, end_time; | 43 | time_t start_time, end_time; |
44 | 44 | ||
45 | /* ************************************************************************** | ||
46 | * max_state(STATE_x, STATE_y) | ||
47 | * compares STATE_x to STATE_y and returns result based on the following | ||
48 | * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL | ||
49 | * | ||
50 | * Note that numerically the above does not hold | ||
51 | ****************************************************************************/ | ||
52 | |||
53 | int max_state(int a, int b) { | ||
54 | if (a == STATE_CRITICAL || b == STATE_CRITICAL) | ||
55 | return STATE_CRITICAL; | ||
56 | else if (a == STATE_WARNING || b == STATE_WARNING) | ||
57 | return STATE_WARNING; | ||
58 | else if (a == STATE_OK || b == STATE_OK) | ||
59 | return STATE_OK; | ||
60 | else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN) | ||
61 | return STATE_UNKNOWN; | ||
62 | else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT) | ||
63 | return STATE_DEPENDENT; | ||
64 | else | ||
65 | return max(a, b); | ||
66 | } | ||
67 | |||
68 | /* ************************************************************************** | ||
69 | * max_state_alt(STATE_x, STATE_y) | ||
70 | * compares STATE_x to STATE_y and returns result based on the following | ||
71 | * STATE_OK < STATE_DEPENDENT < STATE_UNKNOWN < STATE_WARNING < STATE_CRITICAL | ||
72 | * | ||
73 | * The main difference between max_state_alt and max_state it that it doesn't | ||
74 | * allow setting a default to UNKNOWN. It will instead prioritixe any valid | ||
75 | * non-OK state. | ||
76 | ****************************************************************************/ | ||
77 | |||
78 | int max_state_alt(int a, int b) { | ||
79 | if (a == STATE_CRITICAL || b == STATE_CRITICAL) | ||
80 | return STATE_CRITICAL; | ||
81 | else if (a == STATE_WARNING || b == STATE_WARNING) | ||
82 | return STATE_WARNING; | ||
83 | else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN) | ||
84 | return STATE_UNKNOWN; | ||
85 | else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT) | ||
86 | return STATE_DEPENDENT; | ||
87 | else if (a == STATE_OK || b == STATE_OK) | ||
88 | return STATE_OK; | ||
89 | else | ||
90 | return max(a, b); | ||
91 | } | ||
92 | |||
93 | void usage(const char *msg) { | 45 | void usage(const char *msg) { |
94 | printf("%s\n", msg); | 46 | printf("%s\n", msg); |
95 | print_usage(); | 47 | print_usage(); |
diff --git a/plugins/utils.h b/plugins/utils.h index f939e337..029ae5a6 100644 --- a/plugins/utils.h +++ b/plugins/utils.h | |||
@@ -13,11 +13,11 @@ in order to resist overflow attacks. In addition, a few functions are | |||
13 | provided to standardize version and error reporting across the entire | 13 | provided to standardize version and error reporting across the entire |
14 | suite of plugins. */ | 14 | suite of plugins. */ |
15 | 15 | ||
16 | /* now some functions etc are being defined in ../lib/utils_base.c */ | 16 | #include "../config.h" |
17 | #include "utils_base.h" | ||
18 | |||
19 | #include <stdbool.h> | 17 | #include <stdbool.h> |
20 | 18 | #include <stdint.h> | |
19 | #include <stdio.h> | ||
20 | #include <time.h> | ||
21 | 21 | ||
22 | #ifdef NP_EXTRA_OPTS | 22 | #ifdef NP_EXTRA_OPTS |
23 | /* Include extra-opts functions if compiled in */ | 23 | /* Include extra-opts functions if compiled in */ |
@@ -78,9 +78,6 @@ char *strpcat (char *, const char *, const char *); | |||
78 | int xvasprintf (char **strp, const char *fmt, va_list ap); | 78 | int xvasprintf (char **strp, const char *fmt, va_list ap); |
79 | int xasprintf (char **strp, const char *fmt, ...); | 79 | int xasprintf (char **strp, const char *fmt, ...); |
80 | 80 | ||
81 | int max_state (int a, int b); | ||
82 | int max_state_alt (int a, int b); | ||
83 | |||
84 | void usage (const char *) __attribute__((noreturn)); | 81 | void usage (const char *) __attribute__((noreturn)); |
85 | void usage2(const char *, const char *) __attribute__((noreturn)); | 82 | void usage2(const char *, const char *) __attribute__((noreturn)); |
86 | void usage3(const char *, int) __attribute__((noreturn)); | 83 | void usage3(const char *, int) __attribute__((noreturn)); |
@@ -198,4 +195,8 @@ The Monitoring Plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\ | |||
198 | copies of the plugins under the terms of the GNU General Public License.\n\ | 195 | copies of the plugins under the terms of the GNU General Public License.\n\ |
199 | For more information about these matters, see the file named COPYING.\n") | 196 | For more information about these matters, see the file named COPYING.\n") |
200 | 197 | ||
198 | #define UT_OUTPUT_FORMAT _("\ | ||
199 | --output-format=OUTPUT_FORMAT\n\ | ||
200 | Select output format. Valid values: \"multi-line\", \"mp-test-json\"\n") | ||
201 | |||
201 | #endif /* NP_UTILS_H */ | 202 | #endif /* NP_UTILS_H */ |