diff options
Diffstat (limited to 'plugins/check_time.c')
-rw-r--r-- | plugins/check_time.c | 198 |
1 files changed, 111 insertions, 87 deletions
diff --git a/plugins/check_time.c b/plugins/check_time.c index d1f50683..fc9ba3f9 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" | ||
31 | const char *progname = "check_time"; | 32 | const char *progname = "check_time"; |
32 | const char *copyright = "1999-2024"; | 33 | const char *copyright = "1999-2024"; |
33 | const char *email = "devel@monitoring-plugins.org"; | 34 | const 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" | |
39 | enum { | ||
40 | TIME_PORT = 37 | ||
41 | }; | ||
42 | 40 | ||
43 | #define UNIX_EPOCH 2208988800UL | 41 | #define UNIX_EPOCH 2208988800UL |
44 | 42 | ||
45 | static uint32_t raw_server_time; | 43 | typedef struct { |
46 | static unsigned long server_time, diff_time; | 44 | int errorcode; |
47 | static int warning_time = 0; | 45 | check_time_config config; |
48 | static bool check_warning_time = false; | 46 | } check_time_config_wrapper; |
49 | static int critical_time = 0; | 47 | static check_time_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
50 | static bool check_critical_time = false; | ||
51 | static unsigned long warning_diff = 0; | ||
52 | static bool check_warning_diff = false; | ||
53 | static unsigned long critical_diff = 0; | ||
54 | static bool check_critical_diff = false; | ||
55 | static int server_port = TIME_PORT; | ||
56 | static char *server_address = NULL; | ||
57 | static bool use_udp = false; | ||
58 | |||
59 | static int process_arguments(int, char **); | ||
60 | static void print_help(void); | 48 | static void print_help(void); |
61 | void print_usage(void); | 49 | void print_usage(void); |
62 | 50 | ||
@@ -68,8 +56,12 @@ 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")); |
62 | } | ||
63 | |||
64 | const check_time_config config = tmp_config.config; | ||
73 | 65 | ||
74 | /* initialize alarm signal handling */ | 66 | /* initialize alarm signal handling */ |
75 | signal(SIGALRM, socket_timeout_alarm_handler); | 67 | signal(SIGALRM, socket_timeout_alarm_handler); |
@@ -79,37 +71,42 @@ int main(int argc, char **argv) { | |||
79 | time(&start_time); | 71 | time(&start_time); |
80 | 72 | ||
81 | int socket; | 73 | int socket; |
82 | int result = STATE_UNKNOWN; | 74 | mp_state_enum result = STATE_UNKNOWN; |
83 | /* try to connect to the host at the given port number */ | 75 | /* try to connect to the host at the given port number */ |
84 | if (use_udp) { | 76 | if (config.use_udp) { |
85 | result = my_udp_connect(server_address, server_port, &socket); | 77 | result = my_udp_connect(config.server_address, config.server_port, &socket); |
86 | } else { | 78 | } else { |
87 | result = my_tcp_connect(server_address, server_port, &socket); | 79 | result = my_tcp_connect(config.server_address, config.server_port, &socket); |
88 | } | 80 | } |
89 | 81 | ||
90 | if (result != STATE_OK) { | 82 | if (result != STATE_OK) { |
91 | if (check_critical_time) | 83 | if (config.check_critical_time) { |
92 | result = STATE_CRITICAL; | 84 | result = STATE_CRITICAL; |
93 | else if (check_warning_time) | 85 | } else if (config.check_warning_time) { |
94 | result = STATE_WARNING; | 86 | result = STATE_WARNING; |
95 | else | 87 | } else { |
96 | result = STATE_UNKNOWN; | 88 | result = STATE_UNKNOWN; |
97 | die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), server_address, server_port); | 89 | } |
90 | die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), | ||
91 | config.server_address, config.server_port); | ||
98 | } | 92 | } |
99 | 93 | ||
100 | if (use_udp) { | 94 | if (config.use_udp) { |
101 | if (send(socket, "", 0, 0) < 0) { | 95 | if (send(socket, "", 0, 0) < 0) { |
102 | if (check_critical_time) | 96 | if (config.check_critical_time) { |
103 | result = STATE_CRITICAL; | 97 | result = STATE_CRITICAL; |
104 | else if (check_warning_time) | 98 | } else if (config.check_warning_time) { |
105 | result = STATE_WARNING; | 99 | result = STATE_WARNING; |
106 | else | 100 | } else { |
107 | result = STATE_UNKNOWN; | 101 | result = STATE_UNKNOWN; |
108 | die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), server_address, server_port); | 102 | } |
103 | die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), | ||
104 | config.server_address, config.server_port); | ||
109 | } | 105 | } |
110 | } | 106 | } |
111 | 107 | ||
112 | /* watch for the connection string */ | 108 | /* watch for the connection string */ |
109 | uint32_t raw_server_time; | ||
113 | result = recv(socket, (void *)&raw_server_time, sizeof(raw_server_time), 0); | 110 | result = recv(socket, (void *)&raw_server_time, sizeof(raw_server_time), 0); |
114 | 111 | ||
115 | /* close the connection */ | 112 | /* close the connection */ |
@@ -121,48 +118,59 @@ int main(int argc, char **argv) { | |||
121 | 118 | ||
122 | /* return a WARNING status if we couldn't read any data */ | 119 | /* return a WARNING status if we couldn't read any data */ |
123 | if (result <= 0) { | 120 | if (result <= 0) { |
124 | if (check_critical_time) | 121 | if (config.check_critical_time) { |
125 | result = STATE_CRITICAL; | 122 | result = STATE_CRITICAL; |
126 | else if (check_warning_time) | 123 | } else if (config.check_warning_time) { |
127 | result = STATE_WARNING; | 124 | result = STATE_WARNING; |
128 | else | 125 | } else { |
129 | result = STATE_UNKNOWN; | 126 | result = STATE_UNKNOWN; |
130 | die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), server_address, server_port); | 127 | } |
128 | die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), | ||
129 | config.server_address, config.server_port); | ||
131 | } | 130 | } |
132 | 131 | ||
133 | result = STATE_OK; | 132 | result = STATE_OK; |
134 | 133 | ||
135 | time_t conntime = (end_time - start_time); | 134 | time_t conntime = (end_time - start_time); |
136 | if (check_critical_time && conntime > critical_time) | 135 | if (config.check_critical_time && conntime > config.critical_time) { |
137 | result = STATE_CRITICAL; | 136 | result = STATE_CRITICAL; |
138 | else if (check_warning_time && conntime > warning_time) | 137 | } else if (config.check_warning_time && conntime > config.warning_time) { |
139 | result = STATE_WARNING; | 138 | result = STATE_WARNING; |
139 | } | ||
140 | 140 | ||
141 | if (result != STATE_OK) | 141 | if (result != STATE_OK) { |
142 | die(result, _("TIME %s - %d second response time|%s\n"), state_text(result), (int)conntime, | 142 | die(result, _("TIME %s - %d second response time|%s\n"), state_text(result), (int)conntime, |
143 | perfdata("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, true, 0, | 143 | perfdata("time", (long)conntime, "s", config.check_warning_time, |
144 | false, 0)); | 144 | (long)config.warning_time, config.check_critical_time, |
145 | (long)config.critical_time, true, 0, false, 0)); | ||
146 | } | ||
145 | 147 | ||
148 | unsigned long server_time; | ||
149 | unsigned long diff_time; | ||
146 | server_time = ntohl(raw_server_time) - UNIX_EPOCH; | 150 | server_time = ntohl(raw_server_time) - UNIX_EPOCH; |
147 | if (server_time > (unsigned long)end_time) | 151 | if (server_time > (unsigned long)end_time) { |
148 | diff_time = server_time - (unsigned long)end_time; | 152 | diff_time = server_time - (unsigned long)end_time; |
149 | else | 153 | } else { |
150 | diff_time = (unsigned long)end_time - server_time; | 154 | diff_time = (unsigned long)end_time - server_time; |
155 | } | ||
151 | 156 | ||
152 | if (check_critical_diff && diff_time > critical_diff) | 157 | if (config.check_critical_diff && diff_time > config.critical_diff) { |
153 | result = STATE_CRITICAL; | 158 | result = STATE_CRITICAL; |
154 | else if (check_warning_diff && diff_time > warning_diff) | 159 | } else if (config.check_warning_diff && diff_time > config.warning_diff) { |
155 | result = STATE_WARNING; | 160 | result = STATE_WARNING; |
161 | } | ||
156 | 162 | ||
157 | printf(_("TIME %s - %lu second time difference|%s %s\n"), state_text(result), diff_time, | 163 | printf(_("TIME %s - %lu second time difference|%s %s\n"), state_text(result), diff_time, |
158 | perfdata("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, true, 0, | 164 | perfdata("time", (long)conntime, "s", config.check_warning_time, |
159 | false, 0), | 165 | (long)config.warning_time, config.check_critical_time, |
160 | perfdata("offset", diff_time, "s", check_warning_diff, warning_diff, check_critical_diff, critical_diff, true, 0, false, 0)); | 166 | (long)config.critical_time, true, 0, false, 0), |
167 | perfdata("offset", diff_time, "s", config.check_warning_diff, config.warning_diff, | ||
168 | config.check_critical_diff, config.critical_diff, true, 0, false, 0)); | ||
161 | return result; | 169 | return result; |
162 | } | 170 | } |
163 | 171 | ||
164 | /* process command-line arguments */ | 172 | /* process command-line arguments */ |
165 | int process_arguments(int argc, char **argv) { | 173 | check_time_config_wrapper process_arguments(int argc, char **argv) { |
166 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, | 174 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
167 | {"warning-variance", required_argument, 0, 'w'}, | 175 | {"warning-variance", required_argument, 0, 'w'}, |
168 | {"critical-variance", required_argument, 0, 'c'}, | 176 | {"critical-variance", required_argument, 0, 'c'}, |
@@ -175,29 +183,37 @@ int process_arguments(int argc, char **argv) { | |||
175 | {"help", no_argument, 0, 'h'}, | 183 | {"help", no_argument, 0, 'h'}, |
176 | {0, 0, 0, 0}}; | 184 | {0, 0, 0, 0}}; |
177 | 185 | ||
178 | if (argc < 2) | 186 | if (argc < 2) { |
179 | usage("\n"); | 187 | usage("\n"); |
188 | } | ||
180 | 189 | ||
181 | for (int i = 1; i < argc; i++) { | 190 | for (int i = 1; i < argc; i++) { |
182 | if (strcmp("-to", argv[i]) == 0) | 191 | if (strcmp("-to", argv[i]) == 0) { |
183 | strcpy(argv[i], "-t"); | 192 | strcpy(argv[i], "-t"); |
184 | else if (strcmp("-wd", argv[i]) == 0) | 193 | } else if (strcmp("-wd", argv[i]) == 0) { |
185 | strcpy(argv[i], "-w"); | 194 | strcpy(argv[i], "-w"); |
186 | else if (strcmp("-cd", argv[i]) == 0) | 195 | } else if (strcmp("-cd", argv[i]) == 0) { |
187 | strcpy(argv[i], "-c"); | 196 | strcpy(argv[i], "-c"); |
188 | else if (strcmp("-wt", argv[i]) == 0) | 197 | } else if (strcmp("-wt", argv[i]) == 0) { |
189 | strcpy(argv[i], "-W"); | 198 | strcpy(argv[i], "-W"); |
190 | else if (strcmp("-ct", argv[i]) == 0) | 199 | } else if (strcmp("-ct", argv[i]) == 0) { |
191 | strcpy(argv[i], "-C"); | 200 | strcpy(argv[i], "-C"); |
201 | } | ||
192 | } | 202 | } |
193 | 203 | ||
204 | check_time_config_wrapper result = { | ||
205 | .errorcode = OK, | ||
206 | .config = check_time_config_init(), | ||
207 | }; | ||
208 | |||
194 | int option_char; | 209 | int option_char; |
195 | while (true) { | 210 | while (true) { |
196 | int option = 0; | 211 | int option = 0; |
197 | option_char = getopt_long(argc, argv, "hVH:w:c:W:C:p:t:u", longopts, &option); | 212 | option_char = getopt_long(argc, argv, "hVH:w:c:W:C:p:t:u", longopts, &option); |
198 | 213 | ||
199 | if (option_char == -1 || option_char == EOF) | 214 | if (option_char == -1 || option_char == EOF) { |
200 | break; | 215 | break; |
216 | } | ||
201 | 217 | ||
202 | switch (option_char) { | 218 | switch (option_char) { |
203 | case '?': /* print short usage statement if args not parsable */ | 219 | case '?': /* print short usage statement if args not parsable */ |
@@ -209,18 +225,20 @@ int process_arguments(int argc, char **argv) { | |||
209 | print_revision(progname, NP_VERSION); | 225 | print_revision(progname, NP_VERSION); |
210 | exit(STATE_UNKNOWN); | 226 | exit(STATE_UNKNOWN); |
211 | case 'H': /* hostname */ | 227 | case 'H': /* hostname */ |
212 | if (!is_host(optarg)) | 228 | if (!is_host(optarg)) { |
213 | usage2(_("Invalid hostname/address"), optarg); | 229 | usage2(_("Invalid hostname/address"), optarg); |
214 | server_address = optarg; | 230 | } |
231 | result.config.server_address = optarg; | ||
215 | break; | 232 | break; |
216 | case 'w': /* warning-variance */ | 233 | case 'w': /* warning-variance */ |
217 | if (is_intnonneg(optarg)) { | 234 | if (is_intnonneg(optarg)) { |
218 | warning_diff = strtoul(optarg, NULL, 10); | 235 | result.config.warning_diff = strtoul(optarg, NULL, 10); |
219 | check_warning_diff = true; | 236 | result.config.check_warning_diff = true; |
220 | } else if (strspn(optarg, "0123456789:,") > 0) { | 237 | } else if (strspn(optarg, "0123456789:,") > 0) { |
221 | if (sscanf(optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) { | 238 | if (sscanf(optarg, "%lu%*[:,]%d", &result.config.warning_diff, |
222 | check_warning_diff = true; | 239 | &result.config.warning_time) == 2) { |
223 | check_warning_time = true; | 240 | result.config.check_warning_diff = true; |
241 | result.config.check_warning_time = true; | ||
224 | } else { | 242 | } else { |
225 | usage4(_("Warning thresholds must be a positive integer")); | 243 | usage4(_("Warning thresholds must be a positive integer")); |
226 | } | 244 | } |
@@ -230,12 +248,13 @@ int process_arguments(int argc, char **argv) { | |||
230 | break; | 248 | break; |
231 | case 'c': /* critical-variance */ | 249 | case 'c': /* critical-variance */ |
232 | if (is_intnonneg(optarg)) { | 250 | if (is_intnonneg(optarg)) { |
233 | critical_diff = strtoul(optarg, NULL, 10); | 251 | result.config.critical_diff = strtoul(optarg, NULL, 10); |
234 | check_critical_diff = true; | 252 | result.config.check_critical_diff = true; |
235 | } else if (strspn(optarg, "0123456789:,") > 0) { | 253 | } else if (strspn(optarg, "0123456789:,") > 0) { |
236 | if (sscanf(optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) == 2) { | 254 | if (sscanf(optarg, "%lu%*[:,]%d", &result.config.critical_diff, |
237 | check_critical_diff = true; | 255 | &result.config.critical_time) == 2) { |
238 | check_critical_time = true; | 256 | result.config.check_critical_diff = true; |
257 | result.config.check_critical_time = true; | ||
239 | } else { | 258 | } else { |
240 | usage4(_("Critical thresholds must be a positive integer")); | 259 | usage4(_("Critical thresholds must be a positive integer")); |
241 | } | 260 | } |
@@ -244,48 +263,53 @@ int process_arguments(int argc, char **argv) { | |||
244 | } | 263 | } |
245 | break; | 264 | break; |
246 | case 'W': /* warning-connect */ | 265 | case 'W': /* warning-connect */ |
247 | if (!is_intnonneg(optarg)) | 266 | if (!is_intnonneg(optarg)) { |
248 | usage4(_("Warning threshold must be a positive integer")); | 267 | usage4(_("Warning threshold must be a positive integer")); |
249 | else | 268 | } else { |
250 | warning_time = atoi(optarg); | 269 | result.config.warning_time = atoi(optarg); |
251 | check_warning_time = true; | 270 | } |
271 | result.config.check_warning_time = true; | ||
252 | break; | 272 | break; |
253 | case 'C': /* critical-connect */ | 273 | case 'C': /* critical-connect */ |
254 | if (!is_intnonneg(optarg)) | 274 | if (!is_intnonneg(optarg)) { |
255 | usage4(_("Critical threshold must be a positive integer")); | 275 | usage4(_("Critical threshold must be a positive integer")); |
256 | else | 276 | } else { |
257 | critical_time = atoi(optarg); | 277 | result.config.critical_time = atoi(optarg); |
258 | check_critical_time = true; | 278 | } |
279 | result.config.check_critical_time = true; | ||
259 | break; | 280 | break; |
260 | case 'p': /* port */ | 281 | case 'p': /* port */ |
261 | if (!is_intnonneg(optarg)) | 282 | if (!is_intnonneg(optarg)) { |
262 | usage4(_("Port must be a positive integer")); | 283 | usage4(_("Port must be a positive integer")); |
263 | else | 284 | } else { |
264 | server_port = atoi(optarg); | 285 | result.config.server_port = atoi(optarg); |
286 | } | ||
265 | break; | 287 | break; |
266 | case 't': /* timeout */ | 288 | case 't': /* timeout */ |
267 | if (!is_intnonneg(optarg)) | 289 | if (!is_intnonneg(optarg)) { |
268 | usage2(_("Timeout interval must be a positive integer"), optarg); | 290 | usage2(_("Timeout interval must be a positive integer"), optarg); |
269 | else | 291 | } else { |
270 | socket_timeout = atoi(optarg); | 292 | socket_timeout = atoi(optarg); |
293 | } | ||
271 | break; | 294 | break; |
272 | case 'u': /* udp */ | 295 | case 'u': /* udp */ |
273 | use_udp = true; | 296 | result.config.use_udp = true; |
274 | } | 297 | } |
275 | } | 298 | } |
276 | 299 | ||
277 | option_char = optind; | 300 | option_char = optind; |
278 | if (server_address == NULL) { | 301 | if (result.config.server_address == NULL) { |
279 | if (argc > option_char) { | 302 | if (argc > option_char) { |
280 | if (!is_host(argv[option_char])) | 303 | if (!is_host(argv[option_char])) { |
281 | usage2(_("Invalid hostname/address"), optarg); | 304 | usage2(_("Invalid hostname/address"), optarg); |
282 | server_address = argv[option_char]; | 305 | } |
306 | result.config.server_address = argv[option_char]; | ||
283 | } else { | 307 | } else { |
284 | usage4(_("Hostname was not supplied")); | 308 | usage4(_("Hostname was not supplied")); |
285 | } | 309 | } |
286 | } | 310 | } |
287 | 311 | ||
288 | return OK; | 312 | return result; |
289 | } | 313 | } |
290 | 314 | ||
291 | void print_help(void) { | 315 | void print_help(void) { |