diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-10 21:07:55 +0100 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-10 21:07:55 +0100 |
| commit | 3420d88a9c7436965f22a52790ffb8c793e2990f (patch) | |
| tree | 6c65af59151e64282f9f5925be69162adad994d6 /plugins | |
| parent | 5cf53de34fa410702ca59437cdf3f304366c3c55 (diff) | |
| parent | f930f1745b350903efa42933c04f8a2362d66d74 (diff) | |
| download | monitoring-plugins-3420d88a9c7436965f22a52790ffb8c793e2990f.tar.gz | |
Merge branch 'master' into refactor/check_smtp
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/Makefile.am | 4 | ||||
| -rw-r--r-- | plugins/check_apt.c | 151 | ||||
| -rw-r--r-- | plugins/check_apt.d/config.h | 41 | ||||
| -rw-r--r-- | plugins/check_by_ssh.c | 265 | ||||
| -rw-r--r-- | plugins/check_by_ssh.d/config.h | 56 |
5 files changed, 315 insertions, 202 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 116d45a8..d9269691 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
| @@ -54,7 +54,9 @@ EXTRA_DIST = t \ | |||
| 54 | check_dbi.d \ | 54 | check_dbi.d \ |
| 55 | check_ssh.d \ | 55 | check_ssh.d \ |
| 56 | check_dns.d \ | 56 | check_dns.d \ |
| 57 | check_smtp.d | 57 | check_apt.d \ |
| 58 | check_by_ssh.d \ | ||
| 59 | check_smtp.d | ||
| 58 | 60 | ||
| 59 | PLUGINHDRS = common.h | 61 | PLUGINHDRS = common.h |
| 60 | 62 | ||
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..981f4f42 --- /dev/null +++ b/plugins/check_apt.d/config.h | |||
| @@ -0,0 +1,41 @@ | |||
| 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 | .do_include = NULL, | ||
| 37 | .do_exclude = NULL, | ||
| 38 | .do_critical = NULL, | ||
| 39 | .input_filename = NULL}; | ||
| 40 | return tmp; | ||
| 41 | } | ||
diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c index 2ac7805d..2bc38d49 100644 --- a/plugins/check_by_ssh.c +++ b/plugins/check_by_ssh.c | |||
| @@ -32,48 +32,28 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 32 | 32 | ||
| 33 | #include "common.h" | 33 | #include "common.h" |
| 34 | #include "utils.h" | 34 | #include "utils.h" |
| 35 | #include "netutils.h" | ||
| 36 | #include "utils_cmd.h" | 35 | #include "utils_cmd.h" |
| 36 | #include "check_by_ssh.d/config.h" | ||
| 37 | #include "states.h" | ||
| 37 | 38 | ||
| 38 | #ifndef NP_MAXARGS | 39 | #ifndef NP_MAXARGS |
| 39 | # define NP_MAXARGS 1024 | 40 | # define NP_MAXARGS 1024 |
| 40 | #endif | 41 | #endif |
| 41 | 42 | ||
| 42 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 43 | typedef struct { |
| 43 | static int validate_arguments(void); | 44 | int errorcode; |
| 44 | static void comm_append(const char * /*str*/); | 45 | check_by_ssh_config config; |
| 46 | } check_by_ssh_config_wrapper; | ||
| 47 | static check_by_ssh_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 48 | static check_by_ssh_config_wrapper validate_arguments(check_by_ssh_config_wrapper /*config_wrapper*/); | ||
| 49 | |||
| 50 | static command_construct comm_append(command_construct /*cmd*/, const char * /*str*/); | ||
| 45 | static void print_help(void); | 51 | static void print_help(void); |
| 46 | void print_usage(void); | 52 | void print_usage(void); |
| 47 | 53 | ||
| 48 | static unsigned int commands = 0; | ||
| 49 | static unsigned int services = 0; | ||
| 50 | static int skip_stdout = 0; | ||
| 51 | static int skip_stderr = 0; | ||
| 52 | static int warn_on_stderr = 0; | ||
| 53 | static bool unknown_timeout = false; | ||
| 54 | static char *remotecmd = NULL; | ||
| 55 | static char **commargv = NULL; | ||
| 56 | static int commargc = 0; | ||
| 57 | static char *hostname = NULL; | ||
| 58 | static char *outputfile = NULL; | ||
| 59 | static char *host_shortname = NULL; | ||
| 60 | static char **service; | ||
| 61 | static bool passive = false; | ||
| 62 | static bool verbose = false; | 54 | static bool verbose = false; |
| 63 | 55 | ||
| 64 | int main(int argc, char **argv) { | 56 | int main(int argc, char **argv) { |
| 65 | |||
| 66 | char *status_text; | ||
| 67 | int cresult; | ||
| 68 | int result = STATE_UNKNOWN; | ||
| 69 | time_t local_time; | ||
| 70 | FILE *file_pointer = NULL; | ||
| 71 | output chld_out; | ||
| 72 | output chld_err; | ||
| 73 | |||
| 74 | remotecmd = ""; | ||
| 75 | comm_append(SSH_COMMAND); | ||
| 76 | |||
| 77 | setlocale(LC_ALL, ""); | 57 | setlocale(LC_ALL, ""); |
| 78 | bindtextdomain(PACKAGE, LOCALEDIR); | 58 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 79 | textdomain(PACKAGE); | 59 | textdomain(PACKAGE); |
| @@ -81,11 +61,15 @@ int main(int argc, char **argv) { | |||
| 81 | /* Parse extra opts if any */ | 61 | /* Parse extra opts if any */ |
| 82 | argv = np_extra_opts(&argc, argv, progname); | 62 | argv = np_extra_opts(&argc, argv, progname); |
| 83 | 63 | ||
| 64 | check_by_ssh_config_wrapper tmp_config = process_arguments(argc, argv); | ||
| 65 | |||
| 84 | /* process arguments */ | 66 | /* process arguments */ |
| 85 | if (process_arguments(argc, argv) == ERROR) { | 67 | if (tmp_config.errorcode == ERROR) { |
| 86 | usage_va(_("Could not parse arguments")); | 68 | usage_va(_("Could not parse arguments")); |
| 87 | } | 69 | } |
| 88 | 70 | ||
| 71 | const check_by_ssh_config config = tmp_config.config; | ||
| 72 | |||
| 89 | /* Set signal handling and alarm timeout */ | 73 | /* Set signal handling and alarm timeout */ |
| 90 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { | 74 | if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { |
| 91 | usage_va(_("Cannot catch SIGALRM")); | 75 | usage_va(_("Cannot catch SIGALRM")); |
| @@ -94,16 +78,18 @@ int main(int argc, char **argv) { | |||
| 94 | 78 | ||
| 95 | /* run the command */ | 79 | /* run the command */ |
| 96 | if (verbose) { | 80 | if (verbose) { |
| 97 | printf("Command: %s\n", commargv[0]); | 81 | printf("Command: %s\n", config.cmd.commargv[0]); |
| 98 | for (int i = 1; i < commargc; i++) { | 82 | for (int i = 1; i < config.cmd.commargc; i++) { |
| 99 | printf("Argument %i: %s\n", i, commargv[i]); | 83 | printf("Argument %i: %s\n", i, config.cmd.commargv[i]); |
| 100 | } | 84 | } |
| 101 | } | 85 | } |
| 102 | 86 | ||
| 103 | result = cmd_run_array(commargv, &chld_out, &chld_err, 0); | 87 | output chld_out; |
| 88 | output chld_err; | ||
| 89 | mp_state_enum result = cmd_run_array(config.cmd.commargv, &chld_out, &chld_err, 0); | ||
| 104 | 90 | ||
| 105 | /* SSH returns 255 if connection attempt fails; include the first line of error output */ | 91 | /* SSH returns 255 if connection attempt fails; include the first line of error output */ |
| 106 | if (result == 255 && unknown_timeout) { | 92 | if (result == 255 && config.unknown_timeout) { |
| 107 | printf(_("SSH connection failed: %s\n"), chld_err.lines > 0 ? chld_err.line[0] : "(no error output)"); | 93 | printf(_("SSH connection failed: %s\n"), chld_err.lines > 0 ? chld_err.line[0] : "(no error output)"); |
| 108 | return STATE_UNKNOWN; | 94 | return STATE_UNKNOWN; |
| 109 | } | 95 | } |
| @@ -117,17 +103,24 @@ int main(int argc, char **argv) { | |||
| 117 | } | 103 | } |
| 118 | } | 104 | } |
| 119 | 105 | ||
| 120 | if (skip_stdout == -1) { /* --skip-stdout specified without argument */ | 106 | size_t skip_stdout = 0; |
| 107 | if (config.skip_stdout == -1) { /* --skip-stdout specified without argument */ | ||
| 121 | skip_stdout = chld_out.lines; | 108 | skip_stdout = chld_out.lines; |
| 109 | } else { | ||
| 110 | skip_stdout = config.skip_stdout; | ||
| 122 | } | 111 | } |
| 123 | if (skip_stderr == -1) { /* --skip-stderr specified without argument */ | 112 | |
| 113 | size_t skip_stderr = 0; | ||
| 114 | if (config.skip_stderr == -1) { /* --skip-stderr specified without argument */ | ||
| 124 | skip_stderr = chld_err.lines; | 115 | skip_stderr = chld_err.lines; |
| 116 | } else { | ||
| 117 | skip_stderr = config.skip_stderr; | ||
| 125 | } | 118 | } |
| 126 | 119 | ||
| 127 | /* UNKNOWN or worse if (non-skipped) output found on stderr */ | 120 | /* UNKNOWN or worse if (non-skipped) output found on stderr */ |
| 128 | if (chld_err.lines > (size_t)skip_stderr) { | 121 | if (chld_err.lines > (size_t)skip_stderr) { |
| 129 | printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]); | 122 | printf(_("Remote command execution failed: %s\n"), chld_err.line[skip_stderr]); |
| 130 | if (warn_on_stderr) { | 123 | if (config.warn_on_stderr) { |
| 131 | return max_state_alt(result, STATE_WARNING); | 124 | return max_state_alt(result, STATE_WARNING); |
| 132 | } | 125 | } |
| 133 | return max_state_alt(result, STATE_UNKNOWN); | 126 | return max_state_alt(result, STATE_UNKNOWN); |
| @@ -135,13 +128,13 @@ int main(int argc, char **argv) { | |||
| 135 | 128 | ||
| 136 | /* this is simple if we're not supposed to be passive. | 129 | /* this is simple if we're not supposed to be passive. |
| 137 | * Wrap up quickly and keep the tricks below */ | 130 | * Wrap up quickly and keep the tricks below */ |
| 138 | if (!passive) { | 131 | if (!config.passive) { |
| 139 | if (chld_out.lines > (size_t)skip_stdout) { | 132 | if (chld_out.lines > (size_t)skip_stdout) { |
| 140 | for (size_t i = skip_stdout; i < chld_out.lines; i++) { | 133 | for (size_t i = skip_stdout; i < chld_out.lines; i++) { |
| 141 | puts(chld_out.line[i]); | 134 | puts(chld_out.line[i]); |
| 142 | } | 135 | } |
| 143 | } else { | 136 | } else { |
| 144 | printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), remotecmd, result); | 137 | printf(_("%s - check_by_ssh: Remote command '%s' returned status %d\n"), state_text(result), config.remotecmd, result); |
| 145 | } | 138 | } |
| 146 | return result; /* return error status from remote command */ | 139 | return result; /* return error status from remote command */ |
| 147 | } | 140 | } |
| @@ -151,36 +144,34 @@ int main(int argc, char **argv) { | |||
| 151 | */ | 144 | */ |
| 152 | 145 | ||
| 153 | /* process output */ | 146 | /* process output */ |
| 154 | if (!(file_pointer = fopen(outputfile, "a"))) { | 147 | FILE *file_pointer = NULL; |
| 155 | printf(_("SSH WARNING: could not open %s\n"), outputfile); | 148 | if (!(file_pointer = fopen(config.outputfile, "a"))) { |
| 149 | printf(_("SSH WARNING: could not open %s\n"), config.outputfile); | ||
| 156 | exit(STATE_UNKNOWN); | 150 | exit(STATE_UNKNOWN); |
| 157 | } | 151 | } |
| 158 | 152 | ||
| 159 | local_time = time(NULL); | 153 | time_t local_time = time(NULL); |
| 160 | commands = 0; | 154 | unsigned int commands = 0; |
| 155 | char *status_text; | ||
| 156 | int cresult; | ||
| 161 | for (size_t i = skip_stdout; i < chld_out.lines; i++) { | 157 | for (size_t i = skip_stdout; i < chld_out.lines; i++) { |
| 162 | status_text = chld_out.line[i++]; | 158 | status_text = chld_out.line[i++]; |
| 163 | if (i == chld_out.lines || strstr(chld_out.line[i], "STATUS CODE: ") == NULL) { | 159 | if (i == chld_out.lines || strstr(chld_out.line[i], "STATUS CODE: ") == NULL) { |
| 164 | die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); | 160 | die(STATE_UNKNOWN, _("%s: Error parsing output\n"), progname); |
| 165 | } | 161 | } |
| 166 | 162 | ||
| 167 | if (service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { | 163 | if (config.service[commands] && status_text && sscanf(chld_out.line[i], "STATUS CODE: %d", &cresult) == 1) { |
| 168 | fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, host_shortname, service[commands++], | 164 | fprintf(file_pointer, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, config.host_shortname, |
| 169 | cresult, status_text); | 165 | config.service[commands++], cresult, status_text); |
| 170 | } | 166 | } |
| 171 | } | 167 | } |
| 172 | 168 | ||
| 173 | /* Multiple commands and passive checking should always return OK */ | 169 | /* Multiple commands and passive checking should always return OK */ |
| 174 | return result; | 170 | exit(result); |
| 175 | } | 171 | } |
| 176 | 172 | ||
| 177 | /* process command-line arguments */ | 173 | /* process command-line arguments */ |
| 178 | int process_arguments(int argc, char **argv) { | 174 | check_by_ssh_config_wrapper process_arguments(int argc, char **argv) { |
| 179 | int c; | ||
| 180 | char *p1; | ||
| 181 | char *p2; | ||
| 182 | |||
| 183 | int option = 0; | ||
| 184 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, | 175 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, |
| 185 | {"help", no_argument, 0, 'h'}, | 176 | {"help", no_argument, 0, 'h'}, |
| 186 | {"verbose", no_argument, 0, 'v'}, | 177 | {"verbose", no_argument, 0, 'v'}, |
| @@ -210,24 +201,33 @@ int process_arguments(int argc, char **argv) { | |||
| 210 | {"configfile", optional_argument, 0, 'F'}, | 201 | {"configfile", optional_argument, 0, 'F'}, |
| 211 | {0, 0, 0, 0}}; | 202 | {0, 0, 0, 0}}; |
| 212 | 203 | ||
| 204 | check_by_ssh_config_wrapper result = { | ||
| 205 | .errorcode = OK, | ||
| 206 | .config = check_by_ssh_config_init(), | ||
| 207 | }; | ||
| 208 | |||
| 213 | if (argc < 2) { | 209 | if (argc < 2) { |
| 214 | return ERROR; | 210 | result.errorcode = ERROR; |
| 211 | return result; | ||
| 215 | } | 212 | } |
| 216 | 213 | ||
| 217 | for (c = 1; c < argc; c++) { | 214 | for (int index = 1; index < argc; index++) { |
| 218 | if (strcmp("-to", argv[c]) == 0) { | 215 | if (strcmp("-to", argv[index]) == 0) { |
| 219 | strcpy(argv[c], "-t"); | 216 | strcpy(argv[index], "-t"); |
| 220 | } | 217 | } |
| 221 | } | 218 | } |
| 222 | 219 | ||
| 223 | while (1) { | 220 | result.config.cmd = comm_append(result.config.cmd, SSH_COMMAND); |
| 224 | c = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option); | ||
| 225 | 221 | ||
| 226 | if (c == -1 || c == EOF) { | 222 | int option = 0; |
| 223 | while (true) { | ||
| 224 | int opt_index = getopt_long(argc, argv, "Vvh1246fqt:UH:O:p:i:u:l:C:S::E::n:s:o:F:", longopts, &option); | ||
| 225 | |||
| 226 | if (opt_index == -1 || opt_index == EOF) { | ||
| 227 | break; | 227 | break; |
| 228 | } | 228 | } |
| 229 | 229 | ||
| 230 | switch (c) { | 230 | switch (opt_index) { |
| 231 | case 'V': /* version */ | 231 | case 'V': /* version */ |
| 232 | print_revision(progname, NP_VERSION); | 232 | print_revision(progname, NP_VERSION); |
| 233 | exit(STATE_UNKNOWN); | 233 | exit(STATE_UNKNOWN); |
| @@ -245,169 +245,182 @@ int process_arguments(int argc, char **argv) { | |||
| 245 | } | 245 | } |
| 246 | break; | 246 | break; |
| 247 | case 'U': | 247 | case 'U': |
| 248 | unknown_timeout = true; | 248 | result.config.unknown_timeout = true; |
| 249 | break; | 249 | break; |
| 250 | case 'H': /* host */ | 250 | case 'H': /* host */ |
| 251 | hostname = optarg; | 251 | result.config.hostname = optarg; |
| 252 | break; | 252 | break; |
| 253 | case 'p': /* port number */ | 253 | case 'p': /* port number */ |
| 254 | if (!is_integer(optarg)) { | 254 | if (!is_integer(optarg)) { |
| 255 | usage_va(_("Port must be a positive integer")); | 255 | usage_va(_("Port must be a positive integer")); |
| 256 | } | 256 | } |
| 257 | comm_append("-p"); | 257 | result.config.cmd = comm_append(result.config.cmd, "-p"); |
| 258 | comm_append(optarg); | 258 | result.config.cmd = comm_append(result.config.cmd, optarg); |
| 259 | break; | 259 | break; |
| 260 | case 'O': /* output file */ | 260 | case 'O': /* output file */ |
| 261 | outputfile = optarg; | 261 | result.config.outputfile = optarg; |
| 262 | passive = true; | 262 | result.config.passive = true; |
| 263 | break; | 263 | break; |
| 264 | case 's': /* description of service to check */ | 264 | case 's': /* description of service to check */ { |
| 265 | char *p1; | ||
| 266 | char *p2; | ||
| 267 | |||
| 265 | p1 = optarg; | 268 | p1 = optarg; |
| 266 | service = realloc(service, (++services) * sizeof(char *)); | 269 | result.config.service = realloc(result.config.service, (++result.config.number_of_services) * sizeof(char *)); |
| 267 | while ((p2 = index(p1, ':'))) { | 270 | while ((p2 = index(p1, ':'))) { |
| 268 | *p2 = '\0'; | 271 | *p2 = '\0'; |
| 269 | service[services - 1] = p1; | 272 | result.config.service[result.config.number_of_services - 1] = p1; |
| 270 | service = realloc(service, (++services) * sizeof(char *)); | 273 | result.config.service = realloc(result.config.service, (++result.config.number_of_services) * sizeof(char *)); |
| 271 | p1 = p2 + 1; | 274 | p1 = p2 + 1; |
| 272 | } | 275 | } |
| 273 | service[services - 1] = p1; | 276 | result.config.service[result.config.number_of_services - 1] = p1; |
| 274 | break; | 277 | break; |
| 275 | case 'n': /* short name of host in the monitoring configuration */ | 278 | case 'n': /* short name of host in the monitoring configuration */ |
| 276 | host_shortname = optarg; | 279 | result.config.host_shortname = optarg; |
| 277 | break; | 280 | } break; |
| 278 | |||
| 279 | case 'u': | 281 | case 'u': |
| 280 | comm_append("-l"); | 282 | result.config.cmd = comm_append(result.config.cmd, "-l"); |
| 281 | comm_append(optarg); | 283 | result.config.cmd = comm_append(result.config.cmd, optarg); |
| 282 | break; | 284 | break; |
| 283 | case 'l': /* login name */ | 285 | case 'l': /* login name */ |
| 284 | comm_append("-l"); | 286 | result.config.cmd = comm_append(result.config.cmd, "-l"); |
| 285 | comm_append(optarg); | 287 | result.config.cmd = comm_append(result.config.cmd, optarg); |
| 286 | break; | 288 | break; |
| 287 | case 'i': /* identity */ | 289 | case 'i': /* identity */ |
| 288 | comm_append("-i"); | 290 | result.config.cmd = comm_append(result.config.cmd, "-i"); |
| 289 | comm_append(optarg); | 291 | result.config.cmd = comm_append(result.config.cmd, optarg); |
| 290 | break; | 292 | break; |
| 291 | 293 | ||
| 292 | case '1': /* Pass these switches directly to ssh */ | 294 | case '1': /* Pass these switches directly to ssh */ |
| 293 | comm_append("-1"); | 295 | result.config.cmd = comm_append(result.config.cmd, "-1"); |
| 294 | break; | 296 | break; |
| 295 | case '2': /* 1 to force version 1, 2 to force version 2 */ | 297 | case '2': /* 1 to force version 1, 2 to force version 2 */ |
| 296 | comm_append("-2"); | 298 | result.config.cmd = comm_append(result.config.cmd, "-2"); |
| 297 | break; | 299 | break; |
| 298 | case '4': /* -4 for IPv4 */ | 300 | case '4': /* -4 for IPv4 */ |
| 299 | comm_append("-4"); | 301 | result.config.cmd = comm_append(result.config.cmd, "-4"); |
| 300 | break; | 302 | break; |
| 301 | case '6': /* -6 for IPv6 */ | 303 | case '6': /* -6 for IPv6 */ |
| 302 | comm_append("-6"); | 304 | result.config.cmd = comm_append(result.config.cmd, "-6"); |
| 303 | break; | 305 | break; |
| 304 | case 'f': /* fork to background */ | 306 | case 'f': /* fork to background */ |
| 305 | comm_append("-f"); | 307 | result.config.cmd = comm_append(result.config.cmd, "-f"); |
| 306 | break; | 308 | break; |
| 307 | case 'C': /* Command for remote machine */ | 309 | case 'C': /* Command for remote machine */ |
| 308 | commands++; | 310 | result.config.commands++; |
| 309 | if (commands > 1) { | 311 | if (result.config.commands > 1) { |
| 310 | xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); | 312 | xasprintf(&result.config.remotecmd, "%s;echo STATUS CODE: $?;", result.config.remotecmd); |
| 311 | } | 313 | } |
| 312 | xasprintf(&remotecmd, "%s%s", remotecmd, optarg); | 314 | xasprintf(&result.config.remotecmd, "%s%s", result.config.remotecmd, optarg); |
| 313 | break; | 315 | break; |
| 314 | case 'S': /* skip n (or all) lines on stdout */ | 316 | case 'S': /* skip n (or all) lines on stdout */ |
| 315 | if (optarg == NULL) { | 317 | if (optarg == NULL) { |
| 316 | skip_stdout = -1; /* skip all output on stdout */ | 318 | result.config.skip_stdout = -1; /* skip all output on stdout */ |
| 317 | } else if (!is_integer(optarg)) { | 319 | } else if (!is_integer(optarg)) { |
| 318 | usage_va(_("skip-stdout argument must be an integer")); | 320 | usage_va(_("skip-stdout argument must be an integer")); |
| 319 | } else { | 321 | } else { |
| 320 | skip_stdout = atoi(optarg); | 322 | result.config.skip_stdout = atoi(optarg); |
| 321 | } | 323 | } |
| 322 | break; | 324 | break; |
| 323 | case 'E': /* skip n (or all) lines on stderr */ | 325 | case 'E': /* skip n (or all) lines on stderr */ |
| 324 | if (optarg == NULL) { | 326 | if (optarg == NULL) { |
| 325 | skip_stderr = -1; /* skip all output on stderr */ | 327 | result.config.skip_stderr = -1; /* skip all output on stderr */ |
| 326 | } else if (!is_integer(optarg)) { | 328 | } else if (!is_integer(optarg)) { |
| 327 | usage_va(_("skip-stderr argument must be an integer")); | 329 | usage_va(_("skip-stderr argument must be an integer")); |
| 328 | } else { | 330 | } else { |
| 329 | skip_stderr = atoi(optarg); | 331 | result.config.skip_stderr = atoi(optarg); |
| 330 | } | 332 | } |
| 331 | break; | 333 | break; |
| 332 | case 'W': /* exit with warning if there is an output on stderr */ | 334 | case 'W': /* exit with warning if there is an output on stderr */ |
| 333 | warn_on_stderr = 1; | 335 | result.config.warn_on_stderr = true; |
| 334 | break; | 336 | break; |
| 335 | case 'o': /* Extra options for the ssh command */ | 337 | case 'o': /* Extra options for the ssh command */ |
| 336 | comm_append("-o"); | 338 | result.config.cmd = comm_append(result.config.cmd, "-o"); |
| 337 | comm_append(optarg); | 339 | result.config.cmd = comm_append(result.config.cmd, optarg); |
| 338 | break; | 340 | break; |
| 339 | case 'q': /* Tell the ssh command to be quiet */ | 341 | case 'q': /* Tell the ssh command to be quiet */ |
| 340 | comm_append("-q"); | 342 | result.config.cmd = comm_append(result.config.cmd, "-q"); |
| 341 | break; | 343 | break; |
| 342 | case 'F': /* ssh configfile */ | 344 | case 'F': /* ssh configfile */ |
| 343 | comm_append("-F"); | 345 | result.config.cmd = comm_append(result.config.cmd, "-F"); |
| 344 | comm_append(optarg); | 346 | result.config.cmd = comm_append(result.config.cmd, optarg); |
| 345 | break; | 347 | break; |
| 346 | default: /* help */ | 348 | default: /* help */ |
| 347 | usage5(); | 349 | usage5(); |
| 348 | } | 350 | } |
| 349 | } | 351 | } |
| 350 | 352 | ||
| 351 | c = optind; | 353 | int c = optind; |
| 352 | if (hostname == NULL) { | 354 | if (result.config.hostname == NULL) { |
| 353 | if (c <= argc) { | 355 | if (c <= argc) { |
| 354 | die(STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname); | 356 | die(STATE_UNKNOWN, _("%s: You must provide a host name\n"), progname); |
| 355 | } | 357 | } |
| 356 | hostname = argv[c++]; | 358 | result.config.hostname = argv[c++]; |
| 357 | } | 359 | } |
| 358 | 360 | ||
| 359 | if (strlen(remotecmd) == 0) { | 361 | if (strlen(result.config.remotecmd) == 0) { |
| 360 | for (; c < argc; c++) { | 362 | for (; c < argc; c++) { |
| 361 | if (strlen(remotecmd) > 0) { | 363 | if (strlen(result.config.remotecmd) > 0) { |
| 362 | xasprintf(&remotecmd, "%s %s", remotecmd, argv[c]); | 364 | xasprintf(&result.config.remotecmd, "%s %s", result.config.remotecmd, argv[c]); |
| 363 | } else { | 365 | } else { |
| 364 | xasprintf(&remotecmd, "%s", argv[c]); | 366 | xasprintf(&result.config.remotecmd, "%s", argv[c]); |
| 365 | } | 367 | } |
| 366 | } | 368 | } |
| 367 | } | 369 | } |
| 368 | 370 | ||
| 369 | if (commands > 1 || passive) { | 371 | if (result.config.commands > 1 || result.config.passive) { |
| 370 | xasprintf(&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd); | 372 | xasprintf(&result.config.remotecmd, "%s;echo STATUS CODE: $?;", result.config.remotecmd); |
| 371 | } | 373 | } |
| 372 | 374 | ||
| 373 | if (remotecmd == NULL || strlen(remotecmd) <= 1) { | 375 | if (result.config.remotecmd == NULL || strlen(result.config.remotecmd) <= 1) { |
| 374 | usage_va(_("No remotecmd")); | 376 | usage_va(_("No remotecmd")); |
| 375 | } | 377 | } |
| 376 | 378 | ||
| 377 | comm_append(hostname); | 379 | result.config.cmd = comm_append(result.config.cmd, result.config.hostname); |
| 378 | comm_append(remotecmd); | 380 | result.config.cmd = comm_append(result.config.cmd, result.config.remotecmd); |
| 379 | 381 | ||
| 380 | return validate_arguments(); | 382 | return validate_arguments(result); |
| 381 | } | 383 | } |
| 382 | 384 | ||
| 383 | void comm_append(const char *str) { | 385 | command_construct comm_append(command_construct cmd, const char *str) { |
| 386 | |||
| 387 | if (verbose) { | ||
| 388 | for (int i = 0; i < cmd.commargc; i++) { | ||
| 389 | printf("Current command: [%i] %s\n", i, cmd.commargv[i]); | ||
| 390 | } | ||
| 384 | 391 | ||
| 385 | if (++commargc > NP_MAXARGS) { | 392 | printf("Appending: %s\n", str); |
| 393 | } | ||
| 394 | |||
| 395 | if (++cmd.commargc > NP_MAXARGS) { | ||
| 386 | die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); | 396 | die(STATE_UNKNOWN, _("%s: Argument limit of %d exceeded\n"), progname, NP_MAXARGS); |
| 387 | } | 397 | } |
| 388 | 398 | ||
| 389 | if ((commargv = (char **)realloc(commargv, (commargc + 1) * sizeof(char *))) == NULL) { | 399 | if ((cmd.commargv = (char **)realloc(cmd.commargv, (cmd.commargc + 1) * sizeof(char *))) == NULL) { |
| 390 | die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); | 400 | die(STATE_UNKNOWN, _("Can not (re)allocate 'commargv' buffer\n")); |
| 391 | } | 401 | } |
| 392 | 402 | ||
| 393 | commargv[commargc - 1] = strdup(str); | 403 | cmd.commargv[cmd.commargc - 1] = strdup(str); |
| 394 | commargv[commargc] = NULL; | 404 | cmd.commargv[cmd.commargc] = NULL; |
| 405 | |||
| 406 | return cmd; | ||
| 395 | } | 407 | } |
| 396 | 408 | ||
| 397 | int validate_arguments(void) { | 409 | check_by_ssh_config_wrapper validate_arguments(check_by_ssh_config_wrapper config_wrapper) { |
| 398 | if (remotecmd == NULL || hostname == NULL) { | 410 | if (config_wrapper.config.remotecmd == NULL || config_wrapper.config.hostname == NULL) { |
| 399 | return ERROR; | 411 | config_wrapper.errorcode = ERROR; |
| 412 | return config_wrapper; | ||
| 400 | } | 413 | } |
| 401 | 414 | ||
| 402 | if (passive && commands != services) { | 415 | if (config_wrapper.config.passive && config_wrapper.config.commands != config_wrapper.config.number_of_services) { |
| 403 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname); | 416 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide a service name for each command.\n"), progname); |
| 404 | } | 417 | } |
| 405 | 418 | ||
| 406 | if (passive && host_shortname == NULL) { | 419 | if (config_wrapper.config.passive && config_wrapper.config.host_shortname == NULL) { |
| 407 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname); | 420 | die(STATE_UNKNOWN, _("%s: In passive mode, you must provide the host short name from the monitoring configs.\n"), progname); |
| 408 | } | 421 | } |
| 409 | 422 | ||
| 410 | return OK; | 423 | return config_wrapper; |
| 411 | } | 424 | } |
| 412 | 425 | ||
| 413 | void print_help(void) { | 426 | void print_help(void) { |
diff --git a/plugins/check_by_ssh.d/config.h b/plugins/check_by_ssh.d/config.h new file mode 100644 index 00000000..05435def --- /dev/null +++ b/plugins/check_by_ssh.d/config.h | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "../../config.h" | ||
| 4 | #include <stddef.h> | ||
| 5 | |||
| 6 | typedef struct { | ||
| 7 | int commargc; | ||
| 8 | char **commargv; | ||
| 9 | } command_construct; | ||
| 10 | |||
| 11 | typedef struct { | ||
| 12 | char *hostname; | ||
| 13 | char *host_shortname; | ||
| 14 | |||
| 15 | char **service; | ||
| 16 | unsigned int number_of_services; | ||
| 17 | |||
| 18 | unsigned int commands; // Not needed during actual test run | ||
| 19 | char *remotecmd; | ||
| 20 | |||
| 21 | command_construct cmd; | ||
| 22 | |||
| 23 | bool unknown_timeout; | ||
| 24 | bool warn_on_stderr; | ||
| 25 | int skip_stdout; | ||
| 26 | int skip_stderr; | ||
| 27 | bool passive; | ||
| 28 | char *outputfile; | ||
| 29 | } check_by_ssh_config; | ||
| 30 | |||
| 31 | check_by_ssh_config check_by_ssh_config_init() { | ||
| 32 | check_by_ssh_config tmp = { | ||
| 33 | .hostname = NULL, | ||
| 34 | .host_shortname = NULL, | ||
| 35 | |||
| 36 | .service = NULL, | ||
| 37 | .number_of_services = 0, | ||
| 38 | |||
| 39 | .commands = 0, | ||
| 40 | .remotecmd = "", | ||
| 41 | |||
| 42 | .cmd = | ||
| 43 | { | ||
| 44 | .commargc = 0, | ||
| 45 | .commargv = NULL, | ||
| 46 | }, | ||
| 47 | |||
| 48 | .unknown_timeout = false, | ||
| 49 | .warn_on_stderr = false, | ||
| 50 | .skip_stderr = 0, | ||
| 51 | .skip_stdout = 0, | ||
| 52 | .passive = false, | ||
| 53 | .outputfile = NULL, | ||
| 54 | }; | ||
| 55 | return tmp; | ||
| 56 | } | ||
