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