Code

remove UT_HLP_VRS from print_usage
[nagiosplug.git] / plugins / check_time.c
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$
18  
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 unsigned long 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) != TRUE)
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;
173 /* process command-line arguments */
174 int
175 process_arguments (int argc, char **argv)
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                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
220                         print_usage ();
221                         exit (STATE_UNKNOWN);
222                 case 'h':                                                                       /* help */
223                         print_help ();
224                         exit (STATE_OK);
225                 case 'V':                                                                       /* version */
226                         print_revision (progname, revision);
227                         exit (STATE_OK);
228                 case 'H':                                                                       /* hostname */
229                         if (is_host (optarg) == FALSE)
230                                 usage2 (_("Invalid hostname/address"), optarg);
231                         server_address = optarg;
232                         break;
233                 case 'w':                                                                       /* warning-variance */
234                         if (is_intnonneg (optarg)) {
235                                 warning_diff = strtoul (optarg, NULL, 10);
236                                 check_warning_diff = TRUE;
237                         }
238                         else if (strspn (optarg, "0123456789:,") > 0) {
239                                 if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
240                                         check_warning_diff = TRUE;
241                                         check_warning_time = TRUE;
242                                 }
243                                 else {
244                                         usage (_("Warning thresholds must be a positive integer\n"));
245                                 }
246                         }
247                         else {
248                                 usage (_("Warning threshold must be a positive integer\n"));
249                         }
250                         break;
251                 case 'c':                                                                       /* critical-variance */
252                         if (is_intnonneg (optarg)) {
253                                 critical_diff = strtoul (optarg, NULL, 10);
254                                 check_critical_diff = TRUE;
255                         }
256                         else if (strspn (optarg, "0123456789:,") > 0) {
257                                 if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
258                                                 2) {
259                                         check_critical_diff = TRUE;
260                                         check_critical_time = TRUE;
261                                 }
262                                 else {
263                                         usage (_("Critical thresholds must be a positive integer\n"));
264                                 }
265                         }
266                         else {
267                                 usage (_("Critical threshold must be a positive integer\n"));
268                         }
269                         break;
270                 case 'W':                                                                       /* warning-connect */
271                         if (!is_intnonneg (optarg))
272                                 usage (_("Warning threshold must be a positive integer\n"));
273                         else
274                                 warning_time = atoi (optarg);
275                         check_warning_time = TRUE;
276                         break;
277                 case 'C':                                                                       /* critical-connect */
278                         if (!is_intnonneg (optarg))
279                                 usage (_("Critical threshold must be a positive integer\n"));
280                         else
281                                 critical_time = atoi (optarg);
282                         check_critical_time = TRUE;
283                         break;
284                 case 'p':                                                                       /* port */
285                         if (!is_intnonneg (optarg))
286                                 usage (_("Port must be a positive integer\n"));
287                         else
288                                 server_port = atoi (optarg);
289                         break;
290                 case 't':                                                                       /* timeout */
291                         if (!is_intnonneg (optarg))
292                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
293                         else
294                                 socket_timeout = atoi (optarg);
295                         break;
296                 case 'u':                                                                       /* udp */
297                         use_udp = TRUE;
298                 }
299         }
301         c = optind;
302         if (server_address == NULL) {
303                 if (argc > c) {
304                         if (is_host (argv[c]) == FALSE)
305                                 usage2 (_("Invalid hostname/address"), optarg);
306                         server_address = argv[c];
307                 }
308                 else {
309                         usage (_("Hostname was not supplied\n"));
310                 }
311         }
313         return OK;
318 void
319 print_help (void)
321         char *myport;
322         asprintf (&myport, "%d", TIME_PORT);
324         print_revision (progname, revision);
326         printf ("Copyright (c) 1999 Ethan Galstad\n");
327         printf (COPYRIGHT, copyright, email);
329         printf (_("\
330 This plugin will check the time on the specified host.\n\n"));
332         print_usage ();
334         printf (_(UT_HELP_VRSN));
336         printf (_(UT_HOST_PORT), 'p', myport);
338         printf (_("\
339  -u, --udp\n\
340     Use UDP to connect, not TCP\n\
341  -w, --warning-variance=INTEGER\n\
342     Time difference (sec.) necessary to result in a warning status\n\
343  -c, --critical-variance=INTEGER\n\
344     Time difference (sec.) necessary to result in a critical status\n\
345  -W, --warning-connect=INTEGER\n\
346     Response time (sec.) necessary to result in warning status\n\
347  -C, --critical-connect=INTEGER\n\
348     Response time (sec.) necessary to result in critical status\n"));
350         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
352         printf (_(UT_SUPPORT));
357 void
358 print_usage (void)
360         printf ("\
361 Usage: %s -H <host_address> [-p port] [-u] [-w variance] [-c variance]\n\
362           [-W connect_time] [-C connect_time] [-t timeout]\n", progname);