summaryrefslogtreecommitdiffstats
path: root/plugins/check_time.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_time.c')
-rw-r--r--plugins/check_time.c431
1 files changed, 194 insertions, 237 deletions
diff --git a/plugins/check_time.c b/plugins/check_time.c
index f50ea42..d1f5068 100644
--- a/plugins/check_time.c
+++ b/plugins/check_time.c
@@ -1,35 +1,35 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring check_time plugin 3 * Monitoring check_time plugin
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 1999-2007 Monitoring Plugins Development Team 6 * Copyright (c) 1999-2024 Monitoring Plugins Development Team
7* 7 *
8* Description: 8 * Description:
9* 9 *
10* This file contains the check_time plugin 10 * This file contains the check_time plugin
11* 11 *
12* This plugin will check the time difference with the specified host. 12 * This plugin will check the time difference with the specified host.
13* 13 *
14* 14 *
15* This program is free software: you can redistribute it and/or modify 15 * This program is free software: you can redistribute it and/or modify
16* it under the terms of the GNU General Public License as published by 16 * it under the terms of the GNU General Public License as published by
17* the Free Software Foundation, either version 3 of the License, or 17 * the Free Software Foundation, either version 3 of the License, or
18* (at your option) any later version. 18 * (at your option) any later version.
19* 19 *
20* This program is distributed in the hope that it will be useful, 20 * This program is distributed in the hope that it will be useful,
21* but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23* GNU General Public License for more details. 23 * GNU General Public License for more details.
24* 24 *
25* You should have received a copy of the GNU General Public License 25 * You should have received a copy of the GNU General Public License
26* along with this program. If not, see <http://www.gnu.org/licenses/>. 26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27* 27 *
28* 28 *
29*****************************************************************************/ 29 *****************************************************************************/
30 30
31const char *progname = "check_time"; 31const char *progname = "check_time";
32const char *copyright = "1999-2007"; 32const char *copyright = "1999-2024";
33const char *email = "devel@monitoring-plugins.org"; 33const char *email = "devel@monitoring-plugins.org";
34 34
35#include "common.h" 35#include "common.h"
@@ -40,55 +40,51 @@ enum {
40 TIME_PORT = 37 40 TIME_PORT = 37
41}; 41};
42 42
43#define UNIX_EPOCH 2208988800UL 43#define UNIX_EPOCH 2208988800UL
44 44
45uint32_t raw_server_time; 45static uint32_t raw_server_time;
46unsigned long server_time, diff_time; 46static unsigned long server_time, diff_time;
47int warning_time = 0; 47static int warning_time = 0;
48bool check_warning_time = false; 48static bool check_warning_time = false;
49int critical_time = 0; 49static int critical_time = 0;
50bool check_critical_time = false; 50static bool check_critical_time = false;
51unsigned long warning_diff = 0; 51static unsigned long warning_diff = 0;
52bool check_warning_diff = false; 52static bool check_warning_diff = false;
53unsigned long critical_diff = 0; 53static unsigned long critical_diff = 0;
54bool check_critical_diff = false; 54static bool check_critical_diff = false;
55int server_port = TIME_PORT; 55static int server_port = TIME_PORT;
56char *server_address = NULL; 56static char *server_address = NULL;
57bool use_udp = false; 57static bool use_udp = false;
58 58
59int process_arguments (int, char **); 59static int process_arguments(int, char **);
60void print_help (void); 60static void print_help(void);
61void print_usage (void); 61void print_usage(void);
62 62
63int 63int main(int argc, char **argv) {
64main (int argc, char **argv) 64 setlocale(LC_ALL, "");
65{ 65 bindtextdomain(PACKAGE, LOCALEDIR);
66 int sd; 66 textdomain(PACKAGE);
67 int result = STATE_UNKNOWN;
68 time_t conntime;
69
70 setlocale (LC_ALL, "");
71 bindtextdomain (PACKAGE, LOCALEDIR);
72 textdomain (PACKAGE);
73 67
74 /* Parse extra opts if any */ 68 /* Parse extra opts if any */
75 argv=np_extra_opts (&argc, argv, progname); 69 argv = np_extra_opts(&argc, argv, progname);
76 70
77 if (process_arguments (argc, argv) == ERROR) 71 if (process_arguments(argc, argv) == ERROR)
78 usage4 (_("Could not parse arguments")); 72 usage4(_("Could not parse arguments"));
79 73
80 /* initialize alarm signal handling */ 74 /* initialize alarm signal handling */
81 signal (SIGALRM, socket_timeout_alarm_handler); 75 signal(SIGALRM, socket_timeout_alarm_handler);
82 76
83 /* set socket timeout */ 77 /* set socket timeout */
84 alarm (socket_timeout); 78 alarm(socket_timeout);
85 time (&start_time); 79 time(&start_time);
86 80
81 int socket;
82 int result = STATE_UNKNOWN;
87 /* try to connect to the host at the given port number */ 83 /* try to connect to the host at the given port number */
88 if (use_udp) { 84 if (use_udp) {
89 result = my_udp_connect (server_address, server_port, &sd); 85 result = my_udp_connect(server_address, server_port, &socket);
90 } else { 86 } else {
91 result = my_tcp_connect (server_address, server_port, &sd); 87 result = my_tcp_connect(server_address, server_port, &socket);
92 } 88 }
93 89
94 if (result != STATE_OK) { 90 if (result != STATE_OK) {
@@ -98,34 +94,30 @@ main (int argc, char **argv)
98 result = STATE_WARNING; 94 result = STATE_WARNING;
99 else 95 else
100 result = STATE_UNKNOWN; 96 result = STATE_UNKNOWN;
101 die (result, 97 die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), server_address, server_port);
102 _("TIME UNKNOWN - could not connect to server %s, port %d\n"),
103 server_address, server_port);
104 } 98 }
105 99
106 if (use_udp) { 100 if (use_udp) {
107 if (send (sd, "", 0, 0) < 0) { 101 if (send(socket, "", 0, 0) < 0) {
108 if (check_critical_time) 102 if (check_critical_time)
109 result = STATE_CRITICAL; 103 result = STATE_CRITICAL;
110 else if (check_warning_time) 104 else if (check_warning_time)
111 result = STATE_WARNING; 105 result = STATE_WARNING;
112 else 106 else
113 result = STATE_UNKNOWN; 107 result = STATE_UNKNOWN;
114 die (result, 108 die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), server_address, server_port);
115 _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"),
116 server_address, server_port);
117 } 109 }
118 } 110 }
119 111
120 /* watch for the connection string */ 112 /* watch for the connection string */
121 result = recv (sd, (void *)&raw_server_time, sizeof (raw_server_time), 0); 113 result = recv(socket, (void *)&raw_server_time, sizeof(raw_server_time), 0);
122 114
123 /* close the connection */ 115 /* close the connection */
124 close (sd); 116 close(socket);
125 117
126 /* reset the alarm */ 118 /* reset the alarm */
127 time (&end_time); 119 time(&end_time);
128 alarm (0); 120 alarm(0);
129 121
130 /* return a WARNING status if we couldn't read any data */ 122 /* return a WARNING status if we couldn't read any data */
131 if (result <= 0) { 123 if (result <= 0) {
@@ -135,240 +127,205 @@ main (int argc, char **argv)
135 result = STATE_WARNING; 127 result = STATE_WARNING;
136 else 128 else
137 result = STATE_UNKNOWN; 129 result = STATE_UNKNOWN;
138 die (result, 130 die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), server_address, server_port);
139 _("TIME UNKNOWN - no data received from server %s, port %d\n"),
140 server_address, server_port);
141 } 131 }
142 132
143 result = STATE_OK; 133 result = STATE_OK;
144 134
145 conntime = (end_time - start_time); 135 time_t conntime = (end_time - start_time);
146 if (check_critical_time&& conntime > critical_time) 136 if (check_critical_time && conntime > critical_time)
147 result = STATE_CRITICAL; 137 result = STATE_CRITICAL;
148 else if (check_warning_time && conntime > warning_time) 138 else if (check_warning_time && conntime > warning_time)
149 result = STATE_WARNING; 139 result = STATE_WARNING;
150 140
151 if (result != STATE_OK) 141 if (result != STATE_OK)
152 die (result, _("TIME %s - %d second response time|%s\n"), 142 die(result, _("TIME %s - %d second response time|%s\n"), state_text(result), (int)conntime,
153 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,
154 perfdata ("time", (long)conntime, "s", 144 false, 0));
155 check_warning_time, (long)warning_time, 145
156 check_critical_time, (long)critical_time, 146 server_time = ntohl(raw_server_time) - UNIX_EPOCH;
157 true, 0, false, 0));
158
159 server_time = ntohl (raw_server_time) - UNIX_EPOCH;
160 if (server_time > (unsigned long)end_time) 147 if (server_time > (unsigned long)end_time)
161 diff_time = server_time - (unsigned long)end_time; 148 diff_time = server_time - (unsigned long)end_time;
162 else 149 else
163 diff_time = (unsigned long)end_time - server_time; 150 diff_time = (unsigned long)end_time - server_time;
164 151
165 if (check_critical_diff&& diff_time > critical_diff) 152 if (check_critical_diff && diff_time > critical_diff)
166 result = STATE_CRITICAL; 153 result = STATE_CRITICAL;
167 else if (check_warning_diff&& diff_time > warning_diff) 154 else if (check_warning_diff && diff_time > warning_diff)
168 result = STATE_WARNING; 155 result = STATE_WARNING;
169 156
170 printf (_("TIME %s - %lu second time difference|%s %s\n"), 157 printf(_("TIME %s - %lu second time difference|%s %s\n"), state_text(result), diff_time,
171 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,
172 perfdata ("time", (long)conntime, "s", 159 false, 0),
173 check_warning_time, (long)warning_time, 160 perfdata("offset", diff_time, "s", check_warning_diff, warning_diff, check_critical_diff, critical_diff, true, 0, false, 0));
174 check_critical_time, (long)critical_time,
175 true, 0, false, 0),
176 perfdata ("offset", diff_time, "s",
177 check_warning_diff, warning_diff,
178 check_critical_diff, critical_diff,
179 true, 0, false, 0));
180 return result; 161 return result;
181} 162}
182 163
183
184
185/* process command-line arguments */ 164/* process command-line arguments */
186int 165int process_arguments(int argc, char **argv) {
187process_arguments (int argc, char **argv) 166 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
188{ 167 {"warning-variance", required_argument, 0, 'w'},
189 int c; 168 {"critical-variance", required_argument, 0, 'c'},
190 169 {"warning-connect", required_argument, 0, 'W'},
191 int option = 0; 170 {"critical-connect", required_argument, 0, 'C'},
192 static struct option longopts[] = { 171 {"port", required_argument, 0, 'p'},
193 {"hostname", required_argument, 0, 'H'}, 172 {"udp", no_argument, 0, 'u'},
194 {"warning-variance", required_argument, 0, 'w'}, 173 {"timeout", required_argument, 0, 't'},
195 {"critical-variance", required_argument, 0, 'c'}, 174 {"version", no_argument, 0, 'V'},
196 {"warning-connect", required_argument, 0, 'W'}, 175 {"help", no_argument, 0, 'h'},
197 {"critical-connect", required_argument, 0, 'C'}, 176 {0, 0, 0, 0}};
198 {"port", required_argument, 0, 'p'},
199 {"udp", no_argument, 0, 'u'},
200 {"timeout", required_argument, 0, 't'},
201 {"version", no_argument, 0, 'V'},
202 {"help", no_argument, 0, 'h'},
203 {0, 0, 0, 0}
204 };
205 177
206 if (argc < 2) 178 if (argc < 2)
207 usage ("\n"); 179 usage("\n");
208 180
209 for (c = 1; c < argc; c++) { 181 for (int i = 1; i < argc; i++) {
210 if (strcmp ("-to", argv[c]) == 0) 182 if (strcmp("-to", argv[i]) == 0)
211 strcpy (argv[c], "-t"); 183 strcpy(argv[i], "-t");
212 else if (strcmp ("-wd", argv[c]) == 0) 184 else if (strcmp("-wd", argv[i]) == 0)
213 strcpy (argv[c], "-w"); 185 strcpy(argv[i], "-w");
214 else if (strcmp ("-cd", argv[c]) == 0) 186 else if (strcmp("-cd", argv[i]) == 0)
215 strcpy (argv[c], "-c"); 187 strcpy(argv[i], "-c");
216 else if (strcmp ("-wt", argv[c]) == 0) 188 else if (strcmp("-wt", argv[i]) == 0)
217 strcpy (argv[c], "-W"); 189 strcpy(argv[i], "-W");
218 else if (strcmp ("-ct", argv[c]) == 0) 190 else if (strcmp("-ct", argv[i]) == 0)
219 strcpy (argv[c], "-C"); 191 strcpy(argv[i], "-C");
220 } 192 }
221 193
194 int option_char;
222 while (true) { 195 while (true) {
223 c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:u", longopts, 196 int option = 0;
224 &option); 197 option_char = getopt_long(argc, argv, "hVH:w:c:W:C:p:t:u", longopts, &option);
225 198
226 if (c == -1 || c == EOF) 199 if (option_char == -1 || option_char == EOF)
227 break; 200 break;
228 201
229 switch (c) { 202 switch (option_char) {
230 case '?': /* print short usage statement if args not parsable */ 203 case '?': /* print short usage statement if args not parsable */
231 usage5 (); 204 usage5();
232 case 'h': /* help */ 205 case 'h': /* help */
233 print_help (); 206 print_help();
234 exit (STATE_UNKNOWN); 207 exit(STATE_UNKNOWN);
235 case 'V': /* version */ 208 case 'V': /* version */
236 print_revision (progname, NP_VERSION); 209 print_revision(progname, NP_VERSION);
237 exit (STATE_UNKNOWN); 210 exit(STATE_UNKNOWN);
238 case 'H': /* hostname */ 211 case 'H': /* hostname */
239 if (!is_host (optarg)) 212 if (!is_host(optarg))
240 usage2 (_("Invalid hostname/address"), optarg); 213 usage2(_("Invalid hostname/address"), optarg);
241 server_address = optarg; 214 server_address = optarg;
242 break; 215 break;
243 case 'w': /* warning-variance */ 216 case 'w': /* warning-variance */
244 if (is_intnonneg (optarg)) { 217 if (is_intnonneg(optarg)) {
245 warning_diff = strtoul (optarg, NULL, 10); 218 warning_diff = strtoul(optarg, NULL, 10);
246 check_warning_diff = true; 219 check_warning_diff = true;
247 } 220 } else if (strspn(optarg, "0123456789:,") > 0) {
248 else if (strspn (optarg, "0123456789:,") > 0) { 221 if (sscanf(optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
249 if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
250 check_warning_diff = true; 222 check_warning_diff = true;
251 check_warning_time = true; 223 check_warning_time = true;
224 } else {
225 usage4(_("Warning thresholds must be a positive integer"));
252 } 226 }
253 else { 227 } else {
254 usage4 (_("Warning thresholds must be a positive integer")); 228 usage4(_("Warning threshold must be a positive integer"));
255 }
256 }
257 else {
258 usage4 (_("Warning threshold must be a positive integer"));
259 } 229 }
260 break; 230 break;
261 case 'c': /* critical-variance */ 231 case 'c': /* critical-variance */
262 if (is_intnonneg (optarg)) { 232 if (is_intnonneg(optarg)) {
263 critical_diff = strtoul (optarg, NULL, 10); 233 critical_diff = strtoul(optarg, NULL, 10);
264 check_critical_diff = true; 234 check_critical_diff = true;
265 } 235 } else if (strspn(optarg, "0123456789:,") > 0) {
266 else if (strspn (optarg, "0123456789:,") > 0) { 236 if (sscanf(optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) == 2) {
267 if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
268 2) {
269 check_critical_diff = true; 237 check_critical_diff = true;
270 check_critical_time = true; 238 check_critical_time = true;
239 } else {
240 usage4(_("Critical thresholds must be a positive integer"));
271 } 241 }
272 else { 242 } else {
273 usage4 (_("Critical thresholds must be a positive integer")); 243 usage4(_("Critical threshold must be a positive integer"));
274 }
275 }
276 else {
277 usage4 (_("Critical threshold must be a positive integer"));
278 } 244 }
279 break; 245 break;
280 case 'W': /* warning-connect */ 246 case 'W': /* warning-connect */
281 if (!is_intnonneg (optarg)) 247 if (!is_intnonneg(optarg))
282 usage4 (_("Warning threshold must be a positive integer")); 248 usage4(_("Warning threshold must be a positive integer"));
283 else 249 else
284 warning_time = atoi (optarg); 250 warning_time = atoi(optarg);
285 check_warning_time = true; 251 check_warning_time = true;
286 break; 252 break;
287 case 'C': /* critical-connect */ 253 case 'C': /* critical-connect */
288 if (!is_intnonneg (optarg)) 254 if (!is_intnonneg(optarg))
289 usage4 (_("Critical threshold must be a positive integer")); 255 usage4(_("Critical threshold must be a positive integer"));
290 else 256 else
291 critical_time = atoi (optarg); 257 critical_time = atoi(optarg);
292 check_critical_time = true; 258 check_critical_time = true;
293 break; 259 break;
294 case 'p': /* port */ 260 case 'p': /* port */
295 if (!is_intnonneg (optarg)) 261 if (!is_intnonneg(optarg))
296 usage4 (_("Port must be a positive integer")); 262 usage4(_("Port must be a positive integer"));
297 else 263 else
298 server_port = atoi (optarg); 264 server_port = atoi(optarg);
299 break; 265 break;
300 case 't': /* timeout */ 266 case 't': /* timeout */
301 if (!is_intnonneg (optarg)) 267 if (!is_intnonneg(optarg))
302 usage2 (_("Timeout interval must be a positive integer"), optarg); 268 usage2(_("Timeout interval must be a positive integer"), optarg);
303 else 269 else
304 socket_timeout = atoi (optarg); 270 socket_timeout = atoi(optarg);
305 break; 271 break;
306 case 'u': /* udp */ 272 case 'u': /* udp */
307 use_udp = true; 273 use_udp = true;
308 } 274 }
309 } 275 }
310 276
311 c = optind; 277 option_char = optind;
312 if (server_address == NULL) { 278 if (server_address == NULL) {
313 if (argc > c) { 279 if (argc > option_char) {
314 if (!is_host (argv[c])) 280 if (!is_host(argv[option_char]))
315 usage2 (_("Invalid hostname/address"), optarg); 281 usage2(_("Invalid hostname/address"), optarg);
316 server_address = argv[c]; 282 server_address = argv[option_char];
317 } 283 } else {
318 else { 284 usage4(_("Hostname was not supplied"));
319 usage4 (_("Hostname was not supplied"));
320 } 285 }
321 } 286 }
322 287
323 return OK; 288 return OK;
324} 289}
325 290
326 291void print_help(void) {
327
328void
329print_help (void)
330{
331 char *myport; 292 char *myport;
332 xasprintf (&myport, "%d", TIME_PORT); 293 xasprintf(&myport, "%d", TIME_PORT);
333 294
334 print_revision (progname, NP_VERSION); 295 print_revision(progname, NP_VERSION);
335 296
336 printf ("Copyright (c) 1999 Ethan Galstad\n"); 297 printf("Copyright (c) 1999 Ethan Galstad\n");
337 printf (COPYRIGHT, copyright, email); 298 printf(COPYRIGHT, copyright, email);
338 299
339 printf ("%s\n", _("This plugin will check the time on the specified host.")); 300 printf("%s\n", _("This plugin will check the time on the specified host."));
340 301
341 printf ("\n\n"); 302 printf("\n\n");
342 303
343 print_usage (); 304 print_usage();
344 305
345 printf (UT_HELP_VRSN); 306 printf(UT_HELP_VRSN);
346 printf (UT_EXTRA_OPTS); 307 printf(UT_EXTRA_OPTS);
347 308
348 printf (UT_HOST_PORT, 'p', myport); 309 printf(UT_HOST_PORT, 'p', myport);
349 310
350 printf (" %s\n", "-u, --udp"); 311 printf(" %s\n", "-u, --udp");
351 printf (" %s\n", _("Use UDP to connect, not TCP")); 312 printf(" %s\n", _("Use UDP to connect, not TCP"));
352 printf (" %s\n", "-w, --warning-variance=INTEGER"); 313 printf(" %s\n", "-w, --warning-variance=INTEGER");
353 printf (" %s\n", _("Time difference (sec.) necessary to result in a warning status")); 314 printf(" %s\n", _("Time difference (sec.) necessary to result in a warning status"));
354 printf (" %s\n", "-c, --critical-variance=INTEGER"); 315 printf(" %s\n", "-c, --critical-variance=INTEGER");
355 printf (" %s\n", _("Time difference (sec.) necessary to result in a critical status")); 316 printf(" %s\n", _("Time difference (sec.) necessary to result in a critical status"));
356 printf (" %s\n", "-W, --warning-connect=INTEGER"); 317 printf(" %s\n", "-W, --warning-connect=INTEGER");
357 printf (" %s\n", _("Response time (sec.) necessary to result in warning status")); 318 printf(" %s\n", _("Response time (sec.) necessary to result in warning status"));
358 printf (" %s\n", "-C, --critical-connect=INTEGER"); 319 printf(" %s\n", "-C, --critical-connect=INTEGER");
359 printf (" %s\n", _("Response time (sec.) necessary to result in critical status")); 320 printf(" %s\n", _("Response time (sec.) necessary to result in critical status"));
360 321
361 printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 322 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
362 323
363 printf (UT_SUPPORT); 324 printf(UT_SUPPORT);
364} 325}
365 326
366 327void print_usage(void) {
367 328 printf("%s\n", _("Usage:"));
368void 329 printf("%s -H <host_address> [-p port] [-u] [-w variance] [-c variance]\n", progname);
369print_usage (void) 330 printf(" [-W connect_time] [-C connect_time] [-t timeout]\n");
370{
371 printf ("%s\n", _("Usage:"));
372 printf ("%s -H <host_address> [-p port] [-u] [-w variance] [-c variance]\n",progname);
373 printf (" [-W connect_time] [-C connect_time] [-t timeout]\n");
374} 331}