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