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