Code

Fix for regex input of '|', being output causing problems with Nagios' parsing of
[nagiosplug.git] / plugins / check_dig.c
index c9fce613b9266b792d9ed4482042577bcd8e86ee..06f59c80ba2058b328894c6ef743d05ef60bf5e3 100644 (file)
@@ -1,21 +1,29 @@
 /*****************************************************************************
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
- $Id$
+* 
+* Nagios check_dig plugin
+* 
+* License: GPL
+* Copyright (c) 2002-2008 Nagios Plugins Development Team
+* 
+* Description:
+* 
+* This file contains the check_dig plugin
+* 
+* 
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+* 
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+* 
+* You should have received a copy of the GNU General Public License
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
+* 
+* 
 *****************************************************************************/
 
 /* Hackers note:
@@ -25,8 +33,7 @@
  *  because on some architectures those strings are in non-writable memory */
 
 const char *progname = "check_dig";
-const char *revision = "$Revision$";
-const char *copyright = "2002-2005";
+const char *copyright = "2002-2008";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 #include "common.h"
@@ -46,6 +53,7 @@ char *query_address = NULL;
 char *record_type = "A";
 char *expected_address = NULL;
 char *dns_server = NULL;
+char *dig_args = "";
 int verbose = FALSE;
 int server_port = DEFAULT_PORT;
 double warning_interval = UNDEFINED;
@@ -72,12 +80,15 @@ main (int argc, char **argv)
   if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
     usage_va(_("Cannot catch SIGALRM"));
 
+  /* Parse extra opts if any */
+  argv=np_extra_opts (&argc, argv, progname);
+
   if (process_arguments (argc, argv) == ERROR)
     usage_va(_("Could not parse arguments"));
 
   /* get the command to run */
-  asprintf (&command_line, "%s @%s -p %d %s -t %s",
-            PATH_TO_DIG, dns_server, server_port, query_address, record_type);
+  asprintf (&command_line, "%s @%s -p %d %s -t %s %s",
+            PATH_TO_DIG, dns_server, server_port, query_address, record_type, dig_args);
 
   alarm (timeout_interval);
   gettimeofday (&tv, NULL);
@@ -128,8 +139,10 @@ main (int argc, char **argv)
     }
   }
 
-  if (result == STATE_UNKNOWN)
+  if (result == STATE_UNKNOWN) {
     msg = (char *)_("No ANSWER SECTION found");
+    result = STATE_CRITICAL;
+  }
 
   /* If we get anything on STDERR, at least set warning */
   if(chld_err.buflen > 0) {
@@ -179,11 +192,13 @@ process_arguments (int argc, char **argv)
     {"warning", required_argument, 0, 'w'},
     {"critical", required_argument, 0, 'c'},
     {"timeout", required_argument, 0, 't'},
+    {"dig-arguments", required_argument, 0, 'A'},
     {"verbose", no_argument, 0, 'v'},
     {"version", no_argument, 0, 'V'},
     {"help", no_argument, 0, 'h'},
     {"record_type", required_argument, 0, 'T'},
     {"expected_address", required_argument, 0, 'a'},
+    {"port", required_argument, 0, 'p'},
     {0, 0, 0, 0}
   };
 
@@ -191,7 +206,7 @@ process_arguments (int argc, char **argv)
     return ERROR;
 
   while (1) {
-    c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:a:", longopts, &option);
+    c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:A:", longopts, &option);
 
     if (c == -1 || c == EOF)
       break;
@@ -201,7 +216,7 @@ process_arguments (int argc, char **argv)
       print_help ();
       exit (STATE_OK);
     case 'V':                 /* version */
-      print_revision (progname, revision);
+      print_revision (progname, NP_VERSION);
       exit (STATE_OK);
     case 'H':                 /* hostname */
       host_or_die(optarg);
@@ -242,6 +257,9 @@ process_arguments (int argc, char **argv)
         usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
       }
       break;
+    case 'A':                 /* dig arguments */
+      dig_args = strdup(optarg);
+      break;
     case 'v':                 /* verbose */
       verbose = TRUE;
       break;
@@ -251,8 +269,8 @@ process_arguments (int argc, char **argv)
     case 'a':
       expected_address = optarg;
       break;
-    default:                  /* usage_va */
-      usage_va(_("Unknown argument - %s"), optarg);
+    default:                  /* usage5 */
+      usage5();
     }
   }
 
@@ -275,7 +293,10 @@ process_arguments (int argc, char **argv)
 int
 validate_arguments (void)
 {
-  return OK;
+  if (query_address != NULL)
+    return OK;
+  else
+    return ERROR;
 }
 
 
@@ -287,7 +308,7 @@ print_help (void)
 
   asprintf (&myport, "%d", DEFAULT_PORT);
 
-  print_revision (progname, revision);
+  print_revision (progname, NP_VERSION);
 
   printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
   printf (COPYRIGHT, copyright, email);
@@ -298,41 +319,31 @@ print_help (void)
 
   print_usage ();
 
-  printf (_(UT_HELP_VRSN));
-
-  printf (_(UT_HOST_PORT), 'P', myport);
+  printf (UT_HELP_VRSN);
 
-  printf ("-l, --lookup=STRING");
-    
-  printf ("\n");
-
-  printf (_("machine name to lookup"));
+  printf (UT_EXTRA_OPTS);
 
-  printf ("\n");
+  printf (UT_HOST_PORT, 'p', myport);
 
-  printf ("-T, --record_type=STRING");
+  printf (" %s\n","-l, --query_address=STRING");
+  printf ("    %s\n",_("Machine name to lookup"));
+  printf (" %s\n","-T, --record_type=STRING");
+  printf ("    %s\n",_("Record type to lookup (default: A)"));
+  printf (" %s\n","-a, --expected_address=STRING");
+  printf ("    %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
+  printf ("    %s\n",_("was in -l"));
+  printf (" %s\n","-A, --dig-arguments=STRING");
+  printf ("    %s\n",_("Pass STRING as argument(s) to dig"));
+  printf (UT_WARN_CRIT);
+  printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
+  printf (UT_VERBOSE);
 
   printf ("\n");
+  printf ("%s\n", _("Examples:"));
+  printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
+  printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
 
-  printf (_("record type to lookup (default: A)"));
-
-  printf ("\n");
-
-  printf ("-a, --expected_address=STRING");
-
-  printf ("\n");
-
-  printf (_("an address expected to be in the answer section.if not set, uses whatever was in -l"));
-
-  printf ("\n");
-
-  printf (_(UT_WARN_CRIT));
-
-  printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
-
-  printf (_(UT_VERBOSE));
-
-  printf (_(UT_SUPPORT));
+  printf (UT_SUPPORT);
 }
 
 
@@ -340,8 +351,8 @@ print_help (void)
 void
 print_usage (void)
 {
-  printf (_("Usage:"));
-  printf ("%s -H host -l lookup [-p <server port>] [-T <query type>]", progname);
-  printf ("          [-w <warning interval>] [-c <critical interval>] [-t <timeout>]");
-  printf ("          [-a <expected answer address>] [-v]\n");
+  printf ("%s\n", _("Usage:"));
+  printf ("%s -l <query_address> [-H <host>] [-p <server port>]\n", progname);
+  printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
+  printf (" [-t <timeout>] [-a <expected answer address>] [-v]\n");
 }