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