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