summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-12 18:14:54 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-12 18:14:54 +0100
commit2d70bd3bc09ff95cd8525449925938ebf56cbfbb (patch)
tree78b601b307292aa4d4016e08cb4c739f5d2e7b79
parent53e102b6ace40138df373e078916d1a345ec5aa0 (diff)
downloadmonitoring-plugins-2d70bd3bc09ff95cd8525449925938ebf56cbfbb.tar.gz
Refactor check_time
-rw-r--r--plugins/Makefile.am1
-rw-r--r--plugins/check_time.c128
-rw-r--r--plugins/check_time.d/config.h42
3 files changed, 107 insertions, 64 deletions
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 \
54 check_hpjd.d \ 54 check_hpjd.d \
55 check_game.d \ 55 check_game.d \
56 check_radius.d \ 56 check_radius.d \
57 check_time.d \
57 check_nagios.d \ 58 check_nagios.d \
58 check_dbi.d \ 59 check_dbi.d \
59 check_real.d \ 60 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 @@
28 * 28 *
29 *****************************************************************************/ 29 *****************************************************************************/
30 30
31#include "states.h"
31const char *progname = "check_time"; 32const char *progname = "check_time";
32const char *copyright = "1999-2024"; 33const char *copyright = "1999-2024";
33const char *email = "devel@monitoring-plugins.org"; 34const char *email = "devel@monitoring-plugins.org";
@@ -35,28 +36,15 @@ const char *email = "devel@monitoring-plugins.org";
35#include "common.h" 36#include "common.h"
36#include "netutils.h" 37#include "netutils.h"
37#include "utils.h" 38#include "utils.h"
38 39#include "check_time.d/config.h"
39enum {
40 TIME_PORT = 37
41};
42 40
43#define UNIX_EPOCH 2208988800UL 41#define UNIX_EPOCH 2208988800UL
44 42
45static uint32_t raw_server_time; 43typedef struct {
46static unsigned long server_time, diff_time; 44 int errorcode;
47static int warning_time = 0; 45 check_time_config config;
48static bool check_warning_time = false; 46} check_time_config_wrapper;
49static int critical_time = 0; 47static check_time_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
50static bool check_critical_time = false;
51static unsigned long warning_diff = 0;
52static bool check_warning_diff = false;
53static unsigned long critical_diff = 0;
54static bool check_critical_diff = false;
55static int server_port = TIME_PORT;
56static char *server_address = NULL;
57static bool use_udp = false;
58
59static int process_arguments(int, char **);
60static void print_help(void); 48static void print_help(void);
61void print_usage(void); 49void print_usage(void);
62 50
@@ -68,10 +56,13 @@ int main(int argc, char **argv) {
68 /* Parse extra opts if any */ 56 /* Parse extra opts if any */
69 argv = np_extra_opts(&argc, argv, progname); 57 argv = np_extra_opts(&argc, argv, progname);
70 58
71 if (process_arguments(argc, argv) == ERROR) { 59 check_time_config_wrapper tmp_config = process_arguments(argc, argv);
60 if (tmp_config.errorcode == ERROR) {
72 usage4(_("Could not parse arguments")); 61 usage4(_("Could not parse arguments"));
73 } 62 }
74 63
64 const check_time_config config = tmp_config.config;
65
75 /* initialize alarm signal handling */ 66 /* initialize alarm signal handling */
76 signal(SIGALRM, socket_timeout_alarm_handler); 67 signal(SIGALRM, socket_timeout_alarm_handler);
77 68
@@ -80,39 +71,40 @@ int main(int argc, char **argv) {
80 time(&start_time); 71 time(&start_time);
81 72
82 int socket; 73 int socket;
83 int result = STATE_UNKNOWN; 74 mp_state_enum result = STATE_UNKNOWN;
84 /* try to connect to the host at the given port number */ 75 /* try to connect to the host at the given port number */
85 if (use_udp) { 76 if (config.use_udp) {
86 result = my_udp_connect(server_address, server_port, &socket); 77 result = my_udp_connect(config.server_address, config.server_port, &socket);
87 } else { 78 } else {
88 result = my_tcp_connect(server_address, server_port, &socket); 79 result = my_tcp_connect(config.server_address, config.server_port, &socket);
89 } 80 }
90 81
91 if (result != STATE_OK) { 82 if (result != STATE_OK) {
92 if (check_critical_time) { 83 if (config.check_critical_time) {
93 result = STATE_CRITICAL; 84 result = STATE_CRITICAL;
94 } else if (check_warning_time) { 85 } else if (config.check_warning_time) {
95 result = STATE_WARNING; 86 result = STATE_WARNING;
96 } else { 87 } else {
97 result = STATE_UNKNOWN; 88 result = STATE_UNKNOWN;
98 } 89 }
99 die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), server_address, server_port); 90 die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), config.server_address, config.server_port);
100 } 91 }
101 92
102 if (use_udp) { 93 if (config.use_udp) {
103 if (send(socket, "", 0, 0) < 0) { 94 if (send(socket, "", 0, 0) < 0) {
104 if (check_critical_time) { 95 if (config.check_critical_time) {
105 result = STATE_CRITICAL; 96 result = STATE_CRITICAL;
106 } else if (check_warning_time) { 97 } else if (config.check_warning_time) {
107 result = STATE_WARNING; 98 result = STATE_WARNING;
108 } else { 99 } else {
109 result = STATE_UNKNOWN; 100 result = STATE_UNKNOWN;
110 } 101 }
111 die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), server_address, server_port); 102 die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), config.server_address, config.server_port);
112 } 103 }
113 } 104 }
114 105
115 /* watch for the connection string */ 106 /* watch for the connection string */
107 uint32_t raw_server_time;
116 result = recv(socket, (void *)&raw_server_time, sizeof(raw_server_time), 0); 108 result = recv(socket, (void *)&raw_server_time, sizeof(raw_server_time), 0);
117 109
118 /* close the connection */ 110 /* close the connection */
@@ -124,31 +116,33 @@ int main(int argc, char **argv) {
124 116
125 /* return a WARNING status if we couldn't read any data */ 117 /* return a WARNING status if we couldn't read any data */
126 if (result <= 0) { 118 if (result <= 0) {
127 if (check_critical_time) { 119 if (config.check_critical_time) {
128 result = STATE_CRITICAL; 120 result = STATE_CRITICAL;
129 } else if (check_warning_time) { 121 } else if (config.check_warning_time) {
130 result = STATE_WARNING; 122 result = STATE_WARNING;
131 } else { 123 } else {
132 result = STATE_UNKNOWN; 124 result = STATE_UNKNOWN;
133 } 125 }
134 die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), server_address, server_port); 126 die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), config.server_address, config.server_port);
135 } 127 }
136 128
137 result = STATE_OK; 129 result = STATE_OK;
138 130
139 time_t conntime = (end_time - start_time); 131 time_t conntime = (end_time - start_time);
140 if (check_critical_time && conntime > critical_time) { 132 if (config.check_critical_time && conntime > config.critical_time) {
141 result = STATE_CRITICAL; 133 result = STATE_CRITICAL;
142 } else if (check_warning_time && conntime > warning_time) { 134 } else if (config.check_warning_time && conntime > config.warning_time) {
143 result = STATE_WARNING; 135 result = STATE_WARNING;
144 } 136 }
145 137
146 if (result != STATE_OK) { 138 if (result != STATE_OK) {
147 die(result, _("TIME %s - %d second response time|%s\n"), state_text(result), (int)conntime, 139 die(result, _("TIME %s - %d second response time|%s\n"), state_text(result), (int)conntime,
148 perfdata("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, true, 0, 140 perfdata("time", (long)conntime, "s", config.check_warning_time, (long)config.warning_time, config.check_critical_time,
149 false, 0)); 141 (long)config.critical_time, true, 0, false, 0));
150 } 142 }
151 143
144 unsigned long server_time;
145 unsigned long diff_time;
152 server_time = ntohl(raw_server_time) - UNIX_EPOCH; 146 server_time = ntohl(raw_server_time) - UNIX_EPOCH;
153 if (server_time > (unsigned long)end_time) { 147 if (server_time > (unsigned long)end_time) {
154 diff_time = server_time - (unsigned long)end_time; 148 diff_time = server_time - (unsigned long)end_time;
@@ -156,21 +150,22 @@ int main(int argc, char **argv) {
156 diff_time = (unsigned long)end_time - server_time; 150 diff_time = (unsigned long)end_time - server_time;
157 } 151 }
158 152
159 if (check_critical_diff && diff_time > critical_diff) { 153 if (config.check_critical_diff && diff_time > config.critical_diff) {
160 result = STATE_CRITICAL; 154 result = STATE_CRITICAL;
161 } else if (check_warning_diff && diff_time > warning_diff) { 155 } else if (config.check_warning_diff && diff_time > config.warning_diff) {
162 result = STATE_WARNING; 156 result = STATE_WARNING;
163 } 157 }
164 158
165 printf(_("TIME %s - %lu second time difference|%s %s\n"), state_text(result), diff_time, 159 printf(_("TIME %s - %lu second time difference|%s %s\n"), state_text(result), diff_time,
166 perfdata("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, true, 0, 160 perfdata("time", (long)conntime, "s", config.check_warning_time, (long)config.warning_time, config.check_critical_time,
167 false, 0), 161 (long)config.critical_time, true, 0, false, 0),
168 perfdata("offset", diff_time, "s", check_warning_diff, warning_diff, check_critical_diff, critical_diff, true, 0, false, 0)); 162 perfdata("offset", diff_time, "s", config.check_warning_diff, config.warning_diff, config.check_critical_diff,
163 config.critical_diff, true, 0, false, 0));
169 return result; 164 return result;
170} 165}
171 166
172/* process command-line arguments */ 167/* process command-line arguments */
173int process_arguments(int argc, char **argv) { 168check_time_config_wrapper process_arguments(int argc, char **argv) {
174 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, 169 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
175 {"warning-variance", required_argument, 0, 'w'}, 170 {"warning-variance", required_argument, 0, 'w'},
176 {"critical-variance", required_argument, 0, 'c'}, 171 {"critical-variance", required_argument, 0, 'c'},
@@ -201,6 +196,11 @@ int process_arguments(int argc, char **argv) {
201 } 196 }
202 } 197 }
203 198
199 check_time_config_wrapper result = {
200 .errorcode = OK,
201 .config = check_time_config_init(),
202 };
203
204 int option_char; 204 int option_char;
205 while (true) { 205 while (true) {
206 int option = 0; 206 int option = 0;
@@ -223,16 +223,16 @@ int process_arguments(int argc, char **argv) {
223 if (!is_host(optarg)) { 223 if (!is_host(optarg)) {
224 usage2(_("Invalid hostname/address"), optarg); 224 usage2(_("Invalid hostname/address"), optarg);
225 } 225 }
226 server_address = optarg; 226 result.config.server_address = optarg;
227 break; 227 break;
228 case 'w': /* warning-variance */ 228 case 'w': /* warning-variance */
229 if (is_intnonneg(optarg)) { 229 if (is_intnonneg(optarg)) {
230 warning_diff = strtoul(optarg, NULL, 10); 230 result.config.warning_diff = strtoul(optarg, NULL, 10);
231 check_warning_diff = true; 231 result.config.check_warning_diff = true;
232 } else if (strspn(optarg, "0123456789:,") > 0) { 232 } else if (strspn(optarg, "0123456789:,") > 0) {
233 if (sscanf(optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) { 233 if (sscanf(optarg, "%lu%*[:,]%d", &result.config.warning_diff, &result.config.warning_time) == 2) {
234 check_warning_diff = true; 234 result.config.check_warning_diff = true;
235 check_warning_time = true; 235 result.config.check_warning_time = true;
236 } else { 236 } else {
237 usage4(_("Warning thresholds must be a positive integer")); 237 usage4(_("Warning thresholds must be a positive integer"));
238 } 238 }
@@ -242,12 +242,12 @@ int process_arguments(int argc, char **argv) {
242 break; 242 break;
243 case 'c': /* critical-variance */ 243 case 'c': /* critical-variance */
244 if (is_intnonneg(optarg)) { 244 if (is_intnonneg(optarg)) {
245 critical_diff = strtoul(optarg, NULL, 10); 245 result.config.critical_diff = strtoul(optarg, NULL, 10);
246 check_critical_diff = true; 246 result.config.check_critical_diff = true;
247 } else if (strspn(optarg, "0123456789:,") > 0) { 247 } else if (strspn(optarg, "0123456789:,") > 0) {
248 if (sscanf(optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) == 2) { 248 if (sscanf(optarg, "%lu%*[:,]%d", &result.config.critical_diff, &result.config.critical_time) == 2) {
249 check_critical_diff = true; 249 result.config.check_critical_diff = true;
250 check_critical_time = true; 250 result.config.check_critical_time = true;
251 } else { 251 } else {
252 usage4(_("Critical thresholds must be a positive integer")); 252 usage4(_("Critical thresholds must be a positive integer"));
253 } 253 }
@@ -259,23 +259,23 @@ int process_arguments(int argc, char **argv) {
259 if (!is_intnonneg(optarg)) { 259 if (!is_intnonneg(optarg)) {
260 usage4(_("Warning threshold must be a positive integer")); 260 usage4(_("Warning threshold must be a positive integer"));
261 } else { 261 } else {
262 warning_time = atoi(optarg); 262 result.config.warning_time = atoi(optarg);
263 } 263 }
264 check_warning_time = true; 264 result.config.check_warning_time = true;
265 break; 265 break;
266 case 'C': /* critical-connect */ 266 case 'C': /* critical-connect */
267 if (!is_intnonneg(optarg)) { 267 if (!is_intnonneg(optarg)) {
268 usage4(_("Critical threshold must be a positive integer")); 268 usage4(_("Critical threshold must be a positive integer"));
269 } else { 269 } else {
270 critical_time = atoi(optarg); 270 result.config.critical_time = atoi(optarg);
271 } 271 }
272 check_critical_time = true; 272 result.config.check_critical_time = true;
273 break; 273 break;
274 case 'p': /* port */ 274 case 'p': /* port */
275 if (!is_intnonneg(optarg)) { 275 if (!is_intnonneg(optarg)) {
276 usage4(_("Port must be a positive integer")); 276 usage4(_("Port must be a positive integer"));
277 } else { 277 } else {
278 server_port = atoi(optarg); 278 result.config.server_port = atoi(optarg);
279 } 279 }
280 break; 280 break;
281 case 't': /* timeout */ 281 case 't': /* timeout */
@@ -286,23 +286,23 @@ int process_arguments(int argc, char **argv) {
286 } 286 }
287 break; 287 break;
288 case 'u': /* udp */ 288 case 'u': /* udp */
289 use_udp = true; 289 result.config.use_udp = true;
290 } 290 }
291 } 291 }
292 292
293 option_char = optind; 293 option_char = optind;
294 if (server_address == NULL) { 294 if (result.config.server_address == NULL) {
295 if (argc > option_char) { 295 if (argc > option_char) {
296 if (!is_host(argv[option_char])) { 296 if (!is_host(argv[option_char])) {
297 usage2(_("Invalid hostname/address"), optarg); 297 usage2(_("Invalid hostname/address"), optarg);
298 } 298 }
299 server_address = argv[option_char]; 299 result.config.server_address = argv[option_char];
300 } else { 300 } else {
301 usage4(_("Hostname was not supplied")); 301 usage4(_("Hostname was not supplied"));
302 } 302 }
303 } 303 }
304 304
305 return OK; 305 return result;
306} 306}
307 307
308void print_help(void) { 308void 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 @@
1#pragma once
2
3#include "../../config.h"
4#include <stddef.h>
5
6enum {
7 TIME_PORT = 37
8};
9
10typedef struct {
11 char *server_address;
12 int server_port;
13 bool use_udp;
14
15 int warning_time;
16 bool check_warning_time;
17 int critical_time;
18 bool check_critical_time;
19 unsigned long warning_diff;
20 bool check_warning_diff;
21 unsigned long critical_diff;
22 bool check_critical_diff;
23} check_time_config;
24
25check_time_config check_time_config_init() {
26 check_time_config tmp = {
27 .server_address = NULL,
28 .server_port = TIME_PORT,
29 .use_udp = false,
30
31 .warning_time = 0,
32 .check_warning_time = false,
33 .critical_time = 0,
34 .check_critical_time = false,
35
36 .warning_diff = 0,
37 .check_warning_diff = false,
38 .critical_diff = 0,
39 .check_critical_diff = false,
40 };
41 return tmp;
42}