Code

Remove getopt_long checks
[nagiosplug.git] / plugins / check_time.c
1 /******************************************************************************
2 *
3 * CHECK_TIME.C
4 *
5 * Program: Network time server plugin for Nagios
6 * License: GPL
7 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
8 * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
9 *
10 * $Id$
11 *
12 * Description:
13 *
14 * This plugin will attempt to connect to the specified port
15 * on the host.  Successul connects return STATE_OK, refusals
16 * and timeouts return STATE_CRITICAL, other errors return
17 * STATE_UNKNOWN.
18 *
19 * License Information:
20 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 *
35 *****************************************************************************/
37 #include "config.h"
38 #include "common.h"
39 #include "netutils.h"
40 #include "utils.h"
42 const char *progname = "check_time";
43 #define REVISION "$Revision$"
44 #define COPYRIGHT "1999-2002"
45 #define AUTHOR "Ethan Galstad"
46 #define EMAIL "nagios@nagios.org"
47 #define SUMMARY "Check time on the specified host.\n"
49 #define TIME_PORT       37
50 #define UNIX_EPOCH      2208988800UL
52 unsigned long server_time, raw_server_time;
53 time_t 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;
66 int process_arguments (int, char **);
67 void print_usage (void);
68 void print_help (void);
71 int
72 main (int argc, char **argv)
73 {
74         int sd;
75         int result;
77         if (process_arguments (argc, argv) != OK)
78                 usage ("Invalid command arguments supplied\n");
80         /* initialize alarm signal handling */
81         signal (SIGALRM, socket_timeout_alarm_handler);
83         /* set socket timeout */
84         alarm (socket_timeout);
85         time (&start_time);
87         /* try to connect to the host at the given port number */
88         if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK) {
89                 if (check_critical_time == TRUE)
90                         result = STATE_CRITICAL;
91                 else if (check_warning_time == TRUE)
92                         result = STATE_WARNING;
93                 else
94                         result = STATE_UNKNOWN;
95                 terminate (result,
96                            "TIME UNKNOWN - could not connect to server %s, port %d\n",
97                            server_address, server_port);
98         }
100         /* watch for the connection string */
101         result = recv (sd, (void *)&raw_server_time, sizeof (raw_server_time), 0);
103         /* close the connection */
104         close (sd);
106         /* reset the alarm */
107         time (&end_time);
108         alarm (0);
110         /* return a WARNING status if we couldn't read any data */
111         if (result <= 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                 terminate (result,
119                                                          "TIME UNKNOWN - no data on recv() from server %s, port %d\n",
120                                                          server_address, server_port);
121         }
123         result = STATE_OK;
125         if (check_critical_time == TRUE && (end_time - start_time) > critical_time)
126                 result = STATE_CRITICAL;
127         else if (check_warning_time == TRUE
128                                          && (end_time - start_time) > warning_time) result = STATE_WARNING;
130         if (result != STATE_OK)
131                 terminate (result, "TIME %s - %d second response time\n",
132                                                          state_text (result), (int) (end_time - start_time));
134         server_time = ntohl (raw_server_time) - UNIX_EPOCH;
135         if (server_time > end_time)
136                 diff_time = server_time - end_time;
137         else
138                 diff_time = end_time - server_time;
140         if (check_critical_diff == TRUE && diff_time > critical_diff)
141                 result = STATE_CRITICAL;
142         else if (check_warning_diff == TRUE && diff_time > warning_diff)
143                 result = STATE_WARNING;
145         printf ("TIME %s - %lu second time difference\n", state_text (result),
146                                         diff_time);
147         return result;
155 /* process command-line arguments */
156 int
157 process_arguments (int argc, char **argv)
159         int c;
161         int option_index = 0;
162         static struct option long_options[] = {
163                 {"hostname", required_argument, 0, 'H'},
164                 {"warning-variance", required_argument, 0, 'w'},
165                 {"critical-variance", required_argument, 0, 'c'},
166                 {"warning-connect", required_argument, 0, 'W'},
167                 {"critical-connect", required_argument, 0, 'C'},
168                 {"port", required_argument, 0, 'p'},
169                 {"timeout", required_argument, 0, 't'},
170                 {"version", no_argument, 0, 'V'},
171                 {"help", no_argument, 0, 'h'},
172                 {0, 0, 0, 0}
173         };
175         if (argc < 2)
176                 usage ("\n");
178         for (c = 1; c < argc; c++) {
179                 if (strcmp ("-to", argv[c]) == 0)
180                         strcpy (argv[c], "-t");
181                 else if (strcmp ("-wd", argv[c]) == 0)
182                         strcpy (argv[c], "-w");
183                 else if (strcmp ("-cd", argv[c]) == 0)
184                         strcpy (argv[c], "-c");
185                 else if (strcmp ("-wt", argv[c]) == 0)
186                         strcpy (argv[c], "-W");
187                 else if (strcmp ("-ct", argv[c]) == 0)
188                         strcpy (argv[c], "-C");
189         }
191         while (1) {
192                 c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:", long_options,
193                                                                          &option_index);
195                 if (c == -1 || c == EOF)
196                         break;
198                 switch (c) {
199                 case '?':                                                                       /* print short usage statement if args not parsable */
200                         usage3 ("Unknown argument", optopt);
201                 case 'h':                                                                       /* help */
202                         print_help ();
203                         exit (STATE_OK);
204                 case 'V':                                                                       /* version */
205                         print_revision (progname, REVISION);
206                         exit (STATE_OK);
207                 case 'H':                                                                       /* hostname */
208                         if (is_host (optarg) == FALSE)
209                                 usage ("Invalid host name/address\n");
210                         server_address = optarg;
211                         break;
212                 case 'w':                                                                       /* warning-variance */
213                         if (is_intnonneg (optarg)) {
214                                 warning_diff = strtoul (optarg, NULL, 10);
215                                 check_warning_diff = TRUE;
216                         }
217                         else if (strspn (optarg, "0123456789:,") > 0) {
218                                 if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
219                                         check_warning_diff = TRUE;
220                                         check_warning_time = TRUE;
221                                 }
222                                 else {
223                                         usage ("Warning thresholds must be a nonnegative integer\n");
224                                 }
225                         }
226                         else {
227                                 usage ("Warning threshold must be a nonnegative integer\n");
228                         }
229                         break;
230                 case 'c':                                                                       /* critical-variance */
231                         if (is_intnonneg (optarg)) {
232                                 critical_diff = strtoul (optarg, NULL, 10);
233                                 check_critical_diff = TRUE;
234                         }
235                         else if (strspn (optarg, "0123456789:,") > 0) {
236                                 if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
237                                                 2) {
238                                         check_critical_diff = TRUE;
239                                         check_critical_time = TRUE;
240                                 }
241                                 else {
242                                         usage ("Critical thresholds must be a nonnegative integer\n");
243                                 }
244                         }
245                         else {
246                                 usage ("Critical threshold must be a nonnegative integer\n");
247                         }
248                         break;
249                 case 'W':                                                                       /* warning-connect */
250                         if (!is_intnonneg (optarg))
251                                 usage ("Warning threshold must be a nonnegative integer\n");
252                         warning_time = atoi (optarg);
253                         check_warning_time = TRUE;
254                         break;
255                 case 'C':                                                                       /* critical-connect */
256                         if (!is_intnonneg (optarg))
257                                 usage ("Critical threshold must be a nonnegative integer\n");
258                         critical_time = atoi (optarg);
259                         check_critical_time = TRUE;
260                         break;
261                 case 'p':                                                                       /* port */
262                         if (!is_intnonneg (optarg))
263                                 usage ("Serevr port must be a nonnegative integer\n");
264                         server_port = atoi (optarg);
265                         break;
266                 case 't':                                                                       /* timeout */
267                         if (!is_intnonneg (optarg))
268                                 usage ("Timeout interval must be a nonnegative integer\n");
269                         socket_timeout = atoi (optarg);
270                         break;
271                 }
272         }
274         c = optind;
275         if (server_address == NULL) {
276                 if (argc > c) {
277                         if (is_host (argv[c]) == FALSE)
278                                 usage ("Invalid host name/address\n");
279                         server_address = argv[c];
280                 }
281                 else {
282                         usage ("Host name was not supplied\n");
283                 }
284         }
286         return OK;
293 void
294 print_usage (void)
296         printf
297                 ("Usage:\n"
298                  " %s -H <host_address> [-p port] [-w variance] [-c variance]\n"
299                  "           [-W connect_time] [-C connect_time] [-t timeout]\n"
300                  " %s (-h | --help) for detailed help\n"
301                  " %s (-V | --version) for version information\n",
302                  progname, progname, progname);
309 void
310 print_help (void)
312         print_revision (progname, REVISION);
313         printf
314                 ("Copyright (c) %s %s <%s>\n\n%s\n",
315                  COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
316         print_usage ();
317         printf
318                 ("Options:\n"
319                  " -H, --hostname=ADDRESS\n"
320                  "    Host name argument for servers using host headers (use numeric\n"
321                  "    address if possible to bypass DNS lookup).\n"
322                  " -w, --warning-variance=INTEGER\n"
323                  "    Time difference (sec.) necessary to result in a warning status\n"
324                  " -c, --critical-variance=INTEGER\n"
325                  "    Time difference (sec.) necessary to result in a critical status\n"
326                  " -W, --warning-connect=INTEGER\n"
327                  "    Response time (sec.) necessary to result in warning status\n"
328                  " -C, --critical-connect=INTEGER\n"
329                  "    Response time (sec.) necessary to result in critical status\n"
330                  " -t, --timeout=INTEGER\n"
331                  "    Seconds before connection times out (default: %d)\n"
332                  " -p, --port=INTEGER\n"
333                  "    Port number (default: %d)\n"
334                  " -h, --help\n"
335                  "    Print detailed help screen\n"
336                  " -V, --version\n"
337                  "    Print version information\n\n", DEFAULT_SOCKET_TIMEOUT, TIME_PORT);
338         support ();