From 4f4966220bbdf18cd1d525d0ef53faef708ce2bb Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 11 Mar 2025 02:05:13 +0100 Subject: check_fping: clang-format --- plugins/check_fping.c | 122 +++++++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 55 deletions(-) (limited to 'plugins/check_fping.c') diff --git a/plugins/check_fping.c b/plugins/check_fping.c index c1d03ece..b85397d5 100644 --- a/plugins/check_fping.c +++ b/plugins/check_fping.c @@ -91,39 +91,47 @@ int main(int argc, char **argv) { /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - if (process_arguments(argc, argv) == ERROR) + if (process_arguments(argc, argv) == ERROR) { usage4(_("Could not parse arguments")); + } server = strscpy(server, server_name); /* compose the command */ - if (target_timeout) + if (target_timeout) { xasprintf(&option_string, "%s-t %d ", option_string, target_timeout); - if (packet_interval) + } + if (packet_interval) { xasprintf(&option_string, "%s-p %d ", option_string, packet_interval); - if (sourceip) + } + if (sourceip) { xasprintf(&option_string, "%s-S %s ", option_string, sourceip); - if (sourceif) + } + if (sourceif) { xasprintf(&option_string, "%s-I %s ", option_string, sourceif); - if (dontfrag) + } + if (dontfrag) { xasprintf(&option_string, "%s-M ", option_string); - if (randomize_packet_data) + } + if (randomize_packet_data) { xasprintf(&option_string, "%s-R ", option_string); - + } #ifdef PATH_TO_FPING6 - if (address_family != AF_INET && is_inet6_addr(server)) + if (address_family != AF_INET && is_inet6_addr(server)) { fping_prog = strdup(PATH_TO_FPING6); - else + } else { fping_prog = strdup(PATH_TO_FPING); + } #else fping_prog = strdup(PATH_TO_FPING); #endif xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, packet_size, packet_count, server); - if (verbose) + if (verbose) { printf("%s\n", command_line); + } /* run the command */ child_process = spopen(command_line); @@ -138,16 +146,18 @@ int main(int argc, char **argv) { } while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { - if (verbose) + if (verbose) { printf("%s", input_buffer); + } status = max_state(status, textscan(input_buffer)); } /* If we get anything on STDERR, at least set warning */ while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { status = max_state(status, STATE_WARNING); - if (verbose) + if (verbose) { printf("%s", input_buffer); + } status = max_state(status, textscan(input_buffer)); } (void)fclose(child_stderr); @@ -221,16 +231,17 @@ int textscan(char *buf) { rtastr = 1 + index(rtastr, '/'); loss = strtod(losstr, NULL); rta = strtod(rtastr, NULL); - if (cpl_p && loss > cpl) + if (cpl_p && loss > cpl) { status = STATE_CRITICAL; - else if (crta_p && rta > crta) + } else if (crta_p && rta > crta) { status = STATE_CRITICAL; - else if (wpl_p && loss > wpl) + } else if (wpl_p && loss > wpl) { status = STATE_WARNING; - else if (wrta_p && rta > wrta) + } else if (wrta_p && rta > wrta) { status = STATE_WARNING; - else + } else { status = STATE_OK; + } die(status, _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), state_text(status), server_name, loss, rta, perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100), fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0)); @@ -241,19 +252,21 @@ int textscan(char *buf) { losstr = strstr(buf, "="); xmtstr = 1 + losstr; xmt = strtod(xmtstr, NULL); - if (xmt == 0) + if (xmt == 0) { die(STATE_CRITICAL, _("FPING CRITICAL - %s is down\n"), server_name); + } losstr = 1 + strstr(losstr, "/"); losstr = 1 + strstr(losstr, "/"); loss = strtod(losstr, NULL); - if (atoi(losstr) == 100) + if (atoi(losstr) == 100) { status = STATE_CRITICAL; - else if (cpl_p && loss > cpl) + } else if (cpl_p && loss > cpl) { status = STATE_CRITICAL; - else if (wpl_p && loss > wpl) + } else if (wpl_p && loss > wpl) { status = STATE_WARNING; - else + } else { status = STATE_OK; + } /* loss=%.0f%%;%d;%d;0;100 */ die(status, _("FPING %s - %s (loss=%.0f%% )|%s\n"), state_text(status), server_name, loss, perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100)); @@ -271,30 +284,20 @@ int process_arguments(int argc, char **argv) { char *rv[2]; int option = 0; - static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, - {"sourceip", required_argument, 0, 'S'}, - {"sourceif", required_argument, 0, 'I'}, - {"critical", required_argument, 0, 'c'}, - {"warning", required_argument, 0, 'w'}, - {"alive", no_argument, 0, 'a'}, - {"bytes", required_argument, 0, 'b'}, - {"number", required_argument, 0, 'n'}, - {"target-timeout", required_argument, 0, 'T'}, - {"interval", required_argument, 0, 'i'}, - {"verbose", no_argument, 0, 'v'}, - {"version", no_argument, 0, 'V'}, - {"help", no_argument, 0, 'h'}, - {"use-ipv4", no_argument, 0, '4'}, - {"use-ipv6", no_argument, 0, '6'}, - {"dontfrag", no_argument, 0, 'M'}, - {"random", no_argument, 0, 'R'}, - {0, 0, 0, 0}}; + static struct option longopts[] = { + {"hostname", required_argument, 0, 'H'}, {"sourceip", required_argument, 0, 'S'}, {"sourceif", required_argument, 0, 'I'}, + {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"alive", no_argument, 0, 'a'}, + {"bytes", required_argument, 0, 'b'}, {"number", required_argument, 0, 'n'}, {"target-timeout", required_argument, 0, 'T'}, + {"interval", required_argument, 0, 'i'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, + {"help", no_argument, 0, 'h'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, + {"dontfrag", no_argument, 0, 'M'}, {"random", no_argument, 0, 'R'}, {0, 0, 0, 0}}; rv[PL] = NULL; rv[RTA] = NULL; - if (argc < 2) + if (argc < 2) { return ERROR; + } if (!is_option(argv[1])) { server_name = argv[1]; @@ -306,8 +309,9 @@ int process_arguments(int argc, char **argv) { while (1) { c = getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:M:R:46", longopts, &option); - if (c == -1 || c == EOF || c == 1) + if (c == -1 || c == EOF || c == 1) { break; + } switch (c) { case '?': /* print short usage statement if args not parsable */ @@ -376,28 +380,32 @@ int process_arguments(int argc, char **argv) { } break; case 'b': /* bytes per packet */ - if (is_intpos(optarg)) + if (is_intpos(optarg)) { packet_size = atoi(optarg); - else + } else { usage(_("Packet size must be a positive integer")); + } break; case 'n': /* number of packets */ - if (is_intpos(optarg)) + if (is_intpos(optarg)) { packet_count = atoi(optarg); - else + } else { usage(_("Packet count must be a positive integer")); + } break; case 'T': /* timeout in msec */ - if (is_intpos(optarg)) + if (is_intpos(optarg)) { target_timeout = atoi(optarg); - else + } else { usage(_("Target timeout must be a positive integer")); + } break; case 'i': /* interval in msec */ - if (is_intpos(optarg)) + if (is_intpos(optarg)) { packet_interval = atoi(optarg); - else + } else { usage(_("Interval must be a positive integer")); + } break; case 'R': randomize_packet_data = true; @@ -408,8 +416,9 @@ int process_arguments(int argc, char **argv) { } } - if (server_name == NULL) + if (server_name == NULL) { usage4(_("Hostname was not supplied")); + } return OK; } @@ -419,15 +428,18 @@ int get_threshold(char *arg, char *rv[2]) { char *arg2 = NULL; arg1 = strscpy(arg1, arg); - if (strpbrk(arg1, ",:")) + if (strpbrk(arg1, ",:")) { arg2 = 1 + strpbrk(arg1, ",:"); + } if (arg2) { arg1[strcspn(arg1, ",:")] = 0; - if (strstr(arg1, "%") && strstr(arg2, "%")) + if (strstr(arg1, "%") && strstr(arg2, "%")) { die(STATE_UNKNOWN, _("%s: Only one threshold may be packet loss (%s)\n"), progname, arg); - if (!strstr(arg1, "%") && !strstr(arg2, "%")) + } + if (!strstr(arg1, "%") && !strstr(arg2, "%")) { die(STATE_UNKNOWN, _("%s: Only one threshold must be packet loss (%s)\n"), progname, arg); + } } if (arg2 && strstr(arg2, "%")) { -- cgit v1.2.3-74-g34f1 From cf48162487d6f042af084ee436acdd7a2db0cbfd Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 11 Mar 2025 02:43:51 +0100 Subject: Refactor check_fping --- plugins/Makefile.am | 3 +- plugins/check_fping.c | 182 ++++++++++++++++++++--------------------- plugins/check_fping.d/config.h | 58 +++++++++++++ 3 files changed, 147 insertions(+), 96 deletions(-) create mode 100644 plugins/check_fping.d/config.h (limited to 'plugins/check_fping.c') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 41487131..25a286c1 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -57,7 +57,8 @@ EXTRA_DIST = t \ check_apt.d \ check_by_ssh.d \ check_smtp.d \ - check_dig.d + check_dig.d \ + check_fping.d PLUGINHDRS = common.h diff --git a/plugins/check_fping.c b/plugins/check_fping.c index b85397d5..ec7abb67 100644 --- a/plugins/check_fping.c +++ b/plugins/check_fping.c @@ -38,52 +38,29 @@ const char *email = "devel@monitoring-plugins.org"; #include "netutils.h" #include "utils.h" #include +#include "check_fping.d/config.h" +#include "states.h" enum { - PACKET_COUNT = 1, - PACKET_SIZE = 56, PL = 0, RTA = 1 }; -static int textscan(char *buf); -static int process_arguments(int /*argc*/, char ** /*argv*/); +static 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*/, bool /*wpl_p*/, int /*wpl*/, bool /*alive_p*/); + +typedef struct { + int errorcode; + check_fping_config config; +} check_fping_config_wrapper; +static check_fping_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); static int get_threshold(char *arg, char *rv[2]); static void print_help(void); void print_usage(void); -static char *server_name = NULL; -static char *sourceip = NULL; -static char *sourceif = NULL; -static int packet_size = PACKET_SIZE; -static int packet_count = PACKET_COUNT; -static int target_timeout = 0; -static int packet_interval = 0; static bool verbose = false; -static bool dontfrag = false; -static bool randomize_packet_data = false; -static int cpl; -static int wpl; -static double crta; -static double wrta; -static bool cpl_p = false; -static bool wpl_p = false; -static bool alive_p = false; -static bool crta_p = false; -static bool wrta_p = false; int main(int argc, char **argv) { - /* normally should be int result = STATE_UNKNOWN; */ - - int status = STATE_UNKNOWN; - int result = 0; - char *fping_prog = NULL; - char *server = NULL; - char *command_line = NULL; - char *input_buffer = NULL; - char *option_string = ""; - input_buffer = malloc(MAX_INPUT_BUFFER); - setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); @@ -91,32 +68,38 @@ int main(int argc, char **argv) { /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - if (process_arguments(argc, argv) == ERROR) { + check_fping_config_wrapper tmp_config = process_arguments(argc, argv); + if (tmp_config.errorcode == ERROR) { usage4(_("Could not parse arguments")); } - server = strscpy(server, server_name); + const check_fping_config config = tmp_config.config; + + char *server = NULL; + server = strscpy(server, config.server_name); + char *option_string = ""; /* compose the command */ - if (target_timeout) { - xasprintf(&option_string, "%s-t %d ", option_string, target_timeout); + if (config.target_timeout) { + xasprintf(&option_string, "%s-t %d ", option_string, config.target_timeout); } - if (packet_interval) { - xasprintf(&option_string, "%s-p %d ", option_string, packet_interval); + if (config.packet_interval) { + xasprintf(&option_string, "%s-p %d ", option_string, config.packet_interval); } - if (sourceip) { - xasprintf(&option_string, "%s-S %s ", option_string, sourceip); + if (config.sourceip) { + xasprintf(&option_string, "%s-S %s ", option_string, config.sourceip); } - if (sourceif) { - xasprintf(&option_string, "%s-I %s ", option_string, sourceif); + if (config.sourceif) { + xasprintf(&option_string, "%s-I %s ", option_string, config.sourceif); } - if (dontfrag) { + if (config.dontfrag) { xasprintf(&option_string, "%s-M ", option_string); } - if (randomize_packet_data) { + if (config.randomize_packet_data) { xasprintf(&option_string, "%s-R ", option_string); } + char *fping_prog = NULL; #ifdef PATH_TO_FPING6 if (address_family != AF_INET && is_inet6_addr(server)) { fping_prog = strdup(PATH_TO_FPING6); @@ -127,7 +110,8 @@ int main(int argc, char **argv) { fping_prog = strdup(PATH_TO_FPING); #endif - xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, packet_size, packet_count, server); + char *command_line = NULL; + xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, config.packet_size, config.packet_count, server); if (verbose) { printf("%s\n", command_line); @@ -145,11 +129,14 @@ int main(int argc, char **argv) { printf(_("Could not open stderr for %s\n"), command_line); } + char *input_buffer = malloc(MAX_INPUT_BUFFER); + mp_state_enum status = STATE_UNKNOWN; while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { if (verbose) { printf("%s", input_buffer); } - status = max_state(status, textscan(input_buffer)); + status = max_state(status, textscan(input_buffer, config.server_name, config.crta_p, config.crta, config.wrta_p, config.wrta, + config.cpl_p, config.cpl, config.wpl_p, config.wpl, config.alive_p)); } /* If we get anything on STDERR, at least set warning */ @@ -158,12 +145,13 @@ int main(int argc, char **argv) { if (verbose) { printf("%s", input_buffer); } - status = max_state(status, textscan(input_buffer)); + status = max_state(status, textscan(input_buffer, config.server_name, config.crta_p, config.crta, config.wrta_p, config.wrta, + config.cpl_p, config.cpl, config.wpl_p, config.wpl, config.alive_p)); } (void)fclose(child_stderr); /* close the pipe */ - result = spclose(child_process); + int result = spclose(child_process); if (result) { /* need to use max_state not max */ status = max_state(status, STATE_WARNING); @@ -182,21 +170,17 @@ int main(int argc, char **argv) { } } - printf("FPING %s - %s\n", state_text(status), server_name); + printf("FPING %s - %s\n", state_text(status), config.server_name); return status; } -int textscan(char *buf) { - char *rtastr = NULL; - char *losstr = NULL; - char *xmtstr = NULL; - double loss; - double rta; - double xmt; - int status = STATE_UNKNOWN; - +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, + bool wpl_p, int wpl, bool alive_p) { /* stops testing after the first successful reply. */ + double rta; + double loss; + char *rtastr = NULL; if (alive_p && strstr(buf, "avg, 0% loss)")) { rtastr = strstr(buf, "ms ("); rtastr = 1 + index(rtastr, '('); @@ -208,6 +192,10 @@ int textscan(char *buf) { fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0)); } + mp_state_enum status = STATE_UNKNOWN; + char *xmtstr = NULL; + double xmt; + char *losstr = NULL; if (strstr(buf, "not found")) { die(STATE_CRITICAL, _("FPING UNKNOWN - %s not found\n"), server_name); @@ -243,7 +231,7 @@ int textscan(char *buf) { status = STATE_OK; } die(status, _("FPING %s - %s (loss=%.0f%%, rta=%f ms)|%s %s\n"), state_text(status), server_name, loss, rta, - perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100), + perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, false, 0, false, 0), fperfdata("rta", rta / 1.0e3, "s", wrta_p, wrta / 1.0e3, crta_p, crta / 1.0e3, true, 0, false, 0)); } else if (strstr(buf, "xmt/rcv/%loss")) { @@ -269,7 +257,7 @@ int textscan(char *buf) { } /* loss=%.0f%%;%d;%d;0;100 */ die(status, _("FPING %s - %s (loss=%.0f%% )|%s\n"), state_text(status), server_name, loss, - perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, true, 0, true, 100)); + perfdata("loss", (long int)loss, "%", wpl_p, wpl, cpl_p, cpl, false, 0, false, 0)); } else { status = max_state(status, STATE_WARNING); @@ -279,11 +267,7 @@ int textscan(char *buf) { } /* process command-line arguments */ -int process_arguments(int argc, char **argv) { - int c; - char *rv[2]; - - int option = 0; +check_fping_config_wrapper process_arguments(int argc, char **argv) { static struct option longopts[] = { {"hostname", required_argument, 0, 'H'}, {"sourceip", required_argument, 0, 'S'}, {"sourceif", required_argument, 0, 'I'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"alive", no_argument, 0, 'a'}, @@ -292,32 +276,41 @@ int process_arguments(int argc, char **argv) { {"help", no_argument, 0, 'h'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, {"dontfrag", no_argument, 0, 'M'}, {"random", no_argument, 0, 'R'}, {0, 0, 0, 0}}; + char *rv[2]; rv[PL] = NULL; rv[RTA] = NULL; + int option = 0; + + check_fping_config_wrapper result = { + .errorcode = OK, + .config = check_fping_config_init(), + }; + if (argc < 2) { - return ERROR; + result.errorcode = ERROR; + return result; } if (!is_option(argv[1])) { - server_name = argv[1]; + result.config.server_name = argv[1]; argv[1] = argv[0]; argv = &argv[1]; argc--; } while (1) { - c = getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:M:R:46", longopts, &option); + int option_index = getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:M:R:46", longopts, &option); - if (c == -1 || c == EOF || c == 1) { + if (option_index == -1 || option_index == EOF || option_index == 1) { break; } - switch (c) { + switch (option_index) { case '?': /* print short usage statement if args not parsable */ usage5(); case 'a': /* host alive mode */ - alive_p = true; + result.config.alive_p = true; break; case 'h': /* help */ print_help(); @@ -329,19 +322,19 @@ int process_arguments(int argc, char **argv) { verbose = true; break; case 'H': /* hostname */ - if (is_host(optarg) == false) { + if (!is_host(optarg)) { usage2(_("Invalid hostname/address"), optarg); } - server_name = strscpy(server_name, optarg); + result.config.server_name = optarg; break; case 'S': /* sourceip */ - if (is_host(optarg) == false) { + if (!is_host(optarg)) { usage2(_("Invalid hostname/address"), optarg); } - sourceip = strscpy(sourceip, optarg); + result.config.sourceip = optarg; break; case 'I': /* sourceip */ - sourceif = strscpy(sourceif, optarg); + result.config.sourceif = optarg; break; case '4': /* IPv4 only */ address_family = AF_INET; @@ -356,78 +349,77 @@ int process_arguments(int argc, char **argv) { case 'c': get_threshold(optarg, rv); if (rv[RTA]) { - crta = strtod(rv[RTA], NULL); - crta_p = true; + result.config.crta = strtod(rv[RTA], NULL); + result.config.crta_p = true; rv[RTA] = NULL; } if (rv[PL]) { - cpl = atoi(rv[PL]); - cpl_p = true; + result.config.cpl = atoi(rv[PL]); + result.config.cpl_p = true; rv[PL] = NULL; } break; case 'w': get_threshold(optarg, rv); if (rv[RTA]) { - wrta = strtod(rv[RTA], NULL); - wrta_p = true; + result.config.wrta = strtod(rv[RTA], NULL); + result.config.wrta_p = true; rv[RTA] = NULL; } if (rv[PL]) { - wpl = atoi(rv[PL]); - wpl_p = true; + result.config.wpl = atoi(rv[PL]); + result.config.wpl_p = true; rv[PL] = NULL; } break; case 'b': /* bytes per packet */ if (is_intpos(optarg)) { - packet_size = atoi(optarg); + result.config.packet_size = atoi(optarg); } else { usage(_("Packet size must be a positive integer")); } break; case 'n': /* number of packets */ if (is_intpos(optarg)) { - packet_count = atoi(optarg); + result.config.packet_count = atoi(optarg); } else { usage(_("Packet count must be a positive integer")); } break; case 'T': /* timeout in msec */ if (is_intpos(optarg)) { - target_timeout = atoi(optarg); + result.config.target_timeout = atoi(optarg); } else { usage(_("Target timeout must be a positive integer")); } break; case 'i': /* interval in msec */ if (is_intpos(optarg)) { - packet_interval = atoi(optarg); + result.config.packet_interval = atoi(optarg); } else { usage(_("Interval must be a positive integer")); } break; case 'R': - randomize_packet_data = true; + result.config.randomize_packet_data = true; break; case 'M': - dontfrag = true; + result.config.dontfrag = true; break; } } - if (server_name == NULL) { + if (result.config.server_name == NULL) { usage4(_("Hostname was not supplied")); } - return OK; + return result; } int get_threshold(char *arg, char *rv[2]) { - char *arg1 = NULL; char *arg2 = NULL; - arg1 = strscpy(arg1, arg); + char *arg1 = strdup(arg); if (strpbrk(arg1, ",:")) { arg2 = 1 + strpbrk(arg1, ",:"); } 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 @@ +#pragma once + +#include "../../config.h" +#include + +enum { + PACKET_SIZE = 56, + PACKET_COUNT = 1, +}; + +typedef struct { + char *server_name; + char *sourceip; + char *sourceif; + int packet_size; + int packet_count; + int target_timeout; + int packet_interval; + bool randomize_packet_data; + bool dontfrag; + bool alive_p; + + double crta; + bool crta_p; + double wrta; + bool wrta_p; + + int cpl; + bool cpl_p; + int wpl; + bool wpl_p; +} check_fping_config; + +check_fping_config check_fping_config_init() { + check_fping_config tmp = { + .server_name = NULL, + .sourceip = NULL, + .sourceif = NULL, + .packet_size = PACKET_SIZE, + .packet_count = PACKET_COUNT, + .target_timeout = 0, + .packet_interval = 0, + .randomize_packet_data = false, + .dontfrag = false, + .alive_p = false, + + .crta = 0, + .crta_p = false, + .wrta = 0, + .wrta_p = false, + + .cpl = 0, + .cpl_p = false, + .wpl = 0, + .wpl_p = false, + }; + return tmp; +} -- cgit v1.2.3-74-g34f1 From 4acba2b3ecef7c1482b3cd25e35773947d80e2c6 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 27 Mar 2025 11:20:36 +1000 Subject: Improve handling of -4/-6 If fping is used with a target that has dual stack v4/v6, then due to the logic during command construction, ipv4 will never be checked as v6 is preferred by fping. This explicitly flags -4/-6 when it is requested by the user. --- plugins/check_fping.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'plugins/check_fping.c') diff --git a/plugins/check_fping.c b/plugins/check_fping.c index ec7abb67..1c2b7689 100644 --- a/plugins/check_fping.c +++ b/plugins/check_fping.c @@ -79,7 +79,29 @@ int main(int argc, char **argv) { server = strscpy(server, config.server_name); char *option_string = ""; + char *fping_prog = NULL; + /* compose the command */ +#ifdef PATH_TO_FPING6 + if (address_family != AF_INET && is_inet6_addr(server)) { + fping_prog = strdup(PATH_TO_FPING6); + } else { + xasprintf(&option_string, "%s-4 ", option_string); + fping_prog = strdup(PATH_TO_FPING); + } +#else + if (address_family != AF_INET) { + // -4 / -6 must be set explicitly as when a host has dual stack + // if we don't specify -4 then fping selects ipv6 which can mess + // with some checks. + xasprintf(&option_string, "%s-6 ", option_string); + } else { + xasprintf(&option_string, "%s-4 ", option_string); + } + + fping_prog = strdup(PATH_TO_FPING); +#endif + if (config.target_timeout) { xasprintf(&option_string, "%s-t %d ", option_string, config.target_timeout); } @@ -99,17 +121,6 @@ int main(int argc, char **argv) { xasprintf(&option_string, "%s-R ", option_string); } - char *fping_prog = NULL; -#ifdef PATH_TO_FPING6 - if (address_family != AF_INET && is_inet6_addr(server)) { - fping_prog = strdup(PATH_TO_FPING6); - } else { - fping_prog = strdup(PATH_TO_FPING); - } -#else - fping_prog = strdup(PATH_TO_FPING); -#endif - char *command_line = NULL; xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, config.packet_size, config.packet_count, server); -- cgit v1.2.3-74-g34f1 From a1472be88322140cf1f2fc39b9e1b654a86f8155 Mon Sep 17 00:00:00 2001 From: William Date: Fri, 28 Mar 2025 12:40:35 +1000 Subject: Harden check with unspec --- plugins/check_fping.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'plugins/check_fping.c') diff --git a/plugins/check_fping.c b/plugins/check_fping.c index 1c2b7689..cf9c2d1c 100644 --- a/plugins/check_fping.c +++ b/plugins/check_fping.c @@ -83,14 +83,14 @@ int main(int argc, char **argv) { /* compose the command */ #ifdef PATH_TO_FPING6 - if (address_family != AF_INET && is_inet6_addr(server)) { + if (address_family == AF_INET6 || (address_family == AF_UNSPEC && is_inet6_addr(server))) { fping_prog = strdup(PATH_TO_FPING6); } else { xasprintf(&option_string, "%s-4 ", option_string); fping_prog = strdup(PATH_TO_FPING); } #else - if (address_family != AF_INET) { + if (address_family == AF_INET6 || (address_family == AF_UNSPEC && is_inet6_addr(server))) { // -4 / -6 must be set explicitly as when a host has dual stack // if we don't specify -4 then fping selects ipv6 which can mess // with some checks. -- cgit v1.2.3-74-g34f1 From 58a34245110f12192d3b3e99198086c119a57418 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 2 Apr 2025 10:50:56 +1000 Subject: Improve logic --- plugins/check_fping.c | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) (limited to 'plugins/check_fping.c') diff --git a/plugins/check_fping.c b/plugins/check_fping.c index cf9c2d1c..9dadcec7 100644 --- a/plugins/check_fping.c +++ b/plugins/check_fping.c @@ -81,27 +81,61 @@ int main(int argc, char **argv) { char *option_string = ""; char *fping_prog = NULL; - /* compose the command */ + /* First determine if the target is dualstack or ipv6 only. */ +#ifdef USE_IPV6 + bool server_is_inet6_addr = is_inet6_addr(server); +#else + bool server_is_inet6_addr = false; +#endif + + /* PATH_TO_FPING6 implies USE_IPV6 */ #ifdef PATH_TO_FPING6 - if (address_family == AF_INET6 || (address_family == AF_UNSPEC && is_inet6_addr(server))) { + /* + * If the user requested -6 OR the user made no assertion and the address is v6 or dualstack + * -> we use ipv6 + * If the user requested -4 OR the user made no assertion and the address is v4 ONLY + * -> we use ipv4 + */ + if (address_family == AF_INET6 || (address_family == AF_UNSPEC && server_is_inet6_addr)) { fping_prog = strdup(PATH_TO_FPING6); } else { xasprintf(&option_string, "%s-4 ", option_string); fping_prog = strdup(PATH_TO_FPING); } #else - if (address_family == AF_INET6 || (address_family == AF_UNSPEC && is_inet6_addr(server))) { - // -4 / -6 must be set explicitly as when a host has dual stack - // if we don't specify -4 then fping selects ipv6 which can mess - // with some checks. +#ifdef USE_IPV6 + /* + * If the user requested -6 OR the user made no assertion and the address is v6 or dualstack + * -> we use ipv6 + * If the user requested -4 OR the user made no assertion and the address is v4 ONLY + * -> we use ipv4 + */ + if (address_family == AF_INET6 || (address_family == AF_UNSPEC && server_is_inet6_addr)) { xasprintf(&option_string, "%s-6 ", option_string); } else { xasprintf(&option_string, "%s-4 ", option_string); } - +#else + /* + * If the user requested -6 + * -> warn that v6 is not available + * -> we use ipv4 + */ + if (address_family == AF_INET6) { + usage4(_("IPv6 support not available")); + } + /* + * Note here we still have to call with -4, else in the case of a dual stacked target + * we could potentially silently use ipv6 despite having just warned that it is not available + */ + xasprintf(&option_string, "%s-4 ", option_string); + /* end USE_IPV6 */ +#endif fping_prog = strdup(PATH_TO_FPING); + /* end PATH_TO_FPING6 */ #endif + /* compose the command */ if (config.target_timeout) { xasprintf(&option_string, "%s-t %d ", option_string, config.target_timeout); } -- cgit v1.2.3-74-g34f1 From 1fb9300a2f10d5c649df484e1b8d7550f9a5f846 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 7 May 2025 13:17:47 +1000 Subject: Remove un-needed flags --- configure.ac | 7 ------- plugins/check_fping.c | 42 ------------------------------------------ 2 files changed, 49 deletions(-) (limited to 'plugins/check_fping.c') diff --git a/configure.ac b/configure.ac index fdc9b699..bd3de196 100644 --- a/configure.ac +++ b/configure.ac @@ -1524,17 +1524,10 @@ AC_PATH_PROG(PATH_TO_FPING6,fping6) AC_ARG_WITH(fping_command, ACX_HELP_STRING([--with-fping-command=PATH], [Path to fping command]), PATH_TO_FPING=$withval) -AC_ARG_WITH(fping6_command, - ACX_HELP_STRING([--with-fping6-command=PATH], - [Path to fping6 command]), PATH_TO_FPING6=$withval) - if test -n "$PATH_TO_FPING" then AC_DEFINE_UNQUOTED(PATH_TO_FPING,"$PATH_TO_FPING",[path to fping]) EXTRAS="$EXTRAS check_fping\$(EXEEXT)" - if test x"$with_ipv6" != xno && test -n "$PATH_TO_FPING6"; then - AC_DEFINE_UNQUOTED(PATH_TO_FPING6,"$PATH_TO_FPING6",[path to fping6]) - fi else AC_MSG_WARN([Get fping from http://www.fping.com in order to make check_fping plugin]) fi diff --git a/plugins/check_fping.c b/plugins/check_fping.c index 9dadcec7..e05056b2 100644 --- a/plugins/check_fping.c +++ b/plugins/check_fping.c @@ -82,28 +82,8 @@ int main(int argc, char **argv) { char *fping_prog = NULL; /* First determine if the target is dualstack or ipv6 only. */ -#ifdef USE_IPV6 bool server_is_inet6_addr = is_inet6_addr(server); -#else - bool server_is_inet6_addr = false; -#endif - /* PATH_TO_FPING6 implies USE_IPV6 */ -#ifdef PATH_TO_FPING6 - /* - * If the user requested -6 OR the user made no assertion and the address is v6 or dualstack - * -> we use ipv6 - * If the user requested -4 OR the user made no assertion and the address is v4 ONLY - * -> we use ipv4 - */ - if (address_family == AF_INET6 || (address_family == AF_UNSPEC && server_is_inet6_addr)) { - fping_prog = strdup(PATH_TO_FPING6); - } else { - xasprintf(&option_string, "%s-4 ", option_string); - fping_prog = strdup(PATH_TO_FPING); - } -#else -#ifdef USE_IPV6 /* * If the user requested -6 OR the user made no assertion and the address is v6 or dualstack * -> we use ipv6 @@ -115,25 +95,7 @@ int main(int argc, char **argv) { } else { xasprintf(&option_string, "%s-4 ", option_string); } -#else - /* - * If the user requested -6 - * -> warn that v6 is not available - * -> we use ipv4 - */ - if (address_family == AF_INET6) { - usage4(_("IPv6 support not available")); - } - /* - * Note here we still have to call with -4, else in the case of a dual stacked target - * we could potentially silently use ipv6 despite having just warned that it is not available - */ - xasprintf(&option_string, "%s-4 ", option_string); - /* end USE_IPV6 */ -#endif fping_prog = strdup(PATH_TO_FPING); - /* end PATH_TO_FPING6 */ -#endif /* compose the command */ if (config.target_timeout) { @@ -385,11 +347,7 @@ check_fping_config_wrapper process_arguments(int argc, char **argv) { address_family = AF_INET; break; case '6': /* IPv6 only */ -#ifdef USE_IPV6 address_family = AF_INET6; -#else - usage(_("IPv6 support not available\n")); -#endif break; case 'c': get_threshold(optarg, rv); -- cgit v1.2.3-74-g34f1 From 7247fc656a1f475159b7879cc3c3b798e03c1a33 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle Date: Thu, 12 Jun 2025 11:13:59 +0200 Subject: Implement new fping options for fping 5.2 and 5.3 fping 5.2 and 5.3 add some new useful command line options which this commit add to check_fping. These are: * --fwmark - sets a firewall mark in the packages to make them identifiable (fping 5.2) * --icmp-timestamp - fping uses ICMP timestamp instead of ICMP Echo (fping 5.2) * --check-source - fping discards replies which originate not from the target address (fping 5.2) The fping release notes describe theses options ( https://github.com/schweikert/fping/releases ) in a little bit more detail. Currently the help display for those options is only shown when fping was available in the appropriate version during compilation. --- plugins/check_fping.c | 80 +++++++++++++++++++++++++++++++++++++----- plugins/check_fping.d/config.h | 24 +++++++++++++ 2 files changed, 96 insertions(+), 8 deletions(-) (limited to 'plugins/check_fping.c') diff --git a/plugins/check_fping.c b/plugins/check_fping.c index e05056b2..4d328a01 100644 --- a/plugins/check_fping.c +++ b/plugins/check_fping.c @@ -117,8 +117,26 @@ int main(int argc, char **argv) { xasprintf(&option_string, "%s-R ", option_string); } + if (config.fwmark_set) { + xasprintf(&option_string, "%s--fwmark %u ", option_string, config.fwmark); + } + + if (config.icmp_timestamp) { + xasprintf(&option_string, "%s--icmp-timestamp ", option_string); + } + + if (config.check_source) { + xasprintf(&option_string, "%s--check-source ", option_string); + } + char *command_line = NULL; - xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, config.packet_size, config.packet_count, server); + + if (config.icmp_timestamp) { + // no paket size settable for ICMP timestamp + xasprintf(&command_line, "%s %s -c %d %s", fping_prog, option_string, config.packet_count, server); + } else { + xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, config.packet_size, config.packet_count, server); + } if (verbose) { printf("%s\n", command_line); @@ -275,13 +293,35 @@ mp_state_enum textscan(char *buf, const char *server_name, bool crta_p, double c /* process command-line arguments */ check_fping_config_wrapper process_arguments(int argc, char **argv) { - static struct option longopts[] = { - {"hostname", required_argument, 0, 'H'}, {"sourceip", required_argument, 0, 'S'}, {"sourceif", required_argument, 0, 'I'}, - {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"alive", no_argument, 0, 'a'}, - {"bytes", required_argument, 0, 'b'}, {"number", required_argument, 0, 'n'}, {"target-timeout", required_argument, 0, 'T'}, - {"interval", required_argument, 0, 'i'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, - {"help", no_argument, 0, 'h'}, {"use-ipv4", no_argument, 0, '4'}, {"use-ipv6", no_argument, 0, '6'}, - {"dontfrag", no_argument, 0, 'M'}, {"random", no_argument, 0, 'R'}, {0, 0, 0, 0}}; + enum { + FWMARK_OPT = CHAR_MAX + 1, + ICMP_TIMESTAMP_OPT, + CHECK_SOURCE_OPT, + }; + static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, + {"sourceip", required_argument, 0, 'S'}, + {"sourceif", required_argument, 0, 'I'}, + {"critical", required_argument, 0, 'c'}, + {"warning", required_argument, 0, 'w'}, + {"alive", no_argument, 0, 'a'}, + {"bytes", required_argument, 0, 'b'}, + {"number", required_argument, 0, 'n'}, + {"target-timeout", required_argument, 0, 'T'}, + {"interval", required_argument, 0, 'i'}, + {"verbose", no_argument, 0, 'v'}, + {"version", no_argument, 0, 'V'}, + {"help", no_argument, 0, 'h'}, + {"use-ipv4", no_argument, 0, '4'}, + {"use-ipv6", no_argument, 0, '6'}, + {"dontfrag", no_argument, 0, 'M'}, + {"random", no_argument, 0, 'R'}, + // only available with fping version >= 5.3 + {"fwmark", required_argument, NULL, FWMARK_OPT}, + // only available with fping version >= 5.3 + {"icmp-timestamp", no_argument, NULL, ICMP_TIMESTAMP_OPT}, + {"check-source", no_argument, NULL, CHECK_SOURCE_OPT}, + + {0, 0, 0, 0}}; char *rv[2]; rv[PL] = NULL; @@ -409,6 +449,20 @@ check_fping_config_wrapper process_arguments(int argc, char **argv) { case 'M': result.config.dontfrag = true; break; + case FWMARK_OPT: + if (is_intpos(optarg)) { + result.config.fwmark = (unsigned int)atol(optarg); + result.config.fwmark_set = true; + } else { + usage(_("fwmark must be a positive integer")); + } + break; + case ICMP_TIMESTAMP_OPT: + result.config.icmp_timestamp = true; + break; + case CHECK_SOURCE_OPT: + result.config.check_source = true; + break; } } @@ -496,6 +550,16 @@ void print_help(void) { printf(" %s\n", _("set the Don't Fragment flag")); printf(" %s\n", "-R, --random"); printf(" %s\n", _("random packet data (to foil link data compression)")); +#ifdef FPING_VERSION_5_2_OR_HIGHER + printf(" %s\n", "--fwmark=INTEGER"); + printf(" %s\n", _("set the routing mark to INTEGER (fping option)")); +# ifdef FPING_VERSION_5_3_OR_HIGHER + printf(" %s\n", "--icmp-timestamp"); + printf(" %s\n", _("use ICMP Timestamp instead of ICMP Echo (fping option)")); + printf(" %s\n", "--check-source"); + printf(" %s\n", _("discard replies not from target address (fping option)")); +# endif +#endif printf(UT_VERBOSE); printf("\n"); printf(" %s\n", _("THRESHOLD is ,%% where is the round trip average travel time (ms)")); diff --git a/plugins/check_fping.d/config.h b/plugins/check_fping.d/config.h index a0697bf3..6d28382a 100644 --- a/plugins/check_fping.d/config.h +++ b/plugins/check_fping.d/config.h @@ -29,6 +29,21 @@ typedef struct { bool cpl_p; int wpl; bool wpl_p; + + // only available with fping version >= 5.2 + // for a given uint _fwmark_ fping sets _fwmark_ as a firewall mark + // in the pakets + unsigned int fwmark; + bool fwmark_set; + + + // only available with fping version >= 5.3 + // Setting icmp_timestamp tells fping to use ICMP Timestamp (ICMP type 13) instead + // of ICMP Echo + bool icmp_timestamp; + + // Setting check_source lets fping discard replies which are not from the target address + bool check_source; } check_fping_config; check_fping_config check_fping_config_init() { @@ -53,6 +68,15 @@ check_fping_config check_fping_config_init() { .cpl_p = false, .wpl = 0, .wpl_p = false, + + // only available with fping version >= 5.2 + .fwmark = 0, + .fwmark_set = false, // just to be deterministic + + // only available with fping version >= 5.3 + .icmp_timestamp = false, + .check_source = false, + }; return tmp; } -- cgit v1.2.3-74-g34f1 From f2c6ce08e3da320d044564352faedd78eb76f1a1 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle Date: Thu, 12 Jun 2025 11:19:42 +0200 Subject: check_fping: small style improvement --- plugins/check_fping.c | 4 ++-- plugins/check_fping.d/config.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'plugins/check_fping.c') diff --git a/plugins/check_fping.c b/plugins/check_fping.c index 4d328a01..c1e7dd17 100644 --- a/plugins/check_fping.c +++ b/plugins/check_fping.c @@ -132,7 +132,7 @@ int main(int argc, char **argv) { char *command_line = NULL; if (config.icmp_timestamp) { - // no paket size settable for ICMP timestamp + // no packet size settable for ICMP timestamp xasprintf(&command_line, "%s %s -c %d %s", fping_prog, option_string, config.packet_count, server); } else { xasprintf(&command_line, "%s %s-b %d -c %d %s", fping_prog, option_string, config.packet_size, config.packet_count, server); @@ -346,7 +346,7 @@ check_fping_config_wrapper process_arguments(int argc, char **argv) { argc--; } - while (1) { + while (true) { int option_index = getopt_long(argc, argv, "+hVvaH:S:c:w:b:n:T:i:I:M:R:46", longopts, &option); if (option_index == -1 || option_index == EOF || option_index == 1) { diff --git a/plugins/check_fping.d/config.h b/plugins/check_fping.d/config.h index 6d28382a..d95e9ded 100644 --- a/plugins/check_fping.d/config.h +++ b/plugins/check_fping.d/config.h @@ -32,7 +32,7 @@ typedef struct { // only available with fping version >= 5.2 // for a given uint _fwmark_ fping sets _fwmark_ as a firewall mark - // in the pakets + // in the packets unsigned int fwmark; bool fwmark_set; -- cgit v1.2.3-74-g34f1 From 19f409ac559278cd8623e2d5875818938f648996 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle Date: Thu, 12 Jun 2025 13:26:55 +0200 Subject: Remove unnecessary newline --- plugins/check_fping.c | 1 - 1 file changed, 1 deletion(-) (limited to 'plugins/check_fping.c') diff --git a/plugins/check_fping.c b/plugins/check_fping.c index c1e7dd17..5db6b9aa 100644 --- a/plugins/check_fping.c +++ b/plugins/check_fping.c @@ -320,7 +320,6 @@ check_fping_config_wrapper process_arguments(int argc, char **argv) { // only available with fping version >= 5.3 {"icmp-timestamp", no_argument, NULL, ICMP_TIMESTAMP_OPT}, {"check-source", no_argument, NULL, CHECK_SOURCE_OPT}, - {0, 0, 0, 0}}; char *rv[2]; -- cgit v1.2.3-74-g34f1 From a669b2531d3b01aeb5b3e39c28bf2e6816fb14af Mon Sep 17 00:00:00 2001 From: Lorenz Kästle Date: Thu, 12 Jun 2025 13:33:50 +0200 Subject: Remove options if fping version is too low and die directly --- plugins/check_fping.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'plugins/check_fping.c') diff --git a/plugins/check_fping.c b/plugins/check_fping.c index 5db6b9aa..8018e06d 100644 --- a/plugins/check_fping.c +++ b/plugins/check_fping.c @@ -315,11 +315,15 @@ check_fping_config_wrapper process_arguments(int argc, char **argv) { {"use-ipv6", no_argument, 0, '6'}, {"dontfrag", no_argument, 0, 'M'}, {"random", no_argument, 0, 'R'}, - // only available with fping version >= 5.3 +#ifdef FPING_VERSION_5_2_OR_HIGHER + // only available with fping version >= 5.2 {"fwmark", required_argument, NULL, FWMARK_OPT}, +# ifdef FPING_VERSION_5_3_OR_HIGHER // only available with fping version >= 5.3 {"icmp-timestamp", no_argument, NULL, ICMP_TIMESTAMP_OPT}, {"check-source", no_argument, NULL, CHECK_SOURCE_OPT}, +# endif +#endif {0, 0, 0, 0}}; char *rv[2]; -- cgit v1.2.3-74-g34f1