Code

b3210bc3e1f85b59319f7d0bcc61a3fb7bffe3ea
[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 void
33 print_usage (void)
34 {
35         printf (_("\
36 Usage: %s -H <host_address> [-p port] [-w variance] [-c variance]\n\
37     [-W connect_time] [-C connect_time] [-t timeout]\n"), progname);
38         printf (_(UT_HLP_VRS), progname, progname);
39 }
41 void
42 print_help (void)
43 {
44         char *myport;
45         asprintf (&myport, "%d", TIME_PORT);
47         print_revision (progname, revision);
49         printf (_("Copyright (c) 1999 Ethan Galstad\n"));
50         printf (_(COPYRIGHT), copyright, email);
52         printf (_("\
53 This plugin will check the time on the specified host.\n\n"));
55         print_usage ();
57         printf (_(UT_HELP_VRSN));
59         printf (_(UT_HOST_PORT), 'p', myport);
61         printf (_("\
62  -w, --warning-variance=INTEGER\n\
63     Time difference (sec.) necessary to result in a warning status\n\
64  -c, --critical-variance=INTEGER\n\
65     Time difference (sec.) necessary to result in a critical status\n\
66  -W, --warning-connect=INTEGER\n\
67     Response time (sec.) necessary to result in warning status\n\
68  -C, --critical-connect=INTEGER\n\
69     Response time (sec.) necessary to result in critical status\n"));
71         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
73         support ();
74 }
75 \f
77 #define UNIX_EPOCH 2208988800UL
79 unsigned long server_time, raw_server_time;
80 time_t diff_time;
81 int warning_time = 0;
82 int check_warning_time = FALSE;
83 int critical_time = 0;
84 int check_critical_time = FALSE;
85 unsigned long warning_diff = 0;
86 int check_warning_diff = FALSE;
87 unsigned long critical_diff = 0;
88 int check_critical_diff = FALSE;
89 int server_port = TIME_PORT;
90 char *server_address = NULL;
93 int process_arguments (int, char **);
94 void print_usage (void);
95 void print_help (void);
98 int
99 main (int argc, char **argv)
101         int sd;
102         int result;
104         if (process_arguments (argc, argv) != OK)
105                 usage (_("Invalid command arguments supplied\n"));
107         /* initialize alarm signal handling */
108         signal (SIGALRM, socket_timeout_alarm_handler);
110         /* set socket timeout */
111         alarm (socket_timeout);
112         time (&start_time);
114         /* try to connect to the host at the given port number */
115         if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK) {
116                 if (check_critical_time == TRUE)
117                         result = STATE_CRITICAL;
118                 else if (check_warning_time == TRUE)
119                         result = STATE_WARNING;
120                 else
121                         result = STATE_UNKNOWN;
122                 terminate (result,
123                            _("TIME UNKNOWN - could not connect to server %s, port %d\n"),
124                            server_address, server_port);
125         }
127         /* watch for the connection string */
128         result = recv (sd, (void *)&raw_server_time, sizeof (raw_server_time), 0);
130         /* close the connection */
131         close (sd);
133         /* reset the alarm */
134         time (&end_time);
135         alarm (0);
137         /* return a WARNING status if we couldn't read any data */
138         if (result <= 0) {
139                 if (check_critical_time == TRUE)
140                         result = STATE_CRITICAL;
141                 else if (check_warning_time == TRUE)
142                         result = STATE_WARNING;
143                 else
144                         result = STATE_UNKNOWN;
145                 terminate (result,
146                                                          _("TIME UNKNOWN - no data on recv() from server %s, port %d\n"),
147                                                          server_address, server_port);
148         }
150         result = STATE_OK;
152         if (check_critical_time == TRUE && (end_time - start_time) > critical_time)
153                 result = STATE_CRITICAL;
154         else if (check_warning_time == TRUE
155                                          && (end_time - start_time) > warning_time) result = STATE_WARNING;
157         if (result != STATE_OK)
158                 terminate (result, _("TIME %s - %d second response time\n"),
159                                                          state_text (result), (int) (end_time - start_time));
161         server_time = ntohl (raw_server_time) - UNIX_EPOCH;
162         if (server_time > (unsigned long)end_time)
163                 diff_time = server_time - (unsigned long)end_time;
164         else
165                 diff_time = (unsigned long)end_time - server_time;
167         if (check_critical_diff == TRUE && diff_time > (time_t)critical_diff)
168                 result = STATE_CRITICAL;
169         else if (check_warning_diff == TRUE && diff_time > (time_t)warning_diff)
170                 result = STATE_WARNING;
172         printf (_("TIME %s - %lu second time difference\n"), state_text (result),
173                                         diff_time);
174         return result;
182 /* process command-line arguments */
183 int
184 process_arguments (int argc, char **argv)
186         int c;
188         int option_index = 0;
189         static struct option long_options[] = {
190                 {"hostname", required_argument, 0, 'H'},
191                 {"warning-variance", required_argument, 0, 'w'},
192                 {"critical-variance", required_argument, 0, 'c'},
193                 {"warning-connect", required_argument, 0, 'W'},
194                 {"critical-connect", required_argument, 0, 'C'},
195                 {"port", required_argument, 0, 'p'},
196                 {"timeout", required_argument, 0, 't'},
197                 {"version", no_argument, 0, 'V'},
198                 {"help", no_argument, 0, 'h'},
199                 {0, 0, 0, 0}
200         };
202         if (argc < 2)
203                 usage ("\n");
205         for (c = 1; c < argc; c++) {
206                 if (strcmp ("-to", argv[c]) == 0)
207                         strcpy (argv[c], "-t");
208                 else if (strcmp ("-wd", argv[c]) == 0)
209                         strcpy (argv[c], "-w");
210                 else if (strcmp ("-cd", argv[c]) == 0)
211                         strcpy (argv[c], "-c");
212                 else if (strcmp ("-wt", argv[c]) == 0)
213                         strcpy (argv[c], "-W");
214                 else if (strcmp ("-ct", argv[c]) == 0)
215                         strcpy (argv[c], "-C");
216         }
218         while (1) {
219                 c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:", long_options,
220                                                                          &option_index);
222                 if (c == -1 || c == EOF)
223                         break;
225                 switch (c) {
226                 case '?':                                                                       /* print short usage statement if args not parsable */
227                         usage3 (_("Unknown argument"), optopt);
228                 case 'h':                                                                       /* help */
229                         print_help ();
230                         exit (STATE_OK);
231                 case 'V':                                                                       /* version */
232                         print_revision (progname, revision);
233                         exit (STATE_OK);
234                 case 'H':                                                                       /* hostname */
235                         if (is_host (optarg) == FALSE)
236                                 usage (_("Invalid host name/address\n"));
237                         server_address = optarg;
238                         break;
239                 case 'w':                                                                       /* warning-variance */
240                         if (is_intnonneg (optarg)) {
241                                 warning_diff = strtoul (optarg, NULL, 10);
242                                 check_warning_diff = TRUE;
243                         }
244                         else if (strspn (optarg, "0123456789:,") > 0) {
245                                 if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
246                                         check_warning_diff = TRUE;
247                                         check_warning_time = TRUE;
248                                 }
249                                 else {
250                                         usage (_("Warning thresholds must be a nonnegative integer\n"));
251                                 }
252                         }
253                         else {
254                                 usage (_("Warning threshold must be a nonnegative integer\n"));
255                         }
256                         break;
257                 case 'c':                                                                       /* critical-variance */
258                         if (is_intnonneg (optarg)) {
259                                 critical_diff = strtoul (optarg, NULL, 10);
260                                 check_critical_diff = TRUE;
261                         }
262                         else if (strspn (optarg, "0123456789:,") > 0) {
263                                 if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
264                                                 2) {
265                                         check_critical_diff = TRUE;
266                                         check_critical_time = TRUE;
267                                 }
268                                 else {
269                                         usage (_("Critical thresholds must be a nonnegative integer\n"));
270                                 }
271                         }
272                         else {
273                                 usage (_("Critical threshold must be a nonnegative integer\n"));
274                         }
275                         break;
276                 case 'W':                                                                       /* warning-connect */
277                         if (!is_intnonneg (optarg))
278                                 usage (_("Warning threshold must be a nonnegative integer\n"));
279                         warning_time = atoi (optarg);
280                         check_warning_time = TRUE;
281                         break;
282                 case 'C':                                                                       /* critical-connect */
283                         if (!is_intnonneg (optarg))
284                                 usage (_("Critical threshold must be a nonnegative integer\n"));
285                         critical_time = atoi (optarg);
286                         check_critical_time = TRUE;
287                         break;
288                 case 'p':                                                                       /* port */
289                         if (!is_intnonneg (optarg))
290                                 usage (_("Server port must be a nonnegative integer\n"));
291                         server_port = atoi (optarg);
292                         break;
293                 case 't':                                                                       /* timeout */
294                         if (!is_intnonneg (optarg))
295                                 usage (_("Timeout interval must be a nonnegative integer\n"));
296                         socket_timeout = atoi (optarg);
297                         break;
298                 }
299         }
301         c = optind;
302         if (server_address == NULL) {
303                 if (argc > c) {
304                         if (is_host (argv[c]) == FALSE)
305                                 usage (_("Invalid host name/address\n"));
306                         server_address = argv[c];
307                 }
308                 else {
309                         usage (_("Host name was not supplied\n"));
310                 }
311         }
313         return OK;