summaryrefslogtreecommitdiffstats
path: root/web/attachments/258859-check_dns.c.patch
blob: 256624fd0d5b64da44a7d6292726d1fb38633db3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
--- 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");