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 {
67 char *name;
69 int qtypes;
70 int resolver_stats;
71 int cacherrsets;
73 char **zones;
74 size_t zones_num;
75 };
76 typedef struct cb_view_s cb_view_t;
78 struct translation_info_s
79 {
80 const char *xml_name;
81 const char *type;
82 const char *type_instance;
83 };
84 typedef struct translation_info_s translation_info_t;
86 struct translation_table_ptr_s
87 {
88 const translation_info_t *table;
89 size_t table_length;
90 const char *plugin_instance;
91 };
92 typedef struct translation_table_ptr_s translation_table_ptr_t;
94 struct list_info_ptr_s
95 {
96 const char *plugin_instance;
97 const char *type;
98 };
99 typedef struct list_info_ptr_s list_info_ptr_t;
101 /* FIXME: Enabled by default for backwards compatibility. */
102 /* TODO: Remove time parsing code. */
103 static _Bool config_parse_time = 1;
105 static char *url = NULL;
106 static int global_opcodes = 1;
107 static int global_qtypes = 1;
108 static int global_server_stats = 1;
109 static int global_zone_maint_stats = 1;
110 static int global_resolver_stats = 0;
111 static int global_memory_stats = 1;
112 static int timeout = -1;
114 static cb_view_t *views = NULL;
115 static size_t views_num = 0;
117 static CURL *curl = NULL;
119 static char *bind_buffer = NULL;
120 static size_t bind_buffer_size = 0;
121 static size_t bind_buffer_fill = 0;
122 static char bind_curl_error[CURL_ERROR_SIZE];
124 /* Translation table for the `nsstats' values. */
125 static const translation_info_t nsstats_translation_table[] = /* {{{ */
126 {
127 /* Requests */
128 { "Requestv4", "dns_request", "IPv4" },
129 { "Requestv6", "dns_request", "IPv6" },
130 { "ReqEdns0", "dns_request", "EDNS0" },
131 { "ReqBadEDNSVer", "dns_request", "BadEDNSVer" },
132 { "ReqTSIG", "dns_request", "TSIG" },
133 { "ReqSIG0", "dns_request", "SIG0" },
134 { "ReqBadSIG", "dns_request", "BadSIG" },
135 { "ReqTCP", "dns_request", "TCP" },
136 /* Rejects */
137 { "AuthQryRej", "dns_reject", "authoritative" },
138 { "RecQryRej", "dns_reject", "recursive" },
139 { "XfrRej", "dns_reject", "transfer" },
140 { "UpdateRej", "dns_reject", "update" },
141 /* Responses */
142 { "Response", "dns_response", "normal" },
143 { "TruncatedResp", "dns_response", "truncated" },
144 { "RespEDNS0", "dns_response", "EDNS0" },
145 { "RespTSIG", "dns_response", "TSIG" },
146 { "RespSIG0", "dns_response", "SIG0" },
147 /* Queries */
148 { "QryAuthAns", "dns_query", "authoritative" },
149 { "QryNoauthAns", "dns_query", "nonauth" },
150 { "QryReferral", "dns_query", "referral" },
151 { "QryRecursion", "dns_query", "recursion" },
152 { "QryDuplicate", "dns_query", "duplicate" },
153 { "QryDropped", "dns_query", "dropped" },
154 { "QryFailure", "dns_query", "failure" },
155 /* Response codes */
156 { "QrySuccess", "dns_rcode", "tx-NOERROR" },
157 { "QryNxrrset", "dns_rcode", "tx-NXRRSET" },
158 { "QrySERVFAIL", "dns_rcode", "tx-SERVFAIL" },
159 { "QryFORMERR", "dns_rcode", "tx-FORMERR" },
160 { "QryNXDOMAIN", "dns_rcode", "tx-NXDOMAIN" }
161 #if 0
162 { "XfrReqDone", "type", "type_instance" },
163 { "UpdateReqFwd", "type", "type_instance" },
164 { "UpdateRespFwd", "type", "type_instance" },
165 { "UpdateFwdFail", "type", "type_instance" },
166 { "UpdateDone", "type", "type_instance" },
167 { "UpdateFail", "type", "type_instance" },
168 { "UpdateBadPrereq", "type", "type_instance" },
169 #endif
170 };
171 static int nsstats_translation_table_length =
172 STATIC_ARRAY_SIZE (nsstats_translation_table);
173 /* }}} */
175 /* Translation table for the `zonestats' values. */
176 static const translation_info_t zonestats_translation_table[] = /* {{{ */
177 {
178 /* Notify's */
179 { "NotifyOutv4", "dns_notify", "tx-IPv4" },
180 { "NotifyOutv6", "dns_notify", "tx-IPv6" },
181 { "NotifyInv4", "dns_notify", "rx-IPv4" },
182 { "NotifyInv6", "dns_notify", "rx-IPv6" },
183 { "NotifyRej", "dns_notify", "rejected" },
184 /* SOA/AXFS/IXFS requests */
185 { "SOAOutv4", "dns_opcode", "SOA-IPv4" },
186 { "SOAOutv6", "dns_opcode", "SOA-IPv6" },
187 { "AXFRReqv4", "dns_opcode", "AXFR-IPv4" },
188 { "AXFRReqv6", "dns_opcode", "AXFR-IPv6" },
189 { "IXFRReqv4", "dns_opcode", "IXFR-IPv4" },
190 { "IXFRReqv6", "dns_opcode", "IXFR-IPv6" },
191 /* Domain transfers */
192 { "XfrSuccess", "dns_transfer", "success" },
193 { "XfrFail", "dns_transfer", "failure" }
194 };
195 static int zonestats_translation_table_length =
196 STATIC_ARRAY_SIZE (zonestats_translation_table);
197 /* }}} */
199 /* Translation table for the `resstats' values. */
200 static const translation_info_t resstats_translation_table[] = /* {{{ */
201 {
202 /* Generic resolver information */
203 { "Queryv4", "dns_query", "IPv4" },
204 { "Queryv6", "dns_query", "IPv6" },
205 { "Responsev4", "dns_response", "IPv4" },
206 { "Responsev6", "dns_response", "IPv6" },
207 /* Received response codes */
208 { "NXDOMAIN", "dns_rcode", "rx-NXDOMAIN" },
209 { "SERVFAIL", "dns_rcode", "rx-SERVFAIL" },
210 { "FORMERR", "dns_rcode", "rx-FORMERR" },
211 { "OtherError", "dns_rcode", "rx-OTHER" },
212 { "EDNS0Fail", "dns_rcode", "rx-EDNS0Fail"},
213 /* Received responses */
214 { "Mismatch", "dns_response", "mismatch" },
215 { "Truncated", "dns_response", "truncated" },
216 { "Lame", "dns_response", "lame" },
217 { "Retry", "dns_query", "retry" },
218 #if 0
219 { "GlueFetchv4", "type", "type_instance" },
220 { "GlueFetchv6", "type", "type_instance" },
221 { "GlueFetchv4Fail", "type", "type_instance" },
222 { "GlueFetchv6Fail", "type", "type_instance" },
223 #endif
224 /* DNSSEC information */
225 { "ValAttempt", "dns_resolver", "DNSSEC-attempt" },
226 { "ValOk", "dns_resolver", "DNSSEC-okay" },
227 { "ValNegOk", "dns_resolver", "DNSSEC-negokay" },
228 { "ValFail", "dns_resolver", "DNSSEC-fail" }
229 };
230 static int resstats_translation_table_length =
231 STATIC_ARRAY_SIZE (resstats_translation_table);
232 /* }}} */
234 /* Translation table for the `memory/summary' values. */
235 static const translation_info_t memsummary_translation_table[] = /* {{{ */
236 {
237 { "TotalUse", "memory", "TotalUse" },
238 { "InUse", "memory", "InUse" },
239 { "BlockSize", "memory", "BlockSize" },
240 { "ContextSize", "memory", "ContextSize" },
241 { "Lost", "memory", "Lost" }
242 };
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 {
250 value_list_t vl = VALUE_LIST_INIT;
252 vl.values = &value;
253 vl.values_len = 1;
254 if (config_parse_time)
255 vl.time = TIME_T_TO_CDTIME_T (ts);
256 sstrncpy(vl.plugin, "bind", sizeof(vl.plugin));
257 if (plugin_instance) {
258 sstrncpy(vl.plugin_instance, plugin_instance,
259 sizeof(vl.plugin_instance));
260 replace_special (vl.plugin_instance, sizeof (vl.plugin_instance));
261 }
262 sstrncpy(vl.type, type, sizeof(vl.type));
263 if (type_instance) {
264 sstrncpy(vl.type_instance, type_instance,
265 sizeof(vl.type_instance));
266 replace_special (vl.type_instance, sizeof (vl.type_instance));
267 }
268 plugin_dispatch_values(&vl);
269 } /* }}} void submit */
271 static size_t bind_curl_callback (void *buf, size_t size, /* {{{ */
272 size_t nmemb, void __attribute__((unused)) *stream)
273 {
274 size_t len = size * nmemb;
276 if (len == 0)
277 return (len);
279 if ((bind_buffer_fill + len) >= bind_buffer_size)
280 {
281 char *temp;
283 temp = realloc (bind_buffer, bind_buffer_fill + len + 1);
284 if (temp == NULL)
285 {
286 ERROR ("bind plugin: realloc failed.");
287 return (0);
288 }
289 bind_buffer = temp;
290 bind_buffer_size = bind_buffer_fill + len + 1;
291 }
293 memcpy (bind_buffer + bind_buffer_fill, (char *) buf, len);
294 bind_buffer_fill += len;
295 bind_buffer[bind_buffer_fill] = 0;
297 return (len);
298 } /* }}} size_t bind_curl_callback */
300 /*
301 * Callback, that's called with a translation table.
302 * (Plugin instance is fixed, type and type instance come from lookup table.)
303 */
304 static int bind_xml_table_callback (const char *name, value_t value, /* {{{ */
305 time_t current_time, void *user_data)
306 {
307 translation_table_ptr_t *table = (translation_table_ptr_t *) user_data;
309 if (table == NULL)
310 return (-1);
312 for (size_t i = 0; i < table->table_length; i++)
313 {
314 if (strcmp (table->table[i].xml_name, name) != 0)
315 continue;
317 submit (current_time,
318 table->plugin_instance,
319 table->table[i].type,
320 table->table[i].type_instance,
321 value);
322 break;
323 }
325 return (0);
326 } /* }}} int bind_xml_table_callback */
328 /*
329 * Callback, that's used for lists.
330 * (Plugin instance and type are fixed, xml name is used as type instance.)
331 */
332 static int bind_xml_list_callback (const char *name, /* {{{ */
333 value_t value, time_t current_time, void *user_data)
334 {
335 list_info_ptr_t *list_info = (list_info_ptr_t *) user_data;
337 if (list_info == NULL)
338 return (-1);
340 submit (current_time,
341 list_info->plugin_instance,
342 list_info->type,
343 /* type instance = */ name,
344 value);
346 return (0);
347 } /* }}} int bind_xml_list_callback */
349 static int bind_xml_read_derive (xmlDoc *doc, xmlNode *node, /* {{{ */
350 derive_t *ret_value)
351 {
352 char *str_ptr;
353 value_t value;
354 int status;
356 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
357 if (str_ptr == NULL)
358 {
359 ERROR ("bind plugin: bind_xml_read_derive: xmlNodeListGetString failed.");
360 return (-1);
361 }
363 status = parse_value (str_ptr, &value, DS_TYPE_DERIVE);
364 if (status != 0)
365 {
366 ERROR ("bind plugin: Parsing string \"%s\" to derive value failed.",
367 str_ptr);
368 xmlFree(str_ptr);
369 return (-1);
370 }
372 xmlFree(str_ptr);
373 *ret_value = value.derive;
374 return (0);
375 } /* }}} int bind_xml_read_derive */
377 static int bind_xml_read_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
378 gauge_t *ret_value)
379 {
380 char *str_ptr, *end_ptr;
381 double value;
383 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
384 if (str_ptr == NULL)
385 {
386 ERROR ("bind plugin: bind_xml_read_gauge: xmlNodeListGetString failed.");
387 return (-1);
388 }
390 errno = 0;
391 value = strtod (str_ptr, &end_ptr);
392 xmlFree(str_ptr);
393 if (str_ptr == end_ptr || errno)
394 {
395 if (errno && (value < 0))
396 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with underflow.");
397 else if (errno && (value > 0))
398 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with overflow.");
399 else
400 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed.");
401 return (-1);
402 }
404 *ret_value = (gauge_t) value;
405 return (0);
406 } /* }}} int bind_xml_read_gauge */
408 static int bind_xml_read_timestamp (const char *xpath_expression, /* {{{ */
409 xmlDoc *doc, xmlXPathContext *xpathCtx, time_t *ret_value)
410 {
411 xmlXPathObject *xpathObj = NULL;
412 xmlNode *node;
413 char *str_ptr;
414 char *tmp;
415 struct tm tm = { 0 };
417 xpathObj = xmlXPathEvalExpression (BAD_CAST xpath_expression, xpathCtx);
418 if (xpathObj == NULL)
419 {
420 ERROR ("bind plugin: Unable to evaluate XPath expression `%s'.",
421 xpath_expression);
422 return (-1);
423 }
425 if ((xpathObj->nodesetval == NULL) || (xpathObj->nodesetval->nodeNr < 1))
426 {
427 xmlXPathFreeObject (xpathObj);
428 return (-1);
429 }
431 if (xpathObj->nodesetval->nodeNr != 1)
432 {
433 NOTICE ("bind plugin: Evaluating the XPath expression `%s' returned "
434 "%i nodes. Only handling the first one.",
435 xpath_expression, xpathObj->nodesetval->nodeNr);
436 }
438 node = xpathObj->nodesetval->nodeTab[0];
440 if (node->xmlChildrenNode == NULL)
441 {
442 ERROR ("bind plugin: bind_xml_read_timestamp: "
443 "node->xmlChildrenNode == NULL");
444 xmlXPathFreeObject (xpathObj);
445 return (-1);
446 }
448 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
449 if (str_ptr == NULL)
450 {
451 ERROR ("bind plugin: bind_xml_read_timestamp: xmlNodeListGetString failed.");
452 xmlXPathFreeObject (xpathObj);
453 return (-1);
454 }
456 tmp = strptime (str_ptr, "%Y-%m-%dT%T", &tm);
457 xmlFree(str_ptr);
458 if (tmp == NULL)
459 {
460 ERROR ("bind plugin: bind_xml_read_timestamp: strptime failed.");
461 xmlXPathFreeObject (xpathObj);
462 return (-1);
463 }
465 *ret_value = mktime(&tm);
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,
483 xmlDoc *doc, xmlXPathContext *xpathCtx,
484 time_t current_time, int ds_type)
485 {
486 xmlXPathObject *xpathObj = NULL;
487 int num_entries;
489 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
490 if (xpathObj == NULL)
491 {
492 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
493 xpath_expression);
494 return (-1);
495 }
497 num_entries = 0;
498 /* Iterate over all matching nodes. */
499 for (int i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
500 {
501 xmlNode *name_node = NULL;
502 xmlNode *counter = NULL;
503 xmlNode *parent;
505 parent = xpathObj->nodesetval->nodeTab[i];
506 DEBUG ("bind plugin: bind_parse_generic_name_value: parent->name = %s;",
507 (char *) parent->name);
509 /* Iterate over all child nodes. */
510 for (xmlNode *child = parent->xmlChildrenNode;
511 child != NULL;
512 child = child->next)
513 {
514 if (child->type != XML_ELEMENT_NODE)
515 continue;
517 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
518 name_node = child;
519 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
520 counter = child;
521 }
523 if ((name_node != NULL) && (counter != NULL))
524 {
525 char *name = (char *) xmlNodeListGetString (doc,
526 name_node->xmlChildrenNode, 1);
527 value_t value;
528 int status;
530 if (ds_type == DS_TYPE_GAUGE)
531 status = bind_xml_read_gauge (doc, counter, &value.gauge);
532 else
533 status = bind_xml_read_derive (doc, counter, &value.derive);
534 if (status != 0)
535 continue;
537 status = (*list_callback) (name, value, current_time, user_data);
538 if (status == 0)
539 num_entries++;
541 xmlFree (name);
542 }
543 }
545 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
546 num_entries, (num_entries == 1) ? "entry" : "entries",
547 xpath_expression);
549 xmlXPathFreeObject(xpathObj);
551 return (0);
552 } /* }}} int bind_parse_generic_name_value */
554 /*
555 * bind_parse_generic_value_list
556 *
557 * Reads statistics in the form:
558 * <foo>
559 * <name0>123</name0>
560 * <name1>234</name1>
561 * <name2>345</name2>
562 * :
563 * </foo>
564 */
565 static int bind_parse_generic_value_list (const char *xpath_expression, /* {{{ */
566 list_callback_t list_callback,
567 void *user_data,
568 xmlDoc *doc, xmlXPathContext *xpathCtx,
569 time_t current_time, int ds_type)
570 {
571 xmlXPathObject *xpathObj = NULL;
572 int num_entries;
574 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
575 if (xpathObj == NULL)
576 {
577 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
578 xpath_expression);
579 return (-1);
580 }
582 num_entries = 0;
583 /* Iterate over all matching nodes. */
584 for (int i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
585 {
586 /* Iterate over all child nodes. */
587 for (xmlNode *child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
588 child != NULL;
589 child = child->next)
590 {
591 char *node_name;
592 value_t value;
593 int status;
595 if (child->type != XML_ELEMENT_NODE)
596 continue;
598 node_name = (char *) child->name;
600 if (ds_type == DS_TYPE_GAUGE)
601 status = bind_xml_read_gauge (doc, child, &value.gauge);
602 else
603 status = bind_xml_read_derive (doc, child, &value.derive);
604 if (status != 0)
605 continue;
607 status = (*list_callback) (node_name, value, current_time, user_data);
608 if (status == 0)
609 num_entries++;
610 }
611 }
613 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
614 num_entries, (num_entries == 1) ? "entry" : "entries",
615 xpath_expression);
617 xmlXPathFreeObject(xpathObj);
619 return (0);
620 } /* }}} int bind_parse_generic_value_list */
622 /*
623 * bind_parse_generic_name_attr_value_list
624 *
625 * Reads statistics in the form:
626 * <foo>
627 * <counter name="name0">123</counter>
628 * <counter name="name1">234</counter>
629 * <counter name="name2">345</counter>
630 * :
631 * </foo>
632 */
633 static int bind_parse_generic_name_attr_value_list (const char *xpath_expression, /* {{{ */
634 list_callback_t list_callback,
635 void *user_data,
636 xmlDoc *doc, xmlXPathContext *xpathCtx,
637 time_t current_time, int ds_type)
638 {
639 xmlXPathObject *xpathObj = NULL;
640 int num_entries;
642 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
643 if (xpathObj == NULL)
644 {
645 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
646 xpath_expression);
647 return (-1);
648 }
650 num_entries = 0;
651 /* Iterate over all matching nodes. */
652 for (int i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
653 {
654 /* Iterate over all child nodes. */
655 for (xmlNode *child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
656 child != NULL;
657 child = child->next)
658 {
659 if (child->type != XML_ELEMENT_NODE)
660 continue;
662 if (strncmp ("counter", (char *) child->name, strlen ("counter")) != 0)
663 continue;
665 char *attr_name;
666 value_t value;
667 int status;
669 attr_name = (char *) xmlGetProp (child, BAD_CAST "name");
670 if (attr_name == NULL)
671 {
672 DEBUG ("bind plugin: found <counter> without name.");
673 continue;
674 }
675 if (ds_type == DS_TYPE_GAUGE)
676 status = bind_xml_read_gauge (doc, child, &value.gauge);
677 else
678 status = bind_xml_read_derive (doc, child, &value.derive);
679 if (status != 0)
680 continue;
682 status = (*list_callback) (attr_name, value, current_time, user_data);
683 if (status == 0)
684 num_entries++;
685 }
686 }
688 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
689 num_entries, (num_entries == 1) ? "entry" : "entries",
690 xpath_expression);
692 xmlXPathFreeObject(xpathObj);
694 return (0);
695 } /* }}} int bind_parse_generic_name_attr_value_list */
697 static int bind_xml_stats_handle_zone (int version, xmlDoc *doc, /* {{{ */
698 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
699 time_t current_time)
700 {
701 xmlXPathObject *path_obj;
702 char *zone_name = NULL;
703 size_t j;
705 if (version >= 3)
706 {
707 char *n = (char *) xmlGetProp (node, BAD_CAST "name");
708 char *c = (char *) xmlGetProp (node, BAD_CAST "rdataclass");
709 if (n && c)
710 {
711 zone_name = (char *) xmlMalloc(strlen(n) + strlen(c) + 2);
712 snprintf(zone_name, strlen(n) + strlen(c) + 2, "%s/%s", n, c);
713 }
714 xmlFree(n);
715 xmlFree(c);
716 }
717 else
718 {
719 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
720 if (path_obj == NULL)
721 {
722 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
723 return (-1);
724 }
726 for (int i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
727 {
728 zone_name = (char *) xmlNodeListGetString (doc,
729 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
730 if (zone_name != NULL)
731 break;
732 }
733 xmlXPathFreeObject (path_obj);
734 }
736 if (zone_name == NULL)
737 {
738 ERROR ("bind plugin: Could not determine zone name.");
739 return (-1);
740 }
742 for (j = 0; j < view->zones_num; j++)
743 {
744 if (strcasecmp (zone_name, view->zones[j]) == 0)
745 break;
746 }
748 xmlFree (zone_name);
749 zone_name = NULL;
751 if (j >= view->zones_num)
752 return (0);
754 zone_name = view->zones[j];
756 DEBUG ("bind plugin: bind_xml_stats_handle_zone: Found zone `%s'.",
757 zone_name);
759 { /* Parse the <counters> tag {{{ */
760 char plugin_instance[DATA_MAX_NAME_LEN];
761 translation_table_ptr_t table_ptr =
762 {
763 nsstats_translation_table,
764 nsstats_translation_table_length,
765 plugin_instance
766 };
768 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-zone-%s",
769 view->name, zone_name);
771 if (version == 3)
772 {
773 list_info_ptr_t list_info =
774 {
775 plugin_instance,
776 /* type = */ "dns_qtype"
777 };
778 bind_parse_generic_name_attr_value_list (/* xpath = */ "counters[@type='rcode']",
779 /* callback = */ bind_xml_table_callback,
780 /* user_data = */ &table_ptr,
781 doc, path_ctx, current_time, DS_TYPE_COUNTER);
782 bind_parse_generic_name_attr_value_list (/* xpath = */ "counters[@type='qtype']",
783 /* callback = */ bind_xml_list_callback,
784 /* user_data = */ &list_info,
785 doc, path_ctx, current_time, DS_TYPE_COUNTER);
786 }
787 else
788 {
789 bind_parse_generic_value_list (/* xpath = */ "counters",
790 /* callback = */ bind_xml_table_callback,
791 /* user_data = */ &table_ptr,
792 doc, path_ctx, current_time, DS_TYPE_COUNTER);
793 }
794 } /* }}} */
796 return (0);
797 } /* }}} int bind_xml_stats_handle_zone */
799 static int bind_xml_stats_search_zones (int version, xmlDoc *doc, /* {{{ */
800 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
801 time_t current_time)
802 {
803 xmlXPathObject *zone_nodes = NULL;
804 xmlXPathContext *zone_path_context;
806 zone_path_context = xmlXPathNewContext (doc);
807 if (zone_path_context == NULL)
808 {
809 ERROR ("bind plugin: xmlXPathNewContext failed.");
810 return (-1);
811 }
813 zone_nodes = xmlXPathEvalExpression (BAD_CAST "zones/zone", path_ctx);
814 if (zone_nodes == NULL)
815 {
816 ERROR ("bind plugin: Cannot find any <view> tags.");
817 xmlXPathFreeContext (zone_path_context);
818 return (-1);
819 }
821 for (int i = 0; i < zone_nodes->nodesetval->nodeNr; i++)
822 {
823 node = zone_nodes->nodesetval->nodeTab[i];
824 assert (node != NULL);
826 zone_path_context->node = node;
828 bind_xml_stats_handle_zone (version, doc, zone_path_context, node, view,
829 current_time);
830 }
832 xmlXPathFreeObject (zone_nodes);
833 xmlXPathFreeContext (zone_path_context);
834 return (0);
835 } /* }}} int bind_xml_stats_search_zones */
837 static int bind_xml_stats_handle_view (int version, xmlDoc *doc, /* {{{ */
838 xmlXPathContext *path_ctx, xmlNode *node, time_t current_time)
839 {
840 char *view_name = NULL;
841 cb_view_t *view;
842 size_t j;
844 if (version == 3)
845 {
846 view_name = (char*) xmlGetProp(node, BAD_CAST "name");
848 if (view_name == NULL)
849 {
850 ERROR ("bind plugin: Could not determine view name.");
851 return (-1);
852 }
854 for (j = 0; j < views_num; j++)
855 {
856 if (strcasecmp (view_name, views[j].name) == 0)
857 break;
858 }
860 xmlFree (view_name);
861 view_name = NULL;
862 }
863 else
864 {
865 xmlXPathObject *path_obj;
866 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
867 if (path_obj == NULL)
868 {
869 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
870 return (-1);
871 }
873 for (int i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
874 {
875 view_name = (char *) xmlNodeListGetString (doc,
876 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
877 if (view_name != NULL)
878 break;
879 }
881 if (view_name == NULL)
882 {
883 ERROR ("bind plugin: Could not determine view name.");
884 xmlXPathFreeObject (path_obj);
885 return (-1);
886 }
888 for (j = 0; j < views_num; j++)
889 {
890 if (strcasecmp (view_name, views[j].name) == 0)
891 break;
892 }
894 xmlFree (view_name);
895 xmlXPathFreeObject (path_obj);
897 view_name = NULL;
898 path_obj = NULL;
899 }
902 if (j >= views_num)
903 return (0);
905 view = views + j;
907 DEBUG ("bind plugin: bind_xml_stats_handle_view: Found view `%s'.",
908 view->name);
910 if (view->qtypes != 0) /* {{{ */
911 {
912 char plugin_instance[DATA_MAX_NAME_LEN];
913 list_info_ptr_t list_info =
914 {
915 plugin_instance,
916 /* type = */ "dns_qtype"
917 };
919 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-qtypes",
920 view->name);
921 if (version == 3)
922 {
923 bind_parse_generic_name_attr_value_list (/* xpath = */ "counters[@type='resqtype']",
924 /* callback = */ bind_xml_list_callback,
925 /* user_data = */ &list_info,
926 doc, path_ctx, current_time, DS_TYPE_COUNTER);
927 }
928 else
929 {
930 bind_parse_generic_name_value (/* xpath = */ "rdtype",
931 /* callback = */ bind_xml_list_callback,
932 /* user_data = */ &list_info,
933 doc, path_ctx, current_time, DS_TYPE_COUNTER);
934 }
935 } /* }}} */
937 if (view->resolver_stats != 0) /* {{{ */
938 {
939 char plugin_instance[DATA_MAX_NAME_LEN];
940 translation_table_ptr_t table_ptr =
941 {
942 resstats_translation_table,
943 resstats_translation_table_length,
944 plugin_instance
945 };
947 ssnprintf (plugin_instance, sizeof (plugin_instance),
948 "%s-resolver_stats", view->name);
949 if (version == 3)
950 {
951 bind_parse_generic_name_attr_value_list ("counters[@type='resstats']",
952 /* callback = */ bind_xml_table_callback,
953 /* user_data = */ &table_ptr,
954 doc, path_ctx, current_time, DS_TYPE_COUNTER);
955 }
956 else
957 {
958 bind_parse_generic_name_value ("resstat",
959 /* callback = */ bind_xml_table_callback,
960 /* user_data = */ &table_ptr,
961 doc, path_ctx, current_time, DS_TYPE_COUNTER);
962 }
963 } /* }}} */
965 /* Record types in the cache */
966 if (view->cacherrsets != 0) /* {{{ */
967 {
968 char plugin_instance[DATA_MAX_NAME_LEN];
969 list_info_ptr_t list_info =
970 {
971 plugin_instance,
972 /* type = */ "dns_qtype_cached"
973 };
975 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-cache_rr_sets",
976 view->name);
978 bind_parse_generic_name_value (/* xpath = */ "cache/rrset",
979 /* callback = */ bind_xml_list_callback,
980 /* user_data = */ &list_info,
981 doc, path_ctx, current_time, DS_TYPE_GAUGE);
982 } /* }}} */
984 if (view->zones_num > 0)
985 bind_xml_stats_search_zones (version, doc, path_ctx, node, view,
986 current_time);
988 return (0);
989 } /* }}} int bind_xml_stats_handle_view */
991 static int bind_xml_stats_search_views (int version, xmlDoc *doc, /* {{{ */
992 xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time)
993 {
994 xmlXPathObject *view_nodes = NULL;
995 xmlXPathContext *view_path_context;
997 view_path_context = xmlXPathNewContext (doc);
998 if (view_path_context == NULL)
999 {
1000 ERROR ("bind plugin: xmlXPathNewContext failed.");
1001 return (-1);
1002 }
1004 view_nodes = xmlXPathEvalExpression (BAD_CAST "views/view", xpathCtx);
1005 if (view_nodes == NULL)
1006 {
1007 ERROR ("bind plugin: Cannot find any <view> tags.");
1008 xmlXPathFreeContext (view_path_context);
1009 return (-1);
1010 }
1012 for (int i = 0; i < view_nodes->nodesetval->nodeNr; i++)
1013 {
1014 xmlNode *node;
1016 node = view_nodes->nodesetval->nodeTab[i];
1017 assert (node != NULL);
1019 view_path_context->node = node;
1021 bind_xml_stats_handle_view (version, doc, view_path_context, node,
1022 current_time);
1023 }
1025 xmlXPathFreeObject (view_nodes);
1026 xmlXPathFreeContext (view_path_context);
1027 return (0);
1028 } /* }}} int bind_xml_stats_search_views */
1030 static void bind_xml_stats_v3 (xmlDoc *doc, /* {{{ */
1031 xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time)
1032 {
1033 /* XPath: server/counters[@type='opcode']
1034 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
1035 * Layout v3:
1036 * <counters type="opcode">
1037 * <counter name="A">1</counter>
1038 * :
1039 * </counters>
1040 */
1041 if (global_opcodes != 0)
1042 {
1043 list_info_ptr_t list_info =
1044 {
1045 /* plugin instance = */ "global-opcodes",
1046 /* type = */ "dns_opcode"
1047 };
1048 bind_parse_generic_name_attr_value_list (/* xpath = */ "server/counters[@type='opcode']",
1049 /* callback = */ bind_xml_list_callback,
1050 /* user_data = */ &list_info,
1051 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1052 }
1054 /* XPath: server/counters[@type='qtype']
1055 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
1056 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
1057 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
1058 * Layout v3:
1059 * <counters type="opcode">
1060 * <counter name="A">1</counter>
1061 * :
1062 * </counters>
1063 */
1064 if (global_qtypes != 0)
1065 {
1066 list_info_ptr_t list_info =
1067 {
1068 /* plugin instance = */ "global-qtypes",
1069 /* type = */ "dns_qtype"
1070 };
1072 bind_parse_generic_name_attr_value_list (/* xpath = */ "server/counters[@type='qtype']",
1073 /* callback = */ bind_xml_list_callback,
1074 /* user_data = */ &list_info,
1075 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1076 }
1078 /* XPath: server/counters[@type='nsstat']
1079 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
1080 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
1081 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
1082 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
1083 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
1084 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
1085 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
1086 * UpdateBadPrereq
1087 * Layout v3:
1088 * <counters type="nsstat"
1089 * <counter name="Requestv4">1</counter>
1090 * <counter name="Requestv6">0</counter>
1091 * :
1092 * </counter>
1093 */
1094 if (global_server_stats)
1095 {
1096 translation_table_ptr_t table_ptr =
1097 {
1098 nsstats_translation_table,
1099 nsstats_translation_table_length,
1100 /* plugin_instance = */ "global-server_stats"
1101 };
1103 bind_parse_generic_name_attr_value_list ("server/counters[@type='nsstat']",
1104 /* callback = */ bind_xml_table_callback,
1105 /* user_data = */ &table_ptr,
1106 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1107 }
1109 /* XPath: server/zonestats, server/zonestat, server/counters[@type='zonestat']
1110 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1111 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1112 * XfrSuccess, XfrFail
1113 * Layout v3:
1114 * <counters type="zonestat"
1115 * <counter name="NotifyOutv4">0</counter>
1116 * <counter name="NotifyOutv6">0</counter>
1117 * :
1118 * </counter>
1119 */
1120 if (global_zone_maint_stats)
1121 {
1122 translation_table_ptr_t table_ptr =
1123 {
1124 zonestats_translation_table,
1125 zonestats_translation_table_length,
1126 /* plugin_instance = */ "global-zone_maint_stats"
1127 };
1129 bind_parse_generic_name_attr_value_list ("server/counters[@type='zonestat']",
1130 /* callback = */ bind_xml_table_callback,
1131 /* user_data = */ &table_ptr,
1132 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1133 }
1135 /* XPath: server/resstats, server/counters[@type='resstat']
1136 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1137 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1138 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1139 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1140 * Layout v3:
1141 * <counters type="resstat"
1142 * <counter name="Queryv4">0</counter>
1143 * <counter name="Queryv6">0</counter>
1144 * :
1145 * </counter>
1146 */
1147 if (global_resolver_stats != 0)
1148 {
1149 translation_table_ptr_t table_ptr =
1150 {
1151 resstats_translation_table,
1152 resstats_translation_table_length,
1153 /* plugin_instance = */ "global-resolver_stats"
1154 };
1156 bind_parse_generic_name_attr_value_list ("server/counters[@type='resstat']",
1157 /* callback = */ bind_xml_table_callback,
1158 /* user_data = */ &table_ptr,
1159 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1160 }
1161 } /* }}} bind_xml_stats_v3 */
1163 static void bind_xml_stats_v1_v2 (int version, xmlDoc *doc, /* {{{ */
1164 xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time)
1165 {
1166 /* XPath: server/requests/opcode, server/counters[@type='opcode']
1167 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
1168 * Layout V1 and V2:
1169 * <opcode>
1170 * <name>A</name>
1171 * <counter>1</counter>
1172 * </opcode>
1173 * :
1174 */
1175 if (global_opcodes != 0)
1176 {
1177 list_info_ptr_t list_info =
1178 {
1179 /* plugin instance = */ "global-opcodes",
1180 /* type = */ "dns_opcode"
1181 };
1183 bind_parse_generic_name_value (/* xpath = */ "server/requests/opcode",
1184 /* callback = */ bind_xml_list_callback,
1185 /* user_data = */ &list_info,
1186 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1187 }
1189 /* XPath: server/queries-in/rdtype, server/counters[@type='qtype']
1190 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
1191 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
1192 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
1193 * Layout v1 or v2:
1194 * <rdtype>
1195 * <name>A</name>
1196 * <counter>1</counter>
1197 * </rdtype>
1198 * :
1199 */
1200 if (global_qtypes != 0)
1201 {
1202 list_info_ptr_t list_info =
1203 {
1204 /* plugin instance = */ "global-qtypes",
1205 /* type = */ "dns_qtype"
1206 };
1208 bind_parse_generic_name_value (/* xpath = */ "server/queries-in/rdtype",
1209 /* callback = */ bind_xml_list_callback,
1210 /* user_data = */ &list_info,
1211 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1212 }
1214 /* XPath: server/nsstats, server/nsstat, server/counters[@type='nsstat']
1215 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
1216 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
1217 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
1218 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
1219 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
1220 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
1221 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
1222 * UpdateBadPrereq
1223 * Layout v1:
1224 * <nsstats>
1225 * <Requestv4>1</Requestv4>
1226 * <Requestv6>0</Requestv6>
1227 * :
1228 * </nsstats>
1229 * Layout v2:
1230 * <nsstat>
1231 * <name>Requestv4</name>
1232 * <counter>1</counter>
1233 * </nsstat>
1234 * <nsstat>
1235 * <name>Requestv6</name>
1236 * <counter>0</counter>
1237 * </nsstat>
1238 * :
1239 */
1240 if (global_server_stats)
1241 {
1242 translation_table_ptr_t table_ptr =
1243 {
1244 nsstats_translation_table,
1245 nsstats_translation_table_length,
1246 /* plugin_instance = */ "global-server_stats"
1247 };
1249 if (version == 1)
1250 {
1251 bind_parse_generic_value_list ("server/nsstats",
1252 /* callback = */ bind_xml_table_callback,
1253 /* user_data = */ &table_ptr,
1254 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1255 }
1256 else
1257 {
1258 bind_parse_generic_name_value ("server/nsstat",
1259 /* callback = */ bind_xml_table_callback,
1260 /* user_data = */ &table_ptr,
1261 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1262 }
1263 }
1265 /* XPath: server/zonestats, server/zonestat, server/counters[@type='zonestat']
1266 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1267 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1268 * XfrSuccess, XfrFail
1269 * Layout v1:
1270 * <zonestats>
1271 * <NotifyOutv4>0</NotifyOutv4>
1272 * <NotifyOutv6>0</NotifyOutv6>
1273 * :
1274 * </zonestats>
1275 * Layout v2:
1276 * <zonestat>
1277 * <name>NotifyOutv4</name>
1278 * <counter>0</counter>
1279 * </zonestat>
1280 * <zonestat>
1281 * <name>NotifyOutv6</name>
1282 * <counter>0</counter>
1283 * </zonestat>
1284 * :
1285 */
1286 if (global_zone_maint_stats)
1287 {
1288 translation_table_ptr_t table_ptr =
1289 {
1290 zonestats_translation_table,
1291 zonestats_translation_table_length,
1292 /* plugin_instance = */ "global-zone_maint_stats"
1293 };
1295 if (version == 1)
1296 {
1297 bind_parse_generic_value_list ("server/zonestats",
1298 /* callback = */ bind_xml_table_callback,
1299 /* user_data = */ &table_ptr,
1300 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1301 }
1302 else
1303 {
1304 bind_parse_generic_name_value ("server/zonestat",
1305 /* callback = */ bind_xml_table_callback,
1306 /* user_data = */ &table_ptr,
1307 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1308 }
1309 }
1311 /* XPath: server/resstats, server/counters[@type='resstat']
1312 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1313 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1314 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1315 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1316 * Layout v1:
1317 * <resstats>
1318 * <Queryv4>0</Queryv4>
1319 * <Queryv6>0</Queryv6>
1320 * :
1321 * </resstats>
1322 * Layout v2:
1323 * <resstat>
1324 * <name>Queryv4</name>
1325 * <counter>0</counter>
1326 * </resstat>
1327 * <resstat>
1328 * <name>Queryv6</name>
1329 * <counter>0</counter>
1330 * </resstat>
1331 * :
1332 */
1333 if (global_resolver_stats != 0)
1334 {
1335 translation_table_ptr_t table_ptr =
1336 {
1337 resstats_translation_table,
1338 resstats_translation_table_length,
1339 /* plugin_instance = */ "global-resolver_stats"
1340 };
1342 if (version == 1)
1343 {
1344 bind_parse_generic_value_list ("server/resstats",
1345 /* callback = */ bind_xml_table_callback,
1346 /* user_data = */ &table_ptr,
1347 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1348 }
1349 else
1350 {
1351 bind_parse_generic_name_value ("server/resstat",
1352 /* callback = */ bind_xml_table_callback,
1353 /* user_data = */ &table_ptr,
1354 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1355 }
1356 }
1357 } /* }}} bind_xml_stats_v1_v2 */
1359 static int bind_xml_stats (int version, xmlDoc *doc, /* {{{ */
1360 xmlXPathContext *xpathCtx, xmlNode *statsnode)
1361 {
1362 time_t current_time = 0;
1363 int status;
1365 xpathCtx->node = statsnode;
1367 /* TODO: Check `server/boot-time' to recognize server restarts. */
1369 status = bind_xml_read_timestamp ("server/current-time",
1370 doc, xpathCtx, ¤t_time);
1371 if (status != 0)
1372 {
1373 ERROR ("bind plugin: Reading `server/current-time' failed.");
1374 return (-1);
1375 }
1376 DEBUG ("bind plugin: Current server time is %i.", (int) current_time);
1378 if (version == 3)
1379 {
1380 bind_xml_stats_v3(doc, xpathCtx, statsnode, current_time);
1381 }
1382 else
1383 {
1384 bind_xml_stats_v1_v2(version, doc, xpathCtx, statsnode, current_time);
1385 }
1387 /* XPath: memory/summary
1388 * Variables: TotalUse, InUse, BlockSize, ContextSize, Lost
1389 * Layout: v2 and v3:
1390 * <summary>
1391 * <TotalUse>6587096</TotalUse>
1392 * <InUse>1345424</InUse>
1393 * <BlockSize>5505024</BlockSize>
1394 * <ContextSize>3732456</ContextSize>
1395 * <Lost>0</Lost>
1396 * </summary>
1397 */
1398 if (global_memory_stats != 0)
1399 {
1400 translation_table_ptr_t table_ptr =
1401 {
1402 memsummary_translation_table,
1403 memsummary_translation_table_length,
1404 /* plugin_instance = */ "global-memory_stats"
1405 };
1407 bind_parse_generic_value_list ("memory/summary",
1408 /* callback = */ bind_xml_table_callback,
1409 /* user_data = */ &table_ptr,
1410 doc, xpathCtx, current_time, DS_TYPE_GAUGE);
1411 }
1413 if (views_num > 0)
1414 bind_xml_stats_search_views (version, doc, xpathCtx, statsnode,
1415 current_time);
1417 return 0;
1418 } /* }}} int bind_xml_stats */
1420 static int bind_xml (const char *data) /* {{{ */
1421 {
1422 xmlDoc *doc = NULL;
1423 xmlXPathContext *xpathCtx = NULL;
1424 xmlXPathObject *xpathObj = NULL;
1425 int ret = -1;
1427 doc = xmlParseMemory (data, strlen (data));
1428 if (doc == NULL)
1429 {
1430 ERROR ("bind plugin: xmlParseMemory failed.");
1431 return (-1);
1432 }
1434 xpathCtx = xmlXPathNewContext (doc);
1435 if (xpathCtx == NULL)
1436 {
1437 ERROR ("bind plugin: xmlXPathNewContext failed.");
1438 xmlFreeDoc (doc);
1439 return (-1);
1440 }
1442 //
1443 // version 3.* of statistics XML (since BIND9.9)
1444 //
1446 xpathObj = xmlXPathEvalExpression (BAD_CAST "/statistics", xpathCtx);
1447 if (xpathObj == NULL || xpathObj->nodesetval == NULL || xpathObj->nodesetval->nodeNr == 0)
1448 {
1449 DEBUG ("bind plugin: Statistics appears not to be v3");
1450 // we will fallback to v1 or v2 detection
1451 if (xpathObj != NULL) { xmlXPathFreeObject (xpathObj); }
1452 }
1453 else
1454 {
1455 for (int i = 0; i < xpathObj->nodesetval->nodeNr; i++)
1456 {
1457 xmlNode *node;
1458 char *attr_version;
1460 node = xpathObj->nodesetval->nodeTab[i];
1461 assert (node != NULL);
1463 attr_version = (char *) xmlGetProp (node, BAD_CAST "version");
1464 if (attr_version == NULL)
1465 {
1466 NOTICE ("bind plugin: Found <statistics> tag doesn't have a "
1467 "`version' attribute.");
1468 continue;
1469 }
1470 DEBUG ("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1472 if (strncmp ("3.", attr_version, strlen ("3.")) != 0)
1473 {
1474 /* TODO: Use the complaint mechanism here. */
1475 NOTICE ("bind plugin: Found <statistics> tag with version `%s'. "
1476 "Unfortunately I have no clue how to parse that. "
1477 "Please open a bug report for this.", attr_version);
1478 xmlFree (attr_version);
1479 continue;
1480 }
1481 ret = bind_xml_stats (3, doc, xpathCtx, node);
1483 xmlFree (attr_version);
1484 /* One <statistics> node ought to be enough. */
1485 break;
1486 }
1488 // we are finished, early-return
1489 xmlXPathFreeObject (xpathObj);
1490 xmlXPathFreeContext (xpathCtx);
1491 xmlFreeDoc (doc);
1493 return (ret);
1494 }
1496 //
1497 // versions 1.* or 2.* of statistics XML
1498 //
1500 xpathObj = xmlXPathEvalExpression (BAD_CAST "/isc/bind/statistics", xpathCtx);
1501 if (xpathObj == NULL)
1502 {
1503 ERROR ("bind plugin: Cannot find the <statistics> tag.");
1504 xmlXPathFreeContext (xpathCtx);
1505 xmlFreeDoc (doc);
1506 return (-1);
1507 }
1508 else if (xpathObj->nodesetval == NULL)
1509 {
1510 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
1511 xmlXPathFreeObject (xpathObj);
1512 xmlXPathFreeContext (xpathCtx);
1513 xmlFreeDoc (doc);
1514 return (-1);
1515 }
1517 for (int i = 0; i < xpathObj->nodesetval->nodeNr; i++)
1518 {
1519 xmlNode *node;
1520 char *attr_version;
1521 int parsed_version = 0;
1523 node = xpathObj->nodesetval->nodeTab[i];
1524 assert (node != NULL);
1526 attr_version = (char *) xmlGetProp (node, BAD_CAST "version");
1527 if (attr_version == NULL)
1528 {
1529 NOTICE ("bind plugin: Found <statistics> tag doesn't have a "
1530 "`version' attribute.");
1531 continue;
1532 }
1533 DEBUG ("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1535 /* At the time this plugin was written, version "1.0" was used by
1536 * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
1537 * that "1.*" and "2.*" don't introduce structural changes, so we just
1538 * check for the first two characters here. */
1539 if (strncmp ("1.", attr_version, strlen ("1.")) == 0)
1540 parsed_version = 1;
1541 else if (strncmp ("2.", attr_version, strlen ("2.")) == 0)
1542 parsed_version = 2;
1543 else
1544 {
1545 /* TODO: Use the complaint mechanism here. */
1546 NOTICE ("bind plugin: Found <statistics> tag with version `%s'. "
1547 "Unfortunately I have no clue how to parse that. "
1548 "Please open a bug report for this.", attr_version);
1549 xmlFree (attr_version);
1550 continue;
1551 }
1553 ret = bind_xml_stats (parsed_version,
1554 doc, xpathCtx, node);
1556 xmlFree (attr_version);
1557 /* One <statistics> node ought to be enough. */
1558 break;
1559 }
1561 xmlXPathFreeObject (xpathObj);
1562 xmlXPathFreeContext (xpathCtx);
1563 xmlFreeDoc (doc);
1565 return (ret);
1566 } /* }}} int bind_xml */
1568 static int bind_config_set_bool (const char *name, int *var, /* {{{ */
1569 oconfig_item_t *ci)
1570 {
1571 if ((ci->values_num != 1) || ( ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1572 {
1573 WARNING ("bind plugin: The `%s' option needs "
1574 "exactly one boolean argument.", name);
1575 return (-1);
1576 }
1578 if (ci->values[0].value.boolean)
1579 *var = 1;
1580 else
1581 *var = 0;
1582 return 0;
1583 } /* }}} int bind_config_set_bool */
1585 static int bind_config_add_view_zone (cb_view_t *view, /* {{{ */
1586 oconfig_item_t *ci)
1587 {
1588 char **tmp;
1590 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1591 {
1592 WARNING ("bind plugin: The `Zone' option needs "
1593 "exactly one string argument.");
1594 return (-1);
1595 }
1597 tmp = realloc (view->zones,
1598 sizeof (char *) * (view->zones_num + 1));
1599 if (tmp == NULL)
1600 {
1601 ERROR ("bind plugin: realloc failed.");
1602 return (-1);
1603 }
1604 view->zones = tmp;
1606 view->zones[view->zones_num] = strdup (ci->values[0].value.string);
1607 if (view->zones[view->zones_num] == NULL)
1608 {
1609 ERROR ("bind plugin: strdup failed.");
1610 return (-1);
1611 }
1612 view->zones_num++;
1614 return (0);
1615 } /* }}} int bind_config_add_view_zone */
1617 static int bind_config_add_view (oconfig_item_t *ci) /* {{{ */
1618 {
1619 cb_view_t *tmp;
1621 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1622 {
1623 WARNING ("bind plugin: `View' blocks need exactly one string argument.");
1624 return (-1);
1625 }
1627 tmp = realloc (views, sizeof (*views) * (views_num + 1));
1628 if (tmp == NULL)
1629 {
1630 ERROR ("bind plugin: realloc failed.");
1631 return (-1);
1632 }
1633 views = tmp;
1634 tmp = views + views_num;
1636 memset (tmp, 0, sizeof (*tmp));
1637 tmp->qtypes = 1;
1638 tmp->resolver_stats = 1;
1639 tmp->cacherrsets = 1;
1640 tmp->zones = NULL;
1641 tmp->zones_num = 0;
1643 tmp->name = strdup (ci->values[0].value.string);
1644 if (tmp->name == NULL)
1645 {
1646 ERROR ("bind plugin: strdup failed.");
1647 sfree (views);
1648 return (-1);
1649 }
1651 for (int i = 0; i < ci->children_num; i++)
1652 {
1653 oconfig_item_t *child = ci->children + i;
1655 if (strcasecmp ("QTypes", child->key) == 0)
1656 bind_config_set_bool ("QTypes", &tmp->qtypes, child);
1657 else if (strcasecmp ("ResolverStats", child->key) == 0)
1658 bind_config_set_bool ("ResolverStats", &tmp->resolver_stats, child);
1659 else if (strcasecmp ("CacheRRSets", child->key) == 0)
1660 bind_config_set_bool ("CacheRRSets", &tmp->cacherrsets, child);
1661 else if (strcasecmp ("Zone", child->key) == 0)
1662 bind_config_add_view_zone (tmp, child);
1663 else
1664 {
1665 WARNING ("bind plugin: Unknown configuration option "
1666 "`%s' in view `%s' will be ignored.", child->key, tmp->name);
1667 }
1668 } /* for (i = 0; i < ci->children_num; i++) */
1670 views_num++;
1671 return (0);
1672 } /* }}} int bind_config_add_view */
1674 static int bind_config (oconfig_item_t *ci) /* {{{ */
1675 {
1676 for (int i = 0; i < ci->children_num; i++)
1677 {
1678 oconfig_item_t *child = ci->children + i;
1680 if (strcasecmp ("Url", child->key) == 0) {
1681 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING))
1682 {
1683 WARNING ("bind plugin: The `Url' option needs "
1684 "exactly one string argument.");
1685 return (-1);
1686 }
1688 sfree (url);
1689 url = strdup (child->values[0].value.string);
1690 } else if (strcasecmp ("OpCodes", child->key) == 0)
1691 bind_config_set_bool ("OpCodes", &global_opcodes, child);
1692 else if (strcasecmp ("QTypes", child->key) == 0)
1693 bind_config_set_bool ("QTypes", &global_qtypes, child);
1694 else if (strcasecmp ("ServerStats", child->key) == 0)
1695 bind_config_set_bool ("ServerStats", &global_server_stats, child);
1696 else if (strcasecmp ("ZoneMaintStats", child->key) == 0)
1697 bind_config_set_bool ("ZoneMaintStats", &global_zone_maint_stats, child);
1698 else if (strcasecmp ("ResolverStats", child->key) == 0)
1699 bind_config_set_bool ("ResolverStats", &global_resolver_stats, child);
1700 else if (strcasecmp ("MemoryStats", child->key) == 0)
1701 bind_config_set_bool ("MemoryStats", &global_memory_stats, child);
1702 else if (strcasecmp ("View", child->key) == 0)
1703 bind_config_add_view (child);
1704 else if (strcasecmp ("ParseTime", child->key) == 0)
1705 cf_util_get_boolean (child, &config_parse_time);
1706 else if (strcasecmp ("Timeout", child->key) == 0)
1707 cf_util_get_int (child, &timeout);
1708 else
1709 {
1710 WARNING ("bind plugin: Unknown configuration option "
1711 "`%s' will be ignored.", child->key);
1712 }
1713 }
1715 return (0);
1716 } /* }}} int bind_config */
1718 static int bind_init (void) /* {{{ */
1719 {
1720 if (curl != NULL)
1721 return (0);
1723 curl = curl_easy_init ();
1724 if (curl == NULL)
1725 {
1726 ERROR ("bind plugin: bind_init: curl_easy_init failed.");
1727 return (-1);
1728 }
1730 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L);
1731 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
1732 curl_easy_setopt (curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
1733 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, bind_curl_error);
1734 curl_easy_setopt (curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL);
1735 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
1736 curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
1737 #ifdef HAVE_CURLOPT_TIMEOUT_MS
1738 curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, (timeout >= 0) ?
1739 (long) timeout : (long) CDTIME_T_TO_MS(plugin_get_interval()));
1740 #endif
1743 return (0);
1744 } /* }}} int bind_init */
1746 static int bind_read (void) /* {{{ */
1747 {
1748 int status;
1750 if (curl == NULL)
1751 {
1752 ERROR ("bind plugin: I don't have a CURL object.");
1753 return (-1);
1754 }
1756 bind_buffer_fill = 0;
1757 if (curl_easy_perform (curl) != CURLE_OK)
1758 {
1759 ERROR ("bind plugin: curl_easy_perform failed: %s",
1760 bind_curl_error);
1761 return (-1);
1762 }
1764 status = bind_xml (bind_buffer);
1765 if (status != 0)
1766 return (-1);
1767 else
1768 return (0);
1769 } /* }}} int bind_read */
1771 static int bind_shutdown (void) /* {{{ */
1772 {
1773 if (curl != NULL)
1774 {
1775 curl_easy_cleanup (curl);
1776 curl = NULL;
1777 }
1779 return (0);
1780 } /* }}} int bind_shutdown */
1782 void module_register (void)
1783 {
1784 plugin_register_complex_config ("bind", bind_config);
1785 plugin_register_init ("bind", bind_init);
1786 plugin_register_read ("bind", bind_read);
1787 plugin_register_shutdown ("bind", bind_shutdown);
1788 } /* void module_register */
1790 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */