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 ******************************************************************************/
19 #include "common.h"
20 #include "netutils.h"
21 #include "utils.h"
23 const char *progname = "check_time";
24 const char *revision = "$Revision$";
25 const char *copyright = "1999-2003";
26 const char *email = "nagiosplug-devel@lists.sourceforge.net";
28 enum {
29 TIME_PORT = 37
30 };
32 #define UNIX_EPOCH 2208988800UL
34 unsigned long server_time, raw_server_time;
35 time_t diff_time;
36 int warning_time = 0;
37 int check_warning_time = FALSE;
38 int critical_time = 0;
39 int check_critical_time = FALSE;
40 unsigned long warning_diff = 0;
41 int check_warning_diff = FALSE;
42 unsigned long critical_diff = 0;
43 int check_critical_diff = FALSE;
44 int server_port = TIME_PORT;
45 char *server_address = NULL;
46 int use_udp = FALSE;
48 int process_arguments (int, char **);
49 void print_help (void);
50 void print_usage (void);
52 int
53 main (int argc, char **argv)
54 {
55 int sd;
56 int result;
58 setlocale (LC_ALL, "");
59 bindtextdomain (PACKAGE, LOCALEDIR);
60 textdomain (PACKAGE);
62 if (process_arguments (argc, argv) != OK)
63 usage (_("Invalid command arguments supplied\n"));
65 /* initialize alarm signal handling */
66 signal (SIGALRM, socket_timeout_alarm_handler);
68 /* set socket timeout */
69 alarm (socket_timeout);
70 time (&start_time);
72 /* try to connect to the host at the given port number */
73 if (use_udp) {
74 result = my_udp_connect (server_address, server_port, &sd);
75 } else {
76 result = my_tcp_connect (server_address, server_port, &sd);
77 }
79 if (result != STATE_OK) {
80 if (check_critical_time == TRUE)
81 result = STATE_CRITICAL;
82 else if (check_warning_time == TRUE)
83 result = STATE_WARNING;
84 else
85 result = STATE_UNKNOWN;
86 die (result,
87 _("TIME UNKNOWN - could not connect to server %s, port %d\n"),
88 server_address, server_port);
89 }
91 if (use_udp) {
92 if (send (sd, "", 0, 0) < 0) {
93 if (check_critical_time == TRUE)
94 result = STATE_CRITICAL;
95 else if (check_warning_time == TRUE)
96 result = STATE_WARNING;
97 else
98 result = STATE_UNKNOWN;
99 die (result,
100 _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"),
101 server_address, server_port);
102 }
103 }
105 /* watch for the connection string */
106 result = recv (sd, (void *)&raw_server_time, sizeof (raw_server_time), 0);
108 /* close the connection */
109 close (sd);
111 /* reset the alarm */
112 time (&end_time);
113 alarm (0);
115 /* return a WARNING status if we couldn't read any data */
116 if (result <= 0) {
117 if (check_critical_time == TRUE)
118 result = STATE_CRITICAL;
119 else if (check_warning_time == TRUE)
120 result = STATE_WARNING;
121 else
122 result = STATE_UNKNOWN;
123 die (result,
124 _("TIME UNKNOWN - no data on recv() from server %s, port %d\n"),
125 server_address, server_port);
126 }
128 result = STATE_OK;
130 if (check_critical_time == TRUE && (end_time - start_time) > critical_time)
131 result = STATE_CRITICAL;
132 else if (check_warning_time == TRUE
133 && (end_time - start_time) > warning_time) result = STATE_WARNING;
135 if (result != STATE_OK)
136 die (result, _("TIME %s - %d second response time\n"),
137 state_text (result), (int) (end_time - start_time));
139 server_time = ntohl (raw_server_time) - UNIX_EPOCH;
140 if (server_time > (unsigned long)end_time)
141 diff_time = server_time - (unsigned long)end_time;
142 else
143 diff_time = (unsigned long)end_time - server_time;
145 if (check_critical_diff == TRUE && diff_time > (time_t)critical_diff)
146 result = STATE_CRITICAL;
147 else if (check_warning_diff == TRUE && diff_time > (time_t)warning_diff)
148 result = STATE_WARNING;
150 printf (_("TIME %s - %lu second time difference\n"), state_text (result),
151 diff_time);
152 return result;
153 }
160 /* process command-line arguments */
161 int
162 process_arguments (int argc, char **argv)
163 {
164 int c;
166 int option = 0;
167 static struct option longopts[] = {
168 {"hostname", required_argument, 0, 'H'},
169 {"warning-variance", required_argument, 0, 'w'},
170 {"critical-variance", required_argument, 0, 'c'},
171 {"warning-connect", required_argument, 0, 'W'},
172 {"critical-connect", required_argument, 0, 'C'},
173 {"port", required_argument, 0, 'p'},
174 {"udp", no_argument, 0, 'u'},
175 {"timeout", required_argument, 0, 't'},
176 {"version", no_argument, 0, 'V'},
177 {"help", no_argument, 0, 'h'},
178 {0, 0, 0, 0}
179 };
181 if (argc < 2)
182 usage ("\n");
184 for (c = 1; c < argc; c++) {
185 if (strcmp ("-to", argv[c]) == 0)
186 strcpy (argv[c], "-t");
187 else if (strcmp ("-wd", argv[c]) == 0)
188 strcpy (argv[c], "-w");
189 else if (strcmp ("-cd", argv[c]) == 0)
190 strcpy (argv[c], "-c");
191 else if (strcmp ("-wt", argv[c]) == 0)
192 strcpy (argv[c], "-W");
193 else if (strcmp ("-ct", argv[c]) == 0)
194 strcpy (argv[c], "-C");
195 }
197 while (1) {
198 c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:u", longopts,
199 &option);
201 if (c == -1 || c == EOF)
202 break;
204 switch (c) {
205 case '?': /* print short usage statement if args not parsable */
206 usage3 (_("Unknown argument"), optopt);
207 case 'h': /* help */
208 print_help ();
209 exit (STATE_OK);
210 case 'V': /* version */
211 print_revision (progname, revision);
212 exit (STATE_OK);
213 case 'H': /* hostname */
214 if (is_host (optarg) == FALSE)
215 usage (_("Invalid host name/address\n"));
216 server_address = optarg;
217 break;
218 case 'w': /* warning-variance */
219 if (is_intnonneg (optarg)) {
220 warning_diff = strtoul (optarg, NULL, 10);
221 check_warning_diff = TRUE;
222 }
223 else if (strspn (optarg, "0123456789:,") > 0) {
224 if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
225 check_warning_diff = TRUE;
226 check_warning_time = TRUE;
227 }
228 else {
229 usage (_("Warning thresholds must be a nonnegative integer\n"));
230 }
231 }
232 else {
233 usage (_("Warning threshold must be a nonnegative integer\n"));
234 }
235 break;
236 case 'c': /* critical-variance */
237 if (is_intnonneg (optarg)) {
238 critical_diff = strtoul (optarg, NULL, 10);
239 check_critical_diff = TRUE;
240 }
241 else if (strspn (optarg, "0123456789:,") > 0) {
242 if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
243 2) {
244 check_critical_diff = TRUE;
245 check_critical_time = TRUE;
246 }
247 else {
248 usage (_("Critical thresholds must be a nonnegative integer\n"));
249 }
250 }
251 else {
252 usage (_("Critical threshold must be a nonnegative integer\n"));
253 }
254 break;
255 case 'W': /* warning-connect */
256 if (!is_intnonneg (optarg))
257 usage (_("Warning threshold must be a nonnegative integer\n"));
258 else
259 warning_time = atoi (optarg);
260 check_warning_time = TRUE;
261 break;
262 case 'C': /* critical-connect */
263 if (!is_intnonneg (optarg))
264 usage (_("Critical threshold must be a nonnegative integer\n"));
265 else
266 critical_time = atoi (optarg);
267 check_critical_time = TRUE;
268 break;
269 case 'p': /* port */
270 if (!is_intnonneg (optarg))
271 usage (_("Server port must be a nonnegative integer\n"));
272 else
273 server_port = atoi (optarg);
274 break;
275 case 't': /* timeout */
276 if (!is_intnonneg (optarg))
277 usage (_("Timeout interval must be a nonnegative integer\n"));
278 else
279 socket_timeout = atoi (optarg);
280 break;
281 case 'u': /* udp */
282 use_udp = TRUE;
283 }
284 }
286 c = optind;
287 if (server_address == NULL) {
288 if (argc > c) {
289 if (is_host (argv[c]) == FALSE)
290 usage (_("Invalid host name/address\n"));
291 server_address = argv[c];
292 }
293 else {
294 usage (_("Host name was not supplied\n"));
295 }
296 }
298 return OK;
299 }
305 \f
306 void
307 print_help (void)
308 {
309 char *myport;
310 asprintf (&myport, "%d", TIME_PORT);
312 print_revision (progname, revision);
314 printf (_("Copyright (c) 1999 Ethan Galstad\n"));
315 printf (_(COPYRIGHT), copyright, email);
317 printf (_("\
318 This plugin will check the time on the specified host.\n\n"));
320 print_usage ();
322 printf (_(UT_HELP_VRSN));
324 printf (_(UT_HOST_PORT), 'p', myport);
326 printf (_("\
327 -u, --udp\n\
328 Use UDP to connect, not TCP\n\
329 -w, --warning-variance=INTEGER\n\
330 Time difference (sec.) necessary to result in a warning status\n\
331 -c, --critical-variance=INTEGER\n\
332 Time difference (sec.) necessary to result in a critical status\n\
333 -W, --warning-connect=INTEGER\n\
334 Response time (sec.) necessary to result in warning status\n\
335 -C, --critical-connect=INTEGER\n\
336 Response time (sec.) necessary to result in critical status\n"));
338 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
340 printf (_(UT_SUPPORT));
341 }
346 void
347 print_usage (void)
348 {
349 printf (_("\
350 Usage: %s -H <host_address> [-p port] [-u] [-w variance] [-c variance]\n\
351 [-W connect_time] [-C connect_time] [-t timeout]\n"), progname);
352 printf (_(UT_HLP_VRS), progname, progname);
353 }