Code

Making messages more consistent
[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;
57         time_t conntime;
59         setlocale (LC_ALL, "");
60         bindtextdomain (PACKAGE, LOCALEDIR);
61         textdomain (PACKAGE);
63         if (process_arguments (argc, argv) != OK)
64                 usage (_("Incorrect arguments supplied\n"));
66         /* initialize alarm signal handling */
67         signal (SIGALRM, socket_timeout_alarm_handler);
69         /* set socket timeout */
70         alarm (socket_timeout);
71         time (&start_time);
73         /* try to connect to the host at the given port number */
74         if (use_udp) {
75                 result = my_udp_connect (server_address, server_port, &sd);
76         } else {
77                 result = my_tcp_connect (server_address, server_port, &sd);
78         }
80         if (result != STATE_OK) {
81                 if (check_critical_time == TRUE)
82                         result = STATE_CRITICAL;
83                 else if (check_warning_time == TRUE)
84                         result = STATE_WARNING;
85                 else
86                         result = STATE_UNKNOWN;
87                 die (result,
88                            _("TIME UNKNOWN - could not connect to server %s, port %d\n"),
89                            server_address, server_port);
90         }
92         if (use_udp) {
93                 if (send (sd, "", 0, 0) < 0) {
94                         if (check_critical_time == TRUE)
95                                 result = STATE_CRITICAL;
96                         else if (check_warning_time == TRUE)
97                                 result = STATE_WARNING;
98                         else
99                                 result = STATE_UNKNOWN;
100                         die (result, 
101                           _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"),
102                           server_address, server_port);
103                 }
104         }
106         /* watch for the connection string */
107         result = recv (sd, (void *)&raw_server_time, sizeof (raw_server_time), 0);
109         /* close the connection */
110         close (sd);
112         /* reset the alarm */
113         time (&end_time);
114         alarm (0);
116         /* return a WARNING status if we couldn't read any data */
117         if (result <= 0) {
118                 if (check_critical_time == TRUE)
119                         result = STATE_CRITICAL;
120                 else if (check_warning_time == TRUE)
121                         result = STATE_WARNING;
122                 else
123                         result = STATE_UNKNOWN;
124                 die (result,
125                                                          _("TIME UNKNOWN - no data on recv() from server %s, port %d\n"),
126                                                          server_address, server_port);
127         }
129         result = STATE_OK;
131         conntime = (end_time - start_time);
132         if (check_critical_time == TRUE && conntime > critical_time)
133                 result = STATE_CRITICAL;
134         else if (check_warning_time == TRUE && conntime > warning_time)
135                 result = STATE_WARNING;
137         if (result != STATE_OK)
138                 die (result, _("TIME %s - %d second response time|%s\n"),
139                      state_text (result), (int)conntime,
140                      perfdata ("time", (long)conntime, "s",
141                                check_warning_time, (long)warning_time,
142                                check_critical_time, (long)critical_time,
143                                TRUE, 0, FALSE, 0));
145         server_time = ntohl (raw_server_time) - UNIX_EPOCH;
146         if (server_time > (unsigned long)end_time)
147                 diff_time = server_time - (unsigned long)end_time;
148         else
149                 diff_time = (unsigned long)end_time - server_time;
151         if (check_critical_diff == TRUE && diff_time > (time_t)critical_diff)
152                 result = STATE_CRITICAL;
153         else if (check_warning_diff == TRUE && diff_time > (time_t)warning_diff)
154                 result = STATE_WARNING;
156         printf (_("TIME %s - %lu second time difference|%s %s\n"),
157                 state_text (result), diff_time,
158                 perfdata ("time", (long)conntime, "s",
159                           check_warning_time, (long)warning_time,
160                           check_critical_time, (long)critical_time,
161                           TRUE, 0, FALSE, 0),
162                 perfdata ("offset", (long)diff_time, "s",
163                           check_warning_diff, (long)warning_diff,
164                           check_critical_diff, (long)critical_diff,
165                           TRUE, 0, FALSE, 0));
166         return result;
174 /* process command-line arguments */
175 int
176 process_arguments (int argc, char **argv)
178         int c;
180         int option = 0;
181         static struct option longopts[] = {
182                 {"hostname", required_argument, 0, 'H'},
183                 {"warning-variance", required_argument, 0, 'w'},
184                 {"critical-variance", required_argument, 0, 'c'},
185                 {"warning-connect", required_argument, 0, 'W'},
186                 {"critical-connect", required_argument, 0, 'C'},
187                 {"port", required_argument, 0, 'p'},
188                 {"udp", no_argument, 0, 'u'},
189                 {"timeout", required_argument, 0, 't'},
190                 {"version", no_argument, 0, 'V'},
191                 {"help", no_argument, 0, 'h'},
192                 {0, 0, 0, 0}
193         };
195         if (argc < 2)
196                 usage ("\n");
198         for (c = 1; c < argc; c++) {
199                 if (strcmp ("-to", argv[c]) == 0)
200                         strcpy (argv[c], "-t");
201                 else if (strcmp ("-wd", argv[c]) == 0)
202                         strcpy (argv[c], "-w");
203                 else if (strcmp ("-cd", argv[c]) == 0)
204                         strcpy (argv[c], "-c");
205                 else if (strcmp ("-wt", argv[c]) == 0)
206                         strcpy (argv[c], "-W");
207                 else if (strcmp ("-ct", argv[c]) == 0)
208                         strcpy (argv[c], "-C");
209         }
211         while (1) {
212                 c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:u", longopts,
213                                                                          &option);
215                 if (c == -1 || c == EOF)
216                         break;
218                 switch (c) {
219                 case '?':                                                                       /* print short usage statement if args not parsable */
220                         usage3 (_("Unknown argument"), optopt);
221                 case 'h':                                                                       /* help */
222                         print_help ();
223                         exit (STATE_OK);
224                 case 'V':                                                                       /* version */
225                         print_revision (progname, revision);
226                         exit (STATE_OK);
227                 case 'H':                                                                       /* hostname */
228                         if (is_host (optarg) == FALSE)
229                                 usage2 (_("Invalid host name/address"), optarg);
230                         server_address = optarg;
231                         break;
232                 case 'w':                                                                       /* warning-variance */
233                         if (is_intnonneg (optarg)) {
234                                 warning_diff = strtoul (optarg, NULL, 10);
235                                 check_warning_diff = TRUE;
236                         }
237                         else if (strspn (optarg, "0123456789:,") > 0) {
238                                 if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
239                                         check_warning_diff = TRUE;
240                                         check_warning_time = TRUE;
241                                 }
242                                 else {
243                                         usage (_("Warning thresholds must be a nonnegative integer\n"));
244                                 }
245                         }
246                         else {
247                                 usage (_("Warning threshold must be a nonnegative integer\n"));
248                         }
249                         break;
250                 case 'c':                                                                       /* critical-variance */
251                         if (is_intnonneg (optarg)) {
252                                 critical_diff = strtoul (optarg, NULL, 10);
253                                 check_critical_diff = TRUE;
254                         }
255                         else if (strspn (optarg, "0123456789:,") > 0) {
256                                 if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
257                                                 2) {
258                                         check_critical_diff = TRUE;
259                                         check_critical_time = TRUE;
260                                 }
261                                 else {
262                                         usage (_("Critical thresholds must be a nonnegative integer\n"));
263                                 }
264                         }
265                         else {
266                                 usage (_("Critical threshold must be a nonnegative integer\n"));
267                         }
268                         break;
269                 case 'W':                                                                       /* warning-connect */
270                         if (!is_intnonneg (optarg))
271                                 usage (_("Warning threshold must be a nonnegative integer\n"));
272                         else
273                                 warning_time = atoi (optarg);
274                         check_warning_time = TRUE;
275                         break;
276                 case 'C':                                                                       /* critical-connect */
277                         if (!is_intnonneg (optarg))
278                                 usage (_("Critical threshold must be a nonnegative integer\n"));
279                         else
280                                 critical_time = atoi (optarg);
281                         check_critical_time = TRUE;
282                         break;
283                 case 'p':                                                                       /* port */
284                         if (!is_intnonneg (optarg))
285                                 usage (_("Server port must be a nonnegative integer\n"));
286                         else
287                                 server_port = atoi (optarg);
288                         break;
289                 case 't':                                                                       /* timeout */
290                         if (!is_intnonneg (optarg))
291                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
292                         else
293                                 socket_timeout = atoi (optarg);
294                         break;
295                 case 'u':                                                                       /* udp */
296                         use_udp = TRUE;
297                 }
298         }
300         c = optind;
301         if (server_address == NULL) {
302                 if (argc > c) {
303                         if (is_host (argv[c]) == FALSE)
304                                 usage2 (_("Invalid host name/address"), optarg);
305                         server_address = argv[c];
306                 }
307                 else {
308                         usage (_("Host name was not supplied\n"));
309                 }
310         }
312         return OK;
319 \f
320 void
321 print_help (void)
323         char *myport;
324         asprintf (&myport, "%d", TIME_PORT);
326         print_revision (progname, revision);
328         printf ("Copyright (c) 1999 Ethan Galstad\n");
329         printf (COPYRIGHT, copyright, email);
331         printf (_("\
332 This plugin will check the time on the specified host.\n\n"));
334         print_usage ();
336         printf (_(UT_HELP_VRSN));
338         printf (_(UT_HOST_PORT), 'p', myport);
340         printf (_("\
341  -u, --udp\n\
342     Use UDP to connect, not TCP\n\
343  -w, --warning-variance=INTEGER\n\
344     Time difference (sec.) necessary to result in a warning status\n\
345  -c, --critical-variance=INTEGER\n\
346     Time difference (sec.) necessary to result in a critical status\n\
347  -W, --warning-connect=INTEGER\n\
348     Response time (sec.) necessary to result in warning status\n\
349  -C, --critical-connect=INTEGER\n\
350     Response time (sec.) necessary to result in critical status\n"));
352         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
354         printf (_(UT_SUPPORT));
360 void
361 print_usage (void)
363         printf (_("\
364 Usage: %s -H <host_address> [-p port] [-u] [-w variance] [-c variance]\n\
365     [-W connect_time] [-C connect_time] [-t timeout]\n"), progname);
366         printf (_(UT_HLP_VRS), progname, progname);