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