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 submit_counter(time_t ts, const char *plugin_instance, const char *type,
80 const char *type_instance, counter_t value)
83 value_list_t vl = VALUE_LIST_INIT;
86 values[0].counter = value;
90 vl.time = ts == 0 ? time(NULL) : ts;
91 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
92 sstrncpy(vl.plugin, "bind", sizeof(vl.plugin));
93 if (plugin_instance) {
94 sstrncpy(vl.plugin_instance, plugin_instance,
95 sizeof(vl.plugin_instance));
96 for (p = vl.type_instance; *p; p++)
97 if ((*p < 'a' || *p > 'z') && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9') && *p != '_')
100 sstrncpy(vl.type, type, sizeof(vl.type));
102 sstrncpy(vl.type_instance, type_instance,
103 sizeof(vl.type_instance));
104 for (p = vl.type_instance; *p; p++)
105 if ((*p < 'a' || *p > 'z') && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9') && *p != '_')
108 plugin_dispatch_values(&vl);
109 } /* void submit_counter */
111 static size_t bind_curl_callback (void *buf, size_t size, size_t nmemb,
114 size_t len = size * nmemb;
119 if ((bind_buffer_fill + len) >= bind_buffer_size)
123 temp = realloc(bind_buffer, bind_buffer_fill + len + 1);
126 ERROR ("bind plugin: realloc failed.");
130 bind_buffer_size = bind_buffer_fill + len + 1;
133 memcpy (bind_buffer + bind_buffer_fill, (char *) buf, len);
134 bind_buffer_fill += len;
135 bind_buffer[bind_buffer_fill] = 0;
138 } /* size_t bind_curl_callback */
140 static int bind_xml_read_counter (xmlDoc *doc, xmlNode *node,
141 counter_t *ret_value)
143 char *str_ptr, *end_ptr;
146 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
149 ERROR ("bind plugin: bind_xml_read_int64: xmlNodeListGetString failed.");
154 value = strtoll (str_ptr, &end_ptr, 10);
156 if (str_ptr == end_ptr || errno)
158 if (errno && value == LLONG_MIN)
159 ERROR ("bind plugin: bind_xml_read_int64: strtoll failed with underflow.");
160 else if (errno && value == LLONG_MAX)
161 ERROR ("bind plugin: bind_xml_read_int64: strtoll failed with overflow.");
163 ERROR ("bind plugin: bind_xml_read_int64: strtoll failed.");
169 } /* int bind_xml_read_counter */
171 static int bind_xml_read_timestamp (xmlDoc *doc, xmlNode *node,
177 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
180 ERROR ("bind plugin: bind_xml_read_int: xmlNodeListGetString failed.");
184 memset(&tm, 0, sizeof(tm));
185 p = strptime(str_ptr, "%Y-%m-%dT%T", &tm);
189 ERROR ("bind plugin: bind_xml_read_timestamp: strptime failed.");
193 *ret_value = timegm(&tm);
195 } /* int bind_xml_read_timestamp */
198 static int bind_xml_stats_v1(xmlDoc *doc, xmlXPathContext *xpathCtx, xmlNode *statsnode)
200 xmlXPathObjectPtr xpathObj = NULL;
204 xpathCtx->node = statsnode;
206 /* server/boot-time -- detect possible counter-resets
207 * Type: XML DateTime */
208 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/boot-time", xpathCtx)) == NULL) {
209 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-boottime");
211 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
212 char *boot_tm = (char *) xmlNodeListGetString (doc, xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode, 1);
213 if (srv_boot_ts == NULL || strcmp(srv_boot_ts, boot_tm) != 0) {
214 xmlFree(srv_boot_ts);
215 srv_boot_ts = boot_tm;
216 /* TODO: tell collectd that our counters got reset ... */
217 DEBUG ("bind plugin: Statv1: Server boot time: %s (%d nodes)", srv_boot_ts, xpathObj->nodesetval->nodeNr);
221 xmlXPathFreeObject(xpathObj);
223 /* server/current-time -- parse our time-stamp
224 * Type: XML DateTime */
225 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/current-time", xpathCtx)) == NULL) {
226 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-currenttime");
228 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
229 if (bind_xml_read_timestamp(doc, xpathObj->nodesetval->nodeTab[0], ¤t_time))
230 current_time = time(NULL);
232 DEBUG ("bind plugin: Statv1: Server current time: %ld (%d nodes)", current_time, xpathObj->nodesetval->nodeNr);
234 xmlXPathFreeObject(xpathObj);
236 /* requests/opcode -- [name] = [counter]
237 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ... */
239 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/requests/opcode", xpathCtx)) == NULL) {
240 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-currenttime");
242 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
243 xmlNode *name = NULL, *counter = NULL, *child;
244 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
245 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
247 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
249 if (name && counter) {
250 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
252 if (!bind_xml_read_counter(doc, counter, &val))
253 submit_counter(current_time, NULL, "dns_opcode", tinst, val);
257 DEBUG ("bind plugin: Statv1: Found %d entries for requests/opcode", xpathObj->nodesetval->nodeNr);
258 xmlXPathFreeObject(xpathObj);
261 /* queries-in/rdtype -- [name] = [counter]
262 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP, X25, PX, AAAA, LOC,
263 * SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY, SPF, TKEY, IXFR, AXFR, ANY, ..., Others */
264 if (use_rrqueries_in) {
265 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/queries-in/rdtype", xpathCtx)) == NULL) {
266 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-currenttime");
268 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
269 xmlNode *name = NULL, *counter = NULL, *child;
270 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
271 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
273 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
275 if (name && counter) {
276 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
278 if (!bind_xml_read_counter(doc, counter, &val))
279 submit_counter(current_time, NULL, "dns_qtype", tinst, val);
283 DEBUG ("bind plugin: Statv1: Found %d entries for queries-in/rdtype", xpathObj->nodesetval->nodeNr);
284 xmlXPathFreeObject(xpathObj);
287 /* nsstats -- [$name] = [$value] */
288 /* Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG, ReqSIG0, ReqBadSIG, ReqTCP,
289 * AuthQryRej, RecQryRej, XfrRej, UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
290 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral, QryNxrrset, QrySERVFAIL,
291 * QryFORMERR, QryNXDOMAIN, QryRecursion, QryDuplicate, QryDropped, QryFailure, XfrReqDone,
292 * UpdateReqFwd, UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail, UpdateBadPrereq */
293 if (use_updates || use_query_results || use_requests) {
294 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/nsstats", xpathCtx)) == NULL) {
295 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-nsstats");
297 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
301 for (child = xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode; child != NULL; child = child->next, n++)
302 if (child->type == XML_ELEMENT_NODE && !bind_xml_read_counter(doc, child, &val)) {
303 if (use_updates && (strncmp("Update", (char *)child->name, 6) == 0 || strcmp("XfrReqDone", (char *)child->name) == 0))
304 submit_counter(current_time, NULL, "dns_update", (char *)child->name, val);
305 else if (use_query_results && strncmp("Qry", (char *)child->name, 3) == 0)
306 submit_counter(current_time, NULL, "dns_rcode", (char *)child->name, val);
307 else if (use_query_results && (strncmp("Resp", (char *)child->name, 4) == 0 ||
308 strcmp("AuthQryRej", (char *)child->name) == 0 || strcmp("RecQryRej", (char *)child->name) == 0 ||
309 strcmp("XfrRej", (char *)child->name) == 0 || strcmp("TruncatedResp", (char *)child->name) == 0))
310 submit_counter(current_time, NULL, "dns_rcode", (char *)child->name, val);
311 else if (use_requests && strncmp("Req", (char *)child->name, 3) == 0)
312 submit_counter(current_time, NULL, "dns_request", (char *)child->name, val);
314 DEBUG ("bind plugin: Statv1: Found %d entries for %d nsstats", n, xpathObj->nodesetval->nodeNr);
316 xmlXPathFreeObject(xpathObj);
319 /* zonestats -- [$name] = [$value] */
320 /* Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej, SOAOutv4, SOAOutv6,
321 * AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6, XfrSuccess, XfrFail */
322 if (use_zone_maint) {
323 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/zonestats", xpathCtx)) == NULL) {
324 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-zonestats");
326 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
330 for (child = xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode; child != NULL; child = child->next, n++)
331 if (child->type == XML_ELEMENT_NODE) {
332 if (!bind_xml_read_counter(doc, child, &val))
333 submit_counter(current_time, NULL, "dns_zops", (char *)child->name, val);
335 DEBUG ("bind plugin: Statv1: Found %d entries for %d zonestats", n, xpathObj->nodesetval->nodeNr);
337 xmlXPathFreeObject(xpathObj);
340 /* resstats -- [$name] = [$value] */
341 /* Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL, FORMERR, OtherError,
342 * EDNS0Fail, Mismatch, Truncated, Lame, Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
343 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail */
345 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/resstats", xpathCtx)) == NULL) {
346 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-resstats");
348 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
352 for (child = xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode; child != NULL; child = child->next, n++)
353 if (child->type == XML_ELEMENT_NODE) {
354 if (!bind_xml_read_counter(doc, child, &val))
355 submit_counter(current_time, NULL, "dns_resolver", (char *)child->name, val);
357 DEBUG ("bind plugin: Statv1: Found %d entries for %d resstats", n, xpathObj->nodesetval->nodeNr);
359 xmlXPathFreeObject(xpathObj);
365 static int bind_xml_stats_v2(xmlDoc *doc, xmlXPathContext *xpathCtx, xmlNode *statsnode)
367 xmlXPathObjectPtr xpathObj = NULL;
371 xpathCtx->node = statsnode;
373 /* server/boot-time -- detect possible counter-resets
374 * Type: XML DateTime */
375 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/boot-time", xpathCtx)) == NULL) {
376 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-boottime");
378 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
379 char *boot_tm = (char *) xmlNodeListGetString (doc, xpathObj->nodesetval->nodeTab[0]->xmlChildrenNode, 1);
380 if (srv_boot_ts == NULL || strcmp(srv_boot_ts, boot_tm) != 0) {
381 xmlFree(srv_boot_ts);
382 srv_boot_ts = boot_tm;
383 /* TODO: tell collectd that our counters got reset ... */
384 DEBUG ("bind plugin: Statv2: Server boot time: %s (%d nodes)", srv_boot_ts, xpathObj->nodesetval->nodeNr);
388 xmlXPathFreeObject(xpathObj);
390 /* server/current-time -- parse our time-stamp
391 * Type: XML DateTime */
392 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/current-time", xpathCtx)) == NULL) {
393 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-currenttime");
395 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
396 if (bind_xml_read_timestamp(doc, xpathObj->nodesetval->nodeTab[0], ¤t_time))
397 current_time = time(NULL);
399 DEBUG ("bind plugin: Statv2: Server current time: %ld (%d nodes)", current_time, xpathObj->nodesetval->nodeNr);
401 xmlXPathFreeObject(xpathObj);
403 /* requests/opcode -- [name] = [counter]
404 * Variables: QUERY, ... */
406 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/requests/opcode", xpathCtx)) == NULL) {
407 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-opcode");
409 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
410 xmlNode *name = NULL, *counter = NULL, *child;
411 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
412 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
414 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
416 if (name && counter) {
417 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
419 if (!bind_xml_read_counter(doc, counter, &val))
420 submit_counter(current_time, NULL, "dns_opcode", tinst, val);
424 DEBUG ("bind plugin: Statv2: Found %d entries for requests/opcode", xpathObj->nodesetval->nodeNr);
425 xmlXPathFreeObject(xpathObj);
428 /* queries-in/rdtype -- [name] = [counter]
429 * Variables: A, NS, SOA, PTR, MX, TXT, SRV, ... */
430 if (use_rrqueries_in) {
431 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/queries-in/rdtype", xpathCtx)) == NULL) {
432 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-rdtype");
434 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
435 xmlNode *name = NULL, *counter = NULL, *child;
436 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
437 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
439 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
441 if (name && counter) {
442 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
444 if (!bind_xml_read_counter(doc, counter, &val))
445 submit_counter(current_time, NULL, "dns_qtype", tinst, val);
449 DEBUG ("bind plugin: Statv2: Found %d entries for queries-in/rdtype", xpathObj->nodesetval->nodeNr);
450 xmlXPathFreeObject(xpathObj);
453 /* nsstat -- [name] = [counter]
454 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG, ReqSIG0, ReqBadSIG, ReqTCP,
455 * AuthQryRej, RecQryRej, XfrRej, UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
456 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral, QryNxrrset, QrySERVFAIL,
457 * QryFORMERR, QryNXDOMAIN, QryRecursion, QryDuplicate, QryDropped, QryFailure, XfrReqDone,
458 * UpdateReqFwd, UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail, UpdateBadPrereq, */
459 if (use_updates || use_query_results || use_requests) {
460 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/nsstat", xpathCtx)) == NULL) {
461 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-nsstat");
463 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
464 xmlNode *name = NULL, *counter = NULL, *child;
465 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
466 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
468 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
470 if (name && counter) {
471 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
473 if (!bind_xml_read_counter(doc, counter, &val)) {
474 if (use_updates && (strncmp("Update", tinst, 6) == 0 || strcmp("XfrReqDone", tinst) == 0))
475 submit_counter(current_time, NULL, "dns_update", tinst, val);
476 else if (use_query_results && strncmp("Qry", tinst, 3) == 0)
477 submit_counter(current_time, NULL, "dns_rcode", tinst, val);
478 else if (use_query_results && (strncmp("Resp", tinst, 4) == 0 || strcmp("AuthQryRej", tinst) == 0 ||
479 strcmp("RecQryRej", tinst) == 0 || strcmp("XfrRej", tinst) == 0 || strcmp("TruncatedResp", tinst) == 0))
480 submit_counter(current_time, NULL, "dns_rcode", tinst, val);
481 else if (use_requests && strncmp("Req", tinst, 3) == 0)
482 submit_counter(current_time, NULL, "dns_request", tinst, val);
487 DEBUG ("bind plugin: Statv2: Found %d entries for nsstat", xpathObj->nodesetval->nodeNr);
488 xmlXPathFreeObject(xpathObj);
491 /* zonestat -- [name] = [counter]
492 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej, SOAOutv4, SOAOutv6,
493 * AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6, XfrSuccess, XfrFail */
494 if (use_zone_maint) {
495 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/zonestat", xpathCtx)) == NULL) {
496 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-zonestat");
498 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
499 xmlNode *name = NULL, *counter = NULL, *child;
500 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next)
501 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
503 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
505 if (name && counter) {
506 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
508 if (!bind_xml_read_counter(doc, counter, &val))
509 submit_counter(current_time, NULL, "dns_zops", tinst, val);
513 DEBUG ("bind plugin: Statv2: Found %d entries for zonestat", xpathObj->nodesetval->nodeNr);
514 xmlXPathFreeObject(xpathObj);
517 /* WARNING: per-view only: views/view/resstat, view-name as plugin-instance */
518 /* resstat -- [name] = [counter]
519 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL, FORMERR, OtherError,
520 * EDNS0Fail, Mismatch, Truncated, Lame, Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
521 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail */
523 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "views/view", xpathCtx)) == NULL) {
524 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-view");
526 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
528 xmlNode *name = NULL, *counter = NULL, *rchild, *child;
530 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL && zname == NULL; child = child->next)
531 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
532 zname = (char *) xmlNodeListGetString (doc, child->xmlChildrenNode, 1);
533 if (!zname || strcmp("_bind", zname) == 0)
534 continue; /* Unnamed zone?? */
535 /* else TODO: allow zone filtering */
536 for (rchild = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; rchild != NULL; rchild = rchild->next, n++)
537 if (xmlStrcmp (BAD_CAST "resstat", rchild->name) == 0) {
538 for (child = rchild->xmlChildrenNode; child != NULL; child = child->next)
539 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
541 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
543 if (name && counter) {
544 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
546 if (!bind_xml_read_counter(doc, counter, &val))
547 submit_counter(current_time, zname, "dns_resolver", tinst, val);
551 DEBUG ("bind plugin: Statv2: Found %d entries for view %s", n, zname);
554 xmlXPathFreeObject(xpathObj);
559 static int bind_xml (const char *data)
562 xmlXPathContextPtr xpathCtx = NULL;
563 xmlXPathObjectPtr xpathObj = NULL;
566 doc = xmlParseMemory (data, strlen (data));
568 ERROR ("bind plugin: xmlParseMemory failed.");
572 if ((xpathCtx = xmlXPathNewContext(doc)) == NULL) {
573 ERROR ("bind plugin: xmlXPathNewContext failed.");
576 /* Look for /isc/bind/statistics[@version='2.0'] */
577 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "/isc[@version='1.0']/bind/statistics[@version='1.0']", xpathCtx)) == NULL) {
578 ERROR("bind plugin: unable to evaluate XPath expression StatsV1");
580 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
581 /* We have Bind-9.5.x */
582 ret = bind_xml_stats_v1(doc, xpathCtx, xpathObj->nodesetval->nodeTab[0]);
585 xmlXPathFreeObject(xpathObj);
588 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "/isc[@version='1.0']/bind/statistics[@version='2.0']", xpathCtx)) == NULL) {
589 ERROR("bind plugin: unable to evaluate XPath expression StatsV2");
591 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
592 /* We have Bind-9.6.x */
593 ret = bind_xml_stats_v2(doc, xpathCtx, xpathObj->nodesetval->nodeTab[0]);
596 xmlXPathFreeObject(xpathObj);
599 ERROR("bind plugin: unable to find statistics in supported version.");
602 xmlXPathFreeObject(xpathObj);
603 xmlXPathFreeContext(xpathCtx);
608 static int config_set_str (char **var, const char *value)
616 if ((*var = strdup (value)) == NULL)
620 } /* int config_set_str */
622 static int config_set_bool (bool *var, const char *value)
624 if ((strcasecmp ("true", value) == 0)
625 || (strcasecmp ("yes", value) == 0)
626 || (strcasecmp ("on", value) == 0))
628 else if ((strcasecmp ("false", value) == 0)
629 || (strcasecmp ("no", value) == 0)
630 || (strcasecmp ("off", value) == 0))
635 } /* int config_set_bool */
637 static int bind_config (const char *key, const char *value)
639 if (strcasecmp (key, "URL") == 0)
640 return (config_set_str (&url, value));
641 else if (strcasecmp (key, "RRQueriesIn") == 0)
642 return (config_set_bool (&use_rrqueries_in, value));
643 else if (strcasecmp (key, "Requests") == 0)
644 return (config_set_bool (&use_requests, value));
645 else if (strcasecmp (key, "QueryResults") == 0)
646 return (config_set_bool (&use_query_results, value));
647 else if (strcasecmp (key, "Updates") == 0)
648 return (config_set_bool (&use_updates, value));
649 else if (strcasecmp (key, "ZoneMaintenance") == 0)
650 return (config_set_bool (&use_zone_maint, value));
651 else if (strcasecmp (key, "Resolver") == 0)
652 return (config_set_bool (&use_resolver, value));
655 } /* int bind_config */
657 static int bind_init (void)
661 WARNING ("bind plugin: bind_init: No URL configured, "
662 "returning an error.");
667 curl_easy_cleanup (curl);
669 if ((curl = curl_easy_init ()) == NULL)
671 ERROR ("bind plugin: bind_init: curl_easy_init failed.");
675 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
676 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
677 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, bind_curl_error);
678 curl_easy_setopt (curl, CURLOPT_URL, url);
680 } /* int bind_init */
682 static int bind_read (void)
688 ERROR ("bind plugin: I don't have a CURL object.");
694 ERROR ("bind plugin: No URL has been configured.");
698 bind_buffer_fill = 0;
699 if (curl_easy_perform (curl) != 0)
701 ERROR ("bind plugin: curl_easy_perform failed: %s",
706 status = bind_xml (bind_buffer);
711 } /* int bind_read */
713 void module_register (void)
715 plugin_register_config ("bind", bind_config, config_keys, config_keys_num);
716 plugin_register_init ("bind", bind_init);
717 plugin_register_read ("bind", bind_read);
718 } /* void module_register */
720 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */