From f83981580eced39b10e096b8eb9a6c4e6434e772 Mon Sep 17 00:00:00 2001 From: Matthias Eble Date: Sat, 5 Jan 2008 01:06:36 +0000 Subject: [PATCH] check_dns now sorts addresses for -a support with multiple address replies (Matthias Urlichs #1724052) git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1886 f882894a-f735-0410-b71e-b25c423dba1c --- NEWS | 1 + THANKS.in | 1 + plugins/check_dns.c | 42 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 8efceba..485693a 100644 --- a/NEWS +++ b/NEWS @@ -4,6 +4,7 @@ This file documents the major additions and syntax changes between releases. Added ./check_nt -v INSTANCES to count number of instances (Alessandro Ren) check_tcp now returns UNKNOWN with invalid hostname New check_icmp -s option to specify the source IP address + check_dns now sorts addresses for testing results for more than one returned IP (Matthias Urlichs) 1.4.11 13th December 2007 Fixed check_http regression in 1.4.10 where following redirects to diff --git a/THANKS.in b/THANKS.in index 36ae0b9..abec771 100644 --- a/THANKS.in +++ b/THANKS.in @@ -231,3 +231,4 @@ fabiodds Tom Payerle Alessandro Ren Harald Jenny +Matthias Urlichs diff --git a/plugins/check_dns.c b/plugins/check_dns.c index 454f813..8ae48cd 100644 --- a/plugins/check_dns.c +++ b/plugins/check_dns.c @@ -62,12 +62,24 @@ int match_expected_address = FALSE; int expect_authority = FALSE; thresholds *time_thresholds = NULL; +static int +qstrcmp(const void *p1, const void *p2) +{ + /* The actual arguments to this function are "pointers to + pointers to char", but strcmp() arguments are "pointers + to char", hence the following cast plus dereference */ + return strcmp(* (char * const *) p1, * (char * const *) p2); +} + + int main (int argc, char **argv) { char *command_line = NULL; char input_buffer[MAX_INPUT_BUFFER]; char *address = NULL; + char **addresses = NULL; + int n_addresses = 0; char *msg = NULL; char *temp_buffer = NULL; int non_authoritative = FALSE; @@ -141,16 +153,17 @@ main (int argc, char **argv) NSLOOKUP_COMMAND); } - if (address == NULL) - address = strdup (temp_buffer); - else - asprintf(&address, "%s,%s", address, temp_buffer); + if (addresses == NULL) + addresses = malloc(sizeof(*addresses)*10); + else if (!(n_addresses % 10)) + addresses = realloc(addresses,sizeof(*addresses) * (n_addresses + 10)); + addresses[n_addresses++] = strdup(temp_buffer); } - else if (strstr (chld_out.line[i], _("Non-authoritative answer:"))) { non_authoritative = TRUE; } + result = error_scan (chld_out.line[i]); if (result != STATE_OK) { msg = strchr (chld_out.line[i], ':'); @@ -171,9 +184,21 @@ main (int argc, char **argv) } } - /* If we got here, we should have an address string, - * and we can segfault if we do not */ - if (address==NULL || strlen(address)==0) + if (addresses) { + int i,slen; + char *adrp; + qsort(addresses, n_addresses, sizeof(*addresses), qstrcmp); + for(i=0, slen=1; i < n_addresses; i++) { + slen += strlen(addresses[i])+1; + } + adrp = address = malloc(slen); + for(i=0; i < n_addresses; i++) { + if (i) *adrp++ = ','; + strcpy(adrp, addresses[i]); + adrp += strlen(addresses[i]); + } + *adrp = 0; + } else die (STATE_CRITICAL, _("DNS CRITICAL - '%s' msg parsing exited with no address\n"), NSLOOKUP_COMMAND); @@ -429,6 +454,7 @@ print_help (void) printf (" %s\n", _("Optional DNS server you want to use for the lookup")); printf (" -a, --expected-address=IP-ADDRESS|HOST\n"); printf (" %s\n", _("Optional IP-ADDRESS you expect the DNS server to return. HOST must end with .")); + printf (" %s\n", _("Multiple addresses can be separated with commas, and need to be sorted.")); printf (" -A, --expect-authority\n"); printf (" %s\n", _("Optionally expect the DNS server to be authoritative for the lookup")); printf (" -w, --warning=seconds\n"); -- 2.30.2