Code

e6c3c7167cdb0d4ed098db8044ce666f248524de
[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 ******************************************************************************/
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         if (process_arguments (argc, argv) != OK)
59                 usage (_("Invalid command arguments supplied\n"));
61         /* initialize alarm signal handling */
62         signal (SIGALRM, socket_timeout_alarm_handler);
64         /* set socket timeout */
65         alarm (socket_timeout);
66         time (&start_time);
68         /* try to connect to the host at the given port number */
69         if (use_udp) {
70                 result = my_udp_connect (server_address, server_port, &sd);
71         } else {
72                 result = my_tcp_connect (server_address, server_port, &sd);
73         }
75         if (result != STATE_OK) {
76                 if (check_critical_time == TRUE)
77                         result = STATE_CRITICAL;
78                 else if (check_warning_time == TRUE)
79                         result = STATE_WARNING;
80                 else
81                         result = STATE_UNKNOWN;
82                 die (result,
83                            _("TIME UNKNOWN - could not connect to server %s, port %d\n"),
84                            server_address, server_port);
85         }
87         if (use_udp) {
88                 if (send (sd, "", 0, 0) < 0) {
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                         die (result, 
96                           _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"),
97                           server_address, server_port);
98                 }
99         }
101         /* watch for the connection string */
102         result = recv (sd, (void *)&raw_server_time, sizeof (raw_server_time), 0);
104         /* close the connection */
105         close (sd);
107         /* reset the alarm */
108         time (&end_time);
109         alarm (0);
111         /* return a WARNING status if we couldn't read any data */
112         if (result <= 0) {
113                 if (check_critical_time == TRUE)
114                         result = STATE_CRITICAL;
115                 else if (check_warning_time == TRUE)
116                         result = STATE_WARNING;
117                 else
118                         result = STATE_UNKNOWN;
119                 die (result,
120                                                          _("TIME UNKNOWN - no data on recv() from server %s, port %d\n"),
121                                                          server_address, server_port);
122         }
124         result = STATE_OK;
126         if (check_critical_time == TRUE && (end_time - start_time) > critical_time)
127                 result = STATE_CRITICAL;
128         else if (check_warning_time == TRUE
129                                          && (end_time - start_time) > warning_time) result = STATE_WARNING;
131         if (result != STATE_OK)
132                 die (result, _("TIME %s - %d second response time\n"),
133                                                          state_text (result), (int) (end_time - start_time));
135         server_time = ntohl (raw_server_time) - UNIX_EPOCH;
136         if (server_time > (unsigned long)end_time)
137                 diff_time = server_time - (unsigned long)end_time;
138         else
139                 diff_time = (unsigned long)end_time - server_time;
141         if (check_critical_diff == TRUE && diff_time > (time_t)critical_diff)
142                 result = STATE_CRITICAL;
143         else if (check_warning_diff == TRUE && diff_time > (time_t)warning_diff)
144                 result = STATE_WARNING;
146         printf (_("TIME %s - %lu second time difference\n"), state_text (result),
147                                         diff_time);
148         return result;
156 /* process command-line arguments */
157 int
158 process_arguments (int argc, char **argv)
160         int c;
162         int option = 0;
163         static struct option longopts[] = {
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                 {"udp", no_argument, 0, 'u'},
171                 {"timeout", required_argument, 0, 't'},
172                 {"version", no_argument, 0, 'V'},
173                 {"help", no_argument, 0, 'h'},
174                 {0, 0, 0, 0}
175         };
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                 c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:u", longopts,
195                                                                          &option);
197                 if (c == -1 || c == EOF)
198                         break;
200                 switch (c) {
201                 case '?':                                                                       /* print short usage statement if args not parsable */
202                         usage3 (_("Unknown argument"), optopt);
203                 case 'h':                                                                       /* help */
204                         print_help ();
205                         exit (STATE_OK);
206                 case 'V':                                                                       /* version */
207                         print_revision (progname, revision);
208                         exit (STATE_OK);
209                 case 'H':                                                                       /* hostname */
210                         if (is_host (optarg) == FALSE)
211                                 usage (_("Invalid host name/address\n"));
212                         server_address = optarg;
213                         break;
214                 case 'w':                                                                       /* warning-variance */
215                         if (is_intnonneg (optarg)) {
216                                 warning_diff = strtoul (optarg, NULL, 10);
217                                 check_warning_diff = TRUE;
218                         }
219                         else if (strspn (optarg, "0123456789:,") > 0) {
220                                 if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
221                                         check_warning_diff = TRUE;
222                                         check_warning_time = TRUE;
223                                 }
224                                 else {
225                                         usage (_("Warning thresholds must be a nonnegative integer\n"));
226                                 }
227                         }
228                         else {
229                                 usage (_("Warning threshold must be a nonnegative integer\n"));
230                         }
231                         break;
232                 case 'c':                                                                       /* critical-variance */
233                         if (is_intnonneg (optarg)) {
234                                 critical_diff = strtoul (optarg, NULL, 10);
235                                 check_critical_diff = TRUE;
236                         }
237                         else if (strspn (optarg, "0123456789:,") > 0) {
238                                 if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
239                                                 2) {
240                                         check_critical_diff = TRUE;
241                                         check_critical_time = TRUE;
242                                 }
243                                 else {
244                                         usage (_("Critical thresholds must be a nonnegative integer\n"));
245                                 }
246                         }
247                         else {
248                                 usage (_("Critical threshold must be a nonnegative integer\n"));
249                         }
250                         break;
251                 case 'W':                                                                       /* warning-connect */
252                         if (!is_intnonneg (optarg))
253                                 usage (_("Warning threshold must be a nonnegative integer\n"));
254                         else
255                                 warning_time = atoi (optarg);
256                         check_warning_time = TRUE;
257                         break;
258                 case 'C':                                                                       /* critical-connect */
259                         if (!is_intnonneg (optarg))
260                                 usage (_("Critical threshold must be a nonnegative integer\n"));
261                         else
262                                 critical_time = atoi (optarg);
263                         check_critical_time = TRUE;
264                         break;
265                 case 'p':                                                                       /* port */
266                         if (!is_intnonneg (optarg))
267                                 usage (_("Server port must be a nonnegative integer\n"));
268                         else
269                                 server_port = atoi (optarg);
270                         break;
271                 case 't':                                                                       /* timeout */
272                         if (!is_intnonneg (optarg))
273                                 usage (_("Timeout interval must be a nonnegative integer\n"));
274                         else
275                                 socket_timeout = atoi (optarg);
276                         break;
277                 case 'u':                                                                       /* udp */
278                         use_udp = TRUE;
279                 }
280         }
282         c = optind;
283         if (server_address == NULL) {
284                 if (argc > c) {
285                         if (is_host (argv[c]) == FALSE)
286                                 usage (_("Invalid host name/address\n"));
287                         server_address = argv[c];
288                 }
289                 else {
290                         usage (_("Host name was not supplied\n"));
291                 }
292         }
294         return OK;
301 \f
302 void
303 print_help (void)
305         char *myport;
306         asprintf (&myport, "%d", TIME_PORT);
308         print_revision (progname, revision);
310         printf (_("Copyright (c) 1999 Ethan Galstad\n"));
311         printf (_(COPYRIGHT), copyright, email);
313         printf (_("\
314 This plugin will check the time on the specified host.\n\n"));
316         print_usage ();
318         printf (_(UT_HELP_VRSN));
320         printf (_(UT_HOST_PORT), 'p', myport);
322         printf (_("\
323  -u, --udp\n\
324     Use UDP to connect, not TCP\n\
325  -w, --warning-variance=INTEGER\n\
326     Time difference (sec.) necessary to result in a warning status\n\
327  -c, --critical-variance=INTEGER\n\
328     Time difference (sec.) necessary to result in a critical status\n\
329  -W, --warning-connect=INTEGER\n\
330     Response time (sec.) necessary to result in warning status\n\
331  -C, --critical-connect=INTEGER\n\
332     Response time (sec.) necessary to result in critical status\n"));
334         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
336         printf (_(UT_SUPPORT));
342 void
343 print_usage (void)
345         printf (_("\
346 Usage: %s -H <host_address> [-p port] [-u] [-w variance] [-c variance]\n\
347     [-W connect_time] [-C connect_time] [-t timeout]\n"), progname);
348         printf (_(UT_HLP_VRS), progname, progname);