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>
28 #include "configfile.h"
30 #include <curl/curl.h>
31 #include <libxml/parser.h>
32 #include <libxml/xpath.h>
34 #ifndef BIND_DEFAULT_URL
35 # define BIND_DEFAULT_URL "http://localhost:8053/"
39 * Some types used for the callback functions. `translation_table_ptr_t' and
40 * `list_info_ptr_t' are passed to the callbacks in the `void *user_data'
43 typedef int (*list_callback_t) (const char *name, value_t value,
44 time_t current_time, void *user_data);
57 typedef struct cb_view_s cb_view_t;
59 struct translation_info_s
63 const char *type_instance;
65 typedef struct translation_info_s translation_info_t;
67 struct translation_table_ptr_s
69 const translation_info_t *table;
71 const char *plugin_instance;
73 typedef struct translation_table_ptr_s translation_table_ptr_t;
75 struct list_info_ptr_s
77 const char *plugin_instance;
80 typedef struct list_info_ptr_s list_info_ptr_t;
82 static char *url = NULL;
83 static int global_opcodes = 1;
84 static int global_qtypes = 1;
85 static int global_server_stats = 1;
86 static int global_zone_maint_stats = 1;
87 static int global_resolver_stats = 0;
88 static int global_memory_stats = 1;
90 static cb_view_t *views = NULL;
91 static size_t views_num = 0;
93 static CURL *curl = NULL;
95 static char *bind_buffer = NULL;
96 static size_t bind_buffer_size = 0;
97 static size_t bind_buffer_fill = 0;
98 static char bind_curl_error[CURL_ERROR_SIZE];
100 /* Translation table for the `nsstats' values. */
101 static const translation_info_t nsstats_translation_table[] = /* {{{ */
104 { "Requestv4", "dns_request", "IPv4" },
105 { "Requestv6", "dns_request", "IPv6" },
106 { "ReqEdns0", "dns_request", "EDNS0" },
107 { "ReqBadEDNSVer", "dns_request", "BadEDNSVer" },
108 { "ReqTSIG", "dns_request", "TSIG" },
109 { "ReqSIG0", "dns_request", "SIG0" },
110 { "ReqBadSIG", "dns_request", "BadSIG" },
111 { "ReqTCP", "dns_request", "TCP" },
113 { "AuthQryRej", "dns_reject", "authorative" },
114 { "RecQryRej", "dns_reject", "recursive" },
115 { "XfrRej", "dns_reject", "transfer" },
116 { "UpdateRej", "dns_reject", "update" },
118 { "Response", "dns_response", "normal" },
119 { "TruncatedResp", "dns_response", "truncated" },
120 { "RespEDNS0", "dns_response", "EDNS0" },
121 { "RespTSIG", "dns_response", "TSIG" },
122 { "RespSIG0", "dns_response", "SIG0" },
124 { "QryAuthAns", "dns_query", "authorative" },
125 { "QryNoauthAns", "dns_query", "nonauth" },
126 { "QryReferral", "dns_query", "referral" },
127 { "QryRecursion", "dns_query", "recursion" },
128 { "QryDuplicate", "dns_query", "dupliate" },
129 { "QryDropped", "dns_query", "dropped" },
130 { "QryFailure", "dns_query", "failure" },
132 { "QrySuccess", "dns_rcode", "tx-NOERROR" },
133 { "QryNxrrset", "dns_rcode", "tx-NXRRSET" },
134 { "QrySERVFAIL", "dns_rcode", "tx-SERVFAIL" },
135 { "QryFORMERR", "dns_rcode", "tx-FORMERR" },
136 { "QryNXDOMAIN", "dns_rcode", "tx-NXDOMAIN" }
138 { "XfrReqDone", "type", "type_instance" },
139 { "UpdateReqFwd", "type", "type_instance" },
140 { "UpdateRespFwd", "type", "type_instance" },
141 { "UpdateFwdFail", "type", "type_instance" },
142 { "UpdateDone", "type", "type_instance" },
143 { "UpdateFail", "type", "type_instance" },
144 { "UpdateBadPrereq", "type", "type_instance" },
147 static int nsstats_translation_table_length =
148 STATIC_ARRAY_SIZE (nsstats_translation_table);
151 /* Translation table for the `zonestats' values. */
152 static const translation_info_t zonestats_translation_table[] = /* {{{ */
155 { "NotifyOutv4", "dns_notify", "tx-IPv4" },
156 { "NotifyOutv6", "dns_notify", "tx-IPv6" },
157 { "NotifyInv4", "dns_notify", "rx-IPv4" },
158 { "NotifyInv6", "dns_notify", "rx-IPv6" },
159 { "NotifyRej", "dns_notify", "rejected" },
160 /* SOA/AXFS/IXFS requests */
161 { "SOAOutv4", "dns_opcode", "SOA-IPv4" },
162 { "SOAOutv6", "dns_opcode", "SOA-IPv6" },
163 { "AXFRReqv4", "dns_opcode", "AXFR-IPv4" },
164 { "AXFRReqv6", "dns_opcode", "AXFR-IPv6" },
165 { "IXFRReqv4", "dns_opcode", "IXFR-IPv4" },
166 { "IXFRReqv6", "dns_opcode", "IXFR-IPv6" },
167 /* Domain transfers */
168 { "XfrSuccess", "dns_transfer", "success" },
169 { "XfrFail", "dns_transfer", "failure" }
171 static int zonestats_translation_table_length =
172 STATIC_ARRAY_SIZE (zonestats_translation_table);
175 /* Translation table for the `resstats' values. */
176 static const translation_info_t resstats_translation_table[] = /* {{{ */
178 /* Generic resolver information */
179 { "Queryv4", "dns_query", "IPv4" },
180 { "Queryv6", "dns_query", "IPv6" },
181 { "Responsev4", "dns_response", "IPv4" },
182 { "Responsev6", "dns_response", "IPv6" },
183 /* Received response codes */
184 { "NXDOMAIN", "dns_rcode", "rx-NXDOMAIN" },
185 { "SERVFAIL", "dns_rcode", "rx-SERVFAIL" },
186 { "FORMERR", "dns_rcode", "rx-FORMERR" },
187 { "OtherError", "dns_rcode", "rx-OTHER" },
188 { "EDNS0Fail", "dns_rcode", "rx-EDNS0Fail"},
189 /* Received responses */
190 { "Mismatch", "dns_response", "mismatch" },
191 { "Truncated", "dns_response", "truncated" },
192 { "Lame", "dns_response", "lame" },
193 { "Retry", "dns_query", "retry" },
195 { "GlueFetchv4", "type", "type_instance" },
196 { "GlueFetchv6", "type", "type_instance" },
197 { "GlueFetchv4Fail", "type", "type_instance" },
198 { "GlueFetchv6Fail", "type", "type_instance" },
200 /* DNSSEC information */
201 { "ValAttempt", "dns_resolver", "DNSSEC-attempt" },
202 { "ValOk", "dns_resolver", "DNSSEC-okay" },
203 { "ValNegOk", "dns_resolver", "DNSSEC-negokay" },
204 { "ValFail", "dns_resolver", "DNSSEC-fail" }
206 static int resstats_translation_table_length =
207 STATIC_ARRAY_SIZE (resstats_translation_table);
210 /* Translation table for the `memory/summary' values. */
211 static const translation_info_t memsummary_translation_table[] = /* {{{ */
213 { "TotalUse", "memory", "TotalUse" },
214 { "InUse", "memory", "InUse" },
215 { "BlockSize", "memory", "BlockSize" },
216 { "ContextSize", "memory", "ContextSize" },
217 { "Lost", "memory", "Lost" }
219 static int memsummary_translation_table_length =
220 STATIC_ARRAY_SIZE (memsummary_translation_table);
223 static void remove_special (char *buffer, size_t buffer_size) /* {{{ */
227 for (i = 0; i < buffer_size; i++)
231 if ((!isalnum ((int) buffer[i])) && (buffer[i] != '-'))
234 } /* }}} void remove_special */
236 static void submit (time_t ts, const char *plugin_instance, /* {{{ */
237 const char *type, const char *type_instance, value_t value)
240 value_list_t vl = VALUE_LIST_INIT;
247 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
248 sstrncpy(vl.plugin, "bind", sizeof(vl.plugin));
249 if (plugin_instance) {
250 sstrncpy(vl.plugin_instance, plugin_instance,
251 sizeof(vl.plugin_instance));
252 remove_special (vl.plugin_instance, sizeof (vl.plugin_instance));
254 sstrncpy(vl.type, type, sizeof(vl.type));
256 sstrncpy(vl.type_instance, type_instance,
257 sizeof(vl.type_instance));
258 remove_special (vl.plugin_instance, sizeof (vl.plugin_instance));
260 plugin_dispatch_values(&vl);
261 } /* }}} void submit */
263 static size_t bind_curl_callback (void *buf, size_t size, /* {{{ */
264 size_t nmemb, void __attribute__((unused)) *stream)
266 size_t len = size * nmemb;
271 if ((bind_buffer_fill + len) >= bind_buffer_size)
275 temp = realloc(bind_buffer, bind_buffer_fill + len + 1);
278 ERROR ("bind plugin: realloc failed.");
282 bind_buffer_size = bind_buffer_fill + len + 1;
285 memcpy (bind_buffer + bind_buffer_fill, (char *) buf, len);
286 bind_buffer_fill += len;
287 bind_buffer[bind_buffer_fill] = 0;
290 } /* }}} size_t bind_curl_callback */
293 * Callback, that's called with a translation table.
294 * (Plugin instance is fixed, type and type instance come from lookup table.)
296 static int bind_xml_table_callback (const char *name, value_t value, /* {{{ */
297 time_t current_time, void *user_data)
299 translation_table_ptr_t *table = (translation_table_ptr_t *) user_data;
305 for (i = 0; i < table->table_length; i++)
307 if (strcmp (table->table[i].xml_name, name) != 0)
310 submit (current_time,
311 table->plugin_instance,
312 table->table[i].type,
313 table->table[i].type_instance,
319 } /* }}} int bind_xml_table_callback */
322 * Callback, that's used for lists.
323 * (Plugin instance and type are fixed, xml name is used as type instance.)
325 static int bind_xml_list_callback (const char *name, /* {{{ */
326 value_t value, time_t current_time, void *user_data)
328 list_info_ptr_t *list_info = (list_info_ptr_t *) user_data;
330 if (list_info == NULL)
333 submit (current_time,
334 list_info->plugin_instance,
336 /* type instance = */ name,
340 } /* }}} int bind_xml_list_callback */
342 static int bind_xml_read_counter (xmlDoc *doc, xmlNode *node, /* {{{ */
343 counter_t *ret_value)
345 char *str_ptr, *end_ptr;
348 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
351 ERROR ("bind plugin: bind_xml_read_counter: xmlNodeListGetString failed.");
356 value = strtoll (str_ptr, &end_ptr, 10);
358 if (str_ptr == end_ptr || errno)
360 if (errno && (value < 0))
361 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed with underflow.");
362 else if (errno && (value > 0))
363 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed with overflow.");
365 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed.");
371 } /* }}} int bind_xml_read_counter */
373 static int bind_xml_read_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
376 char *str_ptr, *end_ptr;
379 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
382 ERROR ("bind plugin: bind_xml_read_gauge: xmlNodeListGetString failed.");
387 value = strtod (str_ptr, &end_ptr);
389 if (str_ptr == end_ptr || errno)
391 if (errno && (value < 0))
392 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with underflow.");
393 else if (errno && (value > 0))
394 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with overflow.");
396 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed.");
400 *ret_value = (gauge_t) value;
402 } /* }}} int bind_xml_read_gauge */
404 static int bind_xml_read_timestamp (const char *xpath_expression, /* {{{ */
405 xmlDoc *doc, xmlXPathContext *xpathCtx, time_t *ret_value)
407 xmlXPathObject *xpathObj = NULL;
413 xpathObj = xmlXPathEvalExpression (BAD_CAST xpath_expression, xpathCtx);
414 if (xpathObj == NULL)
416 ERROR ("bind plugin: Unable to evaluate XPath expression `%s'.",
421 if ((xpathObj->nodesetval == NULL) || (xpathObj->nodesetval->nodeNr < 1))
423 xmlXPathFreeObject (xpathObj);
427 if (xpathObj->nodesetval->nodeNr != 1)
429 NOTICE ("bind plugin: Evaluating the XPath expression `%s' returned "
430 "%i nodes. Only handling the first one.",
431 xpath_expression, xpathObj->nodesetval->nodeNr);
434 node = xpathObj->nodesetval->nodeTab[0];
436 if (node->xmlChildrenNode == NULL)
438 ERROR ("bind plugin: bind_xml_read_timestamp: "
439 "node->xmlChildrenNode == NULL");
440 xmlXPathFreeObject (xpathObj);
444 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
447 ERROR ("bind plugin: bind_xml_read_timestamp: xmlNodeListGetString failed.");
448 xmlXPathFreeObject (xpathObj);
452 memset (&tm, 0, sizeof(tm));
453 tmp = strptime (str_ptr, "%Y-%m-%dT%T", &tm);
457 ERROR ("bind plugin: bind_xml_read_timestamp: strptime failed.");
458 xmlXPathFreeObject (xpathObj);
462 *ret_value = timegm(&tm);
464 xmlXPathFreeObject (xpathObj);
466 } /* }}} int bind_xml_read_timestamp */
469 * bind_parse_generic_name_value
471 * Reads statistics in the form:
474 * <counter>123</counter>
477 static int bind_parse_generic_name_value (const char *xpath_expression, /* {{{ */
478 list_callback_t list_callback,
480 xmlDoc *doc, xmlXPathContext *xpathCtx,
481 time_t current_time, int ds_type)
483 xmlXPathObject *xpathObj = NULL;
487 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
488 if (xpathObj == NULL)
490 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
496 /* Iterate over all matching nodes. */
497 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
499 xmlNode *name_node = NULL;
500 xmlNode *counter = NULL;
504 parent = xpathObj->nodesetval->nodeTab[i];
505 DEBUG ("bind plugin: bind_parse_generic_name_value: parent->name = %s;",
506 (char *) parent->name);
508 /* Iterate over all child nodes. */
509 for (child = parent->xmlChildrenNode;
513 if (child->type != XML_ELEMENT_NODE)
516 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
518 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
522 if ((name_node != NULL) && (counter != NULL))
524 char *name = (char *) xmlNodeListGetString (doc,
525 name_node->xmlChildrenNode, 1);
529 if (ds_type == DS_TYPE_GAUGE)
530 status = bind_xml_read_gauge (doc, counter, &value.gauge);
532 status = bind_xml_read_counter (doc, counter, &value.counter);
536 status = (*list_callback) (name, value, current_time, user_data);
544 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
545 num_entries, (num_entries == 1) ? "entry" : "entries",
548 xmlXPathFreeObject(xpathObj);
551 } /* }}} int bind_parse_generic_name_value */
554 * bind_parse_generic_value_list
556 * Reads statistics in the form:
564 static int bind_parse_generic_value_list (const char *xpath_expression, /* {{{ */
565 list_callback_t list_callback,
567 xmlDoc *doc, xmlXPathContext *xpathCtx,
568 time_t current_time, int ds_type)
570 xmlXPathObject *xpathObj = NULL;
574 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
575 if (xpathObj == NULL)
577 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
583 /* Iterate over all matching nodes. */
584 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
588 /* Iterate over all child nodes. */
589 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
597 if (child->type != XML_ELEMENT_NODE)
600 node_name = (char *) child->name;
602 if (ds_type == DS_TYPE_GAUGE)
603 status = bind_xml_read_gauge (doc, child, &value.gauge);
605 status = bind_xml_read_counter (doc, child, &value.counter);
609 status = (*list_callback) (node_name, value, current_time, user_data);
615 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
616 num_entries, (num_entries == 1) ? "entry" : "entries",
619 xmlXPathFreeObject(xpathObj);
622 } /* }}} int bind_parse_generic_value_list */
624 static int bind_xml_stats_handle_zone (int version, xmlDoc *doc, /* {{{ */
625 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
628 xmlXPathObject *path_obj;
629 char *zone_name = NULL;
633 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
634 if (path_obj == NULL)
636 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
640 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
642 zone_name = (char *) xmlNodeListGetString (doc,
643 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
644 if (zone_name != NULL)
648 if (zone_name == NULL)
650 ERROR ("bind plugin: Could not determine zone name.");
651 xmlXPathFreeObject (path_obj);
655 for (j = 0; j < view->zones_num; j++)
657 if (strcasecmp (zone_name, view->zones[j]) == 0)
666 xmlXPathFreeObject (path_obj);
670 zone_name = view->zones[j];
672 DEBUG ("bind plugin: bind_xml_stats_handle_zone: Found zone `%s'.",
675 { /* Parse the <counters> tag {{{ */
676 char plugin_instance[DATA_MAX_NAME_LEN];
677 translation_table_ptr_t table_ptr =
679 nsstats_translation_table,
680 nsstats_translation_table_length,
684 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-zone-%s",
685 view->name, zone_name);
687 bind_parse_generic_value_list (/* xpath = */ "counters",
688 /* callback = */ bind_xml_table_callback,
689 /* user_data = */ &table_ptr,
690 doc, path_ctx, current_time, DS_TYPE_COUNTER);
693 xmlXPathFreeObject (path_obj);
696 } /* }}} int bind_xml_stats_handle_zone */
698 static int bind_xml_stats_search_zones (int version, xmlDoc *doc, /* {{{ */
699 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
702 xmlXPathObject *zone_nodes = NULL;
703 xmlXPathContext *zone_path_context;
706 zone_path_context = xmlXPathNewContext (doc);
707 if (zone_path_context == NULL)
709 ERROR ("bind plugin: xmlXPathNewContext failed.");
713 zone_nodes = xmlXPathEvalExpression (BAD_CAST "zones/zone", path_ctx);
714 if (zone_nodes == NULL)
716 ERROR ("bind plugin: Cannot find any <view> tags.");
717 xmlXPathFreeContext (zone_path_context);
721 for (i = 0; i < zone_nodes->nodesetval->nodeNr; i++)
725 node = zone_nodes->nodesetval->nodeTab[i];
726 assert (node != NULL);
728 zone_path_context->node = node;
730 bind_xml_stats_handle_zone (version, doc, zone_path_context, node, view,
734 xmlXPathFreeObject (zone_nodes);
735 xmlXPathFreeContext (zone_path_context);
737 } /* }}} int bind_xml_stats_search_zones */
739 static int bind_xml_stats_handle_view (int version, xmlDoc *doc, /* {{{ */
740 xmlXPathContext *path_ctx, xmlNode *node, time_t current_time)
742 xmlXPathObject *path_obj;
743 char *view_name = NULL;
748 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
749 if (path_obj == NULL)
751 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
755 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
757 view_name = (char *) xmlNodeListGetString (doc,
758 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
759 if (view_name != NULL)
763 if (view_name == NULL)
765 ERROR ("bind plugin: Could not determine view name.");
766 xmlXPathFreeObject (path_obj);
770 for (j = 0; j < views_num; j++)
772 if (strcasecmp (view_name, views[j].name) == 0)
777 xmlXPathFreeObject (path_obj);
787 DEBUG ("bind plugin: bind_xml_stats_handle_view: Found view `%s'.",
790 if (view->qtypes != 0) /* {{{ */
792 char plugin_instance[DATA_MAX_NAME_LEN];
793 list_info_ptr_t list_info =
796 /* type = */ "dns_qtype_gauge"
799 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-qtypes",
802 bind_parse_generic_name_value (/* xpath = */ "rdtype",
803 /* callback = */ bind_xml_list_callback,
804 /* user_data = */ &list_info,
805 doc, path_ctx, current_time, DS_TYPE_COUNTER);
808 if (view->resolver_stats != 0) /* {{{ */
810 char plugin_instance[DATA_MAX_NAME_LEN];
811 translation_table_ptr_t table_ptr =
813 resstats_translation_table,
814 resstats_translation_table_length,
818 ssnprintf (plugin_instance, sizeof (plugin_instance),
819 "%s-resolver_stats", view->name);
821 bind_parse_generic_name_value ("resstat",
822 /* callback = */ bind_xml_table_callback,
823 /* user_data = */ &table_ptr,
824 doc, path_ctx, current_time, DS_TYPE_COUNTER);
827 if (view->cacherrsets != 0) /* {{{ */
829 char plugin_instance[DATA_MAX_NAME_LEN];
830 list_info_ptr_t list_info =
833 /* type = */ "dns_qtype_gauge"
836 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-cache_rr_sets",
839 bind_parse_generic_name_value (/* xpath = */ "cache/rrset",
840 /* callback = */ bind_xml_list_callback,
841 /* user_data = */ &list_info,
842 doc, path_ctx, current_time, DS_TYPE_GAUGE);
845 if (view->zones_num > 0)
846 bind_xml_stats_search_zones (version, doc, path_ctx, node, view,
850 } /* }}} int bind_xml_stats_handle_view */
852 static int bind_xml_stats_search_views (int version, xmlDoc *doc, /* {{{ */
853 xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time)
855 xmlXPathObject *view_nodes = NULL;
856 xmlXPathContext *view_path_context;
859 view_path_context = xmlXPathNewContext (doc);
860 if (view_path_context == NULL)
862 ERROR ("bind plugin: xmlXPathNewContext failed.");
866 view_nodes = xmlXPathEvalExpression (BAD_CAST "views/view", xpathCtx);
867 if (view_nodes == NULL)
869 ERROR ("bind plugin: Cannot find any <view> tags.");
870 xmlXPathFreeContext (view_path_context);
874 for (i = 0; i < view_nodes->nodesetval->nodeNr; i++)
878 node = view_nodes->nodesetval->nodeTab[i];
879 assert (node != NULL);
881 view_path_context->node = node;
883 bind_xml_stats_handle_view (version, doc, view_path_context, node,
887 xmlXPathFreeObject (view_nodes);
888 xmlXPathFreeContext (view_path_context);
890 } /* }}} int bind_xml_stats_search_views */
892 static int bind_xml_stats (int version, xmlDoc *doc, /* {{{ */
893 xmlXPathContext *xpathCtx, xmlNode *statsnode)
895 time_t current_time = 0;
898 xpathCtx->node = statsnode;
900 /* TODO: Check `server/boot-time' to recognize server restarts. */
902 status = bind_xml_read_timestamp ("server/current-time",
903 doc, xpathCtx, ¤t_time);
906 ERROR ("bind plugin: Reading `server/current-time' failed.");
909 DEBUG ("bind plugin: Current server time is %i.", (int) current_time);
911 /* XPath: server/requests/opcode
912 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
916 * <counter>1</counter>
920 if (global_opcodes != 0)
922 list_info_ptr_t list_info =
924 /* plugin instance = */ "global-opcodes",
925 /* type = */ "dns_opcode"
928 bind_parse_generic_name_value (/* xpath = */ "server/requests/opcode",
929 /* callback = */ bind_xml_list_callback,
930 /* user_data = */ &list_info,
931 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
934 /* XPath: server/queries-in/rdtype
935 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
936 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
937 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
941 * <counter>1</counter>
945 if (global_qtypes != 0)
947 list_info_ptr_t list_info =
949 /* plugin instance = */ "global-qtypes",
950 /* type = */ "dns_qtype"
953 bind_parse_generic_name_value (/* xpath = */ "server/queries-in/rdtype",
954 /* callback = */ bind_xml_list_callback,
955 /* user_data = */ &list_info,
956 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
959 /* XPath: server/nsstats, server/nsstat
960 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
961 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
962 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
963 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
964 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
965 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
966 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
970 * <Requestv4>1</Requestv4>
971 * <Requestv6>0</Requestv6>
976 * <name>Requestv4</name>
977 * <counter>1</counter>
980 * <name>Requestv6</name>
981 * <counter>0</counter>
985 if (global_server_stats)
987 translation_table_ptr_t table_ptr =
989 nsstats_translation_table,
990 nsstats_translation_table_length,
991 /* plugin_instance = */ "global-server_stats"
996 bind_parse_generic_value_list ("server/nsstats",
997 /* callback = */ bind_xml_table_callback,
998 /* user_data = */ &table_ptr,
999 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1003 bind_parse_generic_name_value ("server/nsstat",
1004 /* callback = */ bind_xml_table_callback,
1005 /* user_data = */ &table_ptr,
1006 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1010 /* XPath: server/zonestats, server/zonestat
1011 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1012 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1013 * XfrSuccess, XfrFail
1016 * <NotifyOutv4>0</NotifyOutv4>
1017 * <NotifyOutv6>0</NotifyOutv6>
1022 * <name>NotifyOutv4</name>
1023 * <counter>0</counter>
1026 * <name>NotifyOutv6</name>
1027 * <counter>0</counter>
1031 if (global_zone_maint_stats)
1033 translation_table_ptr_t table_ptr =
1035 zonestats_translation_table,
1036 zonestats_translation_table_length,
1037 /* plugin_instance = */ "global-zone_maint_stats"
1042 bind_parse_generic_value_list ("server/zonestats",
1043 /* callback = */ bind_xml_table_callback,
1044 /* user_data = */ &table_ptr,
1045 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1049 bind_parse_generic_name_value ("server/zonestat",
1050 /* callback = */ bind_xml_table_callback,
1051 /* user_data = */ &table_ptr,
1052 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1056 /* XPath: server/resstats
1057 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1058 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1059 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1060 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1063 * <Queryv4>0</Queryv4>
1064 * <Queryv6>0</Queryv6>
1069 * <name>Queryv4</name>
1070 * <counter>0</counter>
1073 * <name>Queryv6</name>
1074 * <counter>0</counter>
1078 if (global_resolver_stats != 0)
1080 translation_table_ptr_t table_ptr =
1082 resstats_translation_table,
1083 resstats_translation_table_length,
1084 /* plugin_instance = */ "global-resolver_stats"
1089 bind_parse_generic_value_list ("server/resstats",
1090 /* callback = */ bind_xml_table_callback,
1091 /* user_data = */ &table_ptr,
1092 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1096 bind_parse_generic_name_value ("server/resstat",
1097 /* callback = */ bind_xml_table_callback,
1098 /* user_data = */ &table_ptr,
1099 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1103 /* XPath: memory/summary
1104 * Variables: TotalUse, InUse, BlockSize, ContextSize, Lost
1107 * <TotalUse>6587096</TotalUse>
1108 * <InUse>1345424</InUse>
1109 * <BlockSize>5505024</BlockSize>
1110 * <ContextSize>3732456</ContextSize>
1114 if (global_memory_stats != 0)
1116 translation_table_ptr_t table_ptr =
1118 memsummary_translation_table,
1119 memsummary_translation_table_length,
1120 /* plugin_instance = */ "global-memory_stats"
1123 bind_parse_generic_value_list ("memory/summary",
1124 /* callback = */ bind_xml_table_callback,
1125 /* user_data = */ &table_ptr,
1126 doc, xpathCtx, current_time, DS_TYPE_GAUGE);
1130 bind_xml_stats_search_views (version, doc, xpathCtx, statsnode,
1134 } /* }}} int bind_xml_stats */
1136 static int bind_xml (const char *data) /* {{{ */
1139 xmlXPathContext *xpathCtx = NULL;
1140 xmlXPathObject *xpathObj = NULL;
1144 doc = xmlParseMemory (data, strlen (data));
1147 ERROR ("bind plugin: xmlParseMemory failed.");
1151 xpathCtx = xmlXPathNewContext (doc);
1152 if (xpathCtx == NULL)
1154 ERROR ("bind plugin: xmlXPathNewContext failed.");
1159 xpathObj = xmlXPathEvalExpression (BAD_CAST "/isc/bind/statistics", xpathCtx);
1160 if (xpathObj == NULL)
1162 ERROR ("bind plugin: Cannot find the <statistics> tag.");
1163 xmlXPathFreeContext (xpathCtx);
1167 else if (xpathObj->nodesetval == NULL)
1169 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
1170 xmlXPathFreeObject (xpathObj);
1171 xmlXPathFreeContext (xpathCtx);
1176 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
1180 int parsed_version = 0;
1182 node = xpathObj->nodesetval->nodeTab[i];
1183 assert (node != NULL);
1185 attr_version = (char *) xmlGetProp (node, BAD_CAST "version");
1186 if (attr_version == NULL)
1188 NOTICE ("bind plugin: Found <statistics> tag doesn't have a "
1189 "`version' attribute.");
1192 DEBUG ("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1194 /* At the time this plugin was written, version "1.0" was used by
1195 * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
1196 * that "1.*" and "2.*" don't introduce structural changes, so we just
1197 * check for the first two characters here. */
1198 if (strncmp ("1.", attr_version, strlen ("1.")) == 0)
1200 else if (strncmp ("2.", attr_version, strlen ("2.")) == 0)
1204 /* TODO: Use the complaint mechanism here. */
1205 NOTICE ("bind plugin: Found <statistics> tag with version `%s'. "
1206 "Unfortunately I have no clue how to parse that. "
1207 "Please open a bug report for this.", attr_version);
1208 xmlFree (attr_version);
1212 ret = bind_xml_stats (parsed_version,
1213 doc, xpathCtx, node);
1215 xmlFree (attr_version);
1216 /* One <statistics> node ought to be enough. */
1220 xmlXPathFreeObject (xpathObj);
1221 xmlXPathFreeContext (xpathCtx);
1225 } /* }}} int bind_xml */
1227 static int bind_config_set_bool (const char *name, int *var, /* {{{ */
1230 if ((ci->values_num != 1) || ( ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1232 WARNING ("bind plugin: The `%s' option needs "
1233 "exactly one boolean argument.", name);
1237 if (ci->values[0].value.boolean)
1242 } /* }}} int bind_config_set_bool */
1244 static int bind_config_add_view_zone (cb_view_t *view, /* {{{ */
1249 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1251 WARNING ("bind plugin: The `Zone' option needs "
1252 "exactly one string argument.");
1256 tmp = (char **) realloc (view->zones,
1257 sizeof (char *) * (view->zones_num + 1));
1260 ERROR ("bind plugin: realloc failed.");
1265 view->zones[view->zones_num] = strdup (ci->values[0].value.string);
1266 if (view->zones[view->zones_num] == NULL)
1268 ERROR ("bind plugin: strdup failed.");
1274 } /* }}} int bind_config_add_view_zone */
1276 static int bind_config_add_view (oconfig_item_t *ci) /* {{{ */
1281 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1283 WARNING ("bind plugin: `View' blocks need exactly one string argument.");
1287 tmp = (cb_view_t *) realloc (views, sizeof (*views) * (views_num + 1));
1290 ERROR ("bind plugin: realloc failed.");
1294 tmp = views + views_num;
1296 memset (tmp, 0, sizeof (*tmp));
1298 tmp->resolver_stats = 1;
1299 tmp->cacherrsets = 1;
1303 tmp->name = strdup (ci->values[0].value.string);
1304 if (tmp->name == NULL)
1306 ERROR ("bind plugin: strdup failed.");
1311 for (i = 0; i < ci->children_num; i++)
1313 oconfig_item_t *child = ci->children + i;
1315 if (strcasecmp ("QTypes", child->key) == 0)
1316 bind_config_set_bool ("QTypes", &tmp->qtypes, child);
1317 else if (strcasecmp ("ResolverStats", child->key) == 0)
1318 bind_config_set_bool ("ResolverStats", &tmp->resolver_stats, child);
1319 else if (strcasecmp ("CacheRRSets", child->key) == 0)
1320 bind_config_set_bool ("CacheRRSets", &tmp->cacherrsets, child);
1321 else if (strcasecmp ("Zone", child->key) == 0)
1322 bind_config_add_view_zone (tmp, child);
1325 WARNING ("bind plugin: Unknown configuration option "
1326 "`%s' in view `%s' will be ignored.", child->key, tmp->name);
1328 } /* for (i = 0; i < ci->children_num; i++) */
1332 } /* }}} int bind_config_add_view */
1334 static int bind_config (oconfig_item_t *ci) /* {{{ */
1338 for (i = 0; i < ci->children_num; i++)
1340 oconfig_item_t *child = ci->children + i;
1342 if (strcasecmp ("Url", child->key) == 0) {
1343 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING))
1345 WARNING ("bind plugin: The `Url' option needs "
1346 "exactly one string argument.");
1350 url = strdup (child->values[0].value.string);
1351 } else if (strcasecmp ("OpCodes", child->key) == 0)
1352 bind_config_set_bool ("OpCodes", &global_opcodes, child);
1353 else if (strcasecmp ("QTypes", child->key) == 0)
1354 bind_config_set_bool ("QTypes", &global_qtypes, child);
1355 else if (strcasecmp ("ServerStats", child->key) == 0)
1356 bind_config_set_bool ("ServerStats", &global_server_stats, child);
1357 else if (strcasecmp ("ZoneMaintStats", child->key) == 0)
1358 bind_config_set_bool ("ZoneMaintStats", &global_zone_maint_stats, child);
1359 else if (strcasecmp ("ResolverStats", child->key) == 0)
1360 bind_config_set_bool ("ResolverStats", &global_resolver_stats, child);
1361 else if (strcasecmp ("MemoryStats", child->key) == 0)
1362 bind_config_set_bool ("MemoryStats", &global_memory_stats, child);
1363 else if (strcasecmp ("View", child->key) == 0)
1364 bind_config_add_view (child);
1367 WARNING ("bind plugin: Unknown configuration option "
1368 "`%s' will be ignored.", child->key);
1373 } /* }}} int bind_config */
1375 static int bind_init (void) /* {{{ */
1380 curl = curl_easy_init ();
1383 ERROR ("bind plugin: bind_init: curl_easy_init failed.");
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);
1393 } /* }}} int bind_init */
1395 static int bind_read (void) /* {{{ */
1401 ERROR ("bind plugin: I don't have a CURL object.");
1405 bind_buffer_fill = 0;
1406 if (curl_easy_perform (curl) != 0)
1408 ERROR ("bind plugin: curl_easy_perform failed: %s",
1413 status = bind_xml (bind_buffer);
1418 } /* }}} int bind_read */
1420 static int bind_shutdown (void) /* {{{ */
1424 curl_easy_cleanup (curl);
1429 } /* }}} int bind_shutdown */
1431 void module_register (void)
1433 plugin_register_complex_config ("bind", bind_config);
1434 plugin_register_init ("bind", bind_init);
1435 plugin_register_read ("bind", bind_read);
1436 plugin_register_shutdown ("bind", bind_shutdown);
1437 } /* void module_register */
1439 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */