diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/Makefile.am | 13 | ||||
-rw-r--r-- | plugins/check_apt.c | 151 | ||||
-rw-r--r-- | plugins/check_apt.d/config.h | 42 | ||||
-rw-r--r-- | plugins/check_dns.c | 373 | ||||
-rw-r--r-- | plugins/check_dns.d/config.h | 34 | ||||
-rw-r--r-- | plugins/check_game.c | 229 | ||||
-rw-r--r-- | plugins/check_game.d/config.h | 30 | ||||
-rw-r--r-- | plugins/utils.c | 243 | ||||
-rw-r--r-- | plugins/utils.h | 141 |
9 files changed, 744 insertions, 512 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 28763dfc..2ffb5fd0 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
@@ -47,11 +47,14 @@ SUBDIRS = picohttpparser | |||
47 | np_test_scripts = tests/test_check_swap.t | 47 | np_test_scripts = tests/test_check_swap.t |
48 | 48 | ||
49 | EXTRA_DIST = t \ | 49 | EXTRA_DIST = t \ |
50 | tests \ | 50 | tests \ |
51 | $(np_test_scripts) \ | 51 | $(np_test_scripts) \ |
52 | check_swap.d \ | 52 | check_swap.d \ |
53 | check_dbi.d \ | 53 | check_game.d \ |
54 | check_ssh.d | 54 | check_dbi.d \ |
55 | check_ssh.d \ | ||
56 | check_dns.d \ | ||
57 | check_apt.d | ||
55 | 58 | ||
56 | PLUGINHDRS = common.h | 59 | PLUGINHDRS = common.h |
57 | 60 | ||
diff --git a/plugins/check_apt.c b/plugins/check_apt.c index 1eda45dd..e840184b 100644 --- a/plugins/check_apt.c +++ b/plugins/check_apt.c | |||
@@ -29,6 +29,7 @@ | |||
29 | * | 29 | * |
30 | *****************************************************************************/ | 30 | *****************************************************************************/ |
31 | 31 | ||
32 | #include "states.h" | ||
32 | const char *progname = "check_apt"; | 33 | const char *progname = "check_apt"; |
33 | const char *copyright = "2006-2024"; | 34 | const char *copyright = "2006-2024"; |
34 | const char *email = "devel@monitoring-plugins.org"; | 35 | const char *email = "devel@monitoring-plugins.org"; |
@@ -37,13 +38,7 @@ const char *email = "devel@monitoring-plugins.org"; | |||
37 | #include "runcmd.h" | 38 | #include "runcmd.h" |
38 | #include "utils.h" | 39 | #include "utils.h" |
39 | #include "regex.h" | 40 | #include "regex.h" |
40 | 41 | #include "check_apt.d/config.h" | |
41 | /* some constants */ | ||
42 | typedef enum { | ||
43 | UPGRADE, | ||
44 | DIST_UPGRADE, | ||
45 | NO_UPGRADE | ||
46 | } upgrade_type; | ||
47 | 42 | ||
48 | /* Character for hidden input file option (for testing). */ | 43 | /* Character for hidden input file option (for testing). */ |
49 | #define INPUT_FILE_OPT CHAR_MAX + 1 | 44 | #define INPUT_FILE_OPT CHAR_MAX + 1 |
@@ -61,14 +56,18 @@ typedef enum { | |||
61 | #define SECURITY_RE "^[^\\(]*\\(.* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)" | 56 | #define SECURITY_RE "^[^\\(]*\\(.* (Debian-Security:|Ubuntu:[^/]*/[^-]*-security)" |
62 | 57 | ||
63 | /* some standard functions */ | 58 | /* some standard functions */ |
64 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 59 | typedef struct { |
60 | int errorcode; | ||
61 | check_apt_config config; | ||
62 | } check_apt_config_wrapper; | ||
63 | static check_apt_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
65 | static void print_help(void); | 64 | static void print_help(void); |
66 | void print_usage(void); | 65 | void print_usage(void); |
67 | 66 | ||
68 | /* construct the appropriate apt-get cmdline */ | 67 | /* construct the appropriate apt-get cmdline */ |
69 | static char *construct_cmdline(upgrade_type u, const char *opts); | 68 | static char *construct_cmdline(upgrade_type /*u*/, const char * /*opts*/); |
70 | /* run an apt-get update */ | 69 | /* run an apt-get update */ |
71 | static int run_update(void); | 70 | static int run_update(char * /*update_opts*/); |
72 | 71 | ||
73 | typedef struct { | 72 | typedef struct { |
74 | int errorcode; | 73 | int errorcode; |
@@ -79,42 +78,35 @@ typedef struct { | |||
79 | } run_upgrade_result; | 78 | } run_upgrade_result; |
80 | 79 | ||
81 | /* run an apt-get upgrade */ | 80 | /* run an apt-get upgrade */ |
82 | static run_upgrade_result run_upgrade(void); | 81 | run_upgrade_result run_upgrade(upgrade_type upgrade, const char *do_include, const char *do_exclude, const char *do_critical, |
82 | const char *upgrade_opts, const char *input_filename); | ||
83 | 83 | ||
84 | /* add another clause to a regexp */ | 84 | /* add another clause to a regexp */ |
85 | static char *add_to_regexp(char *expr, const char *next); | 85 | static char *add_to_regexp(char * /*expr*/, const char * /*next*/); |
86 | /* extract package name from Inst line */ | 86 | /* extract package name from Inst line */ |
87 | static char *pkg_name(char *line); | 87 | static char *pkg_name(char * /*line*/); |
88 | /* string comparison function for qsort */ | 88 | /* string comparison function for qsort */ |
89 | static int cmpstringp(const void *p1, const void *p2); | 89 | static int cmpstringp(const void * /*p1*/, const void * /*p2*/); |
90 | 90 | ||
91 | /* configuration variables */ | 91 | /* configuration variables */ |
92 | static int verbose = 0; /* -v */ | 92 | static int verbose = 0; /* -v */ |
93 | static bool list = false; /* list packages available for upgrade */ | ||
94 | static bool do_update = false; /* whether to call apt-get update */ | ||
95 | static bool only_critical = false; /* whether to warn about non-critical updates */ | ||
96 | static upgrade_type upgrade = UPGRADE; /* which type of upgrade to do */ | ||
97 | static char *upgrade_opts = NULL; /* options to override defaults for upgrade */ | ||
98 | static char *update_opts = NULL; /* options to override defaults for update */ | ||
99 | static char *do_include = NULL; /* regexp to only include certain packages */ | ||
100 | static char *do_exclude = NULL; /* regexp to only exclude certain packages */ | ||
101 | static char *do_critical = NULL; /* regexp specifying critical packages */ | ||
102 | static char *input_filename = NULL; /* input filename for testing */ | ||
103 | /* number of packages available for upgrade to return WARNING status */ | ||
104 | static int packages_warning = 1; | ||
105 | 93 | ||
106 | /* other global variables */ | 94 | /* other global variables */ |
107 | static int stderr_warning = 0; /* if a cmd issued output on stderr */ | 95 | static bool stderr_warning = false; /* if a cmd issued output on stderr */ |
108 | static int exec_warning = 0; /* if a cmd exited non-zero */ | 96 | static bool exec_warning = false; /* if a cmd exited non-zero */ |
109 | 97 | ||
110 | int main(int argc, char **argv) { | 98 | int main(int argc, char **argv) { |
111 | /* Parse extra opts if any */ | 99 | /* Parse extra opts if any */ |
112 | argv = np_extra_opts(&argc, argv, progname); | 100 | argv = np_extra_opts(&argc, argv, progname); |
113 | 101 | ||
114 | if (process_arguments(argc, argv) == ERROR) { | 102 | check_apt_config_wrapper tmp_config = process_arguments(argc, argv); |
103 | |||
104 | if (tmp_config.errorcode == ERROR) { | ||
115 | usage_va(_("Could not parse arguments")); | 105 | usage_va(_("Could not parse arguments")); |
116 | } | 106 | } |
117 | 107 | ||
108 | const check_apt_config config = tmp_config.config; | ||
109 | |||
118 | /* Set signal handling and alarm timeout */ | 110 | /* Set signal handling and alarm timeout */ |
119 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { | 111 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
120 | usage_va(_("Cannot catch SIGALRM")); | 112 | usage_va(_("Cannot catch SIGALRM")); |
@@ -123,14 +115,15 @@ int main(int argc, char **argv) { | |||
123 | /* handle timeouts gracefully... */ | 115 | /* handle timeouts gracefully... */ |
124 | alarm(timeout_interval); | 116 | alarm(timeout_interval); |
125 | 117 | ||
126 | int result = STATE_UNKNOWN; | 118 | mp_state_enum result = STATE_UNKNOWN; |
127 | /* if they want to run apt-get update first... */ | 119 | /* if they want to run apt-get update first... */ |
128 | if (do_update) { | 120 | if (config.do_update) { |
129 | result = run_update(); | 121 | result = run_update(config.update_opts); |
130 | } | 122 | } |
131 | 123 | ||
132 | /* apt-get upgrade */ | 124 | /* apt-get upgrade */ |
133 | run_upgrade_result upgrad_res = run_upgrade(); | 125 | run_upgrade_result upgrad_res = |
126 | run_upgrade(config.upgrade, config.do_include, config.do_exclude, config.do_critical, config.upgrade_opts, config.input_filename); | ||
134 | 127 | ||
135 | result = max_state(result, upgrad_res.errorcode); | 128 | result = max_state(result, upgrad_res.errorcode); |
136 | int packages_available = upgrad_res.package_count; | 129 | int packages_available = upgrad_res.package_count; |
@@ -140,18 +133,18 @@ int main(int argc, char **argv) { | |||
140 | 133 | ||
141 | if (sec_count > 0) { | 134 | if (sec_count > 0) { |
142 | result = max_state(result, STATE_CRITICAL); | 135 | result = max_state(result, STATE_CRITICAL); |
143 | } else if (packages_available >= packages_warning && only_critical == false) { | 136 | } else if (packages_available >= config.packages_warning && !config.only_critical) { |
144 | result = max_state(result, STATE_WARNING); | 137 | result = max_state(result, STATE_WARNING); |
145 | } else if (result > STATE_UNKNOWN) { | 138 | } else if (result > STATE_UNKNOWN) { |
146 | result = STATE_UNKNOWN; | 139 | result = STATE_UNKNOWN; |
147 | } | 140 | } |
148 | 141 | ||
149 | printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s|available_upgrades=%d;;;0 critical_updates=%d;;;0\n"), | 142 | printf(_("APT %s: %d packages available for %s (%d critical updates). %s%s%s%s|available_upgrades=%d;;;0 critical_updates=%d;;;0\n"), |
150 | state_text(result), packages_available, (upgrade == DIST_UPGRADE) ? "dist-upgrade" : "upgrade", sec_count, | 143 | state_text(result), packages_available, (config.upgrade == DIST_UPGRADE) ? "dist-upgrade" : "upgrade", sec_count, |
151 | (stderr_warning) ? " warnings detected" : "", (stderr_warning && exec_warning) ? "," : "", | 144 | (stderr_warning) ? " warnings detected" : "", (stderr_warning && exec_warning) ? "," : "", |
152 | (exec_warning) ? " errors detected" : "", (stderr_warning || exec_warning) ? "." : "", packages_available, sec_count); | 145 | (exec_warning) ? " errors detected" : "", (stderr_warning || exec_warning) ? "." : "", packages_available, sec_count); |
153 | 146 | ||
154 | if (list) { | 147 | if (config.list) { |
155 | qsort(secpackages_list, sec_count, sizeof(char *), cmpstringp); | 148 | qsort(secpackages_list, sec_count, sizeof(char *), cmpstringp); |
156 | qsort(packages_list, packages_available - sec_count, sizeof(char *), cmpstringp); | 149 | qsort(packages_list, packages_available - sec_count, sizeof(char *), cmpstringp); |
157 | 150 | ||
@@ -159,7 +152,7 @@ int main(int argc, char **argv) { | |||
159 | printf("%s (security)\n", secpackages_list[i]); | 152 | printf("%s (security)\n", secpackages_list[i]); |
160 | } | 153 | } |
161 | 154 | ||
162 | if (only_critical == false) { | 155 | if (!config.only_critical) { |
163 | for (int i = 0; i < packages_available - sec_count; i++) { | 156 | for (int i = 0; i < packages_available - sec_count; i++) { |
164 | printf("%s\n", packages_list[i]); | 157 | printf("%s\n", packages_list[i]); |
165 | } | 158 | } |
@@ -170,7 +163,7 @@ int main(int argc, char **argv) { | |||
170 | } | 163 | } |
171 | 164 | ||
172 | /* process command-line arguments */ | 165 | /* process command-line arguments */ |
173 | int process_arguments(int argc, char **argv) { | 166 | check_apt_config_wrapper process_arguments(int argc, char **argv) { |
174 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, | 167 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, |
175 | {"help", no_argument, 0, 'h'}, | 168 | {"help", no_argument, 0, 'h'}, |
176 | {"verbose", no_argument, 0, 'v'}, | 169 | {"verbose", no_argument, 0, 'v'}, |
@@ -179,7 +172,7 @@ int process_arguments(int argc, char **argv) { | |||
179 | {"upgrade", optional_argument, 0, 'U'}, | 172 | {"upgrade", optional_argument, 0, 'U'}, |
180 | {"no-upgrade", no_argument, 0, 'n'}, | 173 | {"no-upgrade", no_argument, 0, 'n'}, |
181 | {"dist-upgrade", optional_argument, 0, 'd'}, | 174 | {"dist-upgrade", optional_argument, 0, 'd'}, |
182 | {"list", no_argument, false, 'l'}, | 175 | {"list", no_argument, 0, 'l'}, |
183 | {"include", required_argument, 0, 'i'}, | 176 | {"include", required_argument, 0, 'i'}, |
184 | {"exclude", required_argument, 0, 'e'}, | 177 | {"exclude", required_argument, 0, 'e'}, |
185 | {"critical", required_argument, 0, 'c'}, | 178 | {"critical", required_argument, 0, 'c'}, |
@@ -188,6 +181,11 @@ int process_arguments(int argc, char **argv) { | |||
188 | {"packages-warning", required_argument, 0, 'w'}, | 181 | {"packages-warning", required_argument, 0, 'w'}, |
189 | {0, 0, 0, 0}}; | 182 | {0, 0, 0, 0}}; |
190 | 183 | ||
184 | check_apt_config_wrapper result = { | ||
185 | .errorcode = OK, | ||
186 | .config = check_apt_config_init(), | ||
187 | }; | ||
188 | |||
191 | while (true) { | 189 | while (true) { |
192 | int option_char = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL); | 190 | int option_char = getopt_long(argc, argv, "hVvt:u::U::d::nli:e:c:ow:", longopts, NULL); |
193 | 191 | ||
@@ -209,55 +207,55 @@ int process_arguments(int argc, char **argv) { | |||
209 | timeout_interval = atoi(optarg); | 207 | timeout_interval = atoi(optarg); |
210 | break; | 208 | break; |
211 | case 'd': | 209 | case 'd': |
212 | upgrade = DIST_UPGRADE; | 210 | result.config.upgrade = DIST_UPGRADE; |
213 | if (optarg != NULL) { | 211 | if (optarg != NULL) { |
214 | upgrade_opts = strdup(optarg); | 212 | result.config.upgrade_opts = strdup(optarg); |
215 | if (upgrade_opts == NULL) { | 213 | if (result.config.upgrade_opts == NULL) { |
216 | die(STATE_UNKNOWN, "strdup failed"); | 214 | die(STATE_UNKNOWN, "strdup failed"); |
217 | } | 215 | } |
218 | } | 216 | } |
219 | break; | 217 | break; |
220 | case 'U': | 218 | case 'U': |
221 | upgrade = UPGRADE; | 219 | result.config.upgrade = UPGRADE; |
222 | if (optarg != NULL) { | 220 | if (optarg != NULL) { |
223 | upgrade_opts = strdup(optarg); | 221 | result.config.upgrade_opts = strdup(optarg); |
224 | if (upgrade_opts == NULL) { | 222 | if (result.config.upgrade_opts == NULL) { |
225 | die(STATE_UNKNOWN, "strdup failed"); | 223 | die(STATE_UNKNOWN, "strdup failed"); |
226 | } | 224 | } |
227 | } | 225 | } |
228 | break; | 226 | break; |
229 | case 'n': | 227 | case 'n': |
230 | upgrade = NO_UPGRADE; | 228 | result.config.upgrade = NO_UPGRADE; |
231 | break; | 229 | break; |
232 | case 'u': | 230 | case 'u': |
233 | do_update = true; | 231 | result.config.do_update = true; |
234 | if (optarg != NULL) { | 232 | if (optarg != NULL) { |
235 | update_opts = strdup(optarg); | 233 | result.config.update_opts = strdup(optarg); |
236 | if (update_opts == NULL) { | 234 | if (result.config.update_opts == NULL) { |
237 | die(STATE_UNKNOWN, "strdup failed"); | 235 | die(STATE_UNKNOWN, "strdup failed"); |
238 | } | 236 | } |
239 | } | 237 | } |
240 | break; | 238 | break; |
241 | case 'l': | 239 | case 'l': |
242 | list = true; | 240 | result.config.list = true; |
243 | break; | 241 | break; |
244 | case 'i': | 242 | case 'i': |
245 | do_include = add_to_regexp(do_include, optarg); | 243 | result.config.do_include = add_to_regexp(result.config.do_include, optarg); |
246 | break; | 244 | break; |
247 | case 'e': | 245 | case 'e': |
248 | do_exclude = add_to_regexp(do_exclude, optarg); | 246 | result.config.do_exclude = add_to_regexp(result.config.do_exclude, optarg); |
249 | break; | 247 | break; |
250 | case 'c': | 248 | case 'c': |
251 | do_critical = add_to_regexp(do_critical, optarg); | 249 | result.config.do_critical = add_to_regexp(result.config.do_critical, optarg); |
252 | break; | 250 | break; |
253 | case 'o': | 251 | case 'o': |
254 | only_critical = true; | 252 | result.config.only_critical = true; |
255 | break; | 253 | break; |
256 | case INPUT_FILE_OPT: | 254 | case INPUT_FILE_OPT: |
257 | input_filename = optarg; | 255 | result.config.input_filename = optarg; |
258 | break; | 256 | break; |
259 | case 'w': | 257 | case 'w': |
260 | packages_warning = atoi(optarg); | 258 | result.config.packages_warning = atoi(optarg); |
261 | break; | 259 | break; |
262 | default: | 260 | default: |
263 | /* print short usage statement if args not parsable */ | 261 | /* print short usage statement if args not parsable */ |
@@ -265,11 +263,12 @@ int process_arguments(int argc, char **argv) { | |||
265 | } | 263 | } |
266 | } | 264 | } |
267 | 265 | ||
268 | return OK; | 266 | return result; |
269 | } | 267 | } |
270 | 268 | ||
271 | /* run an apt-get upgrade */ | 269 | /* run an apt-get upgrade */ |
272 | run_upgrade_result run_upgrade(void) { | 270 | run_upgrade_result run_upgrade(const upgrade_type upgrade, const char *do_include, const char *do_exclude, const char *do_critical, |
271 | const char *upgrade_opts, const char *input_filename) { | ||
273 | regex_t ereg; | 272 | regex_t ereg; |
274 | /* initialize ereg as it is possible it is printed while uninitialized */ | 273 | /* initialize ereg as it is possible it is printed while uninitialized */ |
275 | memset(&ereg, '\0', sizeof(ereg.buffer)); | 274 | memset(&ereg, '\0', sizeof(ereg.buffer)); |
@@ -332,7 +331,7 @@ run_upgrade_result run_upgrade(void) { | |||
332 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline); | 331 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline); |
333 | } | 332 | } |
334 | 333 | ||
335 | char **pkglist = malloc(sizeof(char *) * chld_out.lines); | 334 | char **pkglist = malloc(sizeof(char *) * chld_out.lines); |
336 | if (!pkglist) { | 335 | if (!pkglist) { |
337 | die(STATE_UNKNOWN, "malloc failed!\n"); | 336 | die(STATE_UNKNOWN, "malloc failed!\n"); |
338 | } | 337 | } |
@@ -385,7 +384,7 @@ run_upgrade_result run_upgrade(void) { | |||
385 | 384 | ||
386 | /* If we get anything on stderr, at least set warning */ | 385 | /* If we get anything on stderr, at least set warning */ |
387 | if (input_filename == NULL && chld_err.buflen) { | 386 | if (input_filename == NULL && chld_err.buflen) { |
388 | stderr_warning = 1; | 387 | stderr_warning = true; |
389 | result.errorcode = max_state(result.errorcode, STATE_WARNING); | 388 | result.errorcode = max_state(result.errorcode, STATE_WARNING); |
390 | if (verbose) { | 389 | if (verbose) { |
391 | for (size_t i = 0; i < chld_err.lines; i++) { | 390 | for (size_t i = 0; i < chld_err.lines; i++) { |
@@ -405,7 +404,7 @@ run_upgrade_result run_upgrade(void) { | |||
405 | } | 404 | } |
406 | 405 | ||
407 | /* run an apt-get update (needs root) */ | 406 | /* run an apt-get update (needs root) */ |
408 | int run_update(void) { | 407 | int run_update(char *update_opts) { |
409 | int result = STATE_UNKNOWN; | 408 | int result = STATE_UNKNOWN; |
410 | char *cmdline; | 409 | char *cmdline; |
411 | /* run the update */ | 410 | /* run the update */ |
@@ -418,7 +417,7 @@ int run_update(void) { | |||
418 | * since we were explicitly asked to do so, this is treated as | 417 | * since we were explicitly asked to do so, this is treated as |
419 | * a critical error. */ | 418 | * a critical error. */ |
420 | if (result != 0) { | 419 | if (result != 0) { |
421 | exec_warning = 1; | 420 | exec_warning = true; |
422 | result = STATE_CRITICAL; | 421 | result = STATE_CRITICAL; |
423 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline); | 422 | fprintf(stderr, _("'%s' exited with non-zero status.\n"), cmdline); |
424 | } | 423 | } |
@@ -446,7 +445,7 @@ int run_update(void) { | |||
446 | char *pkg_name(char *line) { | 445 | char *pkg_name(char *line) { |
447 | char *start = line + strlen(PKGINST_PREFIX); | 446 | char *start = line + strlen(PKGINST_PREFIX); |
448 | 447 | ||
449 | int len = strlen(start); | 448 | size_t len = strlen(start); |
450 | 449 | ||
451 | char *space = index(start, ' '); | 450 | char *space = index(start, ' '); |
452 | if (space != NULL) { | 451 | if (space != NULL) { |
@@ -464,35 +463,37 @@ char *pkg_name(char *line) { | |||
464 | return pkg; | 463 | return pkg; |
465 | } | 464 | } |
466 | 465 | ||
467 | int cmpstringp(const void *p1, const void *p2) { return strcmp(*(char *const *)p1, *(char *const *)p2); } | 466 | int cmpstringp(const void *left_string, const void *right_string) { |
467 | return strcmp(*(char *const *)left_string, *(char *const *)right_string); | ||
468 | } | ||
468 | 469 | ||
469 | char *add_to_regexp(char *expr, const char *next) { | 470 | char *add_to_regexp(char *expr, const char *next) { |
470 | char *re = NULL; | 471 | char *regex_string = NULL; |
471 | 472 | ||
472 | if (expr == NULL) { | 473 | if (expr == NULL) { |
473 | re = malloc(sizeof(char) * (strlen("()") + strlen(next) + 1)); | 474 | regex_string = malloc(sizeof(char) * (strlen("()") + strlen(next) + 1)); |
474 | if (!re) { | 475 | if (!regex_string) { |
475 | die(STATE_UNKNOWN, "malloc failed!\n"); | 476 | die(STATE_UNKNOWN, "malloc failed!\n"); |
476 | } | 477 | } |
477 | sprintf(re, "(%s)", next); | 478 | sprintf(regex_string, "(%s)", next); |
478 | } else { | 479 | } else { |
479 | /* resize it, adding an extra char for the new '|' separator */ | 480 | /* resize it, adding an extra char for the new '|' separator */ |
480 | re = realloc(expr, sizeof(char) * (strlen(expr) + 1 + strlen(next) + 1)); | 481 | regex_string = realloc(expr, sizeof(char) * (strlen(expr) + 1 + strlen(next) + 1)); |
481 | if (!re) { | 482 | if (!regex_string) { |
482 | die(STATE_UNKNOWN, "realloc failed!\n"); | 483 | die(STATE_UNKNOWN, "realloc failed!\n"); |
483 | } | 484 | } |
484 | /* append it starting at ')' in the old re */ | 485 | /* append it starting at ')' in the old re */ |
485 | sprintf((char *)(re + strlen(re) - 1), "|%s)", next); | 486 | sprintf((char *)(regex_string + strlen(regex_string) - 1), "|%s)", next); |
486 | } | 487 | } |
487 | 488 | ||
488 | return re; | 489 | return regex_string; |
489 | } | 490 | } |
490 | 491 | ||
491 | char *construct_cmdline(upgrade_type u, const char *opts) { | 492 | char *construct_cmdline(upgrade_type upgrade, const char *opts) { |
492 | const char *opts_ptr = NULL; | 493 | const char *opts_ptr = NULL; |
493 | const char *aptcmd = NULL; | 494 | const char *aptcmd = NULL; |
494 | 495 | ||
495 | switch (u) { | 496 | switch (upgrade) { |
496 | case UPGRADE: | 497 | case UPGRADE: |
497 | if (opts == NULL) { | 498 | if (opts == NULL) { |
498 | opts_ptr = UPGRADE_DEFAULT_OPTS; | 499 | opts_ptr = UPGRADE_DEFAULT_OPTS; |
diff --git a/plugins/check_apt.d/config.h b/plugins/check_apt.d/config.h new file mode 100644 index 00000000..2c962e5a --- /dev/null +++ b/plugins/check_apt.d/config.h | |||
@@ -0,0 +1,42 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../config.h" | ||
4 | #include <stddef.h> | ||
5 | |||
6 | /* some constants */ | ||
7 | typedef enum { | ||
8 | UPGRADE, | ||
9 | DIST_UPGRADE, | ||
10 | NO_UPGRADE | ||
11 | } upgrade_type; | ||
12 | |||
13 | typedef struct { | ||
14 | bool do_update; /* whether to call apt-get update */ | ||
15 | upgrade_type upgrade; /* which type of upgrade to do */ | ||
16 | bool only_critical; /* whether to warn about non-critical updates */ | ||
17 | bool list; /* list packages available for upgrade */ | ||
18 | /* number of packages available for upgrade to return WARNING status */ | ||
19 | int packages_warning; | ||
20 | |||
21 | char *upgrade_opts; /* options to override defaults for upgrade */ | ||
22 | char *update_opts; /* options to override defaults for update */ | ||
23 | char *do_include; /* regexp to only include certain packages */ | ||
24 | char *do_exclude; /* regexp to only exclude certain packages */ | ||
25 | char *do_critical; /* regexp specifying critical packages */ | ||
26 | char *input_filename; /* input filename for testing */ | ||
27 | } check_apt_config; | ||
28 | |||
29 | check_apt_config check_apt_config_init() { | ||
30 | check_apt_config tmp = {.do_update = false, | ||
31 | .upgrade = UPGRADE, | ||
32 | .only_critical = false, | ||
33 | .list = false, | ||
34 | .packages_warning = 1, | ||
35 | .update_opts = NULL, | ||
36 | .update_opts = NULL, | ||
37 | .do_include = NULL, | ||
38 | .do_exclude = NULL, | ||
39 | .do_critical = NULL, | ||
40 | .input_filename = NULL}; | ||
41 | return tmp; | ||
42 | } | ||
diff --git a/plugins/check_dns.c b/plugins/check_dns.c index e1e7c00e..95f33083 100644 --- a/plugins/check_dns.c +++ b/plugins/check_dns.c | |||
@@ -39,26 +39,22 @@ const char *email = "devel@monitoring-plugins.org"; | |||
39 | #include "netutils.h" | 39 | #include "netutils.h" |
40 | #include "runcmd.h" | 40 | #include "runcmd.h" |
41 | 41 | ||
42 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 42 | #include "states.h" |
43 | static int validate_arguments(void); | 43 | #include "check_dns.d/config.h" |
44 | static int error_scan(char * /*input_buffer*/, bool *); | 44 | |
45 | typedef struct { | ||
46 | int errorcode; | ||
47 | check_dns_config config; | ||
48 | } check_dns_config_wrapper; | ||
49 | static check_dns_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
50 | static check_dns_config_wrapper validate_arguments(check_dns_config_wrapper /*config_wrapper*/); | ||
51 | static mp_state_enum error_scan(char * /*input_buffer*/, bool * /*is_nxdomain*/, const char /*dns_server*/[ADDRESS_LENGTH]); | ||
45 | static bool ip_match_cidr(const char * /*addr*/, const char * /*cidr_ro*/); | 52 | static bool ip_match_cidr(const char * /*addr*/, const char * /*cidr_ro*/); |
46 | static unsigned long ip2long(const char * /*src*/); | 53 | static unsigned long ip2long(const char * /*src*/); |
47 | static void print_help(void); | 54 | static void print_help(void); |
48 | void print_usage(void); | 55 | void print_usage(void); |
49 | 56 | ||
50 | #define ADDRESS_LENGTH 256 | ||
51 | static char query_address[ADDRESS_LENGTH] = ""; | ||
52 | static char dns_server[ADDRESS_LENGTH] = ""; | ||
53 | static char ptr_server[ADDRESS_LENGTH] = ""; | ||
54 | static bool verbose = false; | 57 | static bool verbose = false; |
55 | static char **expected_address = NULL; | ||
56 | static int expected_address_cnt = 0; | ||
57 | static bool expect_nxdomain = false; | ||
58 | |||
59 | static bool expect_authority = false; | ||
60 | static bool all_match = false; | ||
61 | static thresholds *time_thresholds = NULL; | ||
62 | 58 | ||
63 | static int qstrcmp(const void *p1, const void *p2) { | 59 | static int qstrcmp(const void *p1, const void *p2) { |
64 | /* The actual arguments to this function are "pointers to | 60 | /* The actual arguments to this function are "pointers to |
@@ -68,23 +64,6 @@ static int qstrcmp(const void *p1, const void *p2) { | |||
68 | } | 64 | } |
69 | 65 | ||
70 | int main(int argc, char **argv) { | 66 | int main(int argc, char **argv) { |
71 | char *command_line = NULL; | ||
72 | char input_buffer[MAX_INPUT_BUFFER]; | ||
73 | char *address = NULL; /* comma separated str with addrs/ptrs (sorted) */ | ||
74 | char **addresses = NULL; | ||
75 | int n_addresses = 0; | ||
76 | char *msg = NULL; | ||
77 | char *temp_buffer = NULL; | ||
78 | bool non_authoritative = false; | ||
79 | int result = STATE_UNKNOWN; | ||
80 | double elapsed_time; | ||
81 | long microsec; | ||
82 | struct timeval tv; | ||
83 | bool parse_address = false; /* This flag scans for Address: but only after Name: */ | ||
84 | output chld_out; | ||
85 | output chld_err; | ||
86 | bool is_nxdomain = false; | ||
87 | |||
88 | setlocale(LC_ALL, ""); | 67 | setlocale(LC_ALL, ""); |
89 | bindtextdomain(PACKAGE, LOCALEDIR); | 68 | bindtextdomain(PACKAGE, LOCALEDIR); |
90 | textdomain(PACKAGE); | 69 | textdomain(PACKAGE); |
@@ -97,39 +76,65 @@ int main(int argc, char **argv) { | |||
97 | /* Parse extra opts if any */ | 76 | /* Parse extra opts if any */ |
98 | argv = np_extra_opts(&argc, argv, progname); | 77 | argv = np_extra_opts(&argc, argv, progname); |
99 | 78 | ||
100 | if (process_arguments(argc, argv) == ERROR) { | 79 | check_dns_config_wrapper tmp = process_arguments(argc, argv); |
80 | |||
81 | if (tmp.errorcode == ERROR) { | ||
101 | usage_va(_("Could not parse arguments")); | 82 | usage_va(_("Could not parse arguments")); |
102 | } | 83 | } |
103 | 84 | ||
85 | const check_dns_config config = tmp.config; | ||
86 | |||
87 | char *command_line = NULL; | ||
104 | /* get the command to run */ | 88 | /* get the command to run */ |
105 | xasprintf(&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server); | 89 | xasprintf(&command_line, "%s %s %s", NSLOOKUP_COMMAND, config.query_address, config.dns_server); |
106 | 90 | ||
91 | struct timeval tv; | ||
107 | alarm(timeout_interval); | 92 | alarm(timeout_interval); |
108 | gettimeofday(&tv, NULL); | 93 | gettimeofday(&tv, NULL); |
109 | 94 | ||
110 | if (verbose) | 95 | if (verbose) { |
111 | printf("%s\n", command_line); | 96 | printf("%s\n", command_line); |
97 | } | ||
112 | 98 | ||
99 | output chld_out; | ||
100 | output chld_err; | ||
101 | char *msg = NULL; | ||
102 | mp_state_enum result = STATE_UNKNOWN; | ||
113 | /* run the command */ | 103 | /* run the command */ |
114 | if ((np_runcmd(command_line, &chld_out, &chld_err, 0)) != 0) { | 104 | if ((np_runcmd(command_line, &chld_out, &chld_err, 0)) != 0) { |
115 | msg = (char *)_("nslookup returned an error status"); | 105 | msg = (char *)_("nslookup returned an error status"); |
116 | result = STATE_WARNING; | 106 | result = STATE_WARNING; |
117 | } | 107 | } |
118 | 108 | ||
119 | /* scan stdout */ | 109 | /* ===== |
110 | * scan stdout, main results get retrieved here | ||
111 | * ===== | ||
112 | */ | ||
113 | char *address = NULL; /* comma separated str with addrs/ptrs (sorted) */ | ||
114 | char **addresses = NULL; // All addresses parsed from stdout | ||
115 | size_t n_addresses = 0; // counter for retrieved addresses | ||
116 | bool non_authoritative = false; | ||
117 | bool is_nxdomain = false; | ||
118 | bool parse_address = false; /* This flag scans for Address: but only after Name: */ | ||
120 | for (size_t i = 0; i < chld_out.lines; i++) { | 119 | for (size_t i = 0; i < chld_out.lines; i++) { |
121 | if (addresses == NULL) | 120 | if (addresses == NULL) { |
122 | addresses = malloc(sizeof(*addresses) * 10); | 121 | addresses = malloc(sizeof(*addresses) * 10); |
123 | else if (!(n_addresses % 10)) | 122 | } else if (!(n_addresses % 10)) { |
124 | addresses = realloc(addresses, sizeof(*addresses) * (n_addresses + 10)); | 123 | addresses = realloc(addresses, sizeof(*addresses) * (n_addresses + 10)); |
124 | } | ||
125 | 125 | ||
126 | if (verbose) | 126 | if (verbose) { |
127 | puts(chld_out.line[i]); | 127 | puts(chld_out.line[i]); |
128 | } | ||
128 | 129 | ||
129 | if (strcasestr(chld_out.line[i], ".in-addr.arpa") || strcasestr(chld_out.line[i], ".ip6.arpa")) { | 130 | if (strcasestr(chld_out.line[i], ".in-addr.arpa") || strcasestr(chld_out.line[i], ".ip6.arpa")) { |
130 | if ((temp_buffer = strstr(chld_out.line[i], "name = "))) | 131 | if ((strstr(chld_out.line[i], "canonical name = ") != NULL)) { |
132 | continue; | ||
133 | } | ||
134 | char *temp_buffer = NULL; | ||
135 | if ((temp_buffer = strstr(chld_out.line[i], "name = "))) { | ||
131 | addresses[n_addresses++] = strdup(temp_buffer + 7); | 136 | addresses[n_addresses++] = strdup(temp_buffer + 7); |
132 | else { | 137 | } else { |
133 | msg = (char *)_("Warning plugin error"); | 138 | msg = (char *)_("Warning plugin error"); |
134 | result = STATE_WARNING; | 139 | result = STATE_WARNING; |
135 | } | 140 | } |
@@ -137,37 +142,47 @@ int main(int argc, char **argv) { | |||
137 | 142 | ||
138 | /* bug ID: 2946553 - Older versions of bind will use all available dns | 143 | /* bug ID: 2946553 - Older versions of bind will use all available dns |
139 | servers, we have to match the one specified */ | 144 | servers, we have to match the one specified */ |
140 | if (strstr(chld_out.line[i], "Server:") && strlen(dns_server) > 0) { | 145 | if (strstr(chld_out.line[i], "Server:") && strlen(config.dns_server) > 0) { |
141 | temp_buffer = strchr(chld_out.line[i], ':'); | 146 | char *temp_buffer = strchr(chld_out.line[i], ':'); |
147 | if (temp_buffer == NULL) { | ||
148 | die(STATE_UNKNOWN, _("'%s' returned a weirdly formatted Server line\n"), NSLOOKUP_COMMAND); | ||
149 | } | ||
150 | |||
142 | temp_buffer++; | 151 | temp_buffer++; |
143 | 152 | ||
144 | /* Strip leading tabs */ | 153 | /* Strip leading tabs */ |
145 | for (; *temp_buffer != '\0' && *temp_buffer == '\t'; temp_buffer++) | 154 | for (; *temp_buffer != '\0' && *temp_buffer == '\t'; temp_buffer++) { |
146 | /* NOOP */; | 155 | /* NOOP */; |
156 | } | ||
147 | 157 | ||
148 | strip(temp_buffer); | 158 | strip(temp_buffer); |
149 | if (temp_buffer == NULL || strlen(temp_buffer) == 0) { | 159 | if (strlen(temp_buffer) == 0) { |
150 | die(STATE_CRITICAL, _("DNS CRITICAL - '%s' returned empty server string\n"), NSLOOKUP_COMMAND); | 160 | die(STATE_CRITICAL, _("DNS CRITICAL - '%s' returned empty server string\n"), NSLOOKUP_COMMAND); |
151 | } | 161 | } |
152 | 162 | ||
153 | if (strcmp(temp_buffer, dns_server) != 0) { | 163 | if (strcmp(temp_buffer, config.dns_server) != 0) { |
154 | die(STATE_CRITICAL, _("DNS CRITICAL - No response from DNS %s\n"), dns_server); | 164 | die(STATE_CRITICAL, _("DNS CRITICAL - No response from DNS %s\n"), config.dns_server); |
155 | } | 165 | } |
156 | } | 166 | } |
157 | 167 | ||
158 | /* the server is responding, we just got the host name... */ | 168 | /* the server is responding, we just got the host name... */ |
159 | if (strstr(chld_out.line[i], "Name:")) | 169 | if (strstr(chld_out.line[i], "Name:")) { |
160 | parse_address = true; | 170 | parse_address = true; |
161 | else if (parse_address && (strstr(chld_out.line[i], "Address:") || strstr(chld_out.line[i], "Addresses:"))) { | 171 | } else if (parse_address && (strstr(chld_out.line[i], "Address:") || strstr(chld_out.line[i], "Addresses:"))) { |
162 | temp_buffer = index(chld_out.line[i], ':'); | 172 | char *temp_buffer = strchr(chld_out.line[i], ':'); |
173 | if (temp_buffer == NULL) { | ||
174 | die(STATE_UNKNOWN, _("'%s' returned a weirdly formatted Address line\n"), NSLOOKUP_COMMAND); | ||
175 | } | ||
176 | |||
163 | temp_buffer++; | 177 | temp_buffer++; |
164 | 178 | ||
165 | /* Strip leading spaces */ | 179 | /* Strip leading spaces */ |
166 | while (*temp_buffer == ' ') | 180 | while (*temp_buffer == ' ') { |
167 | temp_buffer++; | 181 | temp_buffer++; |
182 | } | ||
168 | 183 | ||
169 | strip(temp_buffer); | 184 | strip(temp_buffer); |
170 | if (temp_buffer == NULL || strlen(temp_buffer) == 0) { | 185 | if (strlen(temp_buffer) == 0) { |
171 | die(STATE_CRITICAL, _("DNS CRITICAL - '%s' returned empty host name string\n"), NSLOOKUP_COMMAND); | 186 | die(STATE_CRITICAL, _("DNS CRITICAL - '%s' returned empty host name string\n"), NSLOOKUP_COMMAND); |
172 | } | 187 | } |
173 | 188 | ||
@@ -176,65 +191,71 @@ int main(int argc, char **argv) { | |||
176 | non_authoritative = true; | 191 | non_authoritative = true; |
177 | } | 192 | } |
178 | 193 | ||
179 | result = error_scan(chld_out.line[i], &is_nxdomain); | 194 | result = error_scan(chld_out.line[i], &is_nxdomain, config.dns_server); |
180 | if (result != STATE_OK) { | 195 | if (result != STATE_OK) { |
181 | msg = strchr(chld_out.line[i], ':'); | 196 | msg = strchr(chld_out.line[i], ':'); |
182 | if (msg) | 197 | if (msg) { |
183 | msg++; | 198 | msg++; |
199 | } | ||
184 | break; | 200 | break; |
185 | } | 201 | } |
186 | } | 202 | } |
187 | 203 | ||
204 | char input_buffer[MAX_INPUT_BUFFER]; | ||
188 | /* scan stderr */ | 205 | /* scan stderr */ |
189 | for (size_t i = 0; i < chld_err.lines; i++) { | 206 | for (size_t i = 0; i < chld_err.lines; i++) { |
190 | if (verbose) | 207 | if (verbose) { |
191 | puts(chld_err.line[i]); | 208 | puts(chld_err.line[i]); |
209 | } | ||
192 | 210 | ||
193 | if (error_scan(chld_err.line[i], &is_nxdomain) != STATE_OK) { | 211 | if (error_scan(chld_err.line[i], &is_nxdomain, config.dns_server) != STATE_OK) { |
194 | result = max_state(result, error_scan(chld_err.line[i], &is_nxdomain)); | 212 | result = max_state(result, error_scan(chld_err.line[i], &is_nxdomain, config.dns_server)); |
195 | msg = strchr(input_buffer, ':'); | 213 | msg = strchr(input_buffer, ':'); |
196 | if (msg) | 214 | if (msg) { |
197 | msg++; | 215 | msg++; |
198 | else | 216 | } else { |
199 | msg = input_buffer; | 217 | msg = input_buffer; |
218 | } | ||
200 | } | 219 | } |
201 | } | 220 | } |
202 | 221 | ||
203 | if (is_nxdomain && !expect_nxdomain) { | 222 | if (is_nxdomain && !config.expect_nxdomain) { |
204 | die(STATE_CRITICAL, _("Domain '%s' was not found by the server\n"), query_address); | 223 | die(STATE_CRITICAL, _("Domain '%s' was not found by the server\n"), config.query_address); |
205 | } | 224 | } |
206 | 225 | ||
207 | if (addresses) { | 226 | if (addresses) { |
208 | int i; | 227 | size_t slen = 1; |
209 | int slen; | 228 | char *adrp = NULL; |
210 | char *adrp; | ||
211 | qsort(addresses, n_addresses, sizeof(*addresses), qstrcmp); | 229 | qsort(addresses, n_addresses, sizeof(*addresses), qstrcmp); |
212 | for (i = 0, slen = 1; i < n_addresses; i++) { | 230 | for (size_t i = 0; i < n_addresses; i++) { |
213 | slen += strlen(addresses[i]) + 1; | 231 | slen += strlen(addresses[i]) + 1; |
214 | } | 232 | } |
233 | |||
234 | // Temporary pointer adrp gets moved, address stays on the beginning | ||
215 | adrp = address = malloc(slen); | 235 | adrp = address = malloc(slen); |
216 | for (i = 0; i < n_addresses; i++) { | 236 | for (size_t i = 0; i < n_addresses; i++) { |
217 | if (i) | 237 | if (i) { |
218 | *adrp++ = ','; | 238 | *adrp++ = ','; |
239 | } | ||
219 | strcpy(adrp, addresses[i]); | 240 | strcpy(adrp, addresses[i]); |
220 | adrp += strlen(addresses[i]); | 241 | adrp += strlen(addresses[i]); |
221 | } | 242 | } |
222 | *adrp = 0; | 243 | *adrp = 0; |
223 | } else | 244 | } else { |
224 | die(STATE_CRITICAL, _("DNS CRITICAL - '%s' msg parsing exited with no address\n"), NSLOOKUP_COMMAND); | 245 | die(STATE_CRITICAL, _("DNS CRITICAL - '%s' msg parsing exited with no address\n"), NSLOOKUP_COMMAND); |
246 | } | ||
225 | 247 | ||
226 | /* compare to expected address */ | 248 | /* compare to expected address */ |
227 | if (result == STATE_OK && expected_address_cnt > 0) { | 249 | if (result == STATE_OK && config.expected_address_cnt > 0) { |
228 | result = STATE_CRITICAL; | 250 | result = STATE_CRITICAL; |
229 | temp_buffer = ""; | 251 | char *temp_buffer = ""; |
230 | unsigned long expect_match = (1 << expected_address_cnt) - 1; | 252 | unsigned long expect_match = (1 << config.expected_address_cnt) - 1; |
231 | unsigned long addr_match = (1 << n_addresses) - 1; | 253 | unsigned long addr_match = (1 << n_addresses) - 1; |
232 | 254 | ||
233 | for (int i = 0; i < expected_address_cnt; i++) { | 255 | for (size_t i = 0; i < config.expected_address_cnt; i++) { |
234 | int j; | ||
235 | /* check if we get a match on 'raw' ip or cidr */ | 256 | /* check if we get a match on 'raw' ip or cidr */ |
236 | for (j = 0; j < n_addresses; j++) { | 257 | for (size_t j = 0; j < n_addresses; j++) { |
237 | if (strcmp(addresses[j], expected_address[i]) == 0 || ip_match_cidr(addresses[j], expected_address[i])) { | 258 | if (strcmp(addresses[j], config.expected_address[i]) == 0 || ip_match_cidr(addresses[j], config.expected_address[i])) { |
238 | result = STATE_OK; | 259 | result = STATE_OK; |
239 | addr_match &= ~(1 << j); | 260 | addr_match &= ~(1 << j); |
240 | expect_match &= ~(1 << i); | 261 | expect_match &= ~(1 << i); |
@@ -242,11 +263,12 @@ int main(int argc, char **argv) { | |||
242 | } | 263 | } |
243 | 264 | ||
244 | /* prepare an error string */ | 265 | /* prepare an error string */ |
245 | xasprintf(&temp_buffer, "%s%s; ", temp_buffer, expected_address[i]); | 266 | xasprintf(&temp_buffer, "%s%s; ", temp_buffer, config.expected_address[i]); |
246 | } | 267 | } |
247 | /* check if expected_address must cover all in addresses and none may be missing */ | 268 | /* check if expected_address must cover all in addresses and none may be missing */ |
248 | if (all_match && (expect_match != 0 || addr_match != 0)) | 269 | if (config.all_match && (expect_match != 0 || addr_match != 0)) { |
249 | result = STATE_CRITICAL; | 270 | result = STATE_CRITICAL; |
271 | } | ||
250 | if (result == STATE_CRITICAL) { | 272 | if (result == STATE_CRITICAL) { |
251 | /* Strip off last semicolon... */ | 273 | /* Strip off last semicolon... */ |
252 | temp_buffer[strlen(temp_buffer) - 2] = '\0'; | 274 | temp_buffer[strlen(temp_buffer) - 2] = '\0'; |
@@ -254,28 +276,29 @@ int main(int argc, char **argv) { | |||
254 | } | 276 | } |
255 | } | 277 | } |
256 | 278 | ||
257 | if (expect_nxdomain) { | 279 | if (config.expect_nxdomain) { |
258 | if (!is_nxdomain) { | 280 | if (!is_nxdomain) { |
259 | result = STATE_CRITICAL; | 281 | result = STATE_CRITICAL; |
260 | xasprintf(&msg, _("Domain '%s' was found by the server: '%s'\n"), query_address, address); | 282 | xasprintf(&msg, _("Domain '%s' was found by the server: '%s'\n"), config.query_address, address); |
261 | } else { | 283 | } else { |
262 | if (address != NULL) | 284 | if (address != NULL) { |
263 | free(address); | 285 | free(address); |
286 | } | ||
264 | address = "NXDOMAIN"; | 287 | address = "NXDOMAIN"; |
265 | } | 288 | } |
266 | } | 289 | } |
267 | 290 | ||
268 | /* check if authoritative */ | 291 | /* check if authoritative */ |
269 | if (result == STATE_OK && expect_authority && non_authoritative) { | 292 | if (result == STATE_OK && config.expect_authority && non_authoritative) { |
270 | result = STATE_CRITICAL; | 293 | result = STATE_CRITICAL; |
271 | xasprintf(&msg, _("server %s is not authoritative for %s"), dns_server, query_address); | 294 | xasprintf(&msg, _("server %s is not authoritative for %s"), config.dns_server, config.query_address); |
272 | } | 295 | } |
273 | 296 | ||
274 | microsec = deltime(tv); | 297 | long microsec = deltime(tv); |
275 | elapsed_time = (double)microsec / 1.0e6; | 298 | double elapsed_time = (double)microsec / 1.0e6; |
276 | 299 | ||
277 | if (result == STATE_OK) { | 300 | if (result == STATE_OK) { |
278 | result = get_status(elapsed_time, time_thresholds); | 301 | result = get_status(elapsed_time, config.time_thresholds); |
279 | if (result == STATE_OK) { | 302 | if (result == STATE_OK) { |
280 | printf("DNS %s: ", _("OK")); | 303 | printf("DNS %s: ", _("OK")); |
281 | } else if (result == STATE_WARNING) { | 304 | } else if (result == STATE_WARNING) { |
@@ -284,24 +307,26 @@ int main(int argc, char **argv) { | |||
284 | printf("DNS %s: ", _("CRITICAL")); | 307 | printf("DNS %s: ", _("CRITICAL")); |
285 | } | 308 | } |
286 | printf(ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), elapsed_time); | 309 | printf(ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), elapsed_time); |
287 | printf(_(". %s returns %s"), query_address, address); | 310 | printf(_(". %s returns %s"), config.query_address, address); |
288 | if ((time_thresholds->warning != NULL) && (time_thresholds->critical != NULL)) { | 311 | if ((config.time_thresholds->warning != NULL) && (config.time_thresholds->critical != NULL)) { |
289 | printf("|%s\n", fperfdata("time", elapsed_time, "s", true, time_thresholds->warning->end, true, time_thresholds->critical->end, | 312 | printf("|%s\n", fperfdata("time", elapsed_time, "s", true, config.time_thresholds->warning->end, true, |
290 | true, 0, false, 0)); | 313 | config.time_thresholds->critical->end, true, 0, false, 0)); |
291 | } else if ((time_thresholds->warning == NULL) && (time_thresholds->critical != NULL)) { | 314 | } else if ((config.time_thresholds->warning == NULL) && (config.time_thresholds->critical != NULL)) { |
292 | printf("|%s\n", fperfdata("time", elapsed_time, "s", false, 0, true, time_thresholds->critical->end, true, 0, false, 0)); | 315 | printf("|%s\n", fperfdata("time", elapsed_time, "s", false, 0, true, config.time_thresholds->critical->end, true, 0, false, 0)); |
293 | } else if ((time_thresholds->warning != NULL) && (time_thresholds->critical == NULL)) { | 316 | } else if ((config.time_thresholds->warning != NULL) && (config.time_thresholds->critical == NULL)) { |
294 | printf("|%s\n", fperfdata("time", elapsed_time, "s", true, time_thresholds->warning->end, false, 0, true, 0, false, 0)); | 317 | printf("|%s\n", fperfdata("time", elapsed_time, "s", true, config.time_thresholds->warning->end, false, 0, true, 0, false, 0)); |
295 | } else | 318 | } else { |
296 | printf("|%s\n", fperfdata("time", elapsed_time, "s", false, 0, false, 0, true, 0, false, 0)); | 319 | printf("|%s\n", fperfdata("time", elapsed_time, "s", false, 0, false, 0, true, 0, false, 0)); |
297 | } else if (result == STATE_WARNING) | 320 | } |
321 | } else if (result == STATE_WARNING) { | ||
298 | printf(_("DNS WARNING - %s\n"), !strcmp(msg, "") ? _(" Probably a non-existent host/domain") : msg); | 322 | printf(_("DNS WARNING - %s\n"), !strcmp(msg, "") ? _(" Probably a non-existent host/domain") : msg); |
299 | else if (result == STATE_CRITICAL) | 323 | } else if (result == STATE_CRITICAL) { |
300 | printf(_("DNS CRITICAL - %s\n"), !strcmp(msg, "") ? _(" Probably a non-existent host/domain") : msg); | 324 | printf(_("DNS CRITICAL - %s\n"), !strcmp(msg, "") ? _(" Probably a non-existent host/domain") : msg); |
301 | else | 325 | } else { |
302 | printf(_("DNS UNKNOWN - %s\n"), !strcmp(msg, "") ? _(" Probably a non-existent host/domain") : msg); | 326 | printf(_("DNS UNKNOWN - %s\n"), !strcmp(msg, "") ? _(" Probably a non-existent host/domain") : msg); |
327 | } | ||
303 | 328 | ||
304 | return result; | 329 | exit(result); |
305 | } | 330 | } |
306 | 331 | ||
307 | bool ip_match_cidr(const char *addr, const char *cidr_ro) { | 332 | bool ip_match_cidr(const char *addr, const char *cidr_ro) { |
@@ -329,64 +354,69 @@ unsigned long ip2long(const char *src) { | |||
329 | : 0; | 354 | : 0; |
330 | } | 355 | } |
331 | 356 | ||
332 | int error_scan(char *input_buffer, bool *is_nxdomain) { | 357 | mp_state_enum error_scan(char *input_buffer, bool *is_nxdomain, const char dns_server[ADDRESS_LENGTH]) { |
333 | 358 | ||
334 | const int nxdomain = strstr(input_buffer, "Non-existent") || strstr(input_buffer, "** server can't find") || | 359 | const int nxdomain = strstr(input_buffer, "Non-existent") || strstr(input_buffer, "** server can't find") || |
335 | strstr(input_buffer, "** Can't find") || strstr(input_buffer, "NXDOMAIN"); | 360 | strstr(input_buffer, "** Can't find") || strstr(input_buffer, "NXDOMAIN"); |
336 | if (nxdomain) | 361 | if (nxdomain) { |
337 | *is_nxdomain = true; | 362 | *is_nxdomain = true; |
363 | } | ||
338 | 364 | ||
339 | /* the DNS lookup timed out */ | 365 | /* the DNS lookup timed out */ |
340 | if (strstr(input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) || | 366 | if (strstr(input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) || |
341 | strstr(input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) || | 367 | strstr(input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) || |
342 | strstr(input_buffer, _("the `-sil[ent]' option to prevent this message from appearing."))) | 368 | strstr(input_buffer, _("the `-sil[ent]' option to prevent this message from appearing."))) { |
343 | return STATE_OK; | 369 | return STATE_OK; |
370 | } | ||
344 | 371 | ||
345 | /* DNS server is not running... */ | 372 | /* DNS server is not running... */ |
346 | else if (strstr(input_buffer, "No response from server")) | 373 | else if (strstr(input_buffer, "No response from server")) { |
347 | die(STATE_CRITICAL, _("No response from DNS %s\n"), dns_server); | 374 | die(STATE_CRITICAL, _("No response from DNS %s\n"), dns_server); |
348 | else if (strstr(input_buffer, "no servers could be reached")) | 375 | } else if (strstr(input_buffer, "no servers could be reached")) { |
349 | die(STATE_CRITICAL, _("No response from DNS %s\n"), dns_server); | 376 | die(STATE_CRITICAL, _("No response from DNS %s\n"), dns_server); |
377 | } | ||
350 | 378 | ||
351 | /* Host name is valid, but server doesn't have records... */ | 379 | /* Host name is valid, but server doesn't have records... */ |
352 | else if (strstr(input_buffer, "No records")) | 380 | else if (strstr(input_buffer, "No records")) { |
353 | die(STATE_CRITICAL, _("DNS %s has no records\n"), dns_server); | 381 | die(STATE_CRITICAL, _("DNS %s has no records\n"), dns_server); |
382 | } | ||
354 | 383 | ||
355 | /* Connection was refused */ | 384 | /* Connection was refused */ |
356 | else if (strstr(input_buffer, "Connection refused") || strstr(input_buffer, "Couldn't find server") || | 385 | else if (strstr(input_buffer, "Connection refused") || strstr(input_buffer, "Couldn't find server") || |
357 | strstr(input_buffer, "Refused") || (strstr(input_buffer, "** server can't find") && strstr(input_buffer, ": REFUSED"))) | 386 | strstr(input_buffer, "Refused") || (strstr(input_buffer, "** server can't find") && strstr(input_buffer, ": REFUSED"))) { |
358 | die(STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server); | 387 | die(STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server); |
388 | } | ||
359 | 389 | ||
360 | /* Query refused (usually by an ACL in the namserver) */ | 390 | /* Query refused (usually by an ACL in the namserver) */ |
361 | else if (strstr(input_buffer, "Query refused")) | 391 | else if (strstr(input_buffer, "Query refused")) { |
362 | die(STATE_CRITICAL, _("Query was refused by DNS server at %s\n"), dns_server); | 392 | die(STATE_CRITICAL, _("Query was refused by DNS server at %s\n"), dns_server); |
393 | } | ||
363 | 394 | ||
364 | /* No information (e.g. nameserver IP has two PTR records) */ | 395 | /* No information (e.g. nameserver IP has two PTR records) */ |
365 | else if (strstr(input_buffer, "No information")) | 396 | else if (strstr(input_buffer, "No information")) { |
366 | die(STATE_CRITICAL, _("No information returned by DNS server at %s\n"), dns_server); | 397 | die(STATE_CRITICAL, _("No information returned by DNS server at %s\n"), dns_server); |
398 | } | ||
367 | 399 | ||
368 | /* Network is unreachable */ | 400 | /* Network is unreachable */ |
369 | else if (strstr(input_buffer, "Network is unreachable")) | 401 | else if (strstr(input_buffer, "Network is unreachable")) { |
370 | die(STATE_CRITICAL, _("Network is unreachable\n")); | 402 | die(STATE_CRITICAL, _("Network is unreachable\n")); |
403 | } | ||
371 | 404 | ||
372 | /* Internal server failure */ | 405 | /* Internal server failure */ |
373 | else if (strstr(input_buffer, "Server failure")) | 406 | else if (strstr(input_buffer, "Server failure")) { |
374 | die(STATE_CRITICAL, _("DNS failure for %s\n"), dns_server); | 407 | die(STATE_CRITICAL, _("DNS failure for %s\n"), dns_server); |
408 | } | ||
375 | 409 | ||
376 | /* Request error or the DNS lookup timed out */ | 410 | /* Request error or the DNS lookup timed out */ |
377 | else if (strstr(input_buffer, "Format error") || strstr(input_buffer, "Timed out")) | 411 | else if (strstr(input_buffer, "Format error") || strstr(input_buffer, "Timed out")) { |
378 | return STATE_WARNING; | 412 | return STATE_WARNING; |
413 | } | ||
379 | 414 | ||
380 | return STATE_OK; | 415 | return STATE_OK; |
381 | } | 416 | } |
382 | 417 | ||
383 | /* process command-line arguments */ | 418 | /* process command-line arguments */ |
384 | int process_arguments(int argc, char **argv) { | 419 | check_dns_config_wrapper process_arguments(int argc, char **argv) { |
385 | int c; | ||
386 | char *warning = NULL; | ||
387 | char *critical = NULL; | ||
388 | |||
389 | int opt_index = 0; | ||
390 | static struct option long_opts[] = {{"help", no_argument, 0, 'h'}, | 420 | static struct option long_opts[] = {{"help", no_argument, 0, 'h'}, |
391 | {"version", no_argument, 0, 'V'}, | 421 | {"version", no_argument, 0, 'V'}, |
392 | {"verbose", no_argument, 0, 'v'}, | 422 | {"verbose", no_argument, 0, 'v'}, |
@@ -402,20 +432,34 @@ int process_arguments(int argc, char **argv) { | |||
402 | {"critical", required_argument, 0, 'c'}, | 432 | {"critical", required_argument, 0, 'c'}, |
403 | {0, 0, 0, 0}}; | 433 | {0, 0, 0, 0}}; |
404 | 434 | ||
405 | if (argc < 2) | 435 | check_dns_config_wrapper result = { |
406 | return ERROR; | 436 | .config = check_dns_config_init(), |
437 | .errorcode = OK, | ||
438 | }; | ||
407 | 439 | ||
408 | for (c = 1; c < argc; c++) | 440 | if (argc < 2) { |
409 | if (strcmp("-to", argv[c]) == 0) | 441 | result.errorcode = ERROR; |
410 | strcpy(argv[c], "-t"); | 442 | return result; |
443 | } | ||
411 | 444 | ||
412 | while (1) { | 445 | for (int index = 1; index < argc; index++) { |
413 | c = getopt_long(argc, argv, "hVvALnt:H:s:r:a:w:c:", long_opts, &opt_index); | 446 | if (strcmp("-to", argv[index]) == 0) { |
447 | strcpy(argv[index], "-t"); | ||
448 | } | ||
449 | } | ||
450 | |||
451 | char *warning = NULL; | ||
452 | char *critical = NULL; | ||
453 | int opt_index = 0; | ||
454 | int index = 0; | ||
455 | while (true) { | ||
456 | index = getopt_long(argc, argv, "hVvALnt:H:s:r:a:w:c:", long_opts, &opt_index); | ||
414 | 457 | ||
415 | if (c == -1 || c == EOF) | 458 | if (index == -1 || index == EOF) { |
416 | break; | 459 | break; |
460 | } | ||
417 | 461 | ||
418 | switch (c) { | 462 | switch (index) { |
419 | case 'h': /* help */ | 463 | case 'h': /* help */ |
420 | print_help(); | 464 | print_help(); |
421 | exit(STATE_UNKNOWN); | 465 | exit(STATE_UNKNOWN); |
@@ -429,54 +473,63 @@ int process_arguments(int argc, char **argv) { | |||
429 | timeout_interval = atoi(optarg); | 473 | timeout_interval = atoi(optarg); |
430 | break; | 474 | break; |
431 | case 'H': /* hostname */ | 475 | case 'H': /* hostname */ |
432 | if (strlen(optarg) >= ADDRESS_LENGTH) | 476 | if (strlen(optarg) >= ADDRESS_LENGTH) { |
433 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); | 477 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); |
434 | strcpy(query_address, optarg); | 478 | } |
479 | strcpy(result.config.query_address, optarg); | ||
435 | break; | 480 | break; |
436 | case 's': /* server name */ | 481 | case 's': /* server name */ |
437 | /* TODO: this host_or_die check is probably unnecessary. | 482 | /* TODO: this host_or_die check is probably unnecessary. |
438 | * Better to confirm nslookup response matches */ | 483 | * Better to confirm nslookup response matches */ |
439 | host_or_die(optarg); | 484 | host_or_die(optarg); |
440 | if (strlen(optarg) >= ADDRESS_LENGTH) | 485 | if (strlen(optarg) >= ADDRESS_LENGTH) { |
441 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); | 486 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); |
442 | strcpy(dns_server, optarg); | 487 | } |
488 | strcpy(result.config.dns_server, optarg); | ||
443 | break; | 489 | break; |
444 | case 'r': /* reverse server name */ | 490 | case 'r': /* reverse server name */ |
445 | /* TODO: Is this host_or_die necessary? */ | 491 | /* TODO: Is this host_or_die necessary? */ |
492 | // TODO This does not do anything!!! 2025-03-08 rincewind | ||
446 | host_or_die(optarg); | 493 | host_or_die(optarg); |
447 | if (strlen(optarg) >= ADDRESS_LENGTH) | 494 | if (strlen(optarg) >= ADDRESS_LENGTH) { |
448 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); | 495 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); |
496 | } | ||
497 | static char ptr_server[ADDRESS_LENGTH] = ""; | ||
449 | strcpy(ptr_server, optarg); | 498 | strcpy(ptr_server, optarg); |
450 | break; | 499 | break; |
451 | case 'a': /* expected address */ | 500 | case 'a': /* expected address */ |
452 | if (strlen(optarg) >= ADDRESS_LENGTH) | 501 | if (strlen(optarg) >= ADDRESS_LENGTH) { |
453 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); | 502 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); |
503 | } | ||
454 | if (strchr(optarg, ',') != NULL) { | 504 | if (strchr(optarg, ',') != NULL) { |
455 | char *comma = strchr(optarg, ','); | 505 | char *comma = strchr(optarg, ','); |
456 | while (comma != NULL) { | 506 | while (comma != NULL) { |
457 | expected_address = (char **)realloc(expected_address, (expected_address_cnt + 1) * sizeof(char **)); | 507 | result.config.expected_address = |
458 | expected_address[expected_address_cnt] = strndup(optarg, comma - optarg); | 508 | (char **)realloc(result.config.expected_address, (result.config.expected_address_cnt + 1) * sizeof(char **)); |
459 | expected_address_cnt++; | 509 | result.config.expected_address[result.config.expected_address_cnt] = strndup(optarg, comma - optarg); |
510 | result.config.expected_address_cnt++; | ||
460 | optarg = comma + 1; | 511 | optarg = comma + 1; |
461 | comma = strchr(optarg, ','); | 512 | comma = strchr(optarg, ','); |
462 | } | 513 | } |
463 | expected_address = (char **)realloc(expected_address, (expected_address_cnt + 1) * sizeof(char **)); | 514 | result.config.expected_address = |
464 | expected_address[expected_address_cnt] = strdup(optarg); | 515 | (char **)realloc(result.config.expected_address, (result.config.expected_address_cnt + 1) * sizeof(char **)); |
465 | expected_address_cnt++; | 516 | result.config.expected_address[result.config.expected_address_cnt] = strdup(optarg); |
517 | result.config.expected_address_cnt++; | ||
466 | } else { | 518 | } else { |
467 | expected_address = (char **)realloc(expected_address, (expected_address_cnt + 1) * sizeof(char **)); | 519 | result.config.expected_address = |
468 | expected_address[expected_address_cnt] = strdup(optarg); | 520 | (char **)realloc(result.config.expected_address, (result.config.expected_address_cnt + 1) * sizeof(char **)); |
469 | expected_address_cnt++; | 521 | result.config.expected_address[result.config.expected_address_cnt] = strdup(optarg); |
522 | result.config.expected_address_cnt++; | ||
470 | } | 523 | } |
471 | break; | 524 | break; |
472 | case 'n': /* expect NXDOMAIN */ | 525 | case 'n': /* expect NXDOMAIN */ |
473 | expect_nxdomain = true; | 526 | result.config.expect_nxdomain = true; |
474 | break; | 527 | break; |
475 | case 'A': /* expect authority */ | 528 | case 'A': /* expect authority */ |
476 | expect_authority = true; | 529 | result.config.expect_authority = true; |
477 | break; | 530 | break; |
478 | case 'L': /* all must match */ | 531 | case 'L': /* all must match */ |
479 | all_match = true; | 532 | result.config.all_match = true; |
480 | break; | 533 | break; |
481 | case 'w': | 534 | case 'w': |
482 | warning = optarg; | 535 | warning = optarg; |
@@ -489,38 +542,42 @@ int process_arguments(int argc, char **argv) { | |||
489 | } | 542 | } |
490 | } | 543 | } |
491 | 544 | ||
492 | c = optind; | 545 | index = optind; |
493 | if (strlen(query_address) == 0 && c < argc) { | 546 | if (strlen(result.config.query_address) == 0 && index < argc) { |
494 | if (strlen(argv[c]) >= ADDRESS_LENGTH) | 547 | if (strlen(argv[index]) >= ADDRESS_LENGTH) { |
495 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); | 548 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); |
496 | strcpy(query_address, argv[c++]); | 549 | } |
550 | strcpy(result.config.query_address, argv[index++]); | ||
497 | } | 551 | } |
498 | 552 | ||
499 | if (strlen(dns_server) == 0 && c < argc) { | 553 | if (strlen(result.config.dns_server) == 0 && index < argc) { |
500 | /* TODO: See -s option */ | 554 | /* TODO: See -s option */ |
501 | host_or_die(argv[c]); | 555 | host_or_die(argv[index]); |
502 | if (strlen(argv[c]) >= ADDRESS_LENGTH) | 556 | if (strlen(argv[index]) >= ADDRESS_LENGTH) { |
503 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); | 557 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); |
504 | strcpy(dns_server, argv[c++]); | 558 | } |
559 | strcpy(result.config.dns_server, argv[index++]); | ||
505 | } | 560 | } |
506 | 561 | ||
507 | set_thresholds(&time_thresholds, warning, critical); | 562 | set_thresholds(&result.config.time_thresholds, warning, critical); |
508 | 563 | ||
509 | return validate_arguments(); | 564 | return validate_arguments(result); |
510 | } | 565 | } |
511 | 566 | ||
512 | int validate_arguments(void) { | 567 | check_dns_config_wrapper validate_arguments(check_dns_config_wrapper config_wrapper) { |
513 | if (query_address[0] == 0) { | 568 | if (config_wrapper.config.query_address[0] == 0) { |
514 | printf("missing --host argument\n"); | 569 | printf("missing --host argument\n"); |
515 | return ERROR; | 570 | config_wrapper.errorcode = ERROR; |
571 | return config_wrapper; | ||
516 | } | 572 | } |
517 | 573 | ||
518 | if (expected_address_cnt > 0 && expect_nxdomain) { | 574 | if (config_wrapper.config.expected_address_cnt > 0 && config_wrapper.config.expect_nxdomain) { |
519 | printf("--expected-address and --expect-nxdomain cannot be combined\n"); | 575 | printf("--expected-address and --expect-nxdomain cannot be combined\n"); |
520 | return ERROR; | 576 | config_wrapper.errorcode = ERROR; |
577 | return config_wrapper; | ||
521 | } | 578 | } |
522 | 579 | ||
523 | return OK; | 580 | return config_wrapper; |
524 | } | 581 | } |
525 | 582 | ||
526 | void print_help(void) { | 583 | void print_help(void) { |
diff --git a/plugins/check_dns.d/config.h b/plugins/check_dns.d/config.h new file mode 100644 index 00000000..9ec4eb82 --- /dev/null +++ b/plugins/check_dns.d/config.h | |||
@@ -0,0 +1,34 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../config.h" | ||
4 | #include "thresholds.h" | ||
5 | #include <stddef.h> | ||
6 | |||
7 | #define ADDRESS_LENGTH 256 | ||
8 | |||
9 | typedef struct { | ||
10 | bool all_match; | ||
11 | char dns_server[ADDRESS_LENGTH]; | ||
12 | char query_address[ADDRESS_LENGTH]; | ||
13 | bool expect_nxdomain; | ||
14 | bool expect_authority; | ||
15 | char **expected_address; | ||
16 | size_t expected_address_cnt; | ||
17 | |||
18 | thresholds *time_thresholds; | ||
19 | } check_dns_config; | ||
20 | |||
21 | check_dns_config check_dns_config_init() { | ||
22 | check_dns_config tmp = { | ||
23 | .all_match = false, | ||
24 | .dns_server = "", | ||
25 | .query_address = "", | ||
26 | .expect_nxdomain = false, | ||
27 | .expect_authority = false, | ||
28 | .expected_address = NULL, | ||
29 | .expected_address_cnt = 0, | ||
30 | |||
31 | .time_thresholds = NULL, | ||
32 | }; | ||
33 | return tmp; | ||
34 | } | ||
diff --git a/plugins/check_game.c b/plugins/check_game.c index 619277e7..c0193b03 100644 --- a/plugins/check_game.c +++ b/plugins/check_game.c | |||
@@ -36,9 +36,15 @@ const char *email = "devel@monitoring-plugins.org"; | |||
36 | #include "common.h" | 36 | #include "common.h" |
37 | #include "utils.h" | 37 | #include "utils.h" |
38 | #include "runcmd.h" | 38 | #include "runcmd.h" |
39 | #include "check_game.d/config.h" | ||
40 | #include "../lib/monitoringplug.h" | ||
39 | 41 | ||
40 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 42 | typedef struct { |
41 | static int validate_arguments(void); | 43 | int errorcode; |
44 | check_game_config config; | ||
45 | } check_game_config_wrapper; | ||
46 | |||
47 | static check_game_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
42 | static void print_help(void); | 48 | static void print_help(void); |
43 | void print_usage(void); | 49 | void print_usage(void); |
44 | 50 | ||
@@ -49,26 +55,9 @@ void print_usage(void); | |||
49 | #define QSTAT_HOST_TIMEOUT "TIMEOUT" | 55 | #define QSTAT_HOST_TIMEOUT "TIMEOUT" |
50 | #define QSTAT_MAX_RETURN_ARGS 12 | 56 | #define QSTAT_MAX_RETURN_ARGS 12 |
51 | 57 | ||
52 | static char *server_ip; | ||
53 | static char *game_type; | ||
54 | static int port = 0; | ||
55 | |||
56 | static bool verbose = false; | 58 | static bool verbose = false; |
57 | 59 | ||
58 | static int qstat_game_players_max = -1; | ||
59 | static int qstat_game_players = -1; | ||
60 | static int qstat_game_field = -1; | ||
61 | static int qstat_map_field = -1; | ||
62 | static int qstat_ping_field = -1; | ||
63 | |||
64 | int main(int argc, char **argv) { | 60 | int main(int argc, char **argv) { |
65 | char *command_line; | ||
66 | int result = STATE_UNKNOWN; | ||
67 | char *p; | ||
68 | char *ret[QSTAT_MAX_RETURN_ARGS]; | ||
69 | size_t i = 0; | ||
70 | output chld_out; | ||
71 | |||
72 | setlocale(LC_ALL, ""); | 61 | setlocale(LC_ALL, ""); |
73 | bindtextdomain(PACKAGE, LOCALEDIR); | 62 | bindtextdomain(PACKAGE, LOCALEDIR); |
74 | textdomain(PACKAGE); | 63 | textdomain(PACKAGE); |
@@ -76,22 +65,31 @@ int main(int argc, char **argv) { | |||
76 | /* Parse extra opts if any */ | 65 | /* Parse extra opts if any */ |
77 | argv = np_extra_opts(&argc, argv, progname); | 66 | argv = np_extra_opts(&argc, argv, progname); |
78 | 67 | ||
79 | if (process_arguments(argc, argv) == ERROR) | 68 | check_game_config_wrapper tmp = process_arguments(argc, argv); |
69 | |||
70 | if (tmp.errorcode == ERROR) { | ||
80 | usage_va(_("Could not parse arguments")); | 71 | usage_va(_("Could not parse arguments")); |
72 | } | ||
73 | |||
74 | check_game_config config = tmp.config; | ||
81 | 75 | ||
82 | result = STATE_OK; | 76 | mp_state_enum result = STATE_OK; |
83 | 77 | ||
84 | /* create the command line to execute */ | 78 | /* create the command line to execute */ |
85 | xasprintf(&command_line, "%s -raw %s -%s %s", PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, game_type, server_ip); | 79 | char *command_line = NULL; |
80 | xasprintf(&command_line, "%s -raw %s -%s %s", PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, config.game_type, config.server_ip); | ||
86 | 81 | ||
87 | if (port) | 82 | if (config.port) { |
88 | xasprintf(&command_line, "%s:%-d", command_line, port); | 83 | xasprintf(&command_line, "%s:%-d", command_line, config.port); |
84 | } | ||
89 | 85 | ||
90 | if (verbose) | 86 | if (verbose) { |
91 | printf("%s\n", command_line); | 87 | printf("%s\n", command_line); |
88 | } | ||
92 | 89 | ||
93 | /* run the command. historically, this plugin ignores output on stderr, | 90 | /* run the command. historically, this plugin ignores output on stderr, |
94 | * as well as return status of the qstat program */ | 91 | * as well as return status of the qstat program */ |
92 | output chld_out = {}; | ||
95 | (void)np_runcmd(command_line, &chld_out, NULL, 0); | 93 | (void)np_runcmd(command_line, &chld_out, NULL, 0); |
96 | 94 | ||
97 | /* sanity check */ | 95 | /* sanity check */ |
@@ -104,19 +102,22 @@ int main(int argc, char **argv) { | |||
104 | In the end, I figured I'd simply let an error occur & then trap it | 102 | In the end, I figured I'd simply let an error occur & then trap it |
105 | */ | 103 | */ |
106 | 104 | ||
107 | if (!strncmp(chld_out.line[0], "unknown option", 14)) { | 105 | if (!strncmp(chld_out.line[0], "unknown option", strlen("unknown option"))) { |
108 | printf(_("CRITICAL - Host type parameter incorrect!\n")); | 106 | printf(_("CRITICAL - Host type parameter incorrect!\n")); |
109 | result = STATE_CRITICAL; | 107 | result = STATE_CRITICAL; |
110 | return result; | 108 | exit(result); |
111 | } | 109 | } |
112 | 110 | ||
113 | p = (char *)strtok(chld_out.line[0], QSTAT_DATA_DELIMITER); | 111 | char *ret[QSTAT_MAX_RETURN_ARGS]; |
114 | while (p != NULL) { | 112 | size_t i = 0; |
115 | ret[i] = p; | 113 | char *sequence = strtok(chld_out.line[0], QSTAT_DATA_DELIMITER); |
116 | p = (char *)strtok(NULL, QSTAT_DATA_DELIMITER); | 114 | while (sequence != NULL) { |
115 | ret[i] = sequence; | ||
116 | sequence = strtok(NULL, QSTAT_DATA_DELIMITER); | ||
117 | i++; | 117 | i++; |
118 | if (i >= QSTAT_MAX_RETURN_ARGS) | 118 | if (i >= QSTAT_MAX_RETURN_ARGS) { |
119 | break; | 119 | break; |
120 | } | ||
120 | } | 121 | } |
121 | 122 | ||
122 | if (strstr(ret[2], QSTAT_HOST_ERROR)) { | 123 | if (strstr(ret[2], QSTAT_HOST_ERROR)) { |
@@ -129,19 +130,20 @@ int main(int argc, char **argv) { | |||
129 | printf(_("CRITICAL - Game server timeout\n")); | 130 | printf(_("CRITICAL - Game server timeout\n")); |
130 | result = STATE_CRITICAL; | 131 | result = STATE_CRITICAL; |
131 | } else { | 132 | } else { |
132 | printf("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n", ret[qstat_game_players], ret[qstat_game_players_max], ret[qstat_game_field], | 133 | printf("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n", ret[config.qstat_game_players], ret[config.qstat_game_players_max], |
133 | ret[qstat_map_field], ret[qstat_ping_field], | 134 | ret[config.qstat_game_field], ret[config.qstat_map_field], ret[config.qstat_ping_field], |
134 | perfdata("players", atol(ret[qstat_game_players]), "", false, 0, false, 0, true, 0, true, atol(ret[qstat_game_players_max])), | 135 | perfdata("players", atol(ret[config.qstat_game_players]), "", false, 0, false, 0, true, 0, true, |
135 | fperfdata("ping", strtod(ret[qstat_ping_field], NULL), "", false, 0, false, 0, true, 0, false, 0)); | 136 | atol(ret[config.qstat_game_players_max])), |
137 | fperfdata("ping", strtod(ret[config.qstat_ping_field], NULL), "", false, 0, false, 0, true, 0, false, 0)); | ||
136 | } | 138 | } |
137 | 139 | ||
138 | return result; | 140 | exit(result); |
139 | } | 141 | } |
140 | 142 | ||
141 | int process_arguments(int argc, char **argv) { | 143 | #define players_field_index 129 |
142 | int c; | 144 | #define max_players_field_index 130 |
143 | 145 | ||
144 | int opt_index = 0; | 146 | check_game_config_wrapper process_arguments(int argc, char **argv) { |
145 | static struct option long_opts[] = {{"help", no_argument, 0, 'h'}, | 147 | static struct option long_opts[] = {{"help", no_argument, 0, 'h'}, |
146 | {"version", no_argument, 0, 'V'}, | 148 | {"version", no_argument, 0, 'V'}, |
147 | {"verbose", no_argument, 0, 'v'}, | 149 | {"verbose", no_argument, 0, 'v'}, |
@@ -152,29 +154,39 @@ int process_arguments(int argc, char **argv) { | |||
152 | {"map-field", required_argument, 0, 'm'}, | 154 | {"map-field", required_argument, 0, 'm'}, |
153 | {"ping-field", required_argument, 0, 'p'}, | 155 | {"ping-field", required_argument, 0, 'p'}, |
154 | {"game-field", required_argument, 0, 'g'}, | 156 | {"game-field", required_argument, 0, 'g'}, |
155 | {"players-field", required_argument, 0, 129}, | 157 | {"players-field", required_argument, 0, players_field_index}, |
156 | {"max-players-field", required_argument, 0, 130}, | 158 | {"max-players-field", required_argument, 0, max_players_field_index}, |
157 | {0, 0, 0, 0}}; | 159 | {0, 0, 0, 0}}; |
158 | 160 | ||
159 | if (argc < 2) | 161 | check_game_config_wrapper result = { |
160 | return ERROR; | 162 | .config = check_game_config_init(), |
163 | .errorcode = OK, | ||
164 | }; | ||
165 | |||
166 | if (argc < 2) { | ||
167 | result.errorcode = ERROR; | ||
168 | return result; | ||
169 | } | ||
161 | 170 | ||
162 | for (c = 1; c < argc; c++) { | 171 | for (int option_counter = 1; option_counter < argc; option_counter++) { |
163 | if (strcmp("-mf", argv[c]) == 0) | 172 | if (strcmp("-mf", argv[option_counter]) == 0) { |
164 | strcpy(argv[c], "-m"); | 173 | strcpy(argv[option_counter], "-m"); |
165 | else if (strcmp("-pf", argv[c]) == 0) | 174 | } else if (strcmp("-pf", argv[option_counter]) == 0) { |
166 | strcpy(argv[c], "-p"); | 175 | strcpy(argv[option_counter], "-p"); |
167 | else if (strcmp("-gf", argv[c]) == 0) | 176 | } else if (strcmp("-gf", argv[option_counter]) == 0) { |
168 | strcpy(argv[c], "-g"); | 177 | strcpy(argv[option_counter], "-g"); |
178 | } | ||
169 | } | 179 | } |
170 | 180 | ||
171 | while (1) { | 181 | int opt_index = 0; |
172 | c = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index); | 182 | while (true) { |
183 | int option_index = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index); | ||
173 | 184 | ||
174 | if (c == -1 || c == EOF) | 185 | if (option_index == -1 || option_index == EOF) { |
175 | break; | 186 | break; |
187 | } | ||
176 | 188 | ||
177 | switch (c) { | 189 | switch (option_index) { |
178 | case 'h': /* help */ | 190 | case 'h': /* help */ |
179 | print_help(); | 191 | print_help(); |
180 | exit(STATE_UNKNOWN); | 192 | exit(STATE_UNKNOWN); |
@@ -188,79 +200,75 @@ int process_arguments(int argc, char **argv) { | |||
188 | timeout_interval = atoi(optarg); | 200 | timeout_interval = atoi(optarg); |
189 | break; | 201 | break; |
190 | case 'H': /* hostname */ | 202 | case 'H': /* hostname */ |
191 | if (strlen(optarg) >= MAX_HOST_ADDRESS_LENGTH) | 203 | if (strlen(optarg) >= MAX_HOST_ADDRESS_LENGTH) { |
192 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); | 204 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); |
193 | server_ip = optarg; | 205 | } |
206 | result.config.server_ip = optarg; | ||
194 | break; | 207 | break; |
195 | case 'P': /* port */ | 208 | case 'P': /* port */ |
196 | port = atoi(optarg); | 209 | result.config.port = atoi(optarg); |
197 | break; | 210 | break; |
198 | case 'G': /* hostname */ | 211 | case 'G': /* hostname */ |
199 | if (strlen(optarg) >= MAX_INPUT_BUFFER) | 212 | if (strlen(optarg) >= MAX_INPUT_BUFFER) { |
200 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); | 213 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); |
201 | game_type = optarg; | 214 | } |
215 | result.config.game_type = optarg; | ||
202 | break; | 216 | break; |
203 | case 'p': /* index of ping field */ | 217 | case 'p': /* index of ping field */ |
204 | qstat_ping_field = atoi(optarg); | 218 | result.config.qstat_ping_field = atoi(optarg); |
205 | if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS) | 219 | if (result.config.qstat_ping_field < 0 || result.config.qstat_ping_field > QSTAT_MAX_RETURN_ARGS) { |
206 | return ERROR; | 220 | result.errorcode = ERROR; |
221 | return result; | ||
222 | } | ||
207 | break; | 223 | break; |
208 | case 'm': /* index on map field */ | 224 | case 'm': /* index on map field */ |
209 | qstat_map_field = atoi(optarg); | 225 | result.config.qstat_map_field = atoi(optarg); |
210 | if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS) | 226 | if (result.config.qstat_map_field < 0 || result.config.qstat_map_field > QSTAT_MAX_RETURN_ARGS) { |
211 | return ERROR; | 227 | result.errorcode = ERROR; |
228 | return result; | ||
229 | } | ||
212 | break; | 230 | break; |
213 | case 'g': /* index of game field */ | 231 | case 'g': /* index of game field */ |
214 | qstat_game_field = atoi(optarg); | 232 | result.config.qstat_game_field = atoi(optarg); |
215 | if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS) | 233 | if (result.config.qstat_game_field < 0 || result.config.qstat_game_field > QSTAT_MAX_RETURN_ARGS) { |
216 | return ERROR; | 234 | result.errorcode = ERROR; |
235 | return result; | ||
236 | } | ||
217 | break; | 237 | break; |
218 | case 129: /* index of player count field */ | 238 | case players_field_index: /* index of player count field */ |
219 | qstat_game_players = atoi(optarg); | 239 | result.config.qstat_game_players = atoi(optarg); |
220 | if (qstat_game_players_max == 0) | 240 | if (result.config.qstat_game_players_max == 0) { |
221 | qstat_game_players_max = qstat_game_players - 1; | 241 | result.config.qstat_game_players_max = result.config.qstat_game_players - 1; |
222 | if (qstat_game_players < 0 || qstat_game_players > QSTAT_MAX_RETURN_ARGS) | 242 | } |
223 | return ERROR; | 243 | if (result.config.qstat_game_players < 0 || result.config.qstat_game_players > QSTAT_MAX_RETURN_ARGS) { |
244 | result.errorcode = ERROR; | ||
245 | return result; | ||
246 | } | ||
224 | break; | 247 | break; |
225 | case 130: /* index of max players field */ | 248 | case max_players_field_index: /* index of max players field */ |
226 | qstat_game_players_max = atoi(optarg); | 249 | result.config.qstat_game_players_max = atoi(optarg); |
227 | if (qstat_game_players_max < 0 || qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) | 250 | if (result.config.qstat_game_players_max < 0 || result.config.qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) { |
228 | return ERROR; | 251 | result.errorcode = ERROR; |
252 | return result; | ||
253 | } | ||
229 | break; | 254 | break; |
230 | default: /* args not parsable */ | 255 | default: /* args not parsable */ |
231 | usage5(); | 256 | usage5(); |
232 | } | 257 | } |
233 | } | 258 | } |
234 | 259 | ||
235 | c = optind; | 260 | int option_counter = optind; |
236 | /* first option is the game type */ | 261 | /* first option is the game type */ |
237 | if (!game_type && c < argc) | 262 | if (!result.config.game_type && option_counter < argc) { |
238 | game_type = strdup(argv[c++]); | 263 | result.config.game_type = strdup(argv[option_counter++]); |
264 | } | ||
239 | 265 | ||
240 | /* Second option is the server name */ | 266 | /* Second option is the server name */ |
241 | if (!server_ip && c < argc) | 267 | if (!result.config.server_ip && option_counter < argc) { |
242 | server_ip = strdup(argv[c++]); | 268 | result.config.server_ip = strdup(argv[option_counter++]); |
243 | 269 | } | |
244 | return validate_arguments(); | ||
245 | } | ||
246 | |||
247 | int validate_arguments(void) { | ||
248 | if (qstat_game_players_max < 0) | ||
249 | qstat_game_players_max = 4; | ||
250 | |||
251 | if (qstat_game_players < 0) | ||
252 | qstat_game_players = 5; | ||
253 | |||
254 | if (qstat_game_field < 0) | ||
255 | qstat_game_field = 2; | ||
256 | |||
257 | if (qstat_map_field < 0) | ||
258 | qstat_map_field = 3; | ||
259 | |||
260 | if (qstat_ping_field < 0) | ||
261 | qstat_ping_field = 5; | ||
262 | 270 | ||
263 | return OK; | 271 | return result; |
264 | } | 272 | } |
265 | 273 | ||
266 | void print_help(void) { | 274 | void print_help(void) { |
@@ -277,14 +285,15 @@ void print_help(void) { | |||
277 | 285 | ||
278 | printf(UT_HELP_VRSN); | 286 | printf(UT_HELP_VRSN); |
279 | printf(UT_EXTRA_OPTS); | 287 | printf(UT_EXTRA_OPTS); |
280 | 288 | printf(" -H, --hostname=ADDRESS\n" | |
281 | printf(" %s\n", "-p"); | 289 | " Host name, IP Address, or unix socket (must be an absolute path)\n"); |
282 | printf(" %s\n", _("Optional port of which to connect")); | 290 | printf(" %s\n", "-P"); |
283 | printf(" %s\n", "gf"); | 291 | printf(" %s\n", _("Optional port to connect to")); |
292 | printf(" %s\n", "-g"); | ||
284 | printf(" %s\n", _("Field number in raw qstat output that contains game name")); | 293 | printf(" %s\n", _("Field number in raw qstat output that contains game name")); |
285 | printf(" %s\n", "-mf"); | 294 | printf(" %s\n", "-m"); |
286 | printf(" %s\n", _("Field number in raw qstat output that contains map name")); | 295 | printf(" %s\n", _("Field number in raw qstat output that contains map name")); |
287 | printf(" %s\n", "-pf"); | 296 | printf(" %s\n", "-p"); |
288 | printf(" %s\n", _("Field number in raw qstat output that contains ping time")); | 297 | printf(" %s\n", _("Field number in raw qstat output that contains ping time")); |
289 | 298 | ||
290 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 299 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
diff --git a/plugins/check_game.d/config.h b/plugins/check_game.d/config.h new file mode 100644 index 00000000..c95a1ced --- /dev/null +++ b/plugins/check_game.d/config.h | |||
@@ -0,0 +1,30 @@ | |||
1 | #pragma once | ||
2 | #include "../../config.h" | ||
3 | #include <stddef.h> | ||
4 | |||
5 | typedef struct { | ||
6 | char *server_ip; | ||
7 | char *game_type; | ||
8 | int port; | ||
9 | |||
10 | int qstat_game_players_max; | ||
11 | int qstat_game_players; | ||
12 | int qstat_game_field; | ||
13 | int qstat_map_field; | ||
14 | int qstat_ping_field; | ||
15 | } check_game_config; | ||
16 | |||
17 | check_game_config check_game_config_init() { | ||
18 | check_game_config tmp = { | ||
19 | .server_ip = NULL, | ||
20 | .game_type = NULL, | ||
21 | .port = 0, | ||
22 | |||
23 | .qstat_game_players_max = 4, | ||
24 | .qstat_game_players = 5, | ||
25 | .qstat_map_field = 3, | ||
26 | .qstat_game_field = 2, | ||
27 | .qstat_ping_field = 5, | ||
28 | }; | ||
29 | return tmp; | ||
30 | } | ||
diff --git a/plugins/utils.c b/plugins/utils.c index 09649429..34335c89 100644 --- a/plugins/utils.c +++ b/plugins/utils.c | |||
@@ -89,41 +89,46 @@ bool is_numeric(char *number) { | |||
89 | char tmp[1]; | 89 | char tmp[1]; |
90 | float x; | 90 | float x; |
91 | 91 | ||
92 | if (!number) | 92 | if (!number) { |
93 | return false; | 93 | return false; |
94 | else if (sscanf(number, "%f%c", &x, tmp) == 1) | 94 | } else if (sscanf(number, "%f%c", &x, tmp) == 1) { |
95 | return true; | 95 | return true; |
96 | else | 96 | } else { |
97 | return false; | 97 | return false; |
98 | } | ||
98 | } | 99 | } |
99 | 100 | ||
100 | bool is_positive(char *number) { | 101 | bool is_positive(char *number) { |
101 | if (is_numeric(number) && atof(number) > 0.0) | 102 | if (is_numeric(number) && atof(number) > 0.0) { |
102 | return true; | 103 | return true; |
103 | else | 104 | } else { |
104 | return false; | 105 | return false; |
106 | } | ||
105 | } | 107 | } |
106 | 108 | ||
107 | bool is_negative(char *number) { | 109 | bool is_negative(char *number) { |
108 | if (is_numeric(number) && atof(number) < 0.0) | 110 | if (is_numeric(number) && atof(number) < 0.0) { |
109 | return true; | 111 | return true; |
110 | else | 112 | } else { |
111 | return false; | 113 | return false; |
114 | } | ||
112 | } | 115 | } |
113 | 116 | ||
114 | bool is_nonnegative(char *number) { | 117 | bool is_nonnegative(char *number) { |
115 | if (is_numeric(number) && atof(number) >= 0.0) | 118 | if (is_numeric(number) && atof(number) >= 0.0) { |
116 | return true; | 119 | return true; |
117 | else | 120 | } else { |
118 | return false; | 121 | return false; |
122 | } | ||
119 | } | 123 | } |
120 | 124 | ||
121 | bool is_percentage(char *number) { | 125 | bool is_percentage(char *number) { |
122 | int x; | 126 | int x; |
123 | if (is_numeric(number) && (x = atof(number)) >= 0 && x <= 100) | 127 | if (is_numeric(number) && (x = atof(number)) >= 0 && x <= 100) { |
124 | return true; | 128 | return true; |
125 | else | 129 | } else { |
126 | return false; | 130 | return false; |
131 | } | ||
127 | } | 132 | } |
128 | 133 | ||
129 | bool is_percentage_expression(const char str[]) { | 134 | bool is_percentage_expression(const char str[]) { |
@@ -156,36 +161,41 @@ bool is_percentage_expression(const char str[]) { | |||
156 | bool is_integer(char *number) { | 161 | bool is_integer(char *number) { |
157 | long int n; | 162 | long int n; |
158 | 163 | ||
159 | if (!number || (strspn(number, "-0123456789 ") != strlen(number))) | 164 | if (!number || (strspn(number, "-0123456789 ") != strlen(number))) { |
160 | return false; | 165 | return false; |
166 | } | ||
161 | 167 | ||
162 | n = strtol(number, NULL, 10); | 168 | n = strtol(number, NULL, 10); |
163 | 169 | ||
164 | if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) | 170 | if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) { |
165 | return true; | 171 | return true; |
166 | else | 172 | } else { |
167 | return false; | 173 | return false; |
174 | } | ||
168 | } | 175 | } |
169 | 176 | ||
170 | bool is_intpos(char *number) { | 177 | bool is_intpos(char *number) { |
171 | if (is_integer(number) && atoi(number) > 0) | 178 | if (is_integer(number) && atoi(number) > 0) { |
172 | return true; | 179 | return true; |
173 | else | 180 | } else { |
174 | return false; | 181 | return false; |
182 | } | ||
175 | } | 183 | } |
176 | 184 | ||
177 | bool is_intneg(char *number) { | 185 | bool is_intneg(char *number) { |
178 | if (is_integer(number) && atoi(number) < 0) | 186 | if (is_integer(number) && atoi(number) < 0) { |
179 | return true; | 187 | return true; |
180 | else | 188 | } else { |
181 | return false; | 189 | return false; |
190 | } | ||
182 | } | 191 | } |
183 | 192 | ||
184 | bool is_intnonneg(char *number) { | 193 | bool is_intnonneg(char *number) { |
185 | if (is_integer(number) && atoi(number) >= 0) | 194 | if (is_integer(number) && atoi(number) >= 0) { |
186 | return true; | 195 | return true; |
187 | else | 196 | } else { |
188 | return false; | 197 | return false; |
198 | } | ||
189 | } | 199 | } |
190 | 200 | ||
191 | /* | 201 | /* |
@@ -247,19 +257,21 @@ bool is_uint64(char *number, uint64_t *target) { | |||
247 | 257 | ||
248 | bool is_intpercent(char *number) { | 258 | bool is_intpercent(char *number) { |
249 | int i; | 259 | int i; |
250 | if (is_integer(number) && (i = atoi(number)) >= 0 && i <= 100) | 260 | if (is_integer(number) && (i = atoi(number)) >= 0 && i <= 100) { |
251 | return true; | 261 | return true; |
252 | else | 262 | } else { |
253 | return false; | 263 | return false; |
264 | } | ||
254 | } | 265 | } |
255 | 266 | ||
256 | bool is_option(char *str) { | 267 | bool is_option(char *str) { |
257 | if (!str) | 268 | if (!str) { |
258 | return false; | 269 | return false; |
259 | else if (strspn(str, "-") == 1 || strspn(str, "-") == 2) | 270 | } else if (strspn(str, "-") == 1 || strspn(str, "-") == 2) { |
260 | return true; | 271 | return true; |
261 | else | 272 | } else { |
262 | return false; | 273 | return false; |
274 | } | ||
263 | } | 275 | } |
264 | 276 | ||
265 | #ifdef NEED_GETTIMEOFDAY | 277 | #ifdef NEED_GETTIMEOFDAY |
@@ -288,10 +300,11 @@ void strip(char *buffer) { | |||
288 | 300 | ||
289 | for (x = strlen(buffer); x >= 1; x--) { | 301 | for (x = strlen(buffer); x >= 1; x--) { |
290 | i = x - 1; | 302 | i = x - 1; |
291 | if (buffer[i] == ' ' || buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t') | 303 | if (buffer[i] == ' ' || buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t') { |
292 | buffer[i] = '\0'; | 304 | buffer[i] = '\0'; |
293 | else | 305 | } else { |
294 | break; | 306 | break; |
307 | } | ||
295 | } | 308 | } |
296 | return; | 309 | return; |
297 | } | 310 | } |
@@ -309,8 +322,9 @@ void strip(char *buffer) { | |||
309 | *****************************************************************************/ | 322 | *****************************************************************************/ |
310 | 323 | ||
311 | char *strscpy(char *dest, const char *src) { | 324 | char *strscpy(char *dest, const char *src) { |
312 | if (src == NULL) | 325 | if (src == NULL) { |
313 | return NULL; | 326 | return NULL; |
327 | } | ||
314 | 328 | ||
315 | xasprintf(&dest, "%s", src); | 329 | xasprintf(&dest, "%s", src); |
316 | 330 | ||
@@ -369,17 +383,21 @@ char *strscpy(char *dest, const char *src) { | |||
369 | 383 | ||
370 | char *strnl(char *str) { | 384 | char *strnl(char *str) { |
371 | size_t len; | 385 | size_t len; |
372 | if (str == NULL) | 386 | if (str == NULL) { |
373 | return NULL; | 387 | return NULL; |
388 | } | ||
374 | str = strpbrk(str, "\r\n"); | 389 | str = strpbrk(str, "\r\n"); |
375 | if (str == NULL) | 390 | if (str == NULL) { |
376 | return NULL; | 391 | return NULL; |
392 | } | ||
377 | len = strspn(str, "\r\n"); | 393 | len = strspn(str, "\r\n"); |
378 | if (str[len] == '\0') | 394 | if (str[len] == '\0') { |
379 | return NULL; | 395 | return NULL; |
396 | } | ||
380 | str += len; | 397 | str += len; |
381 | if (strlen(str) == 0) | 398 | if (strlen(str) == 0) { |
382 | return NULL; | 399 | return NULL; |
400 | } | ||
383 | return str; | 401 | return str; |
384 | } | 402 | } |
385 | 403 | ||
@@ -402,15 +420,18 @@ char *strnl(char *str) { | |||
402 | char *strpcpy(char *dest, const char *src, const char *str) { | 420 | char *strpcpy(char *dest, const char *src, const char *str) { |
403 | size_t len; | 421 | size_t len; |
404 | 422 | ||
405 | if (src) | 423 | if (src) { |
406 | len = strcspn(src, str); | 424 | len = strcspn(src, str); |
407 | else | 425 | } else { |
408 | return NULL; | 426 | return NULL; |
427 | } | ||
409 | 428 | ||
410 | if (dest == NULL || strlen(dest) < len) | 429 | if (dest == NULL || strlen(dest) < len) { |
411 | dest = realloc(dest, len + 1); | 430 | dest = realloc(dest, len + 1); |
412 | if (dest == NULL) | 431 | } |
432 | if (dest == NULL) { | ||
413 | die(STATE_UNKNOWN, _("failed realloc in strpcpy\n")); | 433 | die(STATE_UNKNOWN, _("failed realloc in strpcpy\n")); |
434 | } | ||
414 | 435 | ||
415 | strncpy(dest, src, len); | 436 | strncpy(dest, src, len); |
416 | dest[len] = '\0'; | 437 | dest[len] = '\0'; |
@@ -434,10 +455,11 @@ char *strpcpy(char *dest, const char *src, const char *str) { | |||
434 | char *strpcat(char *dest, const char *src, const char *str) { | 455 | char *strpcat(char *dest, const char *src, const char *str) { |
435 | size_t len, l2; | 456 | size_t len, l2; |
436 | 457 | ||
437 | if (dest) | 458 | if (dest) { |
438 | len = strlen(dest); | 459 | len = strlen(dest); |
439 | else | 460 | } else { |
440 | len = 0; | 461 | len = 0; |
462 | } | ||
441 | 463 | ||
442 | if (src) { | 464 | if (src) { |
443 | l2 = strcspn(src, str); | 465 | l2 = strcspn(src, str); |
@@ -446,8 +468,9 @@ char *strpcat(char *dest, const char *src, const char *str) { | |||
446 | } | 468 | } |
447 | 469 | ||
448 | dest = realloc(dest, len + l2 + 1); | 470 | dest = realloc(dest, len + l2 + 1); |
449 | if (dest == NULL) | 471 | if (dest == NULL) { |
450 | die(STATE_UNKNOWN, _("failed malloc in strscat\n")); | 472 | die(STATE_UNKNOWN, _("failed malloc in strscat\n")); |
473 | } | ||
451 | 474 | ||
452 | strncpy(dest + len, src, l2); | 475 | strncpy(dest + len, src, l2); |
453 | dest[len + l2] = '\0'; | 476 | dest[len + l2] = '\0'; |
@@ -463,8 +486,9 @@ char *strpcat(char *dest, const char *src, const char *str) { | |||
463 | 486 | ||
464 | int xvasprintf(char **strp, const char *fmt, va_list ap) { | 487 | int xvasprintf(char **strp, const char *fmt, va_list ap) { |
465 | int result = vasprintf(strp, fmt, ap); | 488 | int result = vasprintf(strp, fmt, ap); |
466 | if (result == -1 || *strp == NULL) | 489 | if (result == -1 || *strp == NULL) { |
467 | die(STATE_UNKNOWN, _("failed malloc in xvasprintf\n")); | 490 | die(STATE_UNKNOWN, _("failed malloc in xvasprintf\n")); |
491 | } | ||
468 | return result; | 492 | return result; |
469 | } | 493 | } |
470 | 494 | ||
@@ -483,126 +507,145 @@ int xasprintf(char **strp, const char *fmt, ...) { | |||
483 | * | 507 | * |
484 | ******************************************************************************/ | 508 | ******************************************************************************/ |
485 | 509 | ||
486 | char *perfdata(const char *label, long int val, const char *uom, int warnp, long int warn, int critp, long int crit, int minp, | 510 | char *perfdata(const char *label, long int val, const char *uom, bool warnp, long int warn, bool critp, long int crit, bool minp, |
487 | long int minv, int maxp, long int maxv) { | 511 | long int minv, bool maxp, long int maxv) { |
488 | char *data = NULL; | 512 | char *data = NULL; |
489 | 513 | ||
490 | if (strpbrk(label, "'= ")) | 514 | if (strpbrk(label, "'= ")) { |
491 | xasprintf(&data, "'%s'=%ld%s;", label, val, uom); | 515 | xasprintf(&data, "'%s'=%ld%s;", label, val, uom); |
492 | else | 516 | } else { |
493 | xasprintf(&data, "%s=%ld%s;", label, val, uom); | 517 | xasprintf(&data, "%s=%ld%s;", label, val, uom); |
518 | } | ||
494 | 519 | ||
495 | if (warnp) | 520 | if (warnp) { |
496 | xasprintf(&data, "%s%ld;", data, warn); | 521 | xasprintf(&data, "%s%ld;", data, warn); |
497 | else | 522 | } else { |
498 | xasprintf(&data, "%s;", data); | 523 | xasprintf(&data, "%s;", data); |
524 | } | ||
499 | 525 | ||
500 | if (critp) | 526 | if (critp) { |
501 | xasprintf(&data, "%s%ld;", data, crit); | 527 | xasprintf(&data, "%s%ld;", data, crit); |
502 | else | 528 | } else { |
503 | xasprintf(&data, "%s;", data); | 529 | xasprintf(&data, "%s;", data); |
530 | } | ||
504 | 531 | ||
505 | if (minp) | 532 | if (minp) { |
506 | xasprintf(&data, "%s%ld;", data, minv); | 533 | xasprintf(&data, "%s%ld;", data, minv); |
507 | else | 534 | } else { |
508 | xasprintf(&data, "%s;", data); | 535 | xasprintf(&data, "%s;", data); |
536 | } | ||
509 | 537 | ||
510 | if (maxp) | 538 | if (maxp) { |
511 | xasprintf(&data, "%s%ld", data, maxv); | 539 | xasprintf(&data, "%s%ld", data, maxv); |
540 | } | ||
512 | 541 | ||
513 | return data; | 542 | return data; |
514 | } | 543 | } |
515 | 544 | ||
516 | char *perfdata_uint64(const char *label, uint64_t val, const char *uom, int warnp, /* Warning present */ | 545 | char *perfdata_uint64(const char *label, uint64_t val, const char *uom, bool warnp, /* Warning present */ |
517 | uint64_t warn, int critp, /* Critical present */ | 546 | uint64_t warn, bool critp, /* Critical present */ |
518 | uint64_t crit, int minp, /* Minimum present */ | 547 | uint64_t crit, bool minp, /* Minimum present */ |
519 | uint64_t minv, int maxp, /* Maximum present */ | 548 | uint64_t minv, bool maxp, /* Maximum present */ |
520 | uint64_t maxv) { | 549 | uint64_t maxv) { |
521 | char *data = NULL; | 550 | char *data = NULL; |
522 | 551 | ||
523 | if (strpbrk(label, "'= ")) | 552 | if (strpbrk(label, "'= ")) { |
524 | xasprintf(&data, "'%s'=%" PRIu64 "%s;", label, val, uom); | 553 | xasprintf(&data, "'%s'=%" PRIu64 "%s;", label, val, uom); |
525 | else | 554 | } else { |
526 | xasprintf(&data, "%s=%" PRIu64 "%s;", label, val, uom); | 555 | xasprintf(&data, "%s=%" PRIu64 "%s;", label, val, uom); |
556 | } | ||
527 | 557 | ||
528 | if (warnp) | 558 | if (warnp) { |
529 | xasprintf(&data, "%s%" PRIu64 ";", data, warn); | 559 | xasprintf(&data, "%s%" PRIu64 ";", data, warn); |
530 | else | 560 | } else { |
531 | xasprintf(&data, "%s;", data); | 561 | xasprintf(&data, "%s;", data); |
562 | } | ||
532 | 563 | ||
533 | if (critp) | 564 | if (critp) { |
534 | xasprintf(&data, "%s%" PRIu64 ";", data, crit); | 565 | xasprintf(&data, "%s%" PRIu64 ";", data, crit); |
535 | else | 566 | } else { |
536 | xasprintf(&data, "%s;", data); | 567 | xasprintf(&data, "%s;", data); |
568 | } | ||
537 | 569 | ||
538 | if (minp) | 570 | if (minp) { |
539 | xasprintf(&data, "%s%" PRIu64 ";", data, minv); | 571 | xasprintf(&data, "%s%" PRIu64 ";", data, minv); |
540 | else | 572 | } else { |
541 | xasprintf(&data, "%s;", data); | 573 | xasprintf(&data, "%s;", data); |
574 | } | ||
542 | 575 | ||
543 | if (maxp) | 576 | if (maxp) { |
544 | xasprintf(&data, "%s%" PRIu64, data, maxv); | 577 | xasprintf(&data, "%s%" PRIu64, data, maxv); |
578 | } | ||
545 | 579 | ||
546 | return data; | 580 | return data; |
547 | } | 581 | } |
548 | 582 | ||
549 | char *perfdata_int64(const char *label, int64_t val, const char *uom, int warnp, /* Warning present */ | 583 | char *perfdata_int64(const char *label, int64_t val, const char *uom, bool warnp, /* Warning present */ |
550 | int64_t warn, int critp, /* Critical present */ | 584 | int64_t warn, bool critp, /* Critical present */ |
551 | int64_t crit, int minp, /* Minimum present */ | 585 | int64_t crit, bool minp, /* Minimum present */ |
552 | int64_t minv, int maxp, /* Maximum present */ | 586 | int64_t minv, bool maxp, /* Maximum present */ |
553 | int64_t maxv) { | 587 | int64_t maxv) { |
554 | char *data = NULL; | 588 | char *data = NULL; |
555 | 589 | ||
556 | if (strpbrk(label, "'= ")) | 590 | if (strpbrk(label, "'= ")) { |
557 | xasprintf(&data, "'%s'=%" PRId64 "%s;", label, val, uom); | 591 | xasprintf(&data, "'%s'=%" PRId64 "%s;", label, val, uom); |
558 | else | 592 | } else { |
559 | xasprintf(&data, "%s=%" PRId64 "%s;", label, val, uom); | 593 | xasprintf(&data, "%s=%" PRId64 "%s;", label, val, uom); |
594 | } | ||
560 | 595 | ||
561 | if (warnp) | 596 | if (warnp) { |
562 | xasprintf(&data, "%s%" PRId64 ";", data, warn); | 597 | xasprintf(&data, "%s%" PRId64 ";", data, warn); |
563 | else | 598 | } else { |
564 | xasprintf(&data, "%s;", data); | 599 | xasprintf(&data, "%s;", data); |
600 | } | ||
565 | 601 | ||
566 | if (critp) | 602 | if (critp) { |
567 | xasprintf(&data, "%s%" PRId64 ";", data, crit); | 603 | xasprintf(&data, "%s%" PRId64 ";", data, crit); |
568 | else | 604 | } else { |
569 | xasprintf(&data, "%s;", data); | 605 | xasprintf(&data, "%s;", data); |
606 | } | ||
570 | 607 | ||
571 | if (minp) | 608 | if (minp) { |
572 | xasprintf(&data, "%s%" PRId64 ";", data, minv); | 609 | xasprintf(&data, "%s%" PRId64 ";", data, minv); |
573 | else | 610 | } else { |
574 | xasprintf(&data, "%s;", data); | 611 | xasprintf(&data, "%s;", data); |
612 | } | ||
575 | 613 | ||
576 | if (maxp) | 614 | if (maxp) { |
577 | xasprintf(&data, "%s%" PRId64, data, maxv); | 615 | xasprintf(&data, "%s%" PRId64, data, maxv); |
616 | } | ||
578 | 617 | ||
579 | return data; | 618 | return data; |
580 | } | 619 | } |
581 | 620 | ||
582 | char *fperfdata(const char *label, double val, const char *uom, int warnp, double warn, int critp, double crit, int minp, double minv, | 621 | char *fperfdata(const char *label, double val, const char *uom, bool warnp, double warn, bool critp, double crit, bool minp, double minv, |
583 | int maxp, double maxv) { | 622 | bool maxp, double maxv) { |
584 | char *data = NULL; | 623 | char *data = NULL; |
585 | 624 | ||
586 | if (strpbrk(label, "'= ")) | 625 | if (strpbrk(label, "'= ")) { |
587 | xasprintf(&data, "'%s'=", label); | 626 | xasprintf(&data, "'%s'=", label); |
588 | else | 627 | } else { |
589 | xasprintf(&data, "%s=", label); | 628 | xasprintf(&data, "%s=", label); |
629 | } | ||
590 | 630 | ||
591 | xasprintf(&data, "%s%f", data, val); | 631 | xasprintf(&data, "%s%f", data, val); |
592 | xasprintf(&data, "%s%s;", data, uom); | 632 | xasprintf(&data, "%s%s;", data, uom); |
593 | 633 | ||
594 | if (warnp) | 634 | if (warnp) { |
595 | xasprintf(&data, "%s%f", data, warn); | 635 | xasprintf(&data, "%s%f", data, warn); |
636 | } | ||
596 | 637 | ||
597 | xasprintf(&data, "%s;", data); | 638 | xasprintf(&data, "%s;", data); |
598 | 639 | ||
599 | if (critp) | 640 | if (critp) { |
600 | xasprintf(&data, "%s%f", data, crit); | 641 | xasprintf(&data, "%s%f", data, crit); |
642 | } | ||
601 | 643 | ||
602 | xasprintf(&data, "%s;", data); | 644 | xasprintf(&data, "%s;", data); |
603 | 645 | ||
604 | if (minp) | 646 | if (minp) { |
605 | xasprintf(&data, "%s%f", data, minv); | 647 | xasprintf(&data, "%s%f", data, minv); |
648 | } | ||
606 | 649 | ||
607 | if (maxp) { | 650 | if (maxp) { |
608 | xasprintf(&data, "%s;", data); | 651 | xasprintf(&data, "%s;", data); |
@@ -612,28 +655,32 @@ char *fperfdata(const char *label, double val, const char *uom, int warnp, doubl | |||
612 | return data; | 655 | return data; |
613 | } | 656 | } |
614 | 657 | ||
615 | char *sperfdata(const char *label, double val, const char *uom, char *warn, char *crit, int minp, double minv, int maxp, double maxv) { | 658 | char *sperfdata(const char *label, double val, const char *uom, char *warn, char *crit, bool minp, double minv, bool maxp, double maxv) { |
616 | char *data = NULL; | 659 | char *data = NULL; |
617 | if (strpbrk(label, "'= ")) | 660 | if (strpbrk(label, "'= ")) { |
618 | xasprintf(&data, "'%s'=", label); | 661 | xasprintf(&data, "'%s'=", label); |
619 | else | 662 | } else { |
620 | xasprintf(&data, "%s=", label); | 663 | xasprintf(&data, "%s=", label); |
664 | } | ||
621 | 665 | ||
622 | xasprintf(&data, "%s%f", data, val); | 666 | xasprintf(&data, "%s%f", data, val); |
623 | xasprintf(&data, "%s%s;", data, uom); | 667 | xasprintf(&data, "%s%s;", data, uom); |
624 | 668 | ||
625 | if (warn != NULL) | 669 | if (warn != NULL) { |
626 | xasprintf(&data, "%s%s", data, warn); | 670 | xasprintf(&data, "%s%s", data, warn); |
671 | } | ||
627 | 672 | ||
628 | xasprintf(&data, "%s;", data); | 673 | xasprintf(&data, "%s;", data); |
629 | 674 | ||
630 | if (crit != NULL) | 675 | if (crit != NULL) { |
631 | xasprintf(&data, "%s%s", data, crit); | 676 | xasprintf(&data, "%s%s", data, crit); |
677 | } | ||
632 | 678 | ||
633 | xasprintf(&data, "%s;", data); | 679 | xasprintf(&data, "%s;", data); |
634 | 680 | ||
635 | if (minp) | 681 | if (minp) { |
636 | xasprintf(&data, "%s%f", data, minv); | 682 | xasprintf(&data, "%s%f", data, minv); |
683 | } | ||
637 | 684 | ||
638 | if (maxp) { | 685 | if (maxp) { |
639 | xasprintf(&data, "%s;", data); | 686 | xasprintf(&data, "%s;", data); |
@@ -643,28 +690,32 @@ char *sperfdata(const char *label, double val, const char *uom, char *warn, char | |||
643 | return data; | 690 | return data; |
644 | } | 691 | } |
645 | 692 | ||
646 | char *sperfdata_int(const char *label, int val, const char *uom, char *warn, char *crit, int minp, int minv, int maxp, int maxv) { | 693 | char *sperfdata_int(const char *label, int val, const char *uom, char *warn, char *crit, bool minp, int minv, bool maxp, int maxv) { |
647 | char *data = NULL; | 694 | char *data = NULL; |
648 | if (strpbrk(label, "'= ")) | 695 | if (strpbrk(label, "'= ")) { |
649 | xasprintf(&data, "'%s'=", label); | 696 | xasprintf(&data, "'%s'=", label); |
650 | else | 697 | } else { |
651 | xasprintf(&data, "%s=", label); | 698 | xasprintf(&data, "%s=", label); |
699 | } | ||
652 | 700 | ||
653 | xasprintf(&data, "%s%d", data, val); | 701 | xasprintf(&data, "%s%d", data, val); |
654 | xasprintf(&data, "%s%s;", data, uom); | 702 | xasprintf(&data, "%s%s;", data, uom); |
655 | 703 | ||
656 | if (warn != NULL) | 704 | if (warn != NULL) { |
657 | xasprintf(&data, "%s%s", data, warn); | 705 | xasprintf(&data, "%s%s", data, warn); |
706 | } | ||
658 | 707 | ||
659 | xasprintf(&data, "%s;", data); | 708 | xasprintf(&data, "%s;", data); |
660 | 709 | ||
661 | if (crit != NULL) | 710 | if (crit != NULL) { |
662 | xasprintf(&data, "%s%s", data, crit); | 711 | xasprintf(&data, "%s%s", data, crit); |
712 | } | ||
663 | 713 | ||
664 | xasprintf(&data, "%s;", data); | 714 | xasprintf(&data, "%s;", data); |
665 | 715 | ||
666 | if (minp) | 716 | if (minp) { |
667 | xasprintf(&data, "%s%d", data, minv); | 717 | xasprintf(&data, "%s%d", data, minv); |
718 | } | ||
668 | 719 | ||
669 | if (maxp) { | 720 | if (maxp) { |
670 | xasprintf(&data, "%s;", data); | 721 | xasprintf(&data, "%s;", data); |
diff --git a/plugins/utils.h b/plugins/utils.h index 029ae5a6..92a6c115 100644 --- a/plugins/utils.h +++ b/plugins/utils.h | |||
@@ -21,43 +21,43 @@ suite of plugins. */ | |||
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 */ |
24 | #include "extra_opts.h" | 24 | # include "extra_opts.h" |
25 | #else | 25 | #else |
26 | /* else, fake np_extra_opts */ | 26 | /* else, fake np_extra_opts */ |
27 | #define np_extra_opts(acptr,av,pr) av | 27 | # define np_extra_opts(acptr, av, pr) av |
28 | #endif | 28 | #endif |
29 | 29 | ||
30 | /* Standardize version information, termination */ | 30 | /* Standardize version information, termination */ |
31 | 31 | ||
32 | void support (void); | 32 | void support(void); |
33 | void print_revision (const char *, const char *); | 33 | void print_revision(const char *, const char *); |
34 | 34 | ||
35 | extern time_t start_time, end_time; | 35 | extern time_t start_time, end_time; |
36 | 36 | ||
37 | /* Test input types */ | 37 | /* Test input types */ |
38 | 38 | ||
39 | bool is_integer (char *); | 39 | bool is_integer(char *); |
40 | bool is_intpos (char *); | 40 | bool is_intpos(char *); |
41 | bool is_intneg (char *); | 41 | bool is_intneg(char *); |
42 | bool is_intnonneg (char *); | 42 | bool is_intnonneg(char *); |
43 | bool is_intpercent (char *); | 43 | bool is_intpercent(char *); |
44 | bool is_uint64(char *number, uint64_t *target); | 44 | bool is_uint64(char *number, uint64_t *target); |
45 | bool is_int64(char *number, int64_t *target); | 45 | bool is_int64(char *number, int64_t *target); |
46 | 46 | ||
47 | bool is_numeric (char *); | 47 | bool is_numeric(char *); |
48 | bool is_positive (char *); | 48 | bool is_positive(char *); |
49 | bool is_negative (char *); | 49 | bool is_negative(char *); |
50 | bool is_nonnegative (char *); | 50 | bool is_nonnegative(char *); |
51 | bool is_percentage (char *); | 51 | bool is_percentage(char *); |
52 | bool is_percentage_expression (const char[]); | 52 | bool is_percentage_expression(const char[]); |
53 | 53 | ||
54 | bool is_option (char *); | 54 | bool is_option(char *); |
55 | 55 | ||
56 | /* Generalized timer that will do milliseconds if available */ | 56 | /* Generalized timer that will do milliseconds if available */ |
57 | #ifndef HAVE_STRUCT_TIMEVAL | 57 | #ifndef HAVE_STRUCT_TIMEVAL |
58 | struct timeval { | 58 | struct timeval { |
59 | long tv_sec; /* seconds */ | 59 | long tv_sec; /* seconds */ |
60 | long tv_usec; /* microseconds */ | 60 | long tv_usec; /* microseconds */ |
61 | }; | 61 | }; |
62 | #endif | 62 | #endif |
63 | 63 | ||
@@ -65,137 +65,142 @@ struct timeval { | |||
65 | int gettimeofday(struct timeval *, struct timezone *); | 65 | int gettimeofday(struct timeval *, struct timezone *); |
66 | #endif | 66 | #endif |
67 | 67 | ||
68 | double delta_time (struct timeval tv); | 68 | double delta_time(struct timeval tv); |
69 | long deltime (struct timeval tv); | 69 | long deltime(struct timeval tv); |
70 | 70 | ||
71 | /* Handle strings safely */ | 71 | /* Handle strings safely */ |
72 | 72 | ||
73 | void strip (char *); | 73 | void strip(char *); |
74 | char *strscpy (char *, const char *); | 74 | char *strscpy(char *, const char *); |
75 | char *strnl (char *); | 75 | char *strnl(char *); |
76 | char *strpcpy (char *, const char *, const char *); | 76 | char *strpcpy(char *, const char *, const char *); |
77 | char *strpcat (char *, const char *, const char *); | 77 | 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 | void usage (const char *) __attribute__((noreturn)); | 81 | void usage(const char *) __attribute__((noreturn)); |
82 | void usage2(const char *, const char *) __attribute__((noreturn)); | 82 | void usage2(const char *, const char *) __attribute__((noreturn)); |
83 | void usage3(const char *, int) __attribute__((noreturn)); | 83 | void usage3(const char *, int) __attribute__((noreturn)); |
84 | void usage4(const char *) __attribute__((noreturn)); | 84 | void usage4(const char *) __attribute__((noreturn)); |
85 | void usage5(void) __attribute__((noreturn)); | 85 | void usage5(void) __attribute__((noreturn)); |
86 | void usage_va(const char *fmt, ...) __attribute__((noreturn)); | 86 | void usage_va(const char *fmt, ...) __attribute__((noreturn)); |
87 | 87 | ||
88 | #define max(a,b) (((a)>(b))?(a):(b)) | 88 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
89 | #define min(a,b) (((a)<(b))?(a):(b)) | 89 | #define min(a, b) (((a) < (b)) ? (a) : (b)) |
90 | 90 | ||
91 | char *perfdata (const char *, long int, const char *, int, long int, | 91 | char *perfdata(const char *, long int, const char *, bool, long int, bool, long int, bool, long int, bool, long int); |
92 | int, long int, int, long int, int, long int); | ||
93 | 92 | ||
94 | char *perfdata_uint64 (const char *, uint64_t , const char *, int, uint64_t, | 93 | char *perfdata_uint64(const char *, uint64_t, const char *, bool, uint64_t, bool, uint64_t, bool, uint64_t, bool, uint64_t); |
95 | int, uint64_t, int, uint64_t, int, uint64_t); | ||
96 | 94 | ||
97 | char *perfdata_int64 (const char *, int64_t, const char *, int, int64_t, | 95 | char *perfdata_int64(const char *, int64_t, const char *, bool, int64_t, bool, int64_t, bool, int64_t, bool, int64_t); |
98 | int, int64_t, int, int64_t, int, int64_t); | ||
99 | 96 | ||
100 | char *fperfdata (const char *, double, const char *, int, double, | 97 | char *fperfdata(const char *, double, const char *, bool, double, bool, double, bool, double, bool, double); |
101 | int, double, int, double, int, double); | ||
102 | 98 | ||
103 | char *sperfdata (const char *, double, const char *, char *, char *, | 99 | char *sperfdata(const char *, double, const char *, char *, char *, bool, double, bool, double); |
104 | int, double, int, double); | ||
105 | 100 | ||
106 | char *sperfdata_int (const char *, int, const char *, char *, char *, | 101 | char *sperfdata_int(const char *, int, const char *, char *, char *, bool, int, bool, int); |
107 | int, int, int, int); | ||
108 | 102 | ||
109 | /* The idea here is that, although not every plugin will use all of these, | 103 | /* The idea here is that, although not every plugin will use all of these, |
110 | most will or should. Therefore, for consistency, these very common | 104 | most will or should. Therefore, for consistency, these very common |
111 | options should have only these meanings throughout the overall suite */ | 105 | options should have only these meanings throughout the overall suite */ |
112 | 106 | ||
113 | #define STD_LONG_OPTS \ | 107 | #define STD_LONG_OPTS \ |
114 | {"version",no_argument,0,'V'},\ | 108 | {"version", no_argument, 0, 'V'}, {"verbose", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, \ |
115 | {"verbose",no_argument,0,'v'},\ | 109 | {"timeout", required_argument, 0, 't'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, \ |
116 | {"help",no_argument,0,'h'},\ | 110 | {"hostname", required_argument, 0, 'H'} |
117 | {"timeout",required_argument,0,'t'},\ | ||
118 | {"critical",required_argument,0,'c'},\ | ||
119 | {"warning",required_argument,0,'w'},\ | ||
120 | {"hostname",required_argument,0,'H'} | ||
121 | 111 | ||
122 | #define COPYRIGHT "Copyright (c) %s Monitoring Plugins Development Team\n\ | 112 | #define COPYRIGHT \ |
113 | "Copyright (c) %s Monitoring Plugins Development Team\n\ | ||
123 | \t<%s>\n\n" | 114 | \t<%s>\n\n" |
124 | 115 | ||
125 | #define UT_HLP_VRS _("\ | 116 | #define UT_HLP_VRS \ |
117 | _("\ | ||
126 | %s (-h | --help) for detailed help\n\ | 118 | %s (-h | --help) for detailed help\n\ |
127 | %s (-V | --version) for version information\n") | 119 | %s (-V | --version) for version information\n") |
128 | 120 | ||
129 | #define UT_HELP_VRSN _("\ | 121 | #define UT_HELP_VRSN \ |
122 | _("\ | ||
130 | \nOptions:\n\ | 123 | \nOptions:\n\ |
131 | -h, --help\n\ | 124 | -h, --help\n\ |
132 | Print detailed help screen\n\ | 125 | Print detailed help screen\n\ |
133 | -V, --version\n\ | 126 | -V, --version\n\ |
134 | Print version information\n") | 127 | Print version information\n") |
135 | 128 | ||
136 | #define UT_HOST_PORT _("\ | 129 | #define UT_HOST_PORT \ |
130 | _("\ | ||
137 | -H, --hostname=ADDRESS\n\ | 131 | -H, --hostname=ADDRESS\n\ |
138 | Host name, IP Address, or unix socket (must be an absolute path)\n\ | 132 | Host name, IP Address, or unix socket (must be an absolute path)\n\ |
139 | -%c, --port=INTEGER\n\ | 133 | -%c, --port=INTEGER\n\ |
140 | Port number (default: %s)\n") | 134 | Port number (default: %s)\n") |
141 | 135 | ||
142 | #define UT_IPv46 _("\ | 136 | #define UT_IPv46 \ |
137 | _("\ | ||
143 | -4, --use-ipv4\n\ | 138 | -4, --use-ipv4\n\ |
144 | Use IPv4 connection\n\ | 139 | Use IPv4 connection\n\ |
145 | -6, --use-ipv6\n\ | 140 | -6, --use-ipv6\n\ |
146 | Use IPv6 connection\n") | 141 | Use IPv6 connection\n") |
147 | 142 | ||
148 | #define UT_VERBOSE _("\ | 143 | #define UT_VERBOSE \ |
144 | _("\ | ||
149 | -v, --verbose\n\ | 145 | -v, --verbose\n\ |
150 | Show details for command-line debugging (output may be truncated by\n\ | 146 | Show details for command-line debugging (output may be truncated by\n\ |
151 | the monitoring system)\n") | 147 | the monitoring system)\n") |
152 | 148 | ||
153 | #define UT_WARN_CRIT _("\ | 149 | #define UT_WARN_CRIT \ |
150 | _("\ | ||
154 | -w, --warning=DOUBLE\n\ | 151 | -w, --warning=DOUBLE\n\ |
155 | Response time to result in warning status (seconds)\n\ | 152 | Response time to result in warning status (seconds)\n\ |
156 | -c, --critical=DOUBLE\n\ | 153 | -c, --critical=DOUBLE\n\ |
157 | Response time to result in critical status (seconds)\n") | 154 | Response time to result in critical status (seconds)\n") |
158 | 155 | ||
159 | #define UT_WARN_CRIT_RANGE _("\ | 156 | #define UT_WARN_CRIT_RANGE \ |
157 | _("\ | ||
160 | -w, --warning=RANGE\n\ | 158 | -w, --warning=RANGE\n\ |
161 | Warning range (format: start:end). Alert if outside this range\n\ | 159 | Warning range (format: start:end). Alert if outside this range\n\ |
162 | -c, --critical=RANGE\n\ | 160 | -c, --critical=RANGE\n\ |
163 | Critical range\n") | 161 | Critical range\n") |
164 | 162 | ||
165 | #define UT_CONN_TIMEOUT _("\ | 163 | #define UT_CONN_TIMEOUT \ |
164 | _("\ | ||
166 | -t, --timeout=INTEGER\n\ | 165 | -t, --timeout=INTEGER\n\ |
167 | Seconds before connection times out (default: %d)\n") | 166 | Seconds before connection times out (default: %d)\n") |
168 | 167 | ||
169 | #define UT_PLUG_TIMEOUT _("\ | 168 | #define UT_PLUG_TIMEOUT \ |
169 | _("\ | ||
170 | -t, --timeout=INTEGER\n\ | 170 | -t, --timeout=INTEGER\n\ |
171 | Seconds before plugin times out (default: %d)\n") | 171 | Seconds before plugin times out (default: %d)\n") |
172 | 172 | ||
173 | #ifdef NP_EXTRA_OPTS | 173 | #ifdef NP_EXTRA_OPTS |
174 | #define UT_EXTRA_OPTS _("\ | 174 | # define UT_EXTRA_OPTS \ |
175 | _("\ | ||
175 | --extra-opts=[section][@file]\n\ | 176 | --extra-opts=[section][@file]\n\ |
176 | Read options from an ini file. See\n\ | 177 | Read options from an ini file. See\n\ |
177 | https://www.monitoring-plugins.org/doc/extra-opts.html\n\ | 178 | https://www.monitoring-plugins.org/doc/extra-opts.html\n\ |
178 | for usage and examples.\n") | 179 | for usage and examples.\n") |
179 | #else | 180 | #else |
180 | #define UT_EXTRA_OPTS " \b" | 181 | # define UT_EXTRA_OPTS " \b" |
181 | #endif | 182 | #endif |
182 | 183 | ||
183 | #define UT_THRESHOLDS_NOTES _("\ | 184 | #define UT_THRESHOLDS_NOTES \ |
185 | _("\ | ||
184 | See:\n\ | 186 | See:\n\ |
185 | https://www.monitoring-plugins.org/doc/guidelines.html#THRESHOLDFORMAT\n\ | 187 | https://www.monitoring-plugins.org/doc/guidelines.html#THRESHOLDFORMAT\n\ |
186 | for THRESHOLD format and examples.\n") | 188 | for THRESHOLD format and examples.\n") |
187 | 189 | ||
188 | #define UT_SUPPORT _("\n\ | 190 | #define UT_SUPPORT \ |
191 | _("\n\ | ||
189 | Send email to help@monitoring-plugins.org if you have questions regarding\n\ | 192 | Send email to help@monitoring-plugins.org if you have questions regarding\n\ |
190 | use of this software. To submit patches or suggest improvements, send email\n\ | 193 | use of this software. To submit patches or suggest improvements, send email\n\ |
191 | to devel@monitoring-plugins.org\n\n") | 194 | to devel@monitoring-plugins.org\n\n") |
192 | 195 | ||
193 | #define UT_NOWARRANTY _("\n\ | 196 | #define UT_NOWARRANTY \ |
197 | _("\n\ | ||
194 | The Monitoring Plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\ | 198 | The Monitoring Plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\ |
195 | copies of the plugins under the terms of the GNU General Public License.\n\ | 199 | copies of the plugins under the terms of the GNU General Public License.\n\ |
196 | For more information about these matters, see the file named COPYING.\n") | 200 | For more information about these matters, see the file named COPYING.\n") |
197 | 201 | ||
198 | #define UT_OUTPUT_FORMAT _("\ | 202 | #define UT_OUTPUT_FORMAT \ |
203 | _("\ | ||
199 | --output-format=OUTPUT_FORMAT\n\ | 204 | --output-format=OUTPUT_FORMAT\n\ |
200 | Select output format. Valid values: \"multi-line\", \"mp-test-json\"\n") | 205 | Select output format. Valid values: \"multi-line\", \"mp-test-json\"\n") |
201 | 206 | ||