43b61dd25c6233c8e9914300d8e5e05005c3b79d
1 /**
2 * collectd - src/bind.c
3 * Copyright (C) 2009 Bruno Prémont
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Bruno Prémont <bonbons at linux-vserver.org>
20 **/
22 /* Set to C99 and POSIX code */
23 #ifndef _ISOC99_SOURCE
24 # define _ISOC99_SOURCE
25 #endif
26 #ifndef _POSIX_SOURCE
27 # define _POSIX_SOURCE
28 #endif
29 #ifndef _POSIX_C_SOURCE
30 # define _POSIX_C_SOURCE 200112L
31 #endif
32 #ifndef _REENTRANT
33 # define _REENTRANT
34 #endif
35 #ifndef _XOPEN_SOURCE
36 # define _XOPEN_SOURCE 600
37 #endif
38 #ifndef _BSD_SOURCE
39 # define _BSD_SOURCE
40 #endif
42 #include "collectd.h"
43 #include "common.h"
44 #include "plugin.h"
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[] =
68 {
69 "URL",
70 "RRQueriesIn",
71 "Requests",
72 "QueryResults",
73 "Updates",
74 "ZoneMaintenance",
75 "Resolver"
76 };
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)
81 {
82 value_t values[1];
83 value_list_t vl = VALUE_LIST_INIT;
84 char *p;
86 values[0].counter = value;
88 vl.values = values;
89 vl.values_len = 1;
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 != '_')
98 *p = '_';
99 }
100 sstrncpy(vl.type, type, sizeof(vl.type));
101 if (type_instance) {
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 != '_')
106 *p = '_';
107 }
108 plugin_dispatch_values(&vl);
109 } /* void submit_counter */
111 static size_t bind_curl_callback (void *buf, size_t size, size_t nmemb,
112 void *stream)
113 {
114 size_t len = size * nmemb;
116 if (len <= 0)
117 return (len);
119 if ((bind_buffer_fill + len) >= bind_buffer_size)
120 {
121 char *temp;
123 temp = realloc(bind_buffer, bind_buffer_fill + len + 1);
124 if (temp == NULL)
125 {
126 ERROR ("bind plugin: realloc failed.");
127 return (0);
128 }
129 bind_buffer = temp;
130 bind_buffer_size = bind_buffer_fill + len + 1;
131 }
133 memcpy (bind_buffer + bind_buffer_fill, (char *) buf, len);
134 bind_buffer_fill += len;
135 bind_buffer[bind_buffer_fill] = 0;
137 return (len);
138 } /* size_t bind_curl_callback */
140 static int bind_xml_read_counter (xmlDoc *doc, xmlNode *node,
141 counter_t *ret_value)
142 {
143 char *str_ptr, *end_ptr;
144 long long int value;
146 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
147 if (str_ptr == NULL)
148 {
149 ERROR ("bind plugin: bind_xml_read_int64: xmlNodeListGetString failed.");
150 return (-1);
151 }
153 errno = 0;
154 value = strtoll (str_ptr, &end_ptr, 10);
155 xmlFree(str_ptr);
156 if (str_ptr == end_ptr || errno)
157 {
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.");
162 else
163 ERROR ("bind plugin: bind_xml_read_int64: strtoll failed.");
164 return (-1);
165 }
167 *ret_value = value;
168 return (0);
169 } /* int bind_xml_read_counter */
171 static int bind_xml_read_timestamp (xmlDoc *doc, xmlNode *node,
172 time_t *ret_value)
173 {
174 char *str_ptr, *p;
175 struct tm tm;
177 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
178 if (str_ptr == NULL)
179 {
180 ERROR ("bind plugin: bind_xml_read_int: xmlNodeListGetString failed.");
181 return (-1);
182 }
184 memset(&tm, 0, sizeof(tm));
185 p = strptime(str_ptr, "%Y-%m-%dT%T", &tm);
186 xmlFree(str_ptr);
187 if (p == NULL)
188 {
189 ERROR ("bind plugin: bind_xml_read_timestamp: strptime failed.");
190 return (-1);
191 }
193 *ret_value = timegm(&tm);
194 return (0);
195 } /* int bind_xml_read_timestamp */
197 /* Bind 9.5.x */
198 static int bind_xml_stats_v1(xmlDoc *doc, xmlXPathContext *xpathCtx, xmlNode *statsnode)
199 {
200 xmlXPathObjectPtr xpathObj = NULL;
201 time_t current_time;
202 int i;
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");
210 return -1;
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);
218 } else
219 xmlFree(boot_tm);
220 }
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");
227 return -1;
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);
231 else
232 DEBUG ("bind plugin: Statv1: Server current time: %ld (%d nodes)", current_time, xpathObj->nodesetval->nodeNr);
233 }
234 xmlXPathFreeObject(xpathObj);
236 /* requests/opcode -- [name] = [counter]
237 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ... */
238 if (use_requests) {
239 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/requests/opcode", xpathCtx)) == NULL) {
240 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-currenttime");
241 return -1;
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)
246 name = child;
247 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
248 counter = child;
249 if (name && counter) {
250 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
251 counter_t val;
252 if (!bind_xml_read_counter(doc, counter, &val))
253 submit_counter(current_time, NULL, "dns_opcode", tinst, val);
254 xmlFree(tinst);
255 }
256 }
257 DEBUG ("bind plugin: Statv1: Found %d entries for requests/opcode", xpathObj->nodesetval->nodeNr);
258 xmlXPathFreeObject(xpathObj);
259 }
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");
267 return -1;
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)
272 name = child;
273 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
274 counter = child;
275 if (name && counter) {
276 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
277 counter_t val;
278 if (!bind_xml_read_counter(doc, counter, &val))
279 submit_counter(current_time, NULL, "dns_qtype", tinst, val);
280 xmlFree(tinst);
281 }
282 }
283 DEBUG ("bind plugin: Statv1: Found %d entries for queries-in/rdtype", xpathObj->nodesetval->nodeNr);
284 xmlXPathFreeObject(xpathObj);
285 }
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");
296 return -1;
297 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
298 xmlNode *child;
299 counter_t val;
300 int n = 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);
313 }
314 DEBUG ("bind plugin: Statv1: Found %d entries for %d nsstats", n, xpathObj->nodesetval->nodeNr);
315 }
316 xmlXPathFreeObject(xpathObj);
317 }
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");
325 return -1;
326 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
327 xmlNode *child;
328 counter_t val;
329 int n = 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);
334 }
335 DEBUG ("bind plugin: Statv1: Found %d entries for %d zonestats", n, xpathObj->nodesetval->nodeNr);
336 }
337 xmlXPathFreeObject(xpathObj);
338 }
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 */
344 if (use_resolver) {
345 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/resstats", xpathCtx)) == NULL) {
346 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-resstats");
347 return -1;
348 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
349 xmlNode *child;
350 counter_t val;
351 int n = 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);
356 }
357 DEBUG ("bind plugin: Statv1: Found %d entries for %d resstats", n, xpathObj->nodesetval->nodeNr);
358 }
359 xmlXPathFreeObject(xpathObj);
360 }
361 return 0;
362 }
364 /* Bind 9.6.x */
365 static int bind_xml_stats_v2(xmlDoc *doc, xmlXPathContext *xpathCtx, xmlNode *statsnode)
366 {
367 xmlXPathObjectPtr xpathObj = NULL;
368 time_t current_time;
369 int i;
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");
377 return -1;
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);
385 } else
386 xmlFree(boot_tm);
387 }
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");
394 return -1;
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);
398 else
399 DEBUG ("bind plugin: Statv2: Server current time: %ld (%d nodes)", current_time, xpathObj->nodesetval->nodeNr);
400 }
401 xmlXPathFreeObject(xpathObj);
403 /* requests/opcode -- [name] = [counter]
404 * Variables: QUERY, ... */
405 if (use_requests) {
406 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/requests/opcode", xpathCtx)) == NULL) {
407 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-opcode");
408 return -1;
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)
413 name = child;
414 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
415 counter = child;
416 if (name && counter) {
417 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
418 counter_t val;
419 if (!bind_xml_read_counter(doc, counter, &val))
420 submit_counter(current_time, NULL, "dns_opcode", tinst, val);
421 xmlFree(tinst);
422 }
423 }
424 DEBUG ("bind plugin: Statv2: Found %d entries for requests/opcode", xpathObj->nodesetval->nodeNr);
425 xmlXPathFreeObject(xpathObj);
426 }
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");
433 return -1;
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)
438 name = child;
439 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
440 counter = child;
441 if (name && counter) {
442 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
443 counter_t val;
444 if (!bind_xml_read_counter(doc, counter, &val))
445 submit_counter(current_time, NULL, "dns_qtype", tinst, val);
446 xmlFree(tinst);
447 }
448 }
449 DEBUG ("bind plugin: Statv2: Found %d entries for queries-in/rdtype", xpathObj->nodesetval->nodeNr);
450 xmlXPathFreeObject(xpathObj);
451 }
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");
462 return -1;
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)
467 name = child;
468 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
469 counter = child;
470 if (name && counter) {
471 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
472 counter_t val;
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);
483 }
484 xmlFree(tinst);
485 }
486 }
487 DEBUG ("bind plugin: Statv2: Found %d entries for nsstat", xpathObj->nodesetval->nodeNr);
488 xmlXPathFreeObject(xpathObj);
489 }
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");
497 return -1;
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)
502 name = child;
503 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
504 counter = child;
505 if (name && counter) {
506 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
507 counter_t val;
508 if (!bind_xml_read_counter(doc, counter, &val))
509 submit_counter(current_time, NULL, "dns_zops", tinst, val);
510 xmlFree(tinst);
511 }
512 }
513 DEBUG ("bind plugin: Statv2: Found %d entries for zonestat", xpathObj->nodesetval->nodeNr);
514 xmlXPathFreeObject(xpathObj);
515 }
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 */
522 if (use_resolver) {
523 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "views/view", xpathCtx)) == NULL) {
524 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-view");
525 return -1;
526 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
527 char *zname = NULL;
528 xmlNode *name = NULL, *counter = NULL, *rchild, *child;
529 int n = 0;
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)
540 name = child;
541 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
542 counter = child;
543 if (name && counter) {
544 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
545 counter_t val;
546 if (!bind_xml_read_counter(doc, counter, &val))
547 submit_counter(current_time, zname, "dns_resolver", tinst, val);
548 xmlFree(tinst);
549 }
550 }
551 DEBUG ("bind plugin: Statv2: Found %d entries for view %s", n, zname);
552 xmlFree(zname);
553 }
554 xmlXPathFreeObject(xpathObj);
555 }
556 return 0;
557 }
559 static int bind_xml (const char *data)
560 {
561 xmlDoc *doc = NULL;
562 xmlXPathContextPtr xpathCtx = NULL;
563 xmlXPathObjectPtr xpathObj = NULL;
564 int ret = -1;
566 doc = xmlParseMemory (data, strlen (data));
567 if (doc == NULL) {
568 ERROR ("bind plugin: xmlParseMemory failed.");
569 goto out;
570 }
572 if ((xpathCtx = xmlXPathNewContext(doc)) == NULL) {
573 ERROR ("bind plugin: xmlXPathNewContext failed.");
574 goto out;
575 }
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");
579 goto out;
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]);
583 goto out;
584 } else {
585 xmlXPathFreeObject(xpathObj);
586 xpathObj = NULL;
587 }
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");
590 goto out;
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]);
594 goto out;
595 } else {
596 xmlXPathFreeObject(xpathObj);
597 xpathObj = NULL;
598 }
599 ERROR("bind plugin: unable to find statistics in supported version.");
601 out:
602 xmlXPathFreeObject(xpathObj);
603 xmlXPathFreeContext(xpathCtx);
604 xmlFreeDoc (doc);
605 return (ret);
606 } /* int bind_xml */
608 static int config_set_str (char **var, const char *value)
609 {
610 if (*var != NULL)
611 {
612 free (*var);
613 *var = NULL;
614 }
616 if ((*var = strdup (value)) == NULL)
617 return (-1);
618 else
619 return (0);
620 } /* int config_set_str */
622 static int config_set_bool (bool *var, const char *value)
623 {
624 if ((strcasecmp ("true", value) == 0)
625 || (strcasecmp ("yes", value) == 0)
626 || (strcasecmp ("on", value) == 0))
627 *var = 1;
628 else if ((strcasecmp ("false", value) == 0)
629 || (strcasecmp ("no", value) == 0)
630 || (strcasecmp ("off", value) == 0))
631 *var = 0;
632 else
633 return -1;
634 return 0;
635 } /* int config_set_bool */
637 static int bind_config (const char *key, const char *value)
638 {
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));
653 else
654 return (-1);
655 } /* int bind_config */
657 static int bind_init (void)
658 {
659 if (url == NULL)
660 {
661 WARNING ("bind plugin: bind_init: No URL configured, "
662 "returning an error.");
663 return (-1);
664 }
666 if (curl != NULL)
667 curl_easy_cleanup (curl);
669 if ((curl = curl_easy_init ()) == NULL)
670 {
671 ERROR ("bind plugin: bind_init: curl_easy_init failed.");
672 return (-1);
673 }
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);
679 return (0);
680 } /* int bind_init */
682 static int bind_read (void)
683 {
684 int status;
686 if (curl == NULL)
687 {
688 ERROR ("bind plugin: I don't have a CURL object.");
689 return (-1);
690 }
692 if (url == NULL)
693 {
694 ERROR ("bind plugin: No URL has been configured.");
695 return (-1);
696 }
698 bind_buffer_fill = 0;
699 if (curl_easy_perform (curl) != 0)
700 {
701 ERROR ("bind plugin: curl_easy_perform failed: %s",
702 bind_curl_error);
703 return (-1);
704 }
706 status = bind_xml (bind_buffer);
707 if (status != 0)
708 return (-1);
709 else
710 return (0);
711 } /* int bind_read */
713 void module_register (void)
714 {
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 : */