Back to index

nagios-plugins  1.4.16
Defines | Functions | Variables
check_dig.c File Reference
#include "common.h"
#include "netutils.h"
#include "utils.h"
#include "runcmd.h"

Go to the source code of this file.

Defines

#define UNDEFINED   0
#define DEFAULT_PORT   53

Functions

int process_arguments (int, char **)
int validate_arguments (void)
void print_help (void)
void print_usage (void)
int main (int argc, char **argv)

Variables

const char * progname = "check_dig"
const char * copyright = "2002-2008"
const char * email = "nagiosplug-devel@lists.sourceforge.net"
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
double critical_interval = UNDEFINED

Define Documentation

#define DEFAULT_PORT   53

Definition at line 50 of file check_dig.c.

#define UNDEFINED   0

Definition at line 49 of file check_dig.c.


Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 64 of file check_dig.c.

{
  char *command_line;
  output chld_out, chld_err;
  char *msg = NULL;
  size_t i;
  char *t;
  long microsec;
  double elapsed_time;
  int result = STATE_UNKNOWN;

  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  /* Set signal handling and alarm */
  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 %s",
            PATH_TO_DIG, dns_server, server_port, query_address, record_type, dig_args);

  alarm (timeout_interval);
  gettimeofday (&tv, NULL);

  if (verbose) {
    printf ("%s\n", command_line);
    if(expected_address != NULL) {
      printf (_("Looking for: '%s'\n"), expected_address);
    } else {
      printf (_("Looking for: '%s'\n"), query_address);
    }
  }

  /* run the command */
  if(np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
    result = STATE_WARNING;
    msg = (char *)_("dig returned an error status");
  }

  for(i = 0; i < chld_out.lines; i++) {
    /* the server is responding, we just got the host name... */
    if (strstr (chld_out.line[i], ";; ANSWER SECTION:")) {

      /* loop through the whole 'ANSWER SECTION' */
      for(; i < chld_out.lines; i++) {
        /* get the host address */
        if (verbose)
          printf ("%s\n", chld_out.line[i]);

        if (strstr (chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) {
          msg = chld_out.line[i];
          result = STATE_OK;

          /* Translate output TAB -> SPACE */
          t = msg;
          while ((t = strchr(t, '\t')) != NULL) *t = ' ';
          break;
        }
      }

      if (result == STATE_UNKNOWN) {
        msg = (char *)_("Server not found in ANSWER SECTION");
        result = STATE_WARNING;
      }

      /* we found the answer section, so break out of the loop */
      break;
    }
  }

  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) {
    result = max_state(result, STATE_WARNING);
    if(!msg) for(i = 0; i < chld_err.lines; i++) {
      msg = strchr(chld_err.line[0], ':');
      if(msg) {
        msg++;
        break;
      }
    }
  }

  microsec = deltime (tv);
  elapsed_time = (double)microsec / 1.0e6;

  if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
    result = STATE_CRITICAL;

  else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
    result = STATE_WARNING;

  printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
          state_text (result), elapsed_time,
          msg ? msg : _("Probably a non-existent host/domain"),
          fperfdata("time", elapsed_time, "s",
                    (warning_interval>UNDEFINED?TRUE:FALSE),
                    warning_interval,
                    (critical_interval>UNDEFINED?TRUE:FALSE),
            critical_interval,
            TRUE, 0, FALSE, 0));
  return result;
}

Here is the call graph for this function:

void print_help ( void  )
void print_usage ( void  )

Definition at line 389 of file check_cpqarray.c.

{
  printf("cpqarrayd [options]\n");
  printf("   -h         prints this text\n");
  printf("   -d         enables debugging\n");
}
int process_arguments ( int  argc,
char **  argv 
)
int validate_arguments ( void  )

Variable Documentation

const char* copyright = "2002-2008"

Definition at line 36 of file check_dig.c.

Definition at line 60 of file check_dig.c.

char* dig_args = ""

Definition at line 56 of file check_dig.c.

char* dns_server = NULL

Definition at line 55 of file check_dig.c.

const char* email = "nagiosplug-devel@lists.sourceforge.net"

Definition at line 37 of file check_dig.c.

Definition at line 54 of file check_dig.c.

const char* progname = "check_dig"

Definition at line 35 of file check_dig.c.

Definition at line 52 of file check_dig.c.

char* record_type = "A"

Definition at line 53 of file check_dig.c.

Definition at line 58 of file check_dig.c.

int verbose = FALSE

Definition at line 57 of file check_dig.c.

Definition at line 59 of file check_dig.c.