fc1fd92f0385905638e2f750d882b930cdc55b10
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 xmlFree(name);
531 continue;
532 }
534 status = (*list_callback)(name, value, current_time, user_data);
535 if (status == 0)
536 num_entries++;
538 xmlFree(name);
539 }
540 }
542 DEBUG("bind plugin: Found %d %s for XPath expression `%s'", num_entries,
543 (num_entries == 1) ? "entry" : "entries", xpath_expression);
545 xmlXPathFreeObject(xpathObj);
547 return (0);
548 } /* }}} int bind_parse_generic_name_value */
550 /*
551 * bind_parse_generic_value_list
552 *
553 * Reads statistics in the form:
554 * <foo>
555 * <name0>123</name0>
556 * <name1>234</name1>
557 * <name2>345</name2>
558 * :
559 * </foo>
560 */
561 static int bind_parse_generic_value_list(const char *xpath_expression, /* {{{ */
562 list_callback_t list_callback,
563 void *user_data, xmlDoc *doc,
564 xmlXPathContext *xpathCtx,
565 time_t current_time, int ds_type) {
566 xmlXPathObject *xpathObj = NULL;
567 int num_entries;
569 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
570 if (xpathObj == NULL) {
571 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
572 xpath_expression);
573 return (-1);
574 }
576 num_entries = 0;
577 /* Iterate over all matching nodes. */
578 for (int i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr);
579 i++) {
580 /* Iterate over all child nodes. */
581 for (xmlNode *child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
582 child != NULL; child = child->next) {
583 char *node_name;
584 value_t value;
585 int status;
587 if (child->type != XML_ELEMENT_NODE)
588 continue;
590 node_name = (char *)child->name;
592 if (ds_type == DS_TYPE_GAUGE)
593 status = bind_xml_read_gauge(doc, child, &value.gauge);
594 else
595 status = bind_xml_read_derive(doc, child, &value.derive);
596 if (status != 0)
597 continue;
599 status = (*list_callback)(node_name, value, current_time, user_data);
600 if (status == 0)
601 num_entries++;
602 }
603 }
605 DEBUG("bind plugin: Found %d %s for XPath expression `%s'", num_entries,
606 (num_entries == 1) ? "entry" : "entries", xpath_expression);
608 xmlXPathFreeObject(xpathObj);
610 return (0);
611 } /* }}} int bind_parse_generic_value_list */
613 /*
614 * bind_parse_generic_name_attr_value_list
615 *
616 * Reads statistics in the form:
617 * <foo>
618 * <counter name="name0">123</counter>
619 * <counter name="name1">234</counter>
620 * <counter name="name2">345</counter>
621 * :
622 * </foo>
623 */
624 static int bind_parse_generic_name_attr_value_list(
625 const char *xpath_expression, /* {{{ */
626 list_callback_t list_callback, void *user_data, xmlDoc *doc,
627 xmlXPathContext *xpathCtx, time_t current_time, int ds_type) {
628 xmlXPathObject *xpathObj = NULL;
629 int num_entries;
631 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
632 if (xpathObj == NULL) {
633 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
634 xpath_expression);
635 return (-1);
636 }
638 num_entries = 0;
639 /* Iterate over all matching nodes. */
640 for (int i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr);
641 i++) {
642 /* Iterate over all child nodes. */
643 for (xmlNode *child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
644 child != NULL; child = child->next) {
645 if (child->type != XML_ELEMENT_NODE)
646 continue;
648 if (strncmp("counter", (char *)child->name, strlen("counter")) != 0)
649 continue;
651 char *attr_name;
652 value_t value;
653 int status;
655 attr_name = (char *)xmlGetProp(child, BAD_CAST "name");
656 if (attr_name == NULL) {
657 DEBUG("bind plugin: found <counter> without name.");
658 continue;
659 }
660 if (ds_type == DS_TYPE_GAUGE)
661 status = bind_xml_read_gauge(doc, child, &value.gauge);
662 else
663 status = bind_xml_read_derive(doc, child, &value.derive);
664 if (status != 0) {
665 xmlFree(attr_name);
666 continue;
667 }
669 status = (*list_callback)(attr_name, value, current_time, user_data);
670 if (status == 0)
671 num_entries++;
673 xmlFree(attr_name);
674 }
675 }
677 DEBUG("bind plugin: Found %d %s for XPath expression `%s'", num_entries,
678 (num_entries == 1) ? "entry" : "entries", xpath_expression);
680 xmlXPathFreeObject(xpathObj);
682 return (0);
683 } /* }}} int bind_parse_generic_name_attr_value_list */
685 static int bind_xml_stats_handle_zone(int version, xmlDoc *doc, /* {{{ */
686 xmlXPathContext *path_ctx, xmlNode *node,
687 cb_view_t *view, time_t current_time) {
688 xmlXPathObject *path_obj;
689 char *zone_name = NULL;
690 size_t j;
692 if (version >= 3) {
693 char *n = (char *)xmlGetProp(node, BAD_CAST "name");
694 char *c = (char *)xmlGetProp(node, BAD_CAST "rdataclass");
695 if (n && c) {
696 zone_name = (char *)xmlMalloc(strlen(n) + strlen(c) + 2);
697 snprintf(zone_name, strlen(n) + strlen(c) + 2, "%s/%s", n, c);
698 }
699 xmlFree(n);
700 xmlFree(c);
701 } else {
702 path_obj = xmlXPathEvalExpression(BAD_CAST "name", path_ctx);
703 if (path_obj == NULL) {
704 ERROR("bind plugin: xmlXPathEvalExpression failed.");
705 return (-1);
706 }
708 for (int i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr);
709 i++) {
710 zone_name = (char *)xmlNodeListGetString(
711 doc, path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
712 if (zone_name != NULL)
713 break;
714 }
715 xmlXPathFreeObject(path_obj);
716 }
718 if (zone_name == NULL) {
719 ERROR("bind plugin: Could not determine zone name.");
720 return (-1);
721 }
723 for (j = 0; j < view->zones_num; j++) {
724 if (strcasecmp(zone_name, view->zones[j]) == 0)
725 break;
726 }
728 xmlFree(zone_name);
729 zone_name = NULL;
731 if (j >= view->zones_num)
732 return (0);
734 zone_name = view->zones[j];
736 DEBUG("bind plugin: bind_xml_stats_handle_zone: Found zone `%s'.", zone_name);
738 { /* Parse the <counters> tag {{{ */
739 char plugin_instance[DATA_MAX_NAME_LEN];
740 translation_table_ptr_t table_ptr = {nsstats_translation_table,
741 nsstats_translation_table_length,
742 plugin_instance};
744 ssnprintf(plugin_instance, sizeof(plugin_instance), "%s-zone-%s",
745 view->name, zone_name);
747 if (version == 3) {
748 list_info_ptr_t list_info = {plugin_instance,
749 /* type = */ "dns_qtype"};
750 bind_parse_generic_name_attr_value_list(
751 /* xpath = */ "counters[@type='rcode']",
752 /* callback = */ bind_xml_table_callback,
753 /* user_data = */ &table_ptr, doc, path_ctx, current_time,
754 DS_TYPE_COUNTER);
755 bind_parse_generic_name_attr_value_list(
756 /* xpath = */ "counters[@type='qtype']",
757 /* callback = */ bind_xml_list_callback,
758 /* user_data = */ &list_info, doc, path_ctx, current_time,
759 DS_TYPE_COUNTER);
760 } else {
761 bind_parse_generic_value_list(/* xpath = */ "counters",
762 /* callback = */ bind_xml_table_callback,
763 /* user_data = */ &table_ptr, doc, path_ctx,
764 current_time, DS_TYPE_COUNTER);
765 }
766 } /* }}} */
768 return (0);
769 } /* }}} int bind_xml_stats_handle_zone */
771 static int bind_xml_stats_search_zones(int version, xmlDoc *doc, /* {{{ */
772 xmlXPathContext *path_ctx, xmlNode *node,
773 cb_view_t *view, time_t current_time) {
774 xmlXPathObject *zone_nodes = NULL;
775 xmlXPathContext *zone_path_context;
777 zone_path_context = xmlXPathNewContext(doc);
778 if (zone_path_context == NULL) {
779 ERROR("bind plugin: xmlXPathNewContext failed.");
780 return (-1);
781 }
783 zone_nodes = xmlXPathEvalExpression(BAD_CAST "zones/zone", path_ctx);
784 if (zone_nodes == NULL) {
785 ERROR("bind plugin: Cannot find any <view> tags.");
786 xmlXPathFreeContext(zone_path_context);
787 return (-1);
788 }
790 for (int i = 0; i < zone_nodes->nodesetval->nodeNr; i++) {
791 node = zone_nodes->nodesetval->nodeTab[i];
792 assert(node != NULL);
794 zone_path_context->node = node;
796 bind_xml_stats_handle_zone(version, doc, zone_path_context, node, view,
797 current_time);
798 }
800 xmlXPathFreeObject(zone_nodes);
801 xmlXPathFreeContext(zone_path_context);
802 return (0);
803 } /* }}} int bind_xml_stats_search_zones */
805 static int bind_xml_stats_handle_view(int version, xmlDoc *doc, /* {{{ */
806 xmlXPathContext *path_ctx, xmlNode *node,
807 time_t current_time) {
808 char *view_name = NULL;
809 cb_view_t *view;
810 size_t j;
812 if (version == 3) {
813 view_name = (char *)xmlGetProp(node, BAD_CAST "name");
815 if (view_name == NULL) {
816 ERROR("bind plugin: Could not determine view name.");
817 return (-1);
818 }
820 for (j = 0; j < views_num; j++) {
821 if (strcasecmp(view_name, views[j].name) == 0)
822 break;
823 }
825 xmlFree(view_name);
826 view_name = NULL;
827 } else {
828 xmlXPathObject *path_obj;
829 path_obj = xmlXPathEvalExpression(BAD_CAST "name", path_ctx);
830 if (path_obj == NULL) {
831 ERROR("bind plugin: xmlXPathEvalExpression failed.");
832 return (-1);
833 }
835 for (int i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr);
836 i++) {
837 view_name = (char *)xmlNodeListGetString(
838 doc, path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
839 if (view_name != NULL)
840 break;
841 }
843 if (view_name == NULL) {
844 ERROR("bind plugin: Could not determine view name.");
845 xmlXPathFreeObject(path_obj);
846 return (-1);
847 }
849 for (j = 0; j < views_num; j++) {
850 if (strcasecmp(view_name, views[j].name) == 0)
851 break;
852 }
854 xmlFree(view_name);
855 xmlXPathFreeObject(path_obj);
857 view_name = NULL;
858 path_obj = NULL;
859 }
861 if (j >= views_num)
862 return (0);
864 view = views + j;
866 DEBUG("bind plugin: bind_xml_stats_handle_view: Found view `%s'.",
867 view->name);
869 if (view->qtypes != 0) /* {{{ */
870 {
871 char plugin_instance[DATA_MAX_NAME_LEN];
872 list_info_ptr_t list_info = {plugin_instance,
873 /* type = */ "dns_qtype"};
875 ssnprintf(plugin_instance, sizeof(plugin_instance), "%s-qtypes",
876 view->name);
877 if (version == 3) {
878 bind_parse_generic_name_attr_value_list(
879 /* xpath = */ "counters[@type='resqtype']",
880 /* callback = */ bind_xml_list_callback,
881 /* user_data = */ &list_info, doc, path_ctx, current_time,
882 DS_TYPE_COUNTER);
883 } else {
884 bind_parse_generic_name_value(/* xpath = */ "rdtype",
885 /* callback = */ bind_xml_list_callback,
886 /* user_data = */ &list_info, doc, path_ctx,
887 current_time, DS_TYPE_COUNTER);
888 }
889 } /* }}} */
891 if (view->resolver_stats != 0) /* {{{ */
892 {
893 char plugin_instance[DATA_MAX_NAME_LEN];
894 translation_table_ptr_t table_ptr = {resstats_translation_table,
895 resstats_translation_table_length,
896 plugin_instance};
898 ssnprintf(plugin_instance, sizeof(plugin_instance), "%s-resolver_stats",
899 view->name);
900 if (version == 3) {
901 bind_parse_generic_name_attr_value_list(
902 "counters[@type='resstats']",
903 /* callback = */ bind_xml_table_callback,
904 /* user_data = */ &table_ptr, doc, path_ctx, current_time,
905 DS_TYPE_COUNTER);
906 } else {
907 bind_parse_generic_name_value("resstat",
908 /* callback = */ bind_xml_table_callback,
909 /* user_data = */ &table_ptr, doc, path_ctx,
910 current_time, DS_TYPE_COUNTER);
911 }
912 } /* }}} */
914 /* Record types in the cache */
915 if (view->cacherrsets != 0) /* {{{ */
916 {
917 char plugin_instance[DATA_MAX_NAME_LEN];
918 list_info_ptr_t list_info = {plugin_instance,
919 /* type = */ "dns_qtype_cached"};
921 ssnprintf(plugin_instance, sizeof(plugin_instance), "%s-cache_rr_sets",
922 view->name);
924 bind_parse_generic_name_value(/* xpath = */ "cache/rrset",
925 /* callback = */ bind_xml_list_callback,
926 /* user_data = */ &list_info, doc, path_ctx,
927 current_time, DS_TYPE_GAUGE);
928 } /* }}} */
930 if (view->zones_num > 0)
931 bind_xml_stats_search_zones(version, doc, path_ctx, node, view,
932 current_time);
934 return (0);
935 } /* }}} int bind_xml_stats_handle_view */
937 static int bind_xml_stats_search_views(int version, xmlDoc *doc, /* {{{ */
938 xmlXPathContext *xpathCtx,
939 xmlNode *statsnode,
940 time_t current_time) {
941 xmlXPathObject *view_nodes = NULL;
942 xmlXPathContext *view_path_context;
944 view_path_context = xmlXPathNewContext(doc);
945 if (view_path_context == NULL) {
946 ERROR("bind plugin: xmlXPathNewContext failed.");
947 return (-1);
948 }
950 view_nodes = xmlXPathEvalExpression(BAD_CAST "views/view", xpathCtx);
951 if (view_nodes == NULL) {
952 ERROR("bind plugin: Cannot find any <view> tags.");
953 xmlXPathFreeContext(view_path_context);
954 return (-1);
955 }
957 for (int i = 0; i < view_nodes->nodesetval->nodeNr; i++) {
958 xmlNode *node;
960 node = view_nodes->nodesetval->nodeTab[i];
961 assert(node != NULL);
963 view_path_context->node = node;
965 bind_xml_stats_handle_view(version, doc, view_path_context, node,
966 current_time);
967 }
969 xmlXPathFreeObject(view_nodes);
970 xmlXPathFreeContext(view_path_context);
971 return (0);
972 } /* }}} int bind_xml_stats_search_views */
974 static void bind_xml_stats_v3(xmlDoc *doc, /* {{{ */
975 xmlXPathContext *xpathCtx, xmlNode *statsnode,
976 time_t current_time) {
977 /* XPath: server/counters[@type='opcode']
978 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
979 * Layout v3:
980 * <counters type="opcode">
981 * <counter name="A">1</counter>
982 * :
983 * </counters>
984 */
985 if (global_opcodes != 0) {
986 list_info_ptr_t list_info = {/* plugin instance = */ "global-opcodes",
987 /* type = */ "dns_opcode"};
988 bind_parse_generic_name_attr_value_list(
989 /* xpath = */ "server/counters[@type='opcode']",
990 /* callback = */ bind_xml_list_callback,
991 /* user_data = */ &list_info, doc, xpathCtx, current_time,
992 DS_TYPE_COUNTER);
993 }
995 /* XPath: server/counters[@type='qtype']
996 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
997 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
998 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
999 * Layout v3:
1000 * <counters type="opcode">
1001 * <counter name="A">1</counter>
1002 * :
1003 * </counters>
1004 */
1005 if (global_qtypes != 0) {
1006 list_info_ptr_t list_info = {/* plugin instance = */ "global-qtypes",
1007 /* type = */ "dns_qtype"};
1009 bind_parse_generic_name_attr_value_list(
1010 /* xpath = */ "server/counters[@type='qtype']",
1011 /* callback = */ bind_xml_list_callback,
1012 /* user_data = */ &list_info, doc, xpathCtx, current_time,
1013 DS_TYPE_COUNTER);
1014 }
1016 /* XPath: server/counters[@type='nsstat']
1017 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
1018 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
1019 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
1020 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
1021 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
1022 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
1023 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
1024 * UpdateBadPrereq
1025 * Layout v3:
1026 * <counters type="nsstat"
1027 * <counter name="Requestv4">1</counter>
1028 * <counter name="Requestv6">0</counter>
1029 * :
1030 * </counter>
1031 */
1032 if (global_server_stats) {
1033 translation_table_ptr_t table_ptr = {
1034 nsstats_translation_table, nsstats_translation_table_length,
1035 /* plugin_instance = */ "global-server_stats"};
1037 bind_parse_generic_name_attr_value_list(
1038 "server/counters[@type='nsstat']",
1039 /* callback = */ bind_xml_table_callback,
1040 /* user_data = */ &table_ptr, doc, xpathCtx, current_time,
1041 DS_TYPE_COUNTER);
1042 }
1044 /* XPath: server/zonestats, server/zonestat,
1045 * server/counters[@type='zonestat']
1046 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1047 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1048 * XfrSuccess, XfrFail
1049 * Layout v3:
1050 * <counters type="zonestat"
1051 * <counter name="NotifyOutv4">0</counter>
1052 * <counter name="NotifyOutv6">0</counter>
1053 * :
1054 * </counter>
1055 */
1056 if (global_zone_maint_stats) {
1057 translation_table_ptr_t table_ptr = {
1058 zonestats_translation_table, zonestats_translation_table_length,
1059 /* plugin_instance = */ "global-zone_maint_stats"};
1061 bind_parse_generic_name_attr_value_list(
1062 "server/counters[@type='zonestat']",
1063 /* callback = */ bind_xml_table_callback,
1064 /* user_data = */ &table_ptr, doc, xpathCtx, current_time,
1065 DS_TYPE_COUNTER);
1066 }
1068 /* XPath: server/resstats, server/counters[@type='resstat']
1069 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1070 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1071 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1072 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1073 * Layout v3:
1074 * <counters type="resstat"
1075 * <counter name="Queryv4">0</counter>
1076 * <counter name="Queryv6">0</counter>
1077 * :
1078 * </counter>
1079 */
1080 if (global_resolver_stats != 0) {
1081 translation_table_ptr_t table_ptr = {
1082 resstats_translation_table, resstats_translation_table_length,
1083 /* plugin_instance = */ "global-resolver_stats"};
1085 bind_parse_generic_name_attr_value_list(
1086 "server/counters[@type='resstat']",
1087 /* callback = */ bind_xml_table_callback,
1088 /* user_data = */ &table_ptr, doc, xpathCtx, current_time,
1089 DS_TYPE_COUNTER);
1090 }
1091 } /* }}} bind_xml_stats_v3 */
1093 static void bind_xml_stats_v1_v2(int version, xmlDoc *doc, /* {{{ */
1094 xmlXPathContext *xpathCtx, xmlNode *statsnode,
1095 time_t current_time) {
1096 /* XPath: server/requests/opcode, server/counters[@type='opcode']
1097 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
1098 * Layout V1 and V2:
1099 * <opcode>
1100 * <name>A</name>
1101 * <counter>1</counter>
1102 * </opcode>
1103 * :
1104 */
1105 if (global_opcodes != 0) {
1106 list_info_ptr_t list_info = {/* plugin instance = */ "global-opcodes",
1107 /* type = */ "dns_opcode"};
1109 bind_parse_generic_name_value(/* xpath = */ "server/requests/opcode",
1110 /* callback = */ bind_xml_list_callback,
1111 /* user_data = */ &list_info, doc, xpathCtx,
1112 current_time, DS_TYPE_COUNTER);
1113 }
1115 /* XPath: server/queries-in/rdtype, server/counters[@type='qtype']
1116 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
1117 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
1118 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
1119 * Layout v1 or v2:
1120 * <rdtype>
1121 * <name>A</name>
1122 * <counter>1</counter>
1123 * </rdtype>
1124 * :
1125 */
1126 if (global_qtypes != 0) {
1127 list_info_ptr_t list_info = {/* plugin instance = */ "global-qtypes",
1128 /* type = */ "dns_qtype"};
1130 bind_parse_generic_name_value(/* xpath = */ "server/queries-in/rdtype",
1131 /* callback = */ bind_xml_list_callback,
1132 /* user_data = */ &list_info, doc, xpathCtx,
1133 current_time, DS_TYPE_COUNTER);
1134 }
1136 /* XPath: server/nsstats, server/nsstat, server/counters[@type='nsstat']
1137 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
1138 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
1139 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
1140 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
1141 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
1142 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
1143 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
1144 * UpdateBadPrereq
1145 * Layout v1:
1146 * <nsstats>
1147 * <Requestv4>1</Requestv4>
1148 * <Requestv6>0</Requestv6>
1149 * :
1150 * </nsstats>
1151 * Layout v2:
1152 * <nsstat>
1153 * <name>Requestv4</name>
1154 * <counter>1</counter>
1155 * </nsstat>
1156 * <nsstat>
1157 * <name>Requestv6</name>
1158 * <counter>0</counter>
1159 * </nsstat>
1160 * :
1161 */
1162 if (global_server_stats) {
1163 translation_table_ptr_t table_ptr = {
1164 nsstats_translation_table, nsstats_translation_table_length,
1165 /* plugin_instance = */ "global-server_stats"};
1167 if (version == 1) {
1168 bind_parse_generic_value_list("server/nsstats",
1169 /* callback = */ bind_xml_table_callback,
1170 /* user_data = */ &table_ptr, doc, xpathCtx,
1171 current_time, DS_TYPE_COUNTER);
1172 } else {
1173 bind_parse_generic_name_value("server/nsstat",
1174 /* callback = */ bind_xml_table_callback,
1175 /* user_data = */ &table_ptr, doc, xpathCtx,
1176 current_time, DS_TYPE_COUNTER);
1177 }
1178 }
1180 /* XPath: server/zonestats, server/zonestat,
1181 * server/counters[@type='zonestat']
1182 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1183 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1184 * XfrSuccess, XfrFail
1185 * Layout v1:
1186 * <zonestats>
1187 * <NotifyOutv4>0</NotifyOutv4>
1188 * <NotifyOutv6>0</NotifyOutv6>
1189 * :
1190 * </zonestats>
1191 * Layout v2:
1192 * <zonestat>
1193 * <name>NotifyOutv4</name>
1194 * <counter>0</counter>
1195 * </zonestat>
1196 * <zonestat>
1197 * <name>NotifyOutv6</name>
1198 * <counter>0</counter>
1199 * </zonestat>
1200 * :
1201 */
1202 if (global_zone_maint_stats) {
1203 translation_table_ptr_t table_ptr = {
1204 zonestats_translation_table, zonestats_translation_table_length,
1205 /* plugin_instance = */ "global-zone_maint_stats"};
1207 if (version == 1) {
1208 bind_parse_generic_value_list("server/zonestats",
1209 /* callback = */ bind_xml_table_callback,
1210 /* user_data = */ &table_ptr, doc, xpathCtx,
1211 current_time, DS_TYPE_COUNTER);
1212 } else {
1213 bind_parse_generic_name_value("server/zonestat",
1214 /* callback = */ bind_xml_table_callback,
1215 /* user_data = */ &table_ptr, doc, xpathCtx,
1216 current_time, DS_TYPE_COUNTER);
1217 }
1218 }
1220 /* XPath: server/resstats, server/counters[@type='resstat']
1221 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1222 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1223 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1224 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1225 * Layout v1:
1226 * <resstats>
1227 * <Queryv4>0</Queryv4>
1228 * <Queryv6>0</Queryv6>
1229 * :
1230 * </resstats>
1231 * Layout v2:
1232 * <resstat>
1233 * <name>Queryv4</name>
1234 * <counter>0</counter>
1235 * </resstat>
1236 * <resstat>
1237 * <name>Queryv6</name>
1238 * <counter>0</counter>
1239 * </resstat>
1240 * :
1241 */
1242 if (global_resolver_stats != 0) {
1243 translation_table_ptr_t table_ptr = {
1244 resstats_translation_table, resstats_translation_table_length,
1245 /* plugin_instance = */ "global-resolver_stats"};
1247 if (version == 1) {
1248 bind_parse_generic_value_list("server/resstats",
1249 /* callback = */ bind_xml_table_callback,
1250 /* user_data = */ &table_ptr, doc, xpathCtx,
1251 current_time, DS_TYPE_COUNTER);
1252 } else {
1253 bind_parse_generic_name_value("server/resstat",
1254 /* callback = */ bind_xml_table_callback,
1255 /* user_data = */ &table_ptr, doc, xpathCtx,
1256 current_time, DS_TYPE_COUNTER);
1257 }
1258 }
1259 } /* }}} bind_xml_stats_v1_v2 */
1261 static int bind_xml_stats(int version, xmlDoc *doc, /* {{{ */
1262 xmlXPathContext *xpathCtx, xmlNode *statsnode) {
1263 time_t current_time = 0;
1264 int status;
1266 xpathCtx->node = statsnode;
1268 /* TODO: Check `server/boot-time' to recognize server restarts. */
1270 status = bind_xml_read_timestamp("server/current-time", doc, xpathCtx,
1271 ¤t_time);
1272 if (status != 0) {
1273 ERROR("bind plugin: Reading `server/current-time' failed.");
1274 return (-1);
1275 }
1276 DEBUG("bind plugin: Current server time is %i.", (int)current_time);
1278 if (version == 3) {
1279 bind_xml_stats_v3(doc, xpathCtx, statsnode, current_time);
1280 } else {
1281 bind_xml_stats_v1_v2(version, doc, xpathCtx, statsnode, current_time);
1282 }
1284 /* XPath: memory/summary
1285 * Variables: TotalUse, InUse, BlockSize, ContextSize, Lost
1286 * Layout: v2 and v3:
1287 * <summary>
1288 * <TotalUse>6587096</TotalUse>
1289 * <InUse>1345424</InUse>
1290 * <BlockSize>5505024</BlockSize>
1291 * <ContextSize>3732456</ContextSize>
1292 * <Lost>0</Lost>
1293 * </summary>
1294 */
1295 if (global_memory_stats != 0) {
1296 translation_table_ptr_t table_ptr = {
1297 memsummary_translation_table, memsummary_translation_table_length,
1298 /* plugin_instance = */ "global-memory_stats"};
1300 bind_parse_generic_value_list("memory/summary",
1301 /* callback = */ bind_xml_table_callback,
1302 /* user_data = */ &table_ptr, doc, xpathCtx,
1303 current_time, DS_TYPE_GAUGE);
1304 }
1306 if (views_num > 0)
1307 bind_xml_stats_search_views(version, doc, xpathCtx, statsnode,
1308 current_time);
1310 return 0;
1311 } /* }}} int bind_xml_stats */
1313 static int bind_xml(const char *data) /* {{{ */
1314 {
1315 xmlDoc *doc = NULL;
1316 xmlXPathContext *xpathCtx = NULL;
1317 xmlXPathObject *xpathObj = NULL;
1318 int ret = -1;
1320 doc = xmlParseMemory(data, strlen(data));
1321 if (doc == NULL) {
1322 ERROR("bind plugin: xmlParseMemory failed.");
1323 return (-1);
1324 }
1326 xpathCtx = xmlXPathNewContext(doc);
1327 if (xpathCtx == NULL) {
1328 ERROR("bind plugin: xmlXPathNewContext failed.");
1329 xmlFreeDoc(doc);
1330 return (-1);
1331 }
1333 //
1334 // version 3.* of statistics XML (since BIND9.9)
1335 //
1337 xpathObj = xmlXPathEvalExpression(BAD_CAST "/statistics", xpathCtx);
1338 if (xpathObj == NULL || xpathObj->nodesetval == NULL ||
1339 xpathObj->nodesetval->nodeNr == 0) {
1340 DEBUG("bind plugin: Statistics appears not to be v3");
1341 // we will fallback to v1 or v2 detection
1342 if (xpathObj != NULL) {
1343 xmlXPathFreeObject(xpathObj);
1344 }
1345 } else {
1346 for (int i = 0; i < xpathObj->nodesetval->nodeNr; i++) {
1347 xmlNode *node;
1348 char *attr_version;
1350 node = xpathObj->nodesetval->nodeTab[i];
1351 assert(node != NULL);
1353 attr_version = (char *)xmlGetProp(node, BAD_CAST "version");
1354 if (attr_version == NULL) {
1355 NOTICE("bind plugin: Found <statistics> tag doesn't have a "
1356 "`version' attribute.");
1357 continue;
1358 }
1359 DEBUG("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1361 if (strncmp("3.", attr_version, strlen("3.")) != 0) {
1362 /* TODO: Use the complaint mechanism here. */
1363 NOTICE("bind plugin: Found <statistics> tag with version `%s'. "
1364 "Unfortunately I have no clue how to parse that. "
1365 "Please open a bug report for this.",
1366 attr_version);
1367 xmlFree(attr_version);
1368 continue;
1369 }
1370 ret = bind_xml_stats(3, doc, xpathCtx, node);
1372 xmlFree(attr_version);
1373 /* One <statistics> node ought to be enough. */
1374 break;
1375 }
1377 // we are finished, early-return
1378 xmlXPathFreeObject(xpathObj);
1379 xmlXPathFreeContext(xpathCtx);
1380 xmlFreeDoc(doc);
1382 return (ret);
1383 }
1385 //
1386 // versions 1.* or 2.* of statistics XML
1387 //
1389 xpathObj = xmlXPathEvalExpression(BAD_CAST "/isc/bind/statistics", xpathCtx);
1390 if (xpathObj == NULL) {
1391 ERROR("bind plugin: Cannot find the <statistics> tag.");
1392 xmlXPathFreeContext(xpathCtx);
1393 xmlFreeDoc(doc);
1394 return (-1);
1395 } else if (xpathObj->nodesetval == NULL) {
1396 ERROR("bind plugin: xmlXPathEvalExpression failed.");
1397 xmlXPathFreeObject(xpathObj);
1398 xmlXPathFreeContext(xpathCtx);
1399 xmlFreeDoc(doc);
1400 return (-1);
1401 }
1403 for (int i = 0; i < xpathObj->nodesetval->nodeNr; i++) {
1404 xmlNode *node;
1405 char *attr_version;
1406 int parsed_version = 0;
1408 node = xpathObj->nodesetval->nodeTab[i];
1409 assert(node != NULL);
1411 attr_version = (char *)xmlGetProp(node, BAD_CAST "version");
1412 if (attr_version == NULL) {
1413 NOTICE("bind plugin: Found <statistics> tag doesn't have a "
1414 "`version' attribute.");
1415 continue;
1416 }
1417 DEBUG("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1419 /* At the time this plugin was written, version "1.0" was used by
1420 * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
1421 * that "1.*" and "2.*" don't introduce structural changes, so we just
1422 * check for the first two characters here. */
1423 if (strncmp("1.", attr_version, strlen("1.")) == 0)
1424 parsed_version = 1;
1425 else if (strncmp("2.", attr_version, strlen("2.")) == 0)
1426 parsed_version = 2;
1427 else {
1428 /* TODO: Use the complaint mechanism here. */
1429 NOTICE("bind plugin: Found <statistics> tag with version `%s'. "
1430 "Unfortunately I have no clue how to parse that. "
1431 "Please open a bug report for this.",
1432 attr_version);
1433 xmlFree(attr_version);
1434 continue;
1435 }
1437 ret = bind_xml_stats(parsed_version, doc, xpathCtx, node);
1439 xmlFree(attr_version);
1440 /* One <statistics> node ought to be enough. */
1441 break;
1442 }
1444 xmlXPathFreeObject(xpathObj);
1445 xmlXPathFreeContext(xpathCtx);
1446 xmlFreeDoc(doc);
1448 return (ret);
1449 } /* }}} int bind_xml */
1451 static int bind_config_set_bool(const char *name, int *var, /* {{{ */
1452 oconfig_item_t *ci) {
1453 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) {
1454 WARNING("bind plugin: The `%s' option needs "
1455 "exactly one boolean argument.",
1456 name);
1457 return (-1);
1458 }
1460 if (ci->values[0].value.boolean)
1461 *var = 1;
1462 else
1463 *var = 0;
1464 return 0;
1465 } /* }}} int bind_config_set_bool */
1467 static int bind_config_add_view_zone(cb_view_t *view, /* {{{ */
1468 oconfig_item_t *ci) {
1469 char **tmp;
1471 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1472 WARNING("bind plugin: The `Zone' option needs "
1473 "exactly one string argument.");
1474 return (-1);
1475 }
1477 tmp = realloc(view->zones, sizeof(char *) * (view->zones_num + 1));
1478 if (tmp == NULL) {
1479 ERROR("bind plugin: realloc failed.");
1480 return (-1);
1481 }
1482 view->zones = tmp;
1484 view->zones[view->zones_num] = strdup(ci->values[0].value.string);
1485 if (view->zones[view->zones_num] == NULL) {
1486 ERROR("bind plugin: strdup failed.");
1487 return (-1);
1488 }
1489 view->zones_num++;
1491 return (0);
1492 } /* }}} int bind_config_add_view_zone */
1494 static int bind_config_add_view(oconfig_item_t *ci) /* {{{ */
1495 {
1496 cb_view_t *tmp;
1498 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1499 WARNING("bind plugin: `View' blocks need exactly one string argument.");
1500 return (-1);
1501 }
1503 tmp = realloc(views, sizeof(*views) * (views_num + 1));
1504 if (tmp == NULL) {
1505 ERROR("bind plugin: realloc failed.");
1506 return (-1);
1507 }
1508 views = tmp;
1509 tmp = views + views_num;
1511 memset(tmp, 0, sizeof(*tmp));
1512 tmp->qtypes = 1;
1513 tmp->resolver_stats = 1;
1514 tmp->cacherrsets = 1;
1515 tmp->zones = NULL;
1516 tmp->zones_num = 0;
1518 tmp->name = strdup(ci->values[0].value.string);
1519 if (tmp->name == NULL) {
1520 ERROR("bind plugin: strdup failed.");
1521 sfree(views);
1522 return (-1);
1523 }
1525 for (int i = 0; i < ci->children_num; i++) {
1526 oconfig_item_t *child = ci->children + i;
1528 if (strcasecmp("QTypes", child->key) == 0)
1529 bind_config_set_bool("QTypes", &tmp->qtypes, child);
1530 else if (strcasecmp("ResolverStats", child->key) == 0)
1531 bind_config_set_bool("ResolverStats", &tmp->resolver_stats, child);
1532 else if (strcasecmp("CacheRRSets", child->key) == 0)
1533 bind_config_set_bool("CacheRRSets", &tmp->cacherrsets, child);
1534 else if (strcasecmp("Zone", child->key) == 0)
1535 bind_config_add_view_zone(tmp, child);
1536 else {
1537 WARNING("bind plugin: Unknown configuration option "
1538 "`%s' in view `%s' will be ignored.",
1539 child->key, tmp->name);
1540 }
1541 } /* for (i = 0; i < ci->children_num; i++) */
1543 views_num++;
1544 return (0);
1545 } /* }}} int bind_config_add_view */
1547 static int bind_config(oconfig_item_t *ci) /* {{{ */
1548 {
1549 for (int i = 0; i < ci->children_num; i++) {
1550 oconfig_item_t *child = ci->children + i;
1552 if (strcasecmp("Url", child->key) == 0) {
1553 if ((child->values_num != 1) ||
1554 (child->values[0].type != OCONFIG_TYPE_STRING)) {
1555 WARNING("bind plugin: The `Url' option needs "
1556 "exactly one string argument.");
1557 return (-1);
1558 }
1560 sfree(url);
1561 url = strdup(child->values[0].value.string);
1562 } else if (strcasecmp("OpCodes", child->key) == 0)
1563 bind_config_set_bool("OpCodes", &global_opcodes, child);
1564 else if (strcasecmp("QTypes", child->key) == 0)
1565 bind_config_set_bool("QTypes", &global_qtypes, child);
1566 else if (strcasecmp("ServerStats", child->key) == 0)
1567 bind_config_set_bool("ServerStats", &global_server_stats, child);
1568 else if (strcasecmp("ZoneMaintStats", child->key) == 0)
1569 bind_config_set_bool("ZoneMaintStats", &global_zone_maint_stats, child);
1570 else if (strcasecmp("ResolverStats", child->key) == 0)
1571 bind_config_set_bool("ResolverStats", &global_resolver_stats, child);
1572 else if (strcasecmp("MemoryStats", child->key) == 0)
1573 bind_config_set_bool("MemoryStats", &global_memory_stats, child);
1574 else if (strcasecmp("View", child->key) == 0)
1575 bind_config_add_view(child);
1576 else if (strcasecmp("ParseTime", child->key) == 0)
1577 cf_util_get_boolean(child, &config_parse_time);
1578 else if (strcasecmp("Timeout", child->key) == 0)
1579 cf_util_get_int(child, &timeout);
1580 else {
1581 WARNING("bind plugin: Unknown configuration option "
1582 "`%s' will be ignored.",
1583 child->key);
1584 }
1585 }
1587 return (0);
1588 } /* }}} int bind_config */
1590 static int bind_init(void) /* {{{ */
1591 {
1592 if (curl != NULL)
1593 return (0);
1595 curl = curl_easy_init();
1596 if (curl == NULL) {
1597 ERROR("bind plugin: bind_init: curl_easy_init failed.");
1598 return (-1);
1599 }
1601 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
1602 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
1603 curl_easy_setopt(curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
1604 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, bind_curl_error);
1605 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
1606 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
1607 #ifdef HAVE_CURLOPT_TIMEOUT_MS
1608 curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS,
1609 (timeout >= 0) ? (long)timeout : (long)CDTIME_T_TO_MS(
1610 plugin_get_interval()));
1611 #endif
1613 return (0);
1614 } /* }}} int bind_init */
1616 static int bind_read(void) /* {{{ */
1617 {
1618 int status;
1620 if (curl == NULL) {
1621 ERROR("bind plugin: I don't have a CURL object.");
1622 return (-1);
1623 }
1625 bind_buffer_fill = 0;
1627 curl_easy_setopt(curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL);
1629 if (curl_easy_perform(curl) != CURLE_OK) {
1630 ERROR("bind plugin: curl_easy_perform failed: %s", bind_curl_error);
1631 return (-1);
1632 }
1634 status = bind_xml(bind_buffer);
1635 if (status != 0)
1636 return (-1);
1637 else
1638 return (0);
1639 } /* }}} int bind_read */
1641 static int bind_shutdown(void) /* {{{ */
1642 {
1643 if (curl != NULL) {
1644 curl_easy_cleanup(curl);
1645 curl = NULL;
1646 }
1648 return (0);
1649 } /* }}} int bind_shutdown */
1651 void module_register(void) {
1652 plugin_register_complex_config("bind", bind_config);
1653 plugin_register_init("bind", bind_init);
1654 plugin_register_read("bind", bind_read);
1655 plugin_register_shutdown("bind", bind_shutdown);
1656 } /* void module_register */
1658 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */