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