1 /**
2 * collectd - src/bind.c
3 * Copyright (C) 2009 Bruno Prémont
4 * Copyright (C) 2009 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 verplant.org>
22 **/
24 /* Set to C99 and POSIX code */
25 #ifndef _ISOC99_SOURCE
26 # define _ISOC99_SOURCE
27 #endif
28 #ifndef _POSIX_SOURCE
29 # define _POSIX_SOURCE
30 #endif
31 #ifndef _POSIX_C_SOURCE
32 # define _POSIX_C_SOURCE 200112L
33 #endif
34 #ifndef _REENTRANT
35 # define _REENTRANT
36 #endif
37 #ifndef _XOPEN_SOURCE
38 # define _XOPEN_SOURCE 600
39 #endif
40 #ifndef _BSD_SOURCE
41 # define _BSD_SOURCE
42 #endif
44 #include "collectd.h"
45 #include "common.h"
46 #include "plugin.h"
47 #include "configfile.h"
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, counter_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 static char *url = NULL;
102 static int global_opcodes = 1;
103 static int global_qtypes = 1;
104 static int global_server_stats = 1;
105 static int global_zone_maint_stats = 1;
106 static int global_resolver_stats = 0;
107 static int global_memory_stats = 1;
109 static cb_view_t *views = NULL;
110 static size_t views_num = 0;
112 static CURL *curl = NULL;
114 static char *bind_buffer = NULL;
115 static size_t bind_buffer_size = 0;
116 static size_t bind_buffer_fill = 0;
117 static char bind_curl_error[CURL_ERROR_SIZE];
119 /* Translation table for the `nsstats' values. */
120 static const translation_info_t nsstats_translation_table[] = /* {{{ */
121 {
122 /* Requests */
123 { "Requestv4", "dns_request", "IPv4" },
124 { "Requestv6", "dns_request", "IPv6" },
125 { "ReqEdns0", "dns_request", "EDNS0" },
126 { "ReqBadEDNSVer", "dns_request", "BadEDNSVer" },
127 { "ReqTSIG", "dns_request", "TSIG" },
128 { "ReqSIG0", "dns_request", "SIG0" },
129 { "ReqBadSIG", "dns_request", "BadSIG" },
130 { "ReqTCP", "dns_request", "TCP" },
131 /* Rejects */
132 { "AuthQryRej", "dns_reject", "authorative" },
133 { "RecQryRej", "dns_reject", "recursive" },
134 { "XfrRej", "dns_reject", "transer" },
135 { "UpdateRej", "dns_reject", "update" },
136 /* Responses */
137 { "Response", "dns_response", "normal" },
138 { "TruncatedResp", "dns_response", "truncated" },
139 { "RespEDNS0", "dns_response", "EDNS0" },
140 { "RespTSIG", "dns_response", "TSIG" },
141 { "RespSIG0", "dns_response", "SIG0" },
142 /* Queries */
143 { "QryAuthAns", "dns_query", "authorative" },
144 { "QryNoauthAns", "dns_query", "nonauth" },
145 { "QryReferral", "dns_query", "referral" },
146 { "QryRecursion", "dns_query", "recursion" },
147 { "QryDuplicate", "dns_query", "dupliate" },
148 { "QryDropped", "dns_query", "dropped" },
149 { "QryFailure", "dns_query", "failure" },
150 /* Response codes */
151 { "QrySuccess", "dns_rcode", "tx-NOERROR" },
152 { "QryNxrrset", "dns_rcode", "tx-NXRRSET" },
153 { "QrySERVFAIL", "dns_rcode", "tx-SERVFAIL" },
154 { "QryFORMERR", "dns_rcode", "tx-FORMERR" },
155 { "QryNXDOMAIN", "dns_rcode", "tx-NXDOMAIN" }
156 #if 0
157 { "XfrReqDone", "type", "type_instance" },
158 { "UpdateReqFwd", "type", "type_instance" },
159 { "UpdateRespFwd", "type", "type_instance" },
160 { "UpdateFwdFail", "type", "type_instance" },
161 { "UpdateDone", "type", "type_instance" },
162 { "UpdateFail", "type", "type_instance" },
163 { "UpdateBadPrereq", "type", "type_instance" },
164 #endif
165 };
166 static int nsstats_translation_table_length =
167 STATIC_ARRAY_SIZE (nsstats_translation_table);
168 /* }}} */
170 /* Translation table for the `zonestats' values. */
171 static const translation_info_t zonestats_translation_table[] = /* {{{ */
172 {
173 /* Notify's */
174 { "NotifyOutv4", "dns_notify", "tx-IPv4" },
175 { "NotifyOutv6", "dns_notify", "tx-IPv6" },
176 { "NotifyInv4", "dns_notify", "rx-IPv4" },
177 { "NotifyInv6", "dns_notify", "rx-IPv6" },
178 { "NotifyRej", "dns_notify", "rejected" },
179 /* SOA/AXFS/IXFS requests */
180 { "SOAOutv4", "dns_opcode", "SOA-IPv4" },
181 { "SOAOutv6", "dns_opcode", "SOA-IPv4" },
182 { "AXFRReqv4", "dns_opcode", "AXFR-IPv4" },
183 { "AXFRReqv6", "dns_opcode", "AXFR-IPv6" },
184 { "IXFRReqv4", "dns_opcode", "IXFR-IPv4" },
185 { "IXFRReqv6", "dns_opcode", "IXFR-IPv6" },
186 /* Domain transfers */
187 { "XfrSuccess", "dns_transfer", "success" },
188 { "XfrFail", "dns_transfer", "failure" }
189 };
190 static int zonestats_translation_table_length =
191 STATIC_ARRAY_SIZE (zonestats_translation_table);
192 /* }}} */
194 /* Translation table for the `resstats' values. */
195 static const translation_info_t resstats_translation_table[] = /* {{{ */
196 {
197 /* Generic resolver information */
198 { "Queryv4", "dns_query", "IPv4" },
199 { "Queryv6", "dns_query", "IPv6" },
200 { "Responsev4", "dns_response", "IPv4" },
201 { "Responsev6", "dns_response", "IPv6" },
202 /* Received response codes */
203 { "NXDOMAIN", "dns_rcode", "rx-NXDOMAIN" },
204 { "SERVFAIL", "dns_rcode", "rx-SERVFAIL" },
205 { "FORMERR", "dns_rcode", "rx-FORMERR" },
206 { "OtherError", "dns_rcode", "rx-OTHER" },
207 { "EDNS0Fail", "dns_rcode", "rx-EDNS0Fail"},
208 /* Received responses */
209 { "Mismatch", "dns_response", "mismatch" },
210 { "Truncated", "dns_response", "truncated" },
211 { "Lame", "dns_response", "lame" },
212 { "Retry", "dns_query", "retry" },
213 #if 0
214 { "GlueFetchv4", "type", "type_instance" },
215 { "GlueFetchv6", "type", "type_instance" },
216 { "GlueFetchv4Fail", "type", "type_instance" },
217 { "GlueFetchv6Fail", "type", "type_instance" },
218 #endif
219 /* DNSSEC information */
220 { "ValAttempt", "dns_resolver", "DNSSEC-attempt" },
221 { "ValOk", "dns_resolver", "DNSSEC-okay" },
222 { "ValNegOk", "dns_resolver", "DNSSEC-negokay" },
223 { "ValFail", "dns_resolver", "DNSSEC-fail" }
224 };
225 static int resstats_translation_table_length =
226 STATIC_ARRAY_SIZE (resstats_translation_table);
227 /* }}} */
229 /* Translation table for the `memory/summary' values. */
230 static const translation_info_t memsummary_translation_table[] = /* {{{ */
231 {
232 { "TotalUse", "memory", "TotalUse" },
233 { "InUse", "memory", "InUse" },
234 { "BlockSize", "memory", "BlockSize" },
235 { "ContextSize", "memory", "ContextSize" },
236 { "Lost", "memory", "Lost" }
237 };
238 static int memsummary_translation_table_length =
239 STATIC_ARRAY_SIZE (memsummary_translation_table);
240 /* }}} */
242 static void submit_counter(time_t ts, const char *plugin_instance, /* {{{ */
243 const char *type, const char *type_instance, counter_t value)
244 {
245 value_t values[1];
246 value_list_t vl = VALUE_LIST_INIT;
248 values[0].counter = value;
250 vl.values = values;
251 vl.values_len = 1;
252 vl.time = ts;
253 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
254 sstrncpy(vl.plugin, "bind", sizeof(vl.plugin));
255 if (plugin_instance) {
256 sstrncpy(vl.plugin_instance, plugin_instance,
257 sizeof(vl.plugin_instance));
258 replace_special (vl.plugin_instance, sizeof (vl.plugin_instance));
259 }
260 sstrncpy(vl.type, type, sizeof(vl.type));
261 if (type_instance) {
262 sstrncpy(vl.type_instance, type_instance,
263 sizeof(vl.type_instance));
264 replace_special (vl.plugin_instance, sizeof (vl.plugin_instance));
265 }
266 plugin_dispatch_values(&vl);
267 } /* }}} void submit_counter */
269 static size_t bind_curl_callback (void *buf, size_t size, /* {{{ */
270 size_t nmemb, void __attribute__((unused)) *stream)
271 {
272 size_t len = size * nmemb;
274 if (len <= 0)
275 return (len);
277 if ((bind_buffer_fill + len) >= bind_buffer_size)
278 {
279 char *temp;
281 temp = realloc(bind_buffer, bind_buffer_fill + len + 1);
282 if (temp == NULL)
283 {
284 ERROR ("bind plugin: realloc failed.");
285 return (0);
286 }
287 bind_buffer = temp;
288 bind_buffer_size = bind_buffer_fill + len + 1;
289 }
291 memcpy (bind_buffer + bind_buffer_fill, (char *) buf, len);
292 bind_buffer_fill += len;
293 bind_buffer[bind_buffer_fill] = 0;
295 return (len);
296 } /* }}} size_t bind_curl_callback */
298 /*
299 * Callback, that's called with a translation table.
300 * (Plugin instance is fixed, type and type instance come from lookup table.)
301 */
302 static int bind_xml_table_callback (const char *name, counter_t value, /* {{{ */
303 time_t current_time, void *user_data)
304 {
305 translation_table_ptr_t *table = (translation_table_ptr_t *) user_data;
306 size_t i;
308 if (table == NULL)
309 return (-1);
311 for (i = 0; i < table->table_length; i++)
312 {
313 if (strcmp (table->table[i].xml_name, name) != 0)
314 continue;
316 submit_counter (current_time,
317 table->plugin_instance,
318 table->table[i].type,
319 table->table[i].type_instance,
320 value);
321 break;
322 }
324 return (0);
325 } /* }}} int bind_xml_table_callback */
327 /*
328 * Callback, that's used for lists.
329 * (Plugin instance and type are fixed, xml name is used as type instance.)
330 */
331 static int bind_xml_list_callback (const char *name, /* {{{ */
332 counter_t value, time_t current_time, void *user_data)
333 {
334 list_info_ptr_t *list_info = (list_info_ptr_t *) user_data;
336 if (list_info == NULL)
337 return (-1);
339 submit_counter (current_time,
340 list_info->plugin_instance,
341 list_info->type,
342 /* type instance = */ name,
343 value);
345 return (0);
346 } /* }}} int bind_xml_list_callback */
348 static int bind_xml_read_counter (xmlDoc *doc, xmlNode *node, /* {{{ */
349 counter_t *ret_value)
350 {
351 char *str_ptr, *end_ptr;
352 long long int value;
354 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
355 if (str_ptr == NULL)
356 {
357 ERROR ("bind plugin: bind_xml_read_counter: xmlNodeListGetString failed.");
358 return (-1);
359 }
361 errno = 0;
362 value = strtoll (str_ptr, &end_ptr, 10);
363 xmlFree(str_ptr);
364 if (str_ptr == end_ptr || errno)
365 {
366 if (errno && (value < 0))
367 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed with underflow.");
368 else if (errno && (value > 0))
369 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed with overflow.");
370 else
371 ERROR ("bind plugin: bind_xml_read_counter: strtoll failed.");
372 return (-1);
373 }
375 *ret_value = value;
376 return (0);
377 } /* }}} int bind_xml_read_counter */
379 static int bind_xml_read_timestamp (const char *xpath_expression, /* {{{ */
380 xmlDoc *doc, xmlXPathContext *xpathCtx, time_t *ret_value)
381 {
382 xmlXPathObject *xpathObj = NULL;
383 xmlNode *node;
384 char *str_ptr;
385 char *tmp;
386 struct tm tm;
388 xpathObj = xmlXPathEvalExpression (BAD_CAST xpath_expression, xpathCtx);
389 if (xpathObj == NULL)
390 {
391 ERROR ("bind plugin: Unable to evaluate XPath expression `%s'.",
392 xpath_expression);
393 return (-1);
394 }
396 if ((xpathObj->nodesetval == NULL) || (xpathObj->nodesetval->nodeNr < 1))
397 {
398 xmlXPathFreeObject (xpathObj);
399 return (-1);
400 }
402 if (xpathObj->nodesetval->nodeNr != 1)
403 {
404 NOTICE ("bind plugin: Evaluating the XPath expression `%s' returned "
405 "%i nodes. Only handling the first one.",
406 xpath_expression, xpathObj->nodesetval->nodeNr);
407 }
409 node = xpathObj->nodesetval->nodeTab[0];
411 if (node->xmlChildrenNode == NULL)
412 {
413 ERROR ("bind plugin: bind_xml_read_timestamp: "
414 "node->xmlChildrenNode == NULL");
415 xmlXPathFreeObject (xpathObj);
416 return (-1);
417 }
419 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
420 if (str_ptr == NULL)
421 {
422 ERROR ("bind plugin: bind_xml_read_timestamp: xmlNodeListGetString failed.");
423 xmlXPathFreeObject (xpathObj);
424 return (-1);
425 }
427 memset (&tm, 0, sizeof(tm));
428 tmp = strptime (str_ptr, "%Y-%m-%dT%T", &tm);
429 xmlFree(str_ptr);
430 if (tmp == NULL)
431 {
432 ERROR ("bind plugin: bind_xml_read_timestamp: strptime failed.");
433 xmlXPathFreeObject (xpathObj);
434 return (-1);
435 }
437 *ret_value = timegm(&tm);
439 xmlXPathFreeObject (xpathObj);
440 return (0);
441 } /* }}} int bind_xml_read_timestamp */
443 /*
444 * bind_parse_generic_name_value
445 *
446 * Reads statistics in the form:
447 * <foo>
448 * <name>QUERY</name>
449 * <value>123</name>
450 * </foo>
451 */
452 static int bind_parse_generic_name_value (const char *xpath_expression, /* {{{ */
453 list_callback_t list_callback,
454 void *user_data,
455 xmlDoc *doc, xmlXPathContext *xpathCtx,
456 time_t current_time)
457 {
458 xmlXPathObject *xpathObj = NULL;
459 int num_entries;
460 int i;
462 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
463 if (xpathObj == NULL)
464 {
465 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
466 xpath_expression);
467 return (-1);
468 }
470 num_entries = 0;
471 /* Iterate over all matching nodes. */
472 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
473 {
474 xmlNode *name_node = NULL;
475 xmlNode *counter = NULL;
476 xmlNode *child;
478 /* Iterate over all child nodes. */
479 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
480 child != NULL;
481 child = child->next)
482 {
483 if (child->type != XML_ELEMENT_NODE)
484 continue;
486 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
487 name_node = child;
488 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
489 counter = child;
490 }
492 if ((name_node != NULL) && (counter != NULL))
493 {
494 char *name = (char *) xmlNodeListGetString (doc,
495 name_node->xmlChildrenNode, 1);
496 counter_t value;
497 int status;
499 status = bind_xml_read_counter (doc, counter, &value);
500 if (status != 0)
501 continue;
503 status = (*list_callback) (name, value, current_time, user_data);
504 if (status == 0)
505 num_entries++;
507 xmlFree (name);
508 }
509 }
511 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
512 num_entries, (num_entries == 1) ? "entry" : "entries",
513 xpath_expression);
515 xmlXPathFreeObject(xpathObj);
517 return (0);
518 } /* }}} int bind_parse_generic_name_value */
520 /*
521 * bind_parse_generic_value_list
522 *
523 * Reads statistics in the form:
524 * <foo>
525 * <name0>123</name0>
526 * <name1>234</name1>
527 * <name2>345</name2>
528 * :
529 * </foo>
530 */
531 static int bind_parse_generic_value_list (const char *xpath_expression, /* {{{ */
532 list_callback_t list_callback,
533 void *user_data,
534 xmlDoc *doc, xmlXPathContext *xpathCtx,
535 time_t current_time)
536 {
537 xmlXPathObject *xpathObj = NULL;
538 int num_entries;
539 int i;
541 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
542 if (xpathObj == NULL)
543 {
544 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
545 xpath_expression);
546 return (-1);
547 }
549 num_entries = 0;
550 /* Iterate over all matching nodes. */
551 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
552 {
553 xmlNode *child;
555 /* Iterate over all child nodes. */
556 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
557 child != NULL;
558 child = child->next)
559 {
560 char *node_name;
561 counter_t value;
562 int status;
564 if (child->type != XML_ELEMENT_NODE)
565 continue;
567 node_name = (char *) child->name;
568 status = bind_xml_read_counter (doc, child, &value);
569 if (status != 0)
570 continue;
572 status = (*list_callback) (node_name, value, current_time, user_data);
573 if (status == 0)
574 num_entries++;
575 }
576 }
578 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
579 num_entries, (num_entries == 1) ? "entry" : "entries",
580 xpath_expression);
582 xmlXPathFreeObject(xpathObj);
584 return (0);
585 } /* }}} int bind_parse_generic_value_list */
587 static int bind_xml_stats_handle_zone (int version, xmlDoc *doc, /* {{{ */
588 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
589 time_t current_time)
590 {
591 xmlXPathObject *path_obj;
592 char *zone_name = NULL;
593 int i;
594 size_t j;
596 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
597 if (path_obj == NULL)
598 {
599 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
600 return (-1);
601 }
603 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
604 {
605 zone_name = (char *) xmlNodeListGetString (doc,
606 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
607 if (zone_name != NULL)
608 break;
609 }
611 if (zone_name == NULL)
612 {
613 ERROR ("bind plugin: Could not determine zone name.");
614 xmlXPathFreeObject (path_obj);
615 return (-1);
616 }
618 for (j = 0; j < view->zones_num; j++)
619 {
620 if (strcasecmp (zone_name, view->zones[j]) == 0)
621 break;
622 }
624 xmlFree (zone_name);
625 zone_name = NULL;
627 if (j >= views_num)
628 {
629 xmlXPathFreeObject (path_obj);
630 return (0);
631 }
633 zone_name = view->zones[j];
635 DEBUG ("bind plugin: bind_xml_stats_handle_zone: Found zone `%s'.",
636 zone_name);
638 { /* Parse the <counters> tag {{{ */
639 char plugin_instance[DATA_MAX_NAME_LEN];
640 translation_table_ptr_t table_ptr =
641 {
642 nsstats_translation_table,
643 nsstats_translation_table_length,
644 plugin_instance
645 };
647 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-zone-%s",
648 view->name, zone_name);
650 bind_parse_generic_value_list (/* xpath = */ "counters",
651 /* callback = */ bind_xml_table_callback,
652 /* user_data = */ &table_ptr,
653 doc, path_ctx, current_time);
654 } /* }}} */
656 xmlXPathFreeObject (path_obj);
658 return (0);
659 } /* }}} int bind_xml_stats_handle_zone */
661 static int bind_xml_stats_search_zones (int version, xmlDoc *doc, /* {{{ */
662 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
663 time_t current_time)
664 {
665 xmlXPathObject *zone_nodes = NULL;
666 xmlXPathContext *zone_path_context;
667 int i;
669 zone_path_context = xmlXPathNewContext (doc);
670 if (zone_path_context == NULL)
671 {
672 ERROR ("bind plugin: xmlXPathNewContext failed.");
673 return (-1);
674 }
676 zone_nodes = xmlXPathEvalExpression (BAD_CAST "zones/zone", path_ctx);
677 if (zone_nodes == NULL)
678 {
679 ERROR ("bind plugin: Cannot find any <view> tags.");
680 xmlXPathFreeContext (zone_path_context);
681 return (-1);
682 }
684 for (i = 0; i < zone_nodes->nodesetval->nodeNr; i++)
685 {
686 xmlNode *node;
688 node = zone_nodes->nodesetval->nodeTab[i];
689 assert (node != NULL);
691 zone_path_context->node = node;
693 bind_xml_stats_handle_zone (version, doc, zone_path_context, node, view,
694 current_time);
695 }
697 xmlXPathFreeObject (zone_nodes);
698 xmlXPathFreeContext (zone_path_context);
699 return (0);
700 } /* }}} int bind_xml_stats_search_zones */
702 static int bind_xml_stats_handle_view (int version, xmlDoc *doc, /* {{{ */
703 xmlXPathContext *path_ctx, xmlNode *node, time_t current_time)
704 {
705 xmlXPathObject *path_obj;
706 char *view_name = NULL;
707 cb_view_t *view;
708 int i;
709 size_t j;
711 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
712 if (path_obj == NULL)
713 {
714 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
715 return (-1);
716 }
718 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
719 {
720 view_name = (char *) xmlNodeListGetString (doc,
721 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
722 if (view_name != NULL)
723 break;
724 }
726 if (view_name == NULL)
727 {
728 ERROR ("bind plugin: Could not determine view name.");
729 xmlXPathFreeObject (path_obj);
730 return (-1);
731 }
733 for (j = 0; j < views_num; j++)
734 {
735 if (strcasecmp (view_name, views[j].name) == 0)
736 break;
737 }
739 xmlFree (view_name);
740 xmlXPathFreeObject (path_obj);
742 view_name = NULL;
743 path_obj = NULL;
745 if (j >= views_num)
746 return (0);
748 view = views + j;
750 DEBUG ("bind plugin: bind_xml_stats_handle_view: Found view `%s'.",
751 view->name);
753 if (view->qtypes != 0) /* {{{ */
754 {
755 char plugin_instance[DATA_MAX_NAME_LEN];
756 list_info_ptr_t list_info =
757 {
758 plugin_instance,
759 /* type = */ "dns_qtype"
760 };
762 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-qtypes",
763 view->name);
765 bind_parse_generic_name_value (/* xpath = */ "rdtype",
766 /* callback = */ bind_xml_list_callback,
767 /* user_data = */ &list_info,
768 doc, path_ctx, current_time);
769 } /* }}} */
771 if (view->resolver_stats != 0) /* {{{ */
772 {
773 char plugin_instance[DATA_MAX_NAME_LEN];
774 translation_table_ptr_t table_ptr =
775 {
776 resstats_translation_table,
777 resstats_translation_table_length,
778 plugin_instance
779 };
781 ssnprintf (plugin_instance, sizeof (plugin_instance),
782 "%s-resolver_stats", view->name);
784 bind_parse_generic_name_value ("resstat",
785 /* callback = */ bind_xml_table_callback,
786 /* user_data = */ &table_ptr,
787 doc, path_ctx, current_time);
788 } /* }}} */
790 if (view->cacherrsets != 0) /* {{{ */
791 {
792 char plugin_instance[DATA_MAX_NAME_LEN];
793 list_info_ptr_t list_info =
794 {
795 plugin_instance,
796 /* type = */ "dns_qtype"
797 };
799 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-cache_rr_sets",
800 view->name);
802 bind_parse_generic_name_value (/* xpath = */ "cache/rrset",
803 /* callback = */ bind_xml_list_callback,
804 /* user_data = */ &list_info,
805 doc, path_ctx, current_time);
806 } /* }}} */
808 if (view->zones_num > 0)
809 bind_xml_stats_search_zones (version, doc, path_ctx, node, view,
810 current_time);
812 return (0);
813 } /* }}} int bind_xml_stats_handle_view */
815 static int bind_xml_stats_search_views (int version, xmlDoc *doc, /* {{{ */
816 xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time)
817 {
818 xmlXPathObject *view_nodes = NULL;
819 xmlXPathContext *view_path_context;
820 int i;
822 view_path_context = xmlXPathNewContext (doc);
823 if (view_path_context == NULL)
824 {
825 ERROR ("bind plugin: xmlXPathNewContext failed.");
826 return (-1);
827 }
829 view_nodes = xmlXPathEvalExpression (BAD_CAST "views/view", xpathCtx);
830 if (view_nodes == NULL)
831 {
832 ERROR ("bind plugin: Cannot find any <view> tags.");
833 xmlXPathFreeContext (view_path_context);
834 return (-1);
835 }
837 for (i = 0; i < view_nodes->nodesetval->nodeNr; i++)
838 {
839 xmlNode *node;
841 node = view_nodes->nodesetval->nodeTab[i];
842 assert (node != NULL);
844 view_path_context->node = node;
846 bind_xml_stats_handle_view (version, doc, view_path_context, node,
847 current_time);
848 }
850 xmlXPathFreeObject (view_nodes);
851 xmlXPathFreeContext (view_path_context);
852 return (0);
853 } /* }}} int bind_xml_stats_search_views */
855 static int bind_xml_stats (int version, xmlDoc *doc, /* {{{ */
856 xmlXPathContext *xpathCtx, xmlNode *statsnode)
857 {
858 time_t current_time = 0;
859 int status;
861 xpathCtx->node = statsnode;
863 /* TODO: Check `server/boot-time' to recognize server restarts. */
865 status = bind_xml_read_timestamp ("server/current-time",
866 doc, xpathCtx, ¤t_time);
867 if (status != 0)
868 {
869 ERROR ("bind plugin: Reading `server/current-time' failed.");
870 return (-1);
871 }
872 DEBUG ("bind plugin: Current server time is %i.", (int) current_time);
874 /* XPath: server/requests/opcode
875 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
876 * Layout:
877 * <opcode>
878 * <name>A</name>
879 * <counter>1</counter>
880 * </opcode>
881 * :
882 */
883 if (global_opcodes != 0)
884 {
885 list_info_ptr_t list_info =
886 {
887 /* plugin instance = */ "global-opcodes",
888 /* type = */ "dns_opcode"
889 };
891 bind_parse_generic_name_value (/* xpath = */ "server/requests/opcode",
892 /* callback = */ bind_xml_list_callback,
893 /* user_data = */ &list_info,
894 doc, xpathCtx, current_time);
895 }
897 /* XPath: server/queries-in/rdtype
898 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
899 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
900 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
901 * Layout:
902 * <rdtype>
903 * <name>A</name>
904 * <counter>1</counter>
905 * </rdtype>
906 * :
907 */
908 if (global_qtypes != 0)
909 {
910 list_info_ptr_t list_info =
911 {
912 /* plugin instance = */ "global-qtypes",
913 /* type = */ "dns_qtype"
914 };
916 bind_parse_generic_name_value (/* xpath = */ "server/queries-in/rdtype",
917 /* callback = */ bind_xml_list_callback,
918 /* user_data = */ &list_info,
919 doc, xpathCtx, current_time);
920 }
922 /* XPath: server/nsstats, server/nsstat
923 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
924 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
925 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
926 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
927 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
928 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
929 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
930 * UpdateBadPrereq
931 * Layout v1:
932 * <nsstats>
933 * <Requestv4>1</Requestv4>
934 * <Requestv6>0</Requestv6>
935 * :
936 * </nsstats>
937 * Layout v2:
938 * <nsstat>
939 * <name>Requestv4</name>
940 * <counter>1</counter>
941 * </nsstat>
942 * <nsstat>
943 * <name>Requestv6</name>
944 * <counter>0</counter>
945 * </nsstat>
946 * :
947 */
948 if (global_server_stats)
949 {
950 translation_table_ptr_t table_ptr =
951 {
952 nsstats_translation_table,
953 nsstats_translation_table_length,
954 /* plugin_instance = */ "global-server_stats"
955 };
957 if (version == 1)
958 {
959 bind_parse_generic_value_list ("server/nsstats",
960 /* callback = */ bind_xml_table_callback,
961 /* user_data = */ &table_ptr,
962 doc, xpathCtx, current_time);
963 }
964 else
965 {
966 bind_parse_generic_name_value ("server/nsstat",
967 /* callback = */ bind_xml_table_callback,
968 /* user_data = */ &table_ptr,
969 doc, xpathCtx, current_time);
970 }
971 }
973 /* XPath: server/zonestats, server/zonestat
974 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
975 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
976 * XfrSuccess, XfrFail
977 * Layout v1:
978 * <zonestats>
979 * <NotifyOutv4>0</NotifyOutv4>
980 * <NotifyOutv6>0</NotifyOutv6>
981 * :
982 * </zonestats>
983 * Layout v2:
984 * <zonestat>
985 * <name>NotifyOutv4</name>
986 * <counter>0</counter>
987 * </zonestat>
988 * <zonestat>
989 * <name>NotifyOutv6</name>
990 * <counter>0</counter>
991 * </zonestat>
992 * :
993 */
994 if (global_zone_maint_stats)
995 {
996 translation_table_ptr_t table_ptr =
997 {
998 zonestats_translation_table,
999 zonestats_translation_table_length,
1000 /* plugin_instance = */ "global-zone_maint_stats"
1001 };
1003 if (version == 1)
1004 {
1005 bind_parse_generic_value_list ("server/zonestats",
1006 /* callback = */ bind_xml_table_callback,
1007 /* user_data = */ &table_ptr,
1008 doc, xpathCtx, current_time);
1009 }
1010 else
1011 {
1012 bind_parse_generic_name_value ("server/zonestat",
1013 /* callback = */ bind_xml_table_callback,
1014 /* user_data = */ &table_ptr,
1015 doc, xpathCtx, current_time);
1016 }
1017 }
1019 /* XPath: server/resstats
1020 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1021 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1022 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1023 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1024 * Layout v1:
1025 * <resstats>
1026 * <Queryv4>0</Queryv4>
1027 * <Queryv6>0</Queryv6>
1028 * :
1029 * </resstats>
1030 * Layout v2:
1031 * <resstat>
1032 * <name>Queryv4</name>
1033 * <counter>0</counter>
1034 * </resstat>
1035 * <resstat>
1036 * <name>Queryv6</name>
1037 * <counter>0</counter>
1038 * </resstat>
1039 * :
1040 */
1041 if (global_resolver_stats != 0)
1042 {
1043 translation_table_ptr_t table_ptr =
1044 {
1045 resstats_translation_table,
1046 resstats_translation_table_length,
1047 /* plugin_instance = */ "global-resolver_stats"
1048 };
1050 if (version == 1)
1051 {
1052 bind_parse_generic_value_list ("server/resstats",
1053 /* callback = */ bind_xml_table_callback,
1054 /* user_data = */ &table_ptr,
1055 doc, xpathCtx, current_time);
1056 }
1057 else
1058 {
1059 bind_parse_generic_name_value ("server/resstat",
1060 /* callback = */ bind_xml_table_callback,
1061 /* user_data = */ &table_ptr,
1062 doc, xpathCtx, current_time);
1063 }
1064 }
1066 /* XPath: memory/summary
1067 * Variables: TotalUse, InUse, BlockSize, ContextSize, Lost
1068 * Layout: v2:
1069 * <summary>
1070 * <TotalUse>6587096</TotalUse>
1071 * <InUse>1345424</InUse>
1072 * <BlockSize>5505024</BlockSize>
1073 * <ContextSize>3732456</ContextSize>
1074 * <Lost>0</Lost>
1075 * </summary>
1076 */
1077 if (global_memory_stats != 0)
1078 {
1079 translation_table_ptr_t table_ptr =
1080 {
1081 memsummary_translation_table,
1082 memsummary_translation_table_length,
1083 /* plugin_instance = */ "global-memory_stats"
1084 };
1086 bind_parse_generic_value_list ("memory/summary",
1087 /* callback = */ bind_xml_table_callback,
1088 /* user_data = */ &table_ptr,
1089 doc, xpathCtx, current_time);
1090 }
1092 if (views_num > 0)
1093 bind_xml_stats_search_views (version, doc, xpathCtx, statsnode,
1094 current_time);
1096 return 0;
1097 } /* }}} int bind_xml_stats */
1099 static int bind_xml (const char *data) /* {{{ */
1100 {
1101 xmlDoc *doc = NULL;
1102 xmlXPathContext *xpathCtx = NULL;
1103 xmlXPathObject *xpathObj = NULL;
1104 int ret = -1;
1105 int i;
1107 doc = xmlParseMemory (data, strlen (data));
1108 if (doc == NULL)
1109 {
1110 ERROR ("bind plugin: xmlParseMemory failed.");
1111 return (-1);
1112 }
1114 xpathCtx = xmlXPathNewContext (doc);
1115 if (xpathCtx == NULL)
1116 {
1117 ERROR ("bind plugin: xmlXPathNewContext failed.");
1118 xmlFreeDoc (doc);
1119 return (-1);
1120 }
1122 xpathObj = xmlXPathEvalExpression (BAD_CAST "/isc/bind/statistics", xpathCtx);
1123 if (xpathObj == NULL)
1124 {
1125 ERROR ("bind plugin: Cannot find the <statistics> tag.");
1126 xmlXPathFreeContext (xpathCtx);
1127 xmlFreeDoc (doc);
1128 return (-1);
1129 }
1130 else if (xpathObj->nodesetval == NULL)
1131 {
1132 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
1133 xmlXPathFreeObject (xpathObj);
1134 xmlXPathFreeContext (xpathCtx);
1135 xmlFreeDoc (doc);
1136 return (-1);
1137 }
1139 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
1140 {
1141 xmlNode *node;
1142 char *attr_version;
1143 int parsed_version = 0;
1145 node = xpathObj->nodesetval->nodeTab[i];
1146 assert (node != NULL);
1148 attr_version = (char *) xmlGetProp (node, BAD_CAST "version");
1149 if (attr_version == NULL)
1150 {
1151 NOTICE ("bind plugin: Found <statistics> tag doesn't have a "
1152 "`version' attribute.");
1153 continue;
1154 }
1155 DEBUG ("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1157 /* At the time this plugin was written, version "1.0" was used by
1158 * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
1159 * that "1.*" and "2.*" don't introduce structural changes, so we just
1160 * check for the first two characters here. */
1161 if (strncmp ("1.", attr_version, strlen ("1.")) == 0)
1162 parsed_version = 1;
1163 else if (strncmp ("2.", attr_version, strlen ("2.")) == 0)
1164 parsed_version = 2;
1165 else
1166 {
1167 /* TODO: Use the complaint mechanism here. */
1168 NOTICE ("bind plugin: Found <statistics> tag with version `%s'. "
1169 "Unfortunately I have no clue how to parse that. "
1170 "Please open a bug report for this.", attr_version);
1171 xmlFree (attr_version);
1172 continue;
1173 }
1175 ret = bind_xml_stats (parsed_version,
1176 doc, xpathCtx, node);
1178 xmlFree (attr_version);
1179 /* One <statistics> node ought to be enough. */
1180 break;
1181 }
1183 xmlXPathFreeObject (xpathObj);
1184 xmlXPathFreeContext (xpathCtx);
1185 xmlFreeDoc (doc);
1187 return (ret);
1188 } /* }}} int bind_xml */
1190 static int bind_config_set_bool (const char *name, int *var, /* {{{ */
1191 oconfig_item_t *ci)
1192 {
1193 if ((ci->values_num != 1) || ( ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1194 {
1195 WARNING ("bind plugin: The `%s' option needs "
1196 "exactly one boolean argument.", name);
1197 return (-1);
1198 }
1200 if (ci->values[0].value.boolean)
1201 *var = 1;
1202 else
1203 *var = 0;
1204 return 0;
1205 } /* }}} int bind_config_set_bool */
1207 static int bind_config_add_view_zone (cb_view_t *view, /* {{{ */
1208 oconfig_item_t *ci)
1209 {
1210 char **tmp;
1212 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1213 {
1214 WARNING ("bind plugin: The `Zone' option needs "
1215 "exactly one string argument.");
1216 return (-1);
1217 }
1219 tmp = (char **) realloc (view->zones,
1220 sizeof (char *) * (view->zones_num + 1));
1221 if (tmp == NULL)
1222 {
1223 ERROR ("bind plugin: realloc failed.");
1224 return (-1);
1225 }
1226 view->zones = tmp;
1228 view->zones[view->zones_num] = strdup (ci->values[0].value.string);
1229 if (view->zones[view->zones_num] == NULL)
1230 {
1231 ERROR ("bind plugin: strdup failed.");
1232 return (-1);
1233 }
1234 view->zones_num++;
1236 return (0);
1237 } /* }}} int bind_config_add_view_zone */
1239 static int bind_config_add_view (oconfig_item_t *ci) /* {{{ */
1240 {
1241 cb_view_t *tmp;
1242 int i;
1244 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1245 {
1246 WARNING ("bind plugin: `View' blocks need exactly one string argument.");
1247 return (-1);
1248 }
1250 tmp = (cb_view_t *) realloc (views, sizeof (*views) * (views_num + 1));
1251 if (tmp == NULL)
1252 {
1253 ERROR ("bind plugin: realloc failed.");
1254 return (-1);
1255 }
1256 views = tmp;
1257 tmp = views + views_num;
1259 memset (tmp, 0, sizeof (*tmp));
1260 tmp->qtypes = 1;
1261 tmp->resolver_stats = 1;
1262 tmp->cacherrsets = 1;
1263 tmp->zones = NULL;
1264 tmp->zones_num = 0;
1266 tmp->name = strdup (ci->values[0].value.string);
1267 if (tmp->name == NULL)
1268 {
1269 ERROR ("bind plugin: strdup failed.");
1270 free (tmp);
1271 return (-1);
1272 }
1274 for (i = 0; i < ci->children_num; i++)
1275 {
1276 oconfig_item_t *child = ci->children + i;
1278 if (strcasecmp ("QTypes", child->key) == 0)
1279 bind_config_set_bool ("QTypes", &tmp->qtypes, child);
1280 else if (strcasecmp ("ResolverStats", child->key) == 0)
1281 bind_config_set_bool ("ResolverStats", &tmp->resolver_stats, child);
1282 else if (strcasecmp ("CacheRRSets", child->key) == 0)
1283 bind_config_set_bool ("CacheRRSets", &tmp->cacherrsets, child);
1284 else if (strcasecmp ("Zone", child->key) == 0)
1285 bind_config_add_view_zone (tmp, child);
1286 else
1287 {
1288 WARNING ("bind plugin: Unknown configuration option "
1289 "`%s' in view `%s' will be ignored.", child->key, tmp->name);
1290 }
1291 } /* for (i = 0; i < ci->children_num; i++) */
1293 views_num++;
1294 return (0);
1295 } /* }}} int bind_config_add_view */
1297 static int bind_config (oconfig_item_t *ci) /* {{{ */
1298 {
1299 int i;
1301 for (i = 0; i < ci->children_num; i++)
1302 {
1303 oconfig_item_t *child = ci->children + i;
1305 if (strcasecmp ("OpCodes", child->key) == 0)
1306 bind_config_set_bool ("OpCodes", &global_opcodes, child);
1307 else if (strcasecmp ("QTypes", child->key) == 0)
1308 bind_config_set_bool ("QTypes", &global_qtypes, child);
1309 else if (strcasecmp ("ServerStats", child->key) == 0)
1310 bind_config_set_bool ("ServerStats", &global_server_stats, child);
1311 else if (strcasecmp ("ZoneMaintStats", child->key) == 0)
1312 bind_config_set_bool ("ZoneMaintStats", &global_zone_maint_stats, child);
1313 else if (strcasecmp ("ResolverStats", child->key) == 0)
1314 bind_config_set_bool ("ResolverStats", &global_resolver_stats, child);
1315 else if (strcasecmp ("MemoryStats", child->key) == 0)
1316 bind_config_set_bool ("MemoryStats", &global_resolver_stats, child);
1317 else if (strcasecmp ("View", child->key) == 0)
1318 bind_config_add_view (child);
1319 else
1320 {
1321 WARNING ("bind plugin: Unknown configuration option "
1322 "`%s' will be ignored.", child->key);
1323 }
1324 }
1326 return (0);
1327 } /* }}} int bind_config */
1329 static int bind_init (void) /* {{{ */
1330 {
1331 if (curl != NULL)
1332 return (0);
1334 curl = curl_easy_init ();
1335 if (curl == NULL)
1336 {
1337 ERROR ("bind plugin: bind_init: curl_easy_init failed.");
1338 return (-1);
1339 }
1341 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
1342 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
1343 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, bind_curl_error);
1344 curl_easy_setopt (curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL);
1346 return (0);
1347 } /* }}} int bind_init */
1349 static int bind_read (void) /* {{{ */
1350 {
1351 int status;
1353 if (curl == NULL)
1354 {
1355 ERROR ("bind plugin: I don't have a CURL object.");
1356 return (-1);
1357 }
1359 bind_buffer_fill = 0;
1360 if (curl_easy_perform (curl) != 0)
1361 {
1362 ERROR ("bind plugin: curl_easy_perform failed: %s",
1363 bind_curl_error);
1364 return (-1);
1365 }
1367 status = bind_xml (bind_buffer);
1368 if (status != 0)
1369 return (-1);
1370 else
1371 return (0);
1372 } /* }}} int bind_read */
1374 static int bind_shutdown (void) /* {{{ */
1375 {
1376 if (curl != NULL)
1377 {
1378 curl_easy_cleanup (curl);
1379 curl = NULL;
1380 }
1382 return (0);
1383 } /* }}} int bind_shutdown */
1385 void module_register (void)
1386 {
1387 plugin_register_complex_config ("bind", bind_config);
1388 plugin_register_init ("bind", bind_init);
1389 plugin_register_read ("bind", bind_read);
1390 plugin_register_shutdown ("bind", bind_shutdown);
1391 } /* void module_register */
1393 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */