From 8b2222e8b704fc451093996e906c07a6a8e3538a Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 01:00:48 +0100 Subject: Refactor check_ntp_peer --- plugins/Makefile.am | 1 + 1 file changed, 1 insertion(+) (limited to 'plugins/Makefile.am') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 5d4449bf..a294cfef 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -60,6 +60,7 @@ EXTRA_DIST = t \ check_mrtgtraf.d \ check_mysql_query.d \ check_mrtg.d \ + check_ntp_peer.d \ check_apt.d \ check_by_ssh.d \ check_smtp.d \ -- cgit v1.2.3-74-g34f1 From ae60d6d8d818191afba08819c5a62673b98ef29a Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 13:03:17 +0100 Subject: Refactor check_ping --- plugins/Makefile.am | 1 + plugins/check_ping.c | 319 ++++++++++++++++++++++-------------------- plugins/check_ping.d/config.h | 46 ++++++ 3 files changed, 218 insertions(+), 148 deletions(-) create mode 100644 plugins/check_ping.d/config.h (limited to 'plugins/Makefile.am') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 9ea6e85e..10a12168 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -63,6 +63,7 @@ EXTRA_DIST = t \ check_mrtg.d \ check_apt.d \ check_pgsql.d \ + check_ping.d \ check_by_ssh.d \ check_smtp.d \ check_mysql.d \ diff --git a/plugins/check_ping.c b/plugins/check_ping.c index d79a4a61..fcf68f81 100644 --- a/plugins/check_ping.c +++ b/plugins/check_ping.c @@ -36,39 +36,35 @@ const char *email = "devel@monitoring-plugins.org"; #include "netutils.h" #include "popen.h" #include "utils.h" +#include "check_ping.d/config.h" +#include "../lib/states.h" #include -#define WARN_DUPLICATES "DUPLICATES FOUND! " -#define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */ +#define WARN_DUPLICATES "DUPLICATES FOUND! " -enum { - UNKNOWN_PACKET_LOSS = 200, /* 200% */ - DEFAULT_MAX_PACKETS = 5 /* default no. of ICMP ECHO packets */ -}; +typedef struct { + int errorcode; + check_ping_config config; +} check_ping_config_wrapper; +static check_ping_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); +static check_ping_config_wrapper validate_arguments(check_ping_config_wrapper /*config_wrapper*/); -static int process_arguments(int /*argc*/, char ** /*argv*/); -static int get_threshold(char * /*arg*/, float * /*trta*/, int * /*tpl*/); -static int validate_arguments(void); -static int run_ping(const char *cmd, const char *addr); -static int error_scan(char buf[MAX_INPUT_BUFFER], const char *addr); +static int get_threshold(char * /*arg*/, double * /*trta*/, int * /*tpl*/); + +typedef struct { + mp_state_enum state; + double round_trip_average; + int packet_loss; +} ping_result; +static ping_result run_ping(const char *cmd, const char *addr, double /*crta*/); + +static mp_state_enum error_scan(char buf[MAX_INPUT_BUFFER], const char *addr); static void print_help(void); void print_usage(void); -static bool display_html = false; -static int wpl = UNKNOWN_PACKET_LOSS; -static int cpl = UNKNOWN_PACKET_LOSS; -static float wrta = UNKNOWN_TRIP_TIME; -static float crta = UNKNOWN_TRIP_TIME; -static char **addresses = NULL; -static int n_addresses = 0; -static int max_addr = 1; -static int max_packets = -1; static int verbose = 0; -static float round_trip_average = UNKNOWN_TRIP_TIME; -static int packet_loss = UNKNOWN_PACKET_LOSS; - static char *warn_text; int main(int argc, char **argv) { @@ -77,16 +73,16 @@ int main(int argc, char **argv) { bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - addresses = malloc(sizeof(char *) * max_addr); - addresses[0] = NULL; - /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - if (process_arguments(argc, argv) == ERROR) { + check_ping_config_wrapper tmp_config = process_arguments(argc, argv); + if (tmp_config.errorcode == ERROR) { usage4(_("Could not parse arguments")); } + const check_ping_config config = tmp_config.config; + /* Set signal handling and alarm */ if (signal(SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) { usage4(_("Cannot catch SIGALRM")); @@ -102,9 +98,9 @@ int main(int argc, char **argv) { int result = STATE_UNKNOWN; char *rawcmd = NULL; - for (int i = 0; i < n_addresses; i++) { + for (size_t i = 0; i < config.n_addresses; i++) { #ifdef PING6_COMMAND - if (address_family != AF_INET && is_inet6_addr(addresses[i])) { + if (address_family != AF_INET && is_inet6_addr(config.addresses[i])) { rawcmd = strdup(PING6_COMMAND); } else { rawcmd = strdup(PING_COMMAND); @@ -118,12 +114,12 @@ int main(int argc, char **argv) { /* does the host address of number of packets argument come first? */ #ifdef PING_PACKETS_FIRST # ifdef PING_HAS_TIMEOUT - xasprintf(&cmd, rawcmd, timeout_interval, max_packets, addresses[i]); + xasprintf(&cmd, rawcmd, timeout_interval, config.max_packets, config.addresses[i]); # else - xasprintf(&cmd, rawcmd, max_packets, addresses[i]); + xasprintf(&cmd, rawcmd, config.max_packets, addresses[i]); # endif #else - xasprintf(&cmd, rawcmd, addresses[i], max_packets); + xasprintf(&cmd, rawcmd, addresses[i], config.max_packets); #endif if (verbose >= 2) { @@ -131,53 +127,55 @@ int main(int argc, char **argv) { } /* run the command */ - int this_result = run_ping(cmd, addresses[i]); - if (packet_loss == UNKNOWN_PACKET_LOSS || round_trip_average < 0.0) { + ping_result pinged = run_ping(cmd, config.addresses[i], config.crta); + + if (pinged.packet_loss == UNKNOWN_PACKET_LOSS || pinged.round_trip_average < 0.0) { printf("%s\n", cmd); die(STATE_UNKNOWN, _("CRITICAL - Could not interpret output from ping command\n")); } - if (packet_loss >= cpl || round_trip_average >= crta || round_trip_average < 0) { - this_result = STATE_CRITICAL; - } else if (packet_loss >= wpl || round_trip_average >= wrta) { - this_result = STATE_WARNING; - } else if (packet_loss >= 0 && round_trip_average >= 0) { - this_result = max_state(STATE_OK, this_result); + if (pinged.packet_loss >= config.cpl || pinged.round_trip_average >= config.crta || pinged.round_trip_average < 0) { + pinged.state = STATE_CRITICAL; + } else if (pinged.packet_loss >= config.wpl || pinged.round_trip_average >= config.wrta) { + pinged.state = STATE_WARNING; + } else if (pinged.packet_loss >= 0 && pinged.round_trip_average >= 0) { + pinged.state = max_state(STATE_OK, pinged.state); } - if (n_addresses > 1 && this_result != STATE_UNKNOWN) { - die(STATE_OK, "%s is alive\n", addresses[i]); + if (config.n_addresses > 1 && pinged.state != STATE_UNKNOWN) { + die(STATE_OK, "%s is alive\n", config.addresses[i]); } - if (display_html) { - printf("", CGIURL, addresses[i]); + if (config.display_html) { + printf("", CGIURL, config.addresses[i]); } - if (packet_loss == 100) { - printf(_("PING %s - %sPacket loss = %d%%"), state_text(this_result), warn_text, packet_loss); + if (pinged.packet_loss == 100) { + printf(_("PING %s - %sPacket loss = %d%%"), state_text(pinged.state), warn_text, pinged.packet_loss); } else { - printf(_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"), state_text(this_result), warn_text, packet_loss, - round_trip_average); + printf(_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"), state_text(pinged.state), warn_text, pinged.packet_loss, + pinged.round_trip_average); } - if (display_html) { + if (config.display_html) { printf(""); } /* Print performance data */ - if (packet_loss != 100) { - printf("|%s", - fperfdata("rta", (double)round_trip_average, "ms", (bool)(wrta > 0), wrta, (bool)(crta > 0), crta, true, 0, false, 0)); + if (pinged.packet_loss != 100) { + printf("|%s", fperfdata("rta", pinged.round_trip_average, "ms", (bool)(config.wrta > 0), config.wrta, + (bool)(config.crta > 0), config.crta, true, 0, false, 0)); } else { - printf("| rta=U;%f;%f;;", wrta, crta); + printf("| rta=U;%f;%f;;", config.wrta, config.crta); } - printf(" %s\n", perfdata("pl", (long)packet_loss, "%", (bool)(wpl > 0), wpl, (bool)(cpl > 0), cpl, true, 0, false, 0)); + printf(" %s\n", perfdata("pl", (long)pinged.packet_loss, "%", (bool)(config.wpl > 0), config.wpl, (bool)(config.cpl > 0), + config.cpl, true, 0, false, 0)); if (verbose >= 2) { - printf("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl); + printf("%f:%d%% %f:%d%%\n", config.wrta, config.wpl, config.crta, config.cpl); } - result = max_state(result, this_result); + result = max_state(result, pinged.state); free(rawcmd); free(cmd); } @@ -186,7 +184,7 @@ int main(int argc, char **argv) { } /* process command-line arguments */ -int process_arguments(int argc, char **argv) { +check_ping_config_wrapper process_arguments(int argc, char **argv) { static struct option longopts[] = {STD_LONG_OPTS, {"packets", required_argument, 0, 'p'}, {"nohtml", no_argument, 0, 'n'}, @@ -195,8 +193,14 @@ int process_arguments(int argc, char **argv) { {"use-ipv6", no_argument, 0, '6'}, {0, 0, 0, 0}}; + check_ping_config_wrapper result = { + .errorcode = OK, + .config = check_ping_config_init(), + }; + if (argc < 2) { - return ERROR; + result.errorcode = ERROR; + return result; } for (int index = 1; index < argc; index++) { @@ -209,6 +213,7 @@ int process_arguments(int argc, char **argv) { } int option = 0; + size_t max_addr = MAX_ADDR_START; while (true) { int option_index = getopt_long(argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option); @@ -246,15 +251,15 @@ int process_arguments(int argc, char **argv) { case 'H': /* hostname */ { char *ptr = optarg; while (true) { - n_addresses++; - if (n_addresses > max_addr) { + result.config.n_addresses++; + if (result.config.n_addresses > max_addr) { max_addr *= 2; - addresses = realloc(addresses, sizeof(char *) * max_addr); - if (addresses == NULL) { + result.config.addresses = realloc(result.config.addresses, sizeof(char *) * max_addr); + if (result.config.addresses == NULL) { die(STATE_UNKNOWN, _("Could not realloc() addresses\n")); } } - addresses[n_addresses - 1] = ptr; + result.config.addresses[result.config.n_addresses - 1] = ptr; if ((ptr = index(ptr, ','))) { strcpy(ptr, ""); ptr += sizeof(char); @@ -265,105 +270,110 @@ int process_arguments(int argc, char **argv) { } break; case 'p': /* number of packets to send */ if (is_intnonneg(optarg)) { - max_packets = atoi(optarg); + result.config.max_packets = atoi(optarg); } else { usage2(_(" (%s) must be a non-negative number\n"), optarg); } break; case 'n': /* no HTML */ - display_html = false; + result.config.display_html = false; break; case 'L': /* show HTML */ - display_html = true; + result.config.display_html = true; break; case 'c': - get_threshold(optarg, &crta, &cpl); + get_threshold(optarg, &result.config.crta, &result.config.cpl); break; case 'w': - get_threshold(optarg, &wrta, &wpl); + get_threshold(optarg, &result.config.wrta, &result.config.wpl); break; } } int arg_counter = optind; if (arg_counter == argc) { - return validate_arguments(); + return validate_arguments(result); } - if (addresses[0] == NULL) { + if (result.config.addresses[0] == NULL) { if (!is_host(argv[arg_counter])) { usage2(_("Invalid hostname/address"), argv[arg_counter]); } else { - addresses[0] = argv[arg_counter++]; - n_addresses++; + result.config.addresses[0] = argv[arg_counter++]; + result.config.n_addresses++; if (arg_counter == argc) { - return validate_arguments(); + return validate_arguments(result); } } } - if (wpl == UNKNOWN_PACKET_LOSS) { + if (result.config.wpl == UNKNOWN_PACKET_LOSS) { if (!is_intpercent(argv[arg_counter])) { printf(_(" (%s) must be an integer percentage\n"), argv[arg_counter]); - return ERROR; + result.errorcode = ERROR; + return result; } - wpl = atoi(argv[arg_counter++]); + result.config.wpl = atoi(argv[arg_counter++]); if (arg_counter == argc) { - return validate_arguments(); + return validate_arguments(result); } } - if (cpl == UNKNOWN_PACKET_LOSS) { + if (result.config.cpl == UNKNOWN_PACKET_LOSS) { if (!is_intpercent(argv[arg_counter])) { printf(_(" (%s) must be an integer percentage\n"), argv[arg_counter]); - return ERROR; + result.errorcode = ERROR; + return result; } - cpl = atoi(argv[arg_counter++]); + result.config.cpl = atoi(argv[arg_counter++]); if (arg_counter == argc) { - return validate_arguments(); + return validate_arguments(result); } } - if (wrta < 0.0) { + if (result.config.wrta < 0.0) { if (is_negative(argv[arg_counter])) { printf(_(" (%s) must be a non-negative number\n"), argv[arg_counter]); - return ERROR; + result.errorcode = ERROR; + return result; } - wrta = atof(argv[arg_counter++]); + result.config.wrta = atof(argv[arg_counter++]); if (arg_counter == argc) { - return validate_arguments(); + return validate_arguments(result); } } - if (crta < 0.0) { + if (result.config.crta < 0.0) { if (is_negative(argv[arg_counter])) { printf(_(" (%s) must be a non-negative number\n"), argv[arg_counter]); - return ERROR; + result.errorcode = ERROR; + return result; } - crta = atof(argv[arg_counter++]); + result.config.crta = atof(argv[arg_counter++]); if (arg_counter == argc) { - return validate_arguments(); + return validate_arguments(result); } } - if (max_packets == -1) { + if (result.config.max_packets == -1) { if (is_intnonneg(argv[arg_counter])) { - max_packets = atoi(argv[arg_counter++]); + result.config.max_packets = atoi(argv[arg_counter++]); } else { printf(_(" (%s) must be a non-negative number\n"), argv[arg_counter]); - return ERROR; + result.errorcode = ERROR; + return result; } } - return validate_arguments(); + return validate_arguments(result); } -int get_threshold(char *arg, float *trta, int *tpl) { - if (is_intnonneg(arg) && sscanf(arg, "%f", trta) == 1) { +int get_threshold(char *arg, double *trta, int *tpl) { + if (is_intnonneg(arg) && sscanf(arg, "%lf", trta) == 1) { return OK; } - if (strpbrk(arg, ",:") && strstr(arg, "%") && sscanf(arg, "%f%*[:,]%d%%", trta, tpl) == 2) { + if (strpbrk(arg, ",:") && strstr(arg, "%") && sscanf(arg, "%lf%*[:,]%d%%", trta, tpl) == 2) { return OK; } @@ -375,60 +385,66 @@ int get_threshold(char *arg, float *trta, int *tpl) { return STATE_UNKNOWN; } -int validate_arguments() { - if (wrta < 0.0) { +check_ping_config_wrapper validate_arguments(check_ping_config_wrapper config_wrapper) { + if (config_wrapper.config.wrta < 0.0) { printf(_(" was not set\n")); - return ERROR; + config_wrapper.errorcode = ERROR; + return config_wrapper; } - if (crta < 0.0) { + if (config_wrapper.config.crta < 0.0) { printf(_(" was not set\n")); - return ERROR; + config_wrapper.errorcode = ERROR; + return config_wrapper; } - if (wpl == UNKNOWN_PACKET_LOSS) { + if (config_wrapper.config.wpl == UNKNOWN_PACKET_LOSS) { printf(_(" was not set\n")); - return ERROR; + config_wrapper.errorcode = ERROR; + return config_wrapper; } - if (cpl == UNKNOWN_PACKET_LOSS) { + if (config_wrapper.config.cpl == UNKNOWN_PACKET_LOSS) { printf(_(" was not set\n")); - return ERROR; + config_wrapper.errorcode = ERROR; + return config_wrapper; } - if (wrta > crta) { - printf(_(" (%f) cannot be larger than (%f)\n"), wrta, crta); - return ERROR; + if (config_wrapper.config.wrta > config_wrapper.config.crta) { + printf(_(" (%f) cannot be larger than (%f)\n"), config_wrapper.config.wrta, config_wrapper.config.crta); + config_wrapper.errorcode = ERROR; + return config_wrapper; } - if (wpl > cpl) { - printf(_(" (%d) cannot be larger than (%d)\n"), wpl, cpl); - return ERROR; + if (config_wrapper.config.wpl > config_wrapper.config.cpl) { + printf(_(" (%d) cannot be larger than (%d)\n"), config_wrapper.config.wpl, config_wrapper.config.cpl); + config_wrapper.errorcode = ERROR; + return config_wrapper; } - if (max_packets == -1) { - max_packets = DEFAULT_MAX_PACKETS; + if (config_wrapper.config.max_packets == -1) { + config_wrapper.config.max_packets = DEFAULT_MAX_PACKETS; } - float max_seconds = (crta / 1000.0 * max_packets) + max_packets; + double max_seconds = (config_wrapper.config.crta / 1000.0 * config_wrapper.config.max_packets) + config_wrapper.config.max_packets; if (max_seconds > timeout_interval) { timeout_interval = (unsigned int)max_seconds; } - for (int i = 0; i < n_addresses; i++) { - if (!is_host(addresses[i])) { - usage2(_("Invalid hostname/address"), addresses[i]); + for (size_t i = 0; i < config_wrapper.config.n_addresses; i++) { + if (!is_host(config_wrapper.config.addresses[i])) { + usage2(_("Invalid hostname/address"), config_wrapper.config.addresses[i]); } } - if (n_addresses == 0) { + if (config_wrapper.config.n_addresses == 0) { usage(_("You must specify a server address or host name")); } - return OK; + return config_wrapper; } -int run_ping(const char *cmd, const char *addr) { +ping_result run_ping(const char *cmd, const char *addr, double crta) { if ((child_process = spopen(cmd)) == NULL) { die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd); } @@ -439,48 +455,55 @@ int run_ping(const char *cmd, const char *addr) { } char buf[MAX_INPUT_BUFFER]; - int result = STATE_UNKNOWN; + ping_result result = { + .state = STATE_UNKNOWN, + .packet_loss = UNKNOWN_PACKET_LOSS, + .round_trip_average = UNKNOWN_TRIP_TIME, + }; + while (fgets(buf, MAX_INPUT_BUFFER - 1, child_process)) { if (verbose >= 3) { printf("Output: %s", buf); } - result = max_state(result, error_scan(buf, addr)); + result.state = max_state(result.state, error_scan(buf, addr)); /* get the percent loss statistics */ int match = 0; - if ((sscanf(buf, "%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss%n", &packet_loss, &match) && + if ((sscanf(buf, "%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss%n", &result.packet_loss, &match) && + match) || + (sscanf(buf, "%*d packets transmitted, %*d packets received, +%*d duplicates, %d%% packet loss%n", &result.packet_loss, + &match) && match) || - (sscanf(buf, "%*d packets transmitted, %*d packets received, +%*d duplicates, %d%% packet loss%n", &packet_loss, &match) && + (sscanf(buf, "%*d packets transmitted, %*d received, +%*d duplicates, %d%% packet loss%n", &result.packet_loss, &match) && match) || - (sscanf(buf, "%*d packets transmitted, %*d received, +%*d duplicates, %d%% packet loss%n", &packet_loss, &match) && match) || - (sscanf(buf, "%*d packets transmitted, %*d packets received, %d%% packet loss%n", &packet_loss, &match) && match) || - (sscanf(buf, "%*d packets transmitted, %*d packets received, %d%% loss, time%n", &packet_loss, &match) && match) || - (sscanf(buf, "%*d packets transmitted, %*d received, %d%% loss, time%n", &packet_loss, &match) && match) || - (sscanf(buf, "%*d packets transmitted, %*d received, %d%% packet loss, time%n", &packet_loss, &match) && match) || - (sscanf(buf, "%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss%n", &packet_loss, &match) && match) || - (sscanf(buf, "%*d packets transmitted %*d received, +%*d errors, %d%% packet loss%n", &packet_loss, &match) && match) || - (sscanf(buf, "%*[^(](%d%% %*[^)])%n", &packet_loss, &match) && match)) { + (sscanf(buf, "%*d packets transmitted, %*d packets received, %d%% packet loss%n", &result.packet_loss, &match) && match) || + (sscanf(buf, "%*d packets transmitted, %*d packets received, %d%% loss, time%n", &result.packet_loss, &match) && match) || + (sscanf(buf, "%*d packets transmitted, %*d received, %d%% loss, time%n", &result.packet_loss, &match) && match) || + (sscanf(buf, "%*d packets transmitted, %*d received, %d%% packet loss, time%n", &result.packet_loss, &match) && match) || + (sscanf(buf, "%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss%n", &result.packet_loss, &match) && match) || + (sscanf(buf, "%*d packets transmitted %*d received, +%*d errors, %d%% packet loss%n", &result.packet_loss, &match) && match) || + (sscanf(buf, "%*[^(](%d%% %*[^)])%n", &result.packet_loss, &match) && match)) { continue; } /* get the round trip average */ - if ((sscanf(buf, "round-trip min/avg/max = %*f/%f/%*f%n", &round_trip_average, &match) && match) || - (sscanf(buf, "round-trip min/avg/max/mdev = %*f/%f/%*f/%*f%n", &round_trip_average, &match) && match) || - (sscanf(buf, "round-trip min/avg/max/sdev = %*f/%f/%*f/%*f%n", &round_trip_average, &match) && match) || - (sscanf(buf, "round-trip min/avg/max/stddev = %*f/%f/%*f/%*f%n", &round_trip_average, &match) && match) || - (sscanf(buf, "round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f%n", &round_trip_average, &match) && match) || - (sscanf(buf, "round-trip (ms) min/avg/max = %*f/%f/%*f%n", &round_trip_average, &match) && match) || - (sscanf(buf, "round-trip (ms) min/avg/max/stddev = %*f/%f/%*f/%*f%n", &round_trip_average, &match) && match) || - (sscanf(buf, "rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms%n", &round_trip_average, &match) && match) || - (sscanf(buf, "%*[^=] = %*fms, %*[^=] = %*fms, %*[^=] = %fms%n", &round_trip_average, &match) && match)) { + if ((sscanf(buf, "round-trip min/avg/max = %*f/%lf/%*f%n", &result.round_trip_average, &match) && match) || + (sscanf(buf, "round-trip min/avg/max/mdev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) && match) || + (sscanf(buf, "round-trip min/avg/max/sdev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) && match) || + (sscanf(buf, "round-trip min/avg/max/stddev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) && match) || + (sscanf(buf, "round-trip min/avg/max/std-dev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) && match) || + (sscanf(buf, "round-trip (ms) min/avg/max = %*f/%lf/%*f%n", &result.round_trip_average, &match) && match) || + (sscanf(buf, "round-trip (ms) min/avg/max/stddev = %*f/%lf/%*f/%*f%n", &result.round_trip_average, &match) && match) || + (sscanf(buf, "rtt min/avg/max/mdev = %*f/%lf/%*f/%*f ms%n", &result.round_trip_average, &match) && match) || + (sscanf(buf, "%*[^=] = %*fms, %*[^=] = %*fms, %*[^=] = %lfms%n", &result.round_trip_average, &match) && match)) { continue; } } /* this is needed because there is no rta if all packets are lost */ - if (packet_loss == 100) { - round_trip_average = crta; + if (result.packet_loss == 100) { + result.round_trip_average = crta; } /* check stderr, setting at least WARNING if there is output here */ @@ -492,8 +515,8 @@ int run_ping(const char *cmd, const char *addr) { if (verbose >= 3) { printf("Got stderr: %s", buf); } - if ((result = error_scan(buf, addr)) == STATE_OK) { - result = STATE_WARNING; + if ((result.state = error_scan(buf, addr)) == STATE_OK) { + result.state = STATE_WARNING; if (warn_text == NULL) { warn_text = strdup(_("System call sent warnings to stderr ")); } else { @@ -514,7 +537,7 @@ int run_ping(const char *cmd, const char *addr) { return result; } -int error_scan(char buf[MAX_INPUT_BUFFER], const char *addr) { +mp_state_enum error_scan(char buf[MAX_INPUT_BUFFER], const char *addr) { if (strstr(buf, "Network is unreachable") || strstr(buf, "Destination Net Unreachable") || strstr(buf, "No route")) { die(STATE_CRITICAL, _("CRITICAL - Network Unreachable (%s)\n"), addr); } else if (strstr(buf, "Destination Host Unreachable") || strstr(buf, "Address unreachable")) { @@ -543,10 +566,10 @@ int error_scan(char buf[MAX_INPUT_BUFFER], const char *addr) { } else if (!strstr(warn_text, _(WARN_DUPLICATES)) && xasprintf(&warn_text, "%s %s", warn_text, _(WARN_DUPLICATES)) == -1) { die(STATE_UNKNOWN, _("Unable to realloc warn_text\n")); } - return (STATE_WARNING); + return STATE_WARNING; } - return (STATE_OK); + return STATE_OK; } void print_help(void) { diff --git a/plugins/check_ping.d/config.h b/plugins/check_ping.d/config.h new file mode 100644 index 00000000..eb2735a7 --- /dev/null +++ b/plugins/check_ping.d/config.h @@ -0,0 +1,46 @@ +#pragma once + +#include "../../config.h" +#include +#include + +enum { + UNKNOWN_PACKET_LOSS = 200, /* 200% */ + DEFAULT_MAX_PACKETS = 5 /* default no. of ICMP ECHO packets */ +}; + +#define UNKNOWN_TRIP_TIME -1.0 /* -1 seconds */ + +#define MAX_ADDR_START 1 + +typedef struct { + bool display_html; + int max_packets; + + char **addresses; + size_t n_addresses; + + int wpl; + int cpl; + double wrta; + double crta; +} check_ping_config; + +check_ping_config check_ping_config_init() { + check_ping_config tmp = { + .display_html = false, + .max_packets = -1, + + .addresses = NULL, + .n_addresses = 0, + + .wpl = UNKNOWN_PACKET_LOSS, + .cpl = UNKNOWN_PACKET_LOSS, + .wrta = UNKNOWN_TRIP_TIME, + .crta = UNKNOWN_TRIP_TIME, + }; + + tmp.addresses = calloc(MAX_ADDR_START, sizeof(char *)); + tmp.addresses[0] = NULL; + return tmp; +} -- cgit v1.2.3-74-g34f1 From 37c543e2b20657a8f4c120ba6b52e8e605a417bb Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 13:50:39 +0100 Subject: Remove check_nwstat check_nwstat is a plugin which was used to determine the health of things on Novel machines. Since Novel is quite dead (even more so the product, this can be removed and this commit does just that to reduce ressource usage. --- .github/monitoring-plugins.spec | 14 - .gitignore | 1 - REQUIREMENTS | 4 - plugins/Makefile.am | 3 +- plugins/check_nwstat.c | 1527 --------------------------------------- 5 files changed, 1 insertion(+), 1548 deletions(-) delete mode 100644 plugins/check_nwstat.c (limited to 'plugins/Makefile.am') diff --git a/.github/monitoring-plugins.spec b/.github/monitoring-plugins.spec index 64ee34f2..10799128 100644 --- a/.github/monitoring-plugins.spec +++ b/.github/monitoring-plugins.spec @@ -191,7 +191,6 @@ Requires: %{name}-nt Requires: %{name}-ntp Requires: %{name}-ntp_peer Requires: %{name}-ntp_time -Requires: %{name}-nwstat Requires: %{name}-oracle Requires: %{name}-pgsql Requires: %{name}-ping @@ -702,19 +701,6 @@ Provides check_ntp_time of the Monitoring Plugins. -# check_nwstat -%package nwstat -Summary: Monitoring Plugins - check_nwstat -Requires: %{name} = %{version}-%{release} - -%description nwstat -Provides check_nwstat of the Monitoring Plugins. - -%files nwstat -%{plugindir}/check_nwstat - - - # check_oracle %package oracle Summary: Monitoring Plugins - check_oracle diff --git a/.gitignore b/.gitignore index f9cb37e4..7f79265f 100644 --- a/.gitignore +++ b/.gitignore @@ -177,7 +177,6 @@ NP-VERSION-FILE /plugins/check_ntp /plugins/check_ntp_peer /plugins/check_ntp_time -/plugins/check_nwstat /plugins/check_pgsql /plugins/check_ping /plugins/check_pop diff --git a/REQUIREMENTS b/REQUIREMENTS index f3b1c01d..551fdb1a 100644 --- a/REQUIREMENTS +++ b/REQUIREMENTS @@ -87,10 +87,6 @@ check_ifstatus/check_ifoperstatus - Requires Net::SNMP perl module http://www.perl.com/CPAN/modules/by-authors/id/D/DT/DTOWN/ -check_nwstat: - - Requires MRTGEXT NLM for Novell Servers - http://forge.novell.com/modules/xfmod/project/?mrtgext - check_nt: - Requires NSClient to run on the NT server to monitor http://nsclient.ready2run.nl/ diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 6c582a15..7c404a3b 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -27,7 +27,7 @@ MATHLIBS = @MATHLIBS@ #AM_CFLAGS = -Wall libexec_PROGRAMS = check_apt check_cluster check_disk check_dummy check_http check_load \ - check_mrtg check_mrtgtraf check_ntp check_ntp_peer check_nwstat check_ping \ + check_mrtg check_mrtgtraf check_ntp check_ntp_peer check_ping \ check_real check_smtp check_ssh check_tcp check_time check_ntp_time \ check_ups check_users negate \ urlize @EXTRAS@ @@ -131,7 +131,6 @@ check_nagios_LDADD = $(BASEOBJS) check_nt_LDADD = $(NETLIBS) check_ntp_LDADD = $(NETLIBS) $(MATHLIBS) check_ntp_peer_LDADD = $(NETLIBS) $(MATHLIBS) -check_nwstat_LDADD = $(NETLIBS) check_pgsql_LDADD = $(NETLIBS) $(PGLIBS) check_ping_LDADD = $(NETLIBS) check_procs_LDADD = $(BASEOBJS) diff --git a/plugins/check_nwstat.c b/plugins/check_nwstat.c deleted file mode 100644 index 176dfbc8..00000000 --- a/plugins/check_nwstat.c +++ /dev/null @@ -1,1527 +0,0 @@ -/***************************************************************************** - * - * Monitoring check_nwstat plugin - * - * License: GPL - * Copyright (c) 2000-2024 Monitoring Plugins Development Team - * - * Description: - * - * This file contains the check_nwstat plugin - * - * This plugin attempts to contact the MRTGEXT NLM running on a - * Novell server to gather the requested system information. - * - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * - *****************************************************************************/ - -const char *progname = "check_nwstat"; -const char *copyright = "2000-2024"; -const char *email = "devel@monitoring-plugins.org"; - -#include "common.h" -#include "netutils.h" -#include "utils.h" - -enum checkvar { - NONE, - LOAD1, /* check 1 minute CPU load */ - LOAD5, /* check 5 minute CPU load */ - LOAD15, /* check 15 minute CPU load */ - CONNS, /* check number of connections */ - VPF, /* check % free space on volume */ - VMF, /* check MB free space on volume */ - VMU, /* check MB used space on volume */ - VPU, /* check % used space on volume */ - VMP, /* check MB purgeable space on volume */ - VKF, /* check KB free space on volume */ - LTCH, /* check long-term cache hit percentage */ - CBUFF, /* check total cache buffers */ - CDBUFF, /* check dirty cache buffers */ - LRUM, /* check LRU sitting time in minutes */ - DSDB, /* check to see if DS Database is open */ - LOGINS, /* check to see if logins are enabled */ - NRMH, /* check to see NRM Health Status */ - PUPRB, /* check % of used packet receive buffers */ - UPRB, /* check used packet receive buffers */ - SAPENTRIES, /* check SAP entries */ - OFILES, /* check number of open files */ - VKP, /* check KB purgeable space on volume */ - VPP, /* check % purgeable space on volume */ - VKNP, /* check KB not yet purgeable space on volume */ - VPNP, /* check % not yet purgeable space on volume */ - ABENDS, /* check abended thread count */ - CSPROCS, /* check number of current service processes */ - TSYNC, /* check timesync status 0=no 1=yes in sync to the network */ - LRUS, /* check LRU sitting time in seconds */ - DCB, /* check dirty cache buffers as a percentage of the total */ - TCB, /* check total cache buffers as a percentage of the original */ - DSVER, /* check NDS version */ - UPTIME, /* check server uptime */ - NLM, /* check NLM loaded */ - NRMP, /* check NRM Process Values */ - NRMM, /* check NRM Memory Values */ - NRMS, /* check NRM Values */ - NSS1, /* check Statistics from _Admin:Manage_NSS\GeneralStats.xml */ - NSS2, /* check Statistics from _Admin:Manage_NSS\BufferCache.xml */ - NSS3, /* check statistics from _Admin:Manage_NSS\NameCache.xml */ - NSS4, /* check statistics from _Admin:Manage_NSS\FileStats.xml */ - NSS5, /* check statistics from _Admin:Manage_NSS\ObjectCache.xml */ - NSS6, /* check statistics from _Admin:Manage_NSS\Thread.xml */ - NSS7 /* check statistics from _Admin:Manage_NSS\AuthorizationCache.xml */ -}; - -enum { - PORT = 9999 -}; - -static char *server_address = NULL; -static char *volume_name = NULL; -static char *nlm_name = NULL; -static char *nrmp_name = NULL; -static char *nrmm_name = NULL; -static char *nrms_name = NULL; -static char *nss1_name = NULL; -static char *nss2_name = NULL; -static char *nss3_name = NULL; -static char *nss4_name = NULL; -static char *nss5_name = NULL; -static char *nss6_name = NULL; -static char *nss7_name = NULL; -static int server_port = PORT; -static unsigned long warning_value = 0L; -static unsigned long critical_value = 0L; -static bool check_warning_value = false; -static bool check_critical_value = false; -static bool check_netware_version = false; -static enum checkvar vars_to_check = NONE; -static int sap_number = -1; - -static int process_arguments(int /*argc*/, char ** /*argv*/); -static void print_help(void); -void print_usage(void); - -int main(int argc, char **argv) { - int result = STATE_UNKNOWN; - int sd; - char *send_buffer = NULL; - char recv_buffer[MAX_INPUT_BUFFER]; - char *output_message = NULL; - char *temp_buffer = NULL; - char *netware_version = NULL; - - int time_sync_status = 0; - int nrm_health_status = 0; - unsigned long total_cache_buffers = 0; - unsigned long dirty_cache_buffers = 0; - unsigned long open_files = 0; - unsigned long abended_threads = 0; - unsigned long max_service_processes = 0; - unsigned long current_service_processes = 0; - unsigned long free_disk_space = 0L; - unsigned long nrmp_value = 0L; - unsigned long nrmm_value = 0L; - unsigned long nrms_value = 0L; - unsigned long nss1_value = 0L; - unsigned long nss2_value = 0L; - unsigned long nss3_value = 0L; - unsigned long nss4_value = 0L; - unsigned long nss5_value = 0L; - unsigned long nss6_value = 0L; - unsigned long nss7_value = 0L; - unsigned long total_disk_space = 0L; - unsigned long used_disk_space = 0L; - unsigned long percent_used_disk_space = 0L; - unsigned long purgeable_disk_space = 0L; - unsigned long non_purgeable_disk_space = 0L; - unsigned long percent_free_space = 0; - unsigned long percent_purgeable_space = 0; - unsigned long percent_non_purgeable_space = 0; - unsigned long current_connections = 0L; - unsigned long utilization = 0L; - unsigned long cache_hits = 0; - unsigned long cache_buffers = 0L; - unsigned long lru_time = 0L; - unsigned long max_packet_receive_buffers = 0; - unsigned long used_packet_receive_buffers = 0; - unsigned long percent_used_packet_receive_buffers = 0L; - unsigned long sap_entries = 0; - char uptime[MAX_INPUT_BUFFER]; - - setlocale(LC_ALL, ""); - bindtextdomain(PACKAGE, LOCALEDIR); - textdomain(PACKAGE); - - /* Parse extra opts if any */ - argv = np_extra_opts(&argc, argv, progname); - - if (process_arguments(argc, argv) == ERROR) - usage4(_("Could not parse arguments")); - - /* initialize alarm signal handling */ - signal(SIGALRM, socket_timeout_alarm_handler); - - /* set socket timeout */ - alarm(socket_timeout); - - /* open connection */ - my_tcp_connect(server_address, server_port, &sd); - - /* get OS version string */ - if (check_netware_version) { - send_buffer = strdup("S19\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - if (!strcmp(recv_buffer, "-1\n")) - netware_version = strdup(""); - else { - recv_buffer[strlen(recv_buffer) - 1] = 0; - xasprintf(&netware_version, _("NetWare %s: "), recv_buffer); - } - } else - netware_version = strdup(""); - - /* check CPU load */ - if (vars_to_check == LOAD1 || vars_to_check == LOAD5 || vars_to_check == LOAD15) { - - switch (vars_to_check) { - case LOAD1: - temp_buffer = strdup("1"); - break; - case LOAD5: - temp_buffer = strdup("5"); - break; - default: - temp_buffer = strdup("15"); - break; - } - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "UTIL%s\r\n", temp_buffer); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - utilization = strtoul(recv_buffer, NULL, 10); - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - send_buffer = strdup("UPTIME\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - recv_buffer[strlen(recv_buffer) - 1] = 0; - sprintf(uptime, _("Up %s,"), recv_buffer); - - if (check_critical_value && utilization >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && utilization >= warning_value) - result = STATE_WARNING; - - xasprintf(&output_message, _("Load %s - %s %s-min load average = %lu%%|load%s=%lu;%lu;%lu;0;100"), state_text(result), uptime, - temp_buffer, utilization, temp_buffer, utilization, warning_value, critical_value); - - /* check number of user connections */ - } else if (vars_to_check == CONNS) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - send_buffer = strdup("CONNECT\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - current_connections = strtoul(recv_buffer, NULL, 10); - - if (check_critical_value && current_connections >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && current_connections >= warning_value) - result = STATE_WARNING; - - xasprintf(&output_message, _("Conns %s - %lu current connections|Conns=%lu;%lu;%lu;;"), state_text(result), current_connections, - current_connections, warning_value, critical_value); - - /* check % long term cache hits */ - } else if (vars_to_check == LTCH) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - send_buffer = strdup("S1\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - cache_hits = atoi(recv_buffer); - - if (check_critical_value && cache_hits <= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && cache_hits <= warning_value) - result = STATE_WARNING; - - xasprintf(&output_message, _("%s: Long term cache hits = %lu%%"), state_text(result), cache_hits); - - /* check cache buffers */ - } else if (vars_to_check == CBUFF) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - send_buffer = strdup("S2\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - cache_buffers = strtoul(recv_buffer, NULL, 10); - - if (check_critical_value && cache_buffers <= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && cache_buffers <= warning_value) - result = STATE_WARNING; - - xasprintf(&output_message, _("%s: Total cache buffers = %lu|Cachebuffers=%lu;%lu;%lu;;"), state_text(result), cache_buffers, - cache_buffers, warning_value, critical_value); - - /* check dirty cache buffers */ - } else if (vars_to_check == CDBUFF) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - send_buffer = strdup("S3\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - cache_buffers = strtoul(recv_buffer, NULL, 10); - - if (check_critical_value && cache_buffers >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && cache_buffers >= warning_value) - result = STATE_WARNING; - - xasprintf(&output_message, _("%s: Dirty cache buffers = %lu|Dirty-Cache-Buffers=%lu;%lu;%lu;;"), state_text(result), cache_buffers, - cache_buffers, warning_value, critical_value); - - /* check LRU sitting time in minutes */ - } else if (vars_to_check == LRUM) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - send_buffer = strdup("S5\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - lru_time = strtoul(recv_buffer, NULL, 10); - - if (check_critical_value && lru_time <= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && lru_time <= warning_value) - result = STATE_WARNING; - - xasprintf(&output_message, _("%s: LRU sitting time = %lu minutes"), state_text(result), lru_time); - - /* check KB free space on volume */ - } else if (vars_to_check == VKF) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "VKF%s\r\n", volume_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); - result = STATE_CRITICAL; - } else { - free_disk_space = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && free_disk_space <= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && free_disk_space <= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s%lu KB free on volume %s|KBFree%s=%lu;%lu;%lu;;"), (result == STATE_OK) ? "" : _("Only "), - free_disk_space, volume_name, volume_name, free_disk_space, warning_value, critical_value); - } - - /* check MB free space on volume */ - } else if (vars_to_check == VMF) { - - xasprintf(&send_buffer, "VMF%s\r\n", volume_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); - result = STATE_CRITICAL; - } else { - free_disk_space = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && free_disk_space <= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && free_disk_space <= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s%lu MB free on volume %s|MBFree%s=%lu;%lu;%lu;;"), (result == STATE_OK) ? "" : _("Only "), - free_disk_space, volume_name, volume_name, free_disk_space, warning_value, critical_value); - } - /* check MB used space on volume */ - } else if (vars_to_check == VMU) { - - xasprintf(&send_buffer, "VMU%s\r\n", volume_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); - result = STATE_CRITICAL; - } else { - free_disk_space = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && free_disk_space <= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && free_disk_space <= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s%lu MB used on volume %s|MBUsed%s=%lu;%lu;%lu;;"), (result == STATE_OK) ? "" : _("Only "), - free_disk_space, volume_name, volume_name, free_disk_space, warning_value, critical_value); - } - /* check % used space on volume */ - } else if (vars_to_check == VPU) { - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - asprintf(&send_buffer, "VMU%s\r\n", volume_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - asprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); - result = STATE_CRITICAL; - - } else { - used_disk_space = strtoul(recv_buffer, NULL, 10); - close(sd); - my_tcp_connect(server_address, server_port, &sd); - /* get total volume in MB */ - asprintf(&send_buffer, "VMS%s\r\n", volume_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - total_disk_space = strtoul(recv_buffer, NULL, 10); - /* calculate percent used on volume */ - percent_used_disk_space = (unsigned long)(((double)used_disk_space / (double)total_disk_space) * 100.0); - - if (check_critical_value && percent_used_disk_space >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && percent_used_disk_space >= warning_value) - result = STATE_WARNING; - - asprintf(&output_message, _("%lu MB (%lu%%) used on volume %s - total %lu MB|Used space in percent on %s=%lu;%lu;%lu;0;100"), - used_disk_space, percent_used_disk_space, volume_name, total_disk_space, volume_name, percent_used_disk_space, - warning_value, critical_value); - } - - /* check % free space on volume */ - } else if (vars_to_check == VPF) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "VKF%s\r\n", volume_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - - xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); - result = STATE_CRITICAL; - - } else { - - free_disk_space = strtoul(recv_buffer, NULL, 10); - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "VKS%s\r\n", volume_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - total_disk_space = strtoul(recv_buffer, NULL, 10); - - percent_free_space = (unsigned long)(((double)free_disk_space / (double)total_disk_space) * 100.0); - - if (check_critical_value && percent_free_space <= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && percent_free_space <= warning_value) - result = STATE_WARNING; - free_disk_space /= 1024; - total_disk_space /= 1024; - xasprintf(&output_message, _("%lu MB (%lu%%) free on volume %s - total %lu MB|FreeMB%s=%lu;%lu;%lu;0;100"), free_disk_space, - percent_free_space, volume_name, total_disk_space, volume_name, percent_free_space, warning_value, critical_value); - } - - /* check to see if DS Database is open or closed */ - } else if (vars_to_check == DSDB) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - send_buffer = strdup("S11\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - if (atoi(recv_buffer) == 1) - result = STATE_OK; - else - result = STATE_WARNING; - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - send_buffer = strdup("S13\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - temp_buffer = strtok(recv_buffer, "\r\n"); - - xasprintf(&output_message, _("Directory Services Database is %s (DS version %s)"), (result == STATE_OK) ? "open" : "closed", - temp_buffer); - - /* check to see if logins are enabled */ - } else if (vars_to_check == LOGINS) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - send_buffer = strdup("S12\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - if (atoi(recv_buffer) == 1) - result = STATE_OK; - else - result = STATE_WARNING; - - xasprintf(&output_message, _("Logins are %s"), (result == STATE_OK) ? _("enabled") : _("disabled")); - - /* check NRM Health Status Summary*/ - } else if (vars_to_check == NRMH) { - - xasprintf(&send_buffer, "NRMH\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - nrm_health_status = atoi(recv_buffer); - - if (nrm_health_status == 2) { - result = STATE_OK; - xasprintf(&output_message, _("CRITICAL - NRM Status is bad!")); - } else { - if (nrm_health_status == 1) { - result = STATE_WARNING; - xasprintf(&output_message, _("Warning - NRM Status is suspect!")); - } - - xasprintf(&output_message, _("OK - NRM Status is good!")); - } - - /* check packet receive buffers */ - } else if (vars_to_check == UPRB || vars_to_check == PUPRB) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "S15\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - used_packet_receive_buffers = atoi(recv_buffer); - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "S16\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - max_packet_receive_buffers = atoi(recv_buffer); - - percent_used_packet_receive_buffers = - (unsigned long)(((double)used_packet_receive_buffers / (double)max_packet_receive_buffers) * 100.0); - - if (vars_to_check == UPRB) { - if (check_critical_value && used_packet_receive_buffers >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && used_packet_receive_buffers >= warning_value) - result = STATE_WARNING; - } else { - if (check_critical_value && percent_used_packet_receive_buffers >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && percent_used_packet_receive_buffers >= warning_value) - result = STATE_WARNING; - } - - xasprintf(&output_message, _("%lu of %lu (%lu%%) packet receive buffers used"), used_packet_receive_buffers, - max_packet_receive_buffers, percent_used_packet_receive_buffers); - - /* check SAP table entries */ - } else if (vars_to_check == SAPENTRIES) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - if (sap_number == -1) - xasprintf(&send_buffer, "S9\r\n"); - else - xasprintf(&send_buffer, "S9.%d\r\n", sap_number); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - sap_entries = atoi(recv_buffer); - - if (check_critical_value && sap_entries >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && sap_entries >= warning_value) - result = STATE_WARNING; - - if (sap_number == -1) - xasprintf(&output_message, _("%lu entries in SAP table"), sap_entries); - else - xasprintf(&output_message, _("%lu entries in SAP table for SAP type %d"), sap_entries, sap_number); - - /* check KB purgeable space on volume */ - } else if (vars_to_check == VKP) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "VKP%s\r\n", volume_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); - result = STATE_CRITICAL; - } else { - purgeable_disk_space = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && purgeable_disk_space >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && purgeable_disk_space >= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s%lu KB purgeable on volume %s|Purge%s=%lu;%lu;%lu;;"), (result == STATE_OK) ? "" : _("Only "), - purgeable_disk_space, volume_name, volume_name, purgeable_disk_space, warning_value, critical_value); - } - /* check MB purgeable space on volume */ - } else if (vars_to_check == VMP) { - - xasprintf(&send_buffer, "VMP%s\r\n", volume_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); - result = STATE_CRITICAL; - } else { - purgeable_disk_space = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && purgeable_disk_space >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && purgeable_disk_space >= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s%lu MB purgeable on volume %s|Purge%s=%lu;%lu;%lu;;"), (result == STATE_OK) ? "" : _("Only "), - purgeable_disk_space, volume_name, volume_name, purgeable_disk_space, warning_value, critical_value); - } - - /* check % purgeable space on volume */ - } else if (vars_to_check == VPP) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "VKP%s\r\n", volume_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - - xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); - result = STATE_CRITICAL; - - } else { - - purgeable_disk_space = strtoul(recv_buffer, NULL, 10); - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "VKS%s\r\n", volume_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - total_disk_space = strtoul(recv_buffer, NULL, 10); - - percent_purgeable_space = (unsigned long)(((double)purgeable_disk_space / (double)total_disk_space) * 100.0); - - if (check_critical_value && percent_purgeable_space >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && percent_purgeable_space >= warning_value) - result = STATE_WARNING; - purgeable_disk_space /= 1024; - xasprintf(&output_message, _("%lu MB (%lu%%) purgeable on volume %s|Purgeable%s=%lu;%lu;%lu;0;100"), purgeable_disk_space, - percent_purgeable_space, volume_name, volume_name, percent_purgeable_space, warning_value, critical_value); - } - - /* check KB not yet purgeable space on volume */ - } else if (vars_to_check == VKNP) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "VKNP%s\r\n", volume_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); - result = STATE_CRITICAL; - } else { - non_purgeable_disk_space = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && non_purgeable_disk_space >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && non_purgeable_disk_space >= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s%lu KB not yet purgeable on volume %s"), (result == STATE_OK) ? "" : _("Only "), - non_purgeable_disk_space, volume_name); - } - - /* check % not yet purgeable space on volume */ - } else if (vars_to_check == VPNP) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "VKNP%s\r\n", volume_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - - xasprintf(&output_message, _("CRITICAL - Volume '%s' does not exist!"), volume_name); - result = STATE_CRITICAL; - - } else { - - non_purgeable_disk_space = strtoul(recv_buffer, NULL, 10); - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "VKS%s\r\n", volume_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - total_disk_space = strtoul(recv_buffer, NULL, 10); - - percent_non_purgeable_space = (unsigned long)(((double)non_purgeable_disk_space / (double)total_disk_space) * 100.0); - - if (check_critical_value && percent_non_purgeable_space >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && percent_non_purgeable_space >= warning_value) - result = STATE_WARNING; - purgeable_disk_space /= 1024; - xasprintf(&output_message, _("%lu MB (%lu%%) not yet purgeable on volume %s"), non_purgeable_disk_space, - percent_non_purgeable_space, volume_name); - } - - /* check # of open files */ - } else if (vars_to_check == OFILES) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "S18\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - open_files = atoi(recv_buffer); - - if (check_critical_value && open_files >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && open_files >= warning_value) - result = STATE_WARNING; - - xasprintf(&output_message, _("%lu open files|Openfiles=%lu;%lu;%lu;0,0"), open_files, open_files, warning_value, critical_value); - - /* check # of abended threads (Netware > 5.x only) */ - } else if (vars_to_check == ABENDS) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "S17\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - abended_threads = atoi(recv_buffer); - - if (check_critical_value && abended_threads >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && abended_threads >= warning_value) - result = STATE_WARNING; - - xasprintf(&output_message, _("%lu abended threads|Abends=%lu;%lu;%lu;;"), abended_threads, abended_threads, warning_value, - critical_value); - - /* check # of current service processes (Netware 5.x only) */ - } else if (vars_to_check == CSPROCS) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "S20\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - max_service_processes = atoi(recv_buffer); - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "S21\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - current_service_processes = atoi(recv_buffer); - - if (check_critical_value && current_service_processes >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && current_service_processes >= warning_value) - result = STATE_WARNING; - - xasprintf(&output_message, _("%lu current service processes (%lu max)|Processes=%lu;%lu;%lu;0;%lu"), current_service_processes, - max_service_processes, current_service_processes, warning_value, critical_value, max_service_processes); - - /* check # Timesync Status */ - } else if (vars_to_check == TSYNC) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "S22\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - time_sync_status = atoi(recv_buffer); - - if (time_sync_status == 0) { - result = STATE_CRITICAL; - xasprintf(&output_message, _("CRITICAL - Time not in sync with network!")); - } else { - xasprintf(&output_message, _("OK - Time in sync with network!")); - } - - /* check LRU sitting time in secondss */ - } else if (vars_to_check == LRUS) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - send_buffer = strdup("S4\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - lru_time = strtoul(recv_buffer, NULL, 10); - - if (check_critical_value && lru_time <= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && lru_time <= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("LRU sitting time = %lu seconds"), lru_time); - - /* check % dirty cacheobuffers as a percentage of the total*/ - } else if (vars_to_check == DCB) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - send_buffer = strdup("S6\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - dirty_cache_buffers = atoi(recv_buffer); - - if (check_critical_value && dirty_cache_buffers <= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && dirty_cache_buffers <= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("Dirty cache buffers = %lu%% of the total|DCB=%lu;%lu;%lu;0;100"), dirty_cache_buffers, - dirty_cache_buffers, warning_value, critical_value); - - /* check % total cache buffers as a percentage of the original*/ - } else if (vars_to_check == TCB) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - send_buffer = strdup("S7\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - total_cache_buffers = atoi(recv_buffer); - - if (check_critical_value && total_cache_buffers <= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && total_cache_buffers <= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("Total cache buffers = %lu%% of the original|TCB=%lu;%lu;%lu;0;100"), total_cache_buffers, - total_cache_buffers, warning_value, critical_value); - - } else if (vars_to_check == DSVER) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "S13\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - recv_buffer[strlen(recv_buffer) - 1] = 0; - - xasprintf(&output_message, _("NDS Version %s"), recv_buffer); - - } else if (vars_to_check == UPTIME) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "UPTIME\r\n"); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - recv_buffer[sizeof(recv_buffer) - 1] = 0; - recv_buffer[strlen(recv_buffer) - 1] = 0; - - xasprintf(&output_message, _("Up %s"), recv_buffer); - - } else if (vars_to_check == NLM) { - - close(sd); - my_tcp_connect(server_address, server_port, &sd); - - xasprintf(&send_buffer, "S24:%s\r\n", nlm_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - recv_buffer[strlen(recv_buffer) - 1] = 0; - if (strcmp(recv_buffer, "-1")) { - xasprintf(&output_message, _("Module %s version %s is loaded"), nlm_name, recv_buffer); - } else { - result = STATE_CRITICAL; - xasprintf(&output_message, _("Module %s is not loaded"), nlm_name); - } - } else if (vars_to_check == NRMP) { - - xasprintf(&send_buffer, "NRMP:%s\r\n", nrmp_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nrmp_name); - result = STATE_CRITICAL; - } else { - nrmp_value = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && nrmp_value <= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && nrmp_value <= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nrmp_name, nrmp_value, nrmp_name, nrmp_value, warning_value, - critical_value); - } - - } else if (vars_to_check == NRMM) { - - xasprintf(&send_buffer, "NRMM:%s\r\n", nrmm_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nrmm_name); - result = STATE_CRITICAL; - } else { - nrmm_value = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && nrmm_value <= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && nrmm_value <= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nrmm_name, nrmm_value, nrmm_name, nrmm_value, warning_value, - critical_value); - } - - } else if (vars_to_check == NRMS) { - - xasprintf(&send_buffer, "NRMS:%s\r\n", nrms_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nrms_name); - result = STATE_CRITICAL; - } else { - nrms_value = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && nrms_value >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && nrms_value >= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nrms_name, nrms_value, nrms_name, nrms_value, warning_value, - critical_value); - } - - } else if (vars_to_check == NSS1) { - - xasprintf(&send_buffer, "NSS1:%s\r\n", nss1_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nss1_name); - result = STATE_CRITICAL; - } else { - nss1_value = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && nss1_value >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && nss1_value >= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nss1_name, nss1_value, nss1_name, nss1_value, warning_value, - critical_value); - } - - } else if (vars_to_check == NSS2) { - - xasprintf(&send_buffer, "NSS2:%s\r\n", nss2_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nss2_name); - result = STATE_CRITICAL; - } else { - nss2_value = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && nss2_value >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && nss2_value >= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nss2_name, nss2_value, nss2_name, nss2_value, warning_value, - critical_value); - } - - } else if (vars_to_check == NSS3) { - - xasprintf(&send_buffer, "NSS3:%s\r\n", nss3_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nss3_name); - result = STATE_CRITICAL; - } else { - nss3_value = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && nss3_value >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && nss3_value >= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nss3_name, nss3_value, nss3_name, nss3_value, warning_value, - critical_value); - } - - } else if (vars_to_check == NSS4) { - - xasprintf(&send_buffer, "NSS4:%s\r\n", nss4_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nss4_name); - result = STATE_CRITICAL; - } else { - nss4_value = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && nss4_value >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && nss4_value >= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nss4_name, nss4_value, nss4_name, nss4_value, warning_value, - critical_value); - } - - } else if (vars_to_check == NSS5) { - - xasprintf(&send_buffer, "NSS5:%s\r\n", nss5_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nss5_name); - result = STATE_CRITICAL; - } else { - nss5_value = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && nss5_value >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && nss5_value >= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nss5_name, nss5_value, nss5_name, nss5_value, warning_value, - critical_value); - } - - } else if (vars_to_check == NSS6) { - - xasprintf(&send_buffer, "NSS6:%s\r\n", nss6_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nss6_name); - result = STATE_CRITICAL; - } else { - nss6_value = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && nss6_value >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && nss6_value >= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nss6_name, nss6_value, nss6_name, nss6_value, warning_value, - critical_value); - } - - } else if (vars_to_check == NSS7) { - - xasprintf(&send_buffer, "NSS7:%s\r\n", nss7_name); - result = send_tcp_request(sd, send_buffer, recv_buffer, sizeof(recv_buffer)); - if (result != STATE_OK) - return result; - - if (!strcmp(recv_buffer, "-1\n")) { - xasprintf(&output_message, _("CRITICAL - Value '%s' does not exist!"), nss7_name); - result = STATE_CRITICAL; - } else { - nss7_value = strtoul(recv_buffer, NULL, 10); - if (check_critical_value && nss7_value >= critical_value) - result = STATE_CRITICAL; - else if (check_warning_value && nss7_value >= warning_value) - result = STATE_WARNING; - xasprintf(&output_message, _("%s is %lu|%s=%lu;%lu;%lu;;"), nss7_name, nss7_value, nss7_name, nss7_value, warning_value, - critical_value); - } - - } else { - - output_message = strdup(_("Nothing to check!\n")); - result = STATE_UNKNOWN; - } - - close(sd); - - /* reset timeout */ - alarm(0); - - printf("%s%s\n", netware_version, output_message); - - return result; -} - -/* process command-line arguments */ -int process_arguments(int argc, char **argv) { - int c; - - int option = 0; - static struct option longopts[] = {{"port", required_argument, 0, 'p'}, {"timeout", required_argument, 0, 't'}, - {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, - {"variable", required_argument, 0, 'v'}, {"hostname", required_argument, 0, 'H'}, - {"osversion", no_argument, 0, 'o'}, {"version", no_argument, 0, 'V'}, - {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; - - /* no options were supplied */ - if (argc < 2) - return ERROR; - - /* backwards compatibility */ - if (!is_option(argv[1])) { - server_address = argv[1]; - argv[1] = argv[0]; - argv = &argv[1]; - argc--; - } - - for (c = 1; c < argc; c++) { - if (strcmp("-to", argv[c]) == 0) - strcpy(argv[c], "-t"); - else if (strcmp("-wv", argv[c]) == 0) - strcpy(argv[c], "-w"); - else if (strcmp("-cv", argv[c]) == 0) - strcpy(argv[c], "-c"); - } - - while (1) { - c = getopt_long(argc, argv, "+hoVH:t:c:w:p:v:", longopts, &option); - - if (c == -1 || c == EOF || c == 1) - break; - - switch (c) { - case '?': /* print short usage statement if args not parsable */ - usage5(); - case 'h': /* help */ - print_help(); - exit(STATE_UNKNOWN); - case 'V': /* version */ - print_revision(progname, NP_VERSION); - exit(STATE_UNKNOWN); - case 'H': /* hostname */ - server_address = optarg; - break; - case 'o': /* display nos version */ - check_netware_version = true; - break; - case 'p': /* port */ - if (is_intnonneg(optarg)) - server_port = atoi(optarg); - else - die(STATE_UNKNOWN, _("Server port an integer\n")); - break; - case 'v': - if (strlen(optarg) < 3) - return ERROR; - if (!strcmp(optarg, "LOAD1")) - vars_to_check = LOAD1; - else if (!strcmp(optarg, "LOAD5")) - vars_to_check = LOAD5; - else if (!strcmp(optarg, "LOAD15")) - vars_to_check = LOAD15; - else if (!strcmp(optarg, "CONNS")) - vars_to_check = CONNS; - else if (!strcmp(optarg, "LTCH")) - vars_to_check = LTCH; - else if (!strcmp(optarg, "DCB")) - vars_to_check = DCB; - else if (!strcmp(optarg, "TCB")) - vars_to_check = TCB; - else if (!strcmp(optarg, "CBUFF")) - vars_to_check = CBUFF; - else if (!strcmp(optarg, "CDBUFF")) - vars_to_check = CDBUFF; - else if (!strcmp(optarg, "LRUM")) - vars_to_check = LRUM; - else if (!strcmp(optarg, "LRUS")) - vars_to_check = LRUS; - else if (strncmp(optarg, "VPF", 3) == 0) { - vars_to_check = VPF; - volume_name = strdup(optarg + 3); - if (!strcmp(volume_name, "")) - volume_name = strdup("SYS"); - } else if (strncmp(optarg, "VKF", 3) == 0) { - vars_to_check = VKF; - volume_name = strdup(optarg + 3); - if (!strcmp(volume_name, "")) - volume_name = strdup("SYS"); - } else if (strncmp(optarg, "VMF", 3) == 0) { - vars_to_check = VMF; - volume_name = strdup(optarg + 3); - if (!strcmp(volume_name, "")) - volume_name = strdup("SYS"); - } else if (!strcmp(optarg, "DSDB")) - vars_to_check = DSDB; - else if (!strcmp(optarg, "LOGINS")) - vars_to_check = LOGINS; - else if (!strcmp(optarg, "NRMH")) - vars_to_check = NRMH; - else if (!strcmp(optarg, "UPRB")) - vars_to_check = UPRB; - else if (!strcmp(optarg, "PUPRB")) - vars_to_check = PUPRB; - else if (!strncmp(optarg, "SAPENTRIES", 10)) { - vars_to_check = SAPENTRIES; - if (strlen(optarg) > 10) - sap_number = atoi(optarg + 10); - else - sap_number = -1; - } else if (!strcmp(optarg, "OFILES")) - vars_to_check = OFILES; - else if (strncmp(optarg, "VKP", 3) == 0) { - vars_to_check = VKP; - volume_name = strdup(optarg + 3); - if (!strcmp(volume_name, "")) - volume_name = strdup("SYS"); - } else if (strncmp(optarg, "VMP", 3) == 0) { - vars_to_check = VMP; - volume_name = strdup(optarg + 3); - if (!strcmp(volume_name, "")) - volume_name = strdup("SYS"); - } else if (strncmp(optarg, "VMU", 3) == 0) { - vars_to_check = VMU; - volume_name = strdup(optarg + 3); - if (!strcmp(volume_name, "")) - volume_name = strdup("SYS"); - } else if (strncmp(optarg, "VPU", 3) == 0) { - vars_to_check = VPU; - volume_name = strdup(optarg + 3); - if (!strcmp(volume_name, "")) - volume_name = strdup("SYS"); - } else if (strncmp(optarg, "VPP", 3) == 0) { - vars_to_check = VPP; - volume_name = strdup(optarg + 3); - if (!strcmp(volume_name, "")) - volume_name = strdup("SYS"); - } else if (strncmp(optarg, "VKNP", 4) == 0) { - vars_to_check = VKNP; - volume_name = strdup(optarg + 4); - if (!strcmp(volume_name, "")) - volume_name = strdup("SYS"); - } else if (strncmp(optarg, "VPNP", 4) == 0) { - vars_to_check = VPNP; - volume_name = strdup(optarg + 4); - if (!strcmp(volume_name, "")) - volume_name = strdup("SYS"); - } else if (!strcmp(optarg, "ABENDS")) - vars_to_check = ABENDS; - else if (!strcmp(optarg, "CSPROCS")) - vars_to_check = CSPROCS; - else if (!strcmp(optarg, "TSYNC")) - vars_to_check = TSYNC; - else if (!strcmp(optarg, "DSVER")) - vars_to_check = DSVER; - else if (!strcmp(optarg, "UPTIME")) { - vars_to_check = UPTIME; - } else if (strncmp(optarg, "NLM:", 4) == 0) { - vars_to_check = NLM; - nlm_name = strdup(optarg + 4); - } else if (strncmp(optarg, "NRMP", 4) == 0) { - vars_to_check = NRMP; - nrmp_name = strdup(optarg + 4); - if (!strcmp(nrmp_name, "")) - nrmp_name = strdup("AVAILABLE_MEMORY"); - } else if (strncmp(optarg, "NRMM", 4) == 0) { - vars_to_check = NRMM; - nrmm_name = strdup(optarg + 4); - if (!strcmp(nrmm_name, "")) - nrmm_name = strdup("AVAILABLE_CACHE_MEMORY"); - - } - - else if (strncmp(optarg, "NRMS", 4) == 0) { - vars_to_check = NRMS; - nrms_name = strdup(optarg + 4); - if (!strcmp(nrms_name, "")) - nrms_name = strdup("USED_SWAP_SPACE"); - - } - - else if (strncmp(optarg, "NSS1", 4) == 0) { - vars_to_check = NSS1; - nss1_name = strdup(optarg + 4); - if (!strcmp(nss1_name, "")) - nss1_name = strdup("CURRENTBUFFERCACHESIZE"); - - } - - else if (strncmp(optarg, "NSS2", 4) == 0) { - vars_to_check = NSS2; - nss2_name = strdup(optarg + 4); - if (!strcmp(nss2_name, "")) - nss2_name = strdup("CACHEHITS"); - - } - - else if (strncmp(optarg, "NSS3", 4) == 0) { - vars_to_check = NSS3; - nss3_name = strdup(optarg + 4); - if (!strcmp(nss3_name, "")) - nss3_name = strdup("CACHEGITPERCENT"); - - } - - else if (strncmp(optarg, "NSS4", 4) == 0) { - vars_to_check = NSS4; - nss4_name = strdup(optarg + 4); - if (!strcmp(nss4_name, "")) - nss4_name = strdup("CURRENTOPENCOUNT"); - - } - - else if (strncmp(optarg, "NSS5", 4) == 0) { - vars_to_check = NSS5; - nss5_name = strdup(optarg + 4); - if (!strcmp(nss5_name, "")) - nss5_name = strdup("CACHEMISSES"); - - } - - else if (strncmp(optarg, "NSS6", 4) == 0) { - vars_to_check = NSS6; - nss6_name = strdup(optarg + 4); - if (!strcmp(nss6_name, "")) - nss6_name = strdup("PENDINGWORKSCOUNT"); - - } - - else if (strncmp(optarg, "NSS7", 4) == 0) { - vars_to_check = NSS7; - nss7_name = strdup(optarg + 4); - if (!strcmp(nss7_name, "")) - nss7_name = strdup("CACHESIZE"); - - } - - else - return ERROR; - break; - case 'w': /* warning threshold */ - warning_value = strtoul(optarg, NULL, 10); - check_warning_value = true; - break; - case 'c': /* critical threshold */ - critical_value = strtoul(optarg, NULL, 10); - check_critical_value = true; - break; - case 't': /* timeout */ - socket_timeout = atoi(optarg); - if (socket_timeout <= 0) - return ERROR; - } - } - - return OK; -} - -void print_help(void) { - char *myport; - xasprintf(&myport, "%d", PORT); - - print_revision(progname, NP_VERSION); - - printf("Copyright (c) 1999 Ethan Galstad \n"); - printf(COPYRIGHT, copyright, email); - - printf("%s\n", _("This plugin attempts to contact the MRTGEXT NLM running on a")); - printf("%s\n", _("Novell server to gather the requested system information.")); - - printf("\n\n"); - - print_usage(); - - printf(UT_HELP_VRSN); - printf(UT_EXTRA_OPTS); - - printf(UT_HOST_PORT, 'p', myport); - - printf(" %s\n", "-v, --variable=STRING"); - printf(" %s\n", _("Variable to check. Valid variables include:")); - printf(" %s\n", _("LOAD1 = 1 minute average CPU load")); - printf(" %s\n", _("LOAD5 = 5 minute average CPU load")); - printf(" %s\n", _("LOAD15 = 15 minute average CPU load")); - printf(" %s\n", _("CSPROCS = number of current service processes (NW 5.x only)")); - printf(" %s\n", _("ABENDS = number of abended threads (NW 5.x only)")); - printf(" %s\n", _("UPTIME = server uptime")); - printf(" %s\n", _("LTCH = percent long term cache hits")); - printf(" %s\n", _("CBUFF = current number of cache buffers")); - printf(" %s\n", _("CDBUFF = current number of dirty cache buffers")); - printf(" %s\n", _("DCB = dirty cache buffers as a percentage of the total")); - printf(" %s\n", _("TCB = dirty cache buffers as a percentage of the original")); - printf(" %s\n", _("OFILES = number of open files")); - printf(" %s\n", _(" VMF = MB of free space on Volume ")); - printf(" %s\n", _(" VMU = MB used space on Volume ")); - printf(" %s\n", _(" VPU = percent used space on Volume ")); - printf(" %s\n", _(" VMP = MB of purgeable space on Volume ")); - printf(" %s\n", _(" VPF = percent free space on volume ")); - printf(" %s\n", _(" VKF = KB of free space on volume ")); - printf(" %s\n", _(" VPP = percent purgeable space on volume ")); - printf(" %s\n", _(" VKP = KB of purgeable space on volume ")); - printf(" %s\n", _(" VPNP = percent not yet purgeable space on volume ")); - printf(" %s\n", _(" VKNP = KB of not yet purgeable space on volume ")); - printf(" %s\n", _(" LRUM = LRU sitting time in minutes")); - printf(" %s\n", _(" LRUS = LRU sitting time in seconds")); - printf(" %s\n", _(" DSDB = check to see if DS Database is open")); - printf(" %s\n", _(" DSVER = NDS version")); - printf(" %s\n", _(" UPRB = used packet receive buffers")); - printf(" %s\n", _(" PUPRB = percent (of max) used packet receive buffers")); - printf(" %s\n", _(" SAPENTRIES = number of entries in the SAP table")); - printf(" %s\n", _(" SAPENTRIES = number of entries in the SAP table for SAP type ")); - printf(" %s\n", _(" TSYNC = timesync status")); - printf(" %s\n", _(" LOGINS = check to see if logins are enabled")); - printf(" %s\n", _(" CONNS = number of currently licensed connections")); - printf(" %s\n", _(" NRMH = NRM Summary Status")); - printf(" %s\n", _(" NRMP = Returns the current value for a NRM health item")); - printf(" %s\n", _(" NRMM = Returns the current memory stats from NRM")); - printf(" %s\n", _(" NRMS = Returns the current Swapfile stats from NRM")); - printf(" %s\n", _(" NSS1 = Statistics from _Admin:Manage_NSS\\GeneralStats.xml")); - printf(" %s\n", _(" NSS3 = Statistics from _Admin:Manage_NSS\\NameCache.xml")); - printf(" %s\n", _(" NSS4 = Statistics from _Admin:Manage_NSS\\FileStats.xml")); - printf(" %s\n", _(" NSS5 = Statistics from _Admin:Manage_NSS\\ObjectCache.xml")); - printf(" %s\n", _(" NSS6 = Statistics from _Admin:Manage_NSS\\Thread.xml")); - printf(" %s\n", _(" NSS7 = Statistics from _Admin:Manage_NSS\\AuthorizationCache.xml")); - printf(" %s\n", _(" NLM: = check if NLM is loaded and report version")); - printf(" %s\n", _(" (e.g. NLM:TSANDS.NLM)")); - printf("\n"); - printf(" %s\n", "-w, --warning=INTEGER"); - printf(" %s\n", _("Threshold which will result in a warning status")); - printf(" %s\n", "-c, --critical=INTEGER"); - printf(" %s\n", _("Threshold which will result in a critical status")); - printf(" %s\n", "-o, --osversion"); - printf(" %s\n", _("Include server version string in results")); - - printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); - - printf("\n"); - printf("%s\n", _("Notes:")); - printf(" %s\n", _("- This plugin requires that the MRTGEXT.NLM file from James Drews' MRTG")); - printf(" %s\n", _(" extension for NetWare be loaded on the Novell servers you wish to check.")); - printf(" %s\n", _(" (available from http://www.engr.wisc.edu/~drews/mrtg/)")); - printf(" %s\n", _("- Values for critical thresholds should be lower than warning thresholds")); - printf(" %s\n", _(" when the following variables are checked: VPF, VKF, LTCH, CBUFF, DCB, ")); - printf(" %s\n", _(" TCB, LRUS and LRUM.")); - - printf(UT_SUPPORT); -} - -void print_usage(void) { - printf("%s\n", _("Usage:")); - printf("%s -H host [-p port] [-v variable] [-w warning] [-c critical] [-t timeout]\n", progname); -} -- cgit v1.2.3-74-g34f1 From 3008d521c1284f6f182d9cc10d56afdaa3978a04 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 16:51:02 +0100 Subject: Refactor check_radius --- plugins/Makefile.am | 1 + plugins/check_radius.c | 126 +++++++++++++++++++++++----------------- plugins/check_radius.d/config.h | 42 ++++++++++++++ 3 files changed, 115 insertions(+), 54 deletions(-) create mode 100644 plugins/check_radius.d/config.h (limited to 'plugins/Makefile.am') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 6c582a15..dacd36e8 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -53,6 +53,7 @@ EXTRA_DIST = t \ check_ldap.d \ check_hpjd.d \ check_game.d \ + check_radius.d \ check_nagios.d \ check_dbi.d \ check_ssh.d \ diff --git a/plugins/check_radius.c b/plugins/check_radius.c index ab7c6519..6a27e113 100644 --- a/plugins/check_radius.c +++ b/plugins/check_radius.c @@ -36,6 +36,7 @@ const char *email = "devel@monitoring-plugins.org"; #include "utils.h" #include "netutils.h" #include "states.h" +#include "check_radius.d/config.h" #if defined(HAVE_LIBRADCLI) # include @@ -47,7 +48,11 @@ const char *email = "devel@monitoring-plugins.org"; # include #endif -static int process_arguments(int /*argc*/, char ** /*argv*/); +typedef struct { + int errorcode; + check_radius_config config; +} check_radius_config_wrapper; +static check_radius_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); static void print_help(void); void print_usage(void); @@ -79,21 +84,8 @@ void print_usage(void); # define REJECT_RC BADRESP_RC #endif -static int my_rc_read_config(char * /*a*/); +static int my_rc_read_config(char * /*a*/, rc_handle ** /*rch*/); -#if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) -static rc_handle *rch = NULL; -#endif - -static char *server = NULL; -static char *username = NULL; -static char *password = NULL; -static char *nasid = NULL; -static char *nasipaddress = NULL; -static char *expect = NULL; -static char *config_file = NULL; -static unsigned short port = PW_AUTH_UDP_PORT; -static int retries = 1; static bool verbose = false; /****************************************************************************** @@ -157,12 +149,20 @@ 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_radius_config_wrapper tmp_config = process_arguments(argc, argv); + + if (tmp_config.errorcode == ERROR) { usage4(_("Could not parse arguments")); } + check_radius_config config = tmp_config.config; + +#if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) + rc_handle *rch = NULL; +#endif + char *str = strdup("dictionary"); - if ((config_file && my_rc_read_config(config_file)) || my_rc_read_dictionary(my_rc_conf_str(str))) { + if ((config.config_file && my_rc_read_config(config.config_file, &rch)) || my_rc_read_dictionary(my_rc_conf_str(str))) { die(STATE_UNKNOWN, _("Config file error\n")); } @@ -171,36 +171,36 @@ int main(int argc, char **argv) { SEND_DATA data; memset(&data, 0, sizeof(data)); if (!(my_rc_avpair_add(&data.send_pairs, PW_SERVICE_TYPE, &service, 0) && - my_rc_avpair_add(&data.send_pairs, PW_USER_NAME, username, 0) && - my_rc_avpair_add(&data.send_pairs, PW_USER_PASSWORD, password, 0))) { + my_rc_avpair_add(&data.send_pairs, PW_USER_NAME, config.username, 0) && + my_rc_avpair_add(&data.send_pairs, PW_USER_PASSWORD, config.password, 0))) { die(STATE_UNKNOWN, _("Out of Memory?\n")); } - if (nasid != NULL) { - if (!(my_rc_avpair_add(&data.send_pairs, PW_NAS_IDENTIFIER, nasid, 0))) { + if (config.nas_id != NULL) { + if (!(my_rc_avpair_add(&data.send_pairs, PW_NAS_IDENTIFIER, config.nas_id, 0))) { die(STATE_UNKNOWN, _("Invalid NAS-Identifier\n")); } } char name[HOST_NAME_MAX]; - if (nasipaddress == NULL) { + if (config.nas_ip_address == NULL) { if (gethostname(name, sizeof(name)) != 0) { die(STATE_UNKNOWN, _("gethostname() failed!\n")); } - nasipaddress = name; + config.nas_ip_address = name; } - struct sockaddr_storage ss; - if (!dns_lookup(nasipaddress, &ss, AF_INET)) { /* TODO: Support IPv6. */ + struct sockaddr_storage radius_server_socket; + if (!dns_lookup(config.nas_ip_address, &radius_server_socket, AF_INET)) { /* TODO: Support IPv6. */ die(STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); } - uint32_t client_id = ntohl(((struct sockaddr_in *)&ss)->sin_addr.s_addr); + uint32_t client_id = ntohl(((struct sockaddr_in *)&radius_server_socket)->sin_addr.s_addr); if (my_rc_avpair_add(&(data.send_pairs), PW_NAS_IP_ADDRESS, &client_id, 0) == NULL) { die(STATE_UNKNOWN, _("Invalid NAS-IP-Address\n")); } - my_rc_buildreq(&data, PW_ACCESS_REQUEST, server, port, (int)timeout_interval, retries); + my_rc_buildreq(&data, PW_ACCESS_REQUEST, config.server, config.port, (int)timeout_interval, config.retries); #ifdef RC_BUFFER_LEN char msg[RC_BUFFER_LEN]; @@ -208,36 +208,49 @@ int main(int argc, char **argv) { char msg[BUFFER_LEN]; #endif - mp_state_enum result = my_rc_send_server(&data, msg); + int result = my_rc_send_server(&data, msg); rc_avpair_free(data.send_pairs); if (data.receive_pairs) { rc_avpair_free(data.receive_pairs); } if (result == TIMEOUT_RC) { - die(STATE_CRITICAL, _("Timeout\n")); + printf("Timeout\n"); + exit(STATE_CRITICAL); } + if (result == ERROR_RC) { - die(STATE_CRITICAL, _("Auth Error\n")); + printf(_("Auth Error\n")); + exit(STATE_CRITICAL); } + if (result == REJECT_RC) { - die(STATE_WARNING, _("Auth Failed\n")); + printf(_("Auth Failed\n")); + exit(STATE_WARNING); } + if (result == BADRESP_RC) { - die(STATE_WARNING, _("Bad Response\n")); + printf(_("Bad Response\n")); + exit(STATE_WARNING); } - if (expect && !strstr(msg, expect)) { - die(STATE_WARNING, "%s\n", msg); + + if (config.expect && !strstr(msg, config.expect)) { + printf("%s\n", msg); + exit(STATE_WARNING); } + if (result == OK_RC) { - die(STATE_OK, _("Auth OK\n")); + printf(_("Auth OK\n")); + exit(STATE_OK); } + (void)snprintf(msg, sizeof(msg), _("Unexpected result code %d"), result); - die(STATE_UNKNOWN, "%s\n", msg); + printf("%s\n", msg); + exit(STATE_UNKNOWN); } /* process command-line arguments */ -int process_arguments(int argc, char **argv) { +check_radius_config_wrapper process_arguments(int argc, char **argv) { static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'P'}, {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, {"nas-id", required_argument, 0, 'n'}, {"nas-ip-address", required_argument, 0, 'N'}, @@ -246,6 +259,11 @@ int process_arguments(int argc, char **argv) { {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; + check_radius_config_wrapper result = { + .errorcode = OK, + .config = check_radius_config_init(), + }; + while (true) { int option = 0; int option_index = getopt_long(argc, argv, "+hVvH:P:F:u:p:n:N:t:r:e:", longopts, &option); @@ -270,20 +288,20 @@ int process_arguments(int argc, char **argv) { if (!is_host(optarg)) { usage2(_("Invalid hostname/address"), optarg); } - server = optarg; + result.config.server = optarg; break; case 'P': /* port */ if (is_intnonneg(optarg)) { - port = (unsigned short)atoi(optarg); + result.config.port = (unsigned short)atoi(optarg); } else { usage4(_("Port must be a positive integer")); } break; case 'u': /* username */ - username = optarg; + result.config.username = optarg; break; case 'p': /* password */ - password = strdup(optarg); + result.config.password = strdup(optarg); /* Delete the password from process list */ while (*optarg != '\0') { @@ -292,20 +310,20 @@ int process_arguments(int argc, char **argv) { } break; case 'n': /* nas id */ - nasid = optarg; + result.config.nas_id = optarg; break; case 'N': /* nas ip address */ - nasipaddress = optarg; + result.config.nas_ip_address = optarg; break; case 'F': /* configuration file */ - config_file = optarg; + result.config.config_file = optarg; break; case 'e': /* expect */ - expect = optarg; + result.config.expect = optarg; break; case 'r': /* retries */ if (is_intpos(optarg)) { - retries = atoi(optarg); + result.config.retries = atoi(optarg); } else { usage4(_("Number of retries must be a positive integer")); } @@ -320,20 +338,20 @@ int process_arguments(int argc, char **argv) { } } - if (server == NULL) { + if (result.config.server == NULL) { usage4(_("Hostname was not supplied")); } - if (username == NULL) { + if (result.config.username == NULL) { usage4(_("User not specified")); } - if (password == NULL) { + if (result.config.password == NULL) { usage4(_("Password not specified")); } - if (config_file == NULL) { + if (result.config.config_file == NULL) { usage4(_("Configuration file not specified")); } - return OK; + return result; } void print_help(void) { @@ -395,11 +413,11 @@ void print_usage(void) { progname); } -int my_rc_read_config(char *a) { +int my_rc_read_config(char *config_file_name, rc_handle **rch) { #if defined(HAVE_LIBFREERADIUS_CLIENT) || defined(HAVE_LIBRADIUSCLIENT_NG) || defined(HAVE_LIBRADCLI) - rch = rc_read_config(a); + *rch = rc_read_config(config_file_name); return (rch == NULL) ? 1 : 0; #else - return rc_read_config(a); + return rc_read_config(config_file_name); #endif } diff --git a/plugins/check_radius.d/config.h b/plugins/check_radius.d/config.h new file mode 100644 index 00000000..b27d31e7 --- /dev/null +++ b/plugins/check_radius.d/config.h @@ -0,0 +1,42 @@ +#pragma once + +#include "../../config.h" +#include +#if defined(HAVE_LIBRADCLI) +# include +#elif defined(HAVE_LIBFREERADIUS_CLIENT) +# include +#elif defined(HAVE_LIBRADIUSCLIENT_NG) +# include +#else +# include +#endif + +typedef struct { + char *server; + char *username; + char *password; + char *config_file; + char *nas_id; + char *nas_ip_address; + int retries; + unsigned short port; + + char *expect; +} check_radius_config; + +check_radius_config check_radius_config_init() { + check_radius_config tmp = { + .server = NULL, + .username = NULL, + .password = NULL, + .config_file = NULL, + .nas_id = NULL, + .nas_ip_address = NULL, + .retries = 1, + .port = PW_AUTH_UDP_PORT, + + .expect = NULL, + }; + return tmp; +} -- cgit v1.2.3-74-g34f1 From 97e65dddbda048f10b58ef8d190f2c0146a164a0 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 17:22:17 +0100 Subject: Refactor check_real --- plugins/Makefile.am | 1 + plugins/check_real.c | 167 +++++++++++++++++++++++------------------- plugins/check_real.d/config.h | 37 ++++++++++ 3 files changed, 130 insertions(+), 75 deletions(-) create mode 100644 plugins/check_real.d/config.h (limited to 'plugins/Makefile.am') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 6c582a15..3269b9fb 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -55,6 +55,7 @@ EXTRA_DIST = t \ check_game.d \ check_nagios.d \ check_dbi.d \ + check_real.d \ check_ssh.d \ check_nt.d \ check_dns.d \ diff --git a/plugins/check_real.c b/plugins/check_real.c index 3737f69d..ec0928ed 100644 --- a/plugins/check_real.c +++ b/plugins/check_real.c @@ -28,6 +28,8 @@ * *****************************************************************************/ +#include "states.h" +#include const char *progname = "check_real"; const char *copyright = "2000-2024"; const char *email = "devel@monitoring-plugins.org"; @@ -35,27 +37,20 @@ const char *email = "devel@monitoring-plugins.org"; #include "common.h" #include "netutils.h" #include "utils.h" - -enum { - PORT = 554 -}; +#include "check_real.d/config.h" #define EXPECT "RTSP/1." #define URL "" -static int process_arguments(int, char **); +typedef struct { + int errorcode; + check_real_config config; +} check_real_config_wrapper; +static check_real_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); + static void print_help(void); void print_usage(void); -static int server_port = PORT; -static char *server_address; -static char *host_name; -static char *server_url = NULL; -static char *server_expect; -static int warning_time = 0; -static bool check_warning_time = false; -static int critical_time = 0; -static bool check_critical_time = false; static bool verbose = false; int main(int argc, char **argv) { @@ -66,10 +61,13 @@ 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_real_config_wrapper tmp_config = process_arguments(argc, argv); + if (tmp_config.errorcode == ERROR) { usage4(_("Could not parse arguments")); } + const check_real_config config = tmp_config.config; + /* initialize alarm signal handling */ signal(SIGALRM, socket_timeout_alarm_handler); @@ -79,40 +77,50 @@ int main(int argc, char **argv) { /* try to connect to the host at the given port number */ int socket; - if (my_tcp_connect(server_address, server_port, &socket) != STATE_OK) { - die(STATE_CRITICAL, _("Unable to connect to %s on port %d\n"), server_address, server_port); + if (my_tcp_connect(config.server_address, config.server_port, &socket) != STATE_OK) { + die(STATE_CRITICAL, _("Unable to connect to %s on port %d\n"), config.server_address, config.server_port); } /* Part I - Server Check */ /* send the OPTIONS request */ char buffer[MAX_INPUT_BUFFER]; - sprintf(buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", host_name, server_port); - int result = send(socket, buffer, strlen(buffer), 0); + sprintf(buffer, "OPTIONS rtsp://%s:%d RTSP/1.0\r\n", config.host_name, config.server_port); + ssize_t sent_bytes = send(socket, buffer, strlen(buffer), 0); + if (sent_bytes == -1) { + die(STATE_CRITICAL, _("Sending options to %s failed\n"), config.host_name); + } /* send the header sync */ sprintf(buffer, "CSeq: 1\r\n"); - result = send(socket, buffer, strlen(buffer), 0); + sent_bytes = send(socket, buffer, strlen(buffer), 0); + if (sent_bytes == -1) { + die(STATE_CRITICAL, _("Sending header sync to %s failed\n"), config.host_name); + } /* send a newline so the server knows we're done with the request */ sprintf(buffer, "\r\n"); - result = send(socket, buffer, strlen(buffer), 0); + sent_bytes = send(socket, buffer, strlen(buffer), 0); + if (sent_bytes == -1) { + die(STATE_CRITICAL, _("Sending newline to %s failed\n"), config.host_name); + } /* watch for the REAL connection string */ - result = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0); + ssize_t received_bytes = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0); /* return a CRITICAL status if we couldn't read any data */ - if (result == -1) { - die(STATE_CRITICAL, _("No data received from %s\n"), host_name); + if (received_bytes == -1) { + die(STATE_CRITICAL, _("No data received from %s\n"), config.host_name); } + mp_state_enum result = STATE_OK; char *status_line = NULL; /* make sure we find the response we are looking for */ - if (!strstr(buffer, server_expect)) { - if (server_port == PORT) { + if (!strstr(buffer, config.server_expect)) { + if (config.server_port == PORT) { printf("%s\n", _("Invalid REAL response received from host")); } else { - printf(_("Invalid REAL response received from host on port %d\n"), server_port); + printf(_("Invalid REAL response received from host on port %d\n"), config.server_port); } } else { /* else we got the REAL string, so check the return code */ @@ -121,7 +129,7 @@ int main(int argc, char **argv) { result = STATE_OK; - status_line = (char *)strtok(buffer, "\n"); + status_line = strtok(buffer, "\n"); if (strstr(status_line, "200")) { result = STATE_OK; @@ -138,10 +146,8 @@ int main(int argc, char **argv) { result = STATE_WARNING; } else if (strstr(status_line, "404")) { result = STATE_WARNING; - } - - /* server errors result in a critical state */ - else if (strstr(status_line, "500")) { + } else if (strstr(status_line, "500")) { + /* server errors result in a critical state */ result = STATE_CRITICAL; } else if (strstr(status_line, "501")) { result = STATE_CRITICAL; @@ -149,45 +155,52 @@ int main(int argc, char **argv) { result = STATE_CRITICAL; } else if (strstr(status_line, "503")) { result = STATE_CRITICAL; - } - - else { + } else { result = STATE_UNKNOWN; } } /* Part II - Check stream exists and is ok */ - if ((result == STATE_OK) && (server_url != NULL)) { + if ((result == STATE_OK) && (config.server_url != NULL)) { /* Part I - Server Check */ /* send the DESCRIBE request */ - sprintf(buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\r\n", host_name, server_port, server_url); - result = send(socket, buffer, strlen(buffer), 0); + sprintf(buffer, "DESCRIBE rtsp://%s:%d%s RTSP/1.0\r\n", config.host_name, config.server_port, config.server_url); + + ssize_t sent_bytes = send(socket, buffer, strlen(buffer), 0); + if (sent_bytes == -1) { + die(STATE_CRITICAL, _("Sending DESCRIBE request to %s failed\n"), config.host_name); + } /* send the header sync */ sprintf(buffer, "CSeq: 2\r\n"); - result = send(socket, buffer, strlen(buffer), 0); + sent_bytes = send(socket, buffer, strlen(buffer), 0); + if (sent_bytes == -1) { + die(STATE_CRITICAL, _("Sending DESCRIBE request to %s failed\n"), config.host_name); + } /* send a newline so the server knows we're done with the request */ sprintf(buffer, "\r\n"); - result = send(socket, buffer, strlen(buffer), 0); + sent_bytes = send(socket, buffer, strlen(buffer), 0); + if (sent_bytes == -1) { + die(STATE_CRITICAL, _("Sending DESCRIBE request to %s failed\n"), config.host_name); + } /* watch for the REAL connection string */ - result = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0); - buffer[result] = '\0'; /* null terminate received buffer */ - - /* return a CRITICAL status if we couldn't read any data */ - if (result == -1) { + ssize_t recv_bytes = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0); + if (recv_bytes == -1) { + /* return a CRITICAL status if we couldn't read any data */ printf(_("No data received from host\n")); result = STATE_CRITICAL; } else { + buffer[result] = '\0'; /* null terminate received buffer */ /* make sure we find the response we are looking for */ - if (!strstr(buffer, server_expect)) { - if (server_port == PORT) { + if (!strstr(buffer, config.server_expect)) { + if (config.server_port == PORT) { printf("%s\n", _("Invalid REAL response received from host")); } else { - printf(_("Invalid REAL response received from host on port %d\n"), server_port); + printf(_("Invalid REAL response received from host on port %d\n"), config.server_port); } } else { @@ -197,7 +210,7 @@ int main(int argc, char **argv) { result = STATE_OK; - status_line = (char *)strtok(buffer, "\n"); + status_line = strtok(buffer, "\n"); if (strstr(status_line, "200")) { result = STATE_OK; @@ -236,10 +249,9 @@ int main(int argc, char **argv) { /* Return results */ if (result == STATE_OK) { - - if (check_critical_time && (end_time - start_time) > critical_time) { + if (config.check_critical_time && (end_time - start_time) > config.critical_time) { result = STATE_CRITICAL; - } else if (check_warning_time && (end_time - start_time) > warning_time) { + } else if (config.check_warning_time && (end_time - start_time) > config.warning_time) { result = STATE_WARNING; } @@ -255,11 +267,11 @@ int main(int argc, char **argv) { /* reset the alarm */ alarm(0); - return result; + exit(result); } /* process command-line arguments */ -int process_arguments(int argc, char **argv) { +check_real_config_wrapper process_arguments(int argc, char **argv) { static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"IPaddress", required_argument, 0, 'I'}, {"expect", required_argument, 0, 'e'}, {"url", required_argument, 0, 'u'}, {"port", required_argument, 0, 'p'}, {"critical", required_argument, 0, 'c'}, @@ -267,8 +279,14 @@ int process_arguments(int argc, char **argv) { {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; + check_real_config_wrapper result = { + .errorcode = OK, + .config = check_real_config_init(), + }; + if (argc < 2) { - return ERROR; + result.errorcode = ERROR; + return result; } for (int i = 1; i < argc; i++) { @@ -281,10 +299,9 @@ int process_arguments(int argc, char **argv) { } } - int option_char; while (true) { int option = 0; - option_char = getopt_long(argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts, &option); + int option_char = getopt_long(argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts, &option); if (option_char == -1 || option_char == EOF) { break; @@ -293,39 +310,39 @@ int process_arguments(int argc, char **argv) { switch (option_char) { case 'I': /* hostname */ case 'H': /* hostname */ - if (server_address) { + if (result.config.server_address) { break; } else if (is_host(optarg)) { - server_address = optarg; + result.config.server_address = optarg; } else { usage2(_("Invalid hostname/address"), optarg); } break; case 'e': /* string to expect in response header */ - server_expect = optarg; + result.config.server_expect = optarg; break; case 'u': /* server URL */ - server_url = optarg; + result.config.server_url = optarg; break; case 'p': /* port */ if (is_intpos(optarg)) { - server_port = atoi(optarg); + result.config.server_port = atoi(optarg); } else { usage4(_("Port must be a positive integer")); } break; case 'w': /* warning time threshold */ if (is_intnonneg(optarg)) { - warning_time = atoi(optarg); - check_warning_time = true; + result.config.warning_time = atoi(optarg); + result.config.check_warning_time = true; } else { usage4(_("Warning time must be a positive integer")); } break; case 'c': /* critical time threshold */ if (is_intnonneg(optarg)) { - critical_time = atoi(optarg); - check_critical_time = true; + result.config.critical_time = atoi(optarg); + result.config.check_critical_time = true; } else { usage4(_("Critical time must be a positive integer")); } @@ -351,28 +368,28 @@ int process_arguments(int argc, char **argv) { } } - option_char = optind; - if (server_address == NULL && argc > option_char) { + int option_char = optind; + if (result.config.server_address == NULL && argc > option_char) { if (is_host(argv[option_char])) { - server_address = argv[option_char++]; + result.config.server_address = argv[option_char++]; } else { usage2(_("Invalid hostname/address"), argv[option_char]); } } - if (server_address == NULL) { + if (result.config.server_address == NULL) { usage4(_("You must provide a server to check")); } - if (host_name == NULL) { - host_name = strdup(server_address); + if (result.config.host_name == NULL) { + result.config.host_name = strdup(result.config.server_address); } - if (server_expect == NULL) { - server_expect = strdup(EXPECT); + if (result.config.server_expect == NULL) { + result.config.server_expect = strdup(EXPECT); } - return OK; + return result; } void print_help(void) { diff --git a/plugins/check_real.d/config.h b/plugins/check_real.d/config.h new file mode 100644 index 00000000..c4663cf9 --- /dev/null +++ b/plugins/check_real.d/config.h @@ -0,0 +1,37 @@ +#pragma once + +#include "../../config.h" +#include + +enum { + PORT = 554 +}; + +typedef struct { + char *server_address; + char *host_name; + int server_port; + char *server_url; + + char *server_expect; + int warning_time; + bool check_warning_time; + int critical_time; + bool check_critical_time; +} check_real_config; + +check_real_config check_real_config_init() { + check_real_config tmp = { + .server_address = NULL, + .host_name = NULL, + .server_port = PORT, + .server_url = NULL, + + .server_expect = NULL, + .warning_time = 0, + .check_warning_time = false, + .critical_time = 0, + .check_critical_time = false, + }; + return tmp; +} -- cgit v1.2.3-74-g34f1 From 2d70bd3bc09ff95cd8525449925938ebf56cbfbb Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 18:14:54 +0100 Subject: Refactor check_time --- plugins/Makefile.am | 1 + plugins/check_time.c | 128 +++++++++++++++++++++--------------------- plugins/check_time.d/config.h | 42 ++++++++++++++ 3 files changed, 107 insertions(+), 64 deletions(-) create mode 100644 plugins/check_time.d/config.h (limited to 'plugins/Makefile.am') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index bb3f029e..5d03f160 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -54,6 +54,7 @@ EXTRA_DIST = t \ check_hpjd.d \ check_game.d \ check_radius.d \ + check_time.d \ check_nagios.d \ check_dbi.d \ check_real.d \ diff --git a/plugins/check_time.c b/plugins/check_time.c index 02b152c0..debf59f3 100644 --- a/plugins/check_time.c +++ b/plugins/check_time.c @@ -28,6 +28,7 @@ * *****************************************************************************/ +#include "states.h" const char *progname = "check_time"; const char *copyright = "1999-2024"; const char *email = "devel@monitoring-plugins.org"; @@ -35,28 +36,15 @@ const char *email = "devel@monitoring-plugins.org"; #include "common.h" #include "netutils.h" #include "utils.h" - -enum { - TIME_PORT = 37 -}; +#include "check_time.d/config.h" #define UNIX_EPOCH 2208988800UL -static uint32_t raw_server_time; -static unsigned long server_time, diff_time; -static int warning_time = 0; -static bool check_warning_time = false; -static int critical_time = 0; -static bool check_critical_time = false; -static unsigned long warning_diff = 0; -static bool check_warning_diff = false; -static unsigned long critical_diff = 0; -static bool check_critical_diff = false; -static int server_port = TIME_PORT; -static char *server_address = NULL; -static bool use_udp = false; - -static int process_arguments(int, char **); +typedef struct { + int errorcode; + check_time_config config; +} check_time_config_wrapper; +static check_time_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); static void print_help(void); void print_usage(void); @@ -68,10 +56,13 @@ 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_time_config_wrapper tmp_config = process_arguments(argc, argv); + if (tmp_config.errorcode == ERROR) { usage4(_("Could not parse arguments")); } + const check_time_config config = tmp_config.config; + /* initialize alarm signal handling */ signal(SIGALRM, socket_timeout_alarm_handler); @@ -80,39 +71,40 @@ int main(int argc, char **argv) { time(&start_time); int socket; - int result = STATE_UNKNOWN; + mp_state_enum result = STATE_UNKNOWN; /* try to connect to the host at the given port number */ - if (use_udp) { - result = my_udp_connect(server_address, server_port, &socket); + if (config.use_udp) { + result = my_udp_connect(config.server_address, config.server_port, &socket); } else { - result = my_tcp_connect(server_address, server_port, &socket); + result = my_tcp_connect(config.server_address, config.server_port, &socket); } if (result != STATE_OK) { - if (check_critical_time) { + if (config.check_critical_time) { result = STATE_CRITICAL; - } else if (check_warning_time) { + } else if (config.check_warning_time) { result = STATE_WARNING; } else { result = STATE_UNKNOWN; } - die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), server_address, server_port); + die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), config.server_address, config.server_port); } - if (use_udp) { + if (config.use_udp) { if (send(socket, "", 0, 0) < 0) { - if (check_critical_time) { + if (config.check_critical_time) { result = STATE_CRITICAL; - } else if (check_warning_time) { + } else if (config.check_warning_time) { result = STATE_WARNING; } else { result = STATE_UNKNOWN; } - die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), server_address, server_port); + die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), config.server_address, config.server_port); } } /* watch for the connection string */ + uint32_t raw_server_time; result = recv(socket, (void *)&raw_server_time, sizeof(raw_server_time), 0); /* close the connection */ @@ -124,31 +116,33 @@ int main(int argc, char **argv) { /* return a WARNING status if we couldn't read any data */ if (result <= 0) { - if (check_critical_time) { + if (config.check_critical_time) { result = STATE_CRITICAL; - } else if (check_warning_time) { + } else if (config.check_warning_time) { result = STATE_WARNING; } else { result = STATE_UNKNOWN; } - die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), server_address, server_port); + die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), config.server_address, config.server_port); } result = STATE_OK; time_t conntime = (end_time - start_time); - if (check_critical_time && conntime > critical_time) { + if (config.check_critical_time && conntime > config.critical_time) { result = STATE_CRITICAL; - } else if (check_warning_time && conntime > warning_time) { + } else if (config.check_warning_time && conntime > config.warning_time) { result = STATE_WARNING; } if (result != STATE_OK) { die(result, _("TIME %s - %d second response time|%s\n"), state_text(result), (int)conntime, - perfdata("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, true, 0, - false, 0)); + perfdata("time", (long)conntime, "s", config.check_warning_time, (long)config.warning_time, config.check_critical_time, + (long)config.critical_time, true, 0, false, 0)); } + unsigned long server_time; + unsigned long diff_time; server_time = ntohl(raw_server_time) - UNIX_EPOCH; if (server_time > (unsigned long)end_time) { diff_time = server_time - (unsigned long)end_time; @@ -156,21 +150,22 @@ int main(int argc, char **argv) { diff_time = (unsigned long)end_time - server_time; } - if (check_critical_diff && diff_time > critical_diff) { + if (config.check_critical_diff && diff_time > config.critical_diff) { result = STATE_CRITICAL; - } else if (check_warning_diff && diff_time > warning_diff) { + } else if (config.check_warning_diff && diff_time > config.warning_diff) { result = STATE_WARNING; } printf(_("TIME %s - %lu second time difference|%s %s\n"), state_text(result), diff_time, - perfdata("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, true, 0, - false, 0), - perfdata("offset", diff_time, "s", check_warning_diff, warning_diff, check_critical_diff, critical_diff, true, 0, false, 0)); + perfdata("time", (long)conntime, "s", config.check_warning_time, (long)config.warning_time, config.check_critical_time, + (long)config.critical_time, true, 0, false, 0), + perfdata("offset", diff_time, "s", config.check_warning_diff, config.warning_diff, config.check_critical_diff, + config.critical_diff, true, 0, false, 0)); return result; } /* process command-line arguments */ -int process_arguments(int argc, char **argv) { +check_time_config_wrapper process_arguments(int argc, char **argv) { static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"warning-variance", required_argument, 0, 'w'}, {"critical-variance", required_argument, 0, 'c'}, @@ -201,6 +196,11 @@ int process_arguments(int argc, char **argv) { } } + check_time_config_wrapper result = { + .errorcode = OK, + .config = check_time_config_init(), + }; + int option_char; while (true) { int option = 0; @@ -223,16 +223,16 @@ int process_arguments(int argc, char **argv) { if (!is_host(optarg)) { usage2(_("Invalid hostname/address"), optarg); } - server_address = optarg; + result.config.server_address = optarg; break; case 'w': /* warning-variance */ if (is_intnonneg(optarg)) { - warning_diff = strtoul(optarg, NULL, 10); - check_warning_diff = true; + result.config.warning_diff = strtoul(optarg, NULL, 10); + result.config.check_warning_diff = true; } else if (strspn(optarg, "0123456789:,") > 0) { - if (sscanf(optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) { - check_warning_diff = true; - check_warning_time = true; + if (sscanf(optarg, "%lu%*[:,]%d", &result.config.warning_diff, &result.config.warning_time) == 2) { + result.config.check_warning_diff = true; + result.config.check_warning_time = true; } else { usage4(_("Warning thresholds must be a positive integer")); } @@ -242,12 +242,12 @@ int process_arguments(int argc, char **argv) { break; case 'c': /* critical-variance */ if (is_intnonneg(optarg)) { - critical_diff = strtoul(optarg, NULL, 10); - check_critical_diff = true; + result.config.critical_diff = strtoul(optarg, NULL, 10); + result.config.check_critical_diff = true; } else if (strspn(optarg, "0123456789:,") > 0) { - if (sscanf(optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) == 2) { - check_critical_diff = true; - check_critical_time = true; + if (sscanf(optarg, "%lu%*[:,]%d", &result.config.critical_diff, &result.config.critical_time) == 2) { + result.config.check_critical_diff = true; + result.config.check_critical_time = true; } else { usage4(_("Critical thresholds must be a positive integer")); } @@ -259,23 +259,23 @@ int process_arguments(int argc, char **argv) { if (!is_intnonneg(optarg)) { usage4(_("Warning threshold must be a positive integer")); } else { - warning_time = atoi(optarg); + result.config.warning_time = atoi(optarg); } - check_warning_time = true; + result.config.check_warning_time = true; break; case 'C': /* critical-connect */ if (!is_intnonneg(optarg)) { usage4(_("Critical threshold must be a positive integer")); } else { - critical_time = atoi(optarg); + result.config.critical_time = atoi(optarg); } - check_critical_time = true; + result.config.check_critical_time = true; break; case 'p': /* port */ if (!is_intnonneg(optarg)) { usage4(_("Port must be a positive integer")); } else { - server_port = atoi(optarg); + result.config.server_port = atoi(optarg); } break; case 't': /* timeout */ @@ -286,23 +286,23 @@ int process_arguments(int argc, char **argv) { } break; case 'u': /* udp */ - use_udp = true; + result.config.use_udp = true; } } option_char = optind; - if (server_address == NULL) { + if (result.config.server_address == NULL) { if (argc > option_char) { if (!is_host(argv[option_char])) { usage2(_("Invalid hostname/address"), optarg); } - server_address = argv[option_char]; + result.config.server_address = argv[option_char]; } else { usage4(_("Hostname was not supplied")); } } - return OK; + return result; } void print_help(void) { diff --git a/plugins/check_time.d/config.h b/plugins/check_time.d/config.h new file mode 100644 index 00000000..09bd7c45 --- /dev/null +++ b/plugins/check_time.d/config.h @@ -0,0 +1,42 @@ +#pragma once + +#include "../../config.h" +#include + +enum { + TIME_PORT = 37 +}; + +typedef struct { + char *server_address; + int server_port; + bool use_udp; + + int warning_time; + bool check_warning_time; + int critical_time; + bool check_critical_time; + unsigned long warning_diff; + bool check_warning_diff; + unsigned long critical_diff; + bool check_critical_diff; +} check_time_config; + +check_time_config check_time_config_init() { + check_time_config tmp = { + .server_address = NULL, + .server_port = TIME_PORT, + .use_udp = false, + + .warning_time = 0, + .check_warning_time = false, + .critical_time = 0, + .check_critical_time = false, + + .warning_diff = 0, + .check_warning_diff = false, + .critical_diff = 0, + .check_critical_diff = false, + }; + return tmp; +} -- cgit v1.2.3-74-g34f1 From 29b1be07c81099013317b10f25c5f48f6d40312b Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 18:40:14 +0100 Subject: Refactor check_ups --- plugins/Makefile.am | 1 + plugins/check_ups.c | 274 +++++++++++++++++++------------------------ plugins/check_ups.d/config.h | 55 +++++++++ 3 files changed, 178 insertions(+), 152 deletions(-) create mode 100644 plugins/check_ups.d/config.h (limited to 'plugins/Makefile.am') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index bb3f029e..f1b9acf6 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -73,6 +73,7 @@ EXTRA_DIST = t \ check_ntp_time.d \ check_dig.d \ check_cluster.d \ + check_ups.d \ check_fping.d PLUGINHDRS = common.h diff --git a/plugins/check_ups.c b/plugins/check_ups.c index 526a29df..ecc0760f 100644 --- a/plugins/check_ups.c +++ b/plugins/check_ups.c @@ -39,69 +39,29 @@ const char *email = "devel@monitoring-plugins.org"; #include "common.h" #include "netutils.h" #include "utils.h" - -enum { - PORT = 3493 -}; - -#define UPS_NONE 0 /* no supported options */ -#define UPS_UTILITY 1 /* supports utility line */ -#define UPS_BATTPCT 2 /* supports percent battery remaining */ -#define UPS_STATUS 4 /* supports UPS status */ -#define UPS_TEMP 8 /* supports UPS temperature */ -#define UPS_LOADPCT 16 /* supports load percent */ -#define UPS_REALPOWER 32 /* supports real power */ - -#define UPSSTATUS_NONE 0 -#define UPSSTATUS_OFF 1 -#define UPSSTATUS_OL 2 -#define UPSSTATUS_OB 4 -#define UPSSTATUS_LB 8 -#define UPSSTATUS_CAL 16 -#define UPSSTATUS_RB 32 /*Replace Battery */ -#define UPSSTATUS_BYPASS 64 -#define UPSSTATUS_OVER 128 -#define UPSSTATUS_TRIM 256 -#define UPSSTATUS_BOOST 512 -#define UPSSTATUS_CHRG 1024 -#define UPSSTATUS_DISCHRG 2048 -#define UPSSTATUS_UNKNOWN 4096 -#define UPSSTATUS_ALARM 8192 +#include "check_ups.d/config.h" +#include "states.h" enum { NOSUCHVAR = ERROR - 1 }; -typedef struct ups_config { - unsigned int server_port; - char *server_address; - char *ups_name; - double warning_value; - double critical_value; - bool check_warn; - bool check_crit; - int check_variable; - int status; - bool temp_output_c; -} ups_config; - -ups_config ups_config_init(void) { - ups_config tmp = {0}; - tmp.server_port = PORT; - tmp.server_address = NULL; - tmp.ups_name = NULL; - tmp.check_variable = UPS_NONE; - tmp.status = UPSSTATUS_NONE; - - return tmp; -} - // Forward declarations -static int determine_status(ups_config * /*config*/, int *supported_options); -static int get_ups_variable(const char * /*varname*/, char * /*buf*/, ups_config config); +typedef struct { + int errorcode; + int ups_status; + int supported_options; +} determine_status_result; +static determine_status_result determine_status(check_ups_config /*config*/); +static int get_ups_variable(const char * /*varname*/, char * /*buf*/, check_ups_config config); + +typedef struct { + int errorcode; + check_ups_config config; +} check_ups_config_wrapper; +static check_ups_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); +static check_ups_config_wrapper validate_arguments(check_ups_config_wrapper /*config_wrapper*/); -static int process_arguments(int /*argc*/, char ** /*argv*/, ups_config * /*config*/); -static int validate_arguments(ups_config /*config*/); static void print_help(void); void print_usage(void); @@ -109,28 +69,16 @@ int main(int argc, char **argv) { setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - - char *ups_status; - ups_status = strdup("N/A"); - - char *data; - data = strdup(""); - - char *message; - message = strdup(""); - - // Exit result - int result = STATE_UNKNOWN; - /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - // Config from commandline - ups_config config = ups_config_init(); + check_ups_config_wrapper tmp_config = process_arguments(argc, argv); - if (process_arguments(argc, argv, &config) == ERROR) { + if (tmp_config.errorcode == ERROR) { usage4(_("Could not parse arguments")); } + // Config from commandline + check_ups_config config = tmp_config.config; /* initialize alarm signal handling */ signal(SIGALRM, socket_timeout_alarm_handler); @@ -138,71 +86,75 @@ int main(int argc, char **argv) { /* set socket timeout */ alarm(socket_timeout); - int supported_options = UPS_NONE; - /* get the ups status if possible */ - if (determine_status(&config, &supported_options) != OK) { + determine_status_result query_result = determine_status(config); + if (query_result.errorcode != OK) { return STATE_CRITICAL; } - if (supported_options & UPS_STATUS) { + int ups_status_flags = query_result.ups_status; + int supported_options = query_result.supported_options; - ups_status = strdup(""); + // Exit result + mp_state_enum result = STATE_UNKNOWN; + char *message = NULL; + if (supported_options & UPS_STATUS) { + char *ups_status = strdup(""); result = STATE_OK; - if (config.status & UPSSTATUS_OFF) { + if (ups_status_flags & UPSSTATUS_OFF) { xasprintf(&ups_status, "Off"); result = STATE_CRITICAL; - } else if ((config.status & (UPSSTATUS_OB | UPSSTATUS_LB)) == (UPSSTATUS_OB | UPSSTATUS_LB)) { + } else if ((ups_status_flags & (UPSSTATUS_OB | UPSSTATUS_LB)) == (UPSSTATUS_OB | UPSSTATUS_LB)) { xasprintf(&ups_status, _("On Battery, Low Battery")); result = STATE_CRITICAL; } else { - if (config.status & UPSSTATUS_OL) { + if (ups_status_flags & UPSSTATUS_OL) { xasprintf(&ups_status, "%s%s", ups_status, _("Online")); } - if (config.status & UPSSTATUS_OB) { + if (ups_status_flags & UPSSTATUS_OB) { xasprintf(&ups_status, "%s%s", ups_status, _("On Battery")); result = max_state(result, STATE_WARNING); } - if (config.status & UPSSTATUS_LB) { + if (ups_status_flags & UPSSTATUS_LB) { xasprintf(&ups_status, "%s%s", ups_status, _(", Low Battery")); result = max_state(result, STATE_WARNING); } - if (config.status & UPSSTATUS_CAL) { + if (ups_status_flags & UPSSTATUS_CAL) { xasprintf(&ups_status, "%s%s", ups_status, _(", Calibrating")); } - if (config.status & UPSSTATUS_RB) { + if (ups_status_flags & UPSSTATUS_RB) { xasprintf(&ups_status, "%s%s", ups_status, _(", Replace Battery")); result = max_state(result, STATE_WARNING); } - if (config.status & UPSSTATUS_BYPASS) { + if (ups_status_flags & UPSSTATUS_BYPASS) { xasprintf(&ups_status, "%s%s", ups_status, _(", On Bypass")); // Bypassing the battery is likely a bad thing result = STATE_CRITICAL; } - if (config.status & UPSSTATUS_OVER) { + if (ups_status_flags & UPSSTATUS_OVER) { xasprintf(&ups_status, "%s%s", ups_status, _(", Overload")); result = max_state(result, STATE_WARNING); } - if (config.status & UPSSTATUS_TRIM) { + if (ups_status_flags & UPSSTATUS_TRIM) { xasprintf(&ups_status, "%s%s", ups_status, _(", Trimming")); } - if (config.status & UPSSTATUS_BOOST) { + if (ups_status_flags & UPSSTATUS_BOOST) { xasprintf(&ups_status, "%s%s", ups_status, _(", Boosting")); } - if (config.status & UPSSTATUS_CHRG) { + if (ups_status_flags & UPSSTATUS_CHRG) { xasprintf(&ups_status, "%s%s", ups_status, _(", Charging")); } - if (config.status & UPSSTATUS_DISCHRG) { + if (ups_status_flags & UPSSTATUS_DISCHRG) { xasprintf(&ups_status, "%s%s", ups_status, _(", Discharging")); result = max_state(result, STATE_WARNING); } - if (config.status & UPSSTATUS_ALARM) { + if (ups_status_flags & UPSSTATUS_ALARM) { xasprintf(&ups_status, "%s%s", ups_status, _(", ALARM")); result = STATE_CRITICAL; } - if (config.status & UPSSTATUS_UNKNOWN) { + if (ups_status_flags & UPSSTATUS_UNKNOWN) { xasprintf(&ups_status, "%s%s", ups_status, _(", Unknown")); } } @@ -211,7 +163,7 @@ int main(int argc, char **argv) { int res; char temp_buffer[MAX_INPUT_BUFFER]; - + char *performance_data = strdup(""); /* get the ups utility voltage if possible */ res = get_ups_variable("input.voltage", temp_buffer, config); if (res == NOSUCHVAR) { @@ -239,11 +191,12 @@ int main(int argc, char **argv) { } else if (config.check_warn && ups_utility_deviation >= config.warning_value) { result = max_state(result, STATE_WARNING); } - xasprintf(&data, "%s", + xasprintf(&performance_data, "%s", perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", config.check_warn, (long)(1000 * config.warning_value), config.check_crit, (long)(1000 * config.critical_value), true, 0, false, 0)); } else { - xasprintf(&data, "%s", perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", false, 0, false, 0, true, 0, false, 0)); + xasprintf(&performance_data, "%s", + perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", false, 0, false, 0, true, 0, false, 0)); } } @@ -266,11 +219,12 @@ int main(int argc, char **argv) { } else if (config.check_warn && ups_battery_percent <= config.warning_value) { result = max_state(result, STATE_WARNING); } - xasprintf(&data, "%s %s", data, + xasprintf(&performance_data, "%s %s", performance_data, perfdata("battery", (long)ups_battery_percent, "%", config.check_warn, (long)(config.warning_value), config.check_crit, (long)(config.critical_value), true, 0, true, 100)); } else { - xasprintf(&data, "%s %s", data, perfdata("battery", (long)ups_battery_percent, "%", false, 0, false, 0, true, 0, true, 100)); + xasprintf(&performance_data, "%s %s", performance_data, + perfdata("battery", (long)ups_battery_percent, "%", false, 0, false, 0, true, 0, true, 100)); } } @@ -293,11 +247,12 @@ int main(int argc, char **argv) { } else if (config.check_warn && ups_load_percent >= config.warning_value) { result = max_state(result, STATE_WARNING); } - xasprintf(&data, "%s %s", data, + xasprintf(&performance_data, "%s %s", performance_data, perfdata("load", (long)ups_load_percent, "%", config.check_warn, (long)(config.warning_value), config.check_crit, (long)(config.critical_value), true, 0, true, 100)); } else { - xasprintf(&data, "%s %s", data, perfdata("load", (long)ups_load_percent, "%", false, 0, false, 0, true, 0, true, 100)); + xasprintf(&performance_data, "%s %s", performance_data, + perfdata("load", (long)ups_load_percent, "%", false, 0, false, 0, true, 0, true, 100)); } } @@ -329,11 +284,12 @@ int main(int argc, char **argv) { } else if (config.check_warn && ups_temperature >= config.warning_value) { result = max_state(result, STATE_WARNING); } - xasprintf(&data, "%s %s", data, + xasprintf(&performance_data, "%s %s", performance_data, perfdata("temp", (long)ups_temperature, tunits, config.check_warn, (long)(config.warning_value), config.check_crit, (long)(config.critical_value), true, 0, false, 0)); } else { - xasprintf(&data, "%s %s", data, perfdata("temp", (long)ups_temperature, tunits, false, 0, false, 0, true, 0, false, 0)); + xasprintf(&performance_data, "%s %s", performance_data, + perfdata("temp", (long)ups_temperature, tunits, false, 0, false, 0, true, 0, false, 0)); } } @@ -355,11 +311,12 @@ int main(int argc, char **argv) { } else if (config.check_warn && ups_realpower >= config.warning_value) { result = max_state(result, STATE_WARNING); } - xasprintf(&data, "%s %s", data, + xasprintf(&performance_data, "%s %s", performance_data, perfdata("realpower", (long)ups_realpower, "W", config.check_warn, (long)(config.warning_value), config.check_crit, (long)(config.critical_value), true, 0, false, 0)); } else { - xasprintf(&data, "%s %s", data, perfdata("realpower", (long)ups_realpower, "W", false, 0, false, 0, true, 0, false, 0)); + xasprintf(&performance_data, "%s %s", performance_data, + perfdata("realpower", (long)ups_realpower, "W", false, 0, false, 0, true, 0, false, 0)); } } @@ -373,66 +330,73 @@ int main(int argc, char **argv) { /* reset timeout */ alarm(0); - printf("UPS %s - %s|%s\n", state_text(result), message, data); - return result; + printf("UPS %s - %s|%s\n", state_text(result), message, performance_data); + exit(result); } /* determines what options are supported by the UPS */ -int determine_status(ups_config *config, int *supported_options) { - char recv_buffer[MAX_INPUT_BUFFER]; +determine_status_result determine_status(const check_ups_config config) { - int res = get_ups_variable("ups.status", recv_buffer, *config); + determine_status_result result = { + .errorcode = OK, + .ups_status = UPSSTATUS_NONE, + .supported_options = 0, + }; + + char recv_buffer[MAX_INPUT_BUFFER]; + int res = get_ups_variable("ups.status", recv_buffer, config); if (res == NOSUCHVAR) { - return OK; + return result; } if (res != STATE_OK) { printf("%s\n", _("Invalid response received from host")); - return ERROR; + result.errorcode = ERROR; + return result; } - *supported_options |= UPS_STATUS; + result.supported_options |= UPS_STATUS; char temp_buffer[MAX_INPUT_BUFFER]; strcpy(temp_buffer, recv_buffer); - for (char *ptr = (char *)strtok(temp_buffer, " "); ptr != NULL; ptr = (char *)strtok(NULL, " ")) { + for (char *ptr = strtok(temp_buffer, " "); ptr != NULL; ptr = strtok(NULL, " ")) { if (!strcmp(ptr, "OFF")) { - config->status |= UPSSTATUS_OFF; + result.ups_status |= UPSSTATUS_OFF; } else if (!strcmp(ptr, "OL")) { - config->status |= UPSSTATUS_OL; + result.ups_status |= UPSSTATUS_OL; } else if (!strcmp(ptr, "OB")) { - config->status |= UPSSTATUS_OB; + result.ups_status |= UPSSTATUS_OB; } else if (!strcmp(ptr, "LB")) { - config->status |= UPSSTATUS_LB; + result.ups_status |= UPSSTATUS_LB; } else if (!strcmp(ptr, "CAL")) { - config->status |= UPSSTATUS_CAL; + result.ups_status |= UPSSTATUS_CAL; } else if (!strcmp(ptr, "RB")) { - config->status |= UPSSTATUS_RB; + result.ups_status |= UPSSTATUS_RB; } else if (!strcmp(ptr, "BYPASS")) { - config->status |= UPSSTATUS_BYPASS; + result.ups_status |= UPSSTATUS_BYPASS; } else if (!strcmp(ptr, "OVER")) { - config->status |= UPSSTATUS_OVER; + result.ups_status |= UPSSTATUS_OVER; } else if (!strcmp(ptr, "TRIM")) { - config->status |= UPSSTATUS_TRIM; + result.ups_status |= UPSSTATUS_TRIM; } else if (!strcmp(ptr, "BOOST")) { - config->status |= UPSSTATUS_BOOST; + result.ups_status |= UPSSTATUS_BOOST; } else if (!strcmp(ptr, "CHRG")) { - config->status |= UPSSTATUS_CHRG; + result.ups_status |= UPSSTATUS_CHRG; } else if (!strcmp(ptr, "DISCHRG")) { - config->status |= UPSSTATUS_DISCHRG; + result.ups_status |= UPSSTATUS_DISCHRG; } else if (!strcmp(ptr, "ALARM")) { - config->status |= UPSSTATUS_ALARM; + result.ups_status |= UPSSTATUS_ALARM; } else { - config->status |= UPSSTATUS_UNKNOWN; + result.ups_status |= UPSSTATUS_UNKNOWN; } } - return OK; + return result; } /* gets a variable value for a specific UPS */ -int get_ups_variable(const char *varname, char *buf, const ups_config config) { +int get_ups_variable(const char *varname, char *buf, const check_ups_config config) { char send_buffer[MAX_INPUT_BUFFER]; /* create the command string to send to the UPS daemon */ @@ -500,7 +464,7 @@ int get_ups_variable(const char *varname, char *buf, const ups_config config) { [-wv warn_value] [-cv crit_value] [-to to_sec] */ /* process command-line arguments */ -int process_arguments(int argc, char **argv, ups_config *config) { +check_ups_config_wrapper process_arguments(int argc, char **argv) { static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"ups", required_argument, 0, 'u'}, @@ -514,8 +478,14 @@ int process_arguments(int argc, char **argv, ups_config *config) { {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; + check_ups_config_wrapper result = { + .errorcode = OK, + .config = check_ups_config_init(), + }; + if (argc < 2) { - return ERROR; + result.errorcode = ERROR; + return result; } int c; @@ -542,52 +512,52 @@ int process_arguments(int argc, char **argv, ups_config *config) { usage5(); case 'H': /* hostname */ if (is_host(optarg)) { - config->server_address = optarg; + result.config.server_address = optarg; } else { usage2(_("Invalid hostname/address"), optarg); } break; case 'T': /* FIXME: to be improved (ie "-T C" for Celsius or "-T F" for Fahrenheit) */ - config->temp_output_c = true; + result.config.temp_output_c = true; break; case 'u': /* ups name */ - config->ups_name = optarg; + result.config.ups_name = optarg; break; case 'p': /* port */ if (is_intpos(optarg)) { - config->server_port = atoi(optarg); + result.config.server_port = atoi(optarg); } else { usage2(_("Port must be a positive integer"), optarg); } break; case 'c': /* critical time threshold */ if (is_intnonneg(optarg)) { - config->critical_value = atoi(optarg); - config->check_crit = true; + result.config.critical_value = atoi(optarg); + result.config.check_crit = true; } else { usage2(_("Critical time must be a positive integer"), optarg); } break; case 'w': /* warning time threshold */ if (is_intnonneg(optarg)) { - config->warning_value = atoi(optarg); - config->check_warn = true; + result.config.warning_value = atoi(optarg); + result.config.check_warn = true; } else { usage2(_("Warning time must be a positive integer"), optarg); } break; case 'v': /* variable */ if (!strcmp(optarg, "LINE")) { - config->check_variable = UPS_UTILITY; + result.config.check_variable = UPS_UTILITY; } else if (!strcmp(optarg, "TEMP")) { - config->check_variable = UPS_TEMP; + result.config.check_variable = UPS_TEMP; } else if (!strcmp(optarg, "BATTPCT")) { - config->check_variable = UPS_BATTPCT; + result.config.check_variable = UPS_BATTPCT; } else if (!strcmp(optarg, "LOADPCT")) { - config->check_variable = UPS_LOADPCT; + result.config.check_variable = UPS_LOADPCT; } else if (!strcmp(optarg, "REALPOWER")) { - config->check_variable = UPS_REALPOWER; + result.config.check_variable = UPS_REALPOWER; } else { usage2(_("Unrecognized UPS variable"), optarg); } @@ -608,27 +578,27 @@ int process_arguments(int argc, char **argv, ups_config *config) { } } - if (config->server_address == NULL && argc > optind) { + if (result.config.server_address == NULL && argc > optind) { if (is_host(argv[optind])) { - config->server_address = argv[optind++]; + result.config.server_address = argv[optind++]; } else { usage2(_("Invalid hostname/address"), optarg); } } - if (config->server_address == NULL) { - config->server_address = strdup("127.0.0.1"); + if (result.config.server_address == NULL) { + result.config.server_address = strdup("127.0.0.1"); } - return validate_arguments(*config); + return validate_arguments(result); } -int validate_arguments(ups_config config) { - if (!config.ups_name) { +check_ups_config_wrapper validate_arguments(check_ups_config_wrapper config_wrapper) { + if (config_wrapper.config.ups_name) { printf("%s\n", _("Error : no UPS indicated")); - return ERROR; + config_wrapper.errorcode = ERROR; } - return OK; + return config_wrapper; } void print_help(void) { diff --git a/plugins/check_ups.d/config.h b/plugins/check_ups.d/config.h new file mode 100644 index 00000000..353104f2 --- /dev/null +++ b/plugins/check_ups.d/config.h @@ -0,0 +1,55 @@ +#pragma once + +#include "../../config.h" +#include + +#define UPS_NONE 0 /* no supported options */ +#define UPS_UTILITY 1 /* supports utility line */ +#define UPS_BATTPCT 2 /* supports percent battery remaining */ +#define UPS_STATUS 4 /* supports UPS status */ +#define UPS_TEMP 8 /* supports UPS temperature */ +#define UPS_LOADPCT 16 /* supports load percent */ +#define UPS_REALPOWER 32 /* supports real power */ + +#define UPSSTATUS_NONE 0 +#define UPSSTATUS_OFF 1 +#define UPSSTATUS_OL 2 +#define UPSSTATUS_OB 4 +#define UPSSTATUS_LB 8 +#define UPSSTATUS_CAL 16 +#define UPSSTATUS_RB 32 /*Replace Battery */ +#define UPSSTATUS_BYPASS 64 +#define UPSSTATUS_OVER 128 +#define UPSSTATUS_TRIM 256 +#define UPSSTATUS_BOOST 512 +#define UPSSTATUS_CHRG 1024 +#define UPSSTATUS_DISCHRG 2048 +#define UPSSTATUS_UNKNOWN 4096 +#define UPSSTATUS_ALARM 8192 + +enum { + PORT = 3493 +}; + +typedef struct ups_config { + unsigned int server_port; + char *server_address; + char *ups_name; + double warning_value; + double critical_value; + bool check_warn; + bool check_crit; + int check_variable; + bool temp_output_c; +} check_ups_config; + +check_ups_config check_ups_config_init(void) { + check_ups_config tmp = {0}; + tmp.server_port = PORT; + tmp.server_address = NULL; + tmp.ups_name = NULL; + tmp.check_variable = UPS_NONE; + + return tmp; +} + -- cgit v1.2.3-74-g34f1 From 5ae0a8d49559d66695f6f2591f4bbe0bb185ef73 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 21:22:50 +0100 Subject: Refactor negate --- plugins/Makefile.am | 1 + plugins/negate.c | 73 +++++++++++++++++++++++++++-------------------- plugins/negate.d/config.h | 24 ++++++++++++++++ 3 files changed, 67 insertions(+), 31 deletions(-) create mode 100644 plugins/negate.d/config.h (limited to 'plugins/Makefile.am') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index bb3f029e..a420d23d 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -49,6 +49,7 @@ np_test_scripts = tests/test_check_swap.t EXTRA_DIST = t \ tests \ $(np_test_scripts) \ + negate.d \ check_swap.d \ check_ldap.d \ check_hpjd.d \ diff --git a/plugins/negate.c b/plugins/negate.c index 6c53a1b9..08fa1470 100644 --- a/plugins/negate.c +++ b/plugins/negate.c @@ -29,6 +29,7 @@ * *****************************************************************************/ +#include "states.h" const char *progname = "negate"; const char *copyright = "2002-2024"; const char *email = "devel@monitoring-plugins.org"; @@ -38,21 +39,17 @@ const char *email = "devel@monitoring-plugins.org"; #include "common.h" #include "utils.h" #include "utils_cmd.h" +#include "negate.d/config.h" -#include +typedef struct { + int errorcode; + negate_config config; +} negate_config_wrapper; +static negate_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); +static negate_config_wrapper validate_arguments(negate_config_wrapper /*config_wrapper*/); -static const char **process_arguments(int /*argc*/, char ** /*argv*/); -static void validate_arguments(char ** /*command_line*/); static void print_help(void); void print_usage(void); -static bool subst_text = false; - -static int state[4] = { - STATE_OK, - STATE_WARNING, - STATE_CRITICAL, - STATE_UNKNOWN, -}; int main(int argc, char **argv) { setlocale(LC_ALL, ""); @@ -61,16 +58,24 @@ int main(int argc, char **argv) { timeout_interval = DEFAULT_TIMEOUT; - char **command_line = (char **)process_arguments(argc, argv); + negate_config_wrapper tmp_config = process_arguments(argc, argv); + + if (tmp_config.errorcode == ERROR) { + die(STATE_UNKNOWN, _("negate: Failed to parse input")); + } + + negate_config config = tmp_config.config; + + char **command_line = config.command_line; /* Set signal handling and alarm */ if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) { die(STATE_UNKNOWN, _("Cannot catch SIGALRM")); } - (void)alarm((unsigned)timeout_interval); + (void)alarm(timeout_interval); - int result = STATE_UNKNOWN; + mp_state_enum result = STATE_UNKNOWN; output chld_out; output chld_err; @@ -93,34 +98,38 @@ int main(int argc, char **argv) { char *sub; for (size_t i = 0; i < chld_out.lines; i++) { - if (subst_text && result >= 0 && result <= 4 && result != state[result]) { + if (config.subst_text && result >= 0 && result <= 4 && result != config.state[result]) { /* Loop over each match found */ while ((sub = strstr(chld_out.line[i], state_text(result)))) { /* Terminate the first part and skip over the string we'll substitute */ *sub = '\0'; sub += strlen(state_text(result)); /* then put everything back together */ - xasprintf(&chld_out.line[i], "%s%s%s", chld_out.line[i], state_text(state[result]), sub); + xasprintf(&chld_out.line[i], "%s%s%s", chld_out.line[i], state_text(config.state[result]), sub); } } printf("%s\n", chld_out.line[i]); } if (result >= 0 && result <= 4) { - exit(state[result]); + exit(config.state[result]); } else { exit(result); } } /* process command-line arguments */ -static const char **process_arguments(int argc, char **argv) { +static negate_config_wrapper process_arguments(int argc, char **argv) { static struct option longopts[] = {{"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"timeout", required_argument, 0, 't'}, {"timeout-result", required_argument, 0, 'T'}, {"ok", required_argument, 0, 'o'}, {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {"unknown", required_argument, 0, 'u'}, {"substitute", no_argument, 0, 's'}, {0, 0, 0, 0}}; + negate_config_wrapper result = { + .errorcode = OK, + .config = negate_config_init(), + }; bool permute = true; while (true) { int option = 0; @@ -154,54 +163,56 @@ static const char **process_arguments(int argc, char **argv) { } break; case 'o': /* replacement for OK */ - if ((state[STATE_OK] = mp_translate_state(optarg)) == ERROR) { + if ((result.config.state[STATE_OK] = mp_translate_state(optarg)) == ERROR) { usage4(_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); } permute = false; break; case 'w': /* replacement for WARNING */ - if ((state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR) { + if ((result.config.state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR) { usage4(_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); } permute = false; break; case 'c': /* replacement for CRITICAL */ - if ((state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR) { + if ((result.config.state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR) { usage4(_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); } permute = false; break; case 'u': /* replacement for UNKNOWN */ - if ((state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR) { + if ((result.config.state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR) { usage4(_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); } permute = false; break; case 's': /* Substitute status text */ - subst_text = true; + result.config.subst_text = true; break; } } - validate_arguments(&argv[optind]); - if (permute) { /* No [owcu] switch specified, default to this */ - state[STATE_OK] = STATE_CRITICAL; - state[STATE_CRITICAL] = STATE_OK; + result.config.state[STATE_OK] = STATE_CRITICAL; + result.config.state[STATE_CRITICAL] = STATE_OK; } - return (const char **)&argv[optind]; + result.config.command_line = &argv[optind]; + + return validate_arguments(result); } -void validate_arguments(char **command_line) { - if (command_line[0] == NULL) { +negate_config_wrapper validate_arguments(negate_config_wrapper config_wrapper) { + if (config_wrapper.config.command_line[0] == NULL) { usage4(_("Could not parse arguments")); } - if (strncmp(command_line[0], "/", 1) != 0 && strncmp(command_line[0], "./", 2) != 0) { + if (strncmp(config_wrapper.config.command_line[0], "/", 1) != 0 && strncmp(config_wrapper.config.command_line[0], "./", 2) != 0) { usage4(_("Require path to command")); } + + return config_wrapper; } void print_help(void) { diff --git a/plugins/negate.d/config.h b/plugins/negate.d/config.h new file mode 100644 index 00000000..0cf30cd4 --- /dev/null +++ b/plugins/negate.d/config.h @@ -0,0 +1,24 @@ +#pragma once + +#include "states.h" + +typedef struct { + mp_state_enum state[4]; + bool subst_text; + char **command_line; +} negate_config; + +negate_config negate_config_init() { + negate_config tmp = { + .state = + { + STATE_OK, + STATE_WARNING, + STATE_CRITICAL, + STATE_UNKNOWN, + }, + .subst_text = false, + .command_line = NULL, + }; + return tmp; +} -- cgit v1.2.3-74-g34f1 From d2596feaa090c73353412d252cfb7938a9141f9b Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 13 Mar 2025 14:59:35 +0100 Subject: Add forgotten Makefile change --- plugins/Makefile.am | 1 + 1 file changed, 1 insertion(+) (limited to 'plugins/Makefile.am') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 9e4924c3..30ca63d1 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -58,6 +58,7 @@ EXTRA_DIST = t \ check_time.d \ check_nagios.d \ check_dbi.d \ + check_tcp.d \ check_real.d \ check_ssh.d \ check_nt.d \ -- cgit v1.2.3-74-g34f1 From 285db2a9fa25519cacd48a76347ae2dee0c06605 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 18 Mar 2025 14:36:20 +0100 Subject: Move disk specific stuff from lib to plugin specific directory --- lib/Makefile.am | 3 +- lib/utils_disk.c | 255 -------------------------------------- lib/utils_disk.h | 48 ------- plugins/Makefile.am | 2 + plugins/check_disk.d/utils_disk.c | 255 ++++++++++++++++++++++++++++++++++++++ plugins/check_disk.d/utils_disk.h | 48 +++++++ 6 files changed, 306 insertions(+), 305 deletions(-) delete mode 100644 lib/utils_disk.c delete mode 100644 lib/utils_disk.h create mode 100644 plugins/check_disk.d/utils_disk.c create mode 100644 plugins/check_disk.d/utils_disk.h (limited to 'plugins/Makefile.am') diff --git a/lib/Makefile.am b/lib/Makefile.am index e41201c4..a9f3ff40 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -7,10 +7,9 @@ noinst_LIBRARIES = libmonitoringplug.a AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \ -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins -libmonitoringplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c maxfd.c output.c perfdata.c output.c thresholds.c vendor/cJSON/cJSON.c +libmonitoringplug_a_SOURCES = utils_base.c utils_tcp.c utils_cmd.c maxfd.c output.c perfdata.c output.c thresholds.c vendor/cJSON/cJSON.c EXTRA_DIST = utils_base.h \ - utils_disk.h \ utils_tcp.h \ utils_cmd.h \ parse_ini.h \ diff --git a/lib/utils_disk.c b/lib/utils_disk.c deleted file mode 100644 index 2b761f5e..00000000 --- a/lib/utils_disk.c +++ /dev/null @@ -1,255 +0,0 @@ -/***************************************************************************** - * - * Library for check_disk - * - * License: GPL - * Copyright (c) 1999-2024 Monitoring Plugins Development Team - * - * Description: - * - * This file contains utilities for check_disk. These are tested by libtap - * - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * - *****************************************************************************/ - -#include "common.h" -#include "utils_disk.h" -#include "gl/fsusage.h" -#include - -void np_add_name(struct name_list **list, const char *name) { - struct name_list *new_entry; - new_entry = (struct name_list *)malloc(sizeof *new_entry); - new_entry->name = (char *)name; - new_entry->next = *list; - *list = new_entry; -} - -/* @brief Initialises a new regex at the begin of list via regcomp(3) - * - * @details if the regex fails to compile the error code of regcomp(3) is returned - * and list is not modified, otherwise list is modified to point to the new - * element - * @param list Pointer to a linked list of regex_list elements - * @param regex the string containing the regex which should be inserted into the list - * @param clags the cflags parameter for regcomp(3) - */ -int np_add_regex(struct regex_list **list, const char *regex, int cflags) { - struct regex_list *new_entry = (struct regex_list *)malloc(sizeof *new_entry); - - if (new_entry == NULL) { - die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); - } - - int regcomp_result = regcomp(&new_entry->regex, regex, cflags); - - if (!regcomp_result) { - // regcomp succeeded - new_entry->next = *list; - *list = new_entry; - - return 0; - } else { - // regcomp failed - free(new_entry); - - return regcomp_result; - } -} - -/* Initialises a new parameter at the end of list */ -struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name) { - struct parameter_list *current = *list; - struct parameter_list *new_path; - new_path = (struct parameter_list *)malloc(sizeof *new_path); - new_path->name = (char *)malloc(strlen(name) + 1); - new_path->best_match = NULL; - new_path->name_next = NULL; - new_path->name_prev = NULL; - new_path->freespace_bytes = NULL; - new_path->freespace_units = NULL; - new_path->freespace_percent = NULL; - new_path->usedspace_bytes = NULL; - new_path->usedspace_units = NULL; - new_path->usedspace_percent = NULL; - new_path->usedinodes_percent = NULL; - new_path->freeinodes_percent = NULL; - new_path->group = NULL; - new_path->dfree_pct = -1; - new_path->dused_pct = -1; - new_path->total = 0; - new_path->available = 0; - new_path->available_to_root = 0; - new_path->used = 0; - new_path->dused_units = 0; - new_path->dfree_units = 0; - new_path->dtotal_units = 0; - new_path->inodes_total = 0; - new_path->inodes_free = 0; - new_path->inodes_free_to_root = 0; - new_path->inodes_used = 0; - new_path->dused_inodes_percent = 0; - new_path->dfree_inodes_percent = 0; - - strcpy(new_path->name, name); - - if (current == NULL) { - *list = new_path; - new_path->name_prev = NULL; - } else { - while (current->name_next) { - current = current->name_next; - } - current->name_next = new_path; - new_path->name_prev = current; - } - return new_path; -} - -/* Delete a given parameter from list and return pointer to next element*/ -struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev) { - if (item == NULL) { - return NULL; - } - struct parameter_list *next; - - if (item->name_next) - next = item->name_next; - else - next = NULL; - - if (next) - next->name_prev = prev; - - if (prev) - prev->name_next = next; - - if (item->name) { - free(item->name); - } - free(item); - - return next; -} - -/* returns a pointer to the struct found in the list */ -struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name) { - struct parameter_list *temp_list; - for (temp_list = list; temp_list; temp_list = temp_list->name_next) { - if (!strcmp(temp_list->name, name)) - return temp_list; - } - - return NULL; -} - -void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact) { - struct parameter_list *d; - for (d = desired; d; d = d->name_next) { - if (!d->best_match) { - struct mount_entry *me; - size_t name_len = strlen(d->name); - size_t best_match_len = 0; - struct mount_entry *best_match = NULL; - struct fs_usage fsp; - - /* set best match if path name exactly matches a mounted device name */ - for (me = mount_list; me; me = me->me_next) { - if (strcmp(me->me_devname, d->name) == 0) { - if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { - best_match = me; - } - } - } - - /* set best match by directory name if no match was found by devname */ - if (!best_match) { - for (me = mount_list; me; me = me->me_next) { - size_t len = strlen(me->me_mountdir); - if ((!exact && - (best_match_len <= len && len <= name_len && (len == 1 || strncmp(me->me_mountdir, d->name, len) == 0))) || - (exact && strcmp(me->me_mountdir, d->name) == 0)) { - if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { - best_match = me; - best_match_len = len; - } - } - } - } - - if (best_match) { - d->best_match = best_match; - } else { - d->best_match = NULL; /* Not sure why this is needed as it should be null on initialisation */ - } - } - } -} - -/* Returns true if name is in list */ -bool np_find_name(struct name_list *list, const char *name) { - const struct name_list *n; - - if (list == NULL || name == NULL) { - return false; - } - for (n = list; n; n = n->next) { - if (!strcmp(name, n->name)) { - return true; - } - } - return false; -} - -/* Returns true if name is in list */ -bool np_find_regmatch(struct regex_list *list, const char *name) { - int len; - regmatch_t m; - - if (name == NULL) { - return false; - } - - len = strlen(name); - - for (; list; list = list->next) { - /* Emulate a full match as if surrounded with ^( )$ - by checking whether the match spans the whole name */ - if (!regexec(&list->regex, name, 1, &m, 0) && m.rm_so == 0 && m.rm_eo == len) { - return true; - } - } - - return false; -} - -bool np_seen_name(struct name_list *list, const char *name) { - const struct name_list *s; - for (s = list; s; s = s->next) { - if (!strcmp(s->name, name)) { - return true; - } - } - return false; -} - -bool np_regex_match_mount_entry(struct mount_entry *me, regex_t *re) { - if (regexec(re, me->me_devname, (size_t)0, NULL, 0) == 0 || regexec(re, me->me_mountdir, (size_t)0, NULL, 0) == 0) { - return true; - } - return false; -} diff --git a/lib/utils_disk.h b/lib/utils_disk.h deleted file mode 100644 index c5e81dc1..00000000 --- a/lib/utils_disk.h +++ /dev/null @@ -1,48 +0,0 @@ -/* Header file for utils_disk */ - -#include "mountlist.h" -#include "utils_base.h" -#include "regex.h" - -struct name_list { - char *name; - struct name_list *next; -}; - -struct regex_list { - regex_t regex; - struct regex_list *next; -}; - -struct parameter_list { - char *name; - thresholds *freespace_bytes; - thresholds *freespace_units; - thresholds *freespace_percent; - thresholds *usedspace_bytes; - thresholds *usedspace_units; - thresholds *usedspace_percent; - thresholds *usedinodes_percent; - thresholds *freeinodes_percent; - char *group; - struct mount_entry *best_match; - struct parameter_list *name_next; - struct parameter_list *name_prev; - uintmax_t total, available, available_to_root, used, inodes_free, inodes_free_to_root, inodes_used, inodes_total; - double dfree_pct, dused_pct; - uint64_t dused_units, dfree_units, dtotal_units; - double dused_inodes_percent, dfree_inodes_percent; -}; - -void np_add_name(struct name_list **list, const char *name); -bool np_find_name(struct name_list *list, const char *name); -bool np_seen_name(struct name_list *list, const char *name); -int np_add_regex(struct regex_list **list, const char *regex, int cflags); -bool np_find_regmatch(struct regex_list *list, const char *name); -struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name); -struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name); -struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev); - -int search_parameter_list(struct parameter_list *list, const char *name); -void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact); -bool np_regex_match_mount_entry(struct mount_entry *me, regex_t *re); diff --git a/plugins/Makefile.am b/plugins/Makefile.am index e2bed4c3..30283cb4 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -55,6 +55,7 @@ EXTRA_DIST = t \ check_hpjd.d \ check_game.d \ check_radius.d \ + check_disk.d \ check_time.d \ check_nagios.d \ check_dbi.d \ @@ -119,6 +120,7 @@ check_curl_LDADD = $(NETLIBS) $(LIBCURLLIBS) $(SSLOBJS) $(URIPARSERLIBS) picohtt check_dbi_LDADD = $(NETLIBS) $(DBILIBS) check_dig_LDADD = $(NETLIBS) check_disk_LDADD = $(BASEOBJS) +check_disk_SOURCES = check_disk.c check_disk.d/utils_disk.c check_dns_LDADD = $(NETLIBS) check_dummy_LDADD = $(BASEOBJS) check_fping_LDADD = $(NETLIBS) diff --git a/plugins/check_disk.d/utils_disk.c b/plugins/check_disk.d/utils_disk.c new file mode 100644 index 00000000..2b761f5e --- /dev/null +++ b/plugins/check_disk.d/utils_disk.c @@ -0,0 +1,255 @@ +/***************************************************************************** + * + * Library for check_disk + * + * License: GPL + * Copyright (c) 1999-2024 Monitoring Plugins Development Team + * + * Description: + * + * This file contains utilities for check_disk. These are tested by libtap + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + *****************************************************************************/ + +#include "common.h" +#include "utils_disk.h" +#include "gl/fsusage.h" +#include + +void np_add_name(struct name_list **list, const char *name) { + struct name_list *new_entry; + new_entry = (struct name_list *)malloc(sizeof *new_entry); + new_entry->name = (char *)name; + new_entry->next = *list; + *list = new_entry; +} + +/* @brief Initialises a new regex at the begin of list via regcomp(3) + * + * @details if the regex fails to compile the error code of regcomp(3) is returned + * and list is not modified, otherwise list is modified to point to the new + * element + * @param list Pointer to a linked list of regex_list elements + * @param regex the string containing the regex which should be inserted into the list + * @param clags the cflags parameter for regcomp(3) + */ +int np_add_regex(struct regex_list **list, const char *regex, int cflags) { + struct regex_list *new_entry = (struct regex_list *)malloc(sizeof *new_entry); + + if (new_entry == NULL) { + die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); + } + + int regcomp_result = regcomp(&new_entry->regex, regex, cflags); + + if (!regcomp_result) { + // regcomp succeeded + new_entry->next = *list; + *list = new_entry; + + return 0; + } else { + // regcomp failed + free(new_entry); + + return regcomp_result; + } +} + +/* Initialises a new parameter at the end of list */ +struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name) { + struct parameter_list *current = *list; + struct parameter_list *new_path; + new_path = (struct parameter_list *)malloc(sizeof *new_path); + new_path->name = (char *)malloc(strlen(name) + 1); + new_path->best_match = NULL; + new_path->name_next = NULL; + new_path->name_prev = NULL; + new_path->freespace_bytes = NULL; + new_path->freespace_units = NULL; + new_path->freespace_percent = NULL; + new_path->usedspace_bytes = NULL; + new_path->usedspace_units = NULL; + new_path->usedspace_percent = NULL; + new_path->usedinodes_percent = NULL; + new_path->freeinodes_percent = NULL; + new_path->group = NULL; + new_path->dfree_pct = -1; + new_path->dused_pct = -1; + new_path->total = 0; + new_path->available = 0; + new_path->available_to_root = 0; + new_path->used = 0; + new_path->dused_units = 0; + new_path->dfree_units = 0; + new_path->dtotal_units = 0; + new_path->inodes_total = 0; + new_path->inodes_free = 0; + new_path->inodes_free_to_root = 0; + new_path->inodes_used = 0; + new_path->dused_inodes_percent = 0; + new_path->dfree_inodes_percent = 0; + + strcpy(new_path->name, name); + + if (current == NULL) { + *list = new_path; + new_path->name_prev = NULL; + } else { + while (current->name_next) { + current = current->name_next; + } + current->name_next = new_path; + new_path->name_prev = current; + } + return new_path; +} + +/* Delete a given parameter from list and return pointer to next element*/ +struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev) { + if (item == NULL) { + return NULL; + } + struct parameter_list *next; + + if (item->name_next) + next = item->name_next; + else + next = NULL; + + if (next) + next->name_prev = prev; + + if (prev) + prev->name_next = next; + + if (item->name) { + free(item->name); + } + free(item); + + return next; +} + +/* returns a pointer to the struct found in the list */ +struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name) { + struct parameter_list *temp_list; + for (temp_list = list; temp_list; temp_list = temp_list->name_next) { + if (!strcmp(temp_list->name, name)) + return temp_list; + } + + return NULL; +} + +void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact) { + struct parameter_list *d; + for (d = desired; d; d = d->name_next) { + if (!d->best_match) { + struct mount_entry *me; + size_t name_len = strlen(d->name); + size_t best_match_len = 0; + struct mount_entry *best_match = NULL; + struct fs_usage fsp; + + /* set best match if path name exactly matches a mounted device name */ + for (me = mount_list; me; me = me->me_next) { + if (strcmp(me->me_devname, d->name) == 0) { + if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { + best_match = me; + } + } + } + + /* set best match by directory name if no match was found by devname */ + if (!best_match) { + for (me = mount_list; me; me = me->me_next) { + size_t len = strlen(me->me_mountdir); + if ((!exact && + (best_match_len <= len && len <= name_len && (len == 1 || strncmp(me->me_mountdir, d->name, len) == 0))) || + (exact && strcmp(me->me_mountdir, d->name) == 0)) { + if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { + best_match = me; + best_match_len = len; + } + } + } + } + + if (best_match) { + d->best_match = best_match; + } else { + d->best_match = NULL; /* Not sure why this is needed as it should be null on initialisation */ + } + } + } +} + +/* Returns true if name is in list */ +bool np_find_name(struct name_list *list, const char *name) { + const struct name_list *n; + + if (list == NULL || name == NULL) { + return false; + } + for (n = list; n; n = n->next) { + if (!strcmp(name, n->name)) { + return true; + } + } + return false; +} + +/* Returns true if name is in list */ +bool np_find_regmatch(struct regex_list *list, const char *name) { + int len; + regmatch_t m; + + if (name == NULL) { + return false; + } + + len = strlen(name); + + for (; list; list = list->next) { + /* Emulate a full match as if surrounded with ^( )$ + by checking whether the match spans the whole name */ + if (!regexec(&list->regex, name, 1, &m, 0) && m.rm_so == 0 && m.rm_eo == len) { + return true; + } + } + + return false; +} + +bool np_seen_name(struct name_list *list, const char *name) { + const struct name_list *s; + for (s = list; s; s = s->next) { + if (!strcmp(s->name, name)) { + return true; + } + } + return false; +} + +bool np_regex_match_mount_entry(struct mount_entry *me, regex_t *re) { + if (regexec(re, me->me_devname, (size_t)0, NULL, 0) == 0 || regexec(re, me->me_mountdir, (size_t)0, NULL, 0) == 0) { + return true; + } + return false; +} diff --git a/plugins/check_disk.d/utils_disk.h b/plugins/check_disk.d/utils_disk.h new file mode 100644 index 00000000..c5e81dc1 --- /dev/null +++ b/plugins/check_disk.d/utils_disk.h @@ -0,0 +1,48 @@ +/* Header file for utils_disk */ + +#include "mountlist.h" +#include "utils_base.h" +#include "regex.h" + +struct name_list { + char *name; + struct name_list *next; +}; + +struct regex_list { + regex_t regex; + struct regex_list *next; +}; + +struct parameter_list { + char *name; + thresholds *freespace_bytes; + thresholds *freespace_units; + thresholds *freespace_percent; + thresholds *usedspace_bytes; + thresholds *usedspace_units; + thresholds *usedspace_percent; + thresholds *usedinodes_percent; + thresholds *freeinodes_percent; + char *group; + struct mount_entry *best_match; + struct parameter_list *name_next; + struct parameter_list *name_prev; + uintmax_t total, available, available_to_root, used, inodes_free, inodes_free_to_root, inodes_used, inodes_total; + double dfree_pct, dused_pct; + uint64_t dused_units, dfree_units, dtotal_units; + double dused_inodes_percent, dfree_inodes_percent; +}; + +void np_add_name(struct name_list **list, const char *name); +bool np_find_name(struct name_list *list, const char *name); +bool np_seen_name(struct name_list *list, const char *name); +int np_add_regex(struct regex_list **list, const char *regex, int cflags); +bool np_find_regmatch(struct regex_list *list, const char *name); +struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name); +struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name); +struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev); + +int search_parameter_list(struct parameter_list *list, const char *name); +void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact); +bool np_regex_match_mount_entry(struct mount_entry *me, regex_t *re); -- cgit v1.2.3-74-g34f1 From 59e0a258f9c0f393bf29cced1f35743f74e7b10c Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 18 Mar 2025 15:57:44 +0100 Subject: Migrate disk tests from lib, tool --- .gitignore | 3 +- configure.ac | 4 +- lib/tests/Makefile.am | 6 +- lib/tests/test_disk.c | 192 -------------------------------------- lib/tests/test_disk.t | 6 -- plugins/Makefile.am | 8 +- plugins/check_disk.d/utils_disk.c | 4 +- plugins/check_disk.d/utils_disk.h | 2 +- plugins/common.h | 6 +- plugins/tests/test_check_disk.c | 192 ++++++++++++++++++++++++++++++++++++++ plugins/tests/test_check_disk.t | 6 ++ 11 files changed, 216 insertions(+), 213 deletions(-) delete mode 100644 lib/tests/test_disk.c delete mode 100755 lib/tests/test_disk.t create mode 100644 plugins/tests/test_check_disk.c create mode 100755 plugins/tests/test_check_disk.t (limited to 'plugins/Makefile.am') diff --git a/.gitignore b/.gitignore index 5245495e..8b14f429 100644 --- a/.gitignore +++ b/.gitignore @@ -114,7 +114,6 @@ NP-VERSION-FILE /lib/tests/Makefile.in /lib/tests/test_base64 /lib/tests/test_cmd -/lib/tests/test_disk /lib/tests/test_tcp /lib/tests/test_utils /lib/tests/utils_base.Po @@ -223,7 +222,7 @@ plugins/check_disk.d/.dirstamp /plugins/tests/Makefile /plugins/tests/Makefile.in /plugins/tests/test_utils -/plugins/tests/test_disk +/plugins/tests/test_check_disk /plugins/tests/test_check_swap /plugins/tests/.deps /plugins/tests/.dirstamp diff --git a/configure.ac b/configure.ac index 204fc6e3..fdc9b699 100644 --- a/configure.ac +++ b/configure.ac @@ -181,10 +181,10 @@ fi # Finally, define tests if we use libtap if test "$enable_libtap" = "yes" ; then - EXTRA_TEST="test_utils test_disk test_tcp test_cmd test_base64" + EXTRA_TEST="test_utils test_tcp test_cmd test_base64" AC_SUBST(EXTRA_TEST) - EXTRA_PLUGIN_TESTS="tests/test_check_swap" + EXTRA_PLUGIN_TESTS="tests/test_check_swap tests/test_check_disk" AC_SUBST(EXTRA_PLUGIN_TESTS) fi diff --git a/lib/tests/Makefile.am b/lib/tests/Makefile.am index 9be94f6d..7798a72e 100644 --- a/lib/tests/Makefile.am +++ b/lib/tests/Makefile.am @@ -8,9 +8,9 @@ check_PROGRAMS = @EXTRA_TEST@ AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \ -I$(top_srcdir)/lib -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins -EXTRA_PROGRAMS = test_utils test_disk test_tcp test_cmd test_base64 test_ini1 test_ini3 test_opts1 test_opts2 test_opts3 test_generic_output +EXTRA_PROGRAMS = test_utils test_tcp test_cmd test_base64 test_ini1 test_ini3 test_opts1 test_opts2 test_opts3 test_generic_output -np_test_scripts = test_base64.t test_cmd.t test_disk.t test_ini1.t test_ini3.t test_opts1.t test_opts2.t test_opts3.t test_tcp.t test_utils.t test_generic_output.t +np_test_scripts = test_base64.t test_cmd.t test_ini1.t test_ini3.t test_opts1.t test_opts2.t test_opts3.t test_tcp.t test_utils.t test_generic_output.t np_test_files = config-dos.ini config-opts.ini config-tiny.ini plugin.ini plugins.ini EXTRA_DIST = $(np_test_scripts) $(np_test_files) var @@ -29,7 +29,7 @@ AM_CFLAGS = -g -I$(top_srcdir)/lib -I$(top_srcdir)/gl $(tap_cflags) AM_LDFLAGS = $(tap_ldflags) -ltap LDADD = $(top_srcdir)/lib/libmonitoringplug.a $(top_srcdir)/gl/libgnu.a $(LIB_CRYPTO) -SOURCES = test_utils.c test_disk.c test_tcp.c test_cmd.c test_base64.c test_ini1.c test_ini3.c test_opts1.c test_opts2.c test_opts3.c test_generic_output.c +SOURCES = test_utils.c test_tcp.c test_cmd.c test_base64.c test_ini1.c test_ini3.c test_opts1.c test_opts2.c test_opts3.c test_generic_output.c test: ${noinst_PROGRAMS} perl -MTest::Harness -e '$$Test::Harness::switches=""; runtests(map {$$_ .= ".t"} @ARGV)' $(EXTRA_PROGRAMS) diff --git a/lib/tests/test_disk.c b/lib/tests/test_disk.c deleted file mode 100644 index c18db7a4..00000000 --- a/lib/tests/test_disk.c +++ /dev/null @@ -1,192 +0,0 @@ -/***************************************************************************** - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * - *****************************************************************************/ - -#include "common.h" -#include "utils_disk.h" -#include "tap.h" -#include "regex.h" - -void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc); - -int main(int argc, char **argv) { - struct name_list *exclude_filesystem = NULL; - struct name_list *exclude_fstype = NULL; - struct name_list *dummy_mountlist = NULL; - struct name_list *temp_name; - struct parameter_list *paths = NULL; - struct parameter_list *p, *prev = NULL, *last = NULL; - - struct mount_entry *dummy_mount_list; - struct mount_entry *me; - struct mount_entry **mtail = &dummy_mount_list; - int cflags = REG_NOSUB | REG_EXTENDED; - int found = 0, count = 0; - - plan_tests(33); - - ok(np_find_name(exclude_filesystem, "/var/log") == false, "/var/log not in list"); - np_add_name(&exclude_filesystem, "/var/log"); - ok(np_find_name(exclude_filesystem, "/var/log") == true, "is in list now"); - ok(np_find_name(exclude_filesystem, "/home") == false, "/home not in list"); - np_add_name(&exclude_filesystem, "/home"); - ok(np_find_name(exclude_filesystem, "/home") == true, "is in list now"); - ok(np_find_name(exclude_filesystem, "/var/log") == true, "/var/log still in list"); - - ok(np_find_name(exclude_fstype, "iso9660") == false, "iso9660 not in list"); - np_add_name(&exclude_fstype, "iso9660"); - ok(np_find_name(exclude_fstype, "iso9660") == true, "is in list now"); - - ok(np_find_name(exclude_filesystem, "iso9660") == false, "Make sure no clashing in variables"); - - /* - for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) { - printf("Name: %s\n", temp_name->name); - } - */ - - me = (struct mount_entry *)malloc(sizeof *me); - me->me_devname = strdup("/dev/c0t0d0s0"); - me->me_mountdir = strdup("/"); - *mtail = me; - mtail = &me->me_next; - - me = (struct mount_entry *)malloc(sizeof *me); - me->me_devname = strdup("/dev/c1t0d1s0"); - me->me_mountdir = strdup("/var"); - *mtail = me; - mtail = &me->me_next; - - me = (struct mount_entry *)malloc(sizeof *me); - me->me_devname = strdup("/dev/c2t0d0s0"); - me->me_mountdir = strdup("/home"); - *mtail = me; - mtail = &me->me_next; - - np_test_mount_entry_regex(dummy_mount_list, strdup("/"), cflags, 3, strdup("a")); - np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"), cflags, 3, strdup("regex on dev names:")); - np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"), cflags, 0, strdup("regex on non existent dev/path:")); - np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"), cflags | REG_ICASE, 0, strdup("regi on non existent dev/path:")); - np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"), cflags, 3, strdup("partial devname regex match:")); - np_test_mount_entry_regex(dummy_mount_list, strdup("c0t0"), cflags, 1, strdup("partial devname regex match:")); - np_test_mount_entry_regex(dummy_mount_list, strdup("C0t0"), cflags | REG_ICASE, 1, strdup("partial devname regi match:")); - np_test_mount_entry_regex(dummy_mount_list, strdup("home"), cflags, 1, strdup("partial pathname regex match:")); - np_test_mount_entry_regex(dummy_mount_list, strdup("hOme"), cflags | REG_ICASE, 1, strdup("partial pathname regi match:")); - np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"), cflags, 2, strdup("grouped regex pathname match:")); - np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"), cflags | REG_ICASE, 2, strdup("grouped regi pathname match:")); - - np_add_parameter(&paths, "/home/groups"); - np_add_parameter(&paths, "/var"); - np_add_parameter(&paths, "/tmp"); - np_add_parameter(&paths, "/home/tonvoon"); - np_add_parameter(&paths, "/dev/c2t0d0s0"); - - np_set_best_match(paths, dummy_mount_list, false); - for (p = paths; p; p = p->name_next) { - struct mount_entry *temp_me; - temp_me = p->best_match; - if (!strcmp(p->name, "/home/groups")) { - ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home"); - } else if (!strcmp(p->name, "/var")) { - ok(temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var"); - } else if (!strcmp(p->name, "/tmp")) { - ok(temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /"); - } else if (!strcmp(p->name, "/home/tonvoon")) { - ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home"); - } else if (!strcmp(p->name, "/dev/c2t0d0s0")) { - ok(temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0"); - } - } - - paths = NULL; /* Bad boy - should free, but this is a test suite */ - np_add_parameter(&paths, "/home/groups"); - np_add_parameter(&paths, "/var"); - np_add_parameter(&paths, "/tmp"); - np_add_parameter(&paths, "/home/tonvoon"); - np_add_parameter(&paths, "/home"); - - np_set_best_match(paths, dummy_mount_list, true); - for (p = paths; p; p = p->name_next) { - if (!strcmp(p->name, "/home/groups")) { - ok(!p->best_match, "/home/groups correctly not found"); - } else if (!strcmp(p->name, "/var")) { - ok(p->best_match, "/var found"); - } else if (!strcmp(p->name, "/tmp")) { - ok(!p->best_match, "/tmp correctly not found"); - } else if (!strcmp(p->name, "/home/tonvoon")) { - ok(!p->best_match, "/home/tonvoon not found"); - } else if (!strcmp(p->name, "/home")) { - ok(p->best_match, "/home found"); - } - } - - /* test deleting first element in paths */ - paths = np_del_parameter(paths, NULL); - for (p = paths; p; p = p->name_next) { - if (!strcmp(p->name, "/home/groups")) - found = 1; - } - ok(found == 0, "first element successfully deleted"); - found = 0; - - p = paths; - while (p) { - if (!strcmp(p->name, "/tmp")) - p = np_del_parameter(p, prev); - else { - prev = p; - p = p->name_next; - } - } - - for (p = paths; p; p = p->name_next) { - if (!strcmp(p->name, "/tmp")) - found = 1; - if (p->name_next) - prev = p; - else - last = p; - } - ok(found == 0, "/tmp element successfully deleted"); - - p = np_del_parameter(last, prev); - for (p = paths; p; p = p->name_next) { - if (!strcmp(p->name, "/home")) - found = 1; - last = p; - count++; - } - ok(found == 0, "last (/home) element successfully deleted"); - ok(count == 2, "two elements remaining"); - - return exit_status(); -} - -void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc) { - int matches = 0; - regex_t re; - struct mount_entry *me; - if (regcomp(&re, regstr, cflags) == 0) { - for (me = dummy_mount_list; me; me = me->me_next) { - if (np_regex_match_mount_entry(me, &re)) - matches++; - } - ok(matches == expect, "%s '%s' matched %i/3 entries. ok: %i/3", desc, regstr, expect, matches); - - } else - ok(false, "regex '%s' not compilable", regstr); -} diff --git a/lib/tests/test_disk.t b/lib/tests/test_disk.t deleted file mode 100755 index da84dfdf..00000000 --- a/lib/tests/test_disk.t +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/perl -use Test::More; -if (! -e "./test_disk") { - plan skip_all => "./test_disk not compiled - please enable libtap library to test"; -} -exec "./test_disk"; diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 30283cb4..04fb7ed2 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -40,11 +40,13 @@ EXTRA_PROGRAMS = check_mysql check_radius check_pgsql check_snmp check_hpjd \ check_nagios check_by_ssh check_dns check_nt check_ide_smart \ check_procs check_mysql_query check_apt check_dbi check_curl \ \ - tests/test_check_swap + tests/test_check_swap \ + tests/test_check_disk SUBDIRS = picohttpparser -np_test_scripts = tests/test_check_swap.t +np_test_scripts = tests/test_check_swap.t \ + tests/test_check_disk.t EXTRA_DIST = t \ tests \ @@ -167,6 +169,8 @@ endif tests_test_check_swap_LDADD = $(BASEOBJS) $(tap_ldflags) -ltap tests_test_check_swap_SOURCES = tests/test_check_swap.c check_swap.d/swap.c +tests_test_check_disk_LDADD = $(BASEOBJS) $(tap_ldflags) check_disk.d/utils_disk.c -ltap +tests_test_check_disk_SOURCES = tests/test_check_disk.c ############################################################################## # secondary dependencies diff --git a/plugins/check_disk.d/utils_disk.c b/plugins/check_disk.d/utils_disk.c index 1d806715..369c85d5 100644 --- a/plugins/check_disk.d/utils_disk.c +++ b/plugins/check_disk.d/utils_disk.c @@ -26,9 +26,9 @@ * *****************************************************************************/ -#include "common.h" +#include "../common.h" #include "utils_disk.h" -#include "gl/fsusage.h" +#include "../../gl/fsusage.h" #include void np_add_name(struct name_list **list, const char *name) { diff --git a/plugins/check_disk.d/utils_disk.h b/plugins/check_disk.d/utils_disk.h index 1c68fed9..0c69f987 100644 --- a/plugins/check_disk.d/utils_disk.h +++ b/plugins/check_disk.d/utils_disk.h @@ -2,7 +2,7 @@ #include "../../config.h" #include "../../gl/mountlist.h" -#include "utils_base.h" +#include "../../lib/utils_base.h" #include "regex.h" #include diff --git a/plugins/common.h b/plugins/common.h index 603bae55..35d1e549 100644 --- a/plugins/common.h +++ b/plugins/common.h @@ -31,7 +31,7 @@ #ifndef _COMMON_H_ #define _COMMON_H_ -#include "config.h" +#include "../config.h" #include "../lib/monitoringplug.h" #ifdef HAVE_FEATURES_H @@ -110,7 +110,7 @@ /* GNU Libraries */ #include -#include "dirname.h" +#include "../gl/dirname.h" #include @@ -190,7 +190,7 @@ enum { * Internationalization * */ -#include "gettext.h" +#include "../gl/gettext.h" #define _(String) gettext (String) #if ! ENABLE_NLS # undef textdomain diff --git a/plugins/tests/test_check_disk.c b/plugins/tests/test_check_disk.c new file mode 100644 index 00000000..92d0d270 --- /dev/null +++ b/plugins/tests/test_check_disk.c @@ -0,0 +1,192 @@ +/***************************************************************************** + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + *****************************************************************************/ + +#include "common.h" +#include "../check_disk.d/utils_disk.h" +#include "../../tap/tap.h" +#include "regex.h" + +void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc); + +int main(int argc, char **argv) { + struct name_list *exclude_filesystem = NULL; + struct name_list *exclude_fstype = NULL; + struct name_list *dummy_mountlist = NULL; + struct name_list *temp_name; + struct parameter_list *paths = NULL; + struct parameter_list *p, *prev = NULL, *last = NULL; + + struct mount_entry *dummy_mount_list; + struct mount_entry *me; + struct mount_entry **mtail = &dummy_mount_list; + int cflags = REG_NOSUB | REG_EXTENDED; + int found = 0, count = 0; + + plan_tests(33); + + ok(np_find_name(exclude_filesystem, "/var/log") == false, "/var/log not in list"); + np_add_name(&exclude_filesystem, "/var/log"); + ok(np_find_name(exclude_filesystem, "/var/log") == true, "is in list now"); + ok(np_find_name(exclude_filesystem, "/home") == false, "/home not in list"); + np_add_name(&exclude_filesystem, "/home"); + ok(np_find_name(exclude_filesystem, "/home") == true, "is in list now"); + ok(np_find_name(exclude_filesystem, "/var/log") == true, "/var/log still in list"); + + ok(np_find_name(exclude_fstype, "iso9660") == false, "iso9660 not in list"); + np_add_name(&exclude_fstype, "iso9660"); + ok(np_find_name(exclude_fstype, "iso9660") == true, "is in list now"); + + ok(np_find_name(exclude_filesystem, "iso9660") == false, "Make sure no clashing in variables"); + + /* + for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) { + printf("Name: %s\n", temp_name->name); + } + */ + + me = (struct mount_entry *)malloc(sizeof *me); + me->me_devname = strdup("/dev/c0t0d0s0"); + me->me_mountdir = strdup("/"); + *mtail = me; + mtail = &me->me_next; + + me = (struct mount_entry *)malloc(sizeof *me); + me->me_devname = strdup("/dev/c1t0d1s0"); + me->me_mountdir = strdup("/var"); + *mtail = me; + mtail = &me->me_next; + + me = (struct mount_entry *)malloc(sizeof *me); + me->me_devname = strdup("/dev/c2t0d0s0"); + me->me_mountdir = strdup("/home"); + *mtail = me; + mtail = &me->me_next; + + np_test_mount_entry_regex(dummy_mount_list, strdup("/"), cflags, 3, strdup("a")); + np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"), cflags, 3, strdup("regex on dev names:")); + np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"), cflags, 0, strdup("regex on non existent dev/path:")); + np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"), cflags | REG_ICASE, 0, strdup("regi on non existent dev/path:")); + np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"), cflags, 3, strdup("partial devname regex match:")); + np_test_mount_entry_regex(dummy_mount_list, strdup("c0t0"), cflags, 1, strdup("partial devname regex match:")); + np_test_mount_entry_regex(dummy_mount_list, strdup("C0t0"), cflags | REG_ICASE, 1, strdup("partial devname regi match:")); + np_test_mount_entry_regex(dummy_mount_list, strdup("home"), cflags, 1, strdup("partial pathname regex match:")); + np_test_mount_entry_regex(dummy_mount_list, strdup("hOme"), cflags | REG_ICASE, 1, strdup("partial pathname regi match:")); + np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"), cflags, 2, strdup("grouped regex pathname match:")); + np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"), cflags | REG_ICASE, 2, strdup("grouped regi pathname match:")); + + np_add_parameter(&paths, "/home/groups"); + np_add_parameter(&paths, "/var"); + np_add_parameter(&paths, "/tmp"); + np_add_parameter(&paths, "/home/tonvoon"); + np_add_parameter(&paths, "/dev/c2t0d0s0"); + + np_set_best_match(paths, dummy_mount_list, false); + for (p = paths; p; p = p->name_next) { + struct mount_entry *temp_me; + temp_me = p->best_match; + if (!strcmp(p->name, "/home/groups")) { + ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home"); + } else if (!strcmp(p->name, "/var")) { + ok(temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var"); + } else if (!strcmp(p->name, "/tmp")) { + ok(temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /"); + } else if (!strcmp(p->name, "/home/tonvoon")) { + ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home"); + } else if (!strcmp(p->name, "/dev/c2t0d0s0")) { + ok(temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0"); + } + } + + paths = NULL; /* Bad boy - should free, but this is a test suite */ + np_add_parameter(&paths, "/home/groups"); + np_add_parameter(&paths, "/var"); + np_add_parameter(&paths, "/tmp"); + np_add_parameter(&paths, "/home/tonvoon"); + np_add_parameter(&paths, "/home"); + + np_set_best_match(paths, dummy_mount_list, true); + for (p = paths; p; p = p->name_next) { + if (!strcmp(p->name, "/home/groups")) { + ok(!p->best_match, "/home/groups correctly not found"); + } else if (!strcmp(p->name, "/var")) { + ok(p->best_match, "/var found"); + } else if (!strcmp(p->name, "/tmp")) { + ok(!p->best_match, "/tmp correctly not found"); + } else if (!strcmp(p->name, "/home/tonvoon")) { + ok(!p->best_match, "/home/tonvoon not found"); + } else if (!strcmp(p->name, "/home")) { + ok(p->best_match, "/home found"); + } + } + + /* test deleting first element in paths */ + paths = np_del_parameter(paths, NULL); + for (p = paths; p; p = p->name_next) { + if (!strcmp(p->name, "/home/groups")) + found = 1; + } + ok(found == 0, "first element successfully deleted"); + found = 0; + + p = paths; + while (p) { + if (!strcmp(p->name, "/tmp")) + p = np_del_parameter(p, prev); + else { + prev = p; + p = p->name_next; + } + } + + for (p = paths; p; p = p->name_next) { + if (!strcmp(p->name, "/tmp")) + found = 1; + if (p->name_next) + prev = p; + else + last = p; + } + ok(found == 0, "/tmp element successfully deleted"); + + p = np_del_parameter(last, prev); + for (p = paths; p; p = p->name_next) { + if (!strcmp(p->name, "/home")) + found = 1; + last = p; + count++; + } + ok(found == 0, "last (/home) element successfully deleted"); + ok(count == 2, "two elements remaining"); + + return exit_status(); +} + +void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc) { + int matches = 0; + regex_t re; + struct mount_entry *me; + if (regcomp(&re, regstr, cflags) == 0) { + for (me = dummy_mount_list; me; me = me->me_next) { + if (np_regex_match_mount_entry(me, &re)) + matches++; + } + ok(matches == expect, "%s '%s' matched %i/3 entries. ok: %i/3", desc, regstr, expect, matches); + + } else + ok(false, "regex '%s' not compilable", regstr); +} diff --git a/plugins/tests/test_check_disk.t b/plugins/tests/test_check_disk.t new file mode 100755 index 00000000..56354650 --- /dev/null +++ b/plugins/tests/test_check_disk.t @@ -0,0 +1,6 @@ +#!/usr/bin/perl +use Test::More; +if (! -e "./test_check_disk") { + plan skip_all => "./test_check_disk not compiled - please enable libtap library to test"; +} +exec "./test_check_disk"; -- cgit v1.2.3-74-g34f1