Code

Fix translations when extra-opts aren't enabled
[nagiosplug.git] / plugins / check_time.c
1 /*****************************************************************************
2
3 * Nagios check_time plugin
4
5 * License: GPL
6 * Copyright (c) 1999-2007 Nagios Plugins Development Team
7
8 * Description:
9
10 * This file contains the check_time plugin
11
12 * This plugin will check the time difference with the specified host.
13
14
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 * GNU General Public License for more details.
24
25 * You should have received a copy of the GNU General Public License
26 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
28
29 *****************************************************************************/
31 const char *progname = "check_time";
32 const char *copyright = "1999-2007";
33 const char *email = "nagiosplug-devel@lists.sourceforge.net";
35 #include "common.h"
36 #include "netutils.h"
37 #include "utils.h"
39 enum {
40         TIME_PORT = 37
41 };
43 #define UNIX_EPOCH 2208988800UL
45 uint32_t raw_server_time;
46 unsigned long server_time, diff_time;
47 int warning_time = 0;
48 int check_warning_time = FALSE;
49 int critical_time = 0;
50 int check_critical_time = FALSE;
51 unsigned long warning_diff = 0;
52 int check_warning_diff = FALSE;
53 unsigned long critical_diff = 0;
54 int check_critical_diff = FALSE;
55 int server_port = TIME_PORT;
56 char *server_address = NULL;
57 int use_udp = FALSE;
59 int process_arguments (int, char **);
60 void print_help (void);
61 void print_usage (void);
63 int
64 main (int argc, char **argv)
65 {
66         int sd;
67         int result = STATE_UNKNOWN;
68         time_t conntime;
70         setlocale (LC_ALL, "");
71         bindtextdomain (PACKAGE, LOCALEDIR);
72         textdomain (PACKAGE);
74         /* Parse extra opts if any */
75         argv=np_extra_opts (&argc, argv, progname);
77         if (process_arguments (argc, argv) == ERROR)
78                 usage4 (_("Could not parse arguments"));
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 (use_udp) {
89                 result = my_udp_connect (server_address, server_port, &sd);
90         } else {
91                 result = my_tcp_connect (server_address, server_port, &sd);
92         }
94         if (result != STATE_OK) {
95                 if (check_critical_time == TRUE)
96                         result = STATE_CRITICAL;
97                 else if (check_warning_time == TRUE)
98                         result = STATE_WARNING;
99                 else
100                         result = STATE_UNKNOWN;
101                 die (result,
102                            _("TIME UNKNOWN - could not connect to server %s, port %d\n"),
103                            server_address, server_port);
104         }
106         if (use_udp) {
107                 if (send (sd, "", 0, 0) < 0) {
108                         if (check_critical_time == TRUE)
109                                 result = STATE_CRITICAL;
110                         else if (check_warning_time == TRUE)
111                                 result = STATE_WARNING;
112                         else
113                                 result = STATE_UNKNOWN;
114                         die (result,
115                           _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"),
116                           server_address, server_port);
117                 }
118         }
120         /* watch for the connection string */
121         result = recv (sd, (void *)&raw_server_time, sizeof (raw_server_time), 0);
123         /* close the connection */
124         close (sd);
126         /* reset the alarm */
127         time (&end_time);
128         alarm (0);
130         /* return a WARNING status if we couldn't read any data */
131         if (result <= 0) {
132                 if (check_critical_time == TRUE)
133                         result = STATE_CRITICAL;
134                 else if (check_warning_time == TRUE)
135                         result = STATE_WARNING;
136                 else
137                         result = STATE_UNKNOWN;
138                 die (result,
139                                                          _("TIME UNKNOWN - no data received from server %s, port %d\n"),
140                                                          server_address, server_port);
141         }
143         result = STATE_OK;
145         conntime = (end_time - start_time);
146         if (check_critical_time == TRUE && conntime > critical_time)
147                 result = STATE_CRITICAL;
148         else if (check_warning_time == TRUE && conntime > warning_time)
149                 result = STATE_WARNING;
151         if (result != STATE_OK)
152                 die (result, _("TIME %s - %d second response time|%s\n"),
153                      state_text (result), (int)conntime,
154                      perfdata ("time", (long)conntime, "s",
155                                check_warning_time, (long)warning_time,
156                                check_critical_time, (long)critical_time,
157                                TRUE, 0, FALSE, 0));
159         server_time = ntohl (raw_server_time) - UNIX_EPOCH;
160         if (server_time > (unsigned long)end_time)
161                 diff_time = server_time - (unsigned long)end_time;
162         else
163                 diff_time = (unsigned long)end_time - server_time;
165         if (check_critical_diff == TRUE && diff_time > critical_diff)
166                 result = STATE_CRITICAL;
167         else if (check_warning_diff == TRUE && diff_time > warning_diff)
168                 result = STATE_WARNING;
170         printf (_("TIME %s - %lu second time difference|%s %s\n"),
171                 state_text (result), diff_time,
172                 perfdata ("time", (long)conntime, "s",
173                           check_warning_time, (long)warning_time,
174                           check_critical_time, (long)critical_time,
175                           TRUE, 0, FALSE, 0),
176                 perfdata ("offset", diff_time, "s",
177                           check_warning_diff, warning_diff,
178                           check_critical_diff, critical_diff,
179                           TRUE, 0, FALSE, 0));
180         return result;
185 /* process command-line arguments */
186 int
187 process_arguments (int argc, char **argv)
189         int c;
191         int option = 0;
192         static struct option longopts[] = {
193                 {"hostname", required_argument, 0, 'H'},
194                 {"warning-variance", required_argument, 0, 'w'},
195                 {"critical-variance", required_argument, 0, 'c'},
196                 {"warning-connect", required_argument, 0, 'W'},
197                 {"critical-connect", required_argument, 0, 'C'},
198                 {"port", required_argument, 0, 'p'},
199                 {"udp", no_argument, 0, 'u'},
200                 {"timeout", required_argument, 0, 't'},
201                 {"version", no_argument, 0, 'V'},
202                 {"help", no_argument, 0, 'h'},
203                 {0, 0, 0, 0}
204         };
206         if (argc < 2)
207                 usage ("\n");
209         for (c = 1; c < argc; c++) {
210                 if (strcmp ("-to", argv[c]) == 0)
211                         strcpy (argv[c], "-t");
212                 else if (strcmp ("-wd", argv[c]) == 0)
213                         strcpy (argv[c], "-w");
214                 else if (strcmp ("-cd", argv[c]) == 0)
215                         strcpy (argv[c], "-c");
216                 else if (strcmp ("-wt", argv[c]) == 0)
217                         strcpy (argv[c], "-W");
218                 else if (strcmp ("-ct", argv[c]) == 0)
219                         strcpy (argv[c], "-C");
220         }
222         while (1) {
223                 c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:u", longopts,
224                                                                          &option);
226                 if (c == -1 || c == EOF)
227                         break;
229                 switch (c) {
230                 case '?':                                                                       /* print short usage statement if args not parsable */
231                         usage5 ();
232                 case 'h':                                                                       /* help */
233                         print_help ();
234                         exit (STATE_OK);
235                 case 'V':                                                                       /* version */
236                         print_revision (progname, NP_VERSION);
237                         exit (STATE_OK);
238                 case 'H':                                                                       /* hostname */
239                         if (is_host (optarg) == FALSE)
240                                 usage2 (_("Invalid hostname/address"), optarg);
241                         server_address = optarg;
242                         break;
243                 case 'w':                                                                       /* warning-variance */
244                         if (is_intnonneg (optarg)) {
245                                 warning_diff = strtoul (optarg, NULL, 10);
246                                 check_warning_diff = TRUE;
247                         }
248                         else if (strspn (optarg, "0123456789:,") > 0) {
249                                 if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
250                                         check_warning_diff = TRUE;
251                                         check_warning_time = TRUE;
252                                 }
253                                 else {
254                                         usage4 (_("Warning thresholds must be a positive integer"));
255                                 }
256                         }
257                         else {
258                                 usage4 (_("Warning threshold must be a positive integer"));
259                         }
260                         break;
261                 case 'c':                                                                       /* critical-variance */
262                         if (is_intnonneg (optarg)) {
263                                 critical_diff = strtoul (optarg, NULL, 10);
264                                 check_critical_diff = TRUE;
265                         }
266                         else if (strspn (optarg, "0123456789:,") > 0) {
267                                 if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
268                                                 2) {
269                                         check_critical_diff = TRUE;
270                                         check_critical_time = TRUE;
271                                 }
272                                 else {
273                                         usage4 (_("Critical thresholds must be a positive integer"));
274                                 }
275                         }
276                         else {
277                                 usage4 (_("Critical threshold must be a positive integer"));
278                         }
279                         break;
280                 case 'W':                                                                       /* warning-connect */
281                         if (!is_intnonneg (optarg))
282                                 usage4 (_("Warning threshold must be a positive integer"));
283                         else
284                                 warning_time = atoi (optarg);
285                         check_warning_time = TRUE;
286                         break;
287                 case 'C':                                                                       /* critical-connect */
288                         if (!is_intnonneg (optarg))
289                                 usage4 (_("Critical threshold must be a positive integer"));
290                         else
291                                 critical_time = atoi (optarg);
292                         check_critical_time = TRUE;
293                         break;
294                 case 'p':                                                                       /* port */
295                         if (!is_intnonneg (optarg))
296                                 usage4 (_("Port must be a positive integer"));
297                         else
298                                 server_port = atoi (optarg);
299                         break;
300                 case 't':                                                                       /* timeout */
301                         if (!is_intnonneg (optarg))
302                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
303                         else
304                                 socket_timeout = atoi (optarg);
305                         break;
306                 case 'u':                                                                       /* udp */
307                         use_udp = TRUE;
308                 }
309         }
311         c = optind;
312         if (server_address == NULL) {
313                 if (argc > c) {
314                         if (is_host (argv[c]) == FALSE)
315                                 usage2 (_("Invalid hostname/address"), optarg);
316                         server_address = argv[c];
317                 }
318                 else {
319                         usage4 (_("Hostname was not supplied"));
320                 }
321         }
323         return OK;
328 void
329 print_help (void)
331         char *myport;
332         asprintf (&myport, "%d", TIME_PORT);
334         print_revision (progname, NP_VERSION);
336         printf ("Copyright (c) 1999 Ethan Galstad\n");
337         printf (COPYRIGHT, copyright, email);
339         printf ("%s\n", _("This plugin will check the time on the specified host."));
341   printf ("\n\n");
343         print_usage ();
345         printf (UT_HELP_VRSN);
346         printf (UT_EXTRA_OPTS);
348         printf (UT_HOST_PORT, 'p', myport);
350         printf (" %s\n", "-u, --udp");
351   printf ("   %s\n", _("Use UDP to connect, not TCP"));
352   printf (" %s\n", "-w, --warning-variance=INTEGER");
353   printf ("   %s\n", _("Time difference (sec.) necessary to result in a warning status"));
354   printf (" %s\n", "-c, --critical-variance=INTEGER");
355   printf ("   %s\n", _("Time difference (sec.) necessary to result in a critical status"));
356   printf (" %s\n", "-W, --warning-connect=INTEGER");
357   printf ("   %s\n", _("Response time (sec.) necessary to result in warning status"));
358   printf (" %s\n", "-C, --critical-connect=INTEGER");
359   printf ("   %s\n", _("Response time (sec.) necessary to result in critical status"));
361         printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
363 #ifdef NP_EXTRA_OPTS
364         printf ("\n");
365         printf ("%s\n", _("Notes:"));
366         printf (UT_EXTRA_OPTS_NOTES);
367 #endif
369         printf (UT_SUPPORT);
374 void
375 print_usage (void)
377   printf (_("Usage:"));
378         printf ("%s -H <host_address> [-p port] [-u] [-w variance] [-c variance]\n",progname);
379   printf (" [-W connect_time] [-C connect_time] [-t timeout]\n");