--- check_dns.c 2007-01-29 08:46:41.000000000 +1100 +++ check_dns-patched.c 2007-11-26 21:31:23.000000000 +1100 @@ -62,6 +62,9 @@ int expect_authority = FALSE; thresholds *time_thresholds = NULL; +#define IPV4_SIZE 16*sizeof(char) +int order_results = FALSE; + int main (int argc, char **argv) { @@ -178,6 +181,56 @@ _("DNS CRITICAL - '%s' msg parsing exited with no address\n"), NSLOOKUP_COMMAND); + /* Order the results from DNS server */ + if (order_results) { + int num_result = 1; /* No commas? only one address */ + int i = 0; + char *ipv4_sort = NULL; + char *pstrtmp; + char *pstrtok; + + pstrtmp = strchr(address, ','); + while (pstrtmp) { + num_result++; + pstrtmp += sizeof(char); + pstrtmp = strchr(pstrtmp, ','); + } + + if (num_result > 1) { + ipv4_sort = malloc(IPV4_SIZE*num_result); + if (NULL == ipv4_sort) + die (STATE_UNKNOWN, + _("DNS UNKNOWN - Failed allocating %d bytes for ordering results\n"), + IPV4_SIZE*num_result); + + memset(ipv4_sort, 0, IPV4_SIZE*num_result); + pstrtmp = strdup(address); + pstrtok = strtok(pstrtmp, ","); + i = 0; + while (pstrtok) { + strncpy(ipv4_sort+(i*IPV4_SIZE), pstrtok, IPV4_SIZE); + pstrtok = strtok(NULL, ","); + i++; + } + free(pstrtmp); + + qsort(ipv4_sort, num_result, IPV4_SIZE, strcmp); + + pstrtmp = address; + for (i = 0; i < num_result; i++) { + strcpy(pstrtmp, ipv4_sort+(i*IPV4_SIZE)); + pstrtmp += (strlen(pstrtmp)); + if (i+1 < num_result) { + *pstrtmp = ','; + pstrtmp += sizeof(char); + } + *pstrtmp = '\0'; + } + + free(ipv4_sort); + } + } + /* compare to expected address */ if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) { result = STATE_CRITICAL; @@ -316,7 +369,7 @@ strcpy (argv[c], "-t"); while (1) { - c = getopt_long (argc, argv, "hVvAt:H:s:r:a:w:c:", long_opts, &opt_index); + c = getopt_long (argc, argv, "hVvAot:H:s:r:a:w:c:", long_opts, &opt_index); if (c == -1 || c == EOF) break; @@ -331,6 +384,9 @@ case 'v': /* version */ verbose = TRUE; break; + case 'o': + order_results = TRUE; + break; case 't': /* timeout period */ timeout_interval = atoi (optarg); break; @@ -431,6 +487,8 @@ 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 (" -o\n"); + printf (" %s\n", _("Order the results from DNS server. Useful for round-robind setups.")); printf (" -w, --warning=seconds\n"); printf (" %s\n", _("Return warning if elapsed time exceeds value. Default off")); printf (" -c, --critical=seconds\n");