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_gauge"
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 if (view->cacherrsets != 0) /* {{{ */
827 char plugin_instance[DATA_MAX_NAME_LEN];
828 list_info_ptr_t list_info =
831 /* type = */ "dns_qtype_gauge"
834 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-cache_rr_sets",
837 bind_parse_generic_name_value (/* xpath = */ "cache/rrset",
838 /* callback = */ bind_xml_list_callback,
839 /* user_data = */ &list_info,
840 doc, path_ctx, current_time, DS_TYPE_GAUGE);
843 if (view->zones_num > 0)
844 bind_xml_stats_search_zones (version, doc, path_ctx, node, view,
848 } /* }}} int bind_xml_stats_handle_view */
850 static int bind_xml_stats_search_views (int version, xmlDoc *doc, /* {{{ */
851 xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time)
853 xmlXPathObject *view_nodes = NULL;
854 xmlXPathContext *view_path_context;
857 view_path_context = xmlXPathNewContext (doc);
858 if (view_path_context == NULL)
860 ERROR ("bind plugin: xmlXPathNewContext failed.");
864 view_nodes = xmlXPathEvalExpression (BAD_CAST "views/view", xpathCtx);
865 if (view_nodes == NULL)
867 ERROR ("bind plugin: Cannot find any <view> tags.");
868 xmlXPathFreeContext (view_path_context);
872 for (i = 0; i < view_nodes->nodesetval->nodeNr; i++)
876 node = view_nodes->nodesetval->nodeTab[i];
877 assert (node != NULL);
879 view_path_context->node = node;
881 bind_xml_stats_handle_view (version, doc, view_path_context, node,
885 xmlXPathFreeObject (view_nodes);
886 xmlXPathFreeContext (view_path_context);
888 } /* }}} int bind_xml_stats_search_views */
890 static int bind_xml_stats (int version, xmlDoc *doc, /* {{{ */
891 xmlXPathContext *xpathCtx, xmlNode *statsnode)
893 time_t current_time = 0;
896 xpathCtx->node = statsnode;
898 /* TODO: Check `server/boot-time' to recognize server restarts. */
900 status = bind_xml_read_timestamp ("server/current-time",
901 doc, xpathCtx, ¤t_time);
904 ERROR ("bind plugin: Reading `server/current-time' failed.");
907 DEBUG ("bind plugin: Current server time is %i.", (int) current_time);
909 /* XPath: server/requests/opcode
910 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
914 * <counter>1</counter>
918 if (global_opcodes != 0)
920 list_info_ptr_t list_info =
922 /* plugin instance = */ "global-opcodes",
923 /* type = */ "dns_opcode"
926 bind_parse_generic_name_value (/* xpath = */ "server/requests/opcode",
927 /* callback = */ bind_xml_list_callback,
928 /* user_data = */ &list_info,
929 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
932 /* XPath: server/queries-in/rdtype
933 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
934 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
935 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
939 * <counter>1</counter>
943 if (global_qtypes != 0)
945 list_info_ptr_t list_info =
947 /* plugin instance = */ "global-qtypes",
948 /* type = */ "dns_qtype"
951 bind_parse_generic_name_value (/* xpath = */ "server/queries-in/rdtype",
952 /* callback = */ bind_xml_list_callback,
953 /* user_data = */ &list_info,
954 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
957 /* XPath: server/nsstats, server/nsstat
958 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
959 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
960 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
961 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
962 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
963 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
964 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
968 * <Requestv4>1</Requestv4>
969 * <Requestv6>0</Requestv6>
974 * <name>Requestv4</name>
975 * <counter>1</counter>
978 * <name>Requestv6</name>
979 * <counter>0</counter>
983 if (global_server_stats)
985 translation_table_ptr_t table_ptr =
987 nsstats_translation_table,
988 nsstats_translation_table_length,
989 /* plugin_instance = */ "global-server_stats"
994 bind_parse_generic_value_list ("server/nsstats",
995 /* callback = */ bind_xml_table_callback,
996 /* user_data = */ &table_ptr,
997 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1001 bind_parse_generic_name_value ("server/nsstat",
1002 /* callback = */ bind_xml_table_callback,
1003 /* user_data = */ &table_ptr,
1004 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1008 /* XPath: server/zonestats, server/zonestat
1009 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1010 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1011 * XfrSuccess, XfrFail
1014 * <NotifyOutv4>0</NotifyOutv4>
1015 * <NotifyOutv6>0</NotifyOutv6>
1020 * <name>NotifyOutv4</name>
1021 * <counter>0</counter>
1024 * <name>NotifyOutv6</name>
1025 * <counter>0</counter>
1029 if (global_zone_maint_stats)
1031 translation_table_ptr_t table_ptr =
1033 zonestats_translation_table,
1034 zonestats_translation_table_length,
1035 /* plugin_instance = */ "global-zone_maint_stats"
1040 bind_parse_generic_value_list ("server/zonestats",
1041 /* callback = */ bind_xml_table_callback,
1042 /* user_data = */ &table_ptr,
1043 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1047 bind_parse_generic_name_value ("server/zonestat",
1048 /* callback = */ bind_xml_table_callback,
1049 /* user_data = */ &table_ptr,
1050 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1054 /* XPath: server/resstats
1055 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1056 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1057 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1058 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1061 * <Queryv4>0</Queryv4>
1062 * <Queryv6>0</Queryv6>
1067 * <name>Queryv4</name>
1068 * <counter>0</counter>
1071 * <name>Queryv6</name>
1072 * <counter>0</counter>
1076 if (global_resolver_stats != 0)
1078 translation_table_ptr_t table_ptr =
1080 resstats_translation_table,
1081 resstats_translation_table_length,
1082 /* plugin_instance = */ "global-resolver_stats"
1087 bind_parse_generic_value_list ("server/resstats",
1088 /* callback = */ bind_xml_table_callback,
1089 /* user_data = */ &table_ptr,
1090 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1094 bind_parse_generic_name_value ("server/resstat",
1095 /* callback = */ bind_xml_table_callback,
1096 /* user_data = */ &table_ptr,
1097 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1101 /* XPath: memory/summary
1102 * Variables: TotalUse, InUse, BlockSize, ContextSize, Lost
1105 * <TotalUse>6587096</TotalUse>
1106 * <InUse>1345424</InUse>
1107 * <BlockSize>5505024</BlockSize>
1108 * <ContextSize>3732456</ContextSize>
1112 if (global_memory_stats != 0)
1114 translation_table_ptr_t table_ptr =
1116 memsummary_translation_table,
1117 memsummary_translation_table_length,
1118 /* plugin_instance = */ "global-memory_stats"
1121 bind_parse_generic_value_list ("memory/summary",
1122 /* callback = */ bind_xml_table_callback,
1123 /* user_data = */ &table_ptr,
1124 doc, xpathCtx, current_time, DS_TYPE_GAUGE);
1128 bind_xml_stats_search_views (version, doc, xpathCtx, statsnode,
1132 } /* }}} int bind_xml_stats */
1134 static int bind_xml (const char *data) /* {{{ */
1137 xmlXPathContext *xpathCtx = NULL;
1138 xmlXPathObject *xpathObj = NULL;
1142 doc = xmlParseMemory (data, strlen (data));
1145 ERROR ("bind plugin: xmlParseMemory failed.");
1149 xpathCtx = xmlXPathNewContext (doc);
1150 if (xpathCtx == NULL)
1152 ERROR ("bind plugin: xmlXPathNewContext failed.");
1157 xpathObj = xmlXPathEvalExpression (BAD_CAST "/isc/bind/statistics", xpathCtx);
1158 if (xpathObj == NULL)
1160 ERROR ("bind plugin: Cannot find the <statistics> tag.");
1161 xmlXPathFreeContext (xpathCtx);
1165 else if (xpathObj->nodesetval == NULL)
1167 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
1168 xmlXPathFreeObject (xpathObj);
1169 xmlXPathFreeContext (xpathCtx);
1174 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
1178 int parsed_version = 0;
1180 node = xpathObj->nodesetval->nodeTab[i];
1181 assert (node != NULL);
1183 attr_version = (char *) xmlGetProp (node, BAD_CAST "version");
1184 if (attr_version == NULL)
1186 NOTICE ("bind plugin: Found <statistics> tag doesn't have a "
1187 "`version' attribute.");
1190 DEBUG ("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1192 /* At the time this plugin was written, version "1.0" was used by
1193 * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
1194 * that "1.*" and "2.*" don't introduce structural changes, so we just
1195 * check for the first two characters here. */
1196 if (strncmp ("1.", attr_version, strlen ("1.")) == 0)
1198 else if (strncmp ("2.", attr_version, strlen ("2.")) == 0)
1202 /* TODO: Use the complaint mechanism here. */
1203 NOTICE ("bind plugin: Found <statistics> tag with version `%s'. "
1204 "Unfortunately I have no clue how to parse that. "
1205 "Please open a bug report for this.", attr_version);
1206 xmlFree (attr_version);
1210 ret = bind_xml_stats (parsed_version,
1211 doc, xpathCtx, node);
1213 xmlFree (attr_version);
1214 /* One <statistics> node ought to be enough. */
1218 xmlXPathFreeObject (xpathObj);
1219 xmlXPathFreeContext (xpathCtx);
1223 } /* }}} int bind_xml */
1225 static int bind_config_set_bool (const char *name, int *var, /* {{{ */
1228 if ((ci->values_num != 1) || ( ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1230 WARNING ("bind plugin: The `%s' option needs "
1231 "exactly one boolean argument.", name);
1235 if (ci->values[0].value.boolean)
1240 } /* }}} int bind_config_set_bool */
1242 static int bind_config_add_view_zone (cb_view_t *view, /* {{{ */
1247 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1249 WARNING ("bind plugin: The `Zone' option needs "
1250 "exactly one string argument.");
1254 tmp = (char **) realloc (view->zones,
1255 sizeof (char *) * (view->zones_num + 1));
1258 ERROR ("bind plugin: realloc failed.");
1263 view->zones[view->zones_num] = strdup (ci->values[0].value.string);
1264 if (view->zones[view->zones_num] == NULL)
1266 ERROR ("bind plugin: strdup failed.");
1272 } /* }}} int bind_config_add_view_zone */
1274 static int bind_config_add_view (oconfig_item_t *ci) /* {{{ */
1279 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1281 WARNING ("bind plugin: `View' blocks need exactly one string argument.");
1285 tmp = (cb_view_t *) realloc (views, sizeof (*views) * (views_num + 1));
1288 ERROR ("bind plugin: realloc failed.");
1292 tmp = views + views_num;
1294 memset (tmp, 0, sizeof (*tmp));
1296 tmp->resolver_stats = 1;
1297 tmp->cacherrsets = 1;
1301 tmp->name = strdup (ci->values[0].value.string);
1302 if (tmp->name == NULL)
1304 ERROR ("bind plugin: strdup failed.");
1309 for (i = 0; i < ci->children_num; i++)
1311 oconfig_item_t *child = ci->children + i;
1313 if (strcasecmp ("QTypes", child->key) == 0)
1314 bind_config_set_bool ("QTypes", &tmp->qtypes, child);
1315 else if (strcasecmp ("ResolverStats", child->key) == 0)
1316 bind_config_set_bool ("ResolverStats", &tmp->resolver_stats, child);
1317 else if (strcasecmp ("CacheRRSets", child->key) == 0)
1318 bind_config_set_bool ("CacheRRSets", &tmp->cacherrsets, child);
1319 else if (strcasecmp ("Zone", child->key) == 0)
1320 bind_config_add_view_zone (tmp, child);
1323 WARNING ("bind plugin: Unknown configuration option "
1324 "`%s' in view `%s' will be ignored.", child->key, tmp->name);
1326 } /* for (i = 0; i < ci->children_num; i++) */
1330 } /* }}} int bind_config_add_view */
1332 static int bind_config (oconfig_item_t *ci) /* {{{ */
1336 for (i = 0; i < ci->children_num; i++)
1338 oconfig_item_t *child = ci->children + i;
1340 if (strcasecmp ("Url", child->key) == 0) {
1341 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING))
1343 WARNING ("bind plugin: The `Url' option needs "
1344 "exactly one string argument.");
1348 url = strdup (child->values[0].value.string);
1349 } else if (strcasecmp ("OpCodes", child->key) == 0)
1350 bind_config_set_bool ("OpCodes", &global_opcodes, child);
1351 else if (strcasecmp ("QTypes", child->key) == 0)
1352 bind_config_set_bool ("QTypes", &global_qtypes, child);
1353 else if (strcasecmp ("ServerStats", child->key) == 0)
1354 bind_config_set_bool ("ServerStats", &global_server_stats, child);
1355 else if (strcasecmp ("ZoneMaintStats", child->key) == 0)
1356 bind_config_set_bool ("ZoneMaintStats", &global_zone_maint_stats, child);
1357 else if (strcasecmp ("ResolverStats", child->key) == 0)
1358 bind_config_set_bool ("ResolverStats", &global_resolver_stats, child);
1359 else if (strcasecmp ("MemoryStats", child->key) == 0)
1360 bind_config_set_bool ("MemoryStats", &global_memory_stats, child);
1361 else if (strcasecmp ("View", child->key) == 0)
1362 bind_config_add_view (child);
1365 WARNING ("bind plugin: Unknown configuration option "
1366 "`%s' will be ignored.", child->key);
1371 } /* }}} int bind_config */
1373 static int bind_init (void) /* {{{ */
1378 curl = curl_easy_init ();
1381 ERROR ("bind plugin: bind_init: curl_easy_init failed.");
1385 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
1386 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
1387 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, bind_curl_error);
1388 curl_easy_setopt (curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL);
1389 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
1392 } /* }}} int bind_init */
1394 static int bind_read (void) /* {{{ */
1400 ERROR ("bind plugin: I don't have a CURL object.");
1404 bind_buffer_fill = 0;
1405 if (curl_easy_perform (curl) != 0)
1407 ERROR ("bind plugin: curl_easy_perform failed: %s",
1412 status = bind_xml (bind_buffer);
1417 } /* }}} int bind_read */
1419 static int bind_shutdown (void) /* {{{ */
1423 curl_easy_cleanup (curl);
1428 } /* }}} int bind_shutdown */
1430 void module_register (void)
1432 plugin_register_complex_config ("bind", bind_config);
1433 plugin_register_init ("bind", bind_init);
1434 plugin_register_read ("bind", bind_read);
1435 plugin_register_shutdown ("bind", bind_shutdown);
1436 } /* void module_register */
1438 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */