diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-11 12:14:41 +0100 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-11 12:14:41 +0100 |
| commit | 8b25eaa0d13a3ef3db063ccdc1cf0021437d7cd7 (patch) | |
| tree | ed677de769cbf830579e879dbc4d4f6e35b60e5c /plugins | |
| parent | 7a518f99a57a6ee169cedd062e4e2e1e7c3fc2d2 (diff) | |
| parent | 74c3f4811ccc10911cdeff5cd793a8f3a7dd74e5 (diff) | |
| download | monitoring-plugins-8b25eaa0d13a3ef3db063ccdc1cf0021437d7cd7.tar.gz | |
Merge branch 'master' into refactor/check_ldap
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/Makefile.am | 4 | ||||
| -rw-r--r-- | plugins/check_fping.c | 284 | ||||
| -rw-r--r-- | plugins/check_fping.d/config.h | 58 | ||||
| -rw-r--r-- | plugins/check_hpjd.c | 174 | ||||
| -rw-r--r-- | plugins/check_hpjd.d/config.h | 25 |
5 files changed, 316 insertions, 229 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 9f7266ad..783cf467 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
| @@ -51,6 +51,7 @@ EXTRA_DIST = t \ | |||
| 51 | $(np_test_scripts) \ | 51 | $(np_test_scripts) \ |
| 52 | check_swap.d \ | 52 | check_swap.d \ |
| 53 | check_ldap.d \ | 53 | check_ldap.d \ |
| 54 | check_hpjd.d \ | ||
| 54 | check_game.d \ | 55 | check_game.d \ |
| 55 | check_dbi.d \ | 56 | check_dbi.d \ |
| 56 | check_ssh.d \ | 57 | check_ssh.d \ |
| @@ -59,7 +60,8 @@ EXTRA_DIST = t \ | |||
| 59 | check_by_ssh.d \ | 60 | check_by_ssh.d \ |
| 60 | check_smtp.d \ | 61 | check_smtp.d \ |
| 61 | check_dig.d \ | 62 | check_dig.d \ |
| 62 | check_cluster.d | 63 | check_cluster.d \ |
| 64 | check_fping.d | ||
| 63 | 65 | ||
| 64 | PLUGINHDRS = common.h | 66 | PLUGINHDRS = common.h |
| 65 | 67 | ||
diff --git a/plugins/check_fping.c b/plugins/check_fping.c index c1d03ece..ec7abb67 100644 --- a/plugins/check_fping.c +++ b/plugins/check_fping.c | |||
| @@ -38,52 +38,29 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 38 | #include "netutils.h" | 38 | #include "netutils.h" |
| 39 | #include "utils.h" | 39 | #include "utils.h" |
| 40 | #include <stdbool.h> | 40 | #include <stdbool.h> |
| 41 | #include "check_fping.d/config.h" | ||
| 42 | #include "states.h" | ||
| 41 | 43 | ||
| 42 | enum { | 44 | enum { |
| 43 | PACKET_COUNT = 1, | ||
| 44 | PACKET_SIZE = 56, | ||
| 45 | PL = 0, | 45 | PL = 0, |
| 46 | RTA = 1 | 46 | RTA = 1 |
| 47 | }; | 47 | }; |
| 48 | 48 | ||
| 49 | static int textscan(char *buf); | 49 | static mp_state_enum textscan(char *buf, const char * /*server_name*/, bool /*crta_p*/, double /*crta*/, bool /*wrta_p*/, double /*wrta*/, |
| 50 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 50 | bool /*cpl_p*/, int /*cpl*/, bool /*wpl_p*/, int /*wpl*/, bool /*alive_p*/); |
| 51 | |||
| 52 | typedef struct { | ||
| 53 | int errorcode; | ||
| 54 | check_fping_config config; | ||
| 55 | } check_fping_config_wrapper; | ||
| 56 | static check_fping_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 51 | static int get_threshold(char *arg, char *rv[2]); | 57 | static int get_threshold(char *arg, char *rv[2]); |
| 52 | static void print_help(void); | 58 | static void print_help(void); |
| 53 | void print_usage(void); | 59 | void print_usage(void); |
| 54 | 60 | ||
| 55 | static char *server_name = NULL; | ||
| 56 | static char *sourceip = NULL; | ||
| 57 | static char *sourceif = NULL; | ||
| 58 | static int packet_size = PACKET_SIZE; | ||
| 59 | static int packet_count = PACKET_COUNT; | ||
| 60 | static int target_timeout = 0; | ||
| 61 | static int packet_interval = 0; | ||
| 62 | static bool verbose = false; | 61 | static bool verbose = false; |
| 63 | static bool dontfrag = false; | ||
| 64 | static bool randomize_packet_data = false; | ||
| 65 | static int cpl; | ||
| 66 | static int wpl; | ||
| 67 | static double crta; | ||
| 68 | static double wrta; | ||
| 69 | static bool cpl_p = false; | ||
| 70 | static bool wpl_p = false; | ||
| 71 | static bool alive_p = false; | ||
| 72 | static bool crta_p = false; | ||
| 73 | static bool wrta_p = false; | ||
| 74 | 62 | ||
| 75 | int main(int argc, char **argv) { | 63 | int main(int argc, char **argv) { |
| 76 | /* normally should be int result = STATE_UNKNOWN; */ | ||
| 77 | |||
| 78 | int status = STATE_UNKNOWN; | ||
| 79 | int result = 0; | ||
| 80 | char *fping_prog = NULL; | ||
| 81 | char *server = NULL; | ||
| 82 | char *command_line = NULL; | ||
| 83 | char *input_buffer = NULL; | ||
| 84 | char *option_string = ""; | ||
| 85 | input_buffer = malloc(MAX_INPUT_BUFFER); | ||
| 86 | |||
| 87 | setlocale(LC_ALL, ""); | 64 | setlocale(LC_ALL, ""); |
| 88 | bindtextdomain(PACKAGE, LOCALEDIR); | 65 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 89 | textdomain(PACKAGE); | 66 | textdomain(PACKAGE); |
| @@ -91,39 +68,54 @@ int main(int argc, char **argv) { | |||
| 91 | /* Parse extra opts if any */ | 68 | /* Parse extra opts if any */ |
| 92 | argv = np_extra_opts(&argc, argv, progname); | 69 | argv = np_extra_opts(&argc, argv, progname); |
| 93 | 70 | ||
| 94 | if (process_arguments(argc, argv) == ERROR) | 71 | check_fping_config_wrapper tmp_config = process_arguments(argc, argv); |
| 72 | if (tmp_config.errorcode == ERROR) { | ||
| 95 | usage4(_("Could not parse arguments")); | 73 | usage4(_("Could not parse arguments")); |
| 74 | } | ||
| 96 | 75 | ||
| 97 | server = strscpy(server, server_name); | 76 | const check_fping_config config = tmp_config.config; |
| 98 | 77 | ||
| 78 | char *server = NULL; | ||
| 79 | server = strscpy(server, config.server_name); | ||
| 80 | |||
| 81 | char *option_string = ""; | ||
| 99 | /* compose the command */ | 82 | /* compose the command */ |
| 100 | if (target_timeout) | 83 | if (config.target_timeout) { |
| 101 | xasprintf(&option_string, "%s-t %d ", option_string, target_timeout); | 84 | xasprintf(&option_string, "%s-t %d ", option_string, config.target_timeout); |
| 102 | if (packet_interval) | 85 | } |
| 103 | xasprintf(&option_string, "%s-p %d ", option_string, packet_interval); | 86 | if (config.packet_interval) { |
| 104 | if (sourceip) | 87 | xasprintf(&option_string, "%s-p %d ", option_string, config.packet_interval); |
| 105 | xasprintf(&option_string, "%s-S %s ", option_string, sourceip); | 88 | } |
| 106 | if (sourceif) | 89 | if (config.sourceip) { |
| 107 | xasprintf(&option_string, "%s-I %s ", option_string, sourceif); | 90 | xasprintf(&option_string, "%s-S %s ", option_string, config.sourceip); |
| 108 | if (dontfrag) | 91 | } |
| 92 | if (config.sourceif) { | ||
| 93 | xasprintf(&option_string, "%s-I %s ", option_string, config.sourceif); | ||
| 94 | } | ||
| 95 | if (config.dontfrag) { | ||
| 109 | xasprintf(&option_string, "%s-M ", option_string); | 96 | xasprintf(&option_string, "%s-M ", option_string); |
| 110 | if (randomize_packet_data) | 97 | } |
| 98 | if (config.randomize_packet_data) { | ||
| 111 | xasprintf(&option_string, "%s-R ", option_string); | 99 | xasprintf(&option_string, "%s-R ", option_string); |
| 100 | } | ||
| 112 | 101 | ||
| 113 | 102 | char *fping_prog = NULL; | |
| 114 | #ifdef PATH_TO_FPING6 | 103 | #ifdef PATH_TO_FPING6 |
| 115 | if (address_family != AF_INET && is_inet6_addr(server)) | 104 | if (address_family != AF_INET && is_inet6_addr(server)) { |
| 116 | fping_prog = strdup(PATH_TO_FPING6); | 105 | fping_prog = strdup(PATH_TO_FPING6); |
| 117 | else | 106 | } else { |
| 118 | fping_prog = strdup(PATH_TO_FPING); | 107 | fping_prog = strdup(PATH_TO_FPING); |
| 108 | } | ||
| 119 | #else | 109 | #else |
| 120 | fping_prog = strdup(PATH_TO_FPING); | 110 | fping_prog = strdup(PATH_TO_FPING); |
| 121 | #endif | 111 | #endif |
| 122 | 112 | ||
| 123 | xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, packet_size, packet_count, server); | 113 | char *command_line = NULL; |
| 114 | xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, config.packet_size, config.packet_count, server); | ||
| 124 | 115 | ||
| 125 | if (verbose) | 116 | if (verbose) { |
| 126 | printf("%s\n", command_line); | 117 | printf("%s\n", command_line); |
| 118 | } | ||
| 127 | 119 | ||
| 128 | /* run the command */ | 120 | /* run the command */ |
| 129 | child_process = spopen(command_line); | 121 | child_process = spopen(command_line); |
| @@ -137,23 +129,29 @@ int main(int argc, char **argv) { | |||
| 137 | printf(_("Could not open stderr for %s\n"), command_line); | 129 | printf(_("Could not open stderr for %s\n"), command_line); |
| 138 | } | 130 | } |
| 139 | 131 | ||
| 132 | char *input_buffer = malloc(MAX_INPUT_BUFFER); | ||
| 133 | mp_state_enum status = STATE_UNKNOWN; | ||
| 140 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | 134 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { |
| 141 | if (verbose) | 135 | if (verbose) { |
| 142 | printf("%s", input_buffer); | 136 | printf("%s", input_buffer); |
| 143 | status = max_state(status, textscan(input_buffer)); | 137 | } |
| 138 | status = max_state(status, textscan(input_buffer, config.server_name, config.crta_p, config.crta, config.wrta_p, config.wrta, | ||
| 139 | config.cpl_p, config.cpl, config.wpl_p, config.wpl, config.alive_p)); | ||
| 144 | } | 140 | } |
| 145 | 141 | ||
| 146 | /* If we get anything on STDERR, at least set warning */ | 142 | /* If we get anything on STDERR, at least set warning */ |
| 147 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | 143 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { |
| 148 | status = max_state(status, STATE_WARNING); | 144 | status = max_state(status, STATE_WARNING); |
| 149 | if (verbose) | 145 | if (verbose) { |
| 150 | printf("%s", input_buffer); | 146 | printf("%s", input_buffer); |
| 151 | status = max_state(status, textscan(input_buffer)); | 147 | } |
| 148 | status = max_state(status, textscan(input_buffer, config.server_name, config.crta_p, config.crta, config.wrta_p, config.wrta, | ||
| 149 | config.cpl_p, config.cpl, config.wpl_p, config.wpl, config.alive_p)); | ||
| 152 | } | 150 | } |
| 153 | (void)fclose(child_stderr); | 151 | (void)fclose(child_stderr); |
| 154 | 152 | ||
| 155 | /* close the pipe */ | 153 | /* close the pipe */ |
| 156 | result = spclose(child_process); | 154 | int result = spclose(child_process); |
| 157 | if (result) { | 155 | if (result) { |
| 158 | /* need to use max_state not max */ | 156 | /* need to use max_state not max */ |
| 159 | status = max_state(status, STATE_WARNING); | 157 | status = max_state(status, STATE_WARNING); |
| @@ -172,21 +170,17 @@ int main(int argc, char **argv) { | |||
| 172 | } | 170 | } |
| 173 | } | 171 | } |
| 174 | 172 | ||
| 175 | printf("FPING %s - %s\n", state_text(status), server_name); | 173 | printf("FPING %s - %s\n", state_text(status), config.server_name); |
| 176 | 174 | ||
| 177 | return status; | 175 | return status; |
| 178 | } | 176 | } |
| 179 | 177 | ||
| 180 | int textscan(char *buf) { | 178 | mp_state_enum textscan(char *buf, const char *server_name, bool crta_p, double crta, bool wrta_p, double wrta, bool cpl_p, int cpl, |
| 181 | char *rtastr = NULL; | 179 | bool wpl_p, int wpl, bool alive_p) { |
| 182 | char *losstr = NULL; | ||
| 183 | char *xmtstr = NULL; | ||
| 184 | double loss; | ||
| 185 | double rta; | ||
| 186 | double xmt; | ||
| 187 | int status = STATE_UNKNOWN; | ||
| 188 | |||
| 189 | /* stops testing after the first successful reply. */ | 180 | /* stops testing after the first successful reply. */ |
| 181 | double rta; | ||
| 182 | double loss; | ||
| 183 | char *rtastr = NULL; | ||
| 190 | if (alive_p && strstr(buf, "avg, 0% loss)")) { | 184 | if (alive_p && strstr(buf, "avg, 0% loss)")) { |
| 191 | rtastr = strstr(buf, "ms ("); | 185 | rtastr = strstr(buf, "ms ("); |
| 192 | rtastr = 1 + index(rtastr, '('); | 186 | rtastr = 1 + index(rtastr, '('); |
| @@ -198,6 +192,10 @@ int textscan(char *buf) { | |||
| 198 | fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0)); | 192 | fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0)); |
| 199 | } | 193 | } |
| 200 | 194 | ||
| 195 | mp_state_enum status = STATE_UNKNOWN; | ||
| 196 | char *xmtstr = NULL; | ||
| 197 | double xmt; | ||
| 198 | char *losstr = NULL; | ||
| 201 | if (strstr(buf, "not found")) { | 199 | if (strstr(buf, "not found")) { |
| 202 | die(STATE_CRITICAL, _("FPING UNKNOWN - %s not found\n"), server_name); | 200 | die(STATE_CRITICAL, _("FPING UNKNOWN - %s not found\n"), server_name); |
| 203 | 201 | ||
| @@ -221,18 +219,19 @@ int textscan(char *buf) { | |||
| 221 | rtastr = 1 + index(rtastr, '/'); | 219 | rtastr = 1 + index(rtastr, '/'); |
| 222 | loss = strtod(losstr, NULL); | 220 | loss = strtod(losstr, NULL); |
| 223 | rta = strtod(rtastr, NULL); | 221 | rta = strtod(rtastr, NULL); |
| 224 | if (cpl_p && loss > cpl) | 222 | if (cpl_p && loss > cpl) { |
| 225 | status = STATE_CRITICAL; | 223 | status = STATE_CRITICAL; |
| 226 | else if (crta_p && rta > crta) | 224 | } else if (crta_p && rta > crta) { |
| 227 | status = STATE_CRITICAL; | 225 | status = STATE_CRITICAL; |
| 228 | else if (wpl_p && loss > wpl) | 226 | } else if (wpl_p && loss > wpl) { |
| 229 | status = STATE_WARNING; | 227 | status = STATE_WARNING; |
| 230 | else if (wrta_p && rta > wrta) | 228 | } else if (wrta_p && rta > wrta) { |
| 231 | status = STATE_WARNING; | 229 | status = STATE_WARNING; |
| 232 | else | 230 | } else { |
| 233 | status = STATE_OK; | 231 | status = STATE_OK; |
| 232 | } | ||
| 234 | die(status, _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), state_text(status), server_name, loss, rta, | 233 | die(status, _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), state_text(status), server_name, loss, rta, |
| 235 | perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100), | 234 | perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, false, 0, false, 0), |
| 236 | fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0)); | 235 | fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0)); |
| 237 | 236 | ||
| 238 | } else if (strstr(buf, "xmt/rcv/%loss")) { | 237 | } else if (strstr(buf, "xmt/rcv/%loss")) { |
| @@ -241,22 +240,24 @@ int textscan(char *buf) { | |||
| 241 | losstr = strstr(buf, "="); | 240 | losstr = strstr(buf, "="); |
| 242 | xmtstr = 1 + losstr; | 241 | xmtstr = 1 + losstr; |
| 243 | xmt = strtod(xmtstr, NULL); | 242 | xmt = strtod(xmtstr, NULL); |
| 244 | if (xmt == 0) | 243 | if (xmt == 0) { |
| 245 | die(STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name); | 244 | die(STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name); |
| 245 | } | ||
| 246 | losstr = 1 + strstr(losstr, "/"); | 246 | losstr = 1 + strstr(losstr, "/"); |
| 247 | losstr = 1 + strstr(losstr, "/"); | 247 | losstr = 1 + strstr(losstr, "/"); |
| 248 | loss = strtod(losstr, NULL); | 248 | loss = strtod(losstr, NULL); |
| 249 | if (atoi(losstr) == 100) | 249 | if (atoi(losstr) == 100) { |
| 250 | status = STATE_CRITICAL; | 250 | status = STATE_CRITICAL; |
| 251 | else if (cpl_p && loss > cpl) | 251 | } else if (cpl_p && loss > cpl) { |
| 252 | status = STATE_CRITICAL; | 252 | status = STATE_CRITICAL; |
| 253 | else if (wpl_p && loss > wpl) | 253 | } else if (wpl_p && loss > wpl) { |
| 254 | status = STATE_WARNING; | 254 | status = STATE_WARNING; |
| 255 | else | 255 | } else { |
| 256 | status = STATE_OK; | 256 | status = STATE_OK; |
| 257 | } | ||
| 257 | /* loss=%.0f%%;%d;%d;0;100 */ | 258 | /* loss=%.0f%%;%d;%d;0;100 */ |
| 258 | die(status, _("FPING %s - %s (loss=%.0f%% )|%s\n"), state_text(status), server_name, loss, | 259 | die(status, _("FPING %s - %s (loss=%.0f%% )|%s\n"), state_text(status), server_name, loss, |
| 259 | perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100)); | 260 | perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, false, 0, false, 0)); |
| 260 | 261 | ||
| 261 | } else { | 262 | } else { |
| 262 | status = max_state(status, STATE_WARNING); | 263 | status = max_state(status, STATE_WARNING); |
| @@ -266,54 +267,50 @@ int textscan(char *buf) { | |||
| 266 | } | 267 | } |
| 267 | 268 | ||
| 268 | /* process command-line arguments */ | 269 | /* process command-line arguments */ |
| 269 | int process_arguments(int argc, char **argv) { | 270 | check_fping_config_wrapper process_arguments(int argc, char **argv) { |
| 270 | int c; | 271 | static struct option longopts[] = { |
| 272 | {"hostname", required_argument, 0, 'H'}, {"sourceip", required_argument, 0, 'S'}, {"sourceif", required_argument, 0, 'I'}, | ||
| 273 | {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"alive", no_argument, 0, 'a'}, | ||
| 274 | {"bytes", required_argument, 0, 'b'}, {"number", required_argument, 0, 'n'}, {"target-timeout", required_argument, 0, 'T'}, | ||
| 275 | {"interval", required_argument, 0, 'i'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, | ||
| 276 | {"help", no_argument, 0, 'h'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, | ||
| 277 | {"dontfrag", no_argument, 0, 'M'}, {"random", no_argument, 0, 'R'}, {0, 0, 0, 0}}; | ||
| 278 | |||
| 271 | char *rv[2]; | 279 | char *rv[2]; |
| 280 | rv[PL] = NULL; | ||
| 281 | rv[RTA] = NULL; | ||
| 272 | 282 | ||
| 273 | int option = 0; | 283 | int option = 0; |
| 274 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, | ||
| 275 | {"sourceip", required_argument, 0, 'S'}, | ||
| 276 | {"sourceif", required_argument, 0, 'I'}, | ||
| 277 | {"critical", required_argument, 0, 'c'}, | ||
| 278 | {"warning", required_argument, 0, 'w'}, | ||
| 279 | {"alive", no_argument, 0, 'a'}, | ||
| 280 | {"bytes", required_argument, 0, 'b'}, | ||
| 281 | {"number", required_argument, 0, 'n'}, | ||
| 282 | {"target-timeout", required_argument, 0, 'T'}, | ||
| 283 | {"interval", required_argument, 0, 'i'}, | ||
| 284 | {"verbose", no_argument, 0, 'v'}, | ||
| 285 | {"version", no_argument, 0, 'V'}, | ||
| 286 | {"help", no_argument, 0, 'h'}, | ||
| 287 | {"use-ipv4", no_argument, 0, '4'}, | ||
| 288 | {"use-ipv6", no_argument, 0, '6'}, | ||
| 289 | {"dontfrag", no_argument, 0, 'M'}, | ||
| 290 | {"random", no_argument, 0, 'R'}, | ||
| 291 | {0, 0, 0, 0}}; | ||
| 292 | 284 | ||
| 293 | rv[PL] = NULL; | 285 | check_fping_config_wrapper result = { |
| 294 | rv[RTA] = NULL; | 286 | .errorcode = OK, |
| 287 | .config = check_fping_config_init(), | ||
| 288 | }; | ||
| 295 | 289 | ||
| 296 | if (argc < 2) | 290 | if (argc < 2) { |
| 297 | return ERROR; | 291 | result.errorcode = ERROR; |
| 292 | return result; | ||
| 293 | } | ||
| 298 | 294 | ||
| 299 | if (!is_option(argv[1])) { | 295 | if (!is_option(argv[1])) { |
| 300 | server_name = argv[1]; | 296 | result.config.server_name = argv[1]; |
| 301 | argv[1] = argv[0]; | 297 | argv[1] = argv[0]; |
| 302 | argv = &argv[1]; | 298 | argv = &argv[1]; |
| 303 | argc--; | 299 | argc--; |
| 304 | } | 300 | } |
| 305 | 301 | ||
| 306 | while (1) { | 302 | while (1) { |
| 307 | c = getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:M:R:46", longopts, &option); | 303 | int option_index = getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:M:R:46", longopts, &option); |
| 308 | 304 | ||
| 309 | if (c == -1 || c == EOF || c == 1) | 305 | if (option_index == -1 || option_index == EOF || option_index == 1) { |
| 310 | break; | 306 | break; |
| 307 | } | ||
| 311 | 308 | ||
| 312 | switch (c) { | 309 | switch (option_index) { |
| 313 | case '?': /* print short usage statement if args not parsable */ | 310 | case '?': /* print short usage statement if args not parsable */ |
| 314 | usage5(); | 311 | usage5(); |
| 315 | case 'a': /* host alive mode */ | 312 | case 'a': /* host alive mode */ |
| 316 | alive_p = true; | 313 | result.config.alive_p = true; |
| 317 | break; | 314 | break; |
| 318 | case 'h': /* help */ | 315 | case 'h': /* help */ |
| 319 | print_help(); | 316 | print_help(); |
| @@ -325,19 +322,19 @@ int process_arguments(int argc, char **argv) { | |||
| 325 | verbose = true; | 322 | verbose = true; |
| 326 | break; | 323 | break; |
| 327 | case 'H': /* hostname */ | 324 | case 'H': /* hostname */ |
| 328 | if (is_host(optarg) == false) { | 325 | if (!is_host(optarg)) { |
| 329 | usage2(_("Invalid hostname/address"), optarg); | 326 | usage2(_("Invalid hostname/address"), optarg); |
| 330 | } | 327 | } |
| 331 | server_name = strscpy(server_name, optarg); | 328 | result.config.server_name = optarg; |
| 332 | break; | 329 | break; |
| 333 | case 'S': /* sourceip */ | 330 | case 'S': /* sourceip */ |
| 334 | if (is_host(optarg) == false) { | 331 | if (!is_host(optarg)) { |
| 335 | usage2(_("Invalid hostname/address"), optarg); | 332 | usage2(_("Invalid hostname/address"), optarg); |
| 336 | } | 333 | } |
| 337 | sourceip = strscpy(sourceip, optarg); | 334 | result.config.sourceip = optarg; |
| 338 | break; | 335 | break; |
| 339 | case 'I': /* sourceip */ | 336 | case 'I': /* sourceip */ |
| 340 | sourceif = strscpy(sourceif, optarg); | 337 | result.config.sourceif = optarg; |
| 341 | break; | 338 | break; |
| 342 | case '4': /* IPv4 only */ | 339 | case '4': /* IPv4 only */ |
| 343 | address_family = AF_INET; | 340 | address_family = AF_INET; |
| @@ -352,82 +349,89 @@ int process_arguments(int argc, char **argv) { | |||
| 352 | case 'c': | 349 | case 'c': |
| 353 | get_threshold(optarg, rv); | 350 | get_threshold(optarg, rv); |
| 354 | if (rv[RTA]) { | 351 | if (rv[RTA]) { |
| 355 | crta = strtod(rv[RTA], NULL); | 352 | result.config.crta = strtod(rv[RTA], NULL); |
| 356 | crta_p = true; | 353 | result.config.crta_p = true; |
| 357 | rv[RTA] = NULL; | 354 | rv[RTA] = NULL; |
| 358 | } | 355 | } |
| 359 | if (rv[PL]) { | 356 | if (rv[PL]) { |
| 360 | cpl = atoi(rv[PL]); | 357 | result.config.cpl = atoi(rv[PL]); |
| 361 | cpl_p = true; | 358 | result.config.cpl_p = true; |
| 362 | rv[PL] = NULL; | 359 | rv[PL] = NULL; |
| 363 | } | 360 | } |
| 364 | break; | 361 | break; |
| 365 | case 'w': | 362 | case 'w': |
| 366 | get_threshold(optarg, rv); | 363 | get_threshold(optarg, rv); |
| 367 | if (rv[RTA]) { | 364 | if (rv[RTA]) { |
| 368 | wrta = strtod(rv[RTA], NULL); | 365 | result.config.wrta = strtod(rv[RTA], NULL); |
| 369 | wrta_p = true; | 366 | result.config.wrta_p = true; |
| 370 | rv[RTA] = NULL; | 367 | rv[RTA] = NULL; |
| 371 | } | 368 | } |
| 372 | if (rv[PL]) { | 369 | if (rv[PL]) { |
| 373 | wpl = atoi(rv[PL]); | 370 | result.config.wpl = atoi(rv[PL]); |
| 374 | wpl_p = true; | 371 | result.config.wpl_p = true; |
| 375 | rv[PL] = NULL; | 372 | rv[PL] = NULL; |
| 376 | } | 373 | } |
| 377 | break; | 374 | break; |
| 378 | case 'b': /* bytes per packet */ | 375 | case 'b': /* bytes per packet */ |
| 379 | if (is_intpos(optarg)) | 376 | if (is_intpos(optarg)) { |
| 380 | packet_size = atoi(optarg); | 377 | result.config.packet_size = atoi(optarg); |
| 381 | else | 378 | } else { |
| 382 | usage(_("Packet size must be a positive integer")); | 379 | usage(_("Packet size must be a positive integer")); |
| 380 | } | ||
| 383 | break; | 381 | break; |
| 384 | case 'n': /* number of packets */ | 382 | case 'n': /* number of packets */ |
| 385 | if (is_intpos(optarg)) | 383 | if (is_intpos(optarg)) { |
| 386 | packet_count = atoi(optarg); | 384 | result.config.packet_count = atoi(optarg); |
| 387 | else | 385 | } else { |
| 388 | usage(_("Packet count must be a positive integer")); | 386 | usage(_("Packet count must be a positive integer")); |
| 387 | } | ||
| 389 | break; | 388 | break; |
| 390 | case 'T': /* timeout in msec */ | 389 | case 'T': /* timeout in msec */ |
| 391 | if (is_intpos(optarg)) | 390 | if (is_intpos(optarg)) { |
| 392 | target_timeout = atoi(optarg); | 391 | result.config.target_timeout = atoi(optarg); |
| 393 | else | 392 | } else { |
| 394 | usage(_("Target timeout must be a positive integer")); | 393 | usage(_("Target timeout must be a positive integer")); |
| 394 | } | ||
| 395 | break; | 395 | break; |
| 396 | case 'i': /* interval in msec */ | 396 | case 'i': /* interval in msec */ |
| 397 | if (is_intpos(optarg)) | 397 | if (is_intpos(optarg)) { |
| 398 | packet_interval = atoi(optarg); | 398 | result.config.packet_interval = atoi(optarg); |
| 399 | else | 399 | } else { |
| 400 | usage(_("Interval must be a positive integer")); | 400 | usage(_("Interval must be a positive integer")); |
| 401 | } | ||
| 401 | break; | 402 | break; |
| 402 | case 'R': | 403 | case 'R': |
| 403 | randomize_packet_data = true; | 404 | result.config.randomize_packet_data = true; |
| 404 | break; | 405 | break; |
| 405 | case 'M': | 406 | case 'M': |
| 406 | dontfrag = true; | 407 | result.config.dontfrag = true; |
| 407 | break; | 408 | break; |
| 408 | } | 409 | } |
| 409 | } | 410 | } |
| 410 | 411 | ||
| 411 | if (server_name == NULL) | 412 | if (result.config.server_name == NULL) { |
| 412 | usage4(_("Hostname was not supplied")); | 413 | usage4(_("Hostname was not supplied")); |
| 414 | } | ||
| 413 | 415 | ||
| 414 | return OK; | 416 | return result; |
| 415 | } | 417 | } |
| 416 | 418 | ||
| 417 | int get_threshold(char *arg, char *rv[2]) { | 419 | int get_threshold(char *arg, char *rv[2]) { |
| 418 | char *arg1 = NULL; | ||
| 419 | char *arg2 = NULL; | 420 | char *arg2 = NULL; |
| 420 | 421 | ||
| 421 | arg1 = strscpy(arg1, arg); | 422 | char *arg1 = strdup(arg); |
| 422 | if (strpbrk(arg1, ",:")) | 423 | if (strpbrk(arg1, ",:")) { |
| 423 | arg2 = 1 + strpbrk(arg1, ",:"); | 424 | arg2 = 1 + strpbrk(arg1, ",:"); |
| 425 | } | ||
| 424 | 426 | ||
| 425 | if (arg2) { | 427 | if (arg2) { |
| 426 | arg1[strcspn(arg1, ",:")] = 0; | 428 | arg1[strcspn(arg1, ",:")] = 0; |
| 427 | if (strstr(arg1, "%") && strstr(arg2, "%")) | 429 | if (strstr(arg1, "%") && strstr(arg2, "%")) { |
| 428 | die(STATE_UNKNOWN, _("%s: Only one threshold may be packet loss (%s)\n"), progname, arg); | 430 | die(STATE_UNKNOWN, _("%s: Only one threshold may be packet loss (%s)\n"), progname, arg); |
| 429 | if (!strstr(arg1, "%") && !strstr(arg2, "%")) | 431 | } |
| 432 | if (!strstr(arg1, "%") && !strstr(arg2, "%")) { | ||
| 430 | die(STATE_UNKNOWN, _("%s: Only one threshold must be packet loss (%s)\n"), progname, arg); | 433 | die(STATE_UNKNOWN, _("%s: Only one threshold must be packet loss (%s)\n"), progname, arg); |
| 434 | } | ||
| 431 | } | 435 | } |
| 432 | 436 | ||
| 433 | if (arg2 && strstr(arg2, "%")) { | 437 | if (arg2 && strstr(arg2, "%")) { |
diff --git a/plugins/check_fping.d/config.h b/plugins/check_fping.d/config.h new file mode 100644 index 00000000..a0697bf3 --- /dev/null +++ b/plugins/check_fping.d/config.h | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "../../config.h" | ||
| 4 | #include <stddef.h> | ||
| 5 | |||
| 6 | enum { | ||
| 7 | PACKET_SIZE = 56, | ||
| 8 | PACKET_COUNT = 1, | ||
| 9 | }; | ||
| 10 | |||
| 11 | typedef struct { | ||
| 12 | char *server_name; | ||
| 13 | char *sourceip; | ||
| 14 | char *sourceif; | ||
| 15 | int packet_size; | ||
| 16 | int packet_count; | ||
| 17 | int target_timeout; | ||
| 18 | int packet_interval; | ||
| 19 | bool randomize_packet_data; | ||
| 20 | bool dontfrag; | ||
| 21 | bool alive_p; | ||
| 22 | |||
| 23 | double crta; | ||
| 24 | bool crta_p; | ||
| 25 | double wrta; | ||
| 26 | bool wrta_p; | ||
| 27 | |||
| 28 | int cpl; | ||
| 29 | bool cpl_p; | ||
| 30 | int wpl; | ||
| 31 | bool wpl_p; | ||
| 32 | } check_fping_config; | ||
| 33 | |||
| 34 | check_fping_config check_fping_config_init() { | ||
| 35 | check_fping_config tmp = { | ||
| 36 | .server_name = NULL, | ||
| 37 | .sourceip = NULL, | ||
| 38 | .sourceif = NULL, | ||
| 39 | .packet_size = PACKET_SIZE, | ||
| 40 | .packet_count = PACKET_COUNT, | ||
| 41 | .target_timeout = 0, | ||
| 42 | .packet_interval = 0, | ||
| 43 | .randomize_packet_data = false, | ||
| 44 | .dontfrag = false, | ||
| 45 | .alive_p = false, | ||
| 46 | |||
| 47 | .crta = 0, | ||
| 48 | .crta_p = false, | ||
| 49 | .wrta = 0, | ||
| 50 | .wrta_p = false, | ||
| 51 | |||
| 52 | .cpl = 0, | ||
| 53 | .cpl_p = false, | ||
| 54 | .wpl = 0, | ||
| 55 | .wpl_p = false, | ||
| 56 | }; | ||
| 57 | return tmp; | ||
| 58 | } | ||
diff --git a/plugins/check_hpjd.c b/plugins/check_hpjd.c index b39bccff..62417fd6 100644 --- a/plugins/check_hpjd.c +++ b/plugins/check_hpjd.c | |||
| @@ -37,9 +37,10 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 37 | #include "popen.h" | 37 | #include "popen.h" |
| 38 | #include "utils.h" | 38 | #include "utils.h" |
| 39 | #include "netutils.h" | 39 | #include "netutils.h" |
| 40 | #include "states.h" | ||
| 41 | #include "check_hpjd.d/config.h" | ||
| 40 | 42 | ||
| 41 | #define DEFAULT_COMMUNITY "public" | 43 | #define DEFAULT_COMMUNITY "public" |
| 42 | #define DEFAULT_PORT "161" | ||
| 43 | 44 | ||
| 44 | #define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1" | 45 | #define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1" |
| 45 | #define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2" | 46 | #define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2" |
| @@ -57,39 +58,15 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 57 | #define ONLINE 0 | 58 | #define ONLINE 0 |
| 58 | #define OFFLINE 1 | 59 | #define OFFLINE 1 |
| 59 | 60 | ||
| 60 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 61 | typedef struct { |
| 61 | static int validate_arguments(void); | 62 | int errorcode; |
| 63 | check_hpjd_config config; | ||
| 64 | } check_hpjd_config_wrapper; | ||
| 65 | static check_hpjd_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 62 | static void print_help(void); | 66 | static void print_help(void); |
| 63 | void print_usage(void); | 67 | void print_usage(void); |
| 64 | 68 | ||
| 65 | static char *community = NULL; | ||
| 66 | static char *address = NULL; | ||
| 67 | static unsigned int port = 0; | ||
| 68 | static int check_paper_out = 1; | ||
| 69 | |||
| 70 | int main(int argc, char **argv) { | 69 | int main(int argc, char **argv) { |
| 71 | char command_line[1024]; | ||
| 72 | int result = STATE_UNKNOWN; | ||
| 73 | int line; | ||
| 74 | char input_buffer[MAX_INPUT_BUFFER]; | ||
| 75 | char query_string[512]; | ||
| 76 | char *errmsg; | ||
| 77 | char *temp_buffer; | ||
| 78 | int line_status = ONLINE; | ||
| 79 | int paper_status = 0; | ||
| 80 | int intervention_required = 0; | ||
| 81 | int peripheral_error = 0; | ||
| 82 | int paper_jam = 0; | ||
| 83 | int paper_out = 0; | ||
| 84 | int toner_low = 0; | ||
| 85 | int page_punt = 0; | ||
| 86 | int memory_out = 0; | ||
| 87 | int door_open = 0; | ||
| 88 | int paper_output = 0; | ||
| 89 | char display_message[MAX_INPUT_BUFFER]; | ||
| 90 | |||
| 91 | errmsg = malloc(MAX_INPUT_BUFFER); | ||
| 92 | |||
| 93 | setlocale(LC_ALL, ""); | 70 | setlocale(LC_ALL, ""); |
| 94 | bindtextdomain(PACKAGE, LOCALEDIR); | 71 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 95 | textdomain(PACKAGE); | 72 | textdomain(PACKAGE); |
| @@ -97,9 +74,15 @@ int main(int argc, char **argv) { | |||
| 97 | /* Parse extra opts if any */ | 74 | /* Parse extra opts if any */ |
| 98 | argv = np_extra_opts(&argc, argv, progname); | 75 | argv = np_extra_opts(&argc, argv, progname); |
| 99 | 76 | ||
| 100 | if (process_arguments(argc, argv) == ERROR) | 77 | check_hpjd_config_wrapper tmp_config = process_arguments(argc, argv); |
| 78 | |||
| 79 | if (tmp_config.errorcode == ERROR) { | ||
| 101 | usage4(_("Could not parse arguments")); | 80 | usage4(_("Could not parse arguments")); |
| 81 | } | ||
| 82 | |||
| 83 | const check_hpjd_config config = tmp_config.config; | ||
| 102 | 84 | ||
| 85 | char query_string[512]; | ||
| 103 | /* removed ' 2>1' at end of command 10/27/1999 - EG */ | 86 | /* removed ' 2>1' at end of command 10/27/1999 - EG */ |
| 104 | /* create the query string */ | 87 | /* create the query string */ |
| 105 | sprintf(query_string, "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0", HPJD_LINE_STATUS, HPJD_PAPER_STATUS, | 88 | sprintf(query_string, "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0", HPJD_LINE_STATUS, HPJD_PAPER_STATUS, |
| @@ -107,7 +90,8 @@ int main(int argc, char **argv) { | |||
| 107 | HPJD_GD_PAGE_PUNT, HPJD_GD_MEMORY_OUT, HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY); | 90 | HPJD_GD_PAGE_PUNT, HPJD_GD_MEMORY_OUT, HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY); |
| 108 | 91 | ||
| 109 | /* get the command to run */ | 92 | /* get the command to run */ |
| 110 | sprintf(command_line, "%s -OQa -m : -v 1 -c %s %s:%u %s", PATH_TO_SNMPGET, community, address, port, query_string); | 93 | char command_line[1024]; |
| 94 | sprintf(command_line, "%s -OQa -m : -v 1 -c %s %s:%u %s", PATH_TO_SNMPGET, config.community, config.address, config.port, query_string); | ||
| 111 | 95 | ||
| 112 | /* run the command */ | 96 | /* run the command */ |
| 113 | child_process = spopen(command_line); | 97 | child_process = spopen(command_line); |
| @@ -121,29 +105,41 @@ int main(int argc, char **argv) { | |||
| 121 | printf(_("Could not open stderr for %s\n"), command_line); | 105 | printf(_("Could not open stderr for %s\n"), command_line); |
| 122 | } | 106 | } |
| 123 | 107 | ||
| 124 | result = STATE_OK; | 108 | mp_state_enum result = STATE_OK; |
| 125 | 109 | ||
| 126 | line = 0; | 110 | int line_status = ONLINE; |
| 127 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | 111 | int paper_status = 0; |
| 112 | int intervention_required = 0; | ||
| 113 | int peripheral_error = 0; | ||
| 114 | int paper_jam = 0; | ||
| 115 | int paper_out = 0; | ||
| 116 | int toner_low = 0; | ||
| 117 | int page_punt = 0; | ||
| 118 | int memory_out = 0; | ||
| 119 | int door_open = 0; | ||
| 120 | int paper_output = 0; | ||
| 121 | char display_message[MAX_INPUT_BUFFER]; | ||
| 128 | 122 | ||
| 123 | char input_buffer[MAX_INPUT_BUFFER]; | ||
| 124 | char *errmsg = malloc(MAX_INPUT_BUFFER); | ||
| 125 | int line = 0; | ||
| 126 | |||
| 127 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
| 129 | /* strip the newline character from the end of the input */ | 128 | /* strip the newline character from the end of the input */ |
| 130 | if (input_buffer[strlen(input_buffer) - 1] == '\n') | 129 | if (input_buffer[strlen(input_buffer) - 1] == '\n') { |
| 131 | input_buffer[strlen(input_buffer) - 1] = 0; | 130 | input_buffer[strlen(input_buffer) - 1] = 0; |
| 131 | } | ||
| 132 | 132 | ||
| 133 | line++; | 133 | line++; |
| 134 | 134 | ||
| 135 | temp_buffer = strtok(input_buffer, "="); | 135 | char *temp_buffer = strtok(input_buffer, "="); |
| 136 | temp_buffer = strtok(NULL, "="); | 136 | temp_buffer = strtok(NULL, "="); |
| 137 | 137 | ||
| 138 | if (temp_buffer == NULL && line < 13) { | 138 | if (temp_buffer == NULL && line < 13) { |
| 139 | |||
| 140 | result = STATE_UNKNOWN; | 139 | result = STATE_UNKNOWN; |
| 141 | strcpy(errmsg, input_buffer); | 140 | strcpy(errmsg, input_buffer); |
| 142 | |||
| 143 | } else { | 141 | } else { |
| 144 | |||
| 145 | switch (line) { | 142 | switch (line) { |
| 146 | |||
| 147 | case 1: /* 1st line should contain the line status */ | 143 | case 1: /* 1st line should contain the line status */ |
| 148 | line_status = atoi(temp_buffer); | 144 | line_status = atoi(temp_buffer); |
| 149 | break; | 145 | break; |
| @@ -186,16 +182,18 @@ int main(int argc, char **argv) { | |||
| 186 | } | 182 | } |
| 187 | 183 | ||
| 188 | /* break out of the read loop if we encounter an error */ | 184 | /* break out of the read loop if we encounter an error */ |
| 189 | if (result != STATE_OK) | 185 | if (result != STATE_OK) { |
| 190 | break; | 186 | break; |
| 187 | } | ||
| 191 | } | 188 | } |
| 192 | 189 | ||
| 193 | /* WARNING if output found on stderr */ | 190 | /* WARNING if output found on stderr */ |
| 194 | if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | 191 | if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { |
| 195 | result = max_state(result, STATE_WARNING); | 192 | result = max_state(result, STATE_WARNING); |
| 196 | /* remove CRLF */ | 193 | /* remove CRLF */ |
| 197 | if (input_buffer[strlen(input_buffer) - 1] == '\n') | 194 | if (input_buffer[strlen(input_buffer) - 1] == '\n') { |
| 198 | input_buffer[strlen(input_buffer) - 1] = 0; | 195 | input_buffer[strlen(input_buffer) - 1] = 0; |
| 196 | } | ||
| 199 | sprintf(errmsg, "%s", input_buffer); | 197 | sprintf(errmsg, "%s", input_buffer); |
| 200 | } | 198 | } |
| 201 | 199 | ||
| @@ -203,15 +201,15 @@ int main(int argc, char **argv) { | |||
| 203 | (void)fclose(child_stderr); | 201 | (void)fclose(child_stderr); |
| 204 | 202 | ||
| 205 | /* close the pipe */ | 203 | /* close the pipe */ |
| 206 | if (spclose(child_process)) | 204 | if (spclose(child_process)) { |
| 207 | result = max_state(result, STATE_WARNING); | 205 | result = max_state(result, STATE_WARNING); |
| 206 | } | ||
| 208 | 207 | ||
| 209 | /* if there wasn't any output, display an error */ | 208 | /* if there wasn't any output, display an error */ |
| 210 | if (line == 0) { | 209 | if (line == 0) { |
| 211 | |||
| 212 | /* might not be the problem, but most likely is. */ | 210 | /* might not be the problem, but most likely is. */ |
| 213 | result = STATE_UNKNOWN; | 211 | result = STATE_UNKNOWN; |
| 214 | xasprintf(&errmsg, "%s : Timeout from host %s\n", errmsg, address); | 212 | xasprintf(&errmsg, "%s : Timeout from host %s\n", errmsg, config.address); |
| 215 | } | 213 | } |
| 216 | 214 | ||
| 217 | /* if we had no read errors, check the printer status results... */ | 215 | /* if we had no read errors, check the printer status results... */ |
| @@ -221,8 +219,9 @@ int main(int argc, char **argv) { | |||
| 221 | result = STATE_WARNING; | 219 | result = STATE_WARNING; |
| 222 | strcpy(errmsg, _("Paper Jam")); | 220 | strcpy(errmsg, _("Paper Jam")); |
| 223 | } else if (paper_out) { | 221 | } else if (paper_out) { |
| 224 | if (check_paper_out) | 222 | if (config.check_paper_out) { |
| 225 | result = STATE_WARNING; | 223 | result = STATE_WARNING; |
| 224 | } | ||
| 226 | strcpy(errmsg, _("Out of Paper")); | 225 | strcpy(errmsg, _("Out of Paper")); |
| 227 | } else if (line_status == OFFLINE) { | 226 | } else if (line_status == OFFLINE) { |
| 228 | if (strcmp(errmsg, "POWERSAVE ON") != 0) { | 227 | if (strcmp(errmsg, "POWERSAVE ON") != 0) { |
| @@ -256,29 +255,23 @@ int main(int argc, char **argv) { | |||
| 256 | } | 255 | } |
| 257 | } | 256 | } |
| 258 | 257 | ||
| 259 | if (result == STATE_OK) | 258 | if (result == STATE_OK) { |
| 260 | printf(_("Printer ok - (%s)\n"), display_message); | 259 | printf(_("Printer ok - (%s)\n"), display_message); |
| 261 | 260 | } else if (result == STATE_UNKNOWN) { | |
| 262 | else if (result == STATE_UNKNOWN) { | ||
| 263 | |||
| 264 | printf("%s\n", errmsg); | 261 | printf("%s\n", errmsg); |
| 265 | |||
| 266 | /* if printer could not be reached, escalate to critical */ | 262 | /* if printer could not be reached, escalate to critical */ |
| 267 | if (strstr(errmsg, "Timeout")) | 263 | if (strstr(errmsg, "Timeout")) { |
| 268 | result = STATE_CRITICAL; | 264 | result = STATE_CRITICAL; |
| 269 | } | 265 | } |
| 270 | 266 | } else if (result == STATE_WARNING) { | |
| 271 | else if (result == STATE_WARNING) | ||
| 272 | printf("%s (%s)\n", errmsg, display_message); | 267 | printf("%s (%s)\n", errmsg, display_message); |
| 268 | } | ||
| 273 | 269 | ||
| 274 | return result; | 270 | exit(result); |
| 275 | } | 271 | } |
| 276 | 272 | ||
| 277 | /* process command-line arguments */ | 273 | /* process command-line arguments */ |
| 278 | int process_arguments(int argc, char **argv) { | 274 | check_hpjd_config_wrapper process_arguments(int argc, char **argv) { |
| 279 | int c; | ||
| 280 | |||
| 281 | int option = 0; | ||
| 282 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, | 275 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
| 283 | {"community", required_argument, 0, 'C'}, | 276 | {"community", required_argument, 0, 'C'}, |
| 284 | /* {"critical", required_argument,0,'c'}, */ | 277 | /* {"critical", required_argument,0,'c'}, */ |
| @@ -288,34 +281,44 @@ int process_arguments(int argc, char **argv) { | |||
| 288 | {"help", no_argument, 0, 'h'}, | 281 | {"help", no_argument, 0, 'h'}, |
| 289 | {0, 0, 0, 0}}; | 282 | {0, 0, 0, 0}}; |
| 290 | 283 | ||
| 291 | if (argc < 2) | 284 | check_hpjd_config_wrapper result = { |
| 292 | return ERROR; | 285 | .errorcode = OK, |
| 286 | .config = check_hpjd_config_init(), | ||
| 287 | }; | ||
| 288 | |||
| 289 | if (argc < 2) { | ||
| 290 | result.errorcode = ERROR; | ||
| 291 | return result; | ||
| 292 | } | ||
| 293 | 293 | ||
| 294 | while (1) { | 294 | int option = 0; |
| 295 | c = getopt_long(argc, argv, "+hVH:C:p:D", longopts, &option); | 295 | while (true) { |
| 296 | int option_index = getopt_long(argc, argv, "+hVH:C:p:D", longopts, &option); | ||
| 296 | 297 | ||
| 297 | if (c == -1 || c == EOF || c == 1) | 298 | if (option_index == -1 || option_index == EOF || option_index == 1) { |
| 298 | break; | 299 | break; |
| 300 | } | ||
| 299 | 301 | ||
| 300 | switch (c) { | 302 | switch (option_index) { |
| 301 | case 'H': /* hostname */ | 303 | case 'H': /* hostname */ |
| 302 | if (is_host(optarg)) { | 304 | if (is_host(optarg)) { |
| 303 | address = strscpy(address, optarg); | 305 | result.config.address = strscpy(result.config.address, optarg); |
| 304 | } else { | 306 | } else { |
| 305 | usage2(_("Invalid hostname/address"), optarg); | 307 | usage2(_("Invalid hostname/address"), optarg); |
| 306 | } | 308 | } |
| 307 | break; | 309 | break; |
| 308 | case 'C': /* community */ | 310 | case 'C': /* community */ |
| 309 | community = strscpy(community, optarg); | 311 | result.config.community = strscpy(result.config.community, optarg); |
| 310 | break; | 312 | break; |
| 311 | case 'p': | 313 | case 'p': |
| 312 | if (!is_intpos(optarg)) | 314 | if (!is_intpos(optarg)) { |
| 313 | usage2(_("Port must be a positive short integer"), optarg); | 315 | usage2(_("Port must be a positive short integer"), optarg); |
| 314 | else | 316 | } else { |
| 315 | port = atoi(optarg); | 317 | result.config.port = atoi(optarg); |
| 318 | } | ||
| 316 | break; | 319 | break; |
| 317 | case 'D': /* disable paper out check*/ | 320 | case 'D': /* disable paper out check*/ |
| 318 | check_paper_out = 0; | 321 | result.config.check_paper_out = false; |
| 319 | break; | 322 | break; |
| 320 | case 'V': /* version */ | 323 | case 'V': /* version */ |
| 321 | print_revision(progname, NP_VERSION); | 324 | print_revision(progname, NP_VERSION); |
| @@ -328,31 +331,26 @@ int process_arguments(int argc, char **argv) { | |||
| 328 | } | 331 | } |
| 329 | } | 332 | } |
| 330 | 333 | ||
| 331 | c = optind; | 334 | int c = optind; |
| 332 | if (address == NULL) { | 335 | if (result.config.address == NULL) { |
| 333 | if (is_host(argv[c])) { | 336 | if (is_host(argv[c])) { |
| 334 | address = argv[c++]; | 337 | result.config.address = argv[c++]; |
| 335 | } else { | 338 | } else { |
| 336 | usage2(_("Invalid hostname/address"), argv[c]); | 339 | usage2(_("Invalid hostname/address"), argv[c]); |
| 337 | } | 340 | } |
| 338 | } | 341 | } |
| 339 | 342 | ||
| 340 | if (community == NULL) { | 343 | if (result.config.community == NULL) { |
| 341 | if (argv[c] != NULL) | 344 | if (argv[c] != NULL) { |
| 342 | community = argv[c]; | 345 | result.config.community = argv[c]; |
| 343 | else | 346 | } else { |
| 344 | community = strdup(DEFAULT_COMMUNITY); | 347 | result.config.community = strdup(DEFAULT_COMMUNITY); |
| 345 | } | 348 | } |
| 346 | |||
| 347 | if (port == 0) { | ||
| 348 | port = atoi(DEFAULT_PORT); | ||
| 349 | } | 349 | } |
| 350 | 350 | ||
| 351 | return validate_arguments(); | 351 | return result; |
| 352 | } | 352 | } |
| 353 | 353 | ||
| 354 | int validate_arguments(void) { return OK; } | ||
| 355 | |||
| 356 | void print_help(void) { | 354 | void print_help(void) { |
| 357 | print_revision(progname, NP_VERSION); | 355 | print_revision(progname, NP_VERSION); |
| 358 | 356 | ||
diff --git a/plugins/check_hpjd.d/config.h b/plugins/check_hpjd.d/config.h new file mode 100644 index 00000000..e36b7972 --- /dev/null +++ b/plugins/check_hpjd.d/config.h | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "../../config.h" | ||
| 4 | #include <stddef.h> | ||
| 5 | #include <stdlib.h> | ||
| 6 | |||
| 7 | #define DEFAULT_PORT "161" | ||
| 8 | |||
| 9 | typedef struct { | ||
| 10 | char *address; | ||
| 11 | char *community; | ||
| 12 | unsigned int port; | ||
| 13 | bool check_paper_out; | ||
| 14 | |||
| 15 | } check_hpjd_config; | ||
| 16 | |||
| 17 | check_hpjd_config check_hpjd_config_init() { | ||
| 18 | check_hpjd_config tmp = { | ||
| 19 | .address = NULL, | ||
| 20 | .community = NULL, | ||
| 21 | .port = (unsigned int)atoi(DEFAULT_PORT), | ||
| 22 | .check_paper_out = true, | ||
| 23 | }; | ||
| 24 | return tmp; | ||
| 25 | } | ||
