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