2 * collectd - src/bind.c
3 * Copyright (C) 2009 Bruno Prémont
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Bruno Prémont <bonbons at linux-vserver.org>
22 /* Set to C99 and POSIX code */
23 #ifndef _ISOC99_SOURCE
24 # define _ISOC99_SOURCE
27 # define _POSIX_SOURCE
29 #ifndef _POSIX_C_SOURCE
30 # define _POSIX_C_SOURCE 200112L
36 # define _XOPEN_SOURCE 600
45 #include "configfile.h"
47 #include <curl/curl.h>
48 #include <libxml/parser.h>
49 #include <libxml/xpath.h>
51 static char *url = NULL;
52 static bool use_rrqueries_in = 1;
53 static bool use_requests = 1;
54 static bool use_query_results = 1;
55 static bool use_updates = 1;
56 static bool use_zone_maint = 1;
57 static bool use_resolver = 1;
58 static char *srv_boot_ts = NULL;
60 static CURL *curl = NULL;
62 static char *bind_buffer = NULL;
63 static size_t bind_buffer_size = 0;
64 static size_t bind_buffer_fill = 0;
65 static char bind_curl_error[CURL_ERROR_SIZE];
67 static const char *config_keys[] =
77 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
79 static void remove_special (char *buffer, size_t buffer_size)
83 for (i = 0; i < buffer_size; i++)
87 if (!isalnum ((int) buffer[i]))
90 } /* void remove_special */
92 static void submit_counter(time_t ts, const char *plugin_instance, const char *type,
93 const char *type_instance, counter_t value)
96 value_list_t vl = VALUE_LIST_INIT;
98 values[0].counter = value;
102 vl.time = ts == 0 ? time(NULL) : ts;
103 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
104 sstrncpy(vl.plugin, "bind", sizeof(vl.plugin));
105 if (plugin_instance) {
106 sstrncpy(vl.plugin_instance, plugin_instance,
107 sizeof(vl.plugin_instance));
108 remove_special (vl.plugin_instance, sizeof (vl.plugin_instance));
110 sstrncpy(vl.type, type, sizeof(vl.type));
112 sstrncpy(vl.type_instance, type_instance,
113 sizeof(vl.type_instance));
114 remove_special (vl.plugin_instance, sizeof (vl.plugin_instance));
116 plugin_dispatch_values(&vl);
117 } /* void submit_counter */
119 static size_t bind_curl_callback (void *buf, size_t size, size_t nmemb,
122 size_t len = size * nmemb;
127 if ((bind_buffer_fill + len) >= bind_buffer_size)
131 temp = realloc(bind_buffer, bind_buffer_fill + len + 1);
134 ERROR ("bind plugin: realloc failed.");
138 bind_buffer_size = bind_buffer_fill + len + 1;
141 memcpy (bind_buffer + bind_buffer_fill, (char *) buf, len);
142 bind_buffer_fill += len;
143 bind_buffer[bind_buffer_fill] = 0;
146 } /* size_t bind_curl_callback */
148 static int bind_xml_read_counter (xmlDoc *doc, xmlNode *node,
149 counter_t *ret_value)
151 char *str_ptr, *end_ptr;
154 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
157 ERROR ("bind plugin: bind_xml_read_int64: xmlNodeListGetString failed.");
162 value = strtoll (str_ptr, &end_ptr, 10);
164 if (str_ptr == end_ptr || errno)
166 if (errno && value == LLONG_MIN)
167 ERROR ("bind plugin: bind_xml_read_int64: strtoll failed with underflow.");
168 else if (errno && value == LLONG_MAX)
169 ERROR ("bind plugin: bind_xml_read_int64: strtoll failed with overflow.");
171 ERROR ("bind plugin: bind_xml_read_int64: strtoll failed.");
177 } /* int bind_xml_read_counter */
179 static int bind_xml_read_timestamp (xmlDoc *doc, xmlNode *node,
185 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
188 ERROR ("bind plugin: bind_xml_read_int: xmlNodeListGetString failed.");
192 memset(&tm, 0, sizeof(tm));
193 p = strptime(str_ptr, "%Y-%m-%dT%T", &tm);
197 ERROR ("bind plugin: bind_xml_read_timestamp: strptime failed.");
201 *ret_value = timegm(&tm);
203 } /* int bind_xml_read_timestamp */
206 static int bind_xml_stats_v1(xmlDoc *doc, xmlXPathContext *xpathCtx, xmlNode *statsnode)
208 xmlXPathObjectPtr xpathObj = NULL;
212 xpathCtx->node = statsnode;
214 /* server/boot-time -- detect possible counter-resets
215 * Type: XML DateTime */
216 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/boot-time", xpathCtx)) == NULL) {
217 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-boottime");
219 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
220 char *boot_tm = (char *) xmlNodeListGetString (doc, xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode, 1);
221 if (srv_boot_ts == NULL || strcmp(srv_boot_ts, boot_tm) != 0) {
222 xmlFree(srv_boot_ts);
223 srv_boot_ts = boot_tm;
224 /* TODO: tell collectd that our counters got reset ... */
225 DEBUG ("bind plugin: Statv1: Server boot time: %s (%d nodes)", srv_boot_ts, xpathObj->nodesetval->nodeNr);
229 xmlXPathFreeObject(xpathObj);
231 /* server/current-time -- parse our time-stamp
232 * Type: XML DateTime */
233 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/current-time", xpathCtx)) == NULL) {
234 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-currenttime");
236 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
237 if (bind_xml_read_timestamp(doc, xpathObj->nodesetval->nodeTab[0], ¤t_time))
238 current_time = time(NULL);
240 DEBUG ("bind plugin: Statv1: Server current time: %ld (%d nodes)", current_time, xpathObj->nodesetval->nodeNr);
242 xmlXPathFreeObject(xpathObj);
244 /* requests/opcode -- [name] = [counter]
245 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ... */
247 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/requests/opcode", xpathCtx)) == NULL) {
248 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-currenttime");
250 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
251 xmlNode *name = NULL, *counter = NULL, *child;
252 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
253 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
255 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
257 if (name && counter) {
258 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
260 if (!bind_xml_read_counter(doc, counter, &val))
261 submit_counter(current_time, NULL, "dns_opcode", tinst, val);
265 DEBUG ("bind plugin: Statv1: Found %d entries for requests/opcode", xpathObj->nodesetval->nodeNr);
266 xmlXPathFreeObject(xpathObj);
269 /* queries-in/rdtype -- [name] = [counter]
270 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP, X25, PX, AAAA, LOC,
271 * SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY, SPF, TKEY, IXFR, AXFR, ANY, ..., Others */
272 if (use_rrqueries_in) {
273 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/queries-in/rdtype", xpathCtx)) == NULL) {
274 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-currenttime");
276 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
277 xmlNode *name = NULL, *counter = NULL, *child;
278 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
279 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
281 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
283 if (name && counter) {
284 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
286 if (!bind_xml_read_counter(doc, counter, &val))
287 submit_counter(current_time, NULL, "dns_qtype", tinst, val);
291 DEBUG ("bind plugin: Statv1: Found %d entries for queries-in/rdtype", xpathObj->nodesetval->nodeNr);
292 xmlXPathFreeObject(xpathObj);
295 /* nsstats -- [$name] = [$value] */
296 /* Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG, ReqSIG0, ReqBadSIG, ReqTCP,
297 * AuthQryRej, RecQryRej, XfrRej, UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
298 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral, QryNxrrset, QrySERVFAIL,
299 * QryFORMERR, QryNXDOMAIN, QryRecursion, QryDuplicate, QryDropped, QryFailure, XfrReqDone,
300 * UpdateReqFwd, UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail, UpdateBadPrereq */
301 if (use_updates || use_query_results || use_requests) {
302 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/nsstats", xpathCtx)) == NULL) {
303 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-nsstats");
305 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
309 for (child = xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode; child != NULL; child = child->next, n++)
310 if (child->type == XML_ELEMENT_NODE && !bind_xml_read_counter(doc, child, &val)) {
311 if (use_updates && (strncmp("Update", (char *)child->name, 6) == 0 || strcmp("XfrReqDone", (char *)child->name) == 0))
312 submit_counter(current_time, NULL, "dns_update", (char *)child->name, val);
313 else if (use_query_results && strncmp("Qry", (char *)child->name, 3) == 0)
314 submit_counter(current_time, NULL, "dns_rcode", (char *)child->name, val);
315 else if (use_query_results && (strncmp("Resp", (char *)child->name, 4) == 0 ||
316 strcmp("AuthQryRej", (char *)child->name) == 0 || strcmp("RecQryRej", (char *)child->name) == 0 ||
317 strcmp("XfrRej", (char *)child->name) == 0 || strcmp("TruncatedResp", (char *)child->name) == 0))
318 submit_counter(current_time, NULL, "dns_rcode", (char *)child->name, val);
319 else if (use_requests && strncmp("Req", (char *)child->name, 3) == 0)
320 submit_counter(current_time, NULL, "dns_request", (char *)child->name, val);
322 DEBUG ("bind plugin: Statv1: Found %d entries for %d nsstats", n, xpathObj->nodesetval->nodeNr);
324 xmlXPathFreeObject(xpathObj);
327 /* zonestats -- [$name] = [$value] */
328 /* Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej, SOAOutv4, SOAOutv6,
329 * AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6, XfrSuccess, XfrFail */
330 if (use_zone_maint) {
331 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/zonestats", xpathCtx)) == NULL) {
332 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-zonestats");
334 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
338 for (child = xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode; child != NULL; child = child->next, n++)
339 if (child->type == XML_ELEMENT_NODE) {
340 if (!bind_xml_read_counter(doc, child, &val))
341 submit_counter(current_time, NULL, "dns_zops", (char *)child->name, val);
343 DEBUG ("bind plugin: Statv1: Found %d entries for %d zonestats", n, xpathObj->nodesetval->nodeNr);
345 xmlXPathFreeObject(xpathObj);
348 /* resstats -- [$name] = [$value] */
349 /* Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL, FORMERR, OtherError,
350 * EDNS0Fail, Mismatch, Truncated, Lame, Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
351 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail */
353 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/resstats", xpathCtx)) == NULL) {
354 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-resstats");
356 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
360 for (child = xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode; child != NULL; child = child->next, n++)
361 if (child->type == XML_ELEMENT_NODE) {
362 if (!bind_xml_read_counter(doc, child, &val))
363 submit_counter(current_time, NULL, "dns_resolver", (char *)child->name, val);
365 DEBUG ("bind plugin: Statv1: Found %d entries for %d resstats", n, xpathObj->nodesetval->nodeNr);
367 xmlXPathFreeObject(xpathObj);
370 } /* int bind_xml_stats_v1 */
373 static int bind_xml_stats_v2(xmlDoc *doc, xmlXPathContext *xpathCtx, xmlNode *statsnode)
375 xmlXPathObjectPtr xpathObj = NULL;
379 xpathCtx->node = statsnode;
381 /* server/boot-time -- detect possible counter-resets
382 * Type: XML DateTime */
383 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/boot-time", xpathCtx)) == NULL) {
384 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-boottime");
386 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
387 char *boot_tm = (char *) xmlNodeListGetString (doc, xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode, 1);
388 if (srv_boot_ts == NULL || strcmp(srv_boot_ts, boot_tm) != 0) {
389 xmlFree(srv_boot_ts);
390 srv_boot_ts = boot_tm;
391 /* TODO: tell collectd that our counters got reset ... */
392 DEBUG ("bind plugin: Statv2: Server boot time: %s (%d nodes)", srv_boot_ts, xpathObj->nodesetval->nodeNr);
396 xmlXPathFreeObject(xpathObj);
398 /* server/current-time -- parse our time-stamp
399 * Type: XML DateTime */
400 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/current-time", xpathCtx)) == NULL) {
401 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-currenttime");
403 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
404 if (bind_xml_read_timestamp(doc, xpathObj->nodesetval->nodeTab[0], ¤t_time))
405 current_time = time(NULL);
407 DEBUG ("bind plugin: Statv2: Server current time: %ld (%d nodes)", current_time, xpathObj->nodesetval->nodeNr);
409 xmlXPathFreeObject(xpathObj);
411 /* requests/opcode -- [name] = [counter]
412 * Variables: QUERY, ... */
414 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/requests/opcode", xpathCtx)) == NULL) {
415 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-opcode");
417 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
418 xmlNode *name = NULL, *counter = NULL, *child;
419 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
420 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
422 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
424 if (name && counter) {
425 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
427 if (!bind_xml_read_counter(doc, counter, &val))
428 submit_counter(current_time, NULL, "dns_opcode", tinst, val);
432 DEBUG ("bind plugin: Statv2: Found %d entries for requests/opcode", xpathObj->nodesetval->nodeNr);
433 xmlXPathFreeObject(xpathObj);
436 /* queries-in/rdtype -- [name] = [counter]
437 * Variables: A, NS, SOA, PTR, MX, TXT, SRV, ... */
438 if (use_rrqueries_in) {
439 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/queries-in/rdtype", xpathCtx)) == NULL) {
440 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-rdtype");
442 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
443 xmlNode *name = NULL, *counter = NULL, *child;
444 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
445 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
447 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
449 if (name && counter) {
450 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
452 if (!bind_xml_read_counter(doc, counter, &val))
453 submit_counter(current_time, NULL, "dns_qtype", tinst, val);
457 DEBUG ("bind plugin: Statv2: Found %d entries for queries-in/rdtype", xpathObj->nodesetval->nodeNr);
458 xmlXPathFreeObject(xpathObj);
461 /* nsstat -- [name] = [counter]
462 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG, ReqSIG0, ReqBadSIG, ReqTCP,
463 * AuthQryRej, RecQryRej, XfrRej, UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
464 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral, QryNxrrset, QrySERVFAIL,
465 * QryFORMERR, QryNXDOMAIN, QryRecursion, QryDuplicate, QryDropped, QryFailure, XfrReqDone,
466 * UpdateReqFwd, UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail, UpdateBadPrereq, */
467 if (use_updates || use_query_results || use_requests) {
468 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/nsstat", xpathCtx)) == NULL) {
469 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-nsstat");
471 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
472 xmlNode *name = NULL, *counter = NULL, *child;
473 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
474 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
476 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
478 if (name && counter) {
479 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
481 if (!bind_xml_read_counter(doc, counter, &val)) {
482 if (use_updates && (strncmp("Update", tinst, 6) == 0 || strcmp("XfrReqDone", tinst) == 0))
483 submit_counter(current_time, NULL, "dns_update", tinst, val);
484 else if (use_query_results && strncmp("Qry", tinst, 3) == 0)
485 submit_counter(current_time, NULL, "dns_rcode", tinst, val);
486 else if (use_query_results && (strncmp("Resp", tinst, 4) == 0 || strcmp("AuthQryRej", tinst) == 0 ||
487 strcmp("RecQryRej", tinst) == 0 || strcmp("XfrRej", tinst) == 0 || strcmp("TruncatedResp", tinst) == 0))
488 submit_counter(current_time, NULL, "dns_rcode", tinst, val);
489 else if (use_requests && strncmp("Req", tinst, 3) == 0)
490 submit_counter(current_time, NULL, "dns_request", tinst, val);
495 DEBUG ("bind plugin: Statv2: Found %d entries for nsstat", xpathObj->nodesetval->nodeNr);
496 xmlXPathFreeObject(xpathObj);
499 /* zonestat -- [name] = [counter]
500 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej, SOAOutv4, SOAOutv6,
501 * AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6, XfrSuccess, XfrFail */
502 if (use_zone_maint) {
503 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/zonestat", xpathCtx)) == NULL) {
504 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-zonestat");
506 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
507 xmlNode *name = NULL, *counter = NULL, *child;
508 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
509 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
511 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
513 if (name && counter) {
514 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
516 if (!bind_xml_read_counter(doc, counter, &val))
517 submit_counter(current_time, NULL, "dns_zops", tinst, val);
521 DEBUG ("bind plugin: Statv2: Found %d entries for zonestat", xpathObj->nodesetval->nodeNr);
522 xmlXPathFreeObject(xpathObj);
525 /* WARNING: per-view only: views/view/resstat, view-name as plugin-instance */
526 /* resstat -- [name] = [counter]
527 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL, FORMERR, OtherError,
528 * EDNS0Fail, Mismatch, Truncated, Lame, Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
529 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail */
531 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "views/view", xpathCtx)) == NULL) {
532 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-view");
534 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
536 xmlNode *name = NULL, *counter = NULL, *rchild, *child;
538 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL && zname == NULL; child = child->next)
539 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
540 zname = (char *) xmlNodeListGetString (doc, child->xmlChildrenNode, 1);
541 if (!zname || strcmp("_bind", zname) == 0)
542 continue; /* Unnamed zone?? */
543 /* else TODO: allow zone filtering */
544 for (rchild = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; rchild != NULL; rchild = rchild->next, n++)
545 if (xmlStrcmp (BAD_CAST "resstat", rchild->name) == 0) {
546 for (child = rchild->xmlChildrenNode; child != NULL; child = child->next)
547 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
549 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
551 if (name && counter) {
552 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
554 if (!bind_xml_read_counter(doc, counter, &val))
555 submit_counter(current_time, zname, "dns_resolver", tinst, val);
559 DEBUG ("bind plugin: Statv2: Found %d entries for view %s", n, zname);
562 xmlXPathFreeObject(xpathObj);
565 } /* int bind_xml_stats_v2 */
567 static int bind_xml (const char *data)
570 xmlXPathContextPtr xpathCtx = NULL;
571 xmlXPathObjectPtr xpathObj = NULL;
576 doc = xmlParseMemory (data, strlen (data));
578 ERROR ("bind plugin: xmlParseMemory failed.");
582 if ((xpathCtx = xmlXPathNewContext(doc)) == NULL) {
583 ERROR ("bind plugin: xmlXPathNewContext failed.");
587 /* Look for /isc/bind/statistics[@version='1.0'] */
588 xpathObj = xmlXPathEvalExpression(BAD_CAST "/isc[@version='1.0']/bind/statistics[@version='1.0']", xpathCtx);
589 if (xpathObj == NULL)
591 ERROR("bind plugin: unable to evaluate XPath expression StatsV1");
594 else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0)
596 /* We have Bind-9.5.x */
597 ret = bind_xml_stats_v1(doc, xpathCtx, xpathObj->nodesetval->nodeTab[0]);
601 /* Not Bind-9.5.x. Let's clear up and try Bind-9.6.x instead. */
602 xmlXPathFreeObject(xpathObj);
605 /* Look for /isc/bind/statistics[@version='2.0'] */
606 xpathObj = xmlXPathEvalExpression(BAD_CAST "/isc[@version='1.0']/bind/statistics[@version='2.0']", xpathCtx);
607 if (xpathObj == NULL)
609 ERROR("bind plugin: unable to evaluate XPath expression StatsV2");
612 else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0)
614 /* We have Bind-9.6.x */
615 ret = bind_xml_stats_v2(doc, xpathCtx, xpathObj->nodesetval->nodeTab[0]);
619 ERROR("bind plugin: unable to find statistics in supported version.");
622 if (xpathObj != NULL)
623 xmlXPathFreeObject(xpathObj);
624 if (xpathCtx != NULL)
625 xmlXPathFreeContext(xpathCtx);
632 static int config_set_str (char **var, const char *value)
640 if ((*var = strdup (value)) == NULL)
644 } /* int config_set_str */
646 static int config_set_bool (bool *var, const char *value)
648 if ((strcasecmp ("true", value) == 0)
649 || (strcasecmp ("yes", value) == 0)
650 || (strcasecmp ("on", value) == 0))
652 else if ((strcasecmp ("false", value) == 0)
653 || (strcasecmp ("no", value) == 0)
654 || (strcasecmp ("off", value) == 0))
659 } /* int config_set_bool */
661 static int bind_config (const char *key, const char *value)
663 if (strcasecmp (key, "URL") == 0)
664 return (config_set_str (&url, value));
665 else if (strcasecmp (key, "RRQueriesIn") == 0)
666 return (config_set_bool (&use_rrqueries_in, value));
667 else if (strcasecmp (key, "Requests") == 0)
668 return (config_set_bool (&use_requests, value));
669 else if (strcasecmp (key, "QueryResults") == 0)
670 return (config_set_bool (&use_query_results, value));
671 else if (strcasecmp (key, "Updates") == 0)
672 return (config_set_bool (&use_updates, value));
673 else if (strcasecmp (key, "ZoneMaintenance") == 0)
674 return (config_set_bool (&use_zone_maint, value));
675 else if (strcasecmp (key, "Resolver") == 0)
676 return (config_set_bool (&use_resolver, value));
679 } /* int bind_config */
681 static int bind_init (void)
685 WARNING ("bind plugin: bind_init: No URL configured, "
686 "returning an error.");
691 curl_easy_cleanup (curl);
693 if ((curl = curl_easy_init ()) == NULL)
695 ERROR ("bind plugin: bind_init: curl_easy_init failed.");
699 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
700 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
701 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, bind_curl_error);
702 curl_easy_setopt (curl, CURLOPT_URL, url);
704 } /* int bind_init */
706 static int bind_read (void)
712 ERROR ("bind plugin: I don't have a CURL object.");
718 ERROR ("bind plugin: No URL has been configured.");
722 bind_buffer_fill = 0;
723 if (curl_easy_perform (curl) != 0)
725 ERROR ("bind plugin: curl_easy_perform failed: %s",
730 status = bind_xml (bind_buffer);
735 } /* int bind_read */
737 void module_register (void)
739 plugin_register_config ("bind", bind_config, config_keys, config_keys_num);
740 plugin_register_init ("bind", bind_init);
741 plugin_register_read ("bind", bind_read);
742 } /* void module_register */
744 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */