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