f09a08eb6938ab744165fdd37be4d00144a30fc4
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 remove_special (char *buffer, size_t buffer_size)
80 {
81 size_t i;
83 for (i = 0; i < buffer_size; i++)
84 {
85 if (buffer[i] == 0)
86 return;
87 if (!isalnum ((int) buffer[i]))
88 buffer[i] = '_';
89 }
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)
94 {
95 value_t values[1];
96 value_list_t vl = VALUE_LIST_INIT;
98 values[0].counter = value;
100 vl.values = values;
101 vl.values_len = 1;
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));
109 }
110 sstrncpy(vl.type, type, sizeof(vl.type));
111 if (type_instance) {
112 sstrncpy(vl.type_instance, type_instance,
113 sizeof(vl.type_instance));
114 remove_special (vl.plugin_instance, sizeof (vl.plugin_instance));
115 }
116 plugin_dispatch_values(&vl);
117 } /* void submit_counter */
119 static size_t bind_curl_callback (void *buf, size_t size, size_t nmemb,
120 void *stream)
121 {
122 size_t len = size * nmemb;
124 if (len <= 0)
125 return (len);
127 if ((bind_buffer_fill + len) >= bind_buffer_size)
128 {
129 char *temp;
131 temp = realloc(bind_buffer, bind_buffer_fill + len + 1);
132 if (temp == NULL)
133 {
134 ERROR ("bind plugin: realloc failed.");
135 return (0);
136 }
137 bind_buffer = temp;
138 bind_buffer_size = bind_buffer_fill + len + 1;
139 }
141 memcpy (bind_buffer + bind_buffer_fill, (char *) buf, len);
142 bind_buffer_fill += len;
143 bind_buffer[bind_buffer_fill] = 0;
145 return (len);
146 } /* size_t bind_curl_callback */
148 static int bind_xml_read_counter (xmlDoc *doc, xmlNode *node,
149 counter_t *ret_value)
150 {
151 char *str_ptr, *end_ptr;
152 long long int value;
154 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
155 if (str_ptr == NULL)
156 {
157 ERROR ("bind plugin: bind_xml_read_int64: xmlNodeListGetString failed.");
158 return (-1);
159 }
161 errno = 0;
162 value = strtoll (str_ptr, &end_ptr, 10);
163 xmlFree(str_ptr);
164 if (str_ptr == end_ptr || errno)
165 {
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.");
170 else
171 ERROR ("bind plugin: bind_xml_read_int64: strtoll failed.");
172 return (-1);
173 }
175 *ret_value = value;
176 return (0);
177 } /* int bind_xml_read_counter */
179 static int bind_xml_read_timestamp (xmlDoc *doc, xmlNode *node,
180 time_t *ret_value)
181 {
182 char *str_ptr, *p;
183 struct tm tm;
185 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
186 if (str_ptr == NULL)
187 {
188 ERROR ("bind plugin: bind_xml_read_int: xmlNodeListGetString failed.");
189 return (-1);
190 }
192 memset(&tm, 0, sizeof(tm));
193 p = strptime(str_ptr, "%Y-%m-%dT%T", &tm);
194 xmlFree(str_ptr);
195 if (p == NULL)
196 {
197 ERROR ("bind plugin: bind_xml_read_timestamp: strptime failed.");
198 return (-1);
199 }
201 *ret_value = timegm(&tm);
202 return (0);
203 } /* int bind_xml_read_timestamp */
205 /* Bind 9.5.x */
206 static int bind_xml_stats_v1(xmlDoc *doc, xmlXPathContext *xpathCtx, xmlNode *statsnode)
207 {
208 xmlXPathObjectPtr xpathObj = NULL;
209 time_t current_time;
210 int i;
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");
218 return -1;
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);
226 } else
227 xmlFree(boot_tm);
228 }
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");
235 return -1;
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);
239 else
240 DEBUG ("bind plugin: Statv1: Server current time: %ld (%d nodes)", current_time, xpathObj->nodesetval->nodeNr);
241 }
242 xmlXPathFreeObject(xpathObj);
244 /* requests/opcode -- [name] = [counter]
245 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ... */
246 if (use_requests) {
247 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/requests/opcode", xpathCtx)) == NULL) {
248 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-currenttime");
249 return -1;
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)
254 name = child;
255 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
256 counter = child;
257 if (name && counter) {
258 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
259 counter_t val;
260 if (!bind_xml_read_counter(doc, counter, &val))
261 submit_counter(current_time, NULL, "dns_opcode", tinst, val);
262 xmlFree(tinst);
263 }
264 }
265 DEBUG ("bind plugin: Statv1: Found %d entries for requests/opcode", xpathObj->nodesetval->nodeNr);
266 xmlXPathFreeObject(xpathObj);
267 }
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");
275 return -1;
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)
280 name = child;
281 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
282 counter = child;
283 if (name && counter) {
284 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
285 counter_t val;
286 if (!bind_xml_read_counter(doc, counter, &val))
287 submit_counter(current_time, NULL, "dns_qtype", tinst, val);
288 xmlFree(tinst);
289 }
290 }
291 DEBUG ("bind plugin: Statv1: Found %d entries for queries-in/rdtype", xpathObj->nodesetval->nodeNr);
292 xmlXPathFreeObject(xpathObj);
293 }
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");
304 return -1;
305 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
306 xmlNode *child;
307 counter_t val;
308 int n = 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);
321 }
322 DEBUG ("bind plugin: Statv1: Found %d entries for %d nsstats", n, xpathObj->nodesetval->nodeNr);
323 }
324 xmlXPathFreeObject(xpathObj);
325 }
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");
333 return -1;
334 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
335 xmlNode *child;
336 counter_t val;
337 int n = 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);
342 }
343 DEBUG ("bind plugin: Statv1: Found %d entries for %d zonestats", n, xpathObj->nodesetval->nodeNr);
344 }
345 xmlXPathFreeObject(xpathObj);
346 }
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 */
352 if (use_resolver) {
353 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/resstats", xpathCtx)) == NULL) {
354 ERROR("bind plugin: unable to evaluate XPath expression StatsV1-resstats");
355 return -1;
356 } else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
357 xmlNode *child;
358 counter_t val;
359 int n = 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);
364 }
365 DEBUG ("bind plugin: Statv1: Found %d entries for %d resstats", n, xpathObj->nodesetval->nodeNr);
366 }
367 xmlXPathFreeObject(xpathObj);
368 }
369 return 0;
370 } /* int bind_xml_stats_v1 */
372 /* Bind 9.6.x */
373 static int bind_xml_stats_v2(xmlDoc *doc, xmlXPathContext *xpathCtx, xmlNode *statsnode)
374 {
375 xmlXPathObjectPtr xpathObj = NULL;
376 time_t current_time;
377 int i;
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");
385 return -1;
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);
393 } else
394 xmlFree(boot_tm);
395 }
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");
402 return -1;
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);
406 else
407 DEBUG ("bind plugin: Statv2: Server current time: %ld (%d nodes)", current_time, xpathObj->nodesetval->nodeNr);
408 }
409 xmlXPathFreeObject(xpathObj);
411 /* requests/opcode -- [name] = [counter]
412 * Variables: QUERY, ... */
413 if (use_requests) {
414 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "server/requests/opcode", xpathCtx)) == NULL) {
415 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-opcode");
416 return -1;
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)
421 name = child;
422 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
423 counter = child;
424 if (name && counter) {
425 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
426 counter_t val;
427 if (!bind_xml_read_counter(doc, counter, &val))
428 submit_counter(current_time, NULL, "dns_opcode", tinst, val);
429 xmlFree(tinst);
430 }
431 }
432 DEBUG ("bind plugin: Statv2: Found %d entries for requests/opcode", xpathObj->nodesetval->nodeNr);
433 xmlXPathFreeObject(xpathObj);
434 }
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");
441 return -1;
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)
446 name = child;
447 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
448 counter = child;
449 if (name && counter) {
450 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
451 counter_t val;
452 if (!bind_xml_read_counter(doc, counter, &val))
453 submit_counter(current_time, NULL, "dns_qtype", tinst, val);
454 xmlFree(tinst);
455 }
456 }
457 DEBUG ("bind plugin: Statv2: Found %d entries for queries-in/rdtype", xpathObj->nodesetval->nodeNr);
458 xmlXPathFreeObject(xpathObj);
459 }
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");
470 return -1;
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)
475 name = child;
476 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
477 counter = child;
478 if (name && counter) {
479 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
480 counter_t val;
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);
491 }
492 xmlFree(tinst);
493 }
494 }
495 DEBUG ("bind plugin: Statv2: Found %d entries for nsstat", xpathObj->nodesetval->nodeNr);
496 xmlXPathFreeObject(xpathObj);
497 }
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");
505 return -1;
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)
510 name = child;
511 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
512 counter = child;
513 if (name && counter) {
514 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
515 counter_t val;
516 if (!bind_xml_read_counter(doc, counter, &val))
517 submit_counter(current_time, NULL, "dns_zops", tinst, val);
518 xmlFree(tinst);
519 }
520 }
521 DEBUG ("bind plugin: Statv2: Found %d entries for zonestat", xpathObj->nodesetval->nodeNr);
522 xmlXPathFreeObject(xpathObj);
523 }
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 */
530 if (use_resolver) {
531 if ((xpathObj = xmlXPathEvalExpression(BAD_CAST "views/view", xpathCtx)) == NULL) {
532 ERROR("bind plugin: unable to evaluate XPath expression StatsV2-view");
533 return -1;
534 } else for (i = 0; xpathObj->nodesetval && i < xpathObj->nodesetval->nodeNr; i++) {
535 char *zname = NULL;
536 xmlNode *name = NULL, *counter = NULL, *rchild, *child;
537 int n = 0;
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)
548 name = child;
549 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
550 counter = child;
551 if (name && counter) {
552 char *tinst = (char *) xmlNodeListGetString (doc, name->xmlChildrenNode, 1);
553 counter_t val;
554 if (!bind_xml_read_counter(doc, counter, &val))
555 submit_counter(current_time, zname, "dns_resolver", tinst, val);
556 xmlFree(tinst);
557 }
558 }
559 DEBUG ("bind plugin: Statv2: Found %d entries for view %s", n, zname);
560 xmlFree(zname);
561 }
562 xmlXPathFreeObject(xpathObj);
563 }
564 return 0;
565 } /* int bind_xml_stats_v2 */
567 static int bind_xml (const char *data)
568 {
569 xmlDoc *doc = NULL;
570 xmlXPathContextPtr xpathCtx = NULL;
571 xmlXPathObjectPtr xpathObj = NULL;
572 int ret = -1;
574 do
575 {
576 doc = xmlParseMemory (data, strlen (data));
577 if (doc == NULL) {
578 ERROR ("bind plugin: xmlParseMemory failed.");
579 break;
580 }
582 if ((xpathCtx = xmlXPathNewContext(doc)) == NULL) {
583 ERROR ("bind plugin: xmlXPathNewContext failed.");
584 break;
585 }
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)
590 {
591 ERROR("bind plugin: unable to evaluate XPath expression StatsV1");
592 break;
593 }
594 else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0)
595 {
596 /* We have Bind-9.5.x */
597 ret = bind_xml_stats_v1(doc, xpathCtx, xpathObj->nodesetval->nodeTab[0]);
598 break;
599 }
601 /* Not Bind-9.5.x. Let's clear up and try Bind-9.6.x instead. */
602 xmlXPathFreeObject(xpathObj);
603 xpathObj = NULL;
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)
608 {
609 ERROR("bind plugin: unable to evaluate XPath expression StatsV2");
610 break;
611 }
612 else if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0)
613 {
614 /* We have Bind-9.6.x */
615 ret = bind_xml_stats_v2(doc, xpathCtx, xpathObj->nodesetval->nodeTab[0]);
616 break;
617 }
619 ERROR("bind plugin: unable to find statistics in supported version.");
620 } while (0);
622 if (xpathObj != NULL)
623 xmlXPathFreeObject(xpathObj);
624 if (xpathCtx != NULL)
625 xmlXPathFreeContext(xpathCtx);
626 if (doc != NULL)
627 xmlFreeDoc (doc);
629 return (ret);
630 } /* int bind_xml */
632 static int config_set_str (char **var, const char *value)
633 {
634 if (*var != NULL)
635 {
636 free (*var);
637 *var = NULL;
638 }
640 if ((*var = strdup (value)) == NULL)
641 return (-1);
642 else
643 return (0);
644 } /* int config_set_str */
646 static int config_set_bool (bool *var, const char *value)
647 {
648 if ((strcasecmp ("true", value) == 0)
649 || (strcasecmp ("yes", value) == 0)
650 || (strcasecmp ("on", value) == 0))
651 *var = 1;
652 else if ((strcasecmp ("false", value) == 0)
653 || (strcasecmp ("no", value) == 0)
654 || (strcasecmp ("off", value) == 0))
655 *var = 0;
656 else
657 return -1;
658 return 0;
659 } /* int config_set_bool */
661 static int bind_config (const char *key, const char *value)
662 {
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));
677 else
678 return (-1);
679 } /* int bind_config */
681 static int bind_init (void)
682 {
683 if (url == NULL)
684 {
685 WARNING ("bind plugin: bind_init: No URL configured, "
686 "returning an error.");
687 return (-1);
688 }
690 if (curl != NULL)
691 curl_easy_cleanup (curl);
693 if ((curl = curl_easy_init ()) == NULL)
694 {
695 ERROR ("bind plugin: bind_init: curl_easy_init failed.");
696 return (-1);
697 }
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);
703 return (0);
704 } /* int bind_init */
706 static int bind_read (void)
707 {
708 int status;
710 if (curl == NULL)
711 {
712 ERROR ("bind plugin: I don't have a CURL object.");
713 return (-1);
714 }
716 if (url == NULL)
717 {
718 ERROR ("bind plugin: No URL has been configured.");
719 return (-1);
720 }
722 bind_buffer_fill = 0;
723 if (curl_easy_perform (curl) != 0)
724 {
725 ERROR ("bind plugin: curl_easy_perform failed: %s",
726 bind_curl_error);
727 return (-1);
728 }
730 status = bind_xml (bind_buffer);
731 if (status != 0)
732 return (-1);
733 else
734 return (0);
735 } /* int bind_read */
737 void module_register (void)
738 {
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 : */