Code

remove overflow checks of strtod - HUGE_VALF requires C99; also remove equality check...
[nagiosplug.git] / plugins / check_dig.c
index dc4f89ae529a29043e0a51fc0120a5926ad2525d..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,8 +40,9 @@ 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;
 
 int
 main (int argc, char **argv)
@@ -48,6 +50,8 @@ 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 ("");
@@ -68,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) {
@@ -130,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;
 }
 
@@ -162,7 +174,10 @@ process_arguments (int argc, char **argv)
        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'},
@@ -173,7 +188,7 @@ process_arguments (int argc, char **argv)
                return ERROR;
 
        while (1) {
-               c = getopt_long (argc, argv, "hVvt:l:H:", longopts, &option);
+               c = getopt_long (argc, argv, "hVvt:l:H:w:c:", longopts, &option);
 
                if (c == -1 || c == EOF)
                        break;
@@ -195,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 */
@@ -227,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 */