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_gauge"
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   if (view->cacherrsets != 0) /* {{{ */
829   {
830     char plugin_instance[DATA_MAX_NAME_LEN];
831     list_info_ptr_t list_info =
832     {
833       plugin_instance,
834       /* type = */ "dns_qtype_gauge"
835     };
836
837     ssnprintf (plugin_instance, sizeof (plugin_instance), "%s-cache_rr_sets",
838         view->name);
839
840     bind_parse_generic_name_value (/* xpath = */ "cache/rrset",
841         /* callback = */ bind_xml_list_callback,
842         /* user_data = */ &list_info,
843         doc, path_ctx, current_time, DS_TYPE_GAUGE);
844   } /* }}} */
845
846   if (view->zones_num > 0)
847     bind_xml_stats_search_zones (version, doc, path_ctx, node, view,
848         current_time);
849
850   return (0);
851 } /* }}} int bind_xml_stats_handle_view */
852
853 static int bind_xml_stats_search_views (int version, xmlDoc *doc, /* {{{ */
854     xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time)
855 {
856   xmlXPathObject *view_nodes = NULL;
857   xmlXPathContext *view_path_context;
858   int i;
859
860   view_path_context = xmlXPathNewContext (doc);
861   if (view_path_context == NULL)
862   {
863     ERROR ("bind plugin: xmlXPathNewContext failed.");
864     return (-1);
865   }
866
867   view_nodes = xmlXPathEvalExpression (BAD_CAST "views/view", xpathCtx);
868   if (view_nodes == NULL)
869   {
870     ERROR ("bind plugin: Cannot find any <view> tags.");
871     xmlXPathFreeContext (view_path_context);
872     return (-1);
873   }
874
875   for (i = 0; i < view_nodes->nodesetval->nodeNr; i++)
876   {
877     xmlNode *node;
878
879     node = view_nodes->nodesetval->nodeTab[i];
880     assert (node != NULL);
881
882     view_path_context->node = node;
883
884     bind_xml_stats_handle_view (version, doc, view_path_context, node,
885         current_time);
886   }
887
888   xmlXPathFreeObject (view_nodes);
889   xmlXPathFreeContext (view_path_context);
890   return (0);
891 } /* }}} int bind_xml_stats_search_views */
892
893 static int bind_xml_stats (int version, xmlDoc *doc, /* {{{ */
894     xmlXPathContext *xpathCtx, xmlNode *statsnode)
895 {
896   time_t current_time = 0;
897   int status;
898
899   xpathCtx->node = statsnode;
900
901   /* TODO: Check `server/boot-time' to recognize server restarts. */
902
903   status = bind_xml_read_timestamp ("server/current-time",
904       doc, xpathCtx, &current_time);
905   if (status != 0)
906   {
907     ERROR ("bind plugin: Reading `server/current-time' failed.");
908     return (-1);
909   }
910   DEBUG ("bind plugin: Current server time is %i.", (int) current_time);
911
912   /* XPath:     server/requests/opcode
913    * Variables: QUERY, IQUERY, NOTIFY, UPDATE, ...
914    * Layout:
915    *   <opcode>
916    *     <name>A</name>
917    *     <counter>1</counter>
918    *   </opcode>
919    *   :
920    */
921   if (global_opcodes != 0)
922   {
923     list_info_ptr_t list_info =
924     {
925       /* plugin instance = */ "global-opcodes",
926       /* type = */ "dns_opcode"
927     };
928
929     bind_parse_generic_name_value (/* xpath = */ "server/requests/opcode",
930         /* callback = */ bind_xml_list_callback,
931         /* user_data = */ &list_info,
932         doc, xpathCtx, current_time, DS_TYPE_COUNTER);
933   }
934
935   /* XPath:     server/queries-in/rdtype
936    * Variables: RESERVED0, A, NS, CNAME, SOA, MR, PTR, HINFO, MX, TXT, RP,
937    *            X25, PX, AAAA, LOC, SRV, NAPTR, A6, DS, RRSIG, NSEC, DNSKEY,
938    *            SPF, TKEY, IXFR, AXFR, ANY, ..., Others
939    * Layout:
940    *   <rdtype>
941    *     <name>A</name>
942    *     <counter>1</counter>
943    *   </rdtype>
944    *   :
945    */
946   if (global_qtypes != 0)
947   {
948     list_info_ptr_t list_info =
949     {
950       /* plugin instance = */ "global-qtypes",
951       /* type = */ "dns_qtype"
952     };
953
954     bind_parse_generic_name_value (/* xpath = */ "server/queries-in/rdtype",
955         /* callback = */ bind_xml_list_callback,
956         /* user_data = */ &list_info,
957         doc, xpathCtx, current_time, DS_TYPE_COUNTER);
958   }
959   
960   /* XPath:     server/nsstats, server/nsstat
961    * Variables: Requestv4, Requestv6, ReqEdns0, ReqBadEDNSVer, ReqTSIG,
962    *            ReqSIG0, ReqBadSIG, ReqTCP, AuthQryRej, RecQryRej, XfrRej,
963    *            UpdateRej, Response, TruncatedResp, RespEDNS0, RespTSIG,
964    *            RespSIG0, QrySuccess, QryAuthAns, QryNoauthAns, QryReferral,
965    *            QryNxrrset, QrySERVFAIL, QryFORMERR, QryNXDOMAIN, QryRecursion,
966    *            QryDuplicate, QryDropped, QryFailure, XfrReqDone, UpdateReqFwd,
967    *            UpdateRespFwd, UpdateFwdFail, UpdateDone, UpdateFail,
968    *            UpdateBadPrereq
969    * Layout v1:
970    *   <nsstats>
971    *     <Requestv4>1</Requestv4>
972    *     <Requestv6>0</Requestv6>
973    *     :
974    *   </nsstats>
975    * Layout v2:
976    *   <nsstat>
977    *     <name>Requestv4</name>
978    *     <counter>1</counter>
979    *   </nsstat>
980    *   <nsstat>
981    *     <name>Requestv6</name>
982    *     <counter>0</counter>
983    *   </nsstat>
984    *   :
985    */
986   if (global_server_stats)
987   {
988     translation_table_ptr_t table_ptr =
989     { 
990       nsstats_translation_table,
991       nsstats_translation_table_length,
992       /* plugin_instance = */ "global-server_stats"
993     };
994
995     if (version == 1)
996     {
997       bind_parse_generic_value_list ("server/nsstats",
998           /* callback = */ bind_xml_table_callback,
999           /* user_data = */ &table_ptr,
1000           doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1001     }
1002     else
1003     {
1004       bind_parse_generic_name_value ("server/nsstat",
1005           /* callback = */ bind_xml_table_callback,
1006           /* user_data = */ &table_ptr,
1007           doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1008     }
1009   }
1010
1011   /* XPath:     server/zonestats, server/zonestat
1012    * Variables: NotifyOutv4, NotifyOutv6, NotifyInv4, NotifyInv6, NotifyRej,
1013    *            SOAOutv4, SOAOutv6, AXFRReqv4, AXFRReqv6, IXFRReqv4, IXFRReqv6,
1014    *            XfrSuccess, XfrFail
1015    * Layout v1:
1016    *   <zonestats>
1017    *     <NotifyOutv4>0</NotifyOutv4>
1018    *     <NotifyOutv6>0</NotifyOutv6>
1019    *     :
1020    *   </zonestats>
1021    * Layout v2:
1022    *   <zonestat>
1023    *     <name>NotifyOutv4</name>
1024    *     <counter>0</counter>
1025    *   </zonestat>
1026    *   <zonestat>
1027    *     <name>NotifyOutv6</name>
1028    *     <counter>0</counter>
1029    *   </zonestat>
1030    *   :
1031    */
1032   if (global_zone_maint_stats)
1033   {
1034     translation_table_ptr_t table_ptr =
1035     { 
1036       zonestats_translation_table,
1037       zonestats_translation_table_length,
1038       /* plugin_instance = */ "global-zone_maint_stats"
1039     };
1040
1041     if (version == 1)
1042     {
1043       bind_parse_generic_value_list ("server/zonestats",
1044           /* callback = */ bind_xml_table_callback,
1045           /* user_data = */ &table_ptr,
1046           doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1047     }
1048     else
1049     {
1050       bind_parse_generic_name_value ("server/zonestat",
1051           /* callback = */ bind_xml_table_callback,
1052           /* user_data = */ &table_ptr,
1053           doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1054     }
1055   }
1056
1057   /* XPath:     server/resstats
1058    * Variables: Queryv4, Queryv6, Responsev4, Responsev6, NXDOMAIN, SERVFAIL,
1059    *            FORMERR, OtherError, EDNS0Fail, Mismatch, Truncated, Lame,
1060    *            Retry, GlueFetchv4, GlueFetchv6, GlueFetchv4Fail,
1061    *            GlueFetchv6Fail, ValAttempt, ValOk, ValNegOk, ValFail
1062    * Layout v1:
1063    *   <resstats>
1064    *     <Queryv4>0</Queryv4>
1065    *     <Queryv6>0</Queryv6>
1066    *     :
1067    *   </resstats>
1068    * Layout v2:
1069    *   <resstat>
1070    *     <name>Queryv4</name>
1071    *     <counter>0</counter>
1072    *   </resstat>
1073    *   <resstat>
1074    *     <name>Queryv6</name>
1075    *     <counter>0</counter>
1076    *   </resstat>
1077    *   :
1078    */
1079   if (global_resolver_stats != 0)
1080   {
1081     translation_table_ptr_t table_ptr =
1082     { 
1083       resstats_translation_table,
1084       resstats_translation_table_length,
1085       /* plugin_instance = */ "global-resolver_stats"
1086     };
1087
1088     if (version == 1)
1089     {
1090       bind_parse_generic_value_list ("server/resstats",
1091           /* callback = */ bind_xml_table_callback,
1092           /* user_data = */ &table_ptr,
1093           doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1094     }
1095     else
1096     {
1097       bind_parse_generic_name_value ("server/resstat",
1098           /* callback = */ bind_xml_table_callback,
1099           /* user_data = */ &table_ptr,
1100           doc, xpathCtx, current_time, DS_TYPE_COUNTER);
1101     }
1102   }
1103
1104   /* XPath:  memory/summary
1105    * Variables: TotalUse, InUse, BlockSize, ContextSize, Lost
1106    * Layout: v2:
1107    *   <summary>
1108    *     <TotalUse>6587096</TotalUse>
1109    *     <InUse>1345424</InUse>
1110    *     <BlockSize>5505024</BlockSize>
1111    *     <ContextSize>3732456</ContextSize>
1112    *     <Lost>0</Lost>
1113    *   </summary>
1114    */
1115   if (global_memory_stats != 0)
1116   {
1117     translation_table_ptr_t table_ptr =
1118     {
1119       memsummary_translation_table,
1120       memsummary_translation_table_length,
1121       /* plugin_instance = */ "global-memory_stats"
1122     };
1123
1124     bind_parse_generic_value_list ("memory/summary",
1125           /* callback = */ bind_xml_table_callback,
1126           /* user_data = */ &table_ptr,
1127           doc, xpathCtx, current_time, DS_TYPE_GAUGE);
1128   }
1129
1130   if (views_num > 0)
1131     bind_xml_stats_search_views (version, doc, xpathCtx, statsnode,
1132         current_time);
1133
1134   return 0;
1135 } /* }}} int bind_xml_stats */
1136
1137 static int bind_xml (const char *data) /* {{{ */
1138 {
1139   xmlDoc *doc = NULL;
1140   xmlXPathContext *xpathCtx = NULL;
1141   xmlXPathObject *xpathObj = NULL;
1142   int ret = -1;
1143   int i;
1144
1145   doc = xmlParseMemory (data, strlen (data));
1146   if (doc == NULL)
1147   {
1148     ERROR ("bind plugin: xmlParseMemory failed.");
1149     return (-1);
1150   }
1151
1152   xpathCtx = xmlXPathNewContext (doc);
1153   if (xpathCtx == NULL)
1154   {
1155     ERROR ("bind plugin: xmlXPathNewContext failed.");
1156     xmlFreeDoc (doc);
1157     return (-1);
1158   }
1159
1160   xpathObj = xmlXPathEvalExpression (BAD_CAST "/isc/bind/statistics", xpathCtx);
1161   if (xpathObj == NULL)
1162   {
1163     ERROR ("bind plugin: Cannot find the <statistics> tag.");
1164     xmlXPathFreeContext (xpathCtx);
1165     xmlFreeDoc (doc);
1166     return (-1);
1167   }
1168   else if (xpathObj->nodesetval == NULL)
1169   {
1170     ERROR ("bind plugin: xmlXPathEvalExpression failed.");
1171     xmlXPathFreeObject (xpathObj);
1172     xmlXPathFreeContext (xpathCtx);
1173     xmlFreeDoc (doc);
1174     return (-1);
1175   }
1176
1177   for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
1178   {
1179     xmlNode *node;
1180     char *attr_version;
1181     int parsed_version = 0;
1182
1183     node = xpathObj->nodesetval->nodeTab[i];
1184     assert (node != NULL);
1185
1186     attr_version = (char *) xmlGetProp (node, BAD_CAST "version");
1187     if (attr_version == NULL)
1188     {
1189       NOTICE ("bind plugin: Found <statistics> tag doesn't have a "
1190           "`version' attribute.");
1191       continue;
1192     }
1193     DEBUG ("bind plugin: Found: <statistics version=\"%s\">", attr_version);
1194
1195     /* At the time this plugin was written, version "1.0" was used by
1196      * BIND 9.5.0, version "2.0" was used by BIND 9.5.1 and 9.6.0. We assume
1197      * that "1.*" and "2.*" don't introduce structural changes, so we just
1198      * check for the first two characters here. */
1199     if (strncmp ("1.", attr_version, strlen ("1.")) == 0)
1200       parsed_version = 1;
1201     else if (strncmp ("2.", attr_version, strlen ("2.")) == 0)
1202       parsed_version = 2;
1203     else
1204     {
1205       /* TODO: Use the complaint mechanism here. */
1206       NOTICE ("bind plugin: Found <statistics> tag with version `%s'. "
1207           "Unfortunately I have no clue how to parse that. "
1208           "Please open a bug report for this.", attr_version);
1209       xmlFree (attr_version);
1210       continue;
1211     }
1212
1213     ret = bind_xml_stats (parsed_version,
1214         doc, xpathCtx, node);
1215
1216     xmlFree (attr_version);
1217     /* One <statistics> node ought to be enough. */
1218     break;
1219   }
1220
1221   xmlXPathFreeObject (xpathObj);
1222   xmlXPathFreeContext (xpathCtx);
1223   xmlFreeDoc (doc);
1224
1225   return (ret);
1226 } /* }}} int bind_xml */
1227
1228 static int bind_config_set_bool (const char *name, int *var, /* {{{ */
1229     oconfig_item_t *ci)
1230 {
1231   if ((ci->values_num != 1) || ( ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1232   {
1233     WARNING ("bind plugin: The `%s' option needs "
1234         "exactly one boolean argument.", name);
1235     return (-1);
1236   }
1237
1238   if (ci->values[0].value.boolean)
1239     *var = 1;
1240   else
1241     *var = 0;
1242   return 0;
1243 } /* }}} int bind_config_set_bool */
1244
1245 static int bind_config_add_view_zone (cb_view_t *view, /* {{{ */
1246     oconfig_item_t *ci)
1247 {
1248   char **tmp;
1249
1250   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1251   {
1252     WARNING ("bind plugin: The `Zone' option needs "
1253         "exactly one string argument.");
1254     return (-1);
1255   }
1256
1257   tmp = (char **) realloc (view->zones,
1258       sizeof (char *) * (view->zones_num + 1));
1259   if (tmp == NULL)
1260   {
1261     ERROR ("bind plugin: realloc failed.");
1262     return (-1);
1263   }
1264   view->zones = tmp;
1265
1266   view->zones[view->zones_num] = strdup (ci->values[0].value.string);
1267   if (view->zones[view->zones_num] == NULL)
1268   {
1269     ERROR ("bind plugin: strdup failed.");
1270     return (-1);
1271   }
1272   view->zones_num++;
1273
1274   return (0);
1275 } /* }}} int bind_config_add_view_zone */
1276
1277 static int bind_config_add_view (oconfig_item_t *ci) /* {{{ */
1278 {
1279   cb_view_t *tmp;
1280   int i;
1281
1282   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1283   {
1284     WARNING ("bind plugin: `View' blocks need exactly one string argument.");
1285     return (-1);
1286   }
1287
1288   tmp = (cb_view_t *) realloc (views, sizeof (*views) * (views_num + 1));
1289   if (tmp == NULL)
1290   {
1291     ERROR ("bind plugin: realloc failed.");
1292     return (-1);
1293   }
1294   views = tmp;
1295   tmp = views + views_num;
1296
1297   memset (tmp, 0, sizeof (*tmp));
1298   tmp->qtypes = 1;
1299   tmp->resolver_stats = 1;
1300   tmp->cacherrsets = 1;
1301   tmp->zones = NULL;
1302   tmp->zones_num = 0;
1303
1304   tmp->name = strdup (ci->values[0].value.string);
1305   if (tmp->name == NULL)
1306   {
1307     ERROR ("bind plugin: strdup failed.");
1308     free (tmp);
1309     return (-1);
1310   }
1311
1312   for (i = 0; i < ci->children_num; i++)
1313   {
1314     oconfig_item_t *child = ci->children + i;
1315
1316     if (strcasecmp ("QTypes", child->key) == 0)
1317       bind_config_set_bool ("QTypes", &tmp->qtypes, child);
1318     else if (strcasecmp ("ResolverStats", child->key) == 0)
1319       bind_config_set_bool ("ResolverStats", &tmp->resolver_stats, child);
1320     else if (strcasecmp ("CacheRRSets", child->key) == 0)
1321       bind_config_set_bool ("CacheRRSets", &tmp->cacherrsets, child);
1322     else if (strcasecmp ("Zone", child->key) == 0)
1323       bind_config_add_view_zone (tmp, child);
1324     else
1325     {
1326       WARNING ("bind plugin: Unknown configuration option "
1327           "`%s' in view `%s' will be ignored.", child->key, tmp->name);
1328     }
1329   } /* for (i = 0; i < ci->children_num; i++) */
1330
1331   views_num++;
1332   return (0);
1333 } /* }}} int bind_config_add_view */
1334
1335 static int bind_config (oconfig_item_t *ci) /* {{{ */
1336 {
1337   int i;
1338
1339   for (i = 0; i < ci->children_num; i++)
1340   {
1341     oconfig_item_t *child = ci->children + i;
1342
1343     if (strcasecmp ("Url", child->key) == 0) {
1344       if ((child->values_num != 1) || (child->values[0].type != OCONFIG_TYPE_STRING))
1345       {
1346         WARNING ("bind plugin: The `Url' option needs "
1347                  "exactly one string argument.");
1348         return (-1);
1349       }
1350
1351       url = strdup (child->values[0].value.string);
1352     } else if (strcasecmp ("OpCodes", child->key) == 0)
1353       bind_config_set_bool ("OpCodes", &global_opcodes, child);
1354     else if (strcasecmp ("QTypes", child->key) == 0)
1355       bind_config_set_bool ("QTypes", &global_qtypes, child);
1356     else if (strcasecmp ("ServerStats", child->key) == 0)
1357       bind_config_set_bool ("ServerStats", &global_server_stats, child);
1358     else if (strcasecmp ("ZoneMaintStats", child->key) == 0)
1359       bind_config_set_bool ("ZoneMaintStats", &global_zone_maint_stats, child);
1360     else if (strcasecmp ("ResolverStats", child->key) == 0)
1361       bind_config_set_bool ("ResolverStats", &global_resolver_stats, child);
1362     else if (strcasecmp ("MemoryStats", child->key) == 0)
1363       bind_config_set_bool ("MemoryStats", &global_memory_stats, child);
1364     else if (strcasecmp ("View", child->key) == 0)
1365       bind_config_add_view (child);
1366     else
1367     {
1368       WARNING ("bind plugin: Unknown configuration option "
1369           "`%s' will be ignored.", child->key);
1370     }
1371   }
1372
1373   return (0);
1374 } /* }}} int bind_config */
1375
1376 static int bind_init (void) /* {{{ */
1377 {
1378   if (curl != NULL)
1379     return (0);
1380
1381   curl = curl_easy_init ();
1382   if (curl == NULL)
1383   {
1384     ERROR ("bind plugin: bind_init: curl_easy_init failed.");
1385     return (-1);
1386   }
1387
1388   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, bind_curl_callback);
1389   curl_easy_setopt (curl, CURLOPT_USERAGENT, PACKAGE_NAME"/"PACKAGE_VERSION);
1390   curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, bind_curl_error);
1391   curl_easy_setopt (curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL);
1392   curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
1393
1394   return (0);
1395 } /* }}} int bind_init */
1396
1397 static int bind_read (void) /* {{{ */
1398 {
1399   int status;
1400
1401   if (curl == NULL)
1402   {
1403     ERROR ("bind plugin: I don't have a CURL object.");
1404     return (-1);
1405   }
1406
1407   bind_buffer_fill = 0;
1408   if (curl_easy_perform (curl) != 0)
1409   {
1410     ERROR ("bind plugin: curl_easy_perform failed: %s",
1411         bind_curl_error);
1412     return (-1);
1413   }
1414
1415   status = bind_xml (bind_buffer);
1416   if (status != 0)
1417     return (-1);
1418   else
1419     return (0);
1420 } /* }}} int bind_read */
1421
1422 static int bind_shutdown (void) /* {{{ */
1423 {
1424   if (curl != NULL)
1425   {
1426     curl_easy_cleanup (curl);
1427     curl = NULL;
1428   }
1429
1430   return (0);
1431 } /* }}} int bind_shutdown */
1432
1433 void module_register (void)
1434 {
1435   plugin_register_complex_config ("bind", bind_config);
1436   plugin_register_init ("bind", bind_init);
1437   plugin_register_read ("bind", bind_read);
1438   plugin_register_shutdown ("bind", bind_shutdown);
1439 } /* void module_register */
1440
1441 /* vim: set sw=2 sts=2 ts=8 et fdm=marker : */