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>
27 # define _XOPEN_SOURCE 600 /* glibc2 needs this for strptime */
33 #include "configfile.h"
35 /* Some versions of libcurl don't include this themselves and then don't have
36 * fd_set available. */
38 # include <sys/select.h>
41 #include <curl/curl.h>
42 #include <libxml/parser.h>
43 #include <libxml/xpath.h>
45 #ifndef BIND_DEFAULT_URL
46 # define BIND_DEFAULT_URL "http://localhost:8053/"
50 * Some types used for the callback functions. `translation_table_ptr_t' and
51 * `list_info_ptr_t' are passed to the callbacks in the `void *user_data'
54 typedef int (*list_callback_t) (const char *name, value_t value,
55 time_t current_time, void *user_data);
68 typedef struct cb_view_s cb_view_t;
70 struct translation_info_s
74 const char *type_instance;
76 typedef struct translation_info_s translation_info_t;
78 struct translation_table_ptr_s
80 const translation_info_t *table;
82 const char *plugin_instance;
84 typedef struct translation_table_ptr_s translation_table_ptr_t;
86 struct list_info_ptr_s
88 const char *plugin_instance;
91 typedef struct list_info_ptr_s list_info_ptr_t;
93 static char *url = NULL;
94 static int global_opcodes = 1;
95 static int global_qtypes = 1;
96 static int global_server_stats = 1;
97 static int global_zone_maint_stats = 1;
98 static int global_resolver_stats = 0;
99 static int global_memory_stats = 1;
101 static cb_view_t *views = NULL;
102 static size_t views_num = 0;
104 static CURL *curl = NULL;
106 static char *bind_buffer = NULL;
107 static size_t bind_buffer_size = 0;
108 static size_t bind_buffer_fill = 0;
109 static char bind_curl_error[CURL_ERROR_SIZE];
111 /* Translation table for the `nsstats' values. */
112 static const translation_info_t nsstats_translation_table[] = /* {{{ */
115 { "Requestv4", "dns_request", "IPv4" },
116 { "Requestv6", "dns_request", "IPv6" },
117 { "ReqEdns0", "dns_request", "EDNS0" },
118 { "ReqBadEDNSVer", "dns_request", "BadEDNSVer" },
119 { "ReqTSIG", "dns_request", "TSIG" },
120 { "ReqSIG0", "dns_request", "SIG0" },
121 { "ReqBadSIG", "dns_request", "BadSIG" },
122 { "ReqTCP", "dns_request", "TCP" },
124 { "AuthQryRej", "dns_reject", "authorative" },
125 { "RecQryRej", "dns_reject", "recursive" },
126 { "XfrRej", "dns_reject", "transfer" },
127 { "UpdateRej", "dns_reject", "update" },
129 { "Response", "dns_response", "normal" },
130 { "TruncatedResp", "dns_response", "truncated" },
131 { "RespEDNS0", "dns_response", "EDNS0" },
132 { "RespTSIG", "dns_response", "TSIG" },
133 { "RespSIG0", "dns_response", "SIG0" },
135 { "QryAuthAns", "dns_query", "authorative" },
136 { "QryNoauthAns", "dns_query", "nonauth" },
137 { "QryReferral", "dns_query", "referral" },
138 { "QryRecursion", "dns_query", "recursion" },
139 { "QryDuplicate", "dns_query", "dupliate" },
140 { "QryDropped", "dns_query", "dropped" },
141 { "QryFailure", "dns_query", "failure" },
143 { "QrySuccess", "dns_rcode", "tx-NOERROR" },
144 { "QryNxrrset", "dns_rcode", "tx-NXRRSET" },
145 { "QrySERVFAIL", "dns_rcode", "tx-SERVFAIL" },
146 { "QryFORMERR", "dns_rcode", "tx-FORMERR" },
147 { "QryNXDOMAIN", "dns_rcode", "tx-NXDOMAIN" }
149 { "XfrReqDone", "type", "type_instance" },
150 { "UpdateReqFwd", "type", "type_instance" },
151 { "UpdateRespFwd", "type", "type_instance" },
152 { "UpdateFwdFail", "type", "type_instance" },
153 { "UpdateDone", "type", "type_instance" },
154 { "UpdateFail", "type", "type_instance" },
155 { "UpdateBadPrereq", "type", "type_instance" },
158 static int nsstats_translation_table_length =
159 STATIC_ARRAY_SIZE (nsstats_translation_table);
162 /* Translation table for the `zonestats' values. */
163 static const translation_info_t zonestats_translation_table[] = /* {{{ */
166 { "NotifyOutv4", "dns_notify", "tx-IPv4" },
167 { "NotifyOutv6", "dns_notify", "tx-IPv6" },
168 { "NotifyInv4", "dns_notify", "rx-IPv4" },
169 { "NotifyInv6", "dns_notify", "rx-IPv6" },
170 { "NotifyRej", "dns_notify", "rejected" },
171 /* SOA/AXFS/IXFS requests */
172 { "SOAOutv4", "dns_opcode", "SOA-IPv4" },
173 { "SOAOutv6", "dns_opcode", "SOA-IPv6" },
174 { "AXFRReqv4", "dns_opcode", "AXFR-IPv4" },
175 { "AXFRReqv6", "dns_opcode", "AXFR-IPv6" },
176 { "IXFRReqv4", "dns_opcode", "IXFR-IPv4" },
177 { "IXFRReqv6", "dns_opcode", "IXFR-IPv6" },
178 /* Domain transfers */
179 { "XfrSuccess", "dns_transfer", "success" },
180 { "XfrFail", "dns_transfer", "failure" }
182 static int zonestats_translation_table_length =
183 STATIC_ARRAY_SIZE (zonestats_translation_table);
186 /* Translation table for the `resstats' values. */
187 static const translation_info_t resstats_translation_table[] = /* {{{ */
189 /* Generic resolver information */
190 { "Queryv4", "dns_query", "IPv4" },
191 { "Queryv6", "dns_query", "IPv6" },
192 { "Responsev4", "dns_response", "IPv4" },
193 { "Responsev6", "dns_response", "IPv6" },
194 /* Received response codes */
195 { "NXDOMAIN", "dns_rcode", "rx-NXDOMAIN" },
196 { "SERVFAIL", "dns_rcode", "rx-SERVFAIL" },
197 { "FORMERR", "dns_rcode", "rx-FORMERR" },
198 { "OtherError", "dns_rcode", "rx-OTHER" },
199 { "EDNS0Fail", "dns_rcode", "rx-EDNS0Fail"},
200 /* Received responses */
201 { "Mismatch", "dns_response", "mismatch" },
202 { "Truncated", "dns_response", "truncated" },
203 { "Lame", "dns_response", "lame" },
204 { "Retry", "dns_query", "retry" },
206 { "GlueFetchv4", "type", "type_instance" },
207 { "GlueFetchv6", "type", "type_instance" },
208 { "GlueFetchv4Fail", "type", "type_instance" },
209 { "GlueFetchv6Fail", "type", "type_instance" },
211 /* DNSSEC information */
212 { "ValAttempt", "dns_resolver", "DNSSEC-attempt" },
213 { "ValOk", "dns_resolver", "DNSSEC-okay" },
214 { "ValNegOk", "dns_resolver", "DNSSEC-negokay" },
215 { "ValFail", "dns_resolver", "DNSSEC-fail" }
217 static int resstats_translation_table_length =
218 STATIC_ARRAY_SIZE (resstats_translation_table);
221 /* Translation table for the `memory/summary' values. */
222 static const translation_info_t memsummary_translation_table[] = /* {{{ */
224 { "TotalUse", "memory", "TotalUse" },
225 { "InUse", "memory", "InUse" },
226 { "BlockSize", "memory", "BlockSize" },
227 { "ContextSize", "memory", "ContextSize" },
228 { "Lost", "memory", "Lost" }
230 static int memsummary_translation_table_length =
231 STATIC_ARRAY_SIZE (memsummary_translation_table);
234 static void submit (time_t ts, const char *plugin_instance, /* {{{ */
235 const char *type, const char *type_instance, value_t value)
238 value_list_t vl = VALUE_LIST_INIT;
245 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
246 sstrncpy(vl.plugin, "bind", sizeof(vl.plugin));
247 if (plugin_instance) {
248 sstrncpy(vl.plugin_instance, plugin_instance,
249 sizeof(vl.plugin_instance));
250 replace_special (vl.plugin_instance, sizeof (vl.plugin_instance));
252 sstrncpy(vl.type, type, sizeof(vl.type));
254 sstrncpy(vl.type_instance, type_instance,
255 sizeof(vl.type_instance));
256 replace_special (vl.plugin_instance, sizeof (vl.plugin_instance));
258 plugin_dispatch_values(&vl);
259 } /* }}} void submit */
261 static size_t bind_curl_callback (void *buf, size_t size, /* {{{ */
262 size_t nmemb, void __attribute__((unused)) *stream)
264 size_t len = size * nmemb;
269 if ((bind_buffer_fill + len) >= bind_buffer_size)
273 temp = realloc(bind_buffer, bind_buffer_fill + len + 1);
276 ERROR ("bind plugin: realloc failed.");
280 bind_buffer_size = bind_buffer_fill + len + 1;
283 memcpy (bind_buffer + bind_buffer_fill, (char *) buf, len);
284 bind_buffer_fill += len;
285 bind_buffer[bind_buffer_fill] = 0;
288 } /* }}} size_t bind_curl_callback */
291 * Callback, that's called with a translation table.
292 * (Plugin instance is fixed, type and type instance come from lookup table.)
294 static int bind_xml_table_callback (const char *name, value_t value, /* {{{ */
295 time_t current_time, void *user_data)
297 translation_table_ptr_t *table = (translation_table_ptr_t *) user_data;
303 for (i = 0; i < table->table_length; i++)
305 if (strcmp (table->table[i].xml_name, name) != 0)
308 submit (current_time,
309 table->plugin_instance,
310 table->table[i].type,
311 table->table[i].type_instance,
317 } /* }}} int bind_xml_table_callback */
320 * Callback, that's used for lists.
321 * (Plugin instance and type are fixed, xml name is used as type instance.)
323 static int bind_xml_list_callback (const char *name, /* {{{ */
324 value_t value, time_t current_time, void *user_data)
326 list_info_ptr_t *list_info = (list_info_ptr_t *) user_data;
328 if (list_info == NULL)
331 submit (current_time,
332 list_info->plugin_instance,
334 /* type instance = */ name,
338 } /* }}} int bind_xml_list_callback */
340 static int bind_xml_read_counter (xmlDoc *doc, xmlNode *node, /* {{{ */
341 counter_t *ret_value)
343 char *str_ptr, *end_ptr;
346 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
349 ERROR ("bind plugin: bind_xml_read_counter: xmlNodeListGetString failed.");
354 value = strtoll (str_ptr, &end_ptr, 10);
356 if (str_ptr == end_ptr || errno)
358 if (errno && (value < 0))
359 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed with underflow.");
360 else if (errno && (value > 0))
361 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed with overflow.");
363 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed.");
369 } /* }}} int bind_xml_read_counter */
371 static int bind_xml_read_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
374 char *str_ptr, *end_ptr;
377 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
380 ERROR ("bind plugin: bind_xml_read_gauge: xmlNodeListGetString failed.");
385 value = strtod (str_ptr, &end_ptr);
387 if (str_ptr == end_ptr || errno)
389 if (errno && (value < 0))
390 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with underflow.");
391 else if (errno && (value > 0))
392 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with overflow.");
394 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed.");
398 *ret_value = (gauge_t) value;
400 } /* }}} int bind_xml_read_gauge */
402 static int bind_xml_read_timestamp (const char *xpath_expression, /* {{{ */
403 xmlDoc *doc, xmlXPathContext *xpathCtx, time_t *ret_value)
405 xmlXPathObject *xpathObj = NULL;
411 xpathObj = xmlXPathEvalExpression (BAD_CAST xpath_expression, xpathCtx);
412 if (xpathObj == NULL)
414 ERROR ("bind plugin: Unable to evaluate XPath expression `%s'.",
419 if ((xpathObj->nodesetval == NULL) || (xpathObj->nodesetval->nodeNr < 1))
421 xmlXPathFreeObject (xpathObj);
425 if (xpathObj->nodesetval->nodeNr != 1)
427 NOTICE ("bind plugin: Evaluating the XPath expression `%s' returned "
428 "%i nodes. Only handling the first one.",
429 xpath_expression, xpathObj->nodesetval->nodeNr);
432 node = xpathObj->nodesetval->nodeTab[0];
434 if (node->xmlChildrenNode == NULL)
436 ERROR ("bind plugin: bind_xml_read_timestamp: "
437 "node->xmlChildrenNode == NULL");
438 xmlXPathFreeObject (xpathObj);
442 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
445 ERROR ("bind plugin: bind_xml_read_timestamp: xmlNodeListGetString failed.");
446 xmlXPathFreeObject (xpathObj);
450 memset (&tm, 0, sizeof(tm));
451 tmp = strptime (str_ptr, "%Y-%m-%dT%T", &tm);
455 ERROR ("bind plugin: bind_xml_read_timestamp: strptime failed.");
456 xmlXPathFreeObject (xpathObj);
460 *ret_value = mktime(&tm);
462 xmlXPathFreeObject (xpathObj);
464 } /* }}} int bind_xml_read_timestamp */
467 * bind_parse_generic_name_value
469 * Reads statistics in the form:
472 * <counter>123</counter>
475 static int bind_parse_generic_name_value (const char *xpath_expression, /* {{{ */
476 list_callback_t list_callback,
478 xmlDoc *doc, xmlXPathContext *xpathCtx,
479 time_t current_time, int ds_type)
481 xmlXPathObject *xpathObj = NULL;
485 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
486 if (xpathObj == NULL)
488 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
494 /* Iterate over all matching nodes. */
495 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
497 xmlNode *name_node = NULL;
498 xmlNode *counter = NULL;
502 parent = xpathObj->nodesetval->nodeTab[i];
503 DEBUG ("bind plugin: bind_parse_generic_name_value: parent->name = %s;",
504 (char *) parent->name);
506 /* Iterate over all child nodes. */
507 for (child = parent->xmlChildrenNode;
511 if (child->type != XML_ELEMENT_NODE)
514 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
516 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
520 if ((name_node != NULL) && (counter != NULL))
522 char *name = (char *) xmlNodeListGetString (doc,
523 name_node->xmlChildrenNode, 1);
527 if (ds_type == DS_TYPE_GAUGE)
528 status = bind_xml_read_gauge (doc, counter, &value.gauge);
530 status = bind_xml_read_counter (doc, counter, &value.counter);
534 status = (*list_callback) (name, value, current_time, user_data);
542 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
543 num_entries, (num_entries == 1) ? "entry" : "entries",
546 xmlXPathFreeObject(xpathObj);
549 } /* }}} int bind_parse_generic_name_value */
552 * bind_parse_generic_value_list
554 * Reads statistics in the form:
562 static int bind_parse_generic_value_list (const char *xpath_expression, /* {{{ */
563 list_callback_t list_callback,
565 xmlDoc *doc, xmlXPathContext *xpathCtx,
566 time_t current_time, int ds_type)
568 xmlXPathObject *xpathObj = NULL;
572 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
573 if (xpathObj == NULL)
575 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
581 /* Iterate over all matching nodes. */
582 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
586 /* Iterate over all child nodes. */
587 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
595 if (child->type != XML_ELEMENT_NODE)
598 node_name = (char *) child->name;
600 if (ds_type == DS_TYPE_GAUGE)
601 status = bind_xml_read_gauge (doc, child, &value.gauge);
603 status = bind_xml_read_counter (doc, child, &value.counter);
607 status = (*list_callback) (node_name, value, current_time, user_data);
613 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
614 num_entries, (num_entries == 1) ? "entry" : "entries",
617 xmlXPathFreeObject(xpathObj);
620 } /* }}} int bind_parse_generic_value_list */
622 static int bind_xml_stats_handle_zone (int version, xmlDoc *doc, /* {{{ */
623 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
626 xmlXPathObject *path_obj;
627 char *zone_name = NULL;
631 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
632 if (path_obj == NULL)
634 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
638 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
640 zone_name = (char *) xmlNodeListGetString (doc,
641 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
642 if (zone_name != NULL)
646 if (zone_name == NULL)
648 ERROR ("bind plugin: Could not determine zone name.");
649 xmlXPathFreeObject (path_obj);
653 for (j = 0; j < view->zones_num; j++)
655 if (strcasecmp (zone_name, view->zones[j]) == 0)
664 xmlXPathFreeObject (path_obj);
668 zone_name = view->zones[j];
670 DEBUG ("bind plugin: bind_xml_stats_handle_zone: Found zone `%s'.",
673 { /* Parse the <counters> tag {{{ */
674 char plugin_instance[DATA_MAX_NAME_LEN];
675 translation_table_ptr_t table_ptr =
677 nsstats_translation_table,
678 nsstats_translation_table_length,
682 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-zone-%s",
683 view->name, zone_name);
685 bind_parse_generic_value_list (/* xpath = */ "counters",
686 /* callback = */ bind_xml_table_callback,
687 /* user_data = */ &table_ptr,
688 doc, path_ctx, current_time, DS_TYPE_COUNTER);
691 xmlXPathFreeObject (path_obj);
694 } /* }}} int bind_xml_stats_handle_zone */
696 static int bind_xml_stats_search_zones (int version, xmlDoc *doc, /* {{{ */
697 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
700 xmlXPathObject *zone_nodes = NULL;
701 xmlXPathContext *zone_path_context;
704 zone_path_context = xmlXPathNewContext (doc);
705 if (zone_path_context == NULL)
707 ERROR ("bind plugin: xmlXPathNewContext failed.");
711 zone_nodes = xmlXPathEvalExpression (BAD_CAST "zones/zone", path_ctx);
712 if (zone_nodes == NULL)
714 ERROR ("bind plugin: Cannot find any <view> tags.");
715 xmlXPathFreeContext (zone_path_context);
719 for (i = 0; i < zone_nodes->nodesetval->nodeNr; i++)
723 node = zone_nodes->nodesetval->nodeTab[i];
724 assert (node != NULL);
726 zone_path_context->node = node;
728 bind_xml_stats_handle_zone (version, doc, zone_path_context, node, view,
732 xmlXPathFreeObject (zone_nodes);
733 xmlXPathFreeContext (zone_path_context);
735 } /* }}} int bind_xml_stats_search_zones */
737 static int bind_xml_stats_handle_view (int version, xmlDoc *doc, /* {{{ */
738 xmlXPathContext *path_ctx, xmlNode *node, time_t current_time)
740 xmlXPathObject *path_obj;
741 char *view_name = NULL;
746 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
747 if (path_obj == NULL)
749 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
753 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
755 view_name = (char *) xmlNodeListGetString (doc,
756 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
757 if (view_name != NULL)
761 if (view_name == NULL)
763 ERROR ("bind plugin: Could not determine view name.");
764 xmlXPathFreeObject (path_obj);
768 for (j = 0; j < views_num; j++)
770 if (strcasecmp (view_name, views[j].name) == 0)
775 xmlXPathFreeObject (path_obj);
785 DEBUG ("bind plugin: bind_xml_stats_handle_view: Found view `%s'.",
788 if (view->qtypes != 0) /* {{{ */
790 char plugin_instance[DATA_MAX_NAME_LEN];
791 list_info_ptr_t list_info =
794 /* type = */ "dns_qtype"
797 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-qtypes",
800 bind_parse_generic_name_value (/* xpath = */ "rdtype",
801 /* callback = */ bind_xml_list_callback,
802 /* user_data = */ &list_info,
803 doc, path_ctx, current_time, DS_TYPE_COUNTER);
806 if (view->resolver_stats != 0) /* {{{ */
808 char plugin_instance[DATA_MAX_NAME_LEN];
809 translation_table_ptr_t table_ptr =
811 resstats_translation_table,
812 resstats_translation_table_length,
816 ssnprintf (plugin_instance, sizeof (plugin_instance),
817 "%s-resolver_stats", view->name);
819 bind_parse_generic_name_value ("resstat",
820 /* callback = */ bind_xml_table_callback,
821 /* user_data = */ &table_ptr,
822 doc, path_ctx, current_time, DS_TYPE_COUNTER);
825 /* Record types in the cache */
826 if (view->cacherrsets != 0) /* {{{ */
828 char plugin_instance[DATA_MAX_NAME_LEN];
829 list_info_ptr_t list_info =
832 /* type = */ "dns_qtype_cached"
835 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-cache_rr_sets",
838 bind_parse_generic_name_value (/* xpath = */ "cache/rrset",
839 /* callback = */ bind_xml_list_callback,
840 /* user_data = */ &list_info,
841 doc, path_ctx, current_time, DS_TYPE_GAUGE);
844 if (view->zones_num > 0)
845 bind_xml_stats_search_zones (version, doc, path_ctx, node, view,
849 } /* }}} int bind_xml_stats_handle_view */
851 static int bind_xml_stats_search_views (int version, xmlDoc *doc, /* {{{ */
852 xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time)
854 xmlXPathObject *view_nodes = NULL;
855 xmlXPathContext *view_path_context;
858 view_path_context = xmlXPathNewContext (doc);
859 if (view_path_context == NULL)
861 ERROR ("bind plugin: xmlXPathNewContext failed.");
865 view_nodes = xmlXPathEvalExpression (BAD_CAST "views/view", xpathCtx);
866 if (view_nodes == NULL)
868 ERROR ("bind plugin: Cannot find any <view> tags.");
869 xmlXPathFreeContext (view_path_context);
873 for (i = 0; i < view_nodes->nodesetval->nodeNr; i++)
877 node = view_nodes->nodesetval->nodeTab[i];
878 assert (node != NULL);
880 view_path_context->node = node;
882 bind_xml_stats_handle_view (version, doc, view_path_context, node,
886 xmlXPathFreeObject (view_nodes);
887 xmlXPathFreeContext (view_path_context);
889 } /* }}} int bind_xml_stats_search_views */
891 static int bind_xml_stats (int version, xmlDoc *doc, /* {{{ */
892 xmlXPathContext *xpathCtx, xmlNode *statsnode)
894 time_t current_time = 0;
897 xpathCtx->node = statsnode;
899 /* TODO: Check `server/boot-time' to recognize server restarts. */
901 status = bind_xml_read_timestamp ("server/current-time",
902 doc, xpathCtx, ¤t_time);
905 ERROR ("bind plugin: Reading `server/current-time' failed.");
908 DEBUG ("bind plugin: Current server time is %i.", (int) current_time);
910 /* XPath: server/requests/opcode
911 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
915 * <counter>1</counter>
919 if (global_opcodes != 0)
921 list_info_ptr_t list_info =
923 /* plugin instance = */ "global-opcodes",
924 /* type = */ "dns_opcode"
927 bind_parse_generic_name_value (/* xpath = */ "server/requests/opcode",
928 /* callback = */ bind_xml_list_callback,
929 /* user_data = */ &list_info,
930 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
933 /* XPath: server/queries-in/rdtype
934 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
935 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
936 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
940 * <counter>1</counter>
944 if (global_qtypes != 0)
946 list_info_ptr_t list_info =
948 /* plugin instance = */ "global-qtypes",
949 /* type = */ "dns_qtype"
952 bind_parse_generic_name_value (/* xpath = */ "server/queries-in/rdtype",
953 /* callback = */ bind_xml_list_callback,
954 /* user_data = */ &list_info,
955 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
958 /* XPath: server/nsstats, server/nsstat
959 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
960 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
961 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
962 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
963 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
964 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
965 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
969 * <Requestv4>1</Requestv4>
970 * <Requestv6>0</Requestv6>
975 * <name>Requestv4</name>
976 * <counter>1</counter>
979 * <name>Requestv6</name>
980 * <counter>0</counter>
984 if (global_server_stats)
986 translation_table_ptr_t table_ptr =
988 nsstats_translation_table,
989 nsstats_translation_table_length,
990 /* plugin_instance = */ "global-server_stats"
995 bind_parse_generic_value_list ("server/nsstats",
996 /* callback = */ bind_xml_table_callback,
997 /* user_data = */ &table_ptr,
998 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1002 bind_parse_generic_name_value ("server/nsstat",
1003 /* callback = */ bind_xml_table_callback,
1004 /* user_data = */ &table_ptr,
1005 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1009 /* XPath: server/zonestats, server/zonestat
1010 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1011 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1012 * XfrSuccess, XfrFail
1015 * <NotifyOutv4>0</NotifyOutv4>
1016 * <NotifyOutv6>0</NotifyOutv6>
1021 * <name>NotifyOutv4</name>
1022 * <counter>0</counter>
1025 * <name>NotifyOutv6</name>
1026 * <counter>0</counter>
1030 if (global_zone_maint_stats)
1032 translation_table_ptr_t table_ptr =
1034 zonestats_translation_table,
1035 zonestats_translation_table_length,
1036 /* plugin_instance = */ "global-zone_maint_stats"
1041 bind_parse_generic_value_list ("server/zonestats",
1042 /* callback = */ bind_xml_table_callback,
1043 /* user_data = */ &table_ptr,
1044 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1048 bind_parse_generic_name_value ("server/zonestat",
1049 /* callback = */ bind_xml_table_callback,
1050 /* user_data = */ &table_ptr,
1051 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1055 /* XPath: server/resstats
1056 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1057 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1058 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1059 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1062 * <Queryv4>0</Queryv4>
1063 * <Queryv6>0</Queryv6>
1068 * <name>Queryv4</name>
1069 * <counter>0</counter>
1072 * <name>Queryv6</name>
1073 * <counter>0</counter>
1077 if (global_resolver_stats != 0)
1079 translation_table_ptr_t table_ptr =
1081 resstats_translation_table,
1082 resstats_translation_table_length,
1083 /* plugin_instance = */ "global-resolver_stats"
1088 bind_parse_generic_value_list ("server/resstats",
1089 /* callback = */ bind_xml_table_callback,
1090 /* user_data = */ &table_ptr,
1091 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1095 bind_parse_generic_name_value ("server/resstat",
1096 /* callback = */ bind_xml_table_callback,
1097 /* user_data = */ &table_ptr,
1098 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1102 /* XPath: memory/summary
1103 * Variables: TotalUse, InUse, BlockSize, ContextSize, Lost
1106 * <TotalUse>6587096</TotalUse>
1107 * <InUse>1345424</InUse>
1108 * <BlockSize>5505024</BlockSize>
1109 * <ContextSize>3732456</ContextSize>
1113 if (global_memory_stats != 0)
1115 translation_table_ptr_t table_ptr =
1117 memsummary_translation_table,
1118 memsummary_translation_table_length,
1119 /* plugin_instance = */ "global-memory_stats"
1122 bind_parse_generic_value_list ("memory/summary",
1123 /* callback = */ bind_xml_table_callback,
1124 /* user_data = */ &table_ptr,
1125 doc, xpathCtx, current_time, DS_TYPE_GAUGE);
1129 bind_xml_stats_search_views (version, doc, xpathCtx, statsnode,
1133 } /* }}} int bind_xml_stats */
1135 static int bind_xml (const char *data) /* {{{ */
1138 xmlXPathContext *xpathCtx = NULL;
1139 xmlXPathObject *xpathObj = NULL;
1143 doc = xmlParseMemory (data, strlen (data));
1146 ERROR ("bind plugin: xmlParseMemory failed.");
1150 xpathCtx = xmlXPathNewContext (doc);
1151 if (xpathCtx == NULL)
1153 ERROR ("bind plugin: xmlXPathNewContext failed.");
1158 xpathObj = xmlXPathEvalExpression (BAD_CAST "/isc/bind/statistics", xpathCtx);
1159 if (xpathObj == NULL)
1161 ERROR ("bind plugin: Cannot find the <statistics> tag.");
1162 xmlXPathFreeContext (xpathCtx);
1166 else if (xpathObj->nodesetval == NULL)
1168 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
1169 xmlXPathFreeObject (xpathObj);
1170 xmlXPathFreeContext (xpathCtx);
1175 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
1179 int parsed_version = 0;
1181 node = xpathObj->nodesetval->nodeTab[i];
1182 assert (node != NULL);
1184 attr_version = (char *) xmlGetProp (node, BAD_CAST "version");
1185 if (attr_version == NULL)
1187 NOTICE ("bind plugin: Found <statistics> tag doesn't have a "
1188 "`version' attribute.");
1191 DEBUG ("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1193 /* At the time this plugin was written, version "1.0" was used by
1194 * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
1195 * that "1.*" and "2.*" don't introduce structural changes, so we just
1196 * check for the first two characters here. */
1197 if (strncmp ("1.", attr_version, strlen ("1.")) == 0)
1199 else if (strncmp ("2.", attr_version, strlen ("2.")) == 0)
1203 /* TODO: Use the complaint mechanism here. */
1204 NOTICE ("bind plugin: Found <statistics> tag with version `%s'. "
1205 "Unfortunately I have no clue how to parse that. "
1206 "Please open a bug report for this.", attr_version);
1207 xmlFree (attr_version);
1211 ret = bind_xml_stats (parsed_version,
1212 doc, xpathCtx, node);
1214 xmlFree (attr_version);
1215 /* One <statistics> node ought to be enough. */
1219 xmlXPathFreeObject (xpathObj);
1220 xmlXPathFreeContext (xpathCtx);
1224 } /* }}} int bind_xml */
1226 static int bind_config_set_bool (const char *name, int *var, /* {{{ */
1229 if ((ci->values_num != 1) || ( ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1231 WARNING ("bind plugin: The `%s' option needs "
1232 "exactly one boolean argument.", name);
1236 if (ci->values[0].value.boolean)
1241 } /* }}} int bind_config_set_bool */
1243 static int bind_config_add_view_zone (cb_view_t *view, /* {{{ */
1248 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1250 WARNING ("bind plugin: The `Zone' option needs "
1251 "exactly one string argument.");
1255 tmp = (char **) realloc (view->zones,
1256 sizeof (char *) * (view->zones_num + 1));
1259 ERROR ("bind plugin: realloc failed.");
1264 view->zones[view->zones_num] = strdup (ci->values[0].value.string);
1265 if (view->zones[view->zones_num] == NULL)
1267 ERROR ("bind plugin: strdup failed.");
1273 } /* }}} int bind_config_add_view_zone */
1275 static int bind_config_add_view (oconfig_item_t *ci) /* {{{ */
1280 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1282 WARNING ("bind plugin: `View' blocks need exactly one string argument.");
1286 tmp = (cb_view_t *) realloc (views, sizeof (*views) * (views_num + 1));
1289 ERROR ("bind plugin: realloc failed.");
1293 tmp = views + views_num;
1295 memset (tmp, 0, sizeof (*tmp));
1297 tmp->resolver_stats = 1;
1298 tmp->cacherrsets = 1;
1302 tmp->name = strdup (ci->values[0].value.string);
1303 if (tmp->name == NULL)
1305 ERROR ("bind plugin: strdup failed.");
1310 for (i = 0; i < ci->children_num; i++)
1312 oconfig_item_t *child = ci->children + i;
1314 if (strcasecmp ("QTypes", child->key) == 0)
1315 bind_config_set_bool ("QTypes", &tmp->qtypes, child);
1316 else if (strcasecmp ("ResolverStats", child->key) == 0)
1317 bind_config_set_bool ("ResolverStats", &tmp->resolver_stats, child);
1318 else if (strcasecmp ("CacheRRSets", child->key) == 0)
1319 bind_config_set_bool ("CacheRRSets", &tmp->cacherrsets, child);
1320 else if (strcasecmp ("Zone", child->key) == 0)
1321 bind_config_add_view_zone (tmp, child);
1324 WARNING ("bind plugin: Unknown configuration option "
1325 "`%s' in view `%s' will be ignored.", child->key, tmp->name);
1327 } /* for (i = 0; i < ci->children_num; i++) */
1331 } /* }}} int bind_config_add_view */
1333 static int bind_config (oconfig_item_t *ci) /* {{{ */
1337 for (i = 0; i < ci->children_num; i++)
1339 oconfig_item_t *child = ci->children + i;
1341 if (strcasecmp ("Url", child->key) == 0) {
1342 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING))
1344 WARNING ("bind plugin: The `Url' option needs "
1345 "exactly one string argument.");
1349 url = strdup (child->values[0].value.string);
1350 } else if (strcasecmp ("OpCodes", child->key) == 0)
1351 bind_config_set_bool ("OpCodes", &global_opcodes, child);
1352 else if (strcasecmp ("QTypes", child->key) == 0)
1353 bind_config_set_bool ("QTypes", &global_qtypes, child);
1354 else if (strcasecmp ("ServerStats", child->key) == 0)
1355 bind_config_set_bool ("ServerStats", &global_server_stats, child);
1356 else if (strcasecmp ("ZoneMaintStats", child->key) == 0)
1357 bind_config_set_bool ("ZoneMaintStats", &global_zone_maint_stats, child);
1358 else if (strcasecmp ("ResolverStats", child->key) == 0)
1359 bind_config_set_bool ("ResolverStats", &global_resolver_stats, child);
1360 else if (strcasecmp ("MemoryStats", child->key) == 0)
1361 bind_config_set_bool ("MemoryStats", &global_memory_stats, child);
1362 else if (strcasecmp ("View", child->key) == 0)
1363 bind_config_add_view (child);
1366 WARNING ("bind plugin: Unknown configuration option "
1367 "`%s' will be ignored.", child->key);
1372 } /* }}} int bind_config */
1374 static int bind_init (void) /* {{{ */
1379 curl = curl_easy_init ();
1382 ERROR ("bind plugin: bind_init: curl_easy_init failed.");
1386 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L);
1387 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
1388 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
1389 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, bind_curl_error);
1390 curl_easy_setopt (curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL);
1391 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
1392 curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
1395 } /* }}} int bind_init */
1397 static int bind_read (void) /* {{{ */
1403 ERROR ("bind plugin: I don't have a CURL object.");
1407 bind_buffer_fill = 0;
1408 if (curl_easy_perform (curl) != CURLE_OK)
1410 ERROR ("bind plugin: curl_easy_perform failed: %s",
1415 status = bind_xml (bind_buffer);
1420 } /* }}} int bind_read */
1422 static int bind_shutdown (void) /* {{{ */
1426 curl_easy_cleanup (curl);
1431 } /* }}} int bind_shutdown */
1433 void module_register (void)
1435 plugin_register_complex_config ("bind", bind_config);
1436 plugin_register_init ("bind", bind_init);
1437 plugin_register_read ("bind", bind_read);
1438 plugin_register_shutdown ("bind", bind_shutdown);
1439 } /* void module_register */
1441 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */