Code

remove call_getopt
[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 #define 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, &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 #ifdef HAVE_GETOPT_H
162         int option_index = 0;
163         static struct option long_options[] = {
164                 {"hostname", required_argument, 0, 'H'},
165                 {"warning-variance", required_argument, 0, 'w'},
166                 {"critical-variance", required_argument, 0, 'c'},
167                 {"warning-connect", required_argument, 0, 'W'},
168                 {"critical-connect", required_argument, 0, 'C'},
169                 {"port", required_argument, 0, 'p'},
170                 {"timeout", required_argument, 0, 't'},
171                 {"version", no_argument, 0, 'V'},
172                 {"help", no_argument, 0, 'h'},
173                 {0, 0, 0, 0}
174         };
175 #endif
177         if (argc < 2)
178                 usage ("\n");
180         for (c = 1; c < argc; c++) {
181                 if (strcmp ("-to", argv[c]) == 0)
182                         strcpy (argv[c], "-t");
183                 else if (strcmp ("-wd", argv[c]) == 0)
184                         strcpy (argv[c], "-w");
185                 else if (strcmp ("-cd", argv[c]) == 0)
186                         strcpy (argv[c], "-c");
187                 else if (strcmp ("-wt", argv[c]) == 0)
188                         strcpy (argv[c], "-W");
189                 else if (strcmp ("-ct", argv[c]) == 0)
190                         strcpy (argv[c], "-C");
191         }
193         while (1) {
194 #ifdef HAVE_GETOPT_H
195                 c =
196                         getopt_long (argc, argv, "hVH:w:c:W:C:p:t:", long_options,
197                                                                          &option_index);
198 #else
199                 c = getopt (argc, argv, "hVH:w:c:W:C:p:t:");
200 #endif
202                 if (c == -1 || c == EOF)
203                         break;
205                 switch (c) {
206                 case '?':                                                                       /* print short usage statement if args not parsable */
207                         usage3 ("Unknown argument", optopt);
208                 case 'h':                                                                       /* help */
209                         print_help ();
210                         exit (STATE_OK);
211                 case 'V':                                                                       /* version */
212                         print_revision (PROGNAME, REVISION);
213                         exit (STATE_OK);
214                 case 'H':                                                                       /* hostname */
215                         if (is_host (optarg) == FALSE)
216                                 usage ("Invalid host name/address\n");
217                         server_address = optarg;
218                         break;
219                 case 'w':                                                                       /* warning-variance */
220                         if (is_intnonneg (optarg)) {
221                                 warning_diff = strtoul (optarg, NULL, 10);
222                                 check_warning_diff = TRUE;
223                         }
224                         else if (strspn (optarg, "0123456789:,") > 0) {
225                                 if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
226                                         check_warning_diff = TRUE;
227                                         check_warning_time = TRUE;
228                                 }
229                                 else {
230                                         usage ("Warning thresholds must be a nonnegative integer\n");
231                                 }
232                         }
233                         else {
234                                 usage ("Warning threshold must be a nonnegative integer\n");
235                         }
236                         break;
237                 case 'c':                                                                       /* critical-variance */
238                         if (is_intnonneg (optarg)) {
239                                 critical_diff = strtoul (optarg, NULL, 10);
240                                 check_critical_diff = TRUE;
241                         }
242                         else if (strspn (optarg, "0123456789:,") > 0) {
243                                 if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
244                                                 2) {
245                                         check_critical_diff = TRUE;
246                                         check_critical_time = TRUE;
247                                 }
248                                 else {
249                                         usage ("Critical thresholds must be a nonnegative integer\n");
250                                 }
251                         }
252                         else {
253                                 usage ("Critical threshold must be a nonnegative integer\n");
254                         }
255                         break;
256                 case 'W':                                                                       /* warning-connect */
257                         if (!is_intnonneg (optarg))
258                                 usage ("Warning threshold must be a nonnegative integer\n");
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                         critical_time = atoi (optarg);
266                         check_critical_time = TRUE;
267                         break;
268                 case 'p':                                                                       /* port */
269                         if (!is_intnonneg (optarg))
270                                 usage ("Serevr port must be a nonnegative integer\n");
271                         server_port = atoi (optarg);
272                         break;
273                 case 't':                                                                       /* timeout */
274                         if (!is_intnonneg (optarg))
275                                 usage ("Timeout interval must be a nonnegative integer\n");
276                         socket_timeout = atoi (optarg);
277                         break;
278                 }
279         }
281         c = optind;
282         if (server_address == NULL) {
283                 if (argc > c) {
284                         if (is_host (argv[c]) == FALSE)
285                                 usage ("Invalid host name/address\n");
286                         server_address = argv[c];
287                 }
288                 else {
289                         usage ("Host name was not supplied\n");
290                 }
291         }
293         return OK;
300 void
301 print_usage (void)
303         printf
304                 ("Usage:\n"
305                  " %s -H <host_address> [-p port] [-w variance] [-c variance]\n"
306                  "           [-W connect_time] [-C connect_time] [-t timeout]\n"
307                  " %s (-h | --help) for detailed help\n"
308                  " %s (-V | --version) for version information\n",
309                  PROGNAME, PROGNAME, PROGNAME);
316 void
317 print_help (void)
319         print_revision (PROGNAME, REVISION);
320         printf
321                 ("Copyright (c) %s %s <%s>\n\n%s\n",
322                  COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
323         print_usage ();
324         printf
325                 ("Options:\n"
326                  " -H, --hostname=ADDRESS\n"
327                  "    Host name argument for servers using host headers (use numeric\n"
328                  "    address if possible to bypass DNS lookup).\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"
337                  " -t, --timeout=INTEGER\n"
338                  "    Seconds before connection times out (default: %d)\n"
339                  " -p, --port=INTEGER\n"
340                  "    Port number (default: %d)\n"
341                  " -h, --help\n"
342                  "    Print detailed help screen\n"
343                  " -V, --version\n"
344                  "    Print version information\n\n", DEFAULT_SOCKET_TIMEOUT, TIME_PORT);
345         support ();