Code

- bindtextdomain for gettext, a few other smale cleanups here and there
[nagiosplug.git] / plugins / check_time.c
index 36b622fbfa3808de9170d3a4ee90a8dbad2da1cf..f73005d5f3aaab0be2f64ab5b97553741fedae15 100644 (file)
@@ -43,6 +43,7 @@ unsigned long critical_diff = 0;
 int check_critical_diff = FALSE;
 int server_port = TIME_PORT;
 char *server_address = NULL;
+int use_udp = FALSE;
 
 int process_arguments (int, char **);
 void print_help (void);
@@ -54,6 +55,10 @@ main (int argc, char **argv)
        int sd;
        int result;
 
+       setlocale (LC_ALL, "");
+       bindtextdomain (PACKAGE, LOCALEDIR);
+       textdomain (PACKAGE);
+
        if (process_arguments (argc, argv) != OK)
                usage (_("Invalid command arguments supplied\n"));
 
@@ -65,7 +70,13 @@ main (int argc, char **argv)
        time (&start_time);
 
        /* try to connect to the host at the given port number */
-       if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK) {
+       if (use_udp) {
+               result = my_udp_connect (server_address, server_port, &sd);
+       } else {
+               result = my_tcp_connect (server_address, server_port, &sd);
+       }
+
+       if (result != STATE_OK) {
                if (check_critical_time == TRUE)
                        result = STATE_CRITICAL;
                else if (check_warning_time == TRUE)
@@ -77,6 +88,20 @@ main (int argc, char **argv)
                           server_address, server_port);
        }
 
+       if (use_udp) {
+               if (send (sd, "", 0, 0) < 0) {
+                       if (check_critical_time == TRUE)
+                               result = STATE_CRITICAL;
+                       else if (check_warning_time == TRUE)
+                               result = STATE_WARNING;
+                       else
+                               result = STATE_UNKNOWN;
+                       die (result, 
+                         _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"),
+                         server_address, server_port);
+               }
+       }
+
        /* watch for the connection string */
        result = recv (sd, (void *)&raw_server_time, sizeof (raw_server_time), 0);
 
@@ -138,14 +163,15 @@ process_arguments (int argc, char **argv)
 {
        int c;
 
-       int option_index = 0;
-       static struct option long_options[] = {
+       int option = 0;
+       static struct option longopts[] = {
                {"hostname", required_argument, 0, 'H'},
                {"warning-variance", required_argument, 0, 'w'},
                {"critical-variance", required_argument, 0, 'c'},
                {"warning-connect", required_argument, 0, 'W'},
                {"critical-connect", required_argument, 0, 'C'},
                {"port", required_argument, 0, 'p'},
+               {"udp", no_argument, 0, 'u'},
                {"timeout", required_argument, 0, 't'},
                {"version", no_argument, 0, 'V'},
                {"help", no_argument, 0, 'h'},
@@ -169,8 +195,8 @@ process_arguments (int argc, char **argv)
        }
 
        while (1) {
-               c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:", long_options,
-                                                                        &option_index);
+               c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:u", longopts,
+                                                                        &option);
 
                if (c == -1 || c == EOF)
                        break;
@@ -252,6 +278,8 @@ process_arguments (int argc, char **argv)
                        else
                                socket_timeout = atoi (optarg);
                        break;
+               case 'u':                                                                       /* udp */
+                       use_udp = TRUE;
                }
        }
 
@@ -296,6 +324,8 @@ This plugin will check the time on the specified host.\n\n"));
        printf (_(UT_HOST_PORT), 'p', myport);
 
        printf (_("\
+ -u, --udp\n\
+    Use UDP to connect, not TCP\n\
  -w, --warning-variance=INTEGER\n\
     Time difference (sec.) necessary to result in a warning status\n\
  -c, --critical-variance=INTEGER\n\
@@ -307,7 +337,7 @@ This plugin will check the time on the specified host.\n\n"));
 
        printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
 
-       support ();
+       printf (_(UT_SUPPORT));
 }
 
 
@@ -317,7 +347,7 @@ void
 print_usage (void)
 {
        printf (_("\
-Usage: %s -H <host_address> [-p port] [-w variance] [-c variance]\n\
+Usage: %s -H <host_address> [-p port] [-u] [-w variance] [-c variance]\n\
     [-W connect_time] [-C connect_time] [-t timeout]\n"), progname);
        printf (_(UT_HLP_VRS), progname, progname);
 }