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