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