2 * collectd - src/bind.c
3 * Copyright (C) 2009 Bruno Prémont
4 * Copyright (C) 2009,2010 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 collectd.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;
244 vl.time = TIME_T_TO_CDTIME_T (ts);
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_derive (xmlDoc *doc, xmlNode *node, /* {{{ */
347 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
350 ERROR ("bind plugin: bind_xml_read_derive: xmlNodeListGetString failed.");
354 status = parse_value (str_ptr, &value, DS_TYPE_DERIVE);
357 ERROR ("bind plugin: Parsing string \"%s\" to derive value failed.",
362 *ret_value = value.derive;
364 } /* }}} int bind_xml_read_derive */
366 static int bind_xml_read_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
369 char *str_ptr, *end_ptr;
372 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
375 ERROR ("bind plugin: bind_xml_read_gauge: xmlNodeListGetString failed.");
380 value = strtod (str_ptr, &end_ptr);
382 if (str_ptr == end_ptr || errno)
384 if (errno && (value < 0))
385 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with underflow.");
386 else if (errno && (value > 0))
387 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with overflow.");
389 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed.");
393 *ret_value = (gauge_t) value;
395 } /* }}} int bind_xml_read_gauge */
397 static int bind_xml_read_timestamp (const char *xpath_expression, /* {{{ */
398 xmlDoc *doc, xmlXPathContext *xpathCtx, time_t *ret_value)
400 xmlXPathObject *xpathObj = NULL;
406 xpathObj = xmlXPathEvalExpression (BAD_CAST xpath_expression, xpathCtx);
407 if (xpathObj == NULL)
409 ERROR ("bind plugin: Unable to evaluate XPath expression `%s'.",
414 if ((xpathObj->nodesetval == NULL) || (xpathObj->nodesetval->nodeNr < 1))
416 xmlXPathFreeObject (xpathObj);
420 if (xpathObj->nodesetval->nodeNr != 1)
422 NOTICE ("bind plugin: Evaluating the XPath expression `%s' returned "
423 "%i nodes. Only handling the first one.",
424 xpath_expression, xpathObj->nodesetval->nodeNr);
427 node = xpathObj->nodesetval->nodeTab[0];
429 if (node->xmlChildrenNode == NULL)
431 ERROR ("bind plugin: bind_xml_read_timestamp: "
432 "node->xmlChildrenNode == NULL");
433 xmlXPathFreeObject (xpathObj);
437 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
440 ERROR ("bind plugin: bind_xml_read_timestamp: xmlNodeListGetString failed.");
441 xmlXPathFreeObject (xpathObj);
445 memset (&tm, 0, sizeof(tm));
446 tmp = strptime (str_ptr, "%Y-%m-%dT%T", &tm);
450 ERROR ("bind plugin: bind_xml_read_timestamp: strptime failed.");
451 xmlXPathFreeObject (xpathObj);
455 *ret_value = mktime(&tm);
457 xmlXPathFreeObject (xpathObj);
459 } /* }}} int bind_xml_read_timestamp */
462 * bind_parse_generic_name_value
464 * Reads statistics in the form:
467 * <counter>123</counter>
470 static int bind_parse_generic_name_value (const char *xpath_expression, /* {{{ */
471 list_callback_t list_callback,
473 xmlDoc *doc, xmlXPathContext *xpathCtx,
474 time_t current_time, int ds_type)
476 xmlXPathObject *xpathObj = NULL;
480 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
481 if (xpathObj == NULL)
483 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
489 /* Iterate over all matching nodes. */
490 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
492 xmlNode *name_node = NULL;
493 xmlNode *counter = NULL;
497 parent = xpathObj->nodesetval->nodeTab[i];
498 DEBUG ("bind plugin: bind_parse_generic_name_value: parent->name = %s;",
499 (char *) parent->name);
501 /* Iterate over all child nodes. */
502 for (child = parent->xmlChildrenNode;
506 if (child->type != XML_ELEMENT_NODE)
509 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
511 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
515 if ((name_node != NULL) && (counter != NULL))
517 char *name = (char *) xmlNodeListGetString (doc,
518 name_node->xmlChildrenNode, 1);
522 if (ds_type == DS_TYPE_GAUGE)
523 status = bind_xml_read_gauge (doc, counter, &value.gauge);
525 status = bind_xml_read_derive (doc, counter, &value.derive);
529 status = (*list_callback) (name, value, current_time, user_data);
537 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
538 num_entries, (num_entries == 1) ? "entry" : "entries",
541 xmlXPathFreeObject(xpathObj);
544 } /* }}} int bind_parse_generic_name_value */
547 * bind_parse_generic_value_list
549 * Reads statistics in the form:
557 static int bind_parse_generic_value_list (const char *xpath_expression, /* {{{ */
558 list_callback_t list_callback,
560 xmlDoc *doc, xmlXPathContext *xpathCtx,
561 time_t current_time, int ds_type)
563 xmlXPathObject *xpathObj = NULL;
567 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
568 if (xpathObj == NULL)
570 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
576 /* Iterate over all matching nodes. */
577 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
581 /* Iterate over all child nodes. */
582 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
590 if (child->type != XML_ELEMENT_NODE)
593 node_name = (char *) child->name;
595 if (ds_type == DS_TYPE_GAUGE)
596 status = bind_xml_read_gauge (doc, child, &value.gauge);
598 status = bind_xml_read_derive (doc, child, &value.derive);
602 status = (*list_callback) (node_name, value, current_time, user_data);
608 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
609 num_entries, (num_entries == 1) ? "entry" : "entries",
612 xmlXPathFreeObject(xpathObj);
615 } /* }}} int bind_parse_generic_value_list */
617 static int bind_xml_stats_handle_zone (int version, xmlDoc *doc, /* {{{ */
618 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
621 xmlXPathObject *path_obj;
622 char *zone_name = NULL;
626 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
627 if (path_obj == NULL)
629 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
633 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
635 zone_name = (char *) xmlNodeListGetString (doc,
636 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
637 if (zone_name != NULL)
641 if (zone_name == NULL)
643 ERROR ("bind plugin: Could not determine zone name.");
644 xmlXPathFreeObject (path_obj);
648 for (j = 0; j < view->zones_num; j++)
650 if (strcasecmp (zone_name, view->zones[j]) == 0)
659 xmlXPathFreeObject (path_obj);
663 zone_name = view->zones[j];
665 DEBUG ("bind plugin: bind_xml_stats_handle_zone: Found zone `%s'.",
668 { /* Parse the <counters> tag {{{ */
669 char plugin_instance[DATA_MAX_NAME_LEN];
670 translation_table_ptr_t table_ptr =
672 nsstats_translation_table,
673 nsstats_translation_table_length,
677 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-zone-%s",
678 view->name, zone_name);
680 bind_parse_generic_value_list (/* xpath = */ "counters",
681 /* callback = */ bind_xml_table_callback,
682 /* user_data = */ &table_ptr,
683 doc, path_ctx, current_time, DS_TYPE_COUNTER);
686 xmlXPathFreeObject (path_obj);
689 } /* }}} int bind_xml_stats_handle_zone */
691 static int bind_xml_stats_search_zones (int version, xmlDoc *doc, /* {{{ */
692 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
695 xmlXPathObject *zone_nodes = NULL;
696 xmlXPathContext *zone_path_context;
699 zone_path_context = xmlXPathNewContext (doc);
700 if (zone_path_context == NULL)
702 ERROR ("bind plugin: xmlXPathNewContext failed.");
706 zone_nodes = xmlXPathEvalExpression (BAD_CAST "zones/zone", path_ctx);
707 if (zone_nodes == NULL)
709 ERROR ("bind plugin: Cannot find any <view> tags.");
710 xmlXPathFreeContext (zone_path_context);
714 for (i = 0; i < zone_nodes->nodesetval->nodeNr; i++)
718 node = zone_nodes->nodesetval->nodeTab[i];
719 assert (node != NULL);
721 zone_path_context->node = node;
723 bind_xml_stats_handle_zone (version, doc, zone_path_context, node, view,
727 xmlXPathFreeObject (zone_nodes);
728 xmlXPathFreeContext (zone_path_context);
730 } /* }}} int bind_xml_stats_search_zones */
732 static int bind_xml_stats_handle_view (int version, xmlDoc *doc, /* {{{ */
733 xmlXPathContext *path_ctx, xmlNode *node, time_t current_time)
735 xmlXPathObject *path_obj;
736 char *view_name = NULL;
741 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
742 if (path_obj == NULL)
744 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
748 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
750 view_name = (char *) xmlNodeListGetString (doc,
751 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
752 if (view_name != NULL)
756 if (view_name == NULL)
758 ERROR ("bind plugin: Could not determine view name.");
759 xmlXPathFreeObject (path_obj);
763 for (j = 0; j < views_num; j++)
765 if (strcasecmp (view_name, views[j].name) == 0)
770 xmlXPathFreeObject (path_obj);
780 DEBUG ("bind plugin: bind_xml_stats_handle_view: Found view `%s'.",
783 if (view->qtypes != 0) /* {{{ */
785 char plugin_instance[DATA_MAX_NAME_LEN];
786 list_info_ptr_t list_info =
789 /* type = */ "dns_qtype_gauge"
792 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-qtypes",
795 bind_parse_generic_name_value (/* xpath = */ "rdtype",
796 /* callback = */ bind_xml_list_callback,
797 /* user_data = */ &list_info,
798 doc, path_ctx, current_time, DS_TYPE_COUNTER);
801 if (view->resolver_stats != 0) /* {{{ */
803 char plugin_instance[DATA_MAX_NAME_LEN];
804 translation_table_ptr_t table_ptr =
806 resstats_translation_table,
807 resstats_translation_table_length,
811 ssnprintf (plugin_instance, sizeof (plugin_instance),
812 "%s-resolver_stats", view->name);
814 bind_parse_generic_name_value ("resstat",
815 /* callback = */ bind_xml_table_callback,
816 /* user_data = */ &table_ptr,
817 doc, path_ctx, current_time, DS_TYPE_COUNTER);
820 if (view->cacherrsets != 0) /* {{{ */
822 char plugin_instance[DATA_MAX_NAME_LEN];
823 list_info_ptr_t list_info =
826 /* type = */ "dns_qtype_gauge"
829 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-cache_rr_sets",
832 bind_parse_generic_name_value (/* xpath = */ "cache/rrset",
833 /* callback = */ bind_xml_list_callback,
834 /* user_data = */ &list_info,
835 doc, path_ctx, current_time, DS_TYPE_GAUGE);
838 if (view->zones_num > 0)
839 bind_xml_stats_search_zones (version, doc, path_ctx, node, view,
843 } /* }}} int bind_xml_stats_handle_view */
845 static int bind_xml_stats_search_views (int version, xmlDoc *doc, /* {{{ */
846 xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time)
848 xmlXPathObject *view_nodes = NULL;
849 xmlXPathContext *view_path_context;
852 view_path_context = xmlXPathNewContext (doc);
853 if (view_path_context == NULL)
855 ERROR ("bind plugin: xmlXPathNewContext failed.");
859 view_nodes = xmlXPathEvalExpression (BAD_CAST "views/view", xpathCtx);
860 if (view_nodes == NULL)
862 ERROR ("bind plugin: Cannot find any <view> tags.");
863 xmlXPathFreeContext (view_path_context);
867 for (i = 0; i < view_nodes->nodesetval->nodeNr; i++)
871 node = view_nodes->nodesetval->nodeTab[i];
872 assert (node != NULL);
874 view_path_context->node = node;
876 bind_xml_stats_handle_view (version, doc, view_path_context, node,
880 xmlXPathFreeObject (view_nodes);
881 xmlXPathFreeContext (view_path_context);
883 } /* }}} int bind_xml_stats_search_views */
885 static int bind_xml_stats (int version, xmlDoc *doc, /* {{{ */
886 xmlXPathContext *xpathCtx, xmlNode *statsnode)
888 time_t current_time = 0;
891 xpathCtx->node = statsnode;
893 /* TODO: Check `server/boot-time' to recognize server restarts. */
895 status = bind_xml_read_timestamp ("server/current-time",
896 doc, xpathCtx, ¤t_time);
899 ERROR ("bind plugin: Reading `server/current-time' failed.");
902 DEBUG ("bind plugin: Current server time is %i.", (int) current_time);
904 /* XPath: server/requests/opcode
905 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
909 * <counter>1</counter>
913 if (global_opcodes != 0)
915 list_info_ptr_t list_info =
917 /* plugin instance = */ "global-opcodes",
918 /* type = */ "dns_opcode"
921 bind_parse_generic_name_value (/* xpath = */ "server/requests/opcode",
922 /* callback = */ bind_xml_list_callback,
923 /* user_data = */ &list_info,
924 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
927 /* XPath: server/queries-in/rdtype
928 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
929 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
930 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
934 * <counter>1</counter>
938 if (global_qtypes != 0)
940 list_info_ptr_t list_info =
942 /* plugin instance = */ "global-qtypes",
943 /* type = */ "dns_qtype"
946 bind_parse_generic_name_value (/* xpath = */ "server/queries-in/rdtype",
947 /* callback = */ bind_xml_list_callback,
948 /* user_data = */ &list_info,
949 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
952 /* XPath: server/nsstats, server/nsstat
953 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
954 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
955 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
956 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
957 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
958 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
959 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
963 * <Requestv4>1</Requestv4>
964 * <Requestv6>0</Requestv6>
969 * <name>Requestv4</name>
970 * <counter>1</counter>
973 * <name>Requestv6</name>
974 * <counter>0</counter>
978 if (global_server_stats)
980 translation_table_ptr_t table_ptr =
982 nsstats_translation_table,
983 nsstats_translation_table_length,
984 /* plugin_instance = */ "global-server_stats"
989 bind_parse_generic_value_list ("server/nsstats",
990 /* callback = */ bind_xml_table_callback,
991 /* user_data = */ &table_ptr,
992 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
996 bind_parse_generic_name_value ("server/nsstat",
997 /* callback = */ bind_xml_table_callback,
998 /* user_data = */ &table_ptr,
999 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1003 /* XPath: server/zonestats, server/zonestat
1004 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1005 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1006 * XfrSuccess, XfrFail
1009 * <NotifyOutv4>0</NotifyOutv4>
1010 * <NotifyOutv6>0</NotifyOutv6>
1015 * <name>NotifyOutv4</name>
1016 * <counter>0</counter>
1019 * <name>NotifyOutv6</name>
1020 * <counter>0</counter>
1024 if (global_zone_maint_stats)
1026 translation_table_ptr_t table_ptr =
1028 zonestats_translation_table,
1029 zonestats_translation_table_length,
1030 /* plugin_instance = */ "global-zone_maint_stats"
1035 bind_parse_generic_value_list ("server/zonestats",
1036 /* callback = */ bind_xml_table_callback,
1037 /* user_data = */ &table_ptr,
1038 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1042 bind_parse_generic_name_value ("server/zonestat",
1043 /* callback = */ bind_xml_table_callback,
1044 /* user_data = */ &table_ptr,
1045 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1049 /* XPath: server/resstats
1050 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1051 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1052 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1053 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1056 * <Queryv4>0</Queryv4>
1057 * <Queryv6>0</Queryv6>
1062 * <name>Queryv4</name>
1063 * <counter>0</counter>
1066 * <name>Queryv6</name>
1067 * <counter>0</counter>
1071 if (global_resolver_stats != 0)
1073 translation_table_ptr_t table_ptr =
1075 resstats_translation_table,
1076 resstats_translation_table_length,
1077 /* plugin_instance = */ "global-resolver_stats"
1082 bind_parse_generic_value_list ("server/resstats",
1083 /* callback = */ bind_xml_table_callback,
1084 /* user_data = */ &table_ptr,
1085 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1089 bind_parse_generic_name_value ("server/resstat",
1090 /* callback = */ bind_xml_table_callback,
1091 /* user_data = */ &table_ptr,
1092 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1096 /* XPath: memory/summary
1097 * Variables: TotalUse, InUse, BlockSize, ContextSize, Lost
1100 * <TotalUse>6587096</TotalUse>
1101 * <InUse>1345424</InUse>
1102 * <BlockSize>5505024</BlockSize>
1103 * <ContextSize>3732456</ContextSize>
1107 if (global_memory_stats != 0)
1109 translation_table_ptr_t table_ptr =
1111 memsummary_translation_table,
1112 memsummary_translation_table_length,
1113 /* plugin_instance = */ "global-memory_stats"
1116 bind_parse_generic_value_list ("memory/summary",
1117 /* callback = */ bind_xml_table_callback,
1118 /* user_data = */ &table_ptr,
1119 doc, xpathCtx, current_time, DS_TYPE_GAUGE);
1123 bind_xml_stats_search_views (version, doc, xpathCtx, statsnode,
1127 } /* }}} int bind_xml_stats */
1129 static int bind_xml (const char *data) /* {{{ */
1132 xmlXPathContext *xpathCtx = NULL;
1133 xmlXPathObject *xpathObj = NULL;
1137 doc = xmlParseMemory (data, strlen (data));
1140 ERROR ("bind plugin: xmlParseMemory failed.");
1144 xpathCtx = xmlXPathNewContext (doc);
1145 if (xpathCtx == NULL)
1147 ERROR ("bind plugin: xmlXPathNewContext failed.");
1152 xpathObj = xmlXPathEvalExpression (BAD_CAST "/isc/bind/statistics", xpathCtx);
1153 if (xpathObj == NULL)
1155 ERROR ("bind plugin: Cannot find the <statistics> tag.");
1156 xmlXPathFreeContext (xpathCtx);
1160 else if (xpathObj->nodesetval == NULL)
1162 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
1163 xmlXPathFreeObject (xpathObj);
1164 xmlXPathFreeContext (xpathCtx);
1169 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
1173 int parsed_version = 0;
1175 node = xpathObj->nodesetval->nodeTab[i];
1176 assert (node != NULL);
1178 attr_version = (char *) xmlGetProp (node, BAD_CAST "version");
1179 if (attr_version == NULL)
1181 NOTICE ("bind plugin: Found <statistics> tag doesn't have a "
1182 "`version' attribute.");
1185 DEBUG ("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1187 /* At the time this plugin was written, version "1.0" was used by
1188 * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
1189 * that "1.*" and "2.*" don't introduce structural changes, so we just
1190 * check for the first two characters here. */
1191 if (strncmp ("1.", attr_version, strlen ("1.")) == 0)
1193 else if (strncmp ("2.", attr_version, strlen ("2.")) == 0)
1197 /* TODO: Use the complaint mechanism here. */
1198 NOTICE ("bind plugin: Found <statistics> tag with version `%s'. "
1199 "Unfortunately I have no clue how to parse that. "
1200 "Please open a bug report for this.", attr_version);
1201 xmlFree (attr_version);
1205 ret = bind_xml_stats (parsed_version,
1206 doc, xpathCtx, node);
1208 xmlFree (attr_version);
1209 /* One <statistics> node ought to be enough. */
1213 xmlXPathFreeObject (xpathObj);
1214 xmlXPathFreeContext (xpathCtx);
1218 } /* }}} int bind_xml */
1220 static int bind_config_set_bool (const char *name, int *var, /* {{{ */
1223 if ((ci->values_num != 1) || ( ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1225 WARNING ("bind plugin: The `%s' option needs "
1226 "exactly one boolean argument.", name);
1230 if (ci->values[0].value.boolean)
1235 } /* }}} int bind_config_set_bool */
1237 static int bind_config_add_view_zone (cb_view_t *view, /* {{{ */
1242 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1244 WARNING ("bind plugin: The `Zone' option needs "
1245 "exactly one string argument.");
1249 tmp = (char **) realloc (view->zones,
1250 sizeof (char *) * (view->zones_num + 1));
1253 ERROR ("bind plugin: realloc failed.");
1258 view->zones[view->zones_num] = strdup (ci->values[0].value.string);
1259 if (view->zones[view->zones_num] == NULL)
1261 ERROR ("bind plugin: strdup failed.");
1267 } /* }}} int bind_config_add_view_zone */
1269 static int bind_config_add_view (oconfig_item_t *ci) /* {{{ */
1274 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1276 WARNING ("bind plugin: `View' blocks need exactly one string argument.");
1280 tmp = (cb_view_t *) realloc (views, sizeof (*views) * (views_num + 1));
1283 ERROR ("bind plugin: realloc failed.");
1287 tmp = views + views_num;
1289 memset (tmp, 0, sizeof (*tmp));
1291 tmp->resolver_stats = 1;
1292 tmp->cacherrsets = 1;
1296 tmp->name = strdup (ci->values[0].value.string);
1297 if (tmp->name == NULL)
1299 ERROR ("bind plugin: strdup failed.");
1304 for (i = 0; i < ci->children_num; i++)
1306 oconfig_item_t *child = ci->children + i;
1308 if (strcasecmp ("QTypes", child->key) == 0)
1309 bind_config_set_bool ("QTypes", &tmp->qtypes, child);
1310 else if (strcasecmp ("ResolverStats", child->key) == 0)
1311 bind_config_set_bool ("ResolverStats", &tmp->resolver_stats, child);
1312 else if (strcasecmp ("CacheRRSets", child->key) == 0)
1313 bind_config_set_bool ("CacheRRSets", &tmp->cacherrsets, child);
1314 else if (strcasecmp ("Zone", child->key) == 0)
1315 bind_config_add_view_zone (tmp, child);
1318 WARNING ("bind plugin: Unknown configuration option "
1319 "`%s' in view `%s' will be ignored.", child->key, tmp->name);
1321 } /* for (i = 0; i < ci->children_num; i++) */
1325 } /* }}} int bind_config_add_view */
1327 static int bind_config (oconfig_item_t *ci) /* {{{ */
1331 for (i = 0; i < ci->children_num; i++)
1333 oconfig_item_t *child = ci->children + i;
1335 if (strcasecmp ("Url", child->key) == 0) {
1336 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING))
1338 WARNING ("bind plugin: The `Url' option needs "
1339 "exactly one string argument.");
1343 url = strdup (child->values[0].value.string);
1344 } else if (strcasecmp ("OpCodes", child->key) == 0)
1345 bind_config_set_bool ("OpCodes", &global_opcodes, child);
1346 else if (strcasecmp ("QTypes", child->key) == 0)
1347 bind_config_set_bool ("QTypes", &global_qtypes, child);
1348 else if (strcasecmp ("ServerStats", child->key) == 0)
1349 bind_config_set_bool ("ServerStats", &global_server_stats, child);
1350 else if (strcasecmp ("ZoneMaintStats", child->key) == 0)
1351 bind_config_set_bool ("ZoneMaintStats", &global_zone_maint_stats, child);
1352 else if (strcasecmp ("ResolverStats", child->key) == 0)
1353 bind_config_set_bool ("ResolverStats", &global_resolver_stats, child);
1354 else if (strcasecmp ("MemoryStats", child->key) == 0)
1355 bind_config_set_bool ("MemoryStats", &global_memory_stats, child);
1356 else if (strcasecmp ("View", child->key) == 0)
1357 bind_config_add_view (child);
1360 WARNING ("bind plugin: Unknown configuration option "
1361 "`%s' will be ignored.", child->key);
1366 } /* }}} int bind_config */
1368 static int bind_init (void) /* {{{ */
1373 curl = curl_easy_init ();
1376 ERROR ("bind plugin: bind_init: curl_easy_init failed.");
1380 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
1381 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
1382 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, bind_curl_error);
1383 curl_easy_setopt (curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL);
1384 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
1387 } /* }}} int bind_init */
1389 static int bind_read (void) /* {{{ */
1395 ERROR ("bind plugin: I don't have a CURL object.");
1399 bind_buffer_fill = 0;
1400 if (curl_easy_perform (curl) != 0)
1402 ERROR ("bind plugin: curl_easy_perform failed: %s",
1407 status = bind_xml (bind_buffer);
1412 } /* }}} int bind_read */
1414 static int bind_shutdown (void) /* {{{ */
1418 curl_easy_cleanup (curl);
1423 } /* }}} int bind_shutdown */
1425 void module_register (void)
1427 plugin_register_complex_config ("bind", bind_config);
1428 plugin_register_init ("bind", bind_init);
1429 plugin_register_read ("bind", bind_read);
1430 plugin_register_shutdown ("bind", bind_shutdown);
1431 } /* void module_register */
1433 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */