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>
26 #if STRPTIME_NEEDS_STANDARDS
27 # ifndef _ISOC99_SOURCE
28 # define _ISOC99_SOURCE 1
30 # ifndef _POSIX_C_SOURCE
31 # define _POSIX_C_SOURCE 200112L
33 # ifndef _XOPEN_SOURCE
34 # define _XOPEN_SOURCE 500
36 #endif /* STRPTIME_NEEDS_STANDARDS */
41 #include "configfile.h"
43 /* Some versions of libcurl don't include this themselves and then don't have
44 * fd_set available. */
46 # include <sys/select.h>
49 #include <curl/curl.h>
50 #include <libxml/parser.h>
51 #include <libxml/xpath.h>
53 #ifndef BIND_DEFAULT_URL
54 # define BIND_DEFAULT_URL "http://localhost:8053/"
58 * Some types used for the callback functions. `translation_table_ptr_t' and
59 * `list_info_ptr_t' are passed to the callbacks in the `void *user_data'
62 typedef int (*list_callback_t) (const char *name, value_t value,
63 time_t current_time, void *user_data);
76 typedef struct cb_view_s cb_view_t;
78 struct translation_info_s
82 const char *type_instance;
84 typedef struct translation_info_s translation_info_t;
86 struct translation_table_ptr_s
88 const translation_info_t *table;
90 const char *plugin_instance;
92 typedef struct translation_table_ptr_s translation_table_ptr_t;
94 struct list_info_ptr_s
96 const char *plugin_instance;
99 typedef struct list_info_ptr_s list_info_ptr_t;
101 static char *url = NULL;
102 static int global_opcodes = 1;
103 static int global_qtypes = 1;
104 static int global_server_stats = 1;
105 static int global_zone_maint_stats = 1;
106 static int global_resolver_stats = 0;
107 static int global_memory_stats = 1;
109 static cb_view_t *views = NULL;
110 static size_t views_num = 0;
112 static CURL *curl = NULL;
114 static char *bind_buffer = NULL;
115 static size_t bind_buffer_size = 0;
116 static size_t bind_buffer_fill = 0;
117 static char bind_curl_error[CURL_ERROR_SIZE];
119 /* Translation table for the `nsstats' values. */
120 static const translation_info_t nsstats_translation_table[] = /* {{{ */
123 { "Requestv4", "dns_request", "IPv4" },
124 { "Requestv6", "dns_request", "IPv6" },
125 { "ReqEdns0", "dns_request", "EDNS0" },
126 { "ReqBadEDNSVer", "dns_request", "BadEDNSVer" },
127 { "ReqTSIG", "dns_request", "TSIG" },
128 { "ReqSIG0", "dns_request", "SIG0" },
129 { "ReqBadSIG", "dns_request", "BadSIG" },
130 { "ReqTCP", "dns_request", "TCP" },
132 { "AuthQryRej", "dns_reject", "authorative" },
133 { "RecQryRej", "dns_reject", "recursive" },
134 { "XfrRej", "dns_reject", "transfer" },
135 { "UpdateRej", "dns_reject", "update" },
137 { "Response", "dns_response", "normal" },
138 { "TruncatedResp", "dns_response", "truncated" },
139 { "RespEDNS0", "dns_response", "EDNS0" },
140 { "RespTSIG", "dns_response", "TSIG" },
141 { "RespSIG0", "dns_response", "SIG0" },
143 { "QryAuthAns", "dns_query", "authorative" },
144 { "QryNoauthAns", "dns_query", "nonauth" },
145 { "QryReferral", "dns_query", "referral" },
146 { "QryRecursion", "dns_query", "recursion" },
147 { "QryDuplicate", "dns_query", "dupliate" },
148 { "QryDropped", "dns_query", "dropped" },
149 { "QryFailure", "dns_query", "failure" },
151 { "QrySuccess", "dns_rcode", "tx-NOERROR" },
152 { "QryNxrrset", "dns_rcode", "tx-NXRRSET" },
153 { "QrySERVFAIL", "dns_rcode", "tx-SERVFAIL" },
154 { "QryFORMERR", "dns_rcode", "tx-FORMERR" },
155 { "QryNXDOMAIN", "dns_rcode", "tx-NXDOMAIN" }
157 { "XfrReqDone", "type", "type_instance" },
158 { "UpdateReqFwd", "type", "type_instance" },
159 { "UpdateRespFwd", "type", "type_instance" },
160 { "UpdateFwdFail", "type", "type_instance" },
161 { "UpdateDone", "type", "type_instance" },
162 { "UpdateFail", "type", "type_instance" },
163 { "UpdateBadPrereq", "type", "type_instance" },
166 static int nsstats_translation_table_length =
167 STATIC_ARRAY_SIZE (nsstats_translation_table);
170 /* Translation table for the `zonestats' values. */
171 static const translation_info_t zonestats_translation_table[] = /* {{{ */
174 { "NotifyOutv4", "dns_notify", "tx-IPv4" },
175 { "NotifyOutv6", "dns_notify", "tx-IPv6" },
176 { "NotifyInv4", "dns_notify", "rx-IPv4" },
177 { "NotifyInv6", "dns_notify", "rx-IPv6" },
178 { "NotifyRej", "dns_notify", "rejected" },
179 /* SOA/AXFS/IXFS requests */
180 { "SOAOutv4", "dns_opcode", "SOA-IPv4" },
181 { "SOAOutv6", "dns_opcode", "SOA-IPv6" },
182 { "AXFRReqv4", "dns_opcode", "AXFR-IPv4" },
183 { "AXFRReqv6", "dns_opcode", "AXFR-IPv6" },
184 { "IXFRReqv4", "dns_opcode", "IXFR-IPv4" },
185 { "IXFRReqv6", "dns_opcode", "IXFR-IPv6" },
186 /* Domain transfers */
187 { "XfrSuccess", "dns_transfer", "success" },
188 { "XfrFail", "dns_transfer", "failure" }
190 static int zonestats_translation_table_length =
191 STATIC_ARRAY_SIZE (zonestats_translation_table);
194 /* Translation table for the `resstats' values. */
195 static const translation_info_t resstats_translation_table[] = /* {{{ */
197 /* Generic resolver information */
198 { "Queryv4", "dns_query", "IPv4" },
199 { "Queryv6", "dns_query", "IPv6" },
200 { "Responsev4", "dns_response", "IPv4" },
201 { "Responsev6", "dns_response", "IPv6" },
202 /* Received response codes */
203 { "NXDOMAIN", "dns_rcode", "rx-NXDOMAIN" },
204 { "SERVFAIL", "dns_rcode", "rx-SERVFAIL" },
205 { "FORMERR", "dns_rcode", "rx-FORMERR" },
206 { "OtherError", "dns_rcode", "rx-OTHER" },
207 { "EDNS0Fail", "dns_rcode", "rx-EDNS0Fail"},
208 /* Received responses */
209 { "Mismatch", "dns_response", "mismatch" },
210 { "Truncated", "dns_response", "truncated" },
211 { "Lame", "dns_response", "lame" },
212 { "Retry", "dns_query", "retry" },
214 { "GlueFetchv4", "type", "type_instance" },
215 { "GlueFetchv6", "type", "type_instance" },
216 { "GlueFetchv4Fail", "type", "type_instance" },
217 { "GlueFetchv6Fail", "type", "type_instance" },
219 /* DNSSEC information */
220 { "ValAttempt", "dns_resolver", "DNSSEC-attempt" },
221 { "ValOk", "dns_resolver", "DNSSEC-okay" },
222 { "ValNegOk", "dns_resolver", "DNSSEC-negokay" },
223 { "ValFail", "dns_resolver", "DNSSEC-fail" }
225 static int resstats_translation_table_length =
226 STATIC_ARRAY_SIZE (resstats_translation_table);
229 /* Translation table for the `memory/summary' values. */
230 static const translation_info_t memsummary_translation_table[] = /* {{{ */
232 { "TotalUse", "memory", "TotalUse" },
233 { "InUse", "memory", "InUse" },
234 { "BlockSize", "memory", "BlockSize" },
235 { "ContextSize", "memory", "ContextSize" },
236 { "Lost", "memory", "Lost" }
238 static int memsummary_translation_table_length =
239 STATIC_ARRAY_SIZE (memsummary_translation_table);
242 static void submit (time_t ts, const char *plugin_instance, /* {{{ */
243 const char *type, const char *type_instance, value_t value)
246 value_list_t vl = VALUE_LIST_INIT;
252 vl.time = TIME_T_TO_CDTIME_T (ts);
253 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
254 sstrncpy(vl.plugin, "bind", sizeof(vl.plugin));
255 if (plugin_instance) {
256 sstrncpy(vl.plugin_instance, plugin_instance,
257 sizeof(vl.plugin_instance));
258 replace_special (vl.plugin_instance, sizeof (vl.plugin_instance));
260 sstrncpy(vl.type, type, sizeof(vl.type));
262 sstrncpy(vl.type_instance, type_instance,
263 sizeof(vl.type_instance));
264 replace_special (vl.plugin_instance, sizeof (vl.plugin_instance));
266 plugin_dispatch_values(&vl);
267 } /* }}} void submit */
269 static size_t bind_curl_callback (void *buf, size_t size, /* {{{ */
270 size_t nmemb, void __attribute__((unused)) *stream)
272 size_t len = size * nmemb;
277 if ((bind_buffer_fill + len) >= bind_buffer_size)
281 temp = realloc(bind_buffer, bind_buffer_fill + len + 1);
284 ERROR ("bind plugin: realloc failed.");
288 bind_buffer_size = bind_buffer_fill + len + 1;
291 memcpy (bind_buffer + bind_buffer_fill, (char *) buf, len);
292 bind_buffer_fill += len;
293 bind_buffer[bind_buffer_fill] = 0;
296 } /* }}} size_t bind_curl_callback */
299 * Callback, that's called with a translation table.
300 * (Plugin instance is fixed, type and type instance come from lookup table.)
302 static int bind_xml_table_callback (const char *name, value_t value, /* {{{ */
303 time_t current_time, void *user_data)
305 translation_table_ptr_t *table = (translation_table_ptr_t *) user_data;
311 for (i = 0; i < table->table_length; i++)
313 if (strcmp (table->table[i].xml_name, name) != 0)
316 submit (current_time,
317 table->plugin_instance,
318 table->table[i].type,
319 table->table[i].type_instance,
325 } /* }}} int bind_xml_table_callback */
328 * Callback, that's used for lists.
329 * (Plugin instance and type are fixed, xml name is used as type instance.)
331 static int bind_xml_list_callback (const char *name, /* {{{ */
332 value_t value, time_t current_time, void *user_data)
334 list_info_ptr_t *list_info = (list_info_ptr_t *) user_data;
336 if (list_info == NULL)
339 submit (current_time,
340 list_info->plugin_instance,
342 /* type instance = */ name,
346 } /* }}} int bind_xml_list_callback */
348 static int bind_xml_read_derive (xmlDoc *doc, xmlNode *node, /* {{{ */
355 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
358 ERROR ("bind plugin: bind_xml_read_derive: xmlNodeListGetString failed.");
362 status = parse_value (str_ptr, &value, DS_TYPE_DERIVE);
365 ERROR ("bind plugin: Parsing string \"%s\" to derive value failed.",
370 *ret_value = value.derive;
372 } /* }}} int bind_xml_read_derive */
374 static int bind_xml_read_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
377 char *str_ptr, *end_ptr;
380 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
383 ERROR ("bind plugin: bind_xml_read_gauge: xmlNodeListGetString failed.");
388 value = strtod (str_ptr, &end_ptr);
390 if (str_ptr == end_ptr || errno)
392 if (errno && (value < 0))
393 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with underflow.");
394 else if (errno && (value > 0))
395 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with overflow.");
397 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed.");
401 *ret_value = (gauge_t) value;
403 } /* }}} int bind_xml_read_gauge */
405 static int bind_xml_read_timestamp (const char *xpath_expression, /* {{{ */
406 xmlDoc *doc, xmlXPathContext *xpathCtx, time_t *ret_value)
408 xmlXPathObject *xpathObj = NULL;
414 xpathObj = xmlXPathEvalExpression (BAD_CAST xpath_expression, xpathCtx);
415 if (xpathObj == NULL)
417 ERROR ("bind plugin: Unable to evaluate XPath expression `%s'.",
422 if ((xpathObj->nodesetval == NULL) || (xpathObj->nodesetval->nodeNr < 1))
424 xmlXPathFreeObject (xpathObj);
428 if (xpathObj->nodesetval->nodeNr != 1)
430 NOTICE ("bind plugin: Evaluating the XPath expression `%s' returned "
431 "%i nodes. Only handling the first one.",
432 xpath_expression, xpathObj->nodesetval->nodeNr);
435 node = xpathObj->nodesetval->nodeTab[0];
437 if (node->xmlChildrenNode == NULL)
439 ERROR ("bind plugin: bind_xml_read_timestamp: "
440 "node->xmlChildrenNode == NULL");
441 xmlXPathFreeObject (xpathObj);
445 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
448 ERROR ("bind plugin: bind_xml_read_timestamp: xmlNodeListGetString failed.");
449 xmlXPathFreeObject (xpathObj);
453 memset (&tm, 0, sizeof(tm));
454 tmp = strptime (str_ptr, "%Y-%m-%dT%T", &tm);
458 ERROR ("bind plugin: bind_xml_read_timestamp: strptime failed.");
459 xmlXPathFreeObject (xpathObj);
463 *ret_value = mktime(&tm);
465 xmlXPathFreeObject (xpathObj);
467 } /* }}} int bind_xml_read_timestamp */
470 * bind_parse_generic_name_value
472 * Reads statistics in the form:
475 * <counter>123</counter>
478 static int bind_parse_generic_name_value (const char *xpath_expression, /* {{{ */
479 list_callback_t list_callback,
481 xmlDoc *doc, xmlXPathContext *xpathCtx,
482 time_t current_time, int ds_type)
484 xmlXPathObject *xpathObj = NULL;
488 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
489 if (xpathObj == NULL)
491 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
497 /* Iterate over all matching nodes. */
498 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
500 xmlNode *name_node = NULL;
501 xmlNode *counter = NULL;
505 parent = xpathObj->nodesetval->nodeTab[i];
506 DEBUG ("bind plugin: bind_parse_generic_name_value: parent->name = %s;",
507 (char *) parent->name);
509 /* Iterate over all child nodes. */
510 for (child = parent->xmlChildrenNode;
514 if (child->type != XML_ELEMENT_NODE)
517 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
519 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
523 if ((name_node != NULL) && (counter != NULL))
525 char *name = (char *) xmlNodeListGetString (doc,
526 name_node->xmlChildrenNode, 1);
530 if (ds_type == DS_TYPE_GAUGE)
531 status = bind_xml_read_gauge (doc, counter, &value.gauge);
533 status = bind_xml_read_derive (doc, counter, &value.derive);
537 status = (*list_callback) (name, value, current_time, user_data);
545 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
546 num_entries, (num_entries == 1) ? "entry" : "entries",
549 xmlXPathFreeObject(xpathObj);
552 } /* }}} int bind_parse_generic_name_value */
555 * bind_parse_generic_value_list
557 * Reads statistics in the form:
565 static int bind_parse_generic_value_list (const char *xpath_expression, /* {{{ */
566 list_callback_t list_callback,
568 xmlDoc *doc, xmlXPathContext *xpathCtx,
569 time_t current_time, int ds_type)
571 xmlXPathObject *xpathObj = NULL;
575 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
576 if (xpathObj == NULL)
578 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
584 /* Iterate over all matching nodes. */
585 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
589 /* Iterate over all child nodes. */
590 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
598 if (child->type != XML_ELEMENT_NODE)
601 node_name = (char *) child->name;
603 if (ds_type == DS_TYPE_GAUGE)
604 status = bind_xml_read_gauge (doc, child, &value.gauge);
606 status = bind_xml_read_derive (doc, child, &value.derive);
610 status = (*list_callback) (node_name, value, current_time, user_data);
616 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
617 num_entries, (num_entries == 1) ? "entry" : "entries",
620 xmlXPathFreeObject(xpathObj);
623 } /* }}} int bind_parse_generic_value_list */
625 static int bind_xml_stats_handle_zone (int version, xmlDoc *doc, /* {{{ */
626 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
629 xmlXPathObject *path_obj;
630 char *zone_name = NULL;
634 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
635 if (path_obj == NULL)
637 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
641 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
643 zone_name = (char *) xmlNodeListGetString (doc,
644 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
645 if (zone_name != NULL)
649 if (zone_name == NULL)
651 ERROR ("bind plugin: Could not determine zone name.");
652 xmlXPathFreeObject (path_obj);
656 for (j = 0; j < view->zones_num; j++)
658 if (strcasecmp (zone_name, view->zones[j]) == 0)
667 xmlXPathFreeObject (path_obj);
671 zone_name = view->zones[j];
673 DEBUG ("bind plugin: bind_xml_stats_handle_zone: Found zone `%s'.",
676 { /* Parse the <counters> tag {{{ */
677 char plugin_instance[DATA_MAX_NAME_LEN];
678 translation_table_ptr_t table_ptr =
680 nsstats_translation_table,
681 nsstats_translation_table_length,
685 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-zone-%s",
686 view->name, zone_name);
688 bind_parse_generic_value_list (/* xpath = */ "counters",
689 /* callback = */ bind_xml_table_callback,
690 /* user_data = */ &table_ptr,
691 doc, path_ctx, current_time, DS_TYPE_COUNTER);
694 xmlXPathFreeObject (path_obj);
697 } /* }}} int bind_xml_stats_handle_zone */
699 static int bind_xml_stats_search_zones (int version, xmlDoc *doc, /* {{{ */
700 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
703 xmlXPathObject *zone_nodes = NULL;
704 xmlXPathContext *zone_path_context;
707 zone_path_context = xmlXPathNewContext (doc);
708 if (zone_path_context == NULL)
710 ERROR ("bind plugin: xmlXPathNewContext failed.");
714 zone_nodes = xmlXPathEvalExpression (BAD_CAST "zones/zone", path_ctx);
715 if (zone_nodes == NULL)
717 ERROR ("bind plugin: Cannot find any <view> tags.");
718 xmlXPathFreeContext (zone_path_context);
722 for (i = 0; i < zone_nodes->nodesetval->nodeNr; i++)
726 node = zone_nodes->nodesetval->nodeTab[i];
727 assert (node != NULL);
729 zone_path_context->node = node;
731 bind_xml_stats_handle_zone (version, doc, zone_path_context, node, view,
735 xmlXPathFreeObject (zone_nodes);
736 xmlXPathFreeContext (zone_path_context);
738 } /* }}} int bind_xml_stats_search_zones */
740 static int bind_xml_stats_handle_view (int version, xmlDoc *doc, /* {{{ */
741 xmlXPathContext *path_ctx, xmlNode *node, time_t current_time)
743 xmlXPathObject *path_obj;
744 char *view_name = NULL;
749 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
750 if (path_obj == NULL)
752 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
756 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
758 view_name = (char *) xmlNodeListGetString (doc,
759 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
760 if (view_name != NULL)
764 if (view_name == NULL)
766 ERROR ("bind plugin: Could not determine view name.");
767 xmlXPathFreeObject (path_obj);
771 for (j = 0; j < views_num; j++)
773 if (strcasecmp (view_name, views[j].name) == 0)
778 xmlXPathFreeObject (path_obj);
788 DEBUG ("bind plugin: bind_xml_stats_handle_view: Found view `%s'.",
791 if (view->qtypes != 0) /* {{{ */
793 char plugin_instance[DATA_MAX_NAME_LEN];
794 list_info_ptr_t list_info =
797 /* type = */ "dns_qtype_gauge"
800 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-qtypes",
803 bind_parse_generic_name_value (/* xpath = */ "rdtype",
804 /* callback = */ bind_xml_list_callback,
805 /* user_data = */ &list_info,
806 doc, path_ctx, current_time, DS_TYPE_COUNTER);
809 if (view->resolver_stats != 0) /* {{{ */
811 char plugin_instance[DATA_MAX_NAME_LEN];
812 translation_table_ptr_t table_ptr =
814 resstats_translation_table,
815 resstats_translation_table_length,
819 ssnprintf (plugin_instance, sizeof (plugin_instance),
820 "%s-resolver_stats", view->name);
822 bind_parse_generic_name_value ("resstat",
823 /* callback = */ bind_xml_table_callback,
824 /* user_data = */ &table_ptr,
825 doc, path_ctx, current_time, DS_TYPE_COUNTER);
828 if (view->cacherrsets != 0) /* {{{ */
830 char plugin_instance[DATA_MAX_NAME_LEN];
831 list_info_ptr_t list_info =
834 /* type = */ "dns_qtype_gauge"
837 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-cache_rr_sets",
840 bind_parse_generic_name_value (/* xpath = */ "cache/rrset",
841 /* callback = */ bind_xml_list_callback,
842 /* user_data = */ &list_info,
843 doc, path_ctx, current_time, DS_TYPE_GAUGE);
846 if (view->zones_num > 0)
847 bind_xml_stats_search_zones (version, doc, path_ctx, node, view,
851 } /* }}} int bind_xml_stats_handle_view */
853 static int bind_xml_stats_search_views (int version, xmlDoc *doc, /* {{{ */
854 xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time)
856 xmlXPathObject *view_nodes = NULL;
857 xmlXPathContext *view_path_context;
860 view_path_context = xmlXPathNewContext (doc);
861 if (view_path_context == NULL)
863 ERROR ("bind plugin: xmlXPathNewContext failed.");
867 view_nodes = xmlXPathEvalExpression (BAD_CAST "views/view", xpathCtx);
868 if (view_nodes == NULL)
870 ERROR ("bind plugin: Cannot find any <view> tags.");
871 xmlXPathFreeContext (view_path_context);
875 for (i = 0; i < view_nodes->nodesetval->nodeNr; i++)
879 node = view_nodes->nodesetval->nodeTab[i];
880 assert (node != NULL);
882 view_path_context->node = node;
884 bind_xml_stats_handle_view (version, doc, view_path_context, node,
888 xmlXPathFreeObject (view_nodes);
889 xmlXPathFreeContext (view_path_context);
891 } /* }}} int bind_xml_stats_search_views */
893 static int bind_xml_stats (int version, xmlDoc *doc, /* {{{ */
894 xmlXPathContext *xpathCtx, xmlNode *statsnode)
896 time_t current_time = 0;
899 xpathCtx->node = statsnode;
901 /* TODO: Check `server/boot-time' to recognize server restarts. */
903 status = bind_xml_read_timestamp ("server/current-time",
904 doc, xpathCtx, ¤t_time);
907 ERROR ("bind plugin: Reading `server/current-time' failed.");
910 DEBUG ("bind plugin: Current server time is %i.", (int) current_time);
912 /* XPath: server/requests/opcode
913 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
917 * <counter>1</counter>
921 if (global_opcodes != 0)
923 list_info_ptr_t list_info =
925 /* plugin instance = */ "global-opcodes",
926 /* type = */ "dns_opcode"
929 bind_parse_generic_name_value (/* xpath = */ "server/requests/opcode",
930 /* callback = */ bind_xml_list_callback,
931 /* user_data = */ &list_info,
932 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
935 /* XPath: server/queries-in/rdtype
936 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
937 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
938 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
942 * <counter>1</counter>
946 if (global_qtypes != 0)
948 list_info_ptr_t list_info =
950 /* plugin instance = */ "global-qtypes",
951 /* type = */ "dns_qtype"
954 bind_parse_generic_name_value (/* xpath = */ "server/queries-in/rdtype",
955 /* callback = */ bind_xml_list_callback,
956 /* user_data = */ &list_info,
957 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
960 /* XPath: server/nsstats, server/nsstat
961 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
962 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
963 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
964 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
965 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
966 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
967 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
971 * <Requestv4>1</Requestv4>
972 * <Requestv6>0</Requestv6>
977 * <name>Requestv4</name>
978 * <counter>1</counter>
981 * <name>Requestv6</name>
982 * <counter>0</counter>
986 if (global_server_stats)
988 translation_table_ptr_t table_ptr =
990 nsstats_translation_table,
991 nsstats_translation_table_length,
992 /* plugin_instance = */ "global-server_stats"
997 bind_parse_generic_value_list ("server/nsstats",
998 /* callback = */ bind_xml_table_callback,
999 /* user_data = */ &table_ptr,
1000 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1004 bind_parse_generic_name_value ("server/nsstat",
1005 /* callback = */ bind_xml_table_callback,
1006 /* user_data = */ &table_ptr,
1007 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1011 /* XPath: server/zonestats, server/zonestat
1012 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1013 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1014 * XfrSuccess, XfrFail
1017 * <NotifyOutv4>0</NotifyOutv4>
1018 * <NotifyOutv6>0</NotifyOutv6>
1023 * <name>NotifyOutv4</name>
1024 * <counter>0</counter>
1027 * <name>NotifyOutv6</name>
1028 * <counter>0</counter>
1032 if (global_zone_maint_stats)
1034 translation_table_ptr_t table_ptr =
1036 zonestats_translation_table,
1037 zonestats_translation_table_length,
1038 /* plugin_instance = */ "global-zone_maint_stats"
1043 bind_parse_generic_value_list ("server/zonestats",
1044 /* callback = */ bind_xml_table_callback,
1045 /* user_data = */ &table_ptr,
1046 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1050 bind_parse_generic_name_value ("server/zonestat",
1051 /* callback = */ bind_xml_table_callback,
1052 /* user_data = */ &table_ptr,
1053 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1057 /* XPath: server/resstats
1058 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1059 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1060 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1061 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1064 * <Queryv4>0</Queryv4>
1065 * <Queryv6>0</Queryv6>
1070 * <name>Queryv4</name>
1071 * <counter>0</counter>
1074 * <name>Queryv6</name>
1075 * <counter>0</counter>
1079 if (global_resolver_stats != 0)
1081 translation_table_ptr_t table_ptr =
1083 resstats_translation_table,
1084 resstats_translation_table_length,
1085 /* plugin_instance = */ "global-resolver_stats"
1090 bind_parse_generic_value_list ("server/resstats",
1091 /* callback = */ bind_xml_table_callback,
1092 /* user_data = */ &table_ptr,
1093 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1097 bind_parse_generic_name_value ("server/resstat",
1098 /* callback = */ bind_xml_table_callback,
1099 /* user_data = */ &table_ptr,
1100 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1104 /* XPath: memory/summary
1105 * Variables: TotalUse, InUse, BlockSize, ContextSize, Lost
1108 * <TotalUse>6587096</TotalUse>
1109 * <InUse>1345424</InUse>
1110 * <BlockSize>5505024</BlockSize>
1111 * <ContextSize>3732456</ContextSize>
1115 if (global_memory_stats != 0)
1117 translation_table_ptr_t table_ptr =
1119 memsummary_translation_table,
1120 memsummary_translation_table_length,
1121 /* plugin_instance = */ "global-memory_stats"
1124 bind_parse_generic_value_list ("memory/summary",
1125 /* callback = */ bind_xml_table_callback,
1126 /* user_data = */ &table_ptr,
1127 doc, xpathCtx, current_time, DS_TYPE_GAUGE);
1131 bind_xml_stats_search_views (version, doc, xpathCtx, statsnode,
1135 } /* }}} int bind_xml_stats */
1137 static int bind_xml (const char *data) /* {{{ */
1140 xmlXPathContext *xpathCtx = NULL;
1141 xmlXPathObject *xpathObj = NULL;
1145 doc = xmlParseMemory (data, strlen (data));
1148 ERROR ("bind plugin: xmlParseMemory failed.");
1152 xpathCtx = xmlXPathNewContext (doc);
1153 if (xpathCtx == NULL)
1155 ERROR ("bind plugin: xmlXPathNewContext failed.");
1160 xpathObj = xmlXPathEvalExpression (BAD_CAST "/isc/bind/statistics", xpathCtx);
1161 if (xpathObj == NULL)
1163 ERROR ("bind plugin: Cannot find the <statistics> tag.");
1164 xmlXPathFreeContext (xpathCtx);
1168 else if (xpathObj->nodesetval == NULL)
1170 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
1171 xmlXPathFreeObject (xpathObj);
1172 xmlXPathFreeContext (xpathCtx);
1177 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
1181 int parsed_version = 0;
1183 node = xpathObj->nodesetval->nodeTab[i];
1184 assert (node != NULL);
1186 attr_version = (char *) xmlGetProp (node, BAD_CAST "version");
1187 if (attr_version == NULL)
1189 NOTICE ("bind plugin: Found <statistics> tag doesn't have a "
1190 "`version' attribute.");
1193 DEBUG ("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1195 /* At the time this plugin was written, version "1.0" was used by
1196 * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
1197 * that "1.*" and "2.*" don't introduce structural changes, so we just
1198 * check for the first two characters here. */
1199 if (strncmp ("1.", attr_version, strlen ("1.")) == 0)
1201 else if (strncmp ("2.", attr_version, strlen ("2.")) == 0)
1205 /* TODO: Use the complaint mechanism here. */
1206 NOTICE ("bind plugin: Found <statistics> tag with version `%s'. "
1207 "Unfortunately I have no clue how to parse that. "
1208 "Please open a bug report for this.", attr_version);
1209 xmlFree (attr_version);
1213 ret = bind_xml_stats (parsed_version,
1214 doc, xpathCtx, node);
1216 xmlFree (attr_version);
1217 /* One <statistics> node ought to be enough. */
1221 xmlXPathFreeObject (xpathObj);
1222 xmlXPathFreeContext (xpathCtx);
1226 } /* }}} int bind_xml */
1228 static int bind_config_set_bool (const char *name, int *var, /* {{{ */
1231 if ((ci->values_num != 1) || ( ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1233 WARNING ("bind plugin: The `%s' option needs "
1234 "exactly one boolean argument.", name);
1238 if (ci->values[0].value.boolean)
1243 } /* }}} int bind_config_set_bool */
1245 static int bind_config_add_view_zone (cb_view_t *view, /* {{{ */
1250 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1252 WARNING ("bind plugin: The `Zone' option needs "
1253 "exactly one string argument.");
1257 tmp = (char **) realloc (view->zones,
1258 sizeof (char *) * (view->zones_num + 1));
1261 ERROR ("bind plugin: realloc failed.");
1266 view->zones[view->zones_num] = strdup (ci->values[0].value.string);
1267 if (view->zones[view->zones_num] == NULL)
1269 ERROR ("bind plugin: strdup failed.");
1275 } /* }}} int bind_config_add_view_zone */
1277 static int bind_config_add_view (oconfig_item_t *ci) /* {{{ */
1282 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1284 WARNING ("bind plugin: `View' blocks need exactly one string argument.");
1288 tmp = (cb_view_t *) realloc (views, sizeof (*views) * (views_num + 1));
1291 ERROR ("bind plugin: realloc failed.");
1295 tmp = views + views_num;
1297 memset (tmp, 0, sizeof (*tmp));
1299 tmp->resolver_stats = 1;
1300 tmp->cacherrsets = 1;
1304 tmp->name = strdup (ci->values[0].value.string);
1305 if (tmp->name == NULL)
1307 ERROR ("bind plugin: strdup failed.");
1312 for (i = 0; i < ci->children_num; i++)
1314 oconfig_item_t *child = ci->children + i;
1316 if (strcasecmp ("QTypes", child->key) == 0)
1317 bind_config_set_bool ("QTypes", &tmp->qtypes, child);
1318 else if (strcasecmp ("ResolverStats", child->key) == 0)
1319 bind_config_set_bool ("ResolverStats", &tmp->resolver_stats, child);
1320 else if (strcasecmp ("CacheRRSets", child->key) == 0)
1321 bind_config_set_bool ("CacheRRSets", &tmp->cacherrsets, child);
1322 else if (strcasecmp ("Zone", child->key) == 0)
1323 bind_config_add_view_zone (tmp, child);
1326 WARNING ("bind plugin: Unknown configuration option "
1327 "`%s' in view `%s' will be ignored.", child->key, tmp->name);
1329 } /* for (i = 0; i < ci->children_num; i++) */
1333 } /* }}} int bind_config_add_view */
1335 static int bind_config (oconfig_item_t *ci) /* {{{ */
1339 for (i = 0; i < ci->children_num; i++)
1341 oconfig_item_t *child = ci->children + i;
1343 if (strcasecmp ("Url", child->key) == 0) {
1344 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING))
1346 WARNING ("bind plugin: The `Url' option needs "
1347 "exactly one string argument.");
1351 url = strdup (child->values[0].value.string);
1352 } else if (strcasecmp ("OpCodes", child->key) == 0)
1353 bind_config_set_bool ("OpCodes", &global_opcodes, child);
1354 else if (strcasecmp ("QTypes", child->key) == 0)
1355 bind_config_set_bool ("QTypes", &global_qtypes, child);
1356 else if (strcasecmp ("ServerStats", child->key) == 0)
1357 bind_config_set_bool ("ServerStats", &global_server_stats, child);
1358 else if (strcasecmp ("ZoneMaintStats", child->key) == 0)
1359 bind_config_set_bool ("ZoneMaintStats", &global_zone_maint_stats, child);
1360 else if (strcasecmp ("ResolverStats", child->key) == 0)
1361 bind_config_set_bool ("ResolverStats", &global_resolver_stats, child);
1362 else if (strcasecmp ("MemoryStats", child->key) == 0)
1363 bind_config_set_bool ("MemoryStats", &global_memory_stats, child);
1364 else if (strcasecmp ("View", child->key) == 0)
1365 bind_config_add_view (child);
1368 WARNING ("bind plugin: Unknown configuration option "
1369 "`%s' will be ignored.", child->key);
1374 } /* }}} int bind_config */
1376 static int bind_init (void) /* {{{ */
1381 curl = curl_easy_init ();
1384 ERROR ("bind plugin: bind_init: curl_easy_init failed.");
1388 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
1389 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
1390 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
1391 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, bind_curl_error);
1392 curl_easy_setopt (curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL);
1393 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
1396 } /* }}} int bind_init */
1398 static int bind_read (void) /* {{{ */
1404 ERROR ("bind plugin: I don't have a CURL object.");
1408 bind_buffer_fill = 0;
1409 if (curl_easy_perform (curl) != 0)
1411 ERROR ("bind plugin: curl_easy_perform failed: %s",
1416 status = bind_xml (bind_buffer);
1421 } /* }}} int bind_read */
1423 static int bind_shutdown (void) /* {{{ */
1427 curl_easy_cleanup (curl);
1432 } /* }}} int bind_shutdown */
1434 void module_register (void)
1436 plugin_register_complex_config ("bind", bind_config);
1437 plugin_register_init ("bind", bind_init);
1438 plugin_register_read ("bind", bind_read);
1439 plugin_register_shutdown ("bind", bind_shutdown);
1440 } /* void module_register */
1442 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */