2 * collectd - src/bind.c
3 * Copyright (C) 2009 Bruno Prémont
4 * Copyright (C) 2009 Florian Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Bruno Prémont <bonbons at linux-vserver.org>
21 * Florian Forster <octo at verplant.org>
24 #define _XOPEN_SOURCE 600 /* glibc2 needs this for strptime */
29 #include "configfile.h"
31 /* Some versions of libcurl don't include this themselves and then don't have
32 * fd_set available. */
34 # include <sys/select.h>
37 #include <curl/curl.h>
38 #include <libxml/parser.h>
39 #include <libxml/xpath.h>
41 #ifndef BIND_DEFAULT_URL
42 # define BIND_DEFAULT_URL "http://localhost:8053/"
46 * Some types used for the callback functions. `translation_table_ptr_t' and
47 * `list_info_ptr_t' are passed to the callbacks in the `void *user_data'
50 typedef int (*list_callback_t) (const char *name, value_t value,
51 time_t current_time, void *user_data);
64 typedef struct cb_view_s cb_view_t;
66 struct translation_info_s
70 const char *type_instance;
72 typedef struct translation_info_s translation_info_t;
74 struct translation_table_ptr_s
76 const translation_info_t *table;
78 const char *plugin_instance;
80 typedef struct translation_table_ptr_s translation_table_ptr_t;
82 struct list_info_ptr_s
84 const char *plugin_instance;
87 typedef struct list_info_ptr_s list_info_ptr_t;
89 static char *url = NULL;
90 static int global_opcodes = 1;
91 static int global_qtypes = 1;
92 static int global_server_stats = 1;
93 static int global_zone_maint_stats = 1;
94 static int global_resolver_stats = 0;
95 static int global_memory_stats = 1;
97 static cb_view_t *views = NULL;
98 static size_t views_num = 0;
100 static CURL *curl = NULL;
102 static char *bind_buffer = NULL;
103 static size_t bind_buffer_size = 0;
104 static size_t bind_buffer_fill = 0;
105 static char bind_curl_error[CURL_ERROR_SIZE];
107 /* Translation table for the `nsstats' values. */
108 static const translation_info_t nsstats_translation_table[] = /* {{{ */
111 { "Requestv4", "dns_request", "IPv4" },
112 { "Requestv6", "dns_request", "IPv6" },
113 { "ReqEdns0", "dns_request", "EDNS0" },
114 { "ReqBadEDNSVer", "dns_request", "BadEDNSVer" },
115 { "ReqTSIG", "dns_request", "TSIG" },
116 { "ReqSIG0", "dns_request", "SIG0" },
117 { "ReqBadSIG", "dns_request", "BadSIG" },
118 { "ReqTCP", "dns_request", "TCP" },
120 { "AuthQryRej", "dns_reject", "authorative" },
121 { "RecQryRej", "dns_reject", "recursive" },
122 { "XfrRej", "dns_reject", "transfer" },
123 { "UpdateRej", "dns_reject", "update" },
125 { "Response", "dns_response", "normal" },
126 { "TruncatedResp", "dns_response", "truncated" },
127 { "RespEDNS0", "dns_response", "EDNS0" },
128 { "RespTSIG", "dns_response", "TSIG" },
129 { "RespSIG0", "dns_response", "SIG0" },
131 { "QryAuthAns", "dns_query", "authorative" },
132 { "QryNoauthAns", "dns_query", "nonauth" },
133 { "QryReferral", "dns_query", "referral" },
134 { "QryRecursion", "dns_query", "recursion" },
135 { "QryDuplicate", "dns_query", "dupliate" },
136 { "QryDropped", "dns_query", "dropped" },
137 { "QryFailure", "dns_query", "failure" },
139 { "QrySuccess", "dns_rcode", "tx-NOERROR" },
140 { "QryNxrrset", "dns_rcode", "tx-NXRRSET" },
141 { "QrySERVFAIL", "dns_rcode", "tx-SERVFAIL" },
142 { "QryFORMERR", "dns_rcode", "tx-FORMERR" },
143 { "QryNXDOMAIN", "dns_rcode", "tx-NXDOMAIN" }
145 { "XfrReqDone", "type", "type_instance" },
146 { "UpdateReqFwd", "type", "type_instance" },
147 { "UpdateRespFwd", "type", "type_instance" },
148 { "UpdateFwdFail", "type", "type_instance" },
149 { "UpdateDone", "type", "type_instance" },
150 { "UpdateFail", "type", "type_instance" },
151 { "UpdateBadPrereq", "type", "type_instance" },
154 static int nsstats_translation_table_length =
155 STATIC_ARRAY_SIZE (nsstats_translation_table);
158 /* Translation table for the `zonestats' values. */
159 static const translation_info_t zonestats_translation_table[] = /* {{{ */
162 { "NotifyOutv4", "dns_notify", "tx-IPv4" },
163 { "NotifyOutv6", "dns_notify", "tx-IPv6" },
164 { "NotifyInv4", "dns_notify", "rx-IPv4" },
165 { "NotifyInv6", "dns_notify", "rx-IPv6" },
166 { "NotifyRej", "dns_notify", "rejected" },
167 /* SOA/AXFS/IXFS requests */
168 { "SOAOutv4", "dns_opcode", "SOA-IPv4" },
169 { "SOAOutv6", "dns_opcode", "SOA-IPv6" },
170 { "AXFRReqv4", "dns_opcode", "AXFR-IPv4" },
171 { "AXFRReqv6", "dns_opcode", "AXFR-IPv6" },
172 { "IXFRReqv4", "dns_opcode", "IXFR-IPv4" },
173 { "IXFRReqv6", "dns_opcode", "IXFR-IPv6" },
174 /* Domain transfers */
175 { "XfrSuccess", "dns_transfer", "success" },
176 { "XfrFail", "dns_transfer", "failure" }
178 static int zonestats_translation_table_length =
179 STATIC_ARRAY_SIZE (zonestats_translation_table);
182 /* Translation table for the `resstats' values. */
183 static const translation_info_t resstats_translation_table[] = /* {{{ */
185 /* Generic resolver information */
186 { "Queryv4", "dns_query", "IPv4" },
187 { "Queryv6", "dns_query", "IPv6" },
188 { "Responsev4", "dns_response", "IPv4" },
189 { "Responsev6", "dns_response", "IPv6" },
190 /* Received response codes */
191 { "NXDOMAIN", "dns_rcode", "rx-NXDOMAIN" },
192 { "SERVFAIL", "dns_rcode", "rx-SERVFAIL" },
193 { "FORMERR", "dns_rcode", "rx-FORMERR" },
194 { "OtherError", "dns_rcode", "rx-OTHER" },
195 { "EDNS0Fail", "dns_rcode", "rx-EDNS0Fail"},
196 /* Received responses */
197 { "Mismatch", "dns_response", "mismatch" },
198 { "Truncated", "dns_response", "truncated" },
199 { "Lame", "dns_response", "lame" },
200 { "Retry", "dns_query", "retry" },
202 { "GlueFetchv4", "type", "type_instance" },
203 { "GlueFetchv6", "type", "type_instance" },
204 { "GlueFetchv4Fail", "type", "type_instance" },
205 { "GlueFetchv6Fail", "type", "type_instance" },
207 /* DNSSEC information */
208 { "ValAttempt", "dns_resolver", "DNSSEC-attempt" },
209 { "ValOk", "dns_resolver", "DNSSEC-okay" },
210 { "ValNegOk", "dns_resolver", "DNSSEC-negokay" },
211 { "ValFail", "dns_resolver", "DNSSEC-fail" }
213 static int resstats_translation_table_length =
214 STATIC_ARRAY_SIZE (resstats_translation_table);
217 /* Translation table for the `memory/summary' values. */
218 static const translation_info_t memsummary_translation_table[] = /* {{{ */
220 { "TotalUse", "memory", "TotalUse" },
221 { "InUse", "memory", "InUse" },
222 { "BlockSize", "memory", "BlockSize" },
223 { "ContextSize", "memory", "ContextSize" },
224 { "Lost", "memory", "Lost" }
226 static int memsummary_translation_table_length =
227 STATIC_ARRAY_SIZE (memsummary_translation_table);
230 static void submit (time_t ts, const char *plugin_instance, /* {{{ */
231 const char *type, const char *type_instance, value_t value)
234 value_list_t vl = VALUE_LIST_INIT;
241 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
242 sstrncpy(vl.plugin, "bind", sizeof(vl.plugin));
243 if (plugin_instance) {
244 sstrncpy(vl.plugin_instance, plugin_instance,
245 sizeof(vl.plugin_instance));
246 replace_special (vl.plugin_instance, sizeof (vl.plugin_instance));
248 sstrncpy(vl.type, type, sizeof(vl.type));
250 sstrncpy(vl.type_instance, type_instance,
251 sizeof(vl.type_instance));
252 replace_special (vl.plugin_instance, sizeof (vl.plugin_instance));
254 plugin_dispatch_values(&vl);
255 } /* }}} void submit */
257 static size_t bind_curl_callback (void *buf, size_t size, /* {{{ */
258 size_t nmemb, void __attribute__((unused)) *stream)
260 size_t len = size * nmemb;
265 if ((bind_buffer_fill + len) >= bind_buffer_size)
269 temp = realloc(bind_buffer, bind_buffer_fill + len + 1);
272 ERROR ("bind plugin: realloc failed.");
276 bind_buffer_size = bind_buffer_fill + len + 1;
279 memcpy (bind_buffer + bind_buffer_fill, (char *) buf, len);
280 bind_buffer_fill += len;
281 bind_buffer[bind_buffer_fill] = 0;
284 } /* }}} size_t bind_curl_callback */
287 * Callback, that's called with a translation table.
288 * (Plugin instance is fixed, type and type instance come from lookup table.)
290 static int bind_xml_table_callback (const char *name, value_t value, /* {{{ */
291 time_t current_time, void *user_data)
293 translation_table_ptr_t *table = (translation_table_ptr_t *) user_data;
299 for (i = 0; i < table->table_length; i++)
301 if (strcmp (table->table[i].xml_name, name) != 0)
304 submit (current_time,
305 table->plugin_instance,
306 table->table[i].type,
307 table->table[i].type_instance,
313 } /* }}} int bind_xml_table_callback */
316 * Callback, that's used for lists.
317 * (Plugin instance and type are fixed, xml name is used as type instance.)
319 static int bind_xml_list_callback (const char *name, /* {{{ */
320 value_t value, time_t current_time, void *user_data)
322 list_info_ptr_t *list_info = (list_info_ptr_t *) user_data;
324 if (list_info == NULL)
327 submit (current_time,
328 list_info->plugin_instance,
330 /* type instance = */ name,
334 } /* }}} int bind_xml_list_callback */
336 static int bind_xml_read_counter (xmlDoc *doc, xmlNode *node, /* {{{ */
337 counter_t *ret_value)
339 char *str_ptr, *end_ptr;
342 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
345 ERROR ("bind plugin: bind_xml_read_counter: xmlNodeListGetString failed.");
350 value = strtoll (str_ptr, &end_ptr, 10);
352 if (str_ptr == end_ptr || errno)
354 if (errno && (value < 0))
355 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed with underflow.");
356 else if (errno && (value > 0))
357 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed with overflow.");
359 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed.");
365 } /* }}} int bind_xml_read_counter */
367 static int bind_xml_read_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
370 char *str_ptr, *end_ptr;
373 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
376 ERROR ("bind plugin: bind_xml_read_gauge: xmlNodeListGetString failed.");
381 value = strtod (str_ptr, &end_ptr);
383 if (str_ptr == end_ptr || errno)
385 if (errno && (value < 0))
386 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with underflow.");
387 else if (errno && (value > 0))
388 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with overflow.");
390 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed.");
394 *ret_value = (gauge_t) value;
396 } /* }}} int bind_xml_read_gauge */
398 static int bind_xml_read_timestamp (const char *xpath_expression, /* {{{ */
399 xmlDoc *doc, xmlXPathContext *xpathCtx, time_t *ret_value)
401 xmlXPathObject *xpathObj = NULL;
407 xpathObj = xmlXPathEvalExpression (BAD_CAST xpath_expression, xpathCtx);
408 if (xpathObj == NULL)
410 ERROR ("bind plugin: Unable to evaluate XPath expression `%s'.",
415 if ((xpathObj->nodesetval == NULL) || (xpathObj->nodesetval->nodeNr < 1))
417 xmlXPathFreeObject (xpathObj);
421 if (xpathObj->nodesetval->nodeNr != 1)
423 NOTICE ("bind plugin: Evaluating the XPath expression `%s' returned "
424 "%i nodes. Only handling the first one.",
425 xpath_expression, xpathObj->nodesetval->nodeNr);
428 node = xpathObj->nodesetval->nodeTab[0];
430 if (node->xmlChildrenNode == NULL)
432 ERROR ("bind plugin: bind_xml_read_timestamp: "
433 "node->xmlChildrenNode == NULL");
434 xmlXPathFreeObject (xpathObj);
438 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
441 ERROR ("bind plugin: bind_xml_read_timestamp: xmlNodeListGetString failed.");
442 xmlXPathFreeObject (xpathObj);
446 memset (&tm, 0, sizeof(tm));
447 tmp = strptime (str_ptr, "%Y-%m-%dT%T", &tm);
451 ERROR ("bind plugin: bind_xml_read_timestamp: strptime failed.");
452 xmlXPathFreeObject (xpathObj);
456 *ret_value = mktime(&tm);
458 xmlXPathFreeObject (xpathObj);
460 } /* }}} int bind_xml_read_timestamp */
463 * bind_parse_generic_name_value
465 * Reads statistics in the form:
468 * <counter>123</counter>
471 static int bind_parse_generic_name_value (const char *xpath_expression, /* {{{ */
472 list_callback_t list_callback,
474 xmlDoc *doc, xmlXPathContext *xpathCtx,
475 time_t current_time, int ds_type)
477 xmlXPathObject *xpathObj = NULL;
481 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
482 if (xpathObj == NULL)
484 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
490 /* Iterate over all matching nodes. */
491 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
493 xmlNode *name_node = NULL;
494 xmlNode *counter = NULL;
498 parent = xpathObj->nodesetval->nodeTab[i];
499 DEBUG ("bind plugin: bind_parse_generic_name_value: parent->name = %s;",
500 (char *) parent->name);
502 /* Iterate over all child nodes. */
503 for (child = parent->xmlChildrenNode;
507 if (child->type != XML_ELEMENT_NODE)
510 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
512 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
516 if ((name_node != NULL) && (counter != NULL))
518 char *name = (char *) xmlNodeListGetString (doc,
519 name_node->xmlChildrenNode, 1);
523 if (ds_type == DS_TYPE_GAUGE)
524 status = bind_xml_read_gauge (doc, counter, &value.gauge);
526 status = bind_xml_read_counter (doc, counter, &value.counter);
530 status = (*list_callback) (name, value, current_time, user_data);
538 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
539 num_entries, (num_entries == 1) ? "entry" : "entries",
542 xmlXPathFreeObject(xpathObj);
545 } /* }}} int bind_parse_generic_name_value */
548 * bind_parse_generic_value_list
550 * Reads statistics in the form:
558 static int bind_parse_generic_value_list (const char *xpath_expression, /* {{{ */
559 list_callback_t list_callback,
561 xmlDoc *doc, xmlXPathContext *xpathCtx,
562 time_t current_time, int ds_type)
564 xmlXPathObject *xpathObj = NULL;
568 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
569 if (xpathObj == NULL)
571 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
577 /* Iterate over all matching nodes. */
578 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
582 /* Iterate over all child nodes. */
583 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
591 if (child->type != XML_ELEMENT_NODE)
594 node_name = (char *) child->name;
596 if (ds_type == DS_TYPE_GAUGE)
597 status = bind_xml_read_gauge (doc, child, &value.gauge);
599 status = bind_xml_read_counter (doc, child, &value.counter);
603 status = (*list_callback) (node_name, value, current_time, user_data);
609 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
610 num_entries, (num_entries == 1) ? "entry" : "entries",
613 xmlXPathFreeObject(xpathObj);
616 } /* }}} int bind_parse_generic_value_list */
618 static int bind_xml_stats_handle_zone (int version, xmlDoc *doc, /* {{{ */
619 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
622 xmlXPathObject *path_obj;
623 char *zone_name = NULL;
627 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
628 if (path_obj == NULL)
630 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
634 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
636 zone_name = (char *) xmlNodeListGetString (doc,
637 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
638 if (zone_name != NULL)
642 if (zone_name == NULL)
644 ERROR ("bind plugin: Could not determine zone name.");
645 xmlXPathFreeObject (path_obj);
649 for (j = 0; j < view->zones_num; j++)
651 if (strcasecmp (zone_name, view->zones[j]) == 0)
660 xmlXPathFreeObject (path_obj);
664 zone_name = view->zones[j];
666 DEBUG ("bind plugin: bind_xml_stats_handle_zone: Found zone `%s'.",
669 { /* Parse the <counters> tag {{{ */
670 char plugin_instance[DATA_MAX_NAME_LEN];
671 translation_table_ptr_t table_ptr =
673 nsstats_translation_table,
674 nsstats_translation_table_length,
678 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-zone-%s",
679 view->name, zone_name);
681 bind_parse_generic_value_list (/* xpath = */ "counters",
682 /* callback = */ bind_xml_table_callback,
683 /* user_data = */ &table_ptr,
684 doc, path_ctx, current_time, DS_TYPE_COUNTER);
687 xmlXPathFreeObject (path_obj);
690 } /* }}} int bind_xml_stats_handle_zone */
692 static int bind_xml_stats_search_zones (int version, xmlDoc *doc, /* {{{ */
693 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
696 xmlXPathObject *zone_nodes = NULL;
697 xmlXPathContext *zone_path_context;
700 zone_path_context = xmlXPathNewContext (doc);
701 if (zone_path_context == NULL)
703 ERROR ("bind plugin: xmlXPathNewContext failed.");
707 zone_nodes = xmlXPathEvalExpression (BAD_CAST "zones/zone", path_ctx);
708 if (zone_nodes == NULL)
710 ERROR ("bind plugin: Cannot find any <view> tags.");
711 xmlXPathFreeContext (zone_path_context);
715 for (i = 0; i < zone_nodes->nodesetval->nodeNr; i++)
719 node = zone_nodes->nodesetval->nodeTab[i];
720 assert (node != NULL);
722 zone_path_context->node = node;
724 bind_xml_stats_handle_zone (version, doc, zone_path_context, node, view,
728 xmlXPathFreeObject (zone_nodes);
729 xmlXPathFreeContext (zone_path_context);
731 } /* }}} int bind_xml_stats_search_zones */
733 static int bind_xml_stats_handle_view (int version, xmlDoc *doc, /* {{{ */
734 xmlXPathContext *path_ctx, xmlNode *node, time_t current_time)
736 xmlXPathObject *path_obj;
737 char *view_name = NULL;
742 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
743 if (path_obj == NULL)
745 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
749 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
751 view_name = (char *) xmlNodeListGetString (doc,
752 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
753 if (view_name != NULL)
757 if (view_name == NULL)
759 ERROR ("bind plugin: Could not determine view name.");
760 xmlXPathFreeObject (path_obj);
764 for (j = 0; j < views_num; j++)
766 if (strcasecmp (view_name, views[j].name) == 0)
771 xmlXPathFreeObject (path_obj);
781 DEBUG ("bind plugin: bind_xml_stats_handle_view: Found view `%s'.",
784 if (view->qtypes != 0) /* {{{ */
786 char plugin_instance[DATA_MAX_NAME_LEN];
787 list_info_ptr_t list_info =
790 /* type = */ "dns_qtype_gauge"
793 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-qtypes",
796 bind_parse_generic_name_value (/* xpath = */ "rdtype",
797 /* callback = */ bind_xml_list_callback,
798 /* user_data = */ &list_info,
799 doc, path_ctx, current_time, DS_TYPE_COUNTER);
802 if (view->resolver_stats != 0) /* {{{ */
804 char plugin_instance[DATA_MAX_NAME_LEN];
805 translation_table_ptr_t table_ptr =
807 resstats_translation_table,
808 resstats_translation_table_length,
812 ssnprintf (plugin_instance, sizeof (plugin_instance),
813 "%s-resolver_stats", view->name);
815 bind_parse_generic_name_value ("resstat",
816 /* callback = */ bind_xml_table_callback,
817 /* user_data = */ &table_ptr,
818 doc, path_ctx, current_time, DS_TYPE_COUNTER);
821 if (view->cacherrsets != 0) /* {{{ */
823 char plugin_instance[DATA_MAX_NAME_LEN];
824 list_info_ptr_t list_info =
827 /* type = */ "dns_qtype_gauge"
830 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-cache_rr_sets",
833 bind_parse_generic_name_value (/* xpath = */ "cache/rrset",
834 /* callback = */ bind_xml_list_callback,
835 /* user_data = */ &list_info,
836 doc, path_ctx, current_time, DS_TYPE_GAUGE);
839 if (view->zones_num > 0)
840 bind_xml_stats_search_zones (version, doc, path_ctx, node, view,
844 } /* }}} int bind_xml_stats_handle_view */
846 static int bind_xml_stats_search_views (int version, xmlDoc *doc, /* {{{ */
847 xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time)
849 xmlXPathObject *view_nodes = NULL;
850 xmlXPathContext *view_path_context;
853 view_path_context = xmlXPathNewContext (doc);
854 if (view_path_context == NULL)
856 ERROR ("bind plugin: xmlXPathNewContext failed.");
860 view_nodes = xmlXPathEvalExpression (BAD_CAST "views/view", xpathCtx);
861 if (view_nodes == NULL)
863 ERROR ("bind plugin: Cannot find any <view> tags.");
864 xmlXPathFreeContext (view_path_context);
868 for (i = 0; i < view_nodes->nodesetval->nodeNr; i++)
872 node = view_nodes->nodesetval->nodeTab[i];
873 assert (node != NULL);
875 view_path_context->node = node;
877 bind_xml_stats_handle_view (version, doc, view_path_context, node,
881 xmlXPathFreeObject (view_nodes);
882 xmlXPathFreeContext (view_path_context);
884 } /* }}} int bind_xml_stats_search_views */
886 static int bind_xml_stats (int version, xmlDoc *doc, /* {{{ */
887 xmlXPathContext *xpathCtx, xmlNode *statsnode)
889 time_t current_time = 0;
892 xpathCtx->node = statsnode;
894 /* TODO: Check `server/boot-time' to recognize server restarts. */
896 status = bind_xml_read_timestamp ("server/current-time",
897 doc, xpathCtx, ¤t_time);
900 ERROR ("bind plugin: Reading `server/current-time' failed.");
903 DEBUG ("bind plugin: Current server time is %i.", (int) current_time);
905 /* XPath: server/requests/opcode
906 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
910 * <counter>1</counter>
914 if (global_opcodes != 0)
916 list_info_ptr_t list_info =
918 /* plugin instance = */ "global-opcodes",
919 /* type = */ "dns_opcode"
922 bind_parse_generic_name_value (/* xpath = */ "server/requests/opcode",
923 /* callback = */ bind_xml_list_callback,
924 /* user_data = */ &list_info,
925 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
928 /* XPath: server/queries-in/rdtype
929 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
930 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
931 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
935 * <counter>1</counter>
939 if (global_qtypes != 0)
941 list_info_ptr_t list_info =
943 /* plugin instance = */ "global-qtypes",
944 /* type = */ "dns_qtype"
947 bind_parse_generic_name_value (/* xpath = */ "server/queries-in/rdtype",
948 /* callback = */ bind_xml_list_callback,
949 /* user_data = */ &list_info,
950 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
953 /* XPath: server/nsstats, server/nsstat
954 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
955 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
956 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
957 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
958 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
959 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
960 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
964 * <Requestv4>1</Requestv4>
965 * <Requestv6>0</Requestv6>
970 * <name>Requestv4</name>
971 * <counter>1</counter>
974 * <name>Requestv6</name>
975 * <counter>0</counter>
979 if (global_server_stats)
981 translation_table_ptr_t table_ptr =
983 nsstats_translation_table,
984 nsstats_translation_table_length,
985 /* plugin_instance = */ "global-server_stats"
990 bind_parse_generic_value_list ("server/nsstats",
991 /* callback = */ bind_xml_table_callback,
992 /* user_data = */ &table_ptr,
993 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
997 bind_parse_generic_name_value ("server/nsstat",
998 /* callback = */ bind_xml_table_callback,
999 /* user_data = */ &table_ptr,
1000 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1004 /* XPath: server/zonestats, server/zonestat
1005 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1006 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1007 * XfrSuccess, XfrFail
1010 * <NotifyOutv4>0</NotifyOutv4>
1011 * <NotifyOutv6>0</NotifyOutv6>
1016 * <name>NotifyOutv4</name>
1017 * <counter>0</counter>
1020 * <name>NotifyOutv6</name>
1021 * <counter>0</counter>
1025 if (global_zone_maint_stats)
1027 translation_table_ptr_t table_ptr =
1029 zonestats_translation_table,
1030 zonestats_translation_table_length,
1031 /* plugin_instance = */ "global-zone_maint_stats"
1036 bind_parse_generic_value_list ("server/zonestats",
1037 /* callback = */ bind_xml_table_callback,
1038 /* user_data = */ &table_ptr,
1039 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1043 bind_parse_generic_name_value ("server/zonestat",
1044 /* callback = */ bind_xml_table_callback,
1045 /* user_data = */ &table_ptr,
1046 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1050 /* XPath: server/resstats
1051 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1052 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1053 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1054 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1057 * <Queryv4>0</Queryv4>
1058 * <Queryv6>0</Queryv6>
1063 * <name>Queryv4</name>
1064 * <counter>0</counter>
1067 * <name>Queryv6</name>
1068 * <counter>0</counter>
1072 if (global_resolver_stats != 0)
1074 translation_table_ptr_t table_ptr =
1076 resstats_translation_table,
1077 resstats_translation_table_length,
1078 /* plugin_instance = */ "global-resolver_stats"
1083 bind_parse_generic_value_list ("server/resstats",
1084 /* callback = */ bind_xml_table_callback,
1085 /* user_data = */ &table_ptr,
1086 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1090 bind_parse_generic_name_value ("server/resstat",
1091 /* callback = */ bind_xml_table_callback,
1092 /* user_data = */ &table_ptr,
1093 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1097 /* XPath: memory/summary
1098 * Variables: TotalUse, InUse, BlockSize, ContextSize, Lost
1101 * <TotalUse>6587096</TotalUse>
1102 * <InUse>1345424</InUse>
1103 * <BlockSize>5505024</BlockSize>
1104 * <ContextSize>3732456</ContextSize>
1108 if (global_memory_stats != 0)
1110 translation_table_ptr_t table_ptr =
1112 memsummary_translation_table,
1113 memsummary_translation_table_length,
1114 /* plugin_instance = */ "global-memory_stats"
1117 bind_parse_generic_value_list ("memory/summary",
1118 /* callback = */ bind_xml_table_callback,
1119 /* user_data = */ &table_ptr,
1120 doc, xpathCtx, current_time, DS_TYPE_GAUGE);
1124 bind_xml_stats_search_views (version, doc, xpathCtx, statsnode,
1128 } /* }}} int bind_xml_stats */
1130 static int bind_xml (const char *data) /* {{{ */
1133 xmlXPathContext *xpathCtx = NULL;
1134 xmlXPathObject *xpathObj = NULL;
1138 doc = xmlParseMemory (data, strlen (data));
1141 ERROR ("bind plugin: xmlParseMemory failed.");
1145 xpathCtx = xmlXPathNewContext (doc);
1146 if (xpathCtx == NULL)
1148 ERROR ("bind plugin: xmlXPathNewContext failed.");
1153 xpathObj = xmlXPathEvalExpression (BAD_CAST "/isc/bind/statistics", xpathCtx);
1154 if (xpathObj == NULL)
1156 ERROR ("bind plugin: Cannot find the <statistics> tag.");
1157 xmlXPathFreeContext (xpathCtx);
1161 else if (xpathObj->nodesetval == NULL)
1163 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
1164 xmlXPathFreeObject (xpathObj);
1165 xmlXPathFreeContext (xpathCtx);
1170 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
1174 int parsed_version = 0;
1176 node = xpathObj->nodesetval->nodeTab[i];
1177 assert (node != NULL);
1179 attr_version = (char *) xmlGetProp (node, BAD_CAST "version");
1180 if (attr_version == NULL)
1182 NOTICE ("bind plugin: Found <statistics> tag doesn't have a "
1183 "`version' attribute.");
1186 DEBUG ("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1188 /* At the time this plugin was written, version "1.0" was used by
1189 * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
1190 * that "1.*" and "2.*" don't introduce structural changes, so we just
1191 * check for the first two characters here. */
1192 if (strncmp ("1.", attr_version, strlen ("1.")) == 0)
1194 else if (strncmp ("2.", attr_version, strlen ("2.")) == 0)
1198 /* TODO: Use the complaint mechanism here. */
1199 NOTICE ("bind plugin: Found <statistics> tag with version `%s'. "
1200 "Unfortunately I have no clue how to parse that. "
1201 "Please open a bug report for this.", attr_version);
1202 xmlFree (attr_version);
1206 ret = bind_xml_stats (parsed_version,
1207 doc, xpathCtx, node);
1209 xmlFree (attr_version);
1210 /* One <statistics> node ought to be enough. */
1214 xmlXPathFreeObject (xpathObj);
1215 xmlXPathFreeContext (xpathCtx);
1219 } /* }}} int bind_xml */
1221 static int bind_config_set_bool (const char *name, int *var, /* {{{ */
1224 if ((ci->values_num != 1) || ( ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1226 WARNING ("bind plugin: The `%s' option needs "
1227 "exactly one boolean argument.", name);
1231 if (ci->values[0].value.boolean)
1236 } /* }}} int bind_config_set_bool */
1238 static int bind_config_add_view_zone (cb_view_t *view, /* {{{ */
1243 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1245 WARNING ("bind plugin: The `Zone' option needs "
1246 "exactly one string argument.");
1250 tmp = (char **) realloc (view->zones,
1251 sizeof (char *) * (view->zones_num + 1));
1254 ERROR ("bind plugin: realloc failed.");
1259 view->zones[view->zones_num] = strdup (ci->values[0].value.string);
1260 if (view->zones[view->zones_num] == NULL)
1262 ERROR ("bind plugin: strdup failed.");
1268 } /* }}} int bind_config_add_view_zone */
1270 static int bind_config_add_view (oconfig_item_t *ci) /* {{{ */
1275 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1277 WARNING ("bind plugin: `View' blocks need exactly one string argument.");
1281 tmp = (cb_view_t *) realloc (views, sizeof (*views) * (views_num + 1));
1284 ERROR ("bind plugin: realloc failed.");
1288 tmp = views + views_num;
1290 memset (tmp, 0, sizeof (*tmp));
1292 tmp->resolver_stats = 1;
1293 tmp->cacherrsets = 1;
1297 tmp->name = strdup (ci->values[0].value.string);
1298 if (tmp->name == NULL)
1300 ERROR ("bind plugin: strdup failed.");
1305 for (i = 0; i < ci->children_num; i++)
1307 oconfig_item_t *child = ci->children + i;
1309 if (strcasecmp ("QTypes", child->key) == 0)
1310 bind_config_set_bool ("QTypes", &tmp->qtypes, child);
1311 else if (strcasecmp ("ResolverStats", child->key) == 0)
1312 bind_config_set_bool ("ResolverStats", &tmp->resolver_stats, child);
1313 else if (strcasecmp ("CacheRRSets", child->key) == 0)
1314 bind_config_set_bool ("CacheRRSets", &tmp->cacherrsets, child);
1315 else if (strcasecmp ("Zone", child->key) == 0)
1316 bind_config_add_view_zone (tmp, child);
1319 WARNING ("bind plugin: Unknown configuration option "
1320 "`%s' in view `%s' will be ignored.", child->key, tmp->name);
1322 } /* for (i = 0; i < ci->children_num; i++) */
1326 } /* }}} int bind_config_add_view */
1328 static int bind_config (oconfig_item_t *ci) /* {{{ */
1332 for (i = 0; i < ci->children_num; i++)
1334 oconfig_item_t *child = ci->children + i;
1336 if (strcasecmp ("Url", child->key) == 0) {
1337 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING))
1339 WARNING ("bind plugin: The `Url' option needs "
1340 "exactly one string argument.");
1344 url = strdup (child->values[0].value.string);
1345 } else if (strcasecmp ("OpCodes", child->key) == 0)
1346 bind_config_set_bool ("OpCodes", &global_opcodes, child);
1347 else if (strcasecmp ("QTypes", child->key) == 0)
1348 bind_config_set_bool ("QTypes", &global_qtypes, child);
1349 else if (strcasecmp ("ServerStats", child->key) == 0)
1350 bind_config_set_bool ("ServerStats", &global_server_stats, child);
1351 else if (strcasecmp ("ZoneMaintStats", child->key) == 0)
1352 bind_config_set_bool ("ZoneMaintStats", &global_zone_maint_stats, child);
1353 else if (strcasecmp ("ResolverStats", child->key) == 0)
1354 bind_config_set_bool ("ResolverStats", &global_resolver_stats, child);
1355 else if (strcasecmp ("MemoryStats", child->key) == 0)
1356 bind_config_set_bool ("MemoryStats", &global_memory_stats, child);
1357 else if (strcasecmp ("View", child->key) == 0)
1358 bind_config_add_view (child);
1361 WARNING ("bind plugin: Unknown configuration option "
1362 "`%s' will be ignored.", child->key);
1367 } /* }}} int bind_config */
1369 static int bind_init (void) /* {{{ */
1374 curl = curl_easy_init ();
1377 ERROR ("bind plugin: bind_init: curl_easy_init failed.");
1381 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
1382 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
1383 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, bind_curl_error);
1384 curl_easy_setopt (curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL);
1385 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
1388 } /* }}} int bind_init */
1390 static int bind_read (void) /* {{{ */
1396 ERROR ("bind plugin: I don't have a CURL object.");
1400 bind_buffer_fill = 0;
1401 if (curl_easy_perform (curl) != 0)
1403 ERROR ("bind plugin: curl_easy_perform failed: %s",
1408 status = bind_xml (bind_buffer);
1413 } /* }}} int bind_read */
1415 static int bind_shutdown (void) /* {{{ */
1419 curl_easy_cleanup (curl);
1424 } /* }}} int bind_shutdown */
1426 void module_register (void)
1428 plugin_register_complex_config ("bind", bind_config);
1429 plugin_register_init ("bind", bind_init);
1430 plugin_register_read ("bind", bind_read);
1431 plugin_register_shutdown ("bind", bind_shutdown);
1432 } /* void module_register */
1434 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */