From 63271bf65d7f6da74e832e16217bac5778d21d6b Mon Sep 17 00:00:00 2001
From: Ton Voon <tonvoon@users.sourceforge.net>
Date: Wed, 22 Mar 2006 16:45:57 +0000
Subject: Added warning and critical response times (John Rouillard - 1343159)

git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1346 f882894a-f735-0410-b71e-b25c423dba1c

diff --git a/plugins/check_dns.c b/plugins/check_dns.c
index 2a3e376..3b94a7a 100644
--- a/plugins/check_dns.c
+++ b/plugins/check_dns.c
@@ -45,6 +45,7 @@ int verbose = FALSE;
 char expected_address[ADDRESS_LENGTH] = "";
 int match_expected_address = FALSE;
 int expect_authority = FALSE;
+thresholds *time_thresholds = NULL;
 
 int
 main (int argc, char **argv)
@@ -183,7 +184,14 @@ main (int argc, char **argv)
     else
       multi_address = TRUE;
 
-    printf ("DNS %s: ", _("OK"));
+    result = get_status(elapsed_time, time_thresholds);
+    if (result == STATE_OK) {
+      printf ("DNS %s: ", _("OK"));
+    } else if (result == STATE_WARNING) {
+      printf ("DNS %s: ", _("WARNING"));
+    } else if (result == STATE_CRITICAL) {
+      printf ("DNS %s: ", _("CRITICAL"));
+    }
     printf (ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), elapsed_time);
     printf (_(". %s returns %s"), query_address, address);
     printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
@@ -266,6 +274,8 @@ int
 process_arguments (int argc, char **argv)
 {
   int c;
+  char *warning = NULL;
+  char *critical = NULL;
 
   int opt_index = 0;
   static struct option long_opts[] = {
@@ -278,6 +288,8 @@ process_arguments (int argc, char **argv)
     {"reverse-server", required_argument, 0, 'r'},
     {"expected-address", required_argument, 0, 'a'},
     {"expect-authority", no_argument, 0, 'A'},
+    {"warning", no_argument, 0, 'w'},
+    {"critical", no_argument, 0, 'c'},
     {0, 0, 0, 0}
   };
 
@@ -289,7 +301,7 @@ process_arguments (int argc, char **argv)
       strcpy (argv[c], "-t");
 
   while (1) {
-    c = getopt_long (argc, argv, "hVvAt:H:s:r:a:", long_opts, &opt_index);
+    c = getopt_long (argc, argv, "hVvAt:H:s:r:a:w:c:", long_opts, &opt_index);
 
     if (c == -1 || c == EOF)
       break;
@@ -336,6 +348,12 @@ process_arguments (int argc, char **argv)
     case 'A': /* expect authority */
       expect_authority = TRUE;
       break;
+    case 'w':
+      warning = optarg;
+      break;
+    case 'c':
+      critical = optarg;
+      break;
     default: /* args not parsable */
       usage_va(_("Unknown argument - %s"), optarg);
     }
@@ -356,6 +374,8 @@ process_arguments (int argc, char **argv)
     strcpy (dns_server, argv[c++]);
   }
 
+  set_thresholds(&time_thresholds, warning, critical);
+
   return validate_arguments ();
 }
 
@@ -395,6 +415,10 @@ print_help (void)
   printf ("    %s\n", _("Optional IP-ADDRESS you expect the DNS server to return. HOST must end with ."));
   printf (" -A, --expect-authority\n");
   printf ("    %s\n", _("Optionally expect the DNS server to be authoritative for the lookup"));
+  printf (" -w, --warning=seconds\n");
+  printf ("    %s\n", _("Return warning if elapsed time exceeds value. Default off"));
+  printf (" -c, --critical=seconds\n");
+  printf ("    %s\n", _("Return critical if elapsed time exceeds value. Default off"));
 
   printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
   printf (_(UT_SUPPORT));
@@ -406,5 +430,5 @@ print_usage (void)
 {
   printf (_("Usage:"));
   
-  printf ("%s -H host [-s server] [-a expected-address] [-A] [-t timeout]\n", progname);
+  printf ("%s -H host [-s server] [-a expected-address] [-A] [-t timeout] [-w warn] [-c crit]\n", progname);
 }
diff --git a/plugins/t/check_dns.t b/plugins/t/check_dns.t
index 5d750d3..9a95e80 100644
--- a/plugins/t/check_dns.t
+++ b/plugins/t/check_dns.t
@@ -11,7 +11,7 @@ use NPTest;
 
 plan skip_all => "check_dns not compiled" unless (-x "check_dns");
 
-plan tests => 11;
+plan tests => 13;
 
 my $successOutput = '/DNS OK: [\.0-9]+ seconds response time/';
 
@@ -50,6 +50,12 @@ $res = NPTest->testCmd("./check_dns -H $hostname_valid -t 5");
 cmp_ok( $res->return_code, '==', 0, "Found $hostname_valid");
 like  ( $res->output, $successOutput, "Output OK" );
 
+$res = NPTest->testCmd("./check_dns -H $hostname_valid -t 5 -w 0 -c 0");
+cmp_ok( $res->return_code, '==', 2, "Critical threshold passed");
+
+$res = NPTest->testCmd("./check_dns -H $hostname_valid -t 5 -w 0 -c 5");
+cmp_ok( $res->return_code, '==', 1, "Warning threshold passed");
+
 $res = NPTest->testCmd("./check_dns -H $hostname_invalid -t 1");
 cmp_ok( $res->return_code, '==', 2, "Invalid $hostname_invalid");
 
-- 
cgit v0.10-9-g596f

