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