1 /**
2 * collectd - src/bind.c
3 * Copyright (C) 2009 Bruno Prémont
4 * Copyright (C) 2009,2010 Florian Forster
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Bruno Prémont <bonbons at linux-vserver.org>
21 * Florian Forster <octo at collectd.org>
22 **/
24 #include "config.h"
26 #if STRPTIME_NEEDS_STANDARDS
27 #ifndef _ISOC99_SOURCE
28 #define _ISOC99_SOURCE 1
29 #endif
30 #ifndef _POSIX_C_SOURCE
31 #define _POSIX_C_SOURCE 200112L
32 #endif
33 #ifndef _XOPEN_SOURCE
34 #define _XOPEN_SOURCE 500
35 #endif
36 #endif /* STRPTIME_NEEDS_STANDARDS */
38 #if TIMEGM_NEEDS_BSD
39 #ifndef _BSD_SOURCE
40 #define _BSD_SOURCE 1
41 #endif
42 #endif /* TIMEGM_NEEDS_BSD */
44 #include "collectd.h"
46 #include "common.h"
47 #include "plugin.h"
49 #include <time.h>
51 /* Some versions of libcurl don't include this themselves and then don't have
52 * fd_set available. */
53 #if HAVE_SYS_SELECT_H
54 #include <sys/select.h>
55 #endif
57 #include <curl/curl.h>
58 #include <libxml/parser.h>
59 #include <libxml/xpath.h>
61 #ifndef BIND_DEFAULT_URL
62 #define BIND_DEFAULT_URL "http://localhost:8053/"
63 #endif
65 /*
66 * Some types used for the callback functions. `translation_table_ptr_t' and
67 * `list_info_ptr_t' are passed to the callbacks in the `void *user_data'
68 * pointer.
69 */
70 typedef int (*list_callback_t)(const char *name, value_t value,
71 time_t current_time, void *user_data);
73 struct cb_view_s {
74 char *name;
76 int qtypes;
77 int resolver_stats;
78 int cacherrsets;
80 char **zones;
81 size_t zones_num;
82 };
83 typedef struct cb_view_s cb_view_t;
85 struct translation_info_s {
86 const char *xml_name;
87 const char *type;
88 const char *type_instance;
89 };
90 typedef struct translation_info_s translation_info_t;
92 struct translation_table_ptr_s {
93 const translation_info_t *table;
94 size_t table_length;
95 const char *plugin_instance;
96 };
97 typedef struct translation_table_ptr_s translation_table_ptr_t;
99 struct list_info_ptr_s {
100 const char *plugin_instance;
101 const char *type;
102 };
103 typedef struct list_info_ptr_s list_info_ptr_t;
105 /* FIXME: Enabled by default for backwards compatibility. */
106 /* TODO: Remove time parsing code. */
107 static _Bool config_parse_time = 1;
109 static char *url = NULL;
110 static int global_opcodes = 1;
111 static int global_qtypes = 1;
112 static int global_server_stats = 1;
113 static int global_zone_maint_stats = 1;
114 static int global_resolver_stats = 0;
115 static int global_memory_stats = 1;
116 static int timeout = -1;
118 static cb_view_t *views = NULL;
119 static size_t views_num = 0;
121 static CURL *curl = NULL;
123 static char *bind_buffer = NULL;
124 static size_t bind_buffer_size = 0;
125 static size_t bind_buffer_fill = 0;
126 static char bind_curl_error[CURL_ERROR_SIZE];
128 /* Translation table for the `nsstats' values. */
129 static const translation_info_t nsstats_translation_table[] = /* {{{ */
130 {
131 /* Requests */
132 {"Requestv4", "dns_request", "IPv4"},
133 {"Requestv6", "dns_request", "IPv6"},
134 {"ReqEdns0", "dns_request", "EDNS0"},
135 {"ReqBadEDNSVer", "dns_request", "BadEDNSVer"},
136 {"ReqTSIG", "dns_request", "TSIG"},
137 {"ReqSIG0", "dns_request", "SIG0"},
138 {"ReqBadSIG", "dns_request", "BadSIG"},
139 {"ReqTCP", "dns_request", "TCP"},
140 /* Rejects */
141 {"AuthQryRej", "dns_reject", "authorative"},
142 {"RecQryRej", "dns_reject", "recursive"},
143 {"XfrRej", "dns_reject", "transfer"},
144 {"UpdateRej", "dns_reject", "update"},
145 /* Responses */
146 {"Response", "dns_response", "normal"},
147 {"TruncatedResp", "dns_response", "truncated"},
148 {"RespEDNS0", "dns_response", "EDNS0"},
149 {"RespTSIG", "dns_response", "TSIG"},
150 {"RespSIG0", "dns_response", "SIG0"},
151 /* Queries */
152 {"QryAuthAns", "dns_query", "authorative"},
153 {"QryNoauthAns", "dns_query", "nonauth"},
154 {"QryReferral", "dns_query", "referral"},
155 {"QryRecursion", "dns_query", "recursion"},
156 {"QryDuplicate", "dns_query", "dupliate"},
157 {"QryDropped", "dns_query", "dropped"},
158 {"QryFailure", "dns_query", "failure"},
159 /* Response codes */
160 {"QrySuccess", "dns_rcode", "tx-NOERROR"},
161 {"QryNxrrset", "dns_rcode", "tx-NXRRSET"},
162 {"QrySERVFAIL", "dns_rcode", "tx-SERVFAIL"},
163 {"QryFORMERR", "dns_rcode", "tx-FORMERR"},
164 {"QryNXDOMAIN", "dns_rcode", "tx-NXDOMAIN"}
165 #if 0
166 { "XfrReqDone", "type", "type_instance" },
167 { "UpdateReqFwd", "type", "type_instance" },
168 { "UpdateRespFwd", "type", "type_instance" },
169 { "UpdateFwdFail", "type", "type_instance" },
170 { "UpdateDone", "type", "type_instance" },
171 { "UpdateFail", "type", "type_instance" },
172 { "UpdateBadPrereq", "type", "type_instance" },
173 #endif
174 };
175 static int nsstats_translation_table_length =
176 STATIC_ARRAY_SIZE(nsstats_translation_table);
177 /* }}} */
179 /* Translation table for the `zonestats' values. */
180 static const translation_info_t zonestats_translation_table[] = /* {{{ */
181 {
182 /* Notify's */
183 {"NotifyOutv4", "dns_notify", "tx-IPv4"},
184 {"NotifyOutv6", "dns_notify", "tx-IPv6"},
185 {"NotifyInv4", "dns_notify", "rx-IPv4"},
186 {"NotifyInv6", "dns_notify", "rx-IPv6"},
187 {"NotifyRej", "dns_notify", "rejected"},
188 /* SOA/AXFS/IXFS requests */
189 {"SOAOutv4", "dns_opcode", "SOA-IPv4"},
190 {"SOAOutv6", "dns_opcode", "SOA-IPv6"},
191 {"AXFRReqv4", "dns_opcode", "AXFR-IPv4"},
192 {"AXFRReqv6", "dns_opcode", "AXFR-IPv6"},
193 {"IXFRReqv4", "dns_opcode", "IXFR-IPv4"},
194 {"IXFRReqv6", "dns_opcode", "IXFR-IPv6"},
195 /* Domain transfers */
196 {"XfrSuccess", "dns_transfer", "success"},
197 {"XfrFail", "dns_transfer", "failure"}};
198 static int zonestats_translation_table_length =
199 STATIC_ARRAY_SIZE(zonestats_translation_table);
200 /* }}} */
202 /* Translation table for the `resstats' values. */
203 static const translation_info_t resstats_translation_table[] = /* {{{ */
204 {
205 /* Generic resolver information */
206 {"Queryv4", "dns_query", "IPv4"},
207 {"Queryv6", "dns_query", "IPv6"},
208 {"Responsev4", "dns_response", "IPv4"},
209 {"Responsev6", "dns_response", "IPv6"},
210 /* Received response codes */
211 {"NXDOMAIN", "dns_rcode", "rx-NXDOMAIN"},
212 {"SERVFAIL", "dns_rcode", "rx-SERVFAIL"},
213 {"FORMERR", "dns_rcode", "rx-FORMERR"},
214 {"OtherError", "dns_rcode", "rx-OTHER"},
215 {"EDNS0Fail", "dns_rcode", "rx-EDNS0Fail"},
216 /* Received responses */
217 {"Mismatch", "dns_response", "mismatch"},
218 {"Truncated", "dns_response", "truncated"},
219 {"Lame", "dns_response", "lame"},
220 {"Retry", "dns_query", "retry"},
221 #if 0
222 { "GlueFetchv4", "type", "type_instance" },
223 { "GlueFetchv6", "type", "type_instance" },
224 { "GlueFetchv4Fail", "type", "type_instance" },
225 { "GlueFetchv6Fail", "type", "type_instance" },
226 #endif
227 /* DNSSEC information */
228 {"ValAttempt", "dns_resolver", "DNSSEC-attempt"},
229 {"ValOk", "dns_resolver", "DNSSEC-okay"},
230 {"ValNegOk", "dns_resolver", "DNSSEC-negokay"},
231 {"ValFail", "dns_resolver", "DNSSEC-fail"}};
232 static int resstats_translation_table_length =
233 STATIC_ARRAY_SIZE(resstats_translation_table);
234 /* }}} */
236 /* Translation table for the `memory/summary' values. */
237 static const translation_info_t memsummary_translation_table[] = /* {{{ */
238 {{"TotalUse", "memory", "TotalUse"},
239 {"InUse", "memory", "InUse"},
240 {"BlockSize", "memory", "BlockSize"},
241 {"ContextSize", "memory", "ContextSize"},
242 {"Lost", "memory", "Lost"}};
243 static int memsummary_translation_table_length =
244 STATIC_ARRAY_SIZE(memsummary_translation_table);
245 /* }}} */
247 static void submit(time_t ts, const char *plugin_instance, /* {{{ */
248 const char *type, const char *type_instance, value_t value) {
249 value_t values[1];
250 value_list_t vl = VALUE_LIST_INIT;
252 values[0] = value;
254 vl.values = values;
255 vl.values_len = 1;
256 if (config_parse_time)
257 vl.time = TIME_T_TO_CDTIME_T(ts);
258 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
259 sstrncpy(vl.plugin, "bind", sizeof(vl.plugin));
260 if (plugin_instance) {
261 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
262 replace_special(vl.plugin_instance, sizeof(vl.plugin_instance));
263 }
264 sstrncpy(vl.type, type, sizeof(vl.type));
265 if (type_instance) {
266 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
267 replace_special(vl.type_instance, sizeof(vl.type_instance));
268 }
269 plugin_dispatch_values(&vl);
270 } /* }}} void submit */
272 static size_t bind_curl_callback(void *buf, size_t size, /* {{{ */
273 size_t nmemb,
274 void __attribute__((unused)) * stream) {
275 size_t len = size * nmemb;
277 if (len == 0)
278 return (len);
280 if ((bind_buffer_fill + len) >= bind_buffer_size) {
281 char *temp;
283 temp = realloc(bind_buffer, bind_buffer_fill + len + 1);
284 if (temp == NULL) {
285 ERROR("bind plugin: realloc failed.");
286 return (0);
287 }
288 bind_buffer = temp;
289 bind_buffer_size = bind_buffer_fill + len + 1;
290 }
292 memcpy(bind_buffer + bind_buffer_fill, (char *)buf, len);
293 bind_buffer_fill += len;
294 bind_buffer[bind_buffer_fill] = 0;
296 return (len);
297 } /* }}} size_t bind_curl_callback */
299 /*
300 * Callback, that's called with a translation table.
301 * (Plugin instance is fixed, type and type instance come from lookup table.)
302 */
303 static int bind_xml_table_callback(const char *name, value_t value, /* {{{ */
304 time_t current_time, void *user_data) {
305 translation_table_ptr_t *table = (translation_table_ptr_t *)user_data;
307 if (table == NULL)
308 return (-1);
310 for (size_t i = 0; i < table->table_length; i++) {
311 if (strcmp(table->table[i].xml_name, name) != 0)
312 continue;
314 submit(current_time, table->plugin_instance, table->table[i].type,
315 table->table[i].type_instance, value);
316 break;
317 }
319 return (0);
320 } /* }}} int bind_xml_table_callback */
322 /*
323 * Callback, that's used for lists.
324 * (Plugin instance and type are fixed, xml name is used as type instance.)
325 */
326 static int bind_xml_list_callback(const char *name, /* {{{ */
327 value_t value, time_t current_time,
328 void *user_data) {
329 list_info_ptr_t *list_info = (list_info_ptr_t *)user_data;
331 if (list_info == NULL)
332 return (-1);
334 submit(current_time, list_info->plugin_instance, list_info->type,
335 /* type instance = */ name, value);
337 return (0);
338 } /* }}} int bind_xml_list_callback */
340 static int bind_xml_read_derive(xmlDoc *doc, xmlNode *node, /* {{{ */
341 derive_t *ret_value) {
342 char *str_ptr;
343 value_t value;
344 int status;
346 str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
347 if (str_ptr == NULL) {
348 ERROR("bind plugin: bind_xml_read_derive: xmlNodeListGetString failed.");
349 return (-1);
350 }
352 status = parse_value(str_ptr, &value, DS_TYPE_DERIVE);
353 if (status != 0) {
354 ERROR("bind plugin: Parsing string \"%s\" to derive value failed.",
355 str_ptr);
356 xmlFree(str_ptr);
357 return (-1);
358 }
360 xmlFree(str_ptr);
361 *ret_value = value.derive;
362 return (0);
363 } /* }}} int bind_xml_read_derive */
365 static int bind_xml_read_gauge(xmlDoc *doc, xmlNode *node, /* {{{ */
366 gauge_t *ret_value) {
367 char *str_ptr, *end_ptr;
368 double value;
370 str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
371 if (str_ptr == NULL) {
372 ERROR("bind plugin: bind_xml_read_gauge: xmlNodeListGetString failed.");
373 return (-1);
374 }
376 errno = 0;
377 value = strtod(str_ptr, &end_ptr);
378 xmlFree(str_ptr);
379 if (str_ptr == end_ptr || errno) {
380 if (errno && (value < 0))
381 ERROR("bind plugin: bind_xml_read_gauge: strtod failed with underflow.");
382 else if (errno && (value > 0))
383 ERROR("bind plugin: bind_xml_read_gauge: strtod failed with overflow.");
384 else
385 ERROR("bind plugin: bind_xml_read_gauge: strtod failed.");
386 return (-1);
387 }
389 *ret_value = (gauge_t)value;
390 return (0);
391 } /* }}} int bind_xml_read_gauge */
393 static int bind_xml_read_timestamp(const char *xpath_expression, /* {{{ */
394 xmlDoc *doc, xmlXPathContext *xpathCtx,
395 time_t *ret_value) {
396 xmlXPathObject *xpathObj = NULL;
397 xmlNode *node;
398 char *str_ptr;
399 char *tmp;
400 struct tm tm = {0};
402 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
403 if (xpathObj == NULL) {
404 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
405 xpath_expression);
406 return (-1);
407 }
409 if ((xpathObj->nodesetval == NULL) || (xpathObj->nodesetval->nodeNr < 1)) {
410 xmlXPathFreeObject(xpathObj);
411 return (-1);
412 }
414 if (xpathObj->nodesetval->nodeNr != 1) {
415 NOTICE("bind plugin: Evaluating the XPath expression `%s' returned "
416 "%i nodes. Only handling the first one.",
417 xpath_expression, xpathObj->nodesetval->nodeNr);
418 }
420 node = xpathObj->nodesetval->nodeTab[0];
422 if (node->xmlChildrenNode == NULL) {
423 ERROR("bind plugin: bind_xml_read_timestamp: "
424 "node->xmlChildrenNode == NULL");
425 xmlXPathFreeObject(xpathObj);
426 return (-1);
427 }
429 str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
430 if (str_ptr == NULL) {
431 ERROR("bind plugin: bind_xml_read_timestamp: xmlNodeListGetString failed.");
432 xmlXPathFreeObject(xpathObj);
433 return (-1);
434 }
436 tmp = strptime(str_ptr, "%Y-%m-%dT%T", &tm);
437 xmlFree(str_ptr);
438 if (tmp == NULL) {
439 ERROR("bind plugin: bind_xml_read_timestamp: strptime failed.");
440 xmlXPathFreeObject(xpathObj);
441 return (-1);
442 }
444 #if HAVE_TIMEGM
445 time_t t = timegm(&tm);
446 if (t == ((time_t)-1)) {
447 char errbuf[1024];
448 ERROR("bind plugin: timegm() failed: %s",
449 sstrerror(errno, errbuf, sizeof(errbuf)));
450 return (-1);
451 }
452 *ret_value = t;
453 #else
454 time_t t = mktime(&tm);
455 if (t == ((time_t)-1)) {
456 char errbuf[1024];
457 ERROR("bind plugin: mktime() failed: %s",
458 sstrerror(errno, errbuf, sizeof(errbuf)));
459 return (-1);
460 }
461 /* mktime assumes that tm is local time. Luckily, it also sets timezone to
462 * the offset used for the conversion, and we undo the conversion to convert
463 * back to UTC. */
464 *ret_value = t - timezone;
465 #endif
467 xmlXPathFreeObject(xpathObj);
468 return (0);
469 } /* }}} int bind_xml_read_timestamp */
471 /*
472 * bind_parse_generic_name_value
473 *
474 * Reads statistics in the form:
475 * <foo>
476 * <name>QUERY</name>
477 * <counter>123</counter>
478 * </foo>
479 */
480 static int bind_parse_generic_name_value(const char *xpath_expression, /* {{{ */
481 list_callback_t list_callback,
482 void *user_data, xmlDoc *doc,
483 xmlXPathContext *xpathCtx,
484 time_t current_time, int ds_type) {
485 xmlXPathObject *xpathObj = NULL;
486 int num_entries;
488 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
489 if (xpathObj == NULL) {
490 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
491 xpath_expression);
492 return (-1);
493 }
495 num_entries = 0;
496 /* Iterate over all matching nodes. */
497 for (int i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr);
498 i++) {
499 xmlNode *name_node = NULL;
500 xmlNode *counter = NULL;
501 xmlNode *parent;
503 parent = xpathObj->nodesetval->nodeTab[i];
504 DEBUG("bind plugin: bind_parse_generic_name_value: parent->name = %s;",
505 (char *)parent->name);
507 /* Iterate over all child nodes. */
508 for (xmlNode *child = parent->xmlChildrenNode; child != NULL;
509 child = child->next) {
510 if (child->type != XML_ELEMENT_NODE)
511 continue;
513 if (xmlStrcmp(BAD_CAST "name", child->name) == 0)
514 name_node = child;
515 else if (xmlStrcmp(BAD_CAST "counter", child->name) == 0)
516 counter = child;
517 }
519 if ((name_node != NULL) && (counter != NULL)) {
520 char *name =
521 (char *)xmlNodeListGetString(doc, name_node->xmlChildrenNode, 1);
522 value_t value;
523 int status;
525 if (ds_type == DS_TYPE_GAUGE)
526 status = bind_xml_read_gauge(doc, counter, &value.gauge);
527 else
528 status = bind_xml_read_derive(doc, counter, &value.derive);
529 if (status != 0)
530 continue;
532 status = (*list_callback)(name, value, current_time, user_data);
533 if (status == 0)
534 num_entries++;
536 xmlFree(name);
537 }
538 }
540 DEBUG("bind plugin: Found %d %s for XPath expression `%s'", num_entries,
541 (num_entries == 1) ? "entry" : "entries", xpath_expression);
543 xmlXPathFreeObject(xpathObj);
545 return (0);
546 } /* }}} int bind_parse_generic_name_value */
548 /*
549 * bind_parse_generic_value_list
550 *
551 * Reads statistics in the form:
552 * <foo>
553 * <name0>123</name0>
554 * <name1>234</name1>
555 * <name2>345</name2>
556 * :
557 * </foo>
558 */
559 static int bind_parse_generic_value_list(const char *xpath_expression, /* {{{ */
560 list_callback_t list_callback,
561 void *user_data, xmlDoc *doc,
562 xmlXPathContext *xpathCtx,
563 time_t current_time, int ds_type) {
564 xmlXPathObject *xpathObj = NULL;
565 int num_entries;
567 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
568 if (xpathObj == NULL) {
569 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
570 xpath_expression);
571 return (-1);
572 }
574 num_entries = 0;
575 /* Iterate over all matching nodes. */
576 for (int i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr);
577 i++) {
578 /* Iterate over all child nodes. */
579 for (xmlNode *child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
580 child != NULL; child = child->next) {
581 char *node_name;
582 value_t value;
583 int status;
585 if (child->type != XML_ELEMENT_NODE)
586 continue;
588 node_name = (char *)child->name;
590 if (ds_type == DS_TYPE_GAUGE)
591 status = bind_xml_read_gauge(doc, child, &value.gauge);
592 else
593 status = bind_xml_read_derive(doc, child, &value.derive);
594 if (status != 0)
595 continue;
597 status = (*list_callback)(node_name, value, current_time, user_data);
598 if (status == 0)
599 num_entries++;
600 }
601 }
603 DEBUG("bind plugin: Found %d %s for XPath expression `%s'", num_entries,
604 (num_entries == 1) ? "entry" : "entries", xpath_expression);
606 xmlXPathFreeObject(xpathObj);
608 return (0);
609 } /* }}} int bind_parse_generic_value_list */
611 /*
612 * bind_parse_generic_name_attr_value_list
613 *
614 * Reads statistics in the form:
615 * <foo>
616 * <counter name="name0">123</counter>
617 * <counter name="name1">234</counter>
618 * <counter name="name2">345</counter>
619 * :
620 * </foo>
621 */
622 static int bind_parse_generic_name_attr_value_list(
623 const char *xpath_expression, /* {{{ */
624 list_callback_t list_callback, void *user_data, xmlDoc *doc,
625 xmlXPathContext *xpathCtx, time_t current_time, int ds_type) {
626 xmlXPathObject *xpathObj = NULL;
627 int num_entries;
629 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
630 if (xpathObj == NULL) {
631 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
632 xpath_expression);
633 return (-1);
634 }
636 num_entries = 0;
637 /* Iterate over all matching nodes. */
638 for (int i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr);
639 i++) {
640 /* Iterate over all child nodes. */
641 for (xmlNode *child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
642 child != NULL; child = child->next) {
643 if (child->type != XML_ELEMENT_NODE)
644 continue;
646 if (strncmp("counter", (char *)child->name, strlen("counter")) != 0)
647 continue;
649 char *attr_name;
650 value_t value;
651 int status;
653 attr_name = (char *)xmlGetProp(child, BAD_CAST "name");
654 if (attr_name == NULL) {
655 DEBUG("bind plugin: found <counter> without name.");
656 continue;
657 }
658 if (ds_type == DS_TYPE_GAUGE)
659 status = bind_xml_read_gauge(doc, child, &value.gauge);
660 else
661 status = bind_xml_read_derive(doc, child, &value.derive);
662 if (status != 0)
663 continue;
665 status = (*list_callback)(attr_name, value, current_time, user_data);
666 if (status == 0)
667 num_entries++;
668 }
669 }
671 DEBUG("bind plugin: Found %d %s for XPath expression `%s'", num_entries,
672 (num_entries == 1) ? "entry" : "entries", xpath_expression);
674 xmlXPathFreeObject(xpathObj);
676 return (0);
677 } /* }}} int bind_parse_generic_name_attr_value_list */
679 static int bind_xml_stats_handle_zone(int version, xmlDoc *doc, /* {{{ */
680 xmlXPathContext *path_ctx, xmlNode *node,
681 cb_view_t *view, time_t current_time) {
682 xmlXPathObject *path_obj;
683 char *zone_name = NULL;
684 size_t j;
686 if (version >= 3) {
687 char *n = (char *)xmlGetProp(node, BAD_CAST "name");
688 char *c = (char *)xmlGetProp(node, BAD_CAST "rdataclass");
689 if (n && c) {
690 zone_name = (char *)xmlMalloc(strlen(n) + strlen(c) + 2);
691 snprintf(zone_name, strlen(n) + strlen(c) + 2, "%s/%s", n, c);
692 }
693 xmlFree(n);
694 xmlFree(c);
695 } else {
696 path_obj = xmlXPathEvalExpression(BAD_CAST "name", path_ctx);
697 if (path_obj == NULL) {
698 ERROR("bind plugin: xmlXPathEvalExpression failed.");
699 return (-1);
700 }
702 for (int i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr);
703 i++) {
704 zone_name = (char *)xmlNodeListGetString(
705 doc, path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
706 if (zone_name != NULL)
707 break;
708 }
709 xmlXPathFreeObject(path_obj);
710 }
712 if (zone_name == NULL) {
713 ERROR("bind plugin: Could not determine zone name.");
714 return (-1);
715 }
717 for (j = 0; j < view->zones_num; j++) {
718 if (strcasecmp(zone_name, view->zones[j]) == 0)
719 break;
720 }
722 xmlFree(zone_name);
723 zone_name = NULL;
725 if (j >= view->zones_num)
726 return (0);
728 zone_name = view->zones[j];
730 DEBUG("bind plugin: bind_xml_stats_handle_zone: Found zone `%s'.", zone_name);
732 { /* Parse the <counters> tag {{{ */
733 char plugin_instance[DATA_MAX_NAME_LEN];
734 translation_table_ptr_t table_ptr = {nsstats_translation_table,
735 nsstats_translation_table_length,
736 plugin_instance};
738 ssnprintf(plugin_instance, sizeof(plugin_instance), "%s-zone-%s",
739 view->name, zone_name);
741 if (version == 3) {
742 list_info_ptr_t list_info = {plugin_instance,
743 /* type = */ "dns_qtype"};
744 bind_parse_generic_name_attr_value_list(
745 /* xpath = */ "counters[@type='rcode']",
746 /* callback = */ bind_xml_table_callback,
747 /* user_data = */ &table_ptr, doc, path_ctx, current_time,
748 DS_TYPE_COUNTER);
749 bind_parse_generic_name_attr_value_list(
750 /* xpath = */ "counters[@type='qtype']",
751 /* callback = */ bind_xml_list_callback,
752 /* user_data = */ &list_info, doc, path_ctx, current_time,
753 DS_TYPE_COUNTER);
754 } else {
755 bind_parse_generic_value_list(/* xpath = */ "counters",
756 /* callback = */ bind_xml_table_callback,
757 /* user_data = */ &table_ptr, doc, path_ctx,
758 current_time, DS_TYPE_COUNTER);
759 }
760 } /* }}} */
762 return (0);
763 } /* }}} int bind_xml_stats_handle_zone */
765 static int bind_xml_stats_search_zones(int version, xmlDoc *doc, /* {{{ */
766 xmlXPathContext *path_ctx, xmlNode *node,
767 cb_view_t *view, time_t current_time) {
768 xmlXPathObject *zone_nodes = NULL;
769 xmlXPathContext *zone_path_context;
771 zone_path_context = xmlXPathNewContext(doc);
772 if (zone_path_context == NULL) {
773 ERROR("bind plugin: xmlXPathNewContext failed.");
774 return (-1);
775 }
777 zone_nodes = xmlXPathEvalExpression(BAD_CAST "zones/zone", path_ctx);
778 if (zone_nodes == NULL) {
779 ERROR("bind plugin: Cannot find any <view> tags.");
780 xmlXPathFreeContext(zone_path_context);
781 return (-1);
782 }
784 for (int i = 0; i < zone_nodes->nodesetval->nodeNr; i++) {
785 node = zone_nodes->nodesetval->nodeTab[i];
786 assert(node != NULL);
788 zone_path_context->node = node;
790 bind_xml_stats_handle_zone(version, doc, zone_path_context, node, view,
791 current_time);
792 }
794 xmlXPathFreeObject(zone_nodes);
795 xmlXPathFreeContext(zone_path_context);
796 return (0);
797 } /* }}} int bind_xml_stats_search_zones */
799 static int bind_xml_stats_handle_view(int version, xmlDoc *doc, /* {{{ */
800 xmlXPathContext *path_ctx, xmlNode *node,
801 time_t current_time) {
802 char *view_name = NULL;
803 cb_view_t *view;
804 size_t j;
806 if (version == 3) {
807 view_name = (char *)xmlGetProp(node, BAD_CAST "name");
809 if (view_name == NULL) {
810 ERROR("bind plugin: Could not determine view name.");
811 return (-1);
812 }
814 for (j = 0; j < views_num; j++) {
815 if (strcasecmp(view_name, views[j].name) == 0)
816 break;
817 }
819 xmlFree(view_name);
820 view_name = NULL;
821 } else {
822 xmlXPathObject *path_obj;
823 path_obj = xmlXPathEvalExpression(BAD_CAST "name", path_ctx);
824 if (path_obj == NULL) {
825 ERROR("bind plugin: xmlXPathEvalExpression failed.");
826 return (-1);
827 }
829 for (int i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr);
830 i++) {
831 view_name = (char *)xmlNodeListGetString(
832 doc, path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
833 if (view_name != NULL)
834 break;
835 }
837 if (view_name == NULL) {
838 ERROR("bind plugin: Could not determine view name.");
839 xmlXPathFreeObject(path_obj);
840 return (-1);
841 }
843 for (j = 0; j < views_num; j++) {
844 if (strcasecmp(view_name, views[j].name) == 0)
845 break;
846 }
848 xmlFree(view_name);
849 xmlXPathFreeObject(path_obj);
851 view_name = NULL;
852 path_obj = NULL;
853 }
855 if (j >= views_num)
856 return (0);
858 view = views + j;
860 DEBUG("bind plugin: bind_xml_stats_handle_view: Found view `%s'.",
861 view->name);
863 if (view->qtypes != 0) /* {{{ */
864 {
865 char plugin_instance[DATA_MAX_NAME_LEN];
866 list_info_ptr_t list_info = {plugin_instance,
867 /* type = */ "dns_qtype"};
869 ssnprintf(plugin_instance, sizeof(plugin_instance), "%s-qtypes",
870 view->name);
871 if (version == 3) {
872 bind_parse_generic_name_attr_value_list(
873 /* xpath = */ "counters[@type='resqtype']",
874 /* callback = */ bind_xml_list_callback,
875 /* user_data = */ &list_info, doc, path_ctx, current_time,
876 DS_TYPE_COUNTER);
877 } else {
878 bind_parse_generic_name_value(/* xpath = */ "rdtype",
879 /* callback = */ bind_xml_list_callback,
880 /* user_data = */ &list_info, doc, path_ctx,
881 current_time, DS_TYPE_COUNTER);
882 }
883 } /* }}} */
885 if (view->resolver_stats != 0) /* {{{ */
886 {
887 char plugin_instance[DATA_MAX_NAME_LEN];
888 translation_table_ptr_t table_ptr = {resstats_translation_table,
889 resstats_translation_table_length,
890 plugin_instance};
892 ssnprintf(plugin_instance, sizeof(plugin_instance), "%s-resolver_stats",
893 view->name);
894 if (version == 3) {
895 bind_parse_generic_name_attr_value_list(
896 "counters[@type='resstats']",
897 /* callback = */ bind_xml_table_callback,
898 /* user_data = */ &table_ptr, doc, path_ctx, current_time,
899 DS_TYPE_COUNTER);
900 } else {
901 bind_parse_generic_name_value("resstat",
902 /* callback = */ bind_xml_table_callback,
903 /* user_data = */ &table_ptr, doc, path_ctx,
904 current_time, DS_TYPE_COUNTER);
905 }
906 } /* }}} */
908 /* Record types in the cache */
909 if (view->cacherrsets != 0) /* {{{ */
910 {
911 char plugin_instance[DATA_MAX_NAME_LEN];
912 list_info_ptr_t list_info = {plugin_instance,
913 /* type = */ "dns_qtype_cached"};
915 ssnprintf(plugin_instance, sizeof(plugin_instance), "%s-cache_rr_sets",
916 view->name);
918 bind_parse_generic_name_value(/* xpath = */ "cache/rrset",
919 /* callback = */ bind_xml_list_callback,
920 /* user_data = */ &list_info, doc, path_ctx,
921 current_time, DS_TYPE_GAUGE);
922 } /* }}} */
924 if (view->zones_num > 0)
925 bind_xml_stats_search_zones(version, doc, path_ctx, node, view,
926 current_time);
928 return (0);
929 } /* }}} int bind_xml_stats_handle_view */
931 static int bind_xml_stats_search_views(int version, xmlDoc *doc, /* {{{ */
932 xmlXPathContext *xpathCtx,
933 xmlNode *statsnode,
934 time_t current_time) {
935 xmlXPathObject *view_nodes = NULL;
936 xmlXPathContext *view_path_context;
938 view_path_context = xmlXPathNewContext(doc);
939 if (view_path_context == NULL) {
940 ERROR("bind plugin: xmlXPathNewContext failed.");
941 return (-1);
942 }
944 view_nodes = xmlXPathEvalExpression(BAD_CAST "views/view", xpathCtx);
945 if (view_nodes == NULL) {
946 ERROR("bind plugin: Cannot find any <view> tags.");
947 xmlXPathFreeContext(view_path_context);
948 return (-1);
949 }
951 for (int i = 0; i < view_nodes->nodesetval->nodeNr; i++) {
952 xmlNode *node;
954 node = view_nodes->nodesetval->nodeTab[i];
955 assert(node != NULL);
957 view_path_context->node = node;
959 bind_xml_stats_handle_view(version, doc, view_path_context, node,
960 current_time);
961 }
963 xmlXPathFreeObject(view_nodes);
964 xmlXPathFreeContext(view_path_context);
965 return (0);
966 } /* }}} int bind_xml_stats_search_views */
968 static void bind_xml_stats_v3(xmlDoc *doc, /* {{{ */
969 xmlXPathContext *xpathCtx, xmlNode *statsnode,
970 time_t current_time) {
971 /* XPath: server/counters[@type='opcode']
972 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
973 * Layout v3:
974 * <counters type="opcode">
975 * <counter name="A">1</counter>
976 * :
977 * </counters>
978 */
979 if (global_opcodes != 0) {
980 list_info_ptr_t list_info = {/* plugin instance = */ "global-opcodes",
981 /* type = */ "dns_opcode"};
982 bind_parse_generic_name_attr_value_list(
983 /* xpath = */ "server/counters[@type='opcode']",
984 /* callback = */ bind_xml_list_callback,
985 /* user_data = */ &list_info, doc, xpathCtx, current_time,
986 DS_TYPE_COUNTER);
987 }
989 /* XPath: server/counters[@type='qtype']
990 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
991 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
992 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
993 * Layout v3:
994 * <counters type="opcode">
995 * <counter name="A">1</counter>
996 * :
997 * </counters>
998 */
999 if (global_qtypes != 0) {
1000 list_info_ptr_t list_info = {/* plugin instance = */ "global-qtypes",
1001 /* type = */ "dns_qtype"};
1003 bind_parse_generic_name_attr_value_list(
1004 /* xpath = */ "server/counters[@type='qtype']",
1005 /* callback = */ bind_xml_list_callback,
1006 /* user_data = */ &list_info, doc, xpathCtx, current_time,
1007 DS_TYPE_COUNTER);
1008 }
1010 /* XPath: server/counters[@type='nsstat']
1011 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
1012 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
1013 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
1014 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
1015 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
1016 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
1017 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
1018 * UpdateBadPrereq
1019 * Layout v3:
1020 * <counters type="nsstat"
1021 * <counter name="Requestv4">1</counter>
1022 * <counter name="Requestv6">0</counter>
1023 * :
1024 * </counter>
1025 */
1026 if (global_server_stats) {
1027 translation_table_ptr_t table_ptr = {
1028 nsstats_translation_table, nsstats_translation_table_length,
1029 /* plugin_instance = */ "global-server_stats"};
1031 bind_parse_generic_name_attr_value_list(
1032 "server/counters[@type='nsstat']",
1033 /* callback = */ bind_xml_table_callback,
1034 /* user_data = */ &table_ptr, doc, xpathCtx, current_time,
1035 DS_TYPE_COUNTER);
1036 }
1038 /* XPath: server/zonestats, server/zonestat,
1039 * server/counters[@type='zonestat']
1040 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1041 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1042 * XfrSuccess, XfrFail
1043 * Layout v3:
1044 * <counters type="zonestat"
1045 * <counter name="NotifyOutv4">0</counter>
1046 * <counter name="NotifyOutv6">0</counter>
1047 * :
1048 * </counter>
1049 */
1050 if (global_zone_maint_stats) {
1051 translation_table_ptr_t table_ptr = {
1052 zonestats_translation_table, zonestats_translation_table_length,
1053 /* plugin_instance = */ "global-zone_maint_stats"};
1055 bind_parse_generic_name_attr_value_list(
1056 "server/counters[@type='zonestat']",
1057 /* callback = */ bind_xml_table_callback,
1058 /* user_data = */ &table_ptr, doc, xpathCtx, current_time,
1059 DS_TYPE_COUNTER);
1060 }
1062 /* XPath: server/resstats, server/counters[@type='resstat']
1063 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1064 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1065 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1066 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1067 * Layout v3:
1068 * <counters type="resstat"
1069 * <counter name="Queryv4">0</counter>
1070 * <counter name="Queryv6">0</counter>
1071 * :
1072 * </counter>
1073 */
1074 if (global_resolver_stats != 0) {
1075 translation_table_ptr_t table_ptr = {
1076 resstats_translation_table, resstats_translation_table_length,
1077 /* plugin_instance = */ "global-resolver_stats"};
1079 bind_parse_generic_name_attr_value_list(
1080 "server/counters[@type='resstat']",
1081 /* callback = */ bind_xml_table_callback,
1082 /* user_data = */ &table_ptr, doc, xpathCtx, current_time,
1083 DS_TYPE_COUNTER);
1084 }
1085 } /* }}} bind_xml_stats_v3 */
1087 static void bind_xml_stats_v1_v2(int version, xmlDoc *doc, /* {{{ */
1088 xmlXPathContext *xpathCtx, xmlNode *statsnode,
1089 time_t current_time) {
1090 /* XPath: server/requests/opcode, server/counters[@type='opcode']
1091 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
1092 * Layout V1 and V2:
1093 * <opcode>
1094 * <name>A</name>
1095 * <counter>1</counter>
1096 * </opcode>
1097 * :
1098 */
1099 if (global_opcodes != 0) {
1100 list_info_ptr_t list_info = {/* plugin instance = */ "global-opcodes",
1101 /* type = */ "dns_opcode"};
1103 bind_parse_generic_name_value(/* xpath = */ "server/requests/opcode",
1104 /* callback = */ bind_xml_list_callback,
1105 /* user_data = */ &list_info, doc, xpathCtx,
1106 current_time, DS_TYPE_COUNTER);
1107 }
1109 /* XPath: server/queries-in/rdtype, server/counters[@type='qtype']
1110 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
1111 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
1112 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
1113 * Layout v1 or v2:
1114 * <rdtype>
1115 * <name>A</name>
1116 * <counter>1</counter>
1117 * </rdtype>
1118 * :
1119 */
1120 if (global_qtypes != 0) {
1121 list_info_ptr_t list_info = {/* plugin instance = */ "global-qtypes",
1122 /* type = */ "dns_qtype"};
1124 bind_parse_generic_name_value(/* xpath = */ "server/queries-in/rdtype",
1125 /* callback = */ bind_xml_list_callback,
1126 /* user_data = */ &list_info, doc, xpathCtx,
1127 current_time, DS_TYPE_COUNTER);
1128 }
1130 /* XPath: server/nsstats, server/nsstat, server/counters[@type='nsstat']
1131 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
1132 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
1133 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
1134 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
1135 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
1136 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
1137 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
1138 * UpdateBadPrereq
1139 * Layout v1:
1140 * <nsstats>
1141 * <Requestv4>1</Requestv4>
1142 * <Requestv6>0</Requestv6>
1143 * :
1144 * </nsstats>
1145 * Layout v2:
1146 * <nsstat>
1147 * <name>Requestv4</name>
1148 * <counter>1</counter>
1149 * </nsstat>
1150 * <nsstat>
1151 * <name>Requestv6</name>
1152 * <counter>0</counter>
1153 * </nsstat>
1154 * :
1155 */
1156 if (global_server_stats) {
1157 translation_table_ptr_t table_ptr = {
1158 nsstats_translation_table, nsstats_translation_table_length,
1159 /* plugin_instance = */ "global-server_stats"};
1161 if (version == 1) {
1162 bind_parse_generic_value_list("server/nsstats",
1163 /* callback = */ bind_xml_table_callback,
1164 /* user_data = */ &table_ptr, doc, xpathCtx,
1165 current_time, DS_TYPE_COUNTER);
1166 } else {
1167 bind_parse_generic_name_value("server/nsstat",
1168 /* callback = */ bind_xml_table_callback,
1169 /* user_data = */ &table_ptr, doc, xpathCtx,
1170 current_time, DS_TYPE_COUNTER);
1171 }
1172 }
1174 /* XPath: server/zonestats, server/zonestat,
1175 * server/counters[@type='zonestat']
1176 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1177 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1178 * XfrSuccess, XfrFail
1179 * Layout v1:
1180 * <zonestats>
1181 * <NotifyOutv4>0</NotifyOutv4>
1182 * <NotifyOutv6>0</NotifyOutv6>
1183 * :
1184 * </zonestats>
1185 * Layout v2:
1186 * <zonestat>
1187 * <name>NotifyOutv4</name>
1188 * <counter>0</counter>
1189 * </zonestat>
1190 * <zonestat>
1191 * <name>NotifyOutv6</name>
1192 * <counter>0</counter>
1193 * </zonestat>
1194 * :
1195 */
1196 if (global_zone_maint_stats) {
1197 translation_table_ptr_t table_ptr = {
1198 zonestats_translation_table, zonestats_translation_table_length,
1199 /* plugin_instance = */ "global-zone_maint_stats"};
1201 if (version == 1) {
1202 bind_parse_generic_value_list("server/zonestats",
1203 /* callback = */ bind_xml_table_callback,
1204 /* user_data = */ &table_ptr, doc, xpathCtx,
1205 current_time, DS_TYPE_COUNTER);
1206 } else {
1207 bind_parse_generic_name_value("server/zonestat",
1208 /* callback = */ bind_xml_table_callback,
1209 /* user_data = */ &table_ptr, doc, xpathCtx,
1210 current_time, DS_TYPE_COUNTER);
1211 }
1212 }
1214 /* XPath: server/resstats, server/counters[@type='resstat']
1215 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1216 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1217 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1218 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1219 * Layout v1:
1220 * <resstats>
1221 * <Queryv4>0</Queryv4>
1222 * <Queryv6>0</Queryv6>
1223 * :
1224 * </resstats>
1225 * Layout v2:
1226 * <resstat>
1227 * <name>Queryv4</name>
1228 * <counter>0</counter>
1229 * </resstat>
1230 * <resstat>
1231 * <name>Queryv6</name>
1232 * <counter>0</counter>
1233 * </resstat>
1234 * :
1235 */
1236 if (global_resolver_stats != 0) {
1237 translation_table_ptr_t table_ptr = {
1238 resstats_translation_table, resstats_translation_table_length,
1239 /* plugin_instance = */ "global-resolver_stats"};
1241 if (version == 1) {
1242 bind_parse_generic_value_list("server/resstats",
1243 /* callback = */ bind_xml_table_callback,
1244 /* user_data = */ &table_ptr, doc, xpathCtx,
1245 current_time, DS_TYPE_COUNTER);
1246 } else {
1247 bind_parse_generic_name_value("server/resstat",
1248 /* callback = */ bind_xml_table_callback,
1249 /* user_data = */ &table_ptr, doc, xpathCtx,
1250 current_time, DS_TYPE_COUNTER);
1251 }
1252 }
1253 } /* }}} bind_xml_stats_v1_v2 */
1255 static int bind_xml_stats(int version, xmlDoc *doc, /* {{{ */
1256 xmlXPathContext *xpathCtx, xmlNode *statsnode) {
1257 time_t current_time = 0;
1258 int status;
1260 xpathCtx->node = statsnode;
1262 /* TODO: Check `server/boot-time' to recognize server restarts. */
1264 status = bind_xml_read_timestamp("server/current-time", doc, xpathCtx,
1265 ¤t_time);
1266 if (status != 0) {
1267 ERROR("bind plugin: Reading `server/current-time' failed.");
1268 return (-1);
1269 }
1270 DEBUG("bind plugin: Current server time is %i.", (int)current_time);
1272 if (version == 3) {
1273 bind_xml_stats_v3(doc, xpathCtx, statsnode, current_time);
1274 } else {
1275 bind_xml_stats_v1_v2(version, doc, xpathCtx, statsnode, current_time);
1276 }
1278 /* XPath: memory/summary
1279 * Variables: TotalUse, InUse, BlockSize, ContextSize, Lost
1280 * Layout: v2 and v3:
1281 * <summary>
1282 * <TotalUse>6587096</TotalUse>
1283 * <InUse>1345424</InUse>
1284 * <BlockSize>5505024</BlockSize>
1285 * <ContextSize>3732456</ContextSize>
1286 * <Lost>0</Lost>
1287 * </summary>
1288 */
1289 if (global_memory_stats != 0) {
1290 translation_table_ptr_t table_ptr = {
1291 memsummary_translation_table, memsummary_translation_table_length,
1292 /* plugin_instance = */ "global-memory_stats"};
1294 bind_parse_generic_value_list("memory/summary",
1295 /* callback = */ bind_xml_table_callback,
1296 /* user_data = */ &table_ptr, doc, xpathCtx,
1297 current_time, DS_TYPE_GAUGE);
1298 }
1300 if (views_num > 0)
1301 bind_xml_stats_search_views(version, doc, xpathCtx, statsnode,
1302 current_time);
1304 return 0;
1305 } /* }}} int bind_xml_stats */
1307 static int bind_xml(const char *data) /* {{{ */
1308 {
1309 xmlDoc *doc = NULL;
1310 xmlXPathContext *xpathCtx = NULL;
1311 xmlXPathObject *xpathObj = NULL;
1312 int ret = -1;
1314 doc = xmlParseMemory(data, strlen(data));
1315 if (doc == NULL) {
1316 ERROR("bind plugin: xmlParseMemory failed.");
1317 return (-1);
1318 }
1320 xpathCtx = xmlXPathNewContext(doc);
1321 if (xpathCtx == NULL) {
1322 ERROR("bind plugin: xmlXPathNewContext failed.");
1323 xmlFreeDoc(doc);
1324 return (-1);
1325 }
1327 //
1328 // version 3.* of statistics XML (since BIND9.9)
1329 //
1331 xpathObj = xmlXPathEvalExpression(BAD_CAST "/statistics", xpathCtx);
1332 if (xpathObj == NULL || xpathObj->nodesetval == NULL ||
1333 xpathObj->nodesetval->nodeNr == 0) {
1334 DEBUG("bind plugin: Statistics appears not to be v3");
1335 // we will fallback to v1 or v2 detection
1336 if (xpathObj != NULL) {
1337 xmlXPathFreeObject(xpathObj);
1338 }
1339 } else {
1340 for (int i = 0; i < xpathObj->nodesetval->nodeNr; i++) {
1341 xmlNode *node;
1342 char *attr_version;
1344 node = xpathObj->nodesetval->nodeTab[i];
1345 assert(node != NULL);
1347 attr_version = (char *)xmlGetProp(node, BAD_CAST "version");
1348 if (attr_version == NULL) {
1349 NOTICE("bind plugin: Found <statistics> tag doesn't have a "
1350 "`version' attribute.");
1351 continue;
1352 }
1353 DEBUG("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1355 if (strncmp("3.", attr_version, strlen("3.")) != 0) {
1356 /* TODO: Use the complaint mechanism here. */
1357 NOTICE("bind plugin: Found <statistics> tag with version `%s'. "
1358 "Unfortunately I have no clue how to parse that. "
1359 "Please open a bug report for this.",
1360 attr_version);
1361 xmlFree(attr_version);
1362 continue;
1363 }
1364 ret = bind_xml_stats(3, doc, xpathCtx, node);
1366 xmlFree(attr_version);
1367 /* One <statistics> node ought to be enough. */
1368 break;
1369 }
1371 // we are finished, early-return
1372 xmlXPathFreeObject(xpathObj);
1373 xmlXPathFreeContext(xpathCtx);
1374 xmlFreeDoc(doc);
1376 return (ret);
1377 }
1379 //
1380 // versions 1.* or 2.* of statistics XML
1381 //
1383 xpathObj = xmlXPathEvalExpression(BAD_CAST "/isc/bind/statistics", xpathCtx);
1384 if (xpathObj == NULL) {
1385 ERROR("bind plugin: Cannot find the <statistics> tag.");
1386 xmlXPathFreeContext(xpathCtx);
1387 xmlFreeDoc(doc);
1388 return (-1);
1389 } else if (xpathObj->nodesetval == NULL) {
1390 ERROR("bind plugin: xmlXPathEvalExpression failed.");
1391 xmlXPathFreeObject(xpathObj);
1392 xmlXPathFreeContext(xpathCtx);
1393 xmlFreeDoc(doc);
1394 return (-1);
1395 }
1397 for (int i = 0; i < xpathObj->nodesetval->nodeNr; i++) {
1398 xmlNode *node;
1399 char *attr_version;
1400 int parsed_version = 0;
1402 node = xpathObj->nodesetval->nodeTab[i];
1403 assert(node != NULL);
1405 attr_version = (char *)xmlGetProp(node, BAD_CAST "version");
1406 if (attr_version == NULL) {
1407 NOTICE("bind plugin: Found <statistics> tag doesn't have a "
1408 "`version' attribute.");
1409 continue;
1410 }
1411 DEBUG("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1413 /* At the time this plugin was written, version "1.0" was used by
1414 * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
1415 * that "1.*" and "2.*" don't introduce structural changes, so we just
1416 * check for the first two characters here. */
1417 if (strncmp("1.", attr_version, strlen("1.")) == 0)
1418 parsed_version = 1;
1419 else if (strncmp("2.", attr_version, strlen("2.")) == 0)
1420 parsed_version = 2;
1421 else {
1422 /* TODO: Use the complaint mechanism here. */
1423 NOTICE("bind plugin: Found <statistics> tag with version `%s'. "
1424 "Unfortunately I have no clue how to parse that. "
1425 "Please open a bug report for this.",
1426 attr_version);
1427 xmlFree(attr_version);
1428 continue;
1429 }
1431 ret = bind_xml_stats(parsed_version, doc, xpathCtx, node);
1433 xmlFree(attr_version);
1434 /* One <statistics> node ought to be enough. */
1435 break;
1436 }
1438 xmlXPathFreeObject(xpathObj);
1439 xmlXPathFreeContext(xpathCtx);
1440 xmlFreeDoc(doc);
1442 return (ret);
1443 } /* }}} int bind_xml */
1445 static int bind_config_set_bool(const char *name, int *var, /* {{{ */
1446 oconfig_item_t *ci) {
1447 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) {
1448 WARNING("bind plugin: The `%s' option needs "
1449 "exactly one boolean argument.",
1450 name);
1451 return (-1);
1452 }
1454 if (ci->values[0].value.boolean)
1455 *var = 1;
1456 else
1457 *var = 0;
1458 return 0;
1459 } /* }}} int bind_config_set_bool */
1461 static int bind_config_add_view_zone(cb_view_t *view, /* {{{ */
1462 oconfig_item_t *ci) {
1463 char **tmp;
1465 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1466 WARNING("bind plugin: The `Zone' option needs "
1467 "exactly one string argument.");
1468 return (-1);
1469 }
1471 tmp = realloc(view->zones, sizeof(char *) * (view->zones_num + 1));
1472 if (tmp == NULL) {
1473 ERROR("bind plugin: realloc failed.");
1474 return (-1);
1475 }
1476 view->zones = tmp;
1478 view->zones[view->zones_num] = strdup(ci->values[0].value.string);
1479 if (view->zones[view->zones_num] == NULL) {
1480 ERROR("bind plugin: strdup failed.");
1481 return (-1);
1482 }
1483 view->zones_num++;
1485 return (0);
1486 } /* }}} int bind_config_add_view_zone */
1488 static int bind_config_add_view(oconfig_item_t *ci) /* {{{ */
1489 {
1490 cb_view_t *tmp;
1492 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1493 WARNING("bind plugin: `View' blocks need exactly one string argument.");
1494 return (-1);
1495 }
1497 tmp = realloc(views, sizeof(*views) * (views_num + 1));
1498 if (tmp == NULL) {
1499 ERROR("bind plugin: realloc failed.");
1500 return (-1);
1501 }
1502 views = tmp;
1503 tmp = views + views_num;
1505 memset(tmp, 0, sizeof(*tmp));
1506 tmp->qtypes = 1;
1507 tmp->resolver_stats = 1;
1508 tmp->cacherrsets = 1;
1509 tmp->zones = NULL;
1510 tmp->zones_num = 0;
1512 tmp->name = strdup(ci->values[0].value.string);
1513 if (tmp->name == NULL) {
1514 ERROR("bind plugin: strdup failed.");
1515 sfree(views);
1516 return (-1);
1517 }
1519 for (int i = 0; i < ci->children_num; i++) {
1520 oconfig_item_t *child = ci->children + i;
1522 if (strcasecmp("QTypes", child->key) == 0)
1523 bind_config_set_bool("QTypes", &tmp->qtypes, child);
1524 else if (strcasecmp("ResolverStats", child->key) == 0)
1525 bind_config_set_bool("ResolverStats", &tmp->resolver_stats, child);
1526 else if (strcasecmp("CacheRRSets", child->key) == 0)
1527 bind_config_set_bool("CacheRRSets", &tmp->cacherrsets, child);
1528 else if (strcasecmp("Zone", child->key) == 0)
1529 bind_config_add_view_zone(tmp, child);
1530 else {
1531 WARNING("bind plugin: Unknown configuration option "
1532 "`%s' in view `%s' will be ignored.",
1533 child->key, tmp->name);
1534 }
1535 } /* for (i = 0; i < ci->children_num; i++) */
1537 views_num++;
1538 return (0);
1539 } /* }}} int bind_config_add_view */
1541 static int bind_config(oconfig_item_t *ci) /* {{{ */
1542 {
1543 for (int i = 0; i < ci->children_num; i++) {
1544 oconfig_item_t *child = ci->children + i;
1546 if (strcasecmp("Url", child->key) == 0) {
1547 if ((child->values_num != 1) ||
1548 (child->values[0].type != OCONFIG_TYPE_STRING)) {
1549 WARNING("bind plugin: The `Url' option needs "
1550 "exactly one string argument.");
1551 return (-1);
1552 }
1554 sfree(url);
1555 url = strdup(child->values[0].value.string);
1556 } else if (strcasecmp("OpCodes", child->key) == 0)
1557 bind_config_set_bool("OpCodes", &global_opcodes, child);
1558 else if (strcasecmp("QTypes", child->key) == 0)
1559 bind_config_set_bool("QTypes", &global_qtypes, child);
1560 else if (strcasecmp("ServerStats", child->key) == 0)
1561 bind_config_set_bool("ServerStats", &global_server_stats, child);
1562 else if (strcasecmp("ZoneMaintStats", child->key) == 0)
1563 bind_config_set_bool("ZoneMaintStats", &global_zone_maint_stats, child);
1564 else if (strcasecmp("ResolverStats", child->key) == 0)
1565 bind_config_set_bool("ResolverStats", &global_resolver_stats, child);
1566 else if (strcasecmp("MemoryStats", child->key) == 0)
1567 bind_config_set_bool("MemoryStats", &global_memory_stats, child);
1568 else if (strcasecmp("View", child->key) == 0)
1569 bind_config_add_view(child);
1570 else if (strcasecmp("ParseTime", child->key) == 0)
1571 cf_util_get_boolean(child, &config_parse_time);
1572 else if (strcasecmp("Timeout", child->key) == 0)
1573 cf_util_get_int(child, &timeout);
1574 else {
1575 WARNING("bind plugin: Unknown configuration option "
1576 "`%s' will be ignored.",
1577 child->key);
1578 }
1579 }
1581 return (0);
1582 } /* }}} int bind_config */
1584 static int bind_init(void) /* {{{ */
1585 {
1586 if (curl != NULL)
1587 return (0);
1589 curl = curl_easy_init();
1590 if (curl == NULL) {
1591 ERROR("bind plugin: bind_init: curl_easy_init failed.");
1592 return (-1);
1593 }
1595 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
1596 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
1597 curl_easy_setopt(curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
1598 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, bind_curl_error);
1599 curl_easy_setopt(curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL);
1600 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
1601 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
1602 #ifdef HAVE_CURLOPT_TIMEOUT_MS
1603 curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS,
1604 (timeout >= 0) ? (long)timeout : (long)CDTIME_T_TO_MS(
1605 plugin_get_interval()));
1606 #endif
1608 return (0);
1609 } /* }}} int bind_init */
1611 static int bind_read(void) /* {{{ */
1612 {
1613 int status;
1615 if (curl == NULL) {
1616 ERROR("bind plugin: I don't have a CURL object.");
1617 return (-1);
1618 }
1620 bind_buffer_fill = 0;
1621 if (curl_easy_perform(curl) != CURLE_OK) {
1622 ERROR("bind plugin: curl_easy_perform failed: %s", bind_curl_error);
1623 return (-1);
1624 }
1626 status = bind_xml(bind_buffer);
1627 if (status != 0)
1628 return (-1);
1629 else
1630 return (0);
1631 } /* }}} int bind_read */
1633 static int bind_shutdown(void) /* {{{ */
1634 {
1635 if (curl != NULL) {
1636 curl_easy_cleanup(curl);
1637 curl = NULL;
1638 }
1640 return (0);
1641 } /* }}} int bind_shutdown */
1643 void module_register(void) {
1644 plugin_register_complex_config("bind", bind_config);
1645 plugin_register_init("bind", bind_init);
1646 plugin_register_read("bind", bind_read);
1647 plugin_register_shutdown("bind", bind_shutdown);
1648 } /* void module_register */
1650 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */