d91ce5baada860dc1820c804425fc3c959da0519
[collectd.git] / src / powerdns.c
1 /**
2  * collectd - src/powerdns.c
3  * Copyright (C) 2007-2008  C-Ware, Inc.
4  * Copyright (C) 2008       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  * Author:
20  *   Luke Heberling <lukeh at c-ware.com>
21  *   Florian Forster <octo at verplant.org>
22  *
23  * DESCRIPTION
24  *   Queries a PowerDNS control socket for statistics
25  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "utils_llist.h"
32
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/un.h>
42
43 #ifndef UNIX_PATH_MAX
44 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
45 #endif
46 #define FUNC_ERROR(func) do { char errbuf[1024]; ERROR ("powerdns plugin: %s failed: %s", func, sstrerror (errno, errbuf, sizeof (errbuf))); } while (0)
47
48 #define SERVER_SOCKET  "/var/run/pdns.controlsocket"
49 #define SERVER_COMMAND "SHOW *"
50
51 #define RECURSOR_SOCKET  "/var/run/pdns_recursor.controlsocket"
52 #define RECURSOR_COMMAND "get all-outqueries answers0-1 " /* {{{ */ \
53   "answers100-1000 answers10-100 answers1-10 answers-slow cache-entries " \
54   "cache-hits cache-misses chain-resends client-parse-errors " \
55   "concurrent-queries dlg-only-drops ipv6-outqueries negcache-entries " \
56   "noerror-answers nsset-invalidations nsspeeds-entries nxdomain-answers " \
57   "outgoing-timeouts qa-latency questions resource-limits " \
58   "server-parse-errors servfail-answers spoof-prevents sys-msec " \
59   "tcp-client-overflow tcp-outqueries tcp-questions throttled-out " \
60   "throttled-outqueries throttle-entries unauthorized-tcp unauthorized-udp " \
61   "unexpected-packets unreachables user-msec" /* }}} */
62
63 struct list_item_s;
64 typedef struct list_item_s list_item_t;
65
66 struct list_item_s
67 {
68   enum
69   {
70     SRV_AUTHORATIVE,
71     SRV_RECURSOR
72   } server_type;
73   int (*func) (list_item_t *item);
74   char *instance;
75
76   char **fields;
77   int fields_num;
78   char *command;
79
80   struct sockaddr_un sockaddr;
81   int socktype;
82 };
83
84 struct statname_lookup_s
85 {
86   char *name;
87   char *type;
88   char *type_instance;
89 };
90 typedef struct statname_lookup_s statname_lookup_t;
91
92 /* Description of statistics returned by the recursor: {{{
93 all-outqueries      counts the number of outgoing UDP queries since starting
94 answers0-1          counts the number of queries answered within 1 milisecond
95 answers100-1000     counts the number of queries answered within 1 second
96 answers10-100       counts the number of queries answered within 100 miliseconds
97 answers1-10         counts the number of queries answered within 10 miliseconds
98 answers-slow        counts the number of queries answered after 1 second
99 cache-entries       shows the number of entries in the cache
100 cache-hits          counts the number of cache hits since starting
101 cache-misses        counts the number of cache misses since starting
102 chain-resends       number of queries chained to existing outstanding query
103 client-parse-errors counts number of client packets that could not be parsed
104 concurrent-queries  shows the number of MThreads currently running
105 dlg-only-drops      number of records dropped because of delegation only setting
106 negcache-entries    shows the number of entries in the Negative answer cache
107 noerror-answers     counts the number of times it answered NOERROR since starting
108 nsspeeds-entries    shows the number of entries in the NS speeds map
109 nsset-invalidations number of times an nsset was dropped because it no longer worked
110 nxdomain-answers    counts the number of times it answered NXDOMAIN since starting
111 outgoing-timeouts   counts the number of timeouts on outgoing UDP queries since starting
112 qa-latency          shows the current latency average
113 questions           counts all End-user initiated queries with the RD bit set
114 resource-limits     counts number of queries that could not be performed because of resource limits
115 server-parse-errors counts number of server replied packets that could not be parsed
116 servfail-answers    counts the number of times it answered SERVFAIL since starting
117 spoof-prevents      number of times PowerDNS considered itself spoofed, and dropped the data
118 sys-msec            number of CPU milliseconds spent in 'system' mode
119 tcp-client-overflow number of times an IP address was denied TCP access because it already had too many connections
120 tcp-outqueries      counts the number of outgoing TCP queries since starting
121 tcp-questions       counts all incoming TCP queries (since starting)
122 throttled-out       counts the number of throttled outgoing UDP queries since starting
123 throttle-entries    shows the number of entries in the throttle map
124 unauthorized-tcp    number of TCP questions denied because of allow-from restrictions
125 unauthorized-udp    number of UDP questions denied because of allow-from restrictions
126 unexpected-packets  number of answers from remote servers that were unexpected (might point to spoofing)
127 uptime              number of seconds process has been running (since 3.1.5)
128 user-msec           number of CPU milliseconds spent in 'user' mode
129 }}} */
130
131 statname_lookup_t lookup_table[] = /* {{{ */
132 {
133   /*********************
134    * Server statistics *
135    *********************/
136   /* Questions */
137   {"recursing-questions",    "dns_question", "recurse"},
138   {"tcp-queries",            "dns_question", "tcp"},
139   {"udp-queries",            "dns_question", "udp"},
140
141   /* Answers */
142   {"recursing-answers",      "dns_answer",   "recurse"},
143   {"tcp-answers",            "dns_answer",   "tcp"},
144   {"udp-answers",            "dns_answer",   "udp"},
145
146   /* Cache stuff */
147   {"packetcache-hit",        "cache_result", "packet-hit"},
148   {"packetcache-miss",       "cache_result", "packet-miss"},
149   {"packetcache-size",       "cache_size",   "packet"},
150   {"query-cache-hit",        "cache_result", "query-hit"},
151   {"query-cache-miss",       "cache_result", "query-miss"},
152
153   /* Latency */
154   {"latency",                "latency",      NULL},
155
156   /* Other stuff.. */
157   {"corrupt-packets",        "io_packets",   "corrupt"},
158   {"deferred-cache-inserts", "counter",      "cache-deferred_insert"},
159   {"deferred-cache-lookup",  "counter",      "cache-deferred_lookup"},
160   {"qsize-a",                "cache_size",   "answers"},
161   {"qsize-q",                "cache_size",   "questions"},
162   {"servfail-packets",       "io_packets",   "servfail"},
163   {"timedout-packets",       "io_packets",   "timeout"},
164   {"udp4-answers",           "dns_answer",   "udp4"},
165   {"udp4-queries",           "dns_question", "queries-udp4"},
166   {"udp6-answers",           "dns_answer",   "udp6"},
167   {"udp6-queries",           "dns_question", "queries-udp6"},
168
169   /***********************
170    * Recursor statistics *
171    ***********************/
172   /* Answers by return code */
173   {"noerror-answers",     "dns_rcode",    "NOERROR"},
174   {"nxdomain-answers",    "dns_rcode",    "NXDOMAIN"},
175   {"servfail-answers",    "dns_rcode",    "SERVFAIL"},
176
177   /* CPU utilization */
178   {"sys-msec",            "cpu",          "system"},
179   {"user-msec",           "cpu",          "user"},
180
181   /* Question-to-answer latency */
182   {"qa-latency",          "latency",      NULL},
183
184   /* Cache */
185   {"cache-entries",       "cache_size",   NULL},
186   {"cache-hits",          "cache_result", "hit"},
187   {"cache-misses",        "cache_result", "miss"},
188
189   /* Total number of questions.. */
190   {"questions",           "dns_qtype",    "total"}
191
192   /* TODO: Add all recursor metrics here */
193 }; /* }}} */
194 int lookup_table_length = STATIC_ARRAY_SIZE (lookup_table);
195
196 static llist_t *list = NULL;
197
198 #define PDNS_LOCAL_SOCKPATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-powerdns"
199 static char *local_sockpath = NULL;
200
201 /* TODO: Do this before 4.4:
202  * - Recursor:
203  *   - Complete list of known pdns -> collectd mappings.
204  * - Update the collectd.conf(5) manpage.
205  *
206  * -octo
207  */
208
209 /* <http://doc.powerdns.com/recursor-stats.html> */
210 static void submit (const char *plugin_instance, /* {{{ */
211     const char *pdns_type, const char *value)
212 {
213   value_list_t vl = VALUE_LIST_INIT;
214   value_t values[1];
215
216   const char *type = NULL;
217   const char *type_instance = NULL;
218   const data_set_t *ds;
219
220   int i;
221
222   for (i = 0; i < lookup_table_length; i++)
223     if (strcmp (lookup_table[i].name, pdns_type) == 0)
224       break;
225
226   if (lookup_table[i].type == NULL)
227     return;
228
229   if (i >= lookup_table_length)
230   {
231     DEBUG ("powerdns plugin: submit: Not found in lookup table: %s = %s;",
232         pdns_type, value);
233     return;
234   }
235
236   type = lookup_table[i].type;
237   type_instance = lookup_table[i].type_instance;
238
239   ds = plugin_get_ds (type);
240   if (ds == NULL)
241   {
242     ERROR ("powerdns plugin: The lookup table returned type `%s', "
243         "but I cannot find it via `plugin_get_ds'.",
244         type);
245     return;
246   }
247
248   if (ds->ds_num != 1)
249   {
250     ERROR ("powerdns plugin: type `%s' has %i data sources, "
251         "but I can only handle one.",
252         type, ds->ds_num);
253     return;
254   }
255
256   if (ds->ds[0].type == DS_TYPE_GAUGE)
257   {
258     char *endptr = NULL;
259
260     values[0].gauge = strtod (value, &endptr);
261
262     if (endptr == value)
263     {
264       ERROR ("powerdns plugin: Cannot convert `%s' "
265           "to a floating point number.", value);
266       return;
267     }
268   }
269   else
270   {
271     char *endptr = NULL;
272
273     values[0].counter = strtoll (value, &endptr, 0);
274     if (endptr == value)
275     {
276       ERROR ("powerdns plugin: Cannot convert `%s' "
277           "to an integer number.", value);
278       return;
279     }
280   }
281
282   vl.values = values;
283   vl.values_len = 1;
284   vl.time = time (NULL);
285   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
286   sstrncpy (vl.plugin, "powerdns", sizeof (vl.plugin));
287   if (type_instance != NULL)
288     sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
289   sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
290
291   plugin_dispatch_values (type, &vl);
292 } /* }}} static void submit */
293
294 static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */
295     char **ret_buffer,
296     size_t *ret_buffer_size)
297 {
298   int sd;
299   int status;
300
301   char temp[4096];
302   char *buffer = NULL;
303   size_t buffer_size = 0;
304
305   struct sockaddr_un sa_unix;
306
307   sd = socket (PF_UNIX, item->socktype, 0);
308   if (sd < 0)
309   {
310     FUNC_ERROR ("socket");
311     return (-1);
312   }
313
314   memset (&sa_unix, 0, sizeof (sa_unix));
315   sa_unix.sun_family = AF_UNIX;
316   strncpy (sa_unix.sun_path,
317       (local_sockpath != NULL) ? local_sockpath : PDNS_LOCAL_SOCKPATH,
318       sizeof (sa_unix.sun_path));
319   sa_unix.sun_path[sizeof (sa_unix.sun_path) - 1] = 0;
320
321   status = unlink (sa_unix.sun_path);
322   if ((status != 0) && (errno != ENOENT))
323   {
324     FUNC_ERROR ("unlink");
325     close (sd);
326     return (-1);
327   }
328
329   do /* while (0) */
330   {
331     /* We need to bind to a specific path, because this is a datagram socket
332      * and otherwise the daemon cannot answer. */
333     status = bind (sd, (struct sockaddr *) &sa_unix, sizeof (sa_unix));
334     if (status != 0)
335     {
336       FUNC_ERROR ("bind");
337       break;
338     }
339
340     /* Make the socket writeable by the daemon.. */
341     status = chmod (sa_unix.sun_path, 0666);
342     if (status != 0)
343     {
344       FUNC_ERROR ("chmod");
345       break;
346     }
347
348     status = connect (sd, (struct sockaddr *) &item->sockaddr,
349         sizeof (item->sockaddr));
350     if (status != 0)
351     {
352       FUNC_ERROR ("connect");
353       break;
354     }
355
356     status = send (sd, item->command, strlen (item->command), 0);
357     if (status < 0)
358     {
359       FUNC_ERROR ("send");
360       break;
361     }
362
363     status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
364     if (status < 0)
365     {
366       FUNC_ERROR ("recv");
367       break;
368     }
369     status = 0;
370   } while (0);
371
372   close (sd);
373   unlink (sa_unix.sun_path);
374
375   if (status != 0)
376     return (-1);
377
378   buffer_size = status + 1;
379   buffer = (char *) malloc (buffer_size);
380   if (buffer == NULL)
381   {
382     FUNC_ERROR ("malloc");
383     return (-1);
384   }
385
386   memcpy (buffer, temp, status);
387   buffer[status] = 0;
388
389   *ret_buffer = buffer;
390   *ret_buffer_size = buffer_size;
391
392   return (0);
393 } /* }}} int powerdns_get_data_dgram */
394
395 static int powerdns_get_data_stream (list_item_t *item, /* {{{ */
396     char **ret_buffer,
397     size_t *ret_buffer_size)
398 {
399   int sd;
400   int status;
401
402   char temp[4096];
403   char *buffer = NULL;
404   size_t buffer_size = 0;
405
406   sd = socket (PF_UNIX, item->socktype, 0);
407   if (sd < 0)
408   {
409     FUNC_ERROR ("socket");
410     return (-1);
411   }
412
413   status = connect (sd, (struct sockaddr *) &item->sockaddr,
414       sizeof (item->sockaddr));
415   if (status != 0)
416   {
417     FUNC_ERROR ("connect");
418     close (sd);
419     return (-1);
420   }
421
422   /* strlen + 1, because we need to send the terminating NULL byte, too. */
423   status = send (sd, item->command, strlen (item->command) + 1,
424       /* flags = */ 0);
425   if (status < 0)
426   {
427     FUNC_ERROR ("send");
428     close (sd);
429     return (-1);
430   }
431
432   while (42)
433   {
434     char *buffer_new;
435
436     status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
437     if (status < 0)
438     {
439       FUNC_ERROR ("recv");
440       break;
441     }
442     else if (status == 0)
443       break;
444
445     buffer_new = (char *) realloc (buffer, buffer_size + status + 1);
446     if (buffer_new == NULL)
447     {
448       FUNC_ERROR ("realloc");
449       status = -1;
450       break;
451     }
452     buffer = buffer_new;
453
454     memcpy (buffer + buffer_size, temp, status);
455     buffer_size += status;
456     buffer[buffer_size] = 0;
457   } /* while (42) */
458   close (sd);
459   sd = -1;
460
461   if (status < 0)
462   {
463     sfree (buffer);
464   }
465   else
466   {
467     assert (status == 0);
468     *ret_buffer = buffer;
469     *ret_buffer_size = buffer_size;
470   }
471
472   return (status);
473 } /* }}} int powerdns_get_data_stream */
474
475 static int powerdns_get_data (list_item_t *item, char **ret_buffer,
476     size_t *ret_buffer_size)
477 {
478   if (item->socktype == SOCK_DGRAM)
479     return (powerdns_get_data_dgram (item, ret_buffer, ret_buffer_size));
480   else if (item->socktype == SOCK_STREAM)
481     return (powerdns_get_data_stream (item, ret_buffer, ret_buffer_size));
482   else
483   {
484     ERROR ("powerdns plugin: Unknown socket type: %i", (int) item->socktype);
485     return (-1);
486   }
487 } /* int powerdns_get_data */
488
489 static int powerdns_read_server (list_item_t *item) /* {{{ */
490 {
491   char *buffer = NULL;
492   size_t buffer_size = 0;
493   int status;
494
495   char *dummy;
496   char *saveptr;
497
498   char *key;
499   char *value;
500
501   if (item->command == NULL)
502     item->command = strdup ("SHOW *");
503   if (item->command == NULL)
504   {
505     ERROR ("powerdns plugin: strdup failed.");
506     return (-1);
507   }
508
509   status = powerdns_get_data (item, &buffer, &buffer_size);
510   if (status != 0)
511     return (-1);
512
513   /* corrupt-packets=0,deferred-cache-inserts=0,deferred-cache-lookup=0,latency=0,packetcache-hit=0,packetcache-miss=0,packetcache-size=0,qsize-q=0,query-cache-hit=0,query-cache-miss=0,recursing-answers=0,recursing-questions=0,servfail-packets=0,tcp-answers=0,tcp-queries=0,timedout-packets=0,udp-answers=0,udp-queries=0,udp4-answers=0,udp4-queries=0,udp6-answers=0,udp6-queries=0, */
514   dummy = buffer;
515   saveptr = NULL;
516   while ((key = strtok_r (dummy, ",", &saveptr)) != NULL)
517   {
518     int i;
519
520     dummy = NULL;
521
522     value = strchr (key, '=');
523     if (value == NULL)
524       break;
525
526     *value = '\0';
527     value++;
528
529     if (value[0] == '\0')
530       continue;
531
532     /* Check if this item was requested. */
533     for (i = 0; i < item->fields_num; i++)
534       if (strcasecmp (key, item->fields[i]) == 0)
535         break;
536     if (i >= item->fields_num)
537       continue;
538
539     submit (item->instance, key, value);
540   } /* while (strtok_r) */
541
542   sfree (buffer);
543
544   return (0);
545 } /* }}} int powerdns_read_server */
546
547 /*
548  * powerdns_update_recursor_command
549  *
550  * Creates a string that holds the command to be sent to the recursor. This
551  * string is stores in the `command' member of the `list_item_t' passed to the
552  * function. This function is called by `powerdns_read_recursor'.
553  */
554 static int powerdns_update_recursor_command (list_item_t *li) /* {{{ */
555 {
556   char buffer[4096];
557   int status;
558
559   if (li != NULL)
560     return (0);
561
562   strcpy (buffer, "get ");
563   status = strjoin (&buffer[4], sizeof (buffer) - strlen ("get "),
564       li->fields, li->fields_num,
565       /* seperator = */ " ");
566   if (status < 0)
567   {
568     ERROR ("powerdns plugin: strjoin failed.");
569     return (-1);
570   }
571
572   li->command = strdup (buffer);
573   if (li->command == NULL)
574   {
575     ERROR ("powerdns plugin: strdup failed.");
576     return (-1);
577   }
578
579   return (0);
580 } /* }}} int powerdns_update_recursor_command */
581
582 static int powerdns_read_recursor (list_item_t *item) /* {{{ */
583 {
584   char *buffer = NULL;
585   size_t buffer_size = 0;
586   int status;
587
588   char *dummy;
589
590   char *keys_list;
591   char *key;
592   char *key_saveptr;
593   char *value;
594   char *value_saveptr;
595
596   if (item->command == NULL)
597   {
598     status = powerdns_update_recursor_command (item);
599     if (status != 0)
600     {
601       ERROR ("powerdns plugin: powerdns_update_recursor_command failed.");
602       return (-1);
603     }
604   }
605   assert (item->command != NULL);
606
607   status = powerdns_get_data (item, &buffer, &buffer_size);
608   if (status != 0)
609     return (-1);
610
611   keys_list = strdup (item->command);
612   if (keys_list == NULL)
613   {
614     FUNC_ERROR ("strdup");
615     sfree (buffer);
616     return (-1);
617   }
618
619   key_saveptr = NULL;
620   value_saveptr = NULL;
621
622   /* Skip the `get' at the beginning */
623   strtok_r (keys_list, " \t", &key_saveptr);
624
625   dummy = buffer;
626   while ((value = strtok_r (dummy, " \t\n\r", &value_saveptr)) != NULL)
627   {
628     dummy = NULL;
629
630     key = strtok_r (NULL, " \t", &key_saveptr);
631     if (key == NULL)
632       break;
633
634     submit (item->instance, key, value);
635   } /* while (strtok_r) */
636
637   sfree (buffer);
638   sfree (keys_list);
639
640   return (0);
641 } /* }}} int powerdns_read_recursor */
642
643 static int powerdns_config_add_string (const char *name, /* {{{ */
644     char **dest,
645     oconfig_item_t *ci)
646 {
647   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
648   {
649     WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
650         name);
651     return (-1);
652   }
653
654   sfree (*dest);
655   *dest = strdup (ci->values[0].value.string);
656   if (*dest == NULL)
657     return (-1);
658
659   return (0);
660 } /* }}} int powerdns_config_add_string */
661
662 static int powerdns_config_add_collect (list_item_t *li, /* {{{ */
663     oconfig_item_t *ci)
664 {
665   int i;
666   char **temp;
667
668   if (ci->values_num < 1)
669   {
670     WARNING ("powerdns plugin: The `Collect' option needs "
671         "at least one argument.");
672     return (-1);
673   }
674
675   for (i = 0; i < ci->values_num; i++)
676     if (ci->values[i].type != OCONFIG_TYPE_STRING)
677     {
678       WARNING ("powerdns plugin: Only string arguments are allowed to "
679           "the `Collect' option.");
680       return (-1);
681     }
682
683   temp = (char **) realloc (li->fields,
684       sizeof (char *) * (li->fields_num + ci->values_num));
685   if (temp == NULL)
686   {
687     WARNING ("powerdns plugin: realloc failed.");
688     return (-1);
689   }
690   li->fields = temp;
691
692   for (i = 0; i < ci->values_num; i++)
693   {
694     li->fields[li->fields_num] = strdup (ci->values[i].value.string);
695     if (li->fields[li->fields_num] == NULL)
696     {
697       WARNING ("powerdns plugin: strdup failed.");
698       continue;
699     }
700     li->fields_num++;
701   }
702
703   /* Invalidate a previously computed command */
704   sfree (li->command);
705
706   return (0);
707 } /* }}} int powerdns_config_add_collect */
708
709 static int powerdns_config_add_server (oconfig_item_t *ci) /* {{{ */
710 {
711   char *socket_temp;
712
713   list_item_t *item;
714   int status;
715   int i;
716
717   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
718   {
719     WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
720         ci->key);
721     return (-1);
722   }
723
724   item = (list_item_t *) malloc (sizeof (list_item_t));
725   if (item == NULL)
726   {
727     ERROR ("powerdns plugin: malloc failed.");
728     return (-1);
729   }
730   memset (item, '\0', sizeof (list_item_t));
731
732   item->instance = strdup (ci->values[0].value.string);
733   if (item->instance == NULL)
734   {
735     ERROR ("powerdns plugin: strdup failed.");
736     sfree (item);
737     return (-1);
738   }
739
740   /*
741    * Set default values for the members of list_item_t
742    */
743   if (strcasecmp ("Server", ci->key) == 0)
744   {
745     item->server_type = SRV_AUTHORATIVE;
746     item->func = powerdns_read_server;
747     item->socktype = SOCK_STREAM;
748     socket_temp = strdup (SERVER_SOCKET);
749   }
750   else if (strcasecmp ("Recursor", ci->key) == 0)
751   {
752     item->server_type = SRV_RECURSOR;
753     item->func = powerdns_read_recursor;
754     item->socktype = SOCK_DGRAM;
755     socket_temp = strdup (RECURSOR_SOCKET);
756   }
757   else
758   {
759     /* We must never get here.. */
760     assert (0);
761     return (-1);
762   }
763
764   status = 0;
765   for (i = 0; i < ci->children_num; i++)
766   {
767     oconfig_item_t *option = ci->children + i;
768
769     if (strcasecmp ("Collect", option->key) == 0)
770       status = powerdns_config_add_collect (item, option);
771     else if (strcasecmp ("Socket", option->key) == 0)
772       status = powerdns_config_add_string ("Socket", &socket_temp, option);
773     else
774     {
775       ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
776       status = -1;
777     }
778
779     if (status != 0)
780       break;
781   }
782
783   while (status == 0)
784   {
785     llentry_t *e;
786
787     if (socket_temp == NULL)
788     {
789       ERROR ("powerdns plugin: socket_temp == NULL.");
790       status = -1;
791       break;
792     }
793
794     if (item->command == NULL)
795     {
796       ERROR ("powerdns plugin: item->command == NULL.");
797       status = -1;
798       break;
799     }
800
801     item->sockaddr.sun_family = AF_UNIX;
802     sstrncpy (item->sockaddr.sun_path, socket_temp, UNIX_PATH_MAX);
803
804     e = llentry_create (item->instance, item);
805     if (e == NULL)
806     {
807       ERROR ("powerdns plugin: llentry_create failed.");
808       status = -1;
809       break;
810     }
811     llist_append (list, e);
812
813     break;
814   }
815
816   if (status != 0)
817   {
818     sfree (item);
819     return (-1);
820   }
821
822   DEBUG ("powerdns plugin: Add server: instance = %s;", item->instance);
823
824   return (0);
825 } /* }}} int powerdns_config_add_server */
826
827 static int powerdns_config (oconfig_item_t *ci) /* {{{ */
828 {
829   int i;
830
831   DEBUG ("powerdns plugin: powerdns_config (ci = %p);", (void *) ci);
832
833   if (list == NULL)
834   {
835     list = llist_create ();
836
837     if (list == NULL)
838     {
839       ERROR ("powerdns plugin: `llist_create' failed.");
840       return (-1);
841     }
842   }
843
844   for (i = 0; i < ci->children_num; i++)
845   {
846     oconfig_item_t *option = ci->children + i;
847
848     if ((strcasecmp ("Server", option->key) == 0)
849         || (strcasecmp ("Recursor", option->key) == 0))
850       powerdns_config_add_server (option);
851     if (strcasecmp ("LocalSocket", option->key) == 0)
852     {
853       char *temp = strdup (option->key);
854       if (temp == NULL)
855         return (1);
856       sfree (local_sockpath);
857       local_sockpath = temp;
858     }
859     else
860     {
861       ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
862     }
863   } /* for (i = 0; i < ci->children_num; i++) */
864
865   return (0);
866 } /* }}} int powerdns_config */
867
868 static int powerdns_read (void)
869 {
870   llentry_t *e;
871
872   for (e = llist_head (list); e != NULL; e = e->next)
873   {
874     list_item_t *item = e->value;
875     item->func (item);
876   }
877
878   return (0);
879 } /* static int powerdns_read */
880
881 static int powerdns_shutdown (void)
882 {
883   llentry_t *e;
884
885   if (list == NULL)
886     return (0);
887
888   for (e = llist_head (list); e != NULL; e = e->next)
889   {
890     list_item_t *item = (list_item_t *) e->value;
891     e->value = NULL;
892
893     sfree (item->instance);
894     sfree (item->command);
895     sfree (item);
896   }
897
898   llist_destroy (list);
899   list = NULL;
900
901   return (0);
902 } /* static int powerdns_shutdown */
903
904 void module_register (void)
905 {
906   plugin_register_complex_config ("powerdns", powerdns_config);
907   plugin_register_read ("powerdns", powerdns_read);
908   plugin_register_shutdown ("powerdns", powerdns_shutdown );
909 } /* void module_register */
910
911 /* vim: set sw=2 sts=2 ts=8 fdm=marker : */