Merge branch 'master' into er/ted
[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, int ds_type)
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       if (ds_type == DS_TYPE_GAUGE)
536         status = bind_xml_read_gauge (doc, counter, &value.gauge);
537       else
538         status = bind_xml_read_counter (doc, counter, &value.counter);
539       if (status != 0)
540         continue;
541
542       status = (*list_callback) (name, value, current_time, user_data);
543       if (status == 0)
544         num_entries++;
545
546       xmlFree (name);
547     }
548   }
549
550   DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
551       num_entries, (num_entries == 1) ? "entry" : "entries",
552       xpath_expression);
553
554   xmlXPathFreeObject(xpathObj);
555
556   return (0);
557 } /* }}} int bind_parse_generic_name_value */
558
559 /* 
560  * bind_parse_generic_value_list
561  *
562  * Reads statistics in the form:
563  * <foo>
564  *   <name0>123</name0>
565  *   <name1>234</name1>
566  *   <name2>345</name2>
567  *   :
568  * </foo>
569  */
570 static int bind_parse_generic_value_list (const char *xpath_expression, /* {{{ */
571     list_callback_t list_callback,
572     void *user_data,
573     xmlDoc *doc, xmlXPathContext *xpathCtx,
574     time_t current_time, int ds_type)
575 {
576   xmlXPathObject *xpathObj = NULL;
577   int num_entries;
578   int i;
579
580   xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx);
581   if (xpathObj == NULL)
582   {
583     ERROR("bind plugin: Unable to evaluate XPath expression `%s'.",
584         xpath_expression);
585     return (-1);
586   }
587
588   num_entries = 0;
589   /* Iterate over all matching nodes. */
590   for (i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++)
591   {
592     xmlNode *child;
593
594     /* Iterate over all child nodes. */
595     for (child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
596         child != NULL;
597         child = child->next)
598     {
599       char *node_name;
600       value_t value;
601       int status;
602
603       if (child->type != XML_ELEMENT_NODE)
604         continue;
605
606       node_name = (char *) child->name;
607
608       if (ds_type == DS_TYPE_GAUGE)
609         status = bind_xml_read_gauge (doc, child, &value.gauge);
610       else
611         status = bind_xml_read_counter (doc, child, &value.counter);
612       if (status != 0)
613         continue;
614
615       status = (*list_callback) (node_name, value, current_time, user_data);
616       if (status == 0)
617         num_entries++;
618     }
619   }
620
621   DEBUG ("bind plugin: Found %d %s for XPath expression `%s'",
622       num_entries, (num_entries == 1) ? "entry" : "entries",
623       xpath_expression);
624
625   xmlXPathFreeObject(xpathObj);
626
627   return (0);
628 } /* }}} int bind_parse_generic_value_list */
629
630 static int bind_xml_stats_handle_zone (int version, xmlDoc *doc, /* {{{ */
631     xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
632     time_t current_time)
633 {
634   xmlXPathObject *path_obj;
635   char *zone_name = NULL;
636   int i;
637   size_t j;
638
639   path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
640   if (path_obj == NULL)
641   {
642     ERROR ("bind plugin: xmlXPathEvalExpression failed.");
643     return (-1);
644   }
645
646   for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
647   {
648     zone_name = (char *) xmlNodeListGetString (doc,
649         path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
650     if (zone_name != NULL)
651       break;
652   }
653
654   if (zone_name == NULL)
655   {
656     ERROR ("bind plugin: Could not determine zone name.");
657     xmlXPathFreeObject (path_obj);
658     return (-1);
659   }
660
661   for (j = 0; j < view->zones_num; j++)
662   {
663     if (strcasecmp (zone_name, view->zones[j]) == 0)
664       break;
665   }
666
667   xmlFree (zone_name);
668   zone_name = NULL;
669
670   if (j >= views_num)
671   {
672     xmlXPathFreeObject (path_obj);
673     return (0);
674   }
675
676   zone_name = view->zones[j];
677
678   DEBUG ("bind plugin: bind_xml_stats_handle_zone: Found zone `%s'.",
679       zone_name);
680
681   { /* Parse the <counters> tag {{{ */
682     char plugin_instance[DATA_MAX_NAME_LEN];
683     translation_table_ptr_t table_ptr =
684     { 
685       nsstats_translation_table,
686       nsstats_translation_table_length,
687       plugin_instance
688     };
689
690     ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-zone-%s",
691         view->name, zone_name);
692
693     bind_parse_generic_value_list (/* xpath = */ "counters",
694         /* callback = */ bind_xml_table_callback,
695         /* user_data = */ &table_ptr,
696         doc, path_ctx, current_time, DS_TYPE_COUNTER);
697   } /* }}} */
698
699   xmlXPathFreeObject (path_obj);
700
701   return (0);
702 } /* }}} int bind_xml_stats_handle_zone */
703
704 static int bind_xml_stats_search_zones (int version, xmlDoc *doc, /* {{{ */
705     xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view,
706     time_t current_time)
707 {
708   xmlXPathObject *zone_nodes = NULL;
709   xmlXPathContext *zone_path_context;
710   int i;
711
712   zone_path_context = xmlXPathNewContext (doc);
713   if (zone_path_context == NULL)
714   {
715     ERROR ("bind plugin: xmlXPathNewContext failed.");
716     return (-1);
717   }
718
719   zone_nodes = xmlXPathEvalExpression (BAD_CAST "zones/zone", path_ctx);
720   if (zone_nodes == NULL)
721   {
722     ERROR ("bind plugin: Cannot find any <view> tags.");
723     xmlXPathFreeContext (zone_path_context);
724     return (-1);
725   }
726
727   for (i = 0; i < zone_nodes->nodesetval->nodeNr; i++)
728   {
729     xmlNode *node;
730
731     node = zone_nodes->nodesetval->nodeTab[i];
732     assert (node != NULL);
733
734     zone_path_context->node = node;
735
736     bind_xml_stats_handle_zone (version, doc, zone_path_context, node, view,
737         current_time);
738   }
739
740   xmlXPathFreeObject (zone_nodes);
741   xmlXPathFreeContext (zone_path_context);
742   return (0);
743 } /* }}} int bind_xml_stats_search_zones */
744
745 static int bind_xml_stats_handle_view (int version, xmlDoc *doc, /* {{{ */
746     xmlXPathContext *path_ctx, xmlNode *node, time_t current_time)
747 {
748   xmlXPathObject *path_obj;
749   char *view_name = NULL;
750   cb_view_t *view;
751   int i;
752   size_t j;
753
754   path_obj = xmlXPathEvalExpression (BAD_CAST "name", path_ctx);
755   if (path_obj == NULL)
756   {
757     ERROR ("bind plugin: xmlXPathEvalExpression failed.");
758     return (-1);
759   }
760
761   for (i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); i++)
762   {
763     view_name = (char *) xmlNodeListGetString (doc,
764         path_obj->nodesetval->nodeTab[i]->xmlChildrenNode, 1);
765     if (view_name != NULL)
766       break;
767   }
768
769   if (view_name == NULL)
770   {
771     ERROR ("bind plugin: Could not determine view name.");
772     xmlXPathFreeObject (path_obj);
773     return (-1);
774   }
775
776   for (j = 0; j < views_num; j++)
777   {
778     if (strcasecmp (view_name, views[j].name) == 0)
779       break;
780   }
781
782   xmlFree (view_name);
783   xmlXPathFreeObject (path_obj);
784
785   view_name = NULL;
786   path_obj = NULL;
787
788   if (j >= views_num)
789     return (0);
790
791   view = views + j;
792
793   DEBUG ("bind plugin: bind_xml_stats_handle_view: Found view `%s'.",
794       view->name);
795
796   if (view->qtypes != 0) /* {{{ */
797   {
798     char plugin_instance[DATA_MAX_NAME_LEN];
799     list_info_ptr_t list_info =
800     {
801       plugin_instance,
802       /* type = */ "dns_qtype_gauge"
803     };
804
805     ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-qtypes",
806         view->name);
807
808     bind_parse_generic_name_value (/* xpath = */ "rdtype",
809         /* callback = */ bind_xml_list_callback,
810         /* user_data = */ &list_info,
811         doc, path_ctx, current_time, DS_TYPE_COUNTER);
812   } /* }}} */
813
814   if (view->resolver_stats != 0) /* {{{ */
815   {
816     char plugin_instance[DATA_MAX_NAME_LEN];
817     translation_table_ptr_t table_ptr =
818     { 
819       resstats_translation_table,
820       resstats_translation_table_length,
821       plugin_instance
822     };
823
824     ssnprintf (plugin_instance, sizeof (plugin_instance),
825         "%s-resolver_stats", view->name);
826
827     bind_parse_generic_name_value ("resstat",
828         /* callback = */ bind_xml_table_callback,
829         /* user_data = */ &table_ptr,
830         doc, path_ctx, current_time, DS_TYPE_COUNTER);
831   } /* }}} */
832
833   if (view->cacherrsets != 0) /* {{{ */
834   {
835     char plugin_instance[DATA_MAX_NAME_LEN];
836     list_info_ptr_t list_info =
837     {
838       plugin_instance,
839       /* type = */ "dns_qtype_gauge"
840     };
841
842     ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-cache_rr_sets",
843         view->name);
844
845     bind_parse_generic_name_value (/* xpath = */ "cache/rrset",
846         /* callback = */ bind_xml_list_callback,
847         /* user_data = */ &list_info,
848         doc, path_ctx, current_time, DS_TYPE_GAUGE);
849   } /* }}} */
850
851   if (view->zones_num > 0)
852     bind_xml_stats_search_zones (version, doc, path_ctx, node, view,
853         current_time);
854
855   return (0);
856 } /* }}} int bind_xml_stats_handle_view */
857
858 static int bind_xml_stats_search_views (int version, xmlDoc *doc, /* {{{ */
859     xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time)
860 {
861   xmlXPathObject *view_nodes = NULL;
862   xmlXPathContext *view_path_context;
863   int i;
864
865   view_path_context = xmlXPathNewContext (doc);
866   if (view_path_context == NULL)
867   {
868     ERROR ("bind plugin: xmlXPathNewContext failed.");
869     return (-1);
870   }
871
872   view_nodes = xmlXPathEvalExpression (BAD_CAST "views/view", xpathCtx);
873   if (view_nodes == NULL)
874   {
875     ERROR ("bind plugin: Cannot find any <view> tags.");
876     xmlXPathFreeContext (view_path_context);
877     return (-1);
878   }
879
880   for (i = 0; i < view_nodes->nodesetval->nodeNr; i++)
881   {
882     xmlNode *node;
883
884     node = view_nodes->nodesetval->nodeTab[i];
885     assert (node != NULL);
886
887     view_path_context->node = node;
888
889     bind_xml_stats_handle_view (version, doc, view_path_context, node,
890         current_time);
891   }
892
893   xmlXPathFreeObject (view_nodes);
894   xmlXPathFreeContext (view_path_context);
895   return (0);
896 } /* }}} int bind_xml_stats_search_views */
897
898 static int bind_xml_stats (int version, xmlDoc *doc, /* {{{ */
899     xmlXPathContext *xpathCtx, xmlNode *statsnode)
900 {
901   time_t current_time = 0;
902   int status;
903
904   xpathCtx->node = statsnode;
905
906   /* TODO: Check `server/boot-time' to recognize server restarts. */
907
908   status = bind_xml_read_timestamp ("server/current-time",
909       doc, xpathCtx, &current_time);
910   if (status != 0)
911   {
912     ERROR ("bind plugin: Reading `server/current-time' failed.");
913     return (-1);
914   }
915   DEBUG ("bind plugin: Current server time is %i.", (int) current_time);
916
917   /* XPath:     server/requests/opcode
918    * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
919    * Layout:
920    *   <opcode>
921    *     <name>A</name>
922    *     <counter>1</counter>
923    *   </opcode>
924    *   :
925    */
926   if (global_opcodes != 0)
927   {
928     list_info_ptr_t list_info =
929     {
930       /* plugin instance = */ "global-opcodes",
931       /* type = */ "dns_opcode"
932     };
933
934     bind_parse_generic_name_value (/* xpath = */ "server/requests/opcode",
935         /* callback = */ bind_xml_list_callback,
936         /* user_data = */ &list_info,
937         doc, xpathCtx, current_time, DS_TYPE_COUNTER);
938   }
939
940   /* XPath:     server/queries-in/rdtype
941    * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
942    *            X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
943    *            SPF, TKEY, IXFR, AXFR, ANY, ..., Others
944    * Layout:
945    *   <rdtype>
946    *     <name>A</name>
947    *     <counter>1</counter>
948    *   </rdtype>
949    *   :
950    */
951   if (global_qtypes != 0)
952   {
953     list_info_ptr_t list_info =
954     {
955       /* plugin instance = */ "global-qtypes",
956       /* type = */ "dns_qtype"
957     };
958
959     bind_parse_generic_name_value (/* xpath = */ "server/queries-in/rdtype",
960         /* callback = */ bind_xml_list_callback,
961         /* user_data = */ &list_info,
962         doc, xpathCtx, current_time, DS_TYPE_COUNTER);
963   }
964   
965   /* XPath:     server/nsstats, server/nsstat
966    * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
967    *            ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
968    *            UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
969    *            RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
970    *            QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
971    *            QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
972    *            UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
973    *            UpdateBadPrereq
974    * Layout v1:
975    *   <nsstats>
976    *     <Requestv4>1</Requestv4>
977    *     <Requestv6>0</Requestv6>
978    *     :
979    *   </nsstats>
980    * Layout v2:
981    *   <nsstat>
982    *     <name>Requestv4</name>
983    *     <counter>1</counter>
984    *   </nsstat>
985    *   <nsstat>
986    *     <name>Requestv6</name>
987    *     <counter>0</counter>
988    *   </nsstat>
989    *   :
990    */
991   if (global_server_stats)
992   {
993     translation_table_ptr_t table_ptr =
994     { 
995       nsstats_translation_table,
996       nsstats_translation_table_length,
997       /* plugin_instance = */ "global-server_stats"
998     };
999
1000     if (version == 1)
1001     {
1002       bind_parse_generic_value_list ("server/nsstats",
1003           /* callback = */ bind_xml_table_callback,
1004           /* user_data = */ &table_ptr,
1005           doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1006     }
1007     else
1008     {
1009       bind_parse_generic_name_value ("server/nsstat",
1010           /* callback = */ bind_xml_table_callback,
1011           /* user_data = */ &table_ptr,
1012           doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1013     }
1014   }
1015
1016   /* XPath:     server/zonestats, server/zonestat
1017    * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1018    *            SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1019    *            XfrSuccess, XfrFail
1020    * Layout v1:
1021    *   <zonestats>
1022    *     <NotifyOutv4>0</NotifyOutv4>
1023    *     <NotifyOutv6>0</NotifyOutv6>
1024    *     :
1025    *   </zonestats>
1026    * Layout v2:
1027    *   <zonestat>
1028    *     <name>NotifyOutv4</name>
1029    *     <counter>0</counter>
1030    *   </zonestat>
1031    *   <zonestat>
1032    *     <name>NotifyOutv6</name>
1033    *     <counter>0</counter>
1034    *   </zonestat>
1035    *   :
1036    */
1037   if (global_zone_maint_stats)
1038   {
1039     translation_table_ptr_t table_ptr =
1040     { 
1041       zonestats_translation_table,
1042       zonestats_translation_table_length,
1043       /* plugin_instance = */ "global-zone_maint_stats"
1044     };
1045
1046     if (version == 1)
1047     {
1048       bind_parse_generic_value_list ("server/zonestats",
1049           /* callback = */ bind_xml_table_callback,
1050           /* user_data = */ &table_ptr,
1051           doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1052     }
1053     else
1054     {
1055       bind_parse_generic_name_value ("server/zonestat",
1056           /* callback = */ bind_xml_table_callback,
1057           /* user_data = */ &table_ptr,
1058           doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1059     }
1060   }
1061
1062   /* XPath:     server/resstats
1063    * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1064    *            FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1065    *            Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1066    *            GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1067    * Layout v1:
1068    *   <resstats>
1069    *     <Queryv4>0</Queryv4>
1070    *     <Queryv6>0</Queryv6>
1071    *     :
1072    *   </resstats>
1073    * Layout v2:
1074    *   <resstat>
1075    *     <name>Queryv4</name>
1076    *     <counter>0</counter>
1077    *   </resstat>
1078    *   <resstat>
1079    *     <name>Queryv6</name>
1080    *     <counter>0</counter>
1081    *   </resstat>
1082    *   :
1083    */
1084   if (global_resolver_stats != 0)
1085   {
1086     translation_table_ptr_t table_ptr =
1087     { 
1088       resstats_translation_table,
1089       resstats_translation_table_length,
1090       /* plugin_instance = */ "global-resolver_stats"
1091     };
1092
1093     if (version == 1)
1094     {
1095       bind_parse_generic_value_list ("server/resstats",
1096           /* callback = */ bind_xml_table_callback,
1097           /* user_data = */ &table_ptr,
1098           doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1099     }
1100     else
1101     {
1102       bind_parse_generic_name_value ("server/resstat",
1103           /* callback = */ bind_xml_table_callback,
1104           /* user_data = */ &table_ptr,
1105           doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1106     }
1107   }
1108
1109   /* XPath:  memory/summary
1110    * Variables: TotalUse, InUse, BlockSize, ContextSize, Lost
1111    * Layout: v2:
1112    *   <summary>
1113    *     <TotalUse>6587096</TotalUse>
1114    *     <InUse>1345424</InUse>
1115    *     <BlockSize>5505024</BlockSize>
1116    *     <ContextSize>3732456</ContextSize>
1117    *     <Lost>0</Lost>
1118    *   </summary>
1119    */
1120   if (global_memory_stats != 0)
1121   {
1122     translation_table_ptr_t table_ptr =
1123     {
1124       memsummary_translation_table,
1125       memsummary_translation_table_length,
1126       /* plugin_instance = */ "global-memory_stats"
1127     };
1128
1129     bind_parse_generic_value_list ("memory/summary",
1130           /* callback = */ bind_xml_table_callback,
1131           /* user_data = */ &table_ptr,
1132           doc, xpathCtx, current_time, DS_TYPE_GAUGE);
1133   }
1134
1135   if (views_num > 0)
1136     bind_xml_stats_search_views (version, doc, xpathCtx, statsnode,
1137         current_time);
1138
1139   return 0;
1140 } /* }}} int bind_xml_stats */
1141
1142 static int bind_xml (const char *data) /* {{{ */
1143 {
1144   xmlDoc *doc = NULL;
1145   xmlXPathContext *xpathCtx = NULL;
1146   xmlXPathObject *xpathObj = NULL;
1147   int ret = -1;
1148   int i;
1149
1150   doc = xmlParseMemory (data, strlen (data));
1151   if (doc == NULL)
1152   {
1153     ERROR ("bind plugin: xmlParseMemory failed.");
1154     return (-1);
1155   }
1156
1157   xpathCtx = xmlXPathNewContext (doc);
1158   if (xpathCtx == NULL)
1159   {
1160     ERROR ("bind plugin: xmlXPathNewContext failed.");
1161     xmlFreeDoc (doc);
1162     return (-1);
1163   }
1164
1165   xpathObj = xmlXPathEvalExpression (BAD_CAST "/isc/bind/statistics", xpathCtx);
1166   if (xpathObj == NULL)
1167   {
1168     ERROR ("bind plugin: Cannot find the <statistics> tag.");
1169     xmlXPathFreeContext (xpathCtx);
1170     xmlFreeDoc (doc);
1171     return (-1);
1172   }
1173   else if (xpathObj->nodesetval == NULL)
1174   {
1175     ERROR ("bind plugin: xmlXPathEvalExpression failed.");
1176     xmlXPathFreeObject (xpathObj);
1177     xmlXPathFreeContext (xpathCtx);
1178     xmlFreeDoc (doc);
1179     return (-1);
1180   }
1181
1182   for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
1183   {
1184     xmlNode *node;
1185     char *attr_version;
1186     int parsed_version = 0;
1187
1188     node = xpathObj->nodesetval->nodeTab[i];
1189     assert (node != NULL);
1190
1191     attr_version = (char *) xmlGetProp (node, BAD_CAST "version");
1192     if (attr_version == NULL)
1193     {
1194       NOTICE ("bind plugin: Found <statistics> tag doesn't have a "
1195           "`version' attribute.");
1196       continue;
1197     }
1198     DEBUG ("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1199
1200     /* At the time this plugin was written, version "1.0" was used by
1201      * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
1202      * that "1.*" and "2.*" don't introduce structural changes, so we just
1203      * check for the first two characters here. */
1204     if (strncmp ("1.", attr_version, strlen ("1.")) == 0)
1205       parsed_version = 1;
1206     else if (strncmp ("2.", attr_version, strlen ("2.")) == 0)
1207       parsed_version = 2;
1208     else
1209     {
1210       /* TODO: Use the complaint mechanism here. */
1211       NOTICE ("bind plugin: Found <statistics> tag with version `%s'. "
1212           "Unfortunately I have no clue how to parse that. "
1213           "Please open a bug report for this.", attr_version);
1214       xmlFree (attr_version);
1215       continue;
1216     }
1217
1218     ret = bind_xml_stats (parsed_version,
1219         doc, xpathCtx, node);
1220
1221     xmlFree (attr_version);
1222     /* One <statistics> node ought to be enough. */
1223     break;
1224   }
1225
1226   xmlXPathFreeObject (xpathObj);
1227   xmlXPathFreeContext (xpathCtx);
1228   xmlFreeDoc (doc);
1229
1230   return (ret);
1231 } /* }}} int bind_xml */
1232
1233 static int bind_config_set_bool (const char *name, int *var, /* {{{ */
1234     oconfig_item_t *ci)
1235 {
1236   if ((ci->values_num != 1) || ( ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1237   {
1238     WARNING ("bind plugin: The `%s' option needs "
1239         "exactly one boolean argument.", name);
1240     return (-1);
1241   }
1242
1243   if (ci->values[0].value.boolean)
1244     *var = 1;
1245   else
1246     *var = 0;
1247   return 0;
1248 } /* }}} int bind_config_set_bool */
1249
1250 static int bind_config_add_view_zone (cb_view_t *view, /* {{{ */
1251     oconfig_item_t *ci)
1252 {
1253   char **tmp;
1254
1255   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1256   {
1257     WARNING ("bind plugin: The `Zone' option needs "
1258         "exactly one string argument.");
1259     return (-1);
1260   }
1261
1262   tmp = (char **) realloc (view->zones,
1263       sizeof (char *) * (view->zones_num + 1));
1264   if (tmp == NULL)
1265   {
1266     ERROR ("bind plugin: realloc failed.");
1267     return (-1);
1268   }
1269   view->zones = tmp;
1270
1271   view->zones[view->zones_num] = strdup (ci->values[0].value.string);
1272   if (view->zones[view->zones_num] == NULL)
1273   {
1274     ERROR ("bind plugin: strdup failed.");
1275     return (-1);
1276   }
1277   view->zones_num++;
1278
1279   return (0);
1280 } /* }}} int bind_config_add_view_zone */
1281
1282 static int bind_config_add_view (oconfig_item_t *ci) /* {{{ */
1283 {
1284   cb_view_t *tmp;
1285   int i;
1286
1287   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1288   {
1289     WARNING ("bind plugin: `View' blocks need exactly one string argument.");
1290     return (-1);
1291   }
1292
1293   tmp = (cb_view_t *) realloc (views, sizeof (*views) * (views_num + 1));
1294   if (tmp == NULL)
1295   {
1296     ERROR ("bind plugin: realloc failed.");
1297     return (-1);
1298   }
1299   views = tmp;
1300   tmp = views + views_num;
1301
1302   memset (tmp, 0, sizeof (*tmp));
1303   tmp->qtypes = 1;
1304   tmp->resolver_stats = 1;
1305   tmp->cacherrsets = 1;
1306   tmp->zones = NULL;
1307   tmp->zones_num = 0;
1308
1309   tmp->name = strdup (ci->values[0].value.string);
1310   if (tmp->name == NULL)
1311   {
1312     ERROR ("bind plugin: strdup failed.");
1313     free (tmp);
1314     return (-1);
1315   }
1316
1317   for (i = 0; i < ci->children_num; i++)
1318   {
1319     oconfig_item_t *child = ci->children + i;
1320
1321     if (strcasecmp ("QTypes", child->key) == 0)
1322       bind_config_set_bool ("QTypes", &tmp->qtypes, child);
1323     else if (strcasecmp ("ResolverStats", child->key) == 0)
1324       bind_config_set_bool ("ResolverStats", &tmp->resolver_stats, child);
1325     else if (strcasecmp ("CacheRRSets", child->key) == 0)
1326       bind_config_set_bool ("CacheRRSets", &tmp->cacherrsets, child);
1327     else if (strcasecmp ("Zone", child->key) == 0)
1328       bind_config_add_view_zone (tmp, child);
1329     else
1330     {
1331       WARNING ("bind plugin: Unknown configuration option "
1332           "`%s' in view `%s' will be ignored.", child->key, tmp->name);
1333     }
1334   } /* for (i = 0; i < ci->children_num; i++) */
1335
1336   views_num++;
1337   return (0);
1338 } /* }}} int bind_config_add_view */
1339
1340 static int bind_config (oconfig_item_t *ci) /* {{{ */
1341 {
1342   int i;
1343
1344   for (i = 0; i < ci->children_num; i++)
1345   {
1346     oconfig_item_t *child = ci->children + i;
1347
1348     if (strcasecmp ("Url", child->key) == 0) {
1349       if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING))
1350       {
1351         WARNING ("bind plugin: The `Url' option needs "
1352                  "exactly one string argument.");
1353         return (-1);
1354       }
1355
1356       url = strdup (child->values[0].value.string);
1357     } else if (strcasecmp ("OpCodes", child->key) == 0)
1358       bind_config_set_bool ("OpCodes", &global_opcodes, child);
1359     else if (strcasecmp ("QTypes", child->key) == 0)
1360       bind_config_set_bool ("QTypes", &global_qtypes, child);
1361     else if (strcasecmp ("ServerStats", child->key) == 0)
1362       bind_config_set_bool ("ServerStats", &global_server_stats, child);
1363     else if (strcasecmp ("ZoneMaintStats", child->key) == 0)
1364       bind_config_set_bool ("ZoneMaintStats", &global_zone_maint_stats, child);
1365     else if (strcasecmp ("ResolverStats", child->key) == 0)
1366       bind_config_set_bool ("ResolverStats", &global_resolver_stats, child);
1367     else if (strcasecmp ("MemoryStats", child->key) == 0)
1368       bind_config_set_bool ("MemoryStats", &global_memory_stats, child);
1369     else if (strcasecmp ("View", child->key) == 0)
1370       bind_config_add_view (child);
1371     else
1372     {
1373       WARNING ("bind plugin: Unknown configuration option "
1374           "`%s' will be ignored.", child->key);
1375     }
1376   }
1377
1378   return (0);
1379 } /* }}} int bind_config */
1380
1381 static int bind_init (void) /* {{{ */
1382 {
1383   if (curl != NULL)
1384     return (0);
1385
1386   curl = curl_easy_init ();
1387   if (curl == NULL)
1388   {
1389     ERROR ("bind plugin: bind_init: curl_easy_init failed.");
1390     return (-1);
1391   }
1392
1393   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
1394   curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
1395   curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, bind_curl_error);
1396   curl_easy_setopt (curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL);
1397
1398   return (0);
1399 } /* }}} int bind_init */
1400
1401 static int bind_read (void) /* {{{ */
1402 {
1403   int status;
1404
1405   if (curl == NULL)
1406   {
1407     ERROR ("bind plugin: I don't have a CURL object.");
1408     return (-1);
1409   }
1410
1411   bind_buffer_fill = 0;
1412   if (curl_easy_perform (curl) != 0)
1413   {
1414     ERROR ("bind plugin: curl_easy_perform failed: %s",
1415         bind_curl_error);
1416     return (-1);
1417   }
1418
1419   status = bind_xml (bind_buffer);
1420   if (status != 0)
1421     return (-1);
1422   else
1423     return (0);
1424 } /* }}} int bind_read */
1425
1426 static int bind_shutdown (void) /* {{{ */
1427 {
1428   if (curl != NULL)
1429   {
1430     curl_easy_cleanup (curl);
1431     curl = NULL;
1432   }
1433
1434   return (0);
1435 } /* }}} int bind_shutdown */
1436
1437 void module_register (void)
1438 {
1439   plugin_register_complex_config ("bind", bind_config);
1440   plugin_register_init ("bind", bind_init);
1441   plugin_register_read ("bind", bind_read);
1442   plugin_register_shutdown ("bind", bind_shutdown);
1443 } /* void module_register */
1444
1445 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */