1 /**
2 * collectd - src/bind.c
3 * Copyright (C) 2009 Bruno Prémont
4 * Copyright (C) 2009,2010 Florian Forster
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Bruno Prémont <bonbons at linux-vserver.org>
21 * Florian Forster <octo at collectd.org>
22 **/
24 #include "config.h"
26 #if STRPTIME_NEEDS_STANDARDS
27 # ifndef _ISOC99_SOURCE
28 # define _ISOC99_SOURCE 1
29 # endif
30 # ifndef _POSIX_C_SOURCE
31 # define _POSIX_C_SOURCE 200112L
32 # endif
33 # ifndef _XOPEN_SOURCE
34 # define _XOPEN_SOURCE 500
35 # endif
36 #endif /* STRPTIME_NEEDS_STANDARDS */
38 #include "collectd.h"
39 #include "common.h"
40 #include "plugin.h"
41 #include "configfile.h"
43 /* Some versions of libcurl don't include this themselves and then don't have
44 * fd_set available. */
45 #if HAVE_SYS_SELECT_H
46 # include <sys/select.h>
47 #endif
49 #include <curl/curl.h>
50 #include <libxml/parser.h>
51 #include <libxml/xpath.h>
53 #ifndef BIND_DEFAULT_URL
54 # define BIND_DEFAULT_URL "http://localhost:8053/"
55 #endif
57 /*
58 * Some types used for the callback functions. `translation_table_ptr_t' and
59 * `list_info_ptr_t' are passed to the callbacks in the `void *user_data'
60 * pointer.
61 */
62 typedef int (*list_callback_t) (const char *name, value_t value,
63 time_t current_time, void *user_data);
65 struct cb_view_s
66 {
67 char *name;
69 int qtypes;
70 int resolver_stats;
71 int cacherrsets;
73 char **zones;
74 size_t zones_num;
75 };
76 typedef struct cb_view_s cb_view_t;
78 struct translation_info_s
79 {
80 const char *xml_name;
81 const char *type;
82 const char *type_instance;
83 };
84 typedef struct translation_info_s translation_info_t;
86 struct translation_table_ptr_s
87 {
88 const translation_info_t *table;
89 size_t table_length;
90 const char *plugin_instance;
91 };
92 typedef struct translation_table_ptr_s translation_table_ptr_t;
94 struct list_info_ptr_s
95 {
96 const char *plugin_instance;
97 const char *type;
98 };
99 typedef struct list_info_ptr_s list_info_ptr_t;
101 /* FIXME: Enabled by default for backwards compatibility. */
102 /* TODO: Remove time parsing code. */
103 static _Bool config_parse_time = 1;
105 static char *url = NULL;
106 static int global_opcodes = 1;
107 static int global_qtypes = 1;
108 static int global_server_stats = 1;
109 static int global_zone_maint_stats = 1;
110 static int global_resolver_stats = 0;
111 static int global_memory_stats = 1;
113 static cb_view_t *views = NULL;
114 static size_t views_num = 0;
116 static CURL *curl = NULL;
118 static char *bind_buffer = NULL;
119 static size_t bind_buffer_size = 0;
120 static size_t bind_buffer_fill = 0;
121 static char bind_curl_error[CURL_ERROR_SIZE];
123 /* Translation table for the `nsstats' values. */
124 static const translation_info_t nsstats_translation_table[] = /* {{{ */
125 {
126 /* Requests */
127 { "Requestv4", "dns_request", "IPv4" },
128 { "Requestv6", "dns_request", "IPv6" },
129 { "ReqEdns0", "dns_request", "EDNS0" },
130 { "ReqBadEDNSVer", "dns_request", "BadEDNSVer" },
131 { "ReqTSIG", "dns_request", "TSIG" },
132 { "ReqSIG0", "dns_request", "SIG0" },
133 { "ReqBadSIG", "dns_request", "BadSIG" },
134 { "ReqTCP", "dns_request", "TCP" },
135 /* Rejects */
136 { "AuthQryRej", "dns_reject", "authorative" },
137 { "RecQryRej", "dns_reject", "recursive" },
138 { "XfrRej", "dns_reject", "transfer" },
139 { "UpdateRej", "dns_reject", "update" },
140 /* Responses */
141 { "Response", "dns_response", "normal" },
142 { "TruncatedResp", "dns_response", "truncated" },
143 { "RespEDNS0", "dns_response", "EDNS0" },
144 { "RespTSIG", "dns_response", "TSIG" },
145 { "RespSIG0", "dns_response", "SIG0" },
146 /* Queries */
147 { "QryAuthAns", "dns_query", "authorative" },
148 { "QryNoauthAns", "dns_query", "nonauth" },
149 { "QryReferral", "dns_query", "referral" },
150 { "QryRecursion", "dns_query", "recursion" },
151 { "QryDuplicate", "dns_query", "dupliate" },
152 { "QryDropped", "dns_query", "dropped" },
153 { "QryFailure", "dns_query", "failure" },
154 /* Response codes */
155 { "QrySuccess", "dns_rcode", "tx-NOERROR" },
156 { "QryNxrrset", "dns_rcode", "tx-NXRRSET" },
157 { "QrySERVFAIL", "dns_rcode", "tx-SERVFAIL" },
158 { "QryFORMERR", "dns_rcode", "tx-FORMERR" },
159 { "QryNXDOMAIN", "dns_rcode", "tx-NXDOMAIN" }
160 #if 0
161 { "XfrReqDone", "type", "type_instance" },
162 { "UpdateReqFwd", "type", "type_instance" },
163 { "UpdateRespFwd", "type", "type_instance" },
164 { "UpdateFwdFail", "type", "type_instance" },
165 { "UpdateDone", "type", "type_instance" },
166 { "UpdateFail", "type", "type_instance" },
167 { "UpdateBadPrereq", "type", "type_instance" },
168 #endif
169 };
170 static int nsstats_translation_table_length =
171 STATIC_ARRAY_SIZE (nsstats_translation_table);
172 /* }}} */
174 /* Translation table for the `zonestats' values. */
175 static const translation_info_t zonestats_translation_table[] = /* {{{ */
176 {
177 /* Notify's */
178 { "NotifyOutv4", "dns_notify", "tx-IPv4" },
179 { "NotifyOutv6", "dns_notify", "tx-IPv6" },
180 { "NotifyInv4", "dns_notify", "rx-IPv4" },
181 { "NotifyInv6", "dns_notify", "rx-IPv6" },
182 { "NotifyRej", "dns_notify", "rejected" },
183 /* SOA/AXFS/IXFS requests */
184 { "SOAOutv4", "dns_opcode", "SOA-IPv4" },
185 { "SOAOutv6", "dns_opcode", "SOA-IPv6" },
186 { "AXFRReqv4", "dns_opcode", "AXFR-IPv4" },
187 { "AXFRReqv6", "dns_opcode", "AXFR-IPv6" },
188 { "IXFRReqv4", "dns_opcode", "IXFR-IPv4" },
189 { "IXFRReqv6", "dns_opcode", "IXFR-IPv6" },
190 /* Domain transfers */
191 { "XfrSuccess", "dns_transfer", "success" },
192 { "XfrFail", "dns_transfer", "failure" }
193 };
194 static int zonestats_translation_table_length =
195 STATIC_ARRAY_SIZE (zonestats_translation_table);
196 /* }}} */
198 /* Translation table for the `resstats' values. */
199 static const translation_info_t resstats_translation_table[] = /* {{{ */
200 {
201 /* Generic resolver information */
202 { "Queryv4", "dns_query", "IPv4" },
203 { "Queryv6", "dns_query", "IPv6" },
204 { "Responsev4", "dns_response", "IPv4" },
205 { "Responsev6", "dns_response", "IPv6" },
206 /* Received response codes */
207 { "NXDOMAIN", "dns_rcode", "rx-NXDOMAIN" },
208 { "SERVFAIL", "dns_rcode", "rx-SERVFAIL" },
209 { "FORMERR", "dns_rcode", "rx-FORMERR" },
210 { "OtherError", "dns_rcode", "rx-OTHER" },
211 { "EDNS0Fail", "dns_rcode", "rx-EDNS0Fail"},
212 /* Received responses */
213 { "Mismatch", "dns_response", "mismatch" },
214 { "Truncated", "dns_response", "truncated" },
215 { "Lame", "dns_response", "lame" },
216 { "Retry", "dns_query", "retry" },
217 #if 0
218 { "GlueFetchv4", "type", "type_instance" },
219 { "GlueFetchv6", "type", "type_instance" },
220 { "GlueFetchv4Fail", "type", "type_instance" },
221 { "GlueFetchv6Fail", "type", "type_instance" },
222 #endif
223 /* DNSSEC information */
224 { "ValAttempt", "dns_resolver", "DNSSEC-attempt" },
225 { "ValOk", "dns_resolver", "DNSSEC-okay" },
226 { "ValNegOk", "dns_resolver", "DNSSEC-negokay" },
227 { "ValFail", "dns_resolver", "DNSSEC-fail" }
228 };
229 static int resstats_translation_table_length =
230 STATIC_ARRAY_SIZE (resstats_translation_table);
231 /* }}} */
233 /* Translation table for the `memory/summary' values. */
234 static const translation_info_t memsummary_translation_table[] = /* {{{ */
235 {
236 { "TotalUse", "memory", "TotalUse" },
237 { "InUse", "memory", "InUse" },
238 { "BlockSize", "memory", "BlockSize" },
239 { "ContextSize", "memory", "ContextSize" },
240 { "Lost", "memory", "Lost" }
241 };
242 static int memsummary_translation_table_length =
243 STATIC_ARRAY_SIZE (memsummary_translation_table);
244 /* }}} */
246 static void submit (time_t ts, const char *plugin_instance, /* {{{ */
247 const char *type, const char *type_instance, value_t value)
248 {
249 value_t values[1];
250 value_list_t vl = VALUE_LIST_INIT;
252 values[0] = value;
254 vl.values = values;
255 vl.values_len = 1;
256 if (config_parse_time)
257 vl.time = TIME_T_TO_CDTIME_T (ts);
258 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
259 sstrncpy(vl.plugin, "bind", sizeof(vl.plugin));
260 if (plugin_instance) {
261 sstrncpy(vl.plugin_instance, plugin_instance,
262 sizeof(vl.plugin_instance));
263 replace_special (vl.plugin_instance, sizeof (vl.plugin_instance));
264 }
265 sstrncpy(vl.type, type, sizeof(vl.type));
266 if (type_instance) {
267 sstrncpy(vl.type_instance, type_instance,
268 sizeof(vl.type_instance));
269 replace_special (vl.type_instance, sizeof (vl.type_instance));
270 }
271 plugin_dispatch_values(&vl);
272 } /* }}} void submit */
274 static size_t bind_curl_callback (void *buf, size_t size, /* {{{ */
275 size_t nmemb, void __attribute__((unused)) *stream)
276 {
277 size_t len = size * nmemb;
279 if (len <= 0)
280 return (len);
282 if ((bind_buffer_fill + len) >= bind_buffer_size)
283 {
284 char *temp;
286 temp = realloc(bind_buffer, bind_buffer_fill + len + 1);
287 if (temp == NULL)
288 {
289 ERROR ("bind plugin: realloc failed.");
290 return (0);
291 }
292 bind_buffer = temp;
293 bind_buffer_size = bind_buffer_fill + len + 1;
294 }
296 memcpy (bind_buffer + bind_buffer_fill, (char *) buf, len);
297 bind_buffer_fill += len;
298 bind_buffer[bind_buffer_fill] = 0;
300 return (len);
301 } /* }}} size_t bind_curl_callback */
303 /*
304 * Callback, that's called with a translation table.
305 * (Plugin instance is fixed, type and type instance come from lookup table.)
306 */
307 static int bind_xml_table_callback (const char *name, value_t value, /* {{{ */
308 time_t current_time, void *user_data)
309 {
310 translation_table_ptr_t *table = (translation_table_ptr_t *) user_data;
311 size_t i;
313 if (table == NULL)
314 return (-1);
316 for (i = 0; i < table->table_length; i++)
317 {
318 if (strcmp (table->table[i].xml_name, name) != 0)
319 continue;
321 submit (current_time,
322 table->plugin_instance,
323 table->table[i].type,
324 table->table[i].type_instance,
325 value);
326 break;
327 }
329 return (0);
330 } /* }}} int bind_xml_table_callback */
332 /*
333 * Callback, that's used for lists.
334 * (Plugin instance and type are fixed, xml name is used as type instance.)
335 */
336 static int bind_xml_list_callback (const char *name, /* {{{ */
337 value_t value, time_t current_time, void *user_data)
338 {
339 list_info_ptr_t *list_info = (list_info_ptr_t *) user_data;
341 if (list_info == NULL)
342 return (-1);
344 submit (current_time,
345 list_info->plugin_instance,
346 list_info->type,
347 /* type instance = */ name,
348 value);
350 return (0);
351 } /* }}} int bind_xml_list_callback */
353 static int bind_xml_read_derive (xmlDoc *doc, xmlNode *node, /* {{{ */
354 derive_t *ret_value)
355 {
356 char *str_ptr;
357 value_t value;
358 int status;
360 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
361 if (str_ptr == NULL)
362 {
363 ERROR ("bind plugin: bind_xml_read_derive: xmlNodeListGetString failed.");
364 return (-1);
365 }
367 status = parse_value (str_ptr, &value, DS_TYPE_DERIVE);
368 if (status != 0)
369 {
370 ERROR ("bind plugin: Parsing string \"%s\" to derive value failed.",
371 str_ptr);
372 return (-1);
373 }
375 *ret_value = value.derive;
376 return (0);
377 } /* }}} int bind_xml_read_derive */
379 static int bind_xml_read_gauge (xmlDoc *doc, xmlNode *node, /* {{{ */
380 gauge_t *ret_value)
381 {
382 char *str_ptr, *end_ptr;
383 double value;
385 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
386 if (str_ptr == NULL)
387 {
388 ERROR ("bind plugin: bind_xml_read_gauge: xmlNodeListGetString failed.");
389 return (-1);
390 }
392 errno = 0;
393 value = strtod (str_ptr, &end_ptr);
394 xmlFree(str_ptr);
395 if (str_ptr == end_ptr || errno)
396 {
397 if (errno && (value < 0))
398 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with underflow.");
399 else if (errno && (value > 0))
400 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed with overflow.");
401 else
402 ERROR ("bind plugin: bind_xml_read_gauge: strtod failed.");
403 return (-1);
404 }
406 *ret_value = (gauge_t) value;
407 return (0);
408 } /* }}} int bind_xml_read_gauge */
410 static int bind_xml_read_timestamp (const char *xpath_expression, /* {{{ */
411 xmlDoc *doc, xmlXPathContext *xpathCtx, time_t *ret_value)
412 {
413 xmlXPathObject *xpathObj = NULL;
414 xmlNode *node;
415 char *str_ptr;
416 char *tmp;
417 struct tm tm;
419 xpathObj = xmlXPathEvalExpression (BAD_CAST xpath_expression, xpathCtx);
420 if (xpathObj == NULL)
421 {
422 ERROR ("bind plugin: Unable to evaluate XPath expression `%s'.",
423 xpath_expression);
424 return (-1);
425 }
427 if ((xpathObj->nodesetval == NULL) || (xpathObj->nodesetval->nodeNr < 1))
428 {
429 xmlXPathFreeObject (xpathObj);
430 return (-1);
431 }
433 if (xpathObj->nodesetval->nodeNr != 1)
434 {
435 NOTICE ("bind plugin: Evaluating the XPath expression `%s' returned "
436 "%i nodes. Only handling the first one.",
437 xpath_expression, xpathObj->nodesetval->nodeNr);
438 }
440 node = xpathObj->nodesetval->nodeTab[0];
442 if (node->xmlChildrenNode == NULL)
443 {
444 ERROR ("bind plugin: bind_xml_read_timestamp: "
445 "node->xmlChildrenNode == NULL");
446 xmlXPathFreeObject (xpathObj);
447 return (-1);
448 }
450 str_ptr = (char *) xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
451 if (str_ptr == NULL)
452 {
453 ERROR ("bind plugin: bind_xml_read_timestamp: xmlNodeListGetString failed.");
454 xmlXPathFreeObject (xpathObj);
455 return (-1);
456 }
458 memset (&tm, 0, sizeof(tm));
459 tmp = strptime (str_ptr, "%Y-%m-%dT%T", &tm);
460 xmlFree(str_ptr);
461 if (tmp == NULL)
462 {
463 ERROR ("bind plugin: bind_xml_read_timestamp: strptime failed.");
464 xmlXPathFreeObject (xpathObj);
465 return (-1);
466 }
468 *ret_value = mktime(&tm);
470 xmlXPathFreeObject (xpathObj);
471 return (0);
472 } /* }}} int bind_xml_read_timestamp */
474 /*
475 * bind_parse_generic_name_value
476 *
477 * Reads statistics in the form:
478 * <foo>
479 * <name>QUERY</name>
480 * <counter>123</counter>
481 * </foo>
482 */
483 static int bind_parse_generic_name_value (const char *xpath_expression, /* {{{ */
484 list_callback_t list_callback,
485 void *user_data,
486 xmlDoc *doc, xmlXPathContext *xpathCtx,
487 time_t current_time, int ds_type)
488 {
489 xmlXPathObject *xpathObj = NULL;
490 int num_entries;
491 int i;
493 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
494 if (xpathObj == NULL)
495 {
496 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
497 xpath_expression);
498 return (-1);
499 }
501 num_entries = 0;
502 /* Iterate over all matching nodes. */
503 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
504 {
505 xmlNode *name_node = NULL;
506 xmlNode *counter = NULL;
507 xmlNode *parent;
508 xmlNode *child;
510 parent = xpathObj->nodesetval->nodeTab[i];
511 DEBUG ("bind plugin: bind_parse_generic_name_value: parent->name = %s;",
512 (char *) parent->name);
514 /* Iterate over all child nodes. */
515 for (child = parent->xmlChildrenNode;
516 child != NULL;
517 child = child->next)
518 {
519 if (child->type != XML_ELEMENT_NODE)
520 continue;
522 if (xmlStrcmp (BAD_CAST "name", child->name) == 0)
523 name_node = child;
524 else if (xmlStrcmp (BAD_CAST "counter", child->name) == 0)
525 counter = child;
526 }
528 if ((name_node != NULL) && (counter != NULL))
529 {
530 char *name = (char *) xmlNodeListGetString (doc,
531 name_node->xmlChildrenNode, 1);
532 value_t value;
533 int status;
535 if (ds_type == DS_TYPE_GAUGE)
536 status = bind_xml_read_gauge (doc, counter, &value.gauge);
537 else
538 status = bind_xml_read_derive (doc, counter, &value.derive);
539 if (status != 0)
540 continue;
542 status = (*list_callback) (name, value, current_time, user_data);
543 if (status == 0)
544 num_entries++;
546 xmlFree (name);
547 }
548 }
550 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
551 num_entries, (num_entries == 1) ? "entry" : "entries",
552 xpath_expression);
554 xmlXPathFreeObject(xpathObj);
556 return (0);
557 } /* }}} int bind_parse_generic_name_value */
559 /*
560 * bind_parse_generic_value_list
561 *
562 * Reads statistics in the form:
563 * <foo>
564 * <name0>123</name0>
565 * <name1>234</name1>
566 * <name2>345</name2>
567 * :
568 * </foo>
569 */
570 static int bind_parse_generic_value_list (const char *xpath_expression, /* {{{ */
571 list_callback_t list_callback,
572 void *user_data,
573 xmlDoc *doc, xmlXPathContext *xpathCtx,
574 time_t current_time, int ds_type)
575 {
576 xmlXPathObject *xpathObj = NULL;
577 int num_entries;
578 int i;
580 xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
581 if (xpathObj == NULL)
582 {
583 ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
584 xpath_expression);
585 return (-1);
586 }
588 num_entries = 0;
589 /* Iterate over all matching nodes. */
590 for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
591 {
592 xmlNode *child;
594 /* Iterate over all child nodes. */
595 for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
596 child != NULL;
597 child = child->next)
598 {
599 char *node_name;
600 value_t value;
601 int status;
603 if (child->type != XML_ELEMENT_NODE)
604 continue;
606 node_name = (char *) child->name;
608 if (ds_type == DS_TYPE_GAUGE)
609 status = bind_xml_read_gauge (doc, child, &value.gauge);
610 else
611 status = bind_xml_read_derive (doc, child, &value.derive);
612 if (status != 0)
613 continue;
615 status = (*list_callback) (node_name, value, current_time, user_data);
616 if (status == 0)
617 num_entries++;
618 }
619 }
621 DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
622 num_entries, (num_entries == 1) ? "entry" : "entries",
623 xpath_expression);
625 xmlXPathFreeObject(xpathObj);
627 return (0);
628 } /* }}} int bind_parse_generic_value_list */
630 static int bind_xml_stats_handle_zone (int version, xmlDoc *doc, /* {{{ */
631 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
632 time_t current_time)
633 {
634 xmlXPathObject *path_obj;
635 char *zone_name = NULL;
636 int i;
637 size_t j;
639 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
640 if (path_obj == NULL)
641 {
642 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
643 return (-1);
644 }
646 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
647 {
648 zone_name = (char *) xmlNodeListGetString (doc,
649 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
650 if (zone_name != NULL)
651 break;
652 }
654 if (zone_name == NULL)
655 {
656 ERROR ("bind plugin: Could not determine zone name.");
657 xmlXPathFreeObject (path_obj);
658 return (-1);
659 }
661 for (j = 0; j < view->zones_num; j++)
662 {
663 if (strcasecmp (zone_name, view->zones[j]) == 0)
664 break;
665 }
667 xmlFree (zone_name);
668 zone_name = NULL;
670 if (j >= views_num)
671 {
672 xmlXPathFreeObject (path_obj);
673 return (0);
674 }
676 zone_name = view->zones[j];
678 DEBUG ("bind plugin: bind_xml_stats_handle_zone: Found zone `%s'.",
679 zone_name);
681 { /* Parse the <counters> tag {{{ */
682 char plugin_instance[DATA_MAX_NAME_LEN];
683 translation_table_ptr_t table_ptr =
684 {
685 nsstats_translation_table,
686 nsstats_translation_table_length,
687 plugin_instance
688 };
690 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-zone-%s",
691 view->name, zone_name);
693 bind_parse_generic_value_list (/* xpath = */ "counters",
694 /* callback = */ bind_xml_table_callback,
695 /* user_data = */ &table_ptr,
696 doc, path_ctx, current_time, DS_TYPE_COUNTER);
697 } /* }}} */
699 xmlXPathFreeObject (path_obj);
701 return (0);
702 } /* }}} int bind_xml_stats_handle_zone */
704 static int bind_xml_stats_search_zones (int version, xmlDoc *doc, /* {{{ */
705 xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
706 time_t current_time)
707 {
708 xmlXPathObject *zone_nodes = NULL;
709 xmlXPathContext *zone_path_context;
710 int i;
712 zone_path_context = xmlXPathNewContext (doc);
713 if (zone_path_context == NULL)
714 {
715 ERROR ("bind plugin: xmlXPathNewContext failed.");
716 return (-1);
717 }
719 zone_nodes = xmlXPathEvalExpression (BAD_CAST "zones/zone", path_ctx);
720 if (zone_nodes == NULL)
721 {
722 ERROR ("bind plugin: Cannot find any <view> tags.");
723 xmlXPathFreeContext (zone_path_context);
724 return (-1);
725 }
727 for (i = 0; i < zone_nodes->nodesetval->nodeNr; i++)
728 {
729 node = zone_nodes->nodesetval->nodeTab[i];
730 assert (node != NULL);
732 zone_path_context->node = node;
734 bind_xml_stats_handle_zone (version, doc, zone_path_context, node, view,
735 current_time);
736 }
738 xmlXPathFreeObject (zone_nodes);
739 xmlXPathFreeContext (zone_path_context);
740 return (0);
741 } /* }}} int bind_xml_stats_search_zones */
743 static int bind_xml_stats_handle_view (int version, xmlDoc *doc, /* {{{ */
744 xmlXPathContext *path_ctx, xmlNode *node, time_t current_time)
745 {
746 xmlXPathObject *path_obj;
747 char *view_name = NULL;
748 cb_view_t *view;
749 int i;
750 size_t j;
752 path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
753 if (path_obj == NULL)
754 {
755 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
756 return (-1);
757 }
759 for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
760 {
761 view_name = (char *) xmlNodeListGetString (doc,
762 path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
763 if (view_name != NULL)
764 break;
765 }
767 if (view_name == NULL)
768 {
769 ERROR ("bind plugin: Could not determine view name.");
770 xmlXPathFreeObject (path_obj);
771 return (-1);
772 }
774 for (j = 0; j < views_num; j++)
775 {
776 if (strcasecmp (view_name, views[j].name) == 0)
777 break;
778 }
780 xmlFree (view_name);
781 xmlXPathFreeObject (path_obj);
783 view_name = NULL;
784 path_obj = NULL;
786 if (j >= views_num)
787 return (0);
789 view = views + j;
791 DEBUG ("bind plugin: bind_xml_stats_handle_view: Found view `%s'.",
792 view->name);
794 if (view->qtypes != 0) /* {{{ */
795 {
796 char plugin_instance[DATA_MAX_NAME_LEN];
797 list_info_ptr_t list_info =
798 {
799 plugin_instance,
800 /* type = */ "dns_qtype"
801 };
803 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-qtypes",
804 view->name);
806 bind_parse_generic_name_value (/* xpath = */ "rdtype",
807 /* callback = */ bind_xml_list_callback,
808 /* user_data = */ &list_info,
809 doc, path_ctx, current_time, DS_TYPE_COUNTER);
810 } /* }}} */
812 if (view->resolver_stats != 0) /* {{{ */
813 {
814 char plugin_instance[DATA_MAX_NAME_LEN];
815 translation_table_ptr_t table_ptr =
816 {
817 resstats_translation_table,
818 resstats_translation_table_length,
819 plugin_instance
820 };
822 ssnprintf (plugin_instance, sizeof (plugin_instance),
823 "%s-resolver_stats", view->name);
825 bind_parse_generic_name_value ("resstat",
826 /* callback = */ bind_xml_table_callback,
827 /* user_data = */ &table_ptr,
828 doc, path_ctx, current_time, DS_TYPE_COUNTER);
829 } /* }}} */
831 /* Record types in the cache */
832 if (view->cacherrsets != 0) /* {{{ */
833 {
834 char plugin_instance[DATA_MAX_NAME_LEN];
835 list_info_ptr_t list_info =
836 {
837 plugin_instance,
838 /* type = */ "dns_qtype_cached"
839 };
841 ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-cache_rr_sets",
842 view->name);
844 bind_parse_generic_name_value (/* xpath = */ "cache/rrset",
845 /* callback = */ bind_xml_list_callback,
846 /* user_data = */ &list_info,
847 doc, path_ctx, current_time, DS_TYPE_GAUGE);
848 } /* }}} */
850 if (view->zones_num > 0)
851 bind_xml_stats_search_zones (version, doc, path_ctx, node, view,
852 current_time);
854 return (0);
855 } /* }}} int bind_xml_stats_handle_view */
857 static int bind_xml_stats_search_views (int version, xmlDoc *doc, /* {{{ */
858 xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time)
859 {
860 xmlXPathObject *view_nodes = NULL;
861 xmlXPathContext *view_path_context;
862 int i;
864 view_path_context = xmlXPathNewContext (doc);
865 if (view_path_context == NULL)
866 {
867 ERROR ("bind plugin: xmlXPathNewContext failed.");
868 return (-1);
869 }
871 view_nodes = xmlXPathEvalExpression (BAD_CAST "views/view", xpathCtx);
872 if (view_nodes == NULL)
873 {
874 ERROR ("bind plugin: Cannot find any <view> tags.");
875 xmlXPathFreeContext (view_path_context);
876 return (-1);
877 }
879 for (i = 0; i < view_nodes->nodesetval->nodeNr; i++)
880 {
881 xmlNode *node;
883 node = view_nodes->nodesetval->nodeTab[i];
884 assert (node != NULL);
886 view_path_context->node = node;
888 bind_xml_stats_handle_view (version, doc, view_path_context, node,
889 current_time);
890 }
892 xmlXPathFreeObject (view_nodes);
893 xmlXPathFreeContext (view_path_context);
894 return (0);
895 } /* }}} int bind_xml_stats_search_views */
897 static int bind_xml_stats (int version, xmlDoc *doc, /* {{{ */
898 xmlXPathContext *xpathCtx, xmlNode *statsnode)
899 {
900 time_t current_time = 0;
901 int status;
903 xpathCtx->node = statsnode;
905 /* TODO: Check `server/boot-time' to recognize server restarts. */
907 status = bind_xml_read_timestamp ("server/current-time",
908 doc, xpathCtx, ¤t_time);
909 if (status != 0)
910 {
911 ERROR ("bind plugin: Reading `server/current-time' failed.");
912 return (-1);
913 }
914 DEBUG ("bind plugin: Current server time is %i.", (int) current_time);
916 /* XPath: server/requests/opcode
917 * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
918 * Layout:
919 * <opcode>
920 * <name>A</name>
921 * <counter>1</counter>
922 * </opcode>
923 * :
924 */
925 if (global_opcodes != 0)
926 {
927 list_info_ptr_t list_info =
928 {
929 /* plugin instance = */ "global-opcodes",
930 /* type = */ "dns_opcode"
931 };
933 bind_parse_generic_name_value (/* xpath = */ "server/requests/opcode",
934 /* callback = */ bind_xml_list_callback,
935 /* user_data = */ &list_info,
936 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
937 }
939 /* XPath: server/queries-in/rdtype
940 * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
941 * X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
942 * SPF, TKEY, IXFR, AXFR, ANY, ..., Others
943 * Layout:
944 * <rdtype>
945 * <name>A</name>
946 * <counter>1</counter>
947 * </rdtype>
948 * :
949 */
950 if (global_qtypes != 0)
951 {
952 list_info_ptr_t list_info =
953 {
954 /* plugin instance = */ "global-qtypes",
955 /* type = */ "dns_qtype"
956 };
958 bind_parse_generic_name_value (/* xpath = */ "server/queries-in/rdtype",
959 /* callback = */ bind_xml_list_callback,
960 /* user_data = */ &list_info,
961 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
962 }
964 /* XPath: server/nsstats, server/nsstat
965 * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
966 * ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
967 * UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
968 * RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
969 * QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
970 * QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
971 * UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
972 * UpdateBadPrereq
973 * Layout v1:
974 * <nsstats>
975 * <Requestv4>1</Requestv4>
976 * <Requestv6>0</Requestv6>
977 * :
978 * </nsstats>
979 * Layout v2:
980 * <nsstat>
981 * <name>Requestv4</name>
982 * <counter>1</counter>
983 * </nsstat>
984 * <nsstat>
985 * <name>Requestv6</name>
986 * <counter>0</counter>
987 * </nsstat>
988 * :
989 */
990 if (global_server_stats)
991 {
992 translation_table_ptr_t table_ptr =
993 {
994 nsstats_translation_table,
995 nsstats_translation_table_length,
996 /* plugin_instance = */ "global-server_stats"
997 };
999 if (version == 1)
1000 {
1001 bind_parse_generic_value_list ("server/nsstats",
1002 /* callback = */ bind_xml_table_callback,
1003 /* user_data = */ &table_ptr,
1004 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1005 }
1006 else
1007 {
1008 bind_parse_generic_name_value ("server/nsstat",
1009 /* callback = */ bind_xml_table_callback,
1010 /* user_data = */ &table_ptr,
1011 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1012 }
1013 }
1015 /* XPath: server/zonestats, server/zonestat
1016 * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1017 * SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1018 * XfrSuccess, XfrFail
1019 * Layout v1:
1020 * <zonestats>
1021 * <NotifyOutv4>0</NotifyOutv4>
1022 * <NotifyOutv6>0</NotifyOutv6>
1023 * :
1024 * </zonestats>
1025 * Layout v2:
1026 * <zonestat>
1027 * <name>NotifyOutv4</name>
1028 * <counter>0</counter>
1029 * </zonestat>
1030 * <zonestat>
1031 * <name>NotifyOutv6</name>
1032 * <counter>0</counter>
1033 * </zonestat>
1034 * :
1035 */
1036 if (global_zone_maint_stats)
1037 {
1038 translation_table_ptr_t table_ptr =
1039 {
1040 zonestats_translation_table,
1041 zonestats_translation_table_length,
1042 /* plugin_instance = */ "global-zone_maint_stats"
1043 };
1045 if (version == 1)
1046 {
1047 bind_parse_generic_value_list ("server/zonestats",
1048 /* callback = */ bind_xml_table_callback,
1049 /* user_data = */ &table_ptr,
1050 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1051 }
1052 else
1053 {
1054 bind_parse_generic_name_value ("server/zonestat",
1055 /* callback = */ bind_xml_table_callback,
1056 /* user_data = */ &table_ptr,
1057 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1058 }
1059 }
1061 /* XPath: server/resstats
1062 * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1063 * FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1064 * Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1065 * GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1066 * Layout v1:
1067 * <resstats>
1068 * <Queryv4>0</Queryv4>
1069 * <Queryv6>0</Queryv6>
1070 * :
1071 * </resstats>
1072 * Layout v2:
1073 * <resstat>
1074 * <name>Queryv4</name>
1075 * <counter>0</counter>
1076 * </resstat>
1077 * <resstat>
1078 * <name>Queryv6</name>
1079 * <counter>0</counter>
1080 * </resstat>
1081 * :
1082 */
1083 if (global_resolver_stats != 0)
1084 {
1085 translation_table_ptr_t table_ptr =
1086 {
1087 resstats_translation_table,
1088 resstats_translation_table_length,
1089 /* plugin_instance = */ "global-resolver_stats"
1090 };
1092 if (version == 1)
1093 {
1094 bind_parse_generic_value_list ("server/resstats",
1095 /* callback = */ bind_xml_table_callback,
1096 /* user_data = */ &table_ptr,
1097 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1098 }
1099 else
1100 {
1101 bind_parse_generic_name_value ("server/resstat",
1102 /* callback = */ bind_xml_table_callback,
1103 /* user_data = */ &table_ptr,
1104 doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1105 }
1106 }
1108 /* XPath: memory/summary
1109 * Variables: TotalUse, InUse, BlockSize, ContextSize, Lost
1110 * Layout: v2:
1111 * <summary>
1112 * <TotalUse>6587096</TotalUse>
1113 * <InUse>1345424</InUse>
1114 * <BlockSize>5505024</BlockSize>
1115 * <ContextSize>3732456</ContextSize>
1116 * <Lost>0</Lost>
1117 * </summary>
1118 */
1119 if (global_memory_stats != 0)
1120 {
1121 translation_table_ptr_t table_ptr =
1122 {
1123 memsummary_translation_table,
1124 memsummary_translation_table_length,
1125 /* plugin_instance = */ "global-memory_stats"
1126 };
1128 bind_parse_generic_value_list ("memory/summary",
1129 /* callback = */ bind_xml_table_callback,
1130 /* user_data = */ &table_ptr,
1131 doc, xpathCtx, current_time, DS_TYPE_GAUGE);
1132 }
1134 if (views_num > 0)
1135 bind_xml_stats_search_views (version, doc, xpathCtx, statsnode,
1136 current_time);
1138 return 0;
1139 } /* }}} int bind_xml_stats */
1141 static int bind_xml (const char *data) /* {{{ */
1142 {
1143 xmlDoc *doc = NULL;
1144 xmlXPathContext *xpathCtx = NULL;
1145 xmlXPathObject *xpathObj = NULL;
1146 int ret = -1;
1147 int i;
1149 doc = xmlParseMemory (data, strlen (data));
1150 if (doc == NULL)
1151 {
1152 ERROR ("bind plugin: xmlParseMemory failed.");
1153 return (-1);
1154 }
1156 xpathCtx = xmlXPathNewContext (doc);
1157 if (xpathCtx == NULL)
1158 {
1159 ERROR ("bind plugin: xmlXPathNewContext failed.");
1160 xmlFreeDoc (doc);
1161 return (-1);
1162 }
1164 xpathObj = xmlXPathEvalExpression (BAD_CAST "/isc/bind/statistics", xpathCtx);
1165 if (xpathObj == NULL)
1166 {
1167 ERROR ("bind plugin: Cannot find the <statistics> tag.");
1168 xmlXPathFreeContext (xpathCtx);
1169 xmlFreeDoc (doc);
1170 return (-1);
1171 }
1172 else if (xpathObj->nodesetval == NULL)
1173 {
1174 ERROR ("bind plugin: xmlXPathEvalExpression failed.");
1175 xmlXPathFreeObject (xpathObj);
1176 xmlXPathFreeContext (xpathCtx);
1177 xmlFreeDoc (doc);
1178 return (-1);
1179 }
1181 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
1182 {
1183 xmlNode *node;
1184 char *attr_version;
1185 int parsed_version = 0;
1187 node = xpathObj->nodesetval->nodeTab[i];
1188 assert (node != NULL);
1190 attr_version = (char *) xmlGetProp (node, BAD_CAST "version");
1191 if (attr_version == NULL)
1192 {
1193 NOTICE ("bind plugin: Found <statistics> tag doesn't have a "
1194 "`version' attribute.");
1195 continue;
1196 }
1197 DEBUG ("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1199 /* At the time this plugin was written, version "1.0" was used by
1200 * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
1201 * that "1.*" and "2.*" don't introduce structural changes, so we just
1202 * check for the first two characters here. */
1203 if (strncmp ("1.", attr_version, strlen ("1.")) == 0)
1204 parsed_version = 1;
1205 else if (strncmp ("2.", attr_version, strlen ("2.")) == 0)
1206 parsed_version = 2;
1207 else
1208 {
1209 /* TODO: Use the complaint mechanism here. */
1210 NOTICE ("bind plugin: Found <statistics> tag with version `%s'. "
1211 "Unfortunately I have no clue how to parse that. "
1212 "Please open a bug report for this.", attr_version);
1213 xmlFree (attr_version);
1214 continue;
1215 }
1217 ret = bind_xml_stats (parsed_version,
1218 doc, xpathCtx, node);
1220 xmlFree (attr_version);
1221 /* One <statistics> node ought to be enough. */
1222 break;
1223 }
1225 xmlXPathFreeObject (xpathObj);
1226 xmlXPathFreeContext (xpathCtx);
1227 xmlFreeDoc (doc);
1229 return (ret);
1230 } /* }}} int bind_xml */
1232 static int bind_config_set_bool (const char *name, int *var, /* {{{ */
1233 oconfig_item_t *ci)
1234 {
1235 if ((ci->values_num != 1) || ( ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1236 {
1237 WARNING ("bind plugin: The `%s' option needs "
1238 "exactly one boolean argument.", name);
1239 return (-1);
1240 }
1242 if (ci->values[0].value.boolean)
1243 *var = 1;
1244 else
1245 *var = 0;
1246 return 0;
1247 } /* }}} int bind_config_set_bool */
1249 static int bind_config_add_view_zone (cb_view_t *view, /* {{{ */
1250 oconfig_item_t *ci)
1251 {
1252 char **tmp;
1254 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1255 {
1256 WARNING ("bind plugin: The `Zone' option needs "
1257 "exactly one string argument.");
1258 return (-1);
1259 }
1261 tmp = (char **) realloc (view->zones,
1262 sizeof (char *) * (view->zones_num + 1));
1263 if (tmp == NULL)
1264 {
1265 ERROR ("bind plugin: realloc failed.");
1266 return (-1);
1267 }
1268 view->zones = tmp;
1270 view->zones[view->zones_num] = strdup (ci->values[0].value.string);
1271 if (view->zones[view->zones_num] == NULL)
1272 {
1273 ERROR ("bind plugin: strdup failed.");
1274 return (-1);
1275 }
1276 view->zones_num++;
1278 return (0);
1279 } /* }}} int bind_config_add_view_zone */
1281 static int bind_config_add_view (oconfig_item_t *ci) /* {{{ */
1282 {
1283 cb_view_t *tmp;
1284 int i;
1286 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1287 {
1288 WARNING ("bind plugin: `View' blocks need exactly one string argument.");
1289 return (-1);
1290 }
1292 tmp = (cb_view_t *) realloc (views, sizeof (*views) * (views_num + 1));
1293 if (tmp == NULL)
1294 {
1295 ERROR ("bind plugin: realloc failed.");
1296 return (-1);
1297 }
1298 views = tmp;
1299 tmp = views + views_num;
1301 memset (tmp, 0, sizeof (*tmp));
1302 tmp->qtypes = 1;
1303 tmp->resolver_stats = 1;
1304 tmp->cacherrsets = 1;
1305 tmp->zones = NULL;
1306 tmp->zones_num = 0;
1308 tmp->name = strdup (ci->values[0].value.string);
1309 if (tmp->name == NULL)
1310 {
1311 ERROR ("bind plugin: strdup failed.");
1312 sfree (views);
1313 return (-1);
1314 }
1316 for (i = 0; i < ci->children_num; i++)
1317 {
1318 oconfig_item_t *child = ci->children + i;
1320 if (strcasecmp ("QTypes", child->key) == 0)
1321 bind_config_set_bool ("QTypes", &tmp->qtypes, child);
1322 else if (strcasecmp ("ResolverStats", child->key) == 0)
1323 bind_config_set_bool ("ResolverStats", &tmp->resolver_stats, child);
1324 else if (strcasecmp ("CacheRRSets", child->key) == 0)
1325 bind_config_set_bool ("CacheRRSets", &tmp->cacherrsets, child);
1326 else if (strcasecmp ("Zone", child->key) == 0)
1327 bind_config_add_view_zone (tmp, child);
1328 else
1329 {
1330 WARNING ("bind plugin: Unknown configuration option "
1331 "`%s' in view `%s' will be ignored.", child->key, tmp->name);
1332 }
1333 } /* for (i = 0; i < ci->children_num; i++) */
1335 views_num++;
1336 return (0);
1337 } /* }}} int bind_config_add_view */
1339 static int bind_config (oconfig_item_t *ci) /* {{{ */
1340 {
1341 int i;
1343 for (i = 0; i < ci->children_num; i++)
1344 {
1345 oconfig_item_t *child = ci->children + i;
1347 if (strcasecmp ("Url", child->key) == 0) {
1348 if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING))
1349 {
1350 WARNING ("bind plugin: The `Url' option needs "
1351 "exactly one string argument.");
1352 return (-1);
1353 }
1355 sfree (url);
1356 url = strdup (child->values[0].value.string);
1357 } else if (strcasecmp ("OpCodes", child->key) == 0)
1358 bind_config_set_bool ("OpCodes", &global_opcodes, child);
1359 else if (strcasecmp ("QTypes", child->key) == 0)
1360 bind_config_set_bool ("QTypes", &global_qtypes, child);
1361 else if (strcasecmp ("ServerStats", child->key) == 0)
1362 bind_config_set_bool ("ServerStats", &global_server_stats, child);
1363 else if (strcasecmp ("ZoneMaintStats", child->key) == 0)
1364 bind_config_set_bool ("ZoneMaintStats", &global_zone_maint_stats, child);
1365 else if (strcasecmp ("ResolverStats", child->key) == 0)
1366 bind_config_set_bool ("ResolverStats", &global_resolver_stats, child);
1367 else if (strcasecmp ("MemoryStats", child->key) == 0)
1368 bind_config_set_bool ("MemoryStats", &global_memory_stats, child);
1369 else if (strcasecmp ("View", child->key) == 0)
1370 bind_config_add_view (child);
1371 else if (strcasecmp ("ParseTime", child->key) == 0)
1372 cf_util_get_boolean (child, &config_parse_time);
1373 else
1374 {
1375 WARNING ("bind plugin: Unknown configuration option "
1376 "`%s' will be ignored.", child->key);
1377 }
1378 }
1380 return (0);
1381 } /* }}} int bind_config */
1383 static int bind_init (void) /* {{{ */
1384 {
1385 if (curl != NULL)
1386 return (0);
1388 curl = curl_easy_init ();
1389 if (curl == NULL)
1390 {
1391 ERROR ("bind plugin: bind_init: curl_easy_init failed.");
1392 return (-1);
1393 }
1395 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1L);
1396 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
1397 curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
1398 curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, bind_curl_error);
1399 curl_easy_setopt (curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL);
1400 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
1401 curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
1403 return (0);
1404 } /* }}} int bind_init */
1406 static int bind_read (void) /* {{{ */
1407 {
1408 int status;
1410 if (curl == NULL)
1411 {
1412 ERROR ("bind plugin: I don't have a CURL object.");
1413 return (-1);
1414 }
1416 bind_buffer_fill = 0;
1417 if (curl_easy_perform (curl) != CURLE_OK)
1418 {
1419 ERROR ("bind plugin: curl_easy_perform failed: %s",
1420 bind_curl_error);
1421 return (-1);
1422 }
1424 status = bind_xml (bind_buffer);
1425 if (status != 0)
1426 return (-1);
1427 else
1428 return (0);
1429 } /* }}} int bind_read */
1431 static int bind_shutdown (void) /* {{{ */
1432 {
1433 if (curl != NULL)
1434 {
1435 curl_easy_cleanup (curl);
1436 curl = NULL;
1437 }
1439 return (0);
1440 } /* }}} int bind_shutdown */
1442 void module_register (void)
1443 {
1444 plugin_register_complex_config ("bind", bind_config);
1445 plugin_register_init ("bind", bind_init);
1446 plugin_register_read ("bind", bind_read);
1447 plugin_register_shutdown ("bind", bind_shutdown);
1448 } /* void module_register */
1450 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */