bind plugin: Reworked Bruno's patch to remove repeating code.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sun, 18 Jan 2009 20:07:17 +0000 (21:07 +0100)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sun, 18 Jan 2009 20:07:17 +0000 (21:07 +0100)
src/bind.c
src/collectd.conf.in
src/types.db

index f09a08e..1a06a4d 100644 (file)
@@ -1,6 +1,7 @@
 /**
  * collectd - src/bind.c
  * Copyright (C) 2009  Bruno PrĂ©mont
+ * Copyright (C) 2009  Florian Forster
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -17,6 +18,7 @@
  *
  * Authors:
  *   Bruno PrĂ©mont <bonbons at linux-vserver.org>
+ *   Florian Forster <octo at verplant.org>
  **/
 
 /* Set to C99 and POSIX code */
 #include <libxml/parser.h>
 #include <libxml/xpath.h>
 
-static char *url              = NULL;
-static bool use_rrqueries_in  = 1;
-static bool use_requests      = 1;
-static bool use_query_results = 1;
-static bool use_updates       = 1;
-static bool use_zone_maint    = 1;
-static bool use_resolver      = 1;
-static char *srv_boot_ts      = NULL;
+#ifndef BIND_DEFAULT_URL
+# define BIND_DEFAULT_URL "http://localhost:8053/"
+#endif
+
+/* 
+ * Some types used for the callback functions. `translation_table_ptr_t' and
+ * `list_info_ptr_t' are passed to the callbacks in the `void *user_data'
+ * pointer.
+ */
+typedef int (*list_callback_t) (const char *name, counter_t value,
+    time_t current_time, void *user_data);
+
+struct translation_info_s
+{
+  const char *xml_name;
+  const char *type;
+  const char *type_instance;
+  const int  *config_variable;
+};
+typedef struct translation_info_s translation_info_t;
+
+struct translation_table_ptr_s
+{
+  const translation_info_t *table;
+  size_t table_length;
+  const char *plugin_instance;
+};
+typedef struct translation_table_ptr_s translation_table_ptr_t;
+
+struct list_info_ptr_s
+{
+  const char *plugin_instance;
+  const char *type;
+};
+typedef struct list_info_ptr_s list_info_ptr_t;
+
+static char *url               = NULL;
+static int   use_requests      = 1;
+static int   use_rejects       = 1;
+static int   use_responses     = 1;
+static int   use_queries       = 1;
+static int   use_rcode         = 1;
+static int   use_zonestats     = 1;
+static int   use_opcode        = 1;
+static int   use_resolver      = 1;
+static int   use_dnssec        = 1;
+
+static int   use_rrqueries_in  = 1;
+static int   use_query_results = 1;
+static int   use_updates       = 1;
+static int   use_zone_maint    = 1;
 
 static CURL *curl = NULL;
 
@@ -67,15 +112,134 @@ static char   bind_curl_error[CURL_ERROR_SIZE];
 static const char *config_keys[] =
 {
   "URL",
-  "RRQueriesIn",
   "Requests",
+  "Rejects",
+  "Responses",
+  "Queries",
+  "RCode",
+  "ZoneStats",
+  "OpCodes",
+  "Resolver",
+  "DNSSEC",
+
+  "RRQueriesIn",
   "QueryResults",
   "Updates",
-  "ZoneMaintenance",
-  "Resolver"
+  "ZoneMaintenance"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
+/* Translation table for the `nsstats' values. */
+static const translation_info_t nsstats_translation_table[] =
+{
+  /* Requests */
+  { "Requestv4",       "dns_request",  "IPv4",        &use_requests },
+  { "Requestv6",       "dns_request",  "IPv6",        &use_requests },
+  { "ReqEdns0",        "dns_request",  "EDNS0",       &use_requests },
+  { "ReqBadEDNSVer",   "dns_request",  "BadEDNSVer",  &use_requests },
+  { "ReqTSIG",         "dns_request",  "TSIG",        &use_requests },
+  { "ReqSIG0",         "dns_request",  "SIG0",        &use_requests },
+  { "ReqBadSIG",       "dns_request",  "BadSIG",      &use_requests },
+  { "ReqTCP",          "dns_request",  "TCP",         &use_requests },
+  /* Rejects */
+  { "AuthQryRej",      "dns_reject",   "authorative", &use_rejects },
+  { "RecQryRej",       "dns_reject",   "recursive",   &use_rejects },
+  { "XfrRej",          "dns_reject",   "transer",     &use_rejects },
+  { "UpdateRej",       "dns_reject",   "update",      &use_rejects },
+  /* Responses */
+  { "Response",        "dns_response", "normal",      &use_responses },
+  { "TruncatedResp",   "dns_response", "truncated",   &use_responses },
+  { "RespEDNS0",       "dns_response", "EDNS0",       &use_responses },
+  { "RespTSIG",        "dns_response", "TSIG",        &use_responses },
+  { "RespSIG0",        "dns_response", "SIG0",        &use_responses },
+  /* Queries */
+  { "QryAuthAns",      "dns_query",    "authorative", &use_queries },
+  { "QryNoauthAns",    "dns_query",    "nonauth",     &use_queries },
+  { "QryReferral",     "dns_query",    "referral",    &use_queries },
+  { "QryRecursion",    "dns_query",    "recursion",   &use_queries },
+  { "QryDuplicate",    "dns_query",    "dupliate",    &use_queries },
+  { "QryDropped",      "dns_query",    "dropped",     &use_queries },
+  { "QryFailure",      "dns_query",    "failure",     &use_queries },
+  /* Response codes */
+  { "QrySuccess",      "dns_rcode",    "tx-NOERROR",  &use_rcode },
+  { "QryNxrrset",      "dns_rcode",    "tx-NXRRSET",  &use_rcode },
+  { "QrySERVFAIL",     "dns_rcode",    "tx-SERVFAIL", &use_rcode },
+  { "QryFORMERR",      "dns_rcode",    "tx-FORMERR",  &use_rcode },
+  { "QryNXDOMAIN",     "dns_rcode",    "tx-NXDOMAIN", &use_rcode }
+#if 0
+  { "XfrReqDone",      "type", "type_instance", &use_something },
+  { "UpdateReqFwd",    "type", "type_instance", &use_something },
+  { "UpdateRespFwd",   "type", "type_instance", &use_something },
+  { "UpdateFwdFail",   "type", "type_instance", &use_something },
+  { "UpdateDone",      "type", "type_instance", &use_something },
+  { "UpdateFail",      "type", "type_instance", &use_something },
+  { "UpdateBadPrereq", "type", "type_instance", &use_something },
+#endif
+};
+static int nsstats_translation_table_length =
+  STATIC_ARRAY_SIZE (nsstats_translation_table);
+#define PARSE_NSSTATS (use_requests || use_rejects || use_responses \
+    || use_queries || use_rcode)
+
+/* Translation table for the `zonestats' values. */
+static const translation_info_t zonestats_translation_table[] =
+{
+  /* Notify's */
+  { "NotifyOutv4",     "dns_notify",   "tx-IPv4",     &use_zonestats },
+  { "NotifyOutv6",     "dns_notify",   "tx-IPv6",     &use_zonestats },
+  { "NotifyInv4",      "dns_notify",   "rx-IPv4",     &use_zonestats },
+  { "NotifyInv6",      "dns_notify",   "rx-IPv6",     &use_zonestats },
+  { "NotifyRej",       "dns_notify",   "rejected",    &use_zonestats },
+  /* SOA/AXFS/IXFS requests */
+  { "SOAOutv4",        "dns_opcode",   "SOA-IPv4",    &use_opcode },
+  { "SOAOutv6",        "dns_opcode",   "SOA-IPv4",    &use_opcode },
+  { "AXFRReqv4",       "dns_opcode",   "AXFR-IPv4",   &use_opcode },
+  { "AXFRReqv6",       "dns_opcode",   "AXFR-IPv6",   &use_opcode },
+  { "IXFRReqv4",       "dns_opcode",   "IXFR-IPv4",   &use_opcode },
+  { "IXFRReqv6",       "dns_opcode",   "IXFR-IPv6",   &use_opcode },
+  /* Domain transfers */
+  { "XfrSuccess",      "dns_transfer", "success",     &use_zonestats },
+  { "XfrFail",         "dns_transfer", "failure",     &use_zonestats }
+};
+static int zonestats_translation_table_length =
+  STATIC_ARRAY_SIZE (zonestats_translation_table);
+#define PARSE_ZONESTATS (use_zonestats || use_opcode)
+
+/* Translation table for the `resstats' values. */
+static const translation_info_t resstats_translation_table[] =
+{
+  /* Generic resolver information */
+  { "Queryv4",         "dns_query",    "IPv4",           &use_resolver },
+  { "Queryv6",         "dns_query",    "IPv6",           &use_resolver },
+  { "Responsev4",      "dns_response", "IPv4",           &use_resolver },
+  { "Responsev6",      "dns_response", "IPv6",           &use_resolver },
+  /* Received response codes */
+  { "NXDOMAIN",        "dns_rcode",    "rx-NXDOMAIN",    &use_rcode },
+  { "SERVFAIL",        "dns_rcode",    "rx-SERVFAIL",    &use_rcode },
+  { "FORMERR",         "dns_rcode",    "rx-FORMERR",     &use_rcode },
+  { "OtherError",      "dns_rcode",    "rx-OTHER",       &use_rcode },
+  { "EDNS0Fail",       "dns_rcode",    "rx-EDNS0Fail",   &use_rcode },
+  /* Received responses */
+  { "Mismatch",        "dns_response", "mismatch",       &use_responses },
+  { "Truncated",       "dns_response", "truncated",      &use_responses },
+  { "Lame",            "dns_response", "lame",           &use_responses },
+  { "Retry",           "dns_query",    "retry",          &use_responses },
+#if 0
+  { "GlueFetchv4",     "type", "type_instance", &use_something },
+  { "GlueFetchv6",     "type", "type_instance", &use_something },
+  { "GlueFetchv4Fail", "type", "type_instance", &use_something },
+  { "GlueFetchv6Fail", "type", "type_instance", &use_something },
+#endif
+  /* DNSSEC information */
+  { "ValAttempt",      "dns_resolver", "DNSSEC-attempt", &use_dnssec },
+  { "ValOk",           "dns_resolver", "DNSSEC-okay",    &use_dnssec },
+  { "ValNegOk",        "dns_resolver", "DNSSEC-negokay", &use_dnssec },
+  { "ValFail",         "dns_resolver", "DNSSEC-fail",    &use_dnssec }
+};
+static int resstats_translation_table_length =
+  STATIC_ARRAY_SIZE (resstats_translation_table);
+#define PARSE_RESSTATS (use_resolver || use_rcode || use_responses || use_dnssec)
+
 static void remove_special (char *buffer, size_t buffer_size)
 {
   size_t i;
@@ -99,7 +263,7 @@ static void submit_counter(time_t ts, const char *plugin_instance, const char *t
 
   vl.values = values;
   vl.values_len = 1;
-  vl.time = ts == 0 ? time(NULL) : ts;
+  vl.time = (ts == 0) ? time (NULL) : ts;
   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
   sstrncpy(vl.plugin, "bind", sizeof(vl.plugin));
   if (plugin_instance) {
@@ -145,6 +309,59 @@ static size_t bind_curl_callback (void *buf, size_t size, size_t nmemb,
   return (len);
 } /* size_t bind_curl_callback */
 
+/*
+ * Callback, that's called with a translation table.
+ * (Plugin instance is fixed, type and type instance come from lookup table.)
+ */
+static int bind_xml_table_callback (const char *name, counter_t value,
+    time_t current_time, void *user_data)
+{
+  translation_table_ptr_t *table = (translation_table_ptr_t *) user_data;
+  size_t i;
+
+  if (table == NULL)
+    return (-1);
+
+  for (i = 0; i < table->table_length; i++)
+  {
+    if (strcmp (table->table[i].xml_name, name) != 0)
+      continue;
+
+    if (*table->table[i].config_variable == 0)
+      break;
+
+    submit_counter (current_time,
+        table->plugin_instance,
+        table->table[i].type,
+        table->table[i].type_instance,
+        value);
+    break;
+  }
+
+  return (0);
+} /* int bind_xml_table_callback */
+
+/*
+ * Callback, that's used for lists.
+ * (Plugin instance and type are fixed, xml name is used as type instance.)
+ */
+static int bind_xml_list_callback (const char *name, counter_t value,
+    time_t current_time, void *user_data)
+{
+  list_info_ptr_t *list_info = (list_info_ptr_t *) user_data;
+
+  if (list_info == NULL)
+    return (-1);
+
+  submit_counter (current_time,
+      list_info->plugin_instance,
+      list_info->type,
+      /* type instance = */ name,
+      value);
+
+  return (0);
+} /* int bind_xml_list_callback */
+
 static int bind_xml_read_counter (xmlDoc *doc, xmlNode *node, 
     counter_t *ret_value)
 {
@@ -154,7 +371,7 @@ static int bind_xml_read_counter (xmlDoc *doc, xmlNode *node,
   str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
   if (str_ptr == NULL)
   {
-    ERROR ("bind plugin: bind_xml_read_int64: xmlNodeListGetString failed.");
+    ERROR ("bind plugin: bind_xml_read_counter: xmlNodeListGetString failed.");
     return (-1);
   }
 
@@ -164,11 +381,11 @@ static int bind_xml_read_counter (xmlDoc *doc, xmlNode *node,
   if (str_ptr == end_ptr || errno)
   {
     if (errno && value == LLONG_MIN)
-      ERROR ("bind plugin: bind_xml_read_int64: strtoll failed with underflow.");
+      ERROR ("bind plugin: bind_xml_read_counter: strtoll failed with underflow.");
     else if (errno && value == LLONG_MAX)
-      ERROR ("bind plugin: bind_xml_read_int64: strtoll failed with overflow.");
+      ERROR ("bind plugin: bind_xml_read_counter: strtoll failed with overflow.");
     else
-      ERROR ("bind plugin: bind_xml_read_int64: strtoll failed.");
+      ERROR ("bind plugin: bind_xml_read_counter: strtoll failed.");
     return (-1);
   }
 
@@ -176,455 +393,515 @@ static int bind_xml_read_counter (xmlDoc *doc, xmlNode *node,
   return (0);
 } /* int bind_xml_read_counter */
 
-static int bind_xml_read_timestamp (xmlDoc *doc, xmlNode *node,
-    time_t *ret_value)
+static int bind_xml_read_timestamp (const char *xpath_expression, xmlDoc *doc,
+    xmlXPathContext *xpathCtx, time_t *ret_value)
 {
-  char *str_ptr, *p;
+  xmlXPathObject *xpathObj = NULL;
+  xmlNode *node;
+  char *str_ptr;
+  char *tmp;
   struct tm tm;
 
+  xpathObj = xmlXPathEvalExpression (BAD_CAST xpath_expression, xpathCtx);
+  if (xpathObj == NULL)
+  {
+    ERROR ("bind plugin: Unable to evaluate XPath expression `%s'.",
+        xpath_expression);
+    return (-1);
+  }
+
+  if ((xpathObj->nodesetval == NULL) || (xpathObj->nodesetval->nodeNr < 1))
+  {
+    xmlXPathFreeObject (xpathObj);
+    return (-1);
+  }
+
+  if (xpathObj->nodesetval->nodeNr != 1)
+  {
+    NOTICE ("bind plugin: Evaluating the XPath expression `%s' returned "
+        "%i nodes. Only handling the first one.",
+        xpath_expression, xpathObj->nodesetval->nodeNr);
+  }
+
+  node = xpathObj->nodesetval->nodeTab[0];
+
+  if (node->xmlChildrenNode == NULL)
+  {
+    ERROR ("bind plugin: bind_xml_read_timestamp: "
+        "node->xmlChildrenNode == NULL");
+    xmlXPathFreeObject (xpathObj);
+    return (-1);
+  }
+
   str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
   if (str_ptr == NULL)
   {
-    ERROR ("bind plugin: bind_xml_read_int: xmlNodeListGetString failed.");
+    ERROR ("bind plugin: bind_xml_read_timestamp: xmlNodeListGetString failed.");
+    xmlXPathFreeObject (xpathObj);
     return (-1);
   }
 
-  memset(&tm, 0, sizeof(tm));
-  p = strptime(str_ptr, "%Y-%m-%dT%T", &tm);
+  memset (&tm, 0, sizeof(tm));
+  tmp = strptime (str_ptr, "%Y-%m-%dT%T", &tm);
   xmlFree(str_ptr);
-  if (p == NULL)
+  if (tmp == NULL)
   {
     ERROR ("bind plugin: bind_xml_read_timestamp: strptime failed.");
+    xmlXPathFreeObject (xpathObj);
     return (-1);
   }
 
   *ret_value = timegm(&tm);
+
+  xmlXPathFreeObject (xpathObj);
   return (0);
 } /* int bind_xml_read_timestamp */
 
-/* Bind 9.5.x */
-static int bind_xml_stats_v1(xmlDoc *doc, xmlXPathContext *xpathCtx, xmlNode *statsnode)
+/* 
+ * bind_parse_generic_name_value
+ *
+ * Reads statistics in the form:
+ * <foo>
+ *   <name>QUERY</name>
+ *   <value>123</name>
+ * </foo>
+ */
+static int bind_parse_generic_name_value (const char *xpath_expression,
+    list_callback_t list_callback,
+    void *user_data,
+    xmlDoc *doc, xmlXPathContext *xpathCtx,
+    time_t current_time)
 {
-  xmlXPathObjectPtr xpathObj = NULL;
-  time_t current_time;
+  xmlXPathObject *xpathObj = NULL;
+  int num_entries;
   int i;
 
-  xpathCtx->node = statsnode;
+  xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
+  if (xpathObj == NULL)
+  {
+    ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
+        xpath_expression);
+    return (-1);
+  }
 
-  /* server/boot-time -- detect possible counter-resets
-   * Type: XML DateTime */
-  if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/boot-time", xpathCtx)) == NULL) {
-    ERROR("bind plugin: unable to evaluate XPath expression StatsV1-boottime");
-    return -1;
-  } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
-    char *boot_tm = (char *) xmlNodeListGetString (doc, xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode, 1);
-    if (srv_boot_ts == NULL || strcmp(srv_boot_ts, boot_tm) != 0) {
-      xmlFree(srv_boot_ts);
-      srv_boot_ts = boot_tm;
-      /* TODO: tell collectd that our counters got reset ... */
-      DEBUG ("bind plugin: Statv1: Server boot time: %s (%d nodes)", srv_boot_ts, xpathObj->nodesetval->nodeNr);
-    } else
-      xmlFree(boot_tm);
+  num_entries = 0;
+  /* Iterate over all matching nodes. */
+  for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
+  {
+    xmlNode *name_node = NULL;
+    xmlNode *counter = NULL;
+    xmlNode *child;
+
+    /* Iterate over all child nodes. */
+    for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
+        child != NULL;
+        child = child->next)
+    {
+      if (child->type != XML_ELEMENT_NODE)
+        continue;
+
+      if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
+        name_node = child;
+      else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
+        counter = child;
+    }
+
+    if ((name_node != NULL) && (counter != NULL))
+    {
+      char *name = (char *) xmlNodeListGetString (doc,
+          name_node->xmlChildrenNode, 1);
+      counter_t value;
+      int status;
+
+      status = bind_xml_read_counter (doc, counter, &value);
+      if (status != 0)
+        continue;
+
+      status = (*list_callback) (name, value, current_time, user_data);
+      if (status == 0)
+        num_entries++;
+
+      xmlFree (name);
+    }
   }
+
+  DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
+      num_entries, (num_entries == 1) ? "entry" : "entries",
+      xpath_expression);
+
   xmlXPathFreeObject(xpathObj);
 
-  /* server/current-time -- parse our time-stamp
-   * Type: XML DateTime */
-  if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/current-time", xpathCtx)) == NULL) {
-    ERROR("bind plugin: unable to evaluate XPath expression StatsV1-currenttime");
-    return -1;
-  } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
-    if (bind_xml_read_timestamp(doc, xpathObj->nodesetval->nodeTab[0], &current_time))
-      current_time = time(NULL);
-    else
-      DEBUG ("bind plugin: Statv1: Server current time: %ld (%d nodes)", current_time, xpathObj->nodesetval->nodeNr);
+  return (0);
+} /* int bind_parse_generic_name_value */
+
+/* 
+ * bind_parse_generic_value_list
+ *
+ * Reads statistics in the form:
+ * <foo>
+ *   <name0>123</name0>
+ *   <name1>234</name1>
+ *   <name2>345</name2>
+ *   :
+ * </foo>
+ */
+static int bind_parse_generic_value_list (const char *xpath_expression,
+    list_callback_t list_callback,
+    void *user_data,
+    xmlDoc *doc, xmlXPathContext *xpathCtx,
+    time_t current_time)
+{
+  xmlXPathObject *xpathObj = NULL;
+  int num_entries;
+  int i;
+
+  xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
+  if (xpathObj == NULL)
+  {
+    ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
+        xpath_expression);
+    return (-1);
   }
-  xmlXPathFreeObject(xpathObj);
 
-  /* requests/opcode -- [name] = [counter]
-   * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ... */
-  if (use_requests) {
-    if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/requests/opcode", xpathCtx)) == NULL) {
-      ERROR("bind plugin: unable to evaluate XPath expression StatsV1-currenttime");
-      return -1;
-    } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
-      xmlNode *name = NULL, *counter = NULL, *child;
-      for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
-        if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
-          name = child;
-        else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
-          counter = child;
-      if (name && counter) {
-        char *tinst   = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
-        counter_t val;
-        if (!bind_xml_read_counter(doc, counter, &val))
-          submit_counter(current_time, NULL, "dns_opcode", tinst, val);
-        xmlFree(tinst);
-      }
-    }
-    DEBUG ("bind plugin: Statv1: Found %d entries for requests/opcode", xpathObj->nodesetval->nodeNr);
-    xmlXPathFreeObject(xpathObj);
-  }
-
-  /* queries-in/rdtype -- [name] = [counter]
-   * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP, X25, PX, AAAA, LOC,
-   *            SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY, SPF, TKEY, IXFR, AXFR, ANY, ..., Others */
-  if (use_rrqueries_in) {
-    if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/queries-in/rdtype", xpathCtx)) == NULL) {
-      ERROR("bind plugin: unable to evaluate XPath expression StatsV1-currenttime");
-      return -1;
-    } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
-      xmlNode *name = NULL, *counter = NULL, *child;
-      for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
-        if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
-          name = child;
-        else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
-          counter = child;
-      if (name && counter) {
-        char *tinst   = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
-        counter_t val;
-        if (!bind_xml_read_counter(doc, counter, &val))
-          submit_counter(current_time, NULL, "dns_qtype", tinst, val);
-        xmlFree(tinst);
-      }
-    }
-    DEBUG ("bind plugin: Statv1: Found %d entries for queries-in/rdtype", xpathObj->nodesetval->nodeNr);
-    xmlXPathFreeObject(xpathObj);
-  }
-
-  /* nsstats -- [$name] = [$value] */
-  /* Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG, ReqSIG0, ReqBadSIG, ReqTCP,
-   *            AuthQryRej, RecQryRej, XfrRej, UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
-   *            RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral, QryNxrrset, QrySERVFAIL,
-   *            QryFORMERR, QryNXDOMAIN, QryRecursion, QryDuplicate, QryDropped, QryFailure, XfrReqDone,
-   *            UpdateReqFwd, UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail, UpdateBadPrereq */
-  if (use_updates || use_query_results || use_requests) {
-    if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/nsstats", xpathCtx)) == NULL) {
-      ERROR("bind plugin: unable to evaluate XPath expression StatsV1-nsstats");
-      return -1;
-    } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
-      xmlNode *child;
-      counter_t val;
-      int n = 0;
-      for (child = xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode; child != NULL; child = child->next, n++)
-        if (child->type == XML_ELEMENT_NODE && !bind_xml_read_counter(doc, child, &val)) {
-          if (use_updates && (strncmp("Update", (char *)child->name, 6) == 0 || strcmp("XfrReqDone", (char *)child->name) == 0))
-            submit_counter(current_time, NULL, "dns_update", (char *)child->name, val);
-          else if (use_query_results && strncmp("Qry", (char *)child->name, 3) == 0)
-            submit_counter(current_time, NULL, "dns_rcode", (char *)child->name, val);
-          else if (use_query_results && (strncmp("Resp", (char *)child->name, 4) == 0 ||
-                strcmp("AuthQryRej", (char *)child->name) == 0 || strcmp("RecQryRej", (char *)child->name) == 0 ||
-                strcmp("XfrRej", (char *)child->name) == 0 || strcmp("TruncatedResp", (char *)child->name) == 0))
-            submit_counter(current_time, NULL, "dns_rcode", (char *)child->name, val);
-          else if (use_requests && strncmp("Req", (char *)child->name, 3) == 0)
-            submit_counter(current_time, NULL, "dns_request", (char *)child->name, val);
-        }
-      DEBUG ("bind plugin: Statv1: Found %d entries for %d nsstats", n, xpathObj->nodesetval->nodeNr);
-    }
-    xmlXPathFreeObject(xpathObj);
-  }
-
-  /* zonestats -- [$name] = [$value] */
-  /* Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej, SOAOutv4, SOAOutv6,
-   *            AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6, XfrSuccess, XfrFail */
-  if (use_zone_maint) {
-    if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/zonestats", xpathCtx)) == NULL) {
-      ERROR("bind plugin: unable to evaluate XPath expression StatsV1-zonestats");
-      return -1;
-    } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
-      xmlNode *child;
-      counter_t val;
-      int n = 0;
-      for (child = xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode; child != NULL; child = child->next, n++)
-        if (child->type == XML_ELEMENT_NODE) {
-          if (!bind_xml_read_counter(doc, child, &val))
-            submit_counter(current_time, NULL, "dns_zops", (char *)child->name, val);
-        }
-      DEBUG ("bind plugin: Statv1: Found %d entries for %d zonestats", n, xpathObj->nodesetval->nodeNr);
-    }
-    xmlXPathFreeObject(xpathObj);
-  }
-
-  /* resstats -- [$name] = [$value] */
-  /* Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL, FORMERR, OtherError,
-   *            EDNS0Fail, Mismatch, Truncated, Lame, Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
-   *            GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail */
-  if (use_resolver) {
-    if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/resstats", xpathCtx)) == NULL) {
-      ERROR("bind plugin: unable to evaluate XPath expression StatsV1-resstats");
-      return -1;
-    } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
-      xmlNode *child;
-      counter_t val;
-      int n = 0;
-      for (child = xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode; child != NULL; child = child->next, n++)
-        if (child->type == XML_ELEMENT_NODE) {
-          if (!bind_xml_read_counter(doc, child, &val))
-            submit_counter(current_time, NULL, "dns_resolver", (char *)child->name, val);
-        }
-      DEBUG ("bind plugin: Statv1: Found %d entries for %d resstats", n, xpathObj->nodesetval->nodeNr);
+  num_entries = 0;
+  /* Iterate over all matching nodes. */
+  for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
+  {
+    xmlNode *child;
+
+    /* Iterate over all child nodes. */
+    for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
+        child != NULL;
+        child = child->next)
+    {
+      char *node_name;
+      counter_t value;
+      int status;
+
+      if (child->type != XML_ELEMENT_NODE)
+        continue;
+
+      node_name = (char *) child->name;
+      status = bind_xml_read_counter (doc, child, &value);
+      if (status != 0)
+        continue;
+
+      status = (*list_callback) (node_name, value, current_time, user_data);
+      if (status == 0)
+        num_entries++;
     }
