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