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