-    xmlXPathFreeObject(xpathObj);
   }
-  return 0;
-} /* int bind_xml_stats_v1 */
 
-/* Bind 9.6.x */
-static int bind_xml_stats_v2(xmlDoc *doc, xmlXPathContext *xpathCtx, xmlNode *statsnode)
+  DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
+      num_entries, (num_entries == 1) ? "entry" : "entries",
+      xpath_expression);
+
+  xmlXPathFreeObject(xpathObj);
+
+  return (0);
+} /* int bind_parse_generic_value_list */
+
+static int bind_xml_stats (int version, xmlDoc *doc,
+    xmlXPathContext *xpathCtx, xmlNode *statsnode)
 {
-  xmlXPathObjectPtr xpathObj = NULL;
-  time_t current_time;
-  int i;
+  time_t current_time = 0;
+  int status;
 
   xpathCtx->node = statsnode;
 
-  /* server/boot-time -- detect possible counter-resets
-   * Type: XML DateTime */
-  if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/boot-time", xpathCtx)) == NULL) {
-    ERROR("bind plugin: unable to evaluate XPath expression StatsV2-boottime");
-    return -1;
-  } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
-    char *boot_tm = (char *) xmlNodeListGetString (doc, xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode, 1);
-    if (srv_boot_ts == NULL || strcmp(srv_boot_ts, boot_tm) != 0) {
-      xmlFree(srv_boot_ts);
-      srv_boot_ts = boot_tm;
-      /* TODO: tell collectd that our counters got reset ... */
-      DEBUG ("bind plugin: Statv2: Server boot time: %s (%d nodes)", srv_boot_ts, xpathObj->nodesetval->nodeNr);
-    } else
-      xmlFree(boot_tm);
+  /* TODO: Check `server/boot-time' to recognize server restarts. */
+
+  status = bind_xml_read_timestamp ("server/current-time",
+      doc, xpathCtx, &current_time);
+  if (status != 0)
+  {
+    ERROR ("bind plugin: Reading `server/current-time' failed.");
+    return (-1);
+  }
+  DEBUG ("bind plugin: Current server time is %i.", (int) current_time);
+
+  /* XPath:     server/requests/opcode
+   * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
+   * Layout:
+   *   <opcode>
+   *     <name>A</name>
+   *     <counter>1</counter>
+   *   </opcode>
+   *   :
+   */
+  if (use_opcode)
+  {
+    list_info_ptr_t list_info =
+    {
+      /* plugin instance = */ "requests",
+      /* type = */ "dns_opcode"
+    };
+
+    bind_parse_generic_name_value (/* xpath = */ "server/requests/opcode",
+        /* callback = */ bind_xml_list_callback,
+        /* user_data = */ &list_info,
+        doc, xpathCtx, current_time);
   }
