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>
24 /* Set to C99 and POSIX code */
25 #ifndef _ISOC99_SOURCE
26 # define _ISOC99_SOURCE
29 # define _POSIX_SOURCE
31 #ifndef _POSIX_C_SOURCE
32 # define _POSIX_C_SOURCE 200112L
38 # define _XOPEN_SOURCE 600
47 #include "configfile.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 remove_special (char *buffer, size_t buffer_size) /* {{{ */
246 for (i = 0; i < buffer_size; i++)
250 if ((!isalnum ((int) buffer[i])) && (buffer[i] != '-'))
253 } /* }}} void remove_special */
255 static void submit (time_t ts, const char *plugin_instance, /* {{{ */
256 const char *type, const char *type_instance, value_t value)
259 value_list_t vl = VALUE_LIST_INIT;
266 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
267 sstrncpy(vl.plugin, "bind", sizeof(vl.plugin));
268 if (plugin_instance) {
269 sstrncpy(vl.plugin_instance, plugin_instance,
270 sizeof(vl.plugin_instance));
271 remove_special (vl.plugin_instance, sizeof (vl.plugin_instance));
273 sstrncpy(vl.type, type, sizeof(vl.type));
275 sstrncpy(vl.type_instance, type_instance,
276 sizeof(vl.type_instance));
277 remove_special (vl.plugin_instance, sizeof (vl.plugin_instance));
279 plugin_dispatch_values(&vl);
280 } /* }}} void submit */
282 static size_t bind_curl_callback (void *buf, size_t size, /* {{{ */
283 size_t nmemb, void __attribute__((unused)) *stream)
285 size_t len = size * nmemb;
290 if ((bind_buffer_fill + len) >= bind_buffer_size)
294 temp = realloc(bind_buffer, bind_buffer_fill + len + 1);
297 ERROR ("bind plugin: realloc failed.");
301 bind_buffer_size = bind_buffer_fill + len + 1;
304 memcpy (bind_buffer + bind_buffer_fill, (char *) buf, len);
305 bind_buffer_fill += len;
306 bind_buffer[bind_buffer_fill] = 0;
309 } /* }}} size_t bind_curl_callback */
312 * Callback, that's called with a translation table.
313 * (Plugin instance is fixed, type and type instance come from lookup table.)
315 static int bind_xml_table_callback (const char *name, value_t value, /* {{{ */
316 time_t current_time, void *user_data)
318 translation_table_ptr_t *table = (translation_table_ptr_t *) user_data;
324 for (i = 0; i < table->table_length; i++)
326 if (strcmp (table->table[i].xml_name, name) != 0)
329 submit (current_time,
330 table->plugin_instance,
331 table->table[i].type,
332 table->table[i].type_instance,
338 } /* }}} int bind_xml_table_callback */
341 * Callback, that's used for lists.
342 * (Plugin instance and type are fixed, xml name is used as type instance.)
344 static int bind_xml_list_callback (const char *name, /* {{{ */
345 value_t value, time_t current_time, void *user_data)
347 list_info_ptr_t *list_info = (list_info_ptr_t *) user_data;
349 if (list_info == NULL)
352 submit (current_time,
353 list_info->plugin_instance,
355 /* type instance = */ name,
359 } /* }}} int bind_xml_list_callback */
361 static int bind_xml_read_counter (xmlDoc *doc, xmlNode *node, /* {{{ */
362 counter_t *ret_value)
364 char *str_ptr, *end_ptr;
367 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
370 ERROR ("bind plugin: bind_xml_read_counter: xmlNodeListGetString failed.");
375 value = strtoll (str_ptr, &end_ptr, 10);
377 if (str_ptr == end_ptr || errno)
379 if (errno && (value < 0))
380 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed with underflow.");
381 else if (errno && (value > 0))
382 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed with overflow.");
384 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed.");
390 } /* }}} int bind_xml_read_counter */
392 static int bind_xml_read_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
395 char *str_ptr, *end_ptr;
398 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
401 ERROR ("bind plugin: bind_xml_read_gauge: xmlNodeListGetString failed.");
406 value = strtod (str_ptr, &end_ptr);
408 if (str_ptr == end_ptr || errno)
410 if (errno && (value < 0))
411 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with underflow.");
412 else if (errno && (value > 0))
413 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with overflow.");
415 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed.");
419 *ret_value = (gauge_t) value;
421 } /* }}} int bind_xml_read_gauge */
423 static int bind_xml_read_timestamp (const char *xpath_expression, /* {{{ */
424 xmlDoc *doc, xmlXPathContext *xpathCtx, time_t *ret_value)
426 xmlXPathObject *xpathObj = NULL;
432 xpathObj = xmlXPathEvalExpression (BAD_CAST xpath_expression, xpathCtx);
433 if (xpathObj == NULL)
435 ERROR ("bind plugin: Unable to evaluate XPath expression `%s'.",
440 if ((xpathObj->nodesetval == NULL) || (xpathObj->nodesetval->nodeNr < 1))
442 xmlXPathFreeObject (xpathObj);
446 if (xpathObj->nodesetval->nodeNr != 1)
448 NOTICE ("bind plugin: Evaluating the XPath expression `%s' returned "
449 "%i nodes. Only handling the first one.",
450 xpath_expression, xpathObj->nodesetval->nodeNr);
453 node = xpathObj->nodesetval->nodeTab[0];
455 if (node->xmlChildrenNode == NULL)
457 ERROR ("bind plugin: bind_xml_read_timestamp: "
458 "node->xmlChildrenNode == NULL");
459 xmlXPathFreeObject (xpathObj);
463 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
466 ERROR ("bind plugin: bind_xml_read_timestamp: xmlNodeListGetString failed.");
467 xmlXPathFreeObject (xpathObj);
471 memset (&tm, 0, sizeof(tm));
472 tmp = strptime (str_ptr, "%Y-%m-%dT%T", &tm);
476 ERROR ("bind plugin: bind_xml_read_timestamp: strptime failed.");
477 xmlXPathFreeObject (xpathObj);
481 *ret_value = timegm(&tm);
483 xmlXPathFreeObject (xpathObj);
485 } /* }}} int bind_xml_read_timestamp */
488 * bind_parse_generic_name_value
490 * Reads statistics in the form:
493 * <counter>123</counter>
496 static int bind_parse_generic_name_value (const char *xpath_expression, /* {{{ */
497 list_callback_t list_callback,
499 xmlDoc *doc, xmlXPathContext *xpathCtx,
500 time_t current_time, int ds_type)
502 xmlXPathObject *xpathObj = NULL;
506 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
507 if (xpathObj == NULL)
509 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
515 /* Iterate over all matching nodes. */
516 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
518 xmlNode *name_node = NULL;
519 xmlNode *counter = NULL;
523 parent = xpathObj->nodesetval->nodeTab[i];
524 DEBUG ("bind plugin: bind_parse_generic_name_value: parent->name = %s;",
525 (char *) parent->name);
527 /* Iterate over all child nodes. */
528 for (child = parent->xmlChildrenNode;
532 if (child->type != XML_ELEMENT_NODE)
535 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
537 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
541 if ((name_node != NULL) && (counter != NULL))
543 char *name = (char *) xmlNodeListGetString (doc,
544 name_node->xmlChildrenNode, 1);
548 if (ds_type == DS_TYPE_GAUGE)
549 status = bind_xml_read_gauge (doc, counter, &value.gauge);
551 status = bind_xml_read_counter (doc, counter, &value.counter);
555 status = (*list_callback) (name, value, current_time, user_data);
563 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
564 num_entries, (num_entries == 1) ? "entry" : "entries",
567 xmlXPathFreeObject(xpathObj);
570 } /* }}} int bind_parse_generic_name_value */
573 * bind_parse_generic_value_list
575 * Reads statistics in the form:
583 static int bind_parse_generic_value_list (const char *xpath_expression, /* {{{ */
584 list_callback_t list_callback,
586 xmlDoc *doc, xmlXPathContext *xpathCtx,
587 time_t current_time, int ds_type)
589 xmlXPathObject *xpathObj = NULL;
593 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
594 if (xpathObj == NULL)
596 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
602 /* Iterate over all matching nodes. */
603 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
607 /* Iterate over all child nodes. */
608 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
616 if (child->type != XML_ELEMENT_NODE)
619 node_name = (char *) child->name;
621 if (ds_type == DS_TYPE_GAUGE)
622 status = bind_xml_read_gauge (doc, child, &value.gauge);
624 status = bind_xml_read_counter (doc, child, &value.counter);
628 status = (*list_callback) (node_name, value, current_time, user_data);
634 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
635 num_entries, (num_entries == 1) ? "entry" : "entries",
638 xmlXPathFreeObject(xpathObj);
641 } /* }}} int bind_parse_generic_value_list */
643 static int bind_xml_stats_handle_zone (int version, xmlDoc *doc, /* {{{ */
644 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
647 xmlXPathObject *path_obj;
648 char *zone_name = NULL;
652 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
653 if (path_obj == NULL)
655 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
659 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
661 zone_name = (char *) xmlNodeListGetString (doc,
662 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
663 if (zone_name != NULL)
667 if (zone_name == NULL)
669 ERROR ("bind plugin: Could not determine zone name.");
670 xmlXPathFreeObject (path_obj);
674 for (j = 0; j < view->zones_num; j++)
676 if (strcasecmp (zone_name, view->zones[j]) == 0)
685 xmlXPathFreeObject (path_obj);
689 zone_name = view->zones[j];
691 DEBUG ("bind plugin: bind_xml_stats_handle_zone: Found zone `%s'.",
694 { /* Parse the <counters> tag {{{ */
695 char plugin_instance[DATA_MAX_NAME_LEN];
696 translation_table_ptr_t table_ptr =
698 nsstats_translation_table,
699 nsstats_translation_table_length,
703 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-zone-%s",
704 view->name, zone_name);
706 bind_parse_generic_value_list (/* xpath = */ "counters",
707 /* callback = */ bind_xml_table_callback,
708 /* user_data = */ &table_ptr,
709 doc, path_ctx, current_time, DS_TYPE_COUNTER);
712 xmlXPathFreeObject (path_obj);
715 } /* }}} int bind_xml_stats_handle_zone */
717 static int bind_xml_stats_search_zones (int version, xmlDoc *doc, /* {{{ */
718 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
721 xmlXPathObject *zone_nodes = NULL;
722 xmlXPathContext *zone_path_context;
725 zone_path_context = xmlXPathNewContext (doc);
726 if (zone_path_context == NULL)
728 ERROR ("bind plugin: xmlXPathNewContext failed.");
732 zone_nodes = xmlXPathEvalExpression (BAD_CAST "zones/zone", path_ctx);
733 if (zone_nodes == NULL)
735 ERROR ("bind plugin: Cannot find any <view> tags.");
736 xmlXPathFreeContext (zone_path_context);
740 for (i = 0; i < zone_nodes->nodesetval->nodeNr; i++)
744 node = zone_nodes->nodesetval->nodeTab[i];
745 assert (node != NULL);
747 zone_path_context->node = node;
749 bind_xml_stats_handle_zone (version, doc, zone_path_context, node, view,
753 xmlXPathFreeObject (zone_nodes);
754 xmlXPathFreeContext (zone_path_context);
756 } /* }}} int bind_xml_stats_search_zones */
758 static int bind_xml_stats_handle_view (int version, xmlDoc *doc, /* {{{ */
759 xmlXPathContext *path_ctx, xmlNode *node, time_t current_time)
761 xmlXPathObject *path_obj;
762 char *view_name = NULL;
767 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
768 if (path_obj == NULL)
770 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
774 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
776 view_name = (char *) xmlNodeListGetString (doc,
777 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
778 if (view_name != NULL)
782 if (view_name == NULL)
784 ERROR ("bind plugin: Could not determine view name.");
785 xmlXPathFreeObject (path_obj);
789 for (j = 0; j < views_num; j++)
791 if (strcasecmp (view_name, views[j].name) == 0)
796 xmlXPathFreeObject (path_obj);
806 DEBUG ("bind plugin: bind_xml_stats_handle_view: Found view `%s'.",
809 if (view->qtypes != 0) /* {{{ */
811 char plugin_instance[DATA_MAX_NAME_LEN];
812 list_info_ptr_t list_info =
815 /* type = */ "dns_qtype_gauge"
818 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-qtypes",
821 bind_parse_generic_name_value (/* xpath = */ "rdtype",
822 /* callback = */ bind_xml_list_callback,
823 /* user_data = */ &list_info,
824 doc, path_ctx, current_time, DS_TYPE_COUNTER);
827 if (view->resolver_stats != 0) /* {{{ */
829 char plugin_instance[DATA_MAX_NAME_LEN];
830 translation_table_ptr_t table_ptr =
832 resstats_translation_table,
833 resstats_translation_table_length,
837 ssnprintf (plugin_instance, sizeof (plugin_instance),
838 "%s-resolver_stats", view->name);
840 bind_parse_generic_name_value ("resstat",
841 /* callback = */ bind_xml_table_callback,
842 /* user_data = */ &table_ptr,
843 doc, path_ctx, current_time, DS_TYPE_COUNTER);
846 if (view->cacherrsets != 0) /* {{{ */
848 char plugin_instance[DATA_MAX_NAME_LEN];
849 list_info_ptr_t list_info =
852 /* type = */ "dns_qtype_gauge"
855 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-cache_rr_sets",
858 bind_parse_generic_name_value (/* xpath = */ "cache/rrset",
859 /* callback = */ bind_xml_list_callback,
860 /* user_data = */ &list_info,
861 doc, path_ctx, current_time, DS_TYPE_GAUGE);
864 if (view->zones_num > 0)
865 bind_xml_stats_search_zones (version, doc, path_ctx, node, view,
869 } /* }}} int bind_xml_stats_handle_view */
871 static int bind_xml_stats_search_views (int version, xmlDoc *doc, /* {{{ */
872 xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time)
874 xmlXPathObject *view_nodes = NULL;
875 xmlXPathContext *view_path_context;
878 view_path_context = xmlXPathNewContext (doc);
879 if (view_path_context == NULL)
881 ERROR ("bind plugin: xmlXPathNewContext failed.");
885 view_nodes = xmlXPathEvalExpression (BAD_CAST "views/view", xpathCtx);
886 if (view_nodes == NULL)
888 ERROR ("bind plugin: Cannot find any <view> tags.");
889 xmlXPathFreeContext (view_path_context);
893 for (i = 0; i < view_nodes->nodesetval->nodeNr; i++)
897 node = view_nodes->nodesetval->nodeTab[i];
898 assert (node != NULL);
900 view_path_context->node = node;
902 bind_xml_stats_handle_view (version, doc, view_path_context, node,
906 xmlXPathFreeObject (view_nodes);
907 xmlXPathFreeContext (view_path_context);
909 } /* }}} int bind_xml_stats_search_views */
911 static int bind_xml_stats (int version, xmlDoc *doc, /* {{{ */
912 xmlXPathContext *xpathCtx, xmlNode *statsnode)
914 time_t current_time = 0;
917 xpathCtx->node = statsnode;
919 /* TODO: Check `server/boot-time' to recognize server restarts. */
921 status = bind_xml_read_timestamp ("server/current-time",
922 doc, xpathCtx, ¤t_time);
925 ERROR ("bind plugin: Reading `server/current-time' failed.");
928 DEBUG ("bind plugin: Current server time is %i.", (int) current_time);
930 /* XPath: server/requests/opcode
931 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
935 * <counter>1</counter>
939 if (global_opcodes != 0)
941 list_info_ptr_t list_info =
943 /* plugin instance = */ "global-opcodes",
944 /* type = */ "dns_opcode"
947 bind_parse_generic_name_value (/* xpath = */ "server/requests/opcode",
948 /* callback = */ bind_xml_list_callback,
949 /* user_data = */ &list_info,
950 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
953 /* XPath: server/queries-in/rdtype
954 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
955 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
956 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
960 * <counter>1</counter>
964 if (global_qtypes != 0)
966 list_info_ptr_t list_info =
968 /* plugin instance = */ "global-qtypes",
969 /* type = */ "dns_qtype"
972 bind_parse_generic_name_value (/* xpath = */ "server/queries-in/rdtype",
973 /* callback = */ bind_xml_list_callback,
974 /* user_data = */ &list_info,
975 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
978 /* XPath: server/nsstats, server/nsstat
979 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
980 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
981 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
982 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
983 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
984 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
985 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
989 * <Requestv4>1</Requestv4>
990 * <Requestv6>0</Requestv6>
995 * <name>Requestv4</name>
996 * <counter>1</counter>
999 * <name>Requestv6</name>
1000 * <counter>0</counter>
1004 if (global_server_stats)
1006 translation_table_ptr_t table_ptr =
1008 nsstats_translation_table,
1009 nsstats_translation_table_length,
1010 /* plugin_instance = */ "global-server_stats"
1015 bind_parse_generic_value_list ("server/nsstats",
1016 /* callback = */ bind_xml_table_callback,
1017 /* user_data = */ &table_ptr,
1018 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1022 bind_parse_generic_name_value ("server/nsstat",
1023 /* callback = */ bind_xml_table_callback,
1024 /* user_data = */ &table_ptr,
1025 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1029 /* XPath: server/zonestats, server/zonestat
1030 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1031 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1032 * XfrSuccess, XfrFail
1035 * <NotifyOutv4>0</NotifyOutv4>
1036 * <NotifyOutv6>0</NotifyOutv6>
1041 * <name>NotifyOutv4</name>
1042 * <counter>0</counter>
1045 * <name>NotifyOutv6</name>
1046 * <counter>0</counter>
1050 if (global_zone_maint_stats)
1052 translation_table_ptr_t table_ptr =
1054 zonestats_translation_table,
1055 zonestats_translation_table_length,
1056 /* plugin_instance = */ "global-zone_maint_stats"
1061 bind_parse_generic_value_list ("server/zonestats",
1062 /* callback = */ bind_xml_table_callback,
1063 /* user_data = */ &table_ptr,
1064 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1068 bind_parse_generic_name_value ("server/zonestat",
1069 /* callback = */ bind_xml_table_callback,
1070 /* user_data = */ &table_ptr,
1071 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1075 /* XPath: server/resstats
1076 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1077 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1078 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1079 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1082 * <Queryv4>0</Queryv4>
1083 * <Queryv6>0</Queryv6>
1088 * <name>Queryv4</name>
1089 * <counter>0</counter>
1092 * <name>Queryv6</name>
1093 * <counter>0</counter>
1097 if (global_resolver_stats != 0)
1099 translation_table_ptr_t table_ptr =
1101 resstats_translation_table,
1102 resstats_translation_table_length,
1103 /* plugin_instance = */ "global-resolver_stats"
1108 bind_parse_generic_value_list ("server/resstats",
1109 /* callback = */ bind_xml_table_callback,
1110 /* user_data = */ &table_ptr,
1111 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1115 bind_parse_generic_name_value ("server/resstat",
1116 /* callback = */ bind_xml_table_callback,
1117 /* user_data = */ &table_ptr,
1118 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1122 /* XPath: memory/summary
1123 * Variables: TotalUse, InUse, BlockSize, ContextSize, Lost
1126 * <TotalUse>6587096</TotalUse>
1127 * <InUse>1345424</InUse>
1128 * <BlockSize>5505024</BlockSize>
1129 * <ContextSize>3732456</ContextSize>
1133 if (global_memory_stats != 0)
1135 translation_table_ptr_t table_ptr =
1137 memsummary_translation_table,
1138 memsummary_translation_table_length,
1139 /* plugin_instance = */ "global-memory_stats"
1142 bind_parse_generic_value_list ("memory/summary",
1143 /* callback = */ bind_xml_table_callback,
1144 /* user_data = */ &table_ptr,
1145 doc, xpathCtx, current_time, DS_TYPE_GAUGE);
1149 bind_xml_stats_search_views (version, doc, xpathCtx, statsnode,
1153 } /* }}} int bind_xml_stats */
1155 static int bind_xml (const char *data) /* {{{ */
1158 xmlXPathContext *xpathCtx = NULL;
1159 xmlXPathObject *xpathObj = NULL;
1163 doc = xmlParseMemory (data, strlen (data));
1166 ERROR ("bind plugin: xmlParseMemory failed.");
1170 xpathCtx = xmlXPathNewContext (doc);
1171 if (xpathCtx == NULL)
1173 ERROR ("bind plugin: xmlXPathNewContext failed.");
1178 xpathObj = xmlXPathEvalExpression (BAD_CAST "/isc/bind/statistics", xpathCtx);
1179 if (xpathObj == NULL)
1181 ERROR ("bind plugin: Cannot find the <statistics> tag.");
1182 xmlXPathFreeContext (xpathCtx);
1186 else if (xpathObj->nodesetval == NULL)
1188 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
1189 xmlXPathFreeObject (xpathObj);
1190 xmlXPathFreeContext (xpathCtx);
1195 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
1199 int parsed_version = 0;
1201 node = xpathObj->nodesetval->nodeTab[i];
1202 assert (node != NULL);
1204 attr_version = (char *) xmlGetProp (node, BAD_CAST "version");
1205 if (attr_version == NULL)
1207 NOTICE ("bind plugin: Found <statistics> tag doesn't have a "
1208 "`version' attribute.");
1211 DEBUG ("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1213 /* At the time this plugin was written, version "1.0" was used by
1214 * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
1215 * that "1.*" and "2.*" don't introduce structural changes, so we just
1216 * check for the first two characters here. */
1217 if (strncmp ("1.", attr_version, strlen ("1.")) == 0)
1219 else if (strncmp ("2.", attr_version, strlen ("2.")) == 0)
1223 /* TODO: Use the complaint mechanism here. */
1224 NOTICE ("bind plugin: Found <statistics> tag with version `%s'. "
1225 "Unfortunately I have no clue how to parse that. "
1226 "Please open a bug report for this.", attr_version);
1227 xmlFree (attr_version);
1231 ret = bind_xml_stats (parsed_version,
1232 doc, xpathCtx, node);
1234 xmlFree (attr_version);
1235 /* One <statistics> node ought to be enough. */
1239 xmlXPathFreeObject (xpathObj);
1240 xmlXPathFreeContext (xpathCtx);
1244 } /* }}} int bind_xml */
1246 static int bind_config_set_bool (const char *name, int *var, /* {{{ */
1249 if ((ci->values_num != 1) || ( ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1251 WARNING ("bind plugin: The `%s' option needs "
1252 "exactly one boolean argument.", name);
1256 if (ci->values[0].value.boolean)
1261 } /* }}} int bind_config_set_bool */
1263 static int bind_config_add_view_zone (cb_view_t *view, /* {{{ */
1268 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1270 WARNING ("bind plugin: The `Zone' option needs "
1271 "exactly one string argument.");
1275 tmp = (char **) realloc (view->zones,
1276 sizeof (char *) * (view->zones_num + 1));
1279 ERROR ("bind plugin: realloc failed.");
1284 view->zones[view->zones_num] = strdup (ci->values[0].value.string);
1285 if (view->zones[view->zones_num] == NULL)
1287 ERROR ("bind plugin: strdup failed.");
1293 } /* }}} int bind_config_add_view_zone */
1295 static int bind_config_add_view (oconfig_item_t *ci) /* {{{ */
1300 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1302 WARNING ("bind plugin: `View' blocks need exactly one string argument.");
1306 tmp = (cb_view_t *) realloc (views, sizeof (*views) * (views_num + 1));
1309 ERROR ("bind plugin: realloc failed.");
1313 tmp = views + views_num;
1315 memset (tmp, 0, sizeof (*tmp));
1317 tmp->resolver_stats = 1;
1318 tmp->cacherrsets = 1;
1322 tmp->name = strdup (ci->values[0].value.string);
1323 if (tmp->name == NULL)
1325 ERROR ("bind plugin: strdup failed.");
1330 for (i = 0; i < ci->children_num; i++)
1332 oconfig_item_t *child = ci->children + i;
1334 if (strcasecmp ("QTypes", child->key) == 0)
1335 bind_config_set_bool ("QTypes", &tmp->qtypes, child);
1336 else if (strcasecmp ("ResolverStats", child->key) == 0)
1337 bind_config_set_bool ("ResolverStats", &tmp->resolver_stats, child);
1338 else if (strcasecmp ("CacheRRSets", child->key) == 0)
1339 bind_config_set_bool ("CacheRRSets", &tmp->cacherrsets, child);
1340 else if (strcasecmp ("Zone", child->key) == 0)
1341 bind_config_add_view_zone (tmp, child);
1344 WARNING ("bind plugin: Unknown configuration option "
1345 "`%s' in view `%s' will be ignored.", child->key, tmp->name);
1347 } /* for (i = 0; i < ci->children_num; i++) */
1351 } /* }}} int bind_config_add_view */
1353 static int bind_config (oconfig_item_t *ci) /* {{{ */
1357 for (i = 0; i < ci->children_num; i++)
1359 oconfig_item_t *child = ci->children + i;
1361 if (strcasecmp ("Url", child->key) == 0) {
1362 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING))
1364 WARNING ("bind plugin: The `Url' option needs "
1365 "exactly one string argument.");
1369 url = strdup (child->values[0].value.string);
1370 } else if (strcasecmp ("OpCodes", child->key) == 0)
1371 bind_config_set_bool ("OpCodes", &global_opcodes, child);
1372 else if (strcasecmp ("QTypes", child->key) == 0)
1373 bind_config_set_bool ("QTypes", &global_qtypes, child);
1374 else if (strcasecmp ("ServerStats", child->key) == 0)
1375 bind_config_set_bool ("ServerStats", &global_server_stats, child);
1376 else if (strcasecmp ("ZoneMaintStats", child->key) == 0)
1377 bind_config_set_bool ("ZoneMaintStats", &global_zone_maint_stats, child);
1378 else if (strcasecmp ("ResolverStats", child->key) == 0)
1379 bind_config_set_bool ("ResolverStats", &global_resolver_stats, child);
1380 else if (strcasecmp ("MemoryStats", child->key) == 0)
1381 bind_config_set_bool ("MemoryStats", &global_memory_stats, child);
1382 else if (strcasecmp ("View", child->key) == 0)
1383 bind_config_add_view (child);
1386 WARNING ("bind plugin: Unknown configuration option "
1387 "`%s' will be ignored.", child->key);
1392 } /* }}} int bind_config */
1394 static int bind_init (void) /* {{{ */
1399 curl = curl_easy_init ();
1402 ERROR ("bind plugin: bind_init: curl_easy_init failed.");
1406 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
1407 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
1408 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, bind_curl_error);
1409 curl_easy_setopt (curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL);
1412 } /* }}} int bind_init */
1414 static int bind_read (void) /* {{{ */
1420 ERROR ("bind plugin: I don't have a CURL object.");
1424 bind_buffer_fill = 0;
1425 if (curl_easy_perform (curl) != 0)
1427 ERROR ("bind plugin: curl_easy_perform failed: %s",
1432 status = bind_xml (bind_buffer);
1437 } /* }}} int bind_read */
1439 static int bind_shutdown (void) /* {{{ */
1443 curl_easy_cleanup (curl);
1448 } /* }}} int bind_shutdown */
1450 void module_register (void)
1452 plugin_register_complex_config ("bind", bind_config);
1453 plugin_register_init ("bind", bind_init);
1454 plugin_register_read ("bind", bind_read);
1455 plugin_register_shutdown ("bind", bind_shutdown);
1456 } /* void module_register */
1458 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */