diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-11 12:12:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-11 12:12:54 +0100 |
| commit | 74c3f4811ccc10911cdeff5cd793a8f3a7dd74e5 (patch) | |
| tree | c81e165e03a601c6976a11e25ef7797b791ab438 | |
| parent | abd6295439dac66ca4d5dd280d4a6528cdfe973d (diff) | |
| parent | b8460603aaa0d707015733ee0d7d40b1949d40b9 (diff) | |
| download | monitoring-plugins-74c3f481.tar.gz | |
Merge pull request #2084 from RincewindsHat/refactor/check_fping
Refactor/check fping
| -rw-r--r-- | plugins/Makefile.am | 3 | ||||
| -rw-r--r-- | plugins/check_fping.c | 284 | ||||
| -rw-r--r-- | plugins/check_fping.d/config.h | 58 |
3 files changed, 204 insertions, 141 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 1b40b91c..9d310a15 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
| @@ -59,7 +59,8 @@ EXTRA_DIST = t \ | |||
| 59 | check_by_ssh.d \ | 59 | check_by_ssh.d \ |
| 60 | check_smtp.d \ | 60 | check_smtp.d \ |
| 61 | check_dig.d \ | 61 | check_dig.d \ |
| 62 | check_cluster.d | 62 | check_cluster.d \ |
| 63 | check_fping.d | ||
| 63 | 64 | ||
| 64 | PLUGINHDRS = common.h | 65 | PLUGINHDRS = common.h |
| 65 | 66 | ||
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 | } | ||