-  xmlXPathFreeObject(xpathObj);
 
-  /* server/current-time -- parse our time-stamp
-   * Type: XML DateTime */
-  if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/current-time", xpathCtx)) == NULL) {
-    ERROR("bind plugin: unable to evaluate XPath expression StatsV2-currenttime");
-    return -1;
-  } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
-    if (bind_xml_read_timestamp(doc, xpathObj->nodesetval->nodeTab[0], &current_time))
-      current_time = time(NULL);
+  /* XPath:     server/queries-in/rdtype
+   * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
+   *            X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
+   *            SPF, TKEY, IXFR, AXFR, ANY, ..., Others
+   * Layout:
+   *   <rdtype>
+   *     <name>A</name>
+   *     <counter>1</counter>
+   *   </rdtype>
+   *   :
+   */
+  if (use_rrqueries_in)
+  {
+    list_info_ptr_t list_info =
+    {
+      /* plugin instance = */ "queries-in",
+      /* type = */ "dns_qtype"
+    };
+
+    bind_parse_generic_name_value (/* xpath = */ "server/queries-in/rdtype",
+        /* callback = */ bind_xml_list_callback,
+        /* user_data = */ &list_info,
+        doc, xpathCtx, current_time);
+  }
+  
+  /* XPath:     server/nsstats, server/nsstat
+   * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
+   *            ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
+   *            UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
+   *            RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
+   *            QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
+   *            QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
+   *            UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
+   *            UpdateBadPrereq
+   * Layout v1:
+   *   <nsstats>
+   *     <Requestv4>1</Requestv4>
+   *     <Requestv6>0</Requestv6>
+   *     :
+   *   </nsstats>
+   * Layout v2:
+   *   <nsstat>
+   *     <name>Requestv4</name>
+   *     <counter>1</counter>
+   *   </nsstat>
+   *   <nsstat>
+   *     <name>Requestv6</name>
+   *     <counter>0</counter>
+   *   </nsstat>
+   *   :
+   */
+  if (PARSE_NSSTATS)
+  {
+    translation_table_ptr_t table_ptr =
+    { 
+      nsstats_translation_table,
+      nsstats_translation_table_length,
+      /* plugin_instance = */ "nsstats"
+    };
+
+    if (version == 1)
+    {
+      bind_parse_generic_value_list ("server/nsstats",
+          /* callback = */ bind_xml_table_callback,
+          /* user_data = */ &table_ptr,
+          doc, xpathCtx, current_time);
+    }
     else
-      DEBUG ("bind plugin: Statv2: Server current time: %ld (%d nodes)", current_time, xpathObj->nodesetval->nodeNr);
+    {
+      bind_parse_generic_name_value ("server/nsstat",
+          /* callback = */ bind_xml_table_callback,
+          /* user_data = */ &table_ptr,
+          doc, xpathCtx, current_time);
+    }
   }
-  xmlXPathFreeObject(xpathObj);
 
-  /* requests/opcode -- [name] = [counter]
-   * Variables: QUERY, ... */
-  if (use_requests) {
-    if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/requests/opcode", xpathCtx)) == NULL) {
-      ERROR("bind plugin: unable to evaluate XPath expression StatsV2-opcode");
-      return -1;
-    } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
-      xmlNode *name = NULL, *counter = NULL, *child;
-      for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
-        if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
-          name = child;
-        else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
-          counter = child;
-      if (name && counter) {
-        char *tinst   = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
-        counter_t val;
-        if (!bind_xml_read_counter(doc, counter, &val))
-          submit_counter(current_time, NULL, "dns_opcode", tinst, val);
-        xmlFree(tinst);
-      }
-    }
-    DEBUG ("bind plugin: Statv2: Found %d entries for requests/opcode", xpathObj->nodesetval->nodeNr);
-    xmlXPathFreeObject(xpathObj);
-  }
-
-  /* queries-in/rdtype -- [name] = [counter]
-   * Variables: A, NS, SOA, PTR, MX, TXT, SRV, ... */
-  if (use_rrqueries_in) {
-    if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/queries-in/rdtype", xpathCtx)) == NULL) {
-      ERROR("bind plugin: unable to evaluate XPath expression StatsV2-rdtype");
-      return -1;
-    } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
-      xmlNode *name = NULL, *counter = NULL, *child;
-      for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
-        if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
-          name = child;
-        else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
-          counter = child;
-      if (name && counter) {
-        char *tinst   = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
-        counter_t val;
-        if (!bind_xml_read_counter(doc, counter, &val))
-          submit_counter(current_time, NULL, "dns_qtype", tinst, val);
-        xmlFree(tinst);
-      }
+  /* XPath:     server/zonestats, server/zonestat
+   * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
+   *            SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
+   *            XfrSuccess, XfrFail
+   * Layout v1:
+   *   <zonestats>
+   *     <NotifyOutv4>0</NotifyOutv4>
+   *     <NotifyOutv6>0</NotifyOutv6>
+   *     :
+   *   </zonestats>
+   * Layout v2:
+   *   <zonestat>
+   *     <name>NotifyOutv4</name>
+   *     <counter>0</counter>
+   *   </zonestat>
+   *   <zonestat>
+   *     <name>NotifyOutv6</name>
+   *     <counter>0</counter>
+   *   </zonestat>
+   *   :
+   */
+  if (PARSE_ZONESTATS)
+  {
+    translation_table_ptr_t table_ptr =
+    { 
+      zonestats_translation_table,
+      zonestats_translation_table_length,
+      /* plugin_instance = */ "zonestats"
+    };
+
+    if (version == 1)
+    {
+      bind_parse_generic_value_list ("server/zonestats",
+          /* callback = */ bind_xml_table_callback,
+          /* user_data = */ &table_ptr,
+          doc, xpathCtx, current_time);
     }
-    DEBUG ("bind plugin: Statv2: Found %d entries for queries-in/rdtype", xpathObj->nodesetval->nodeNr);
-    xmlXPathFreeObject(xpathObj);
-  }
-
-  /* nsstat -- [name] = [counter]
-   * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG, ReqSIG0, ReqBadSIG, ReqTCP,
-   *            AuthQryRej, RecQryRej, XfrRej, UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
-   *            RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral, QryNxrrset, QrySERVFAIL,
-   *            QryFORMERR, QryNXDOMAIN, QryRecursion, QryDuplicate, QryDropped, QryFailure, XfrReqDone,
-   *            UpdateReqFwd, UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail, UpdateBadPrereq, */
-  if (use_updates || use_query_results || use_requests) {
-    if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/nsstat", xpathCtx)) == NULL) {
-      ERROR("bind plugin: unable to evaluate XPath expression StatsV2-nsstat");
-      return -1;
-    } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
-      xmlNode *name = NULL, *counter = NULL, *child;
-      for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
-        if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
-          name = child;
-        else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
-          counter = child;
-      if (name && counter) {
-        char *tinst   = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
-        counter_t val;
-        if (!bind_xml_read_counter(doc, counter, &val)) {
-          if (use_updates && (strncmp("Update", tinst, 6) == 0 || strcmp("XfrReqDone", tinst) == 0))
-            submit_counter(current_time, NULL, "dns_update", tinst, val);
-          else if (use_query_results && strncmp("Qry", tinst, 3) == 0)
-            submit_counter(current_time, NULL, "dns_rcode", tinst, val);
-          else if (use_query_results && (strncmp("Resp", tinst, 4) == 0 || strcmp("AuthQryRej", tinst) == 0 ||
-                strcmp("RecQryRej", tinst) == 0 || strcmp("XfrRej", tinst) == 0 || strcmp("TruncatedResp", tinst) == 0))
-            submit_counter(current_time, NULL, "dns_rcode", tinst, val);
-          else if (use_requests && strncmp("Req", tinst, 3) == 0)
-            submit_counter(current_time, NULL, "dns_request", tinst, val);
-        }
-        xmlFree(tinst);
-      }
+    else
+    {
+      bind_parse_generic_name_value ("server/zonestat",
+          /* callback = */ bind_xml_table_callback,
+          /* user_data = */ &table_ptr,
+          doc, xpathCtx, current_time);
     }
-    DEBUG ("bind plugin: Statv2: Found %d entries for nsstat", xpathObj->nodesetval->nodeNr);
-    xmlXPathFreeObject(xpathObj);
-  }
-
-  /* zonestat -- [name] = [counter]
-   * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej, SOAOutv4, SOAOutv6,
-   *            AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6, XfrSuccess, XfrFail */
-  if (use_zone_maint) {
-    if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/zonestat", xpathCtx)) == NULL) {
-      ERROR("bind plugin: unable to evaluate XPath expression StatsV2-zonestat");
-      return -1;
-    } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
-      xmlNode *name = NULL, *counter = NULL, *child;
-      for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
-        if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
-          name = child;
-        else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
-          counter = child;
-      if (name && counter) {
-        char *tinst   = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
-        counter_t val;
-        if (!bind_xml_read_counter(doc, counter, &val))
-          submit_counter(current_time, NULL, "dns_zops", tinst, val);
-        xmlFree(tinst);
-      }
+  }
+
+  /* XPath:     server/resstats
+   * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
+   *            FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
+   *            Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
+   *            GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
+   * Layout v1:
+   *   <resstats>
+   *     <Queryv4>0</Queryv4>
+   *     <Queryv6>0</Queryv6>
+   *     :
+   *   </resstats>
+   * Layout v2:
+   *   <resstat>
+   *     <name>Queryv4</name>
+   *     <counter>0</counter>
+   *   </resstat>
+   *   <resstat>
+   *     <name>Queryv6</name>
+   *     <counter>0</counter>
+   *   </resstat>
+   *   :
+   */
+  if (PARSE_RESSTATS)
+  {
+    translation_table_ptr_t table_ptr =
+    { 
+      resstats_translation_table,
+      resstats_translation_table_length,
+      /* plugin_instance = */ "resstats"
+    };
+
+    if (version == 1)
+    {
+      bind_parse_generic_value_list ("server/resstats",
+          /* callback = */ bind_xml_table_callback,
+          /* user_data = */ &table_ptr,
+          doc, xpathCtx, current_time);
     }
-    DEBUG ("bind plugin: Statv2: Found %d entries for zonestat", xpathObj->nodesetval->nodeNr);
-    xmlXPathFreeObject(xpathObj);
-  }
-
-  /* WARNING: per-view only: views/view/resstat, view-name as plugin-instance */
-  /* resstat -- [name] = [counter]
-   * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL, FORMERR, OtherError,
-   *            EDNS0Fail, Mismatch, Truncated, Lame, Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
-   *            GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail */
-  if (use_resolver) {
-    if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "views/view", xpathCtx)) == NULL) {
-      ERROR("bind plugin: unable to evaluate XPath expression StatsV2-view");
-      return -1;
-    } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
-      char *zname = NULL;
-      xmlNode *name = NULL, *counter = NULL, *rchild, *child;
-      int n = 0;
-      for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL && zname == NULL; child = child->next)
-        if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
-          zname = (char *) xmlNodeListGetString (doc, child->xmlChildrenNode, 1);
-      if (!zname || strcmp("_bind", zname) == 0)
-        continue; /* Unnamed zone?? */
-      /* else TODO: allow zone filtering */
-      for (rchild = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; rchild != NULL; rchild = rchild->next, n++)
-        if (xmlStrcmp (BAD_CAST "resstat", rchild->name) == 0) {
-          for (child = rchild->xmlChildrenNode; child != NULL; child = child->next)
-            if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
-              name = child;
-            else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
-              counter = child;
-          if (name && counter) {
-            char *tinst   = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
-            counter_t val;
-            if (!bind_xml_read_counter(doc, counter, &val))
-              submit_counter(current_time, zname, "dns_resolver", tinst, val);
-            xmlFree(tinst);
-          }
-        }
-      DEBUG ("bind plugin: Statv2: Found %d entries for view %s", n, zname);
-      xmlFree(zname);
+    else
+    {
+      bind_parse_generic_name_value ("server/resstat",
+          /* callback = */ bind_xml_table_callback,
+          /* user_data = */ &table_ptr,
+          doc, xpathCtx, current_time);
     }
-    xmlXPathFreeObject(xpathObj);
   }
+
   return 0;
-} /* int bind_xml_stats_v2 */
+} /* int bind_xml_stats */
 
 static int bind_xml (const char *data)
 {
   xmlDoc *doc = NULL;
-  xmlXPathContextPtr xpathCtx = NULL;
-  xmlXPathObjectPtr xpathObj = NULL;
+  xmlXPathContext *xpathCtx = NULL;
+  xmlXPathObject *xpathObj = NULL;
   int ret = -1;
+  int i;
 
-  do
+  doc = xmlParseMemory (data, strlen (data));
+  if (doc == NULL)
   {
-    doc = xmlParseMemory (data, strlen (data));
-    if (doc == NULL) {
-      ERROR ("bind plugin: xmlParseMemory failed.");
-      break;
-    }
+    ERROR ("bind plugin: xmlParseMemory failed.");
+    return (-1);
+  }
 
-    if ((xpathCtx = xmlXPathNewContext(doc)) == NULL) {
-      ERROR ("bind plugin: xmlXPathNewContext failed.");
-      break;
-    }
+  xpathCtx = xmlXPathNewContext (doc);
+  if (xpathCtx == NULL)
+  {
+    ERROR ("bind plugin: xmlXPathNewContext failed.");
+    xmlFreeDoc (doc);
+    return (-1);
+  }
 
-    /* Look for /isc/bind/statistics[@version='1.0'] */
-    xpathObj = xmlXPathEvalExpression(BAD_CAST "/isc[@version='1.0']/bind/statistics[@version='1.0']", xpathCtx);
-    if (xpathObj == NULL)
-    {
-      ERROR("bind plugin: unable to evaluate XPath expression StatsV1");
-      break;
-    }
-    else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0)
-    {
-      /* We have Bind-9.5.x */
-      ret = bind_xml_stats_v1(doc, xpathCtx, xpathObj->nodesetval->nodeTab[0]);
-      break;
-    }
+  xpathObj = xmlXPathEvalExpression (BAD_CAST "/isc/bind/statistics", xpathCtx);
+  if (xpathObj == NULL)
+  {
+    ERROR ("bind plugin: Cannot find the <statistics> tag.");
+    xmlXPathFreeContext (xpathCtx);
+    xmlFreeDoc (doc);
+    return (-1);
+  }
+  else if (xpathObj->nodesetval == NULL)
+  {
+    ERROR ("bind plugin: xmlXPathEvalExpression failed.");
+    xmlXPathFreeObject (xpathObj);
+    xmlXPathFreeContext (xpathCtx);
+    xmlFreeDoc (doc);
+    return (-1);
+  }
 
-    /* Not Bind-9.5.x. Let's clear up and try Bind-9.6.x instead. */
-    xmlXPathFreeObject(xpathObj);
-    xpathObj = NULL;
+  for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
+  {
+    xmlNode *node;
+    char *attr_version;
+    int parsed_version = 0;
 
-    /* Look for /isc/bind/statistics[@version='2.0'] */
-    xpathObj = xmlXPathEvalExpression(BAD_CAST "/isc[@version='1.0']/bind/statistics[@version='2.0']", xpathCtx);
-    if (xpathObj == NULL)
+    node = xpathObj->nodesetval->nodeTab[i];
+    assert (node != NULL);
+
+    attr_version = (char *) xmlGetProp (node, BAD_CAST "version");
+    if (attr_version == NULL)
     {
-      ERROR("bind plugin: unable to evaluate XPath expression StatsV2");
-      break;
+      NOTICE ("bind plugin: Found <statistics> tag doesn't have a "
+          "`version' attribute.");
+      continue;
     }
-    else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0)
+    DEBUG ("bind plugin: Found: <statistics version=\"%s\">", attr_version);
+
+    /* At the time this plugin was written, version "1.0" was used by
+     * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
+     * that "1.*" and "2.*" don't introduce structural changes, so we just
+     * check for the first two characters here. */
+    if (strncmp ("1.", attr_version, strlen ("1.")) == 0)
+      parsed_version = 1;
+    else if (strncmp ("2.", attr_version, strlen ("2.")) == 0)
+      parsed_version = 2;
+    else
     {
-      /* We have Bind-9.6.x */
-      ret = bind_xml_stats_v2(doc, xpathCtx, xpathObj->nodesetval->nodeTab[0]);
-      break;
+      /* TODO: Use the complaint mechanism here. */
+      NOTICE ("bind plugin: Found <statistics> tag with version `%s'. "
+          "Unfortunately I have no clue how to parse that. "
+          "Please open a bug report for this.", attr_version);
+      xmlFree (attr_version);
+      continue;
     }
 
-    ERROR("bind plugin: unable to find statistics in supported version.");
-  } while (0);
+    ret = bind_xml_stats (parsed_version,
+        doc, xpathCtx, node);
 
-  if (xpathObj != NULL)
-    xmlXPathFreeObject(xpathObj);
-  if (xpathCtx != NULL)
-    xmlXPathFreeContext(xpathCtx);
-  if (doc != NULL)
-    xmlFreeDoc (doc);
+    xmlFree (attr_version);
+    /* One <statistics> node ought to be enough. */
+    break;
+  }
+
+  xmlXPathFreeObject (xpathObj);
+  xmlXPathFreeContext (xpathCtx);
+  xmlFreeDoc (doc);
 
   return (ret);
 } /* int bind_xml */
@@ -643,15 +920,11 @@ static int config_set_str (char **var, const char *value)
     return (0);
 } /* int config_set_str */
 
-static int config_set_bool (bool *var, const char *value)
+static int config_set_bool (int *var, const char *value)
 {
-  if ((strcasecmp ("true", value) == 0)
-      || (strcasecmp ("yes", value) == 0)
-      || (strcasecmp ("on", value) == 0))
+  if (IS_TRUE (value))
     *var = 1;
-  else if ((strcasecmp ("false", value) == 0)
-      || (strcasecmp ("no", value) == 0)
-      || (strcasecmp ("off", value) == 0))
+  else if (IS_FALSE (value))
     *var = 0;
   else
     return -1;
@@ -662,35 +935,45 @@ static int bind_config (const char *key, const char *value)
 {
   if (strcasecmp (key, "URL") == 0)
     return (config_set_str (&url, value));
-  else if (strcasecmp (key, "RRQueriesIn") == 0)
-    return (config_set_bool (&use_rrqueries_in, value));
   else if (strcasecmp (key, "Requests") == 0)
     return (config_set_bool (&use_requests, value));
+  else if (strcasecmp (key, "Rejects") == 0)
+    return (config_set_bool (&use_rejects, value));
+  else if (strcasecmp (key, "Responses") == 0)
+    return (config_set_bool (&use_responses, value));
+  else if (strcasecmp (key, "Queries") == 0)
+    return (config_set_bool (&use_queries, value));
+  else if (strcasecmp (key, "RCode") == 0)
+    return (config_set_bool (&use_rcode, value));
+  else if (strcasecmp (key, "ZoneStats") == 0)
+    return (config_set_bool (&use_zonestats, value));
+  else if (strcasecmp (key, "OpCodes") == 0)
+    return (config_set_bool (&use_opcode, value));
+  else if (strcasecmp (key, "Resolver") == 0)
+    return (config_set_bool (&use_resolver, value));
+  else if (strcasecmp (key, "DNSSEC") == 0)
+    return (config_set_bool (&use_dnssec, value));
+
+  else if (strcasecmp (key, "RRQueriesIn") == 0)
+    return (config_set_bool (&use_rrqueries_in, value));
   else if (strcasecmp (key, "QueryResults") == 0)
     return (config_set_bool (&use_query_results, value));
   else if (strcasecmp (key, "Updates") == 0)
     return (config_set_bool (&use_updates, value));
   else if (strcasecmp (key, "ZoneMaintenance") == 0)
     return (config_set_bool (&use_zone_maint, value));
-  else if (strcasecmp (key, "Resolver") == 0)
-    return (config_set_bool (&use_resolver, value));
+
   else
     return (-1);
 } /* int bind_config */
 
 static int bind_init (void)
 {
-  if (url == NULL)
-  {
-    WARNING ("bind plugin: bind_init: No URL configured, "
-        "returning an error.");
-    return (-1);
-  }
-
   if (curl != NULL)
-    curl_easy_cleanup (curl);
+    return (0);
 
-  if ((curl = curl_easy_init ()) == NULL)
+  curl = curl_easy_init ();
+  if (curl == NULL)
   {
     ERROR ("bind plugin: bind_init: curl_easy_init failed.");
     return (-1);
@@ -699,7 +982,8 @@ static int bind_init (void)
   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
   curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
   curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, bind_curl_error);
-  curl_easy_setopt (curl, CURLOPT_URL, url);
+  curl_easy_setopt (curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL);
+
   return (0);
 } /* int bind_init */
 
@@ -713,12 +997,6 @@ static int bind_read (void)
     return (-1);
   }
 
-  if (url == NULL)
-  {
-    ERROR ("bind plugin: No URL has been configured.");
-    return (-1);
-  }
-
   bind_buffer_fill = 0;
   if (curl_easy_perform (curl) != 0)
   {
@@ -734,11 +1012,23 @@ static int bind_read (void)
     return (0);
 } /* int bind_read */
 
+static int bind_shutdown (void)
+{
+  if (curl != NULL)
+  {
+    curl_easy_cleanup (curl);
+    curl = NULL;
+  }
+
+  return (0);
+} /* int bind_shutdown */
+
 void module_register (void)
 {
   plugin_register_config ("bind", bind_config, config_keys, config_keys_num);
   plugin_register_init ("bind", bind_init);
   plugin_register_read ("bind", bind_read);
+  plugin_register_shutdown ("bind", bind_shutdown);
 } /* void module_register */
 
 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */
index 936fb1e..51ca0be 100644 (file)
@@ -110,13 +110,21 @@ FQDNLookup   true
 #</Plugin>
 
 #<Plugin bind>
-#      URL "http://localhost/"
-#      RRQueriesIn true
+#      URL "http://localhost:8053/"
+#      # Features
 #      Requests true
+#      Rejects true
+#      Responses true
+#      Queries true
+#      RCode true
+#      ZoneStats true
+#      OpCodes true
+#      Resolver true
+#      DNSSEC true
+#      RRQueriesIn true
 #      QueryResults true
 #      Updates true
 #      ZoneMaintenance true
-#      Resolver true
 #</Plugin>
 
 #<Plugin csv>
index 1103ad8..6d01532 100644 (file)
@@ -29,6 +29,10 @@ dns_request          value:COUNTER:0:65535
 dns_resolver           value:COUNTER:0:65535
 dns_update             value:COUNTER:0:65535
 dns_zops               value:COUNTER:0:65535
+dns_notify             value:COUNTER:0:65535
+dns_transfer           value:COUNTER:0:65535
+dns_query              value:COUNTER:0:65535
+dns_response           value:COUNTER:0:65535
 email_check            value:GAUGE:0:U
 email_count            value:GAUGE:0:U
 email_size             value:GAUGE:0:U