From 53e102b6ace40138df373e078916d1a345ec5aa0 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 12 Mar 2025 18:01:31 +0100 Subject: check_time: clang-format --- plugins/check_time.c | 85 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 34 deletions(-) (limited to 'plugins/check_time.c') diff --git a/plugins/check_time.c b/plugins/check_time.c index d1f50683..02b152c0 100644 --- a/plugins/check_time.c +++ b/plugins/check_time.c @@ -68,8 +68,9 @@ int main(int argc, char **argv) { /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - if (process_arguments(argc, argv) == ERROR) + if (process_arguments(argc, argv) == ERROR) { usage4(_("Could not parse arguments")); + } /* initialize alarm signal handling */ signal(SIGALRM, socket_timeout_alarm_handler); @@ -88,23 +89,25 @@ int main(int argc, char **argv) { } if (result != STATE_OK) { - if (check_critical_time) + if (check_critical_time) { result = STATE_CRITICAL; - else if (check_warning_time) + } else if (check_warning_time) { result = STATE_WARNING; - else + } else { result = STATE_UNKNOWN; + } die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), server_address, server_port); } if (use_udp) { if (send(socket, "", 0, 0) < 0) { - if (check_critical_time) + if (check_critical_time) { result = STATE_CRITICAL; - else if (check_warning_time) + } else if (check_warning_time) { result = STATE_WARNING; - else + } else { result = STATE_UNKNOWN; + } die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), server_address, server_port); } } @@ -121,38 +124,43 @@ 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 (check_critical_time) { result = STATE_CRITICAL; - else if (check_warning_time) + } else if (check_warning_time) { result = STATE_WARNING; - else + } else { result = STATE_UNKNOWN; + } die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), server_address, server_port); } result = STATE_OK; time_t conntime = (end_time - start_time); - if (check_critical_time && conntime > critical_time) + if (check_critical_time && conntime > critical_time) { result = STATE_CRITICAL; - else if (check_warning_time && conntime > warning_time) + } else if (check_warning_time && conntime > warning_time) { result = STATE_WARNING; + } - if (result != STATE_OK) + 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)); + } server_time = ntohl(raw_server_time) - UNIX_EPOCH; - if (server_time > (unsigned long)end_time) + if (server_time > (unsigned long)end_time) { diff_time = server_time - (unsigned long)end_time; - else + } else { diff_time = (unsigned long)end_time - server_time; + } - if (check_critical_diff && diff_time > critical_diff) + if (check_critical_diff && diff_time > critical_diff) { result = STATE_CRITICAL; - else if (check_warning_diff && diff_time > warning_diff) + } else if (check_warning_diff && diff_time > 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, @@ -175,20 +183,22 @@ int process_arguments(int argc, char **argv) { {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; - if (argc < 2) + if (argc < 2) { usage("\n"); + } for (int i = 1; i < argc; i++) { - if (strcmp("-to", argv[i]) == 0) + if (strcmp("-to", argv[i]) == 0) { strcpy(argv[i], "-t"); - else if (strcmp("-wd", argv[i]) == 0) + } else if (strcmp("-wd", argv[i]) == 0) { strcpy(argv[i], "-w"); - else if (strcmp("-cd", argv[i]) == 0) + } else if (strcmp("-cd", argv[i]) == 0) { strcpy(argv[i], "-c"); - else if (strcmp("-wt", argv[i]) == 0) + } else if (strcmp("-wt", argv[i]) == 0) { strcpy(argv[i], "-W"); - else if (strcmp("-ct", argv[i]) == 0) + } else if (strcmp("-ct", argv[i]) == 0) { strcpy(argv[i], "-C"); + } } int option_char; @@ -196,8 +206,9 @@ int process_arguments(int argc, char **argv) { int option = 0; option_char = getopt_long(argc, argv, "hVH:w:c:W:C:p:t:u", longopts, &option); - if (option_char == -1 || option_char == EOF) + if (option_char == -1 || option_char == EOF) { break; + } switch (option_char) { case '?': /* print short usage statement if args not parsable */ @@ -209,8 +220,9 @@ int process_arguments(int argc, char **argv) { print_revision(progname, NP_VERSION); exit(STATE_UNKNOWN); case 'H': /* hostname */ - if (!is_host(optarg)) + if (!is_host(optarg)) { usage2(_("Invalid hostname/address"), optarg); + } server_address = optarg; break; case 'w': /* warning-variance */ @@ -244,30 +256,34 @@ int process_arguments(int argc, char **argv) { } break; case 'W': /* warning-connect */ - if (!is_intnonneg(optarg)) + if (!is_intnonneg(optarg)) { usage4(_("Warning threshold must be a positive integer")); - else + } else { warning_time = atoi(optarg); + } check_warning_time = true; break; case 'C': /* critical-connect */ - if (!is_intnonneg(optarg)) + if (!is_intnonneg(optarg)) { usage4(_("Critical threshold must be a positive integer")); - else + } else { critical_time = atoi(optarg); + } check_critical_time = true; break; case 'p': /* port */ - if (!is_intnonneg(optarg)) + if (!is_intnonneg(optarg)) { usage4(_("Port must be a positive integer")); - else + } else { server_port = atoi(optarg); + } break; case 't': /* timeout */ - if (!is_intnonneg(optarg)) + if (!is_intnonneg(optarg)) { usage2(_("Timeout interval must be a positive integer"), optarg); - else + } else { socket_timeout = atoi(optarg); + } break; case 'u': /* udp */ use_udp = true; @@ -277,8 +293,9 @@ int process_arguments(int argc, char **argv) { option_char = optind; if (server_address == NULL) { if (argc > option_char) { - if (!is_host(argv[option_char])) + if (!is_host(argv[option_char])) { usage2(_("Invalid hostname/address"), optarg); + } server_address = argv[option_char]; } else { usage4(_("Hostname was not supplied")); -- 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/check_time.c') 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