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