Back to index

nagios-plugins  1.4.16
check_dig.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * Nagios check_dig plugin
00004 * 
00005 * License: GPL
00006 * Copyright (c) 2002-2008 Nagios Plugins Development Team
00007 * 
00008 * Description:
00009 * 
00010 * This file contains the check_dig plugin
00011 * 
00012 * 
00013 * This program is free software: you can redistribute it and/or modify
00014 * it under the terms of the GNU General Public License as published by
00015 * the Free Software Foundation, either version 3 of the License, or
00016 * (at your option) any later version.
00017 * 
00018 * This program is distributed in the hope that it will be useful,
00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021 * GNU General Public License for more details.
00022 * 
00023 * You should have received a copy of the GNU General Public License
00024 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025 * 
00026 * 
00027 *****************************************************************************/
00028 
00029 /* Hackers note:
00030  *  There are typecasts to (char *) from _("foo bar") in this file.
00031  *  They prevent compiler warnings. Never (ever), permute strings obtained
00032  *  that are typecast from (const char *) (which happens when --disable-nls)
00033  *  because on some architectures those strings are in non-writable memory */
00034 
00035 const char *progname = "check_dig";
00036 const char *copyright = "2002-2008";
00037 const char *email = "nagiosplug-devel@lists.sourceforge.net";
00038 
00039 #include "common.h"
00040 #include "netutils.h"
00041 #include "utils.h"
00042 #include "runcmd.h"
00043 
00044 int process_arguments (int, char **);
00045 int validate_arguments (void);
00046 void print_help (void);
00047 void print_usage (void);
00048 
00049 #define UNDEFINED 0
00050 #define DEFAULT_PORT 53
00051 
00052 char *query_address = NULL;
00053 char *record_type = "A";
00054 char *expected_address = NULL;
00055 char *dns_server = NULL;
00056 char *dig_args = "";
00057 int verbose = FALSE;
00058 int server_port = DEFAULT_PORT;
00059 double warning_interval = UNDEFINED;
00060 double critical_interval = UNDEFINED;
00061 struct timeval tv;
00062 
00063 int
00064 main (int argc, char **argv)
00065 {
00066   char *command_line;
00067   output chld_out, chld_err;
00068   char *msg = NULL;
00069   size_t i;
00070   char *t;
00071   long microsec;
00072   double elapsed_time;
00073   int result = STATE_UNKNOWN;
00074 
00075   setlocale (LC_ALL, "");
00076   bindtextdomain (PACKAGE, LOCALEDIR);
00077   textdomain (PACKAGE);
00078 
00079   /* Set signal handling and alarm */
00080   if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
00081     usage_va(_("Cannot catch SIGALRM"));
00082 
00083   /* Parse extra opts if any */
00084   argv=np_extra_opts (&argc, argv, progname);
00085 
00086   if (process_arguments (argc, argv) == ERROR)
00087     usage_va(_("Could not parse arguments"));
00088 
00089   /* get the command to run */
00090   asprintf (&command_line, "%s @%s -p %d %s -t %s %s",
00091             PATH_TO_DIG, dns_server, server_port, query_address, record_type, dig_args);
00092 
00093   alarm (timeout_interval);
00094   gettimeofday (&tv, NULL);
00095 
00096   if (verbose) {
00097     printf ("%s\n", command_line);
00098     if(expected_address != NULL) {
00099       printf (_("Looking for: '%s'\n"), expected_address);
00100     } else {
00101       printf (_("Looking for: '%s'\n"), query_address);
00102     }
00103   }
00104 
00105   /* run the command */
00106   if(np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
00107     result = STATE_WARNING;
00108     msg = (char *)_("dig returned an error status");
00109   }
00110 
00111   for(i = 0; i < chld_out.lines; i++) {
00112     /* the server is responding, we just got the host name... */
00113     if (strstr (chld_out.line[i], ";; ANSWER SECTION:")) {
00114 
00115       /* loop through the whole 'ANSWER SECTION' */
00116       for(; i < chld_out.lines; i++) {
00117         /* get the host address */
00118         if (verbose)
00119           printf ("%s\n", chld_out.line[i]);
00120 
00121         if (strstr (chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) {
00122           msg = chld_out.line[i];
00123           result = STATE_OK;
00124 
00125           /* Translate output TAB -> SPACE */
00126           t = msg;
00127           while ((t = strchr(t, '\t')) != NULL) *t = ' ';
00128           break;
00129         }
00130       }
00131 
00132       if (result == STATE_UNKNOWN) {
00133         msg = (char *)_("Server not found in ANSWER SECTION");
00134         result = STATE_WARNING;
00135       }
00136 
00137       /* we found the answer section, so break out of the loop */
00138       break;
00139     }
00140   }
00141 
00142   if (result == STATE_UNKNOWN) {
00143     msg = (char *)_("No ANSWER SECTION found");
00144     result = STATE_CRITICAL;
00145   }
00146 
00147   /* If we get anything on STDERR, at least set warning */
00148   if(chld_err.buflen > 0) {
00149     result = max_state(result, STATE_WARNING);
00150     if(!msg) for(i = 0; i < chld_err.lines; i++) {
00151       msg = strchr(chld_err.line[0], ':');
00152       if(msg) {
00153         msg++;
00154         break;
00155       }
00156     }
00157   }
00158 
00159   microsec = deltime (tv);
00160   elapsed_time = (double)microsec / 1.0e6;
00161 
00162   if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
00163     result = STATE_CRITICAL;
00164 
00165   else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
00166     result = STATE_WARNING;
00167 
00168   printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
00169           state_text (result), elapsed_time,
00170           msg ? msg : _("Probably a non-existent host/domain"),
00171           fperfdata("time", elapsed_time, "s",
00172                     (warning_interval>UNDEFINED?TRUE:FALSE),
00173                     warning_interval,
00174                     (critical_interval>UNDEFINED?TRUE:FALSE),
00175             critical_interval,
00176             TRUE, 0, FALSE, 0));
00177   return result;
00178 }
00179 
00180 
00181 
00182 /* process command-line arguments */
00183 int
00184 process_arguments (int argc, char **argv)
00185 {
00186   int c;
00187 
00188   int option = 0;
00189   static struct option longopts[] = {
00190     {"hostname", required_argument, 0, 'H'},
00191     {"query_address", required_argument, 0, 'l'},
00192     {"warning", required_argument, 0, 'w'},
00193     {"critical", required_argument, 0, 'c'},
00194     {"timeout", required_argument, 0, 't'},
00195     {"dig-arguments", required_argument, 0, 'A'},
00196     {"verbose", no_argument, 0, 'v'},
00197     {"version", no_argument, 0, 'V'},
00198     {"help", no_argument, 0, 'h'},
00199     {"record_type", required_argument, 0, 'T'},
00200     {"expected_address", required_argument, 0, 'a'},
00201     {"port", required_argument, 0, 'p'},
00202     {0, 0, 0, 0}
00203   };
00204 
00205   if (argc < 2)
00206     return ERROR;
00207 
00208   while (1) {
00209     c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:A:", longopts, &option);
00210 
00211     if (c == -1 || c == EOF)
00212       break;
00213 
00214     switch (c) {
00215     case 'h':                 /* help */
00216       print_help ();
00217       exit (STATE_OK);
00218     case 'V':                 /* version */
00219       print_revision (progname, NP_VERSION);
00220       exit (STATE_OK);
00221     case 'H':                 /* hostname */
00222       host_or_die(optarg);
00223       dns_server = optarg;
00224       break;
00225     case 'p':                 /* server port */
00226       if (is_intpos (optarg)) {
00227         server_port = atoi (optarg);
00228       }
00229       else {
00230         usage_va(_("Port must be a positive integer - %s"), optarg);
00231       }
00232       break;
00233     case 'l':                 /* address to lookup */
00234       query_address = optarg;
00235       break;
00236     case 'w':                 /* warning */
00237       if (is_nonnegative (optarg)) {
00238         warning_interval = strtod (optarg, NULL);
00239       }
00240       else {
00241         usage_va(_("Warning interval must be a positive integer - %s"), optarg);
00242       }
00243       break;
00244     case 'c':                 /* critical */
00245       if (is_nonnegative (optarg)) {
00246         critical_interval = strtod (optarg, NULL);
00247       }
00248       else {
00249         usage_va(_("Critical interval must be a positive integer - %s"), optarg);
00250       }
00251       break;
00252     case 't':                 /* timeout */
00253       if (is_intnonneg (optarg)) {
00254         timeout_interval = atoi (optarg);
00255       }
00256       else {
00257         usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
00258       }
00259       break;
00260     case 'A':                 /* dig arguments */
00261       dig_args = strdup(optarg);
00262       break;
00263     case 'v':                 /* verbose */
00264       verbose = TRUE;
00265       break;
00266     case 'T':
00267       record_type = optarg;
00268       break;
00269     case 'a':
00270       expected_address = optarg;
00271       break;
00272     default:                  /* usage5 */
00273       usage5();
00274     }
00275   }
00276 
00277   c = optind;
00278   if (dns_server == NULL) {
00279     if (c < argc) {
00280       host_or_die(argv[c]);
00281       dns_server = argv[c];
00282     }
00283     else {
00284       dns_server = strdup ("127.0.0.1");
00285     }
00286   }
00287 
00288   return validate_arguments ();
00289 }
00290 
00291 
00292 
00293 int
00294 validate_arguments (void)
00295 {
00296   if (query_address != NULL)
00297     return OK;
00298   else
00299     return ERROR;
00300 }
00301 
00302 
00303 
00304 void
00305 print_help (void)
00306 {
00307   char *myport;
00308 
00309   asprintf (&myport, "%d", DEFAULT_PORT);
00310 
00311   print_revision (progname, NP_VERSION);
00312 
00313   printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
00314   printf (COPYRIGHT, copyright, email);
00315 
00316   printf (_("This plugin test the DNS service on the specified host using dig"));
00317 
00318   printf ("\n\n");
00319 
00320   print_usage ();
00321 
00322   printf (UT_HELP_VRSN);
00323 
00324   printf (UT_EXTRA_OPTS);
00325 
00326   printf (UT_HOST_PORT, 'p', myport);
00327 
00328   printf (" %s\n","-l, --query_address=STRING");
00329   printf ("    %s\n",_("Machine name to lookup"));
00330   printf (" %s\n","-T, --record_type=STRING");
00331   printf ("    %s\n",_("Record type to lookup (default: A)"));
00332   printf (" %s\n","-a, --expected_address=STRING");
00333   printf ("    %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
00334   printf ("    %s\n",_("was in -l"));
00335   printf (" %s\n","-A, --dig-arguments=STRING");
00336   printf ("    %s\n",_("Pass STRING as argument(s) to dig"));
00337   printf (UT_WARN_CRIT);
00338   printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
00339   printf (UT_VERBOSE);
00340 
00341   printf ("\n");
00342   printf ("%s\n", _("Examples:"));
00343   printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
00344   printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
00345 
00346   printf (UT_SUPPORT);
00347 }
00348 
00349 
00350 
00351 void
00352 print_usage (void)
00353 {
00354   printf ("%s\n", _("Usage:"));
00355   printf ("%s -l <query_address> [-H <host>] [-p <server port>]\n", progname);
00356   printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
00357   printf (" [-t <timeout>] [-a <expected answer address>] [-v]\n");
00358 }