Code

remove overflow checks of strtod - HUGE_VALF requires C99; also remove equality check...
[nagiosplug.git] / plugins / check_dig.c
index ff16059158cdcf4dbd41766a0c820fefb07d9a10..e348a352bec09323eeef97c134f70f43e21e1871 100644 (file)
@@ -32,6 +32,7 @@ const char *copyright = "2002-2003";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 enum {
+       UNDEFINED = 0,
        DEFAULT_PORT = 53
 };
 
@@ -39,24 +40,26 @@ char *query_address = NULL;
 char *dns_server = NULL;
 int verbose = FALSE;
 int server_port = DEFAULT_PORT;
-int warning_interval = -1;
-int critical_interval = -1;
+double warning_interval = UNDEFINED;
+double critical_interval = UNDEFINED;
+struct timeval tv;
 
-
-
-
-
-\f
 int
 main (int argc, char **argv)
 {
        char input_buffer[MAX_INPUT_BUFFER];
        char *command_line;
        char *output;
+       long microsec;
+       double elapsed_time;
        int result = STATE_UNKNOWN;
 
        output = strdup ("");
 
+       setlocale (LC_ALL, "");
+       bindtextdomain (PACKAGE, LOCALEDIR);
+       textdomain (PACKAGE);
+
        /* Set signal handling and alarm */
        if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
                usage (_("Cannot catch SIGALRM\n"));
@@ -69,10 +72,11 @@ main (int argc, char **argv)
                  PATH_TO_DIG, dns_server, server_port, query_address);
 
        alarm (timeout_interval);
-       time (&start_time);
+       gettimeofday (&tv, NULL);
 
        if (verbose)
                printf ("%s\n", command_line);
+
        /* run the command */
        child_process = spopen (command_line);
        if (child_process == NULL) {
@@ -131,21 +135,28 @@ main (int argc, char **argv)
                        asprintf (&output, _("dig returned error status"));
        }
 
-       (void) time (&end_time);
+       microsec = deltime (tv);
+       elapsed_time = (double)microsec / 1.0e6;
 
        if (output == NULL || strlen (output) == 0)
                asprintf (&output, _(" Probably a non-existent host/domain"));
 
-       if (result == STATE_OK)
-               printf (_("DNS OK - %d seconds response time (%s)\n"),
-                                               (int) (end_time - start_time), output);
-       else if (result == STATE_WARNING)
-               printf (_("DNS WARNING - %s\n"), output);
-       else if (result == STATE_CRITICAL)
-               printf (_("DNS CRITICAL - %s\n"), output);
-       else
-               printf (_("DNS problem - %s\n"), output);
+       if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
+               result = STATE_CRITICAL;
+
+       else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
+               result = STATE_WARNING;
+
+       asprintf (&output, _("%.3f seconds response time (%s)"), elapsed_time, output);
 
+       printf ("DNS %s - %s|%s\n",
+               state_text (result), output,
+               perfdata("time", microsec, "us",
+                        (warning_interval>UNDEFINED?TRUE:FALSE),
+                        (int)(1e6*warning_interval),
+                        (critical_interval>UNDEFINED?TRUE:FALSE),
+                        (int)(1e6*critical_interval),
+                                                                        TRUE, 0, FALSE, 0));
        return result;
 }
 
@@ -160,10 +171,13 @@ 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'},
-               {"query_address", required_argument, 0, 'e'},
+               {"query_address", required_argument, 0, 'l'},
+               {"warning", required_argument, 0, 'w'},
+               {"critical", required_argument, 0, 'c'},
+               {"timeout", required_argument, 0, 't'},
                {"verbose", no_argument, 0, 'v'},
                {"version", no_argument, 0, 'V'},
                {"help", no_argument, 0, 'h'},
@@ -174,7 +188,7 @@ process_arguments (int argc, char **argv)
                return ERROR;
 
        while (1) {
-               c = getopt_long (argc, argv, "hVvt:l:H:", long_options, &option_index);
+               c = getopt_long (argc, argv, "hVvt:l:H:w:c:", longopts, &option);
 
                if (c == -1 || c == EOF)
                        break;
@@ -196,31 +210,31 @@ process_arguments (int argc, char **argv)
                                usage2 (_("Invalid host name"), optarg);
                        }
                        break;
-               case 'p':
+               case 'p':                 /* server port */
                        if (is_intpos (optarg)) {
                                server_port = atoi (optarg);
                        }
                        else {
-                               usage2 (_("Server port must be a nonnegative integer\n"), optarg);
+                               usage2 (_("Server port must be a nonnegative integer"), optarg);
                        }
                        break;
-               case 'l':                                                                       /* username */
+               case 'l':                                                                       /* address to lookup */
                        query_address = optarg;
                        break;
-               case 'w':                                                                       /* timeout */
-                       if (is_intnonneg (optarg)) {
-                               warning_interval = atoi (optarg);
+               case 'w':                                                                       /* warning */
+                       if (is_nonnegative (optarg)) {
+                               warning_interval = strtod (optarg, NULL);
                        }
                        else {
-                               usage2 (_("Warning interval must be a nonnegative integer\n"), optarg);
+                               usage2 (_("Warning interval must be a nonnegative integer"), optarg);
                        }
                        break;
-               case 'c':                                                                       /* timeout */
-                       if (is_intnonneg (optarg)) {
-                               critical_interval = atoi (optarg);
+               case 'c':                                                                       /* critical */
+                       if (is_nonnegative (optarg)) {
+                               critical_interval = strtod (optarg, NULL);
                        }
                        else {
-                               usage2 (_("Critical interval must be a nonnegative integer\n"), optarg);
+                               usage2 (_("Critical interval must be a nonnegative integer"), optarg);
                        }
                        break;
                case 't':                                                                       /* timeout */
@@ -228,7 +242,7 @@ process_arguments (int argc, char **argv)
                                timeout_interval = atoi (optarg);
                        }
                        else {
-                               usage2 (_("Time interval must be a nonnegative integer\n"), optarg);
+                               usage2 (_("Time interval must be a nonnegative integer"), optarg);
                        }
                        break;
                case 'v':                                                                       /* verbose */
@@ -280,6 +294,7 @@ print_help (void)
 
        print_revision (progname, revision);
 
+       printf (_("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"));
        printf (_(COPYRIGHT), copyright, email);
 
        printf (_("Test the DNS service on the specified host using dig\n\n"));
@@ -300,7 +315,7 @@ print_help (void)
 
        printf (_(UT_VERBOSE));
 
-       support ();
+       printf (_(UT_SUPPORT));
 }