Merge branch 'collectd-4.4' of octo@verplant.org:/var/lib/git/collectd into collectd-4.4
[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   /* All the other stuff.. */
193   {"all-outqueries",      "dns_question", "outgoing"},
194   {"answers0-1",          "dns_answer",   "0_1"},
195   {"answers1-10",         "dns_answer",   "1_10"},
196   {"answers10-100",       "dns_answer",   "10_100"},
197   {"answers100-1000",     "dns_answer",   "100_1000"},
198   {"answers-slow",        "dns_answer",   "slow"},
199   {"chain-resends",       "dns_question", "chained"},
200   {"client-parse-errors", "counter",      "drops-client_parse_error"},
201   {"concurrent-queries",  "dns_question", "concurrent"},
202   {"dlg-only-drops",      "counter",      "drops-delegation_only"},
203   {"negcache-entries",    "cache_size",   "negative"},
204   {"nsspeeds-entries",    "gauge",        "entries-ns_speeds"},
205   {"nsset-invalidations", "counter",      "ns_set_invalidation"},
206   {"outgoing-timeouts",   "counter",      "drops-timeout_outgoing"},
207   {"resource-limits",     "counter",      "drops-resource_limit"},
208   {"server-parse-errors", "counter",      "drops-server_parse_error"},
209   {"spoof-prevents",      "counter",      "drops-spoofed"},
210   {"tcp-client-overflow", "counter",      "denied-client_overflow_tcp"},
211   {"tcp-outqueries",      "dns_question", "outgoing-tcp"},
212   {"tcp-questions",       "dns_question", "incoming-tcp"},
213   {"throttled-out",       "dns_question", "outgoing-throttled"},
214   {"throttle-entries",    "gauge",        "entries-throttle"},
215   {"unauthorized-tcp",    "counter",      "denied-unauthorized_tcp"},
216   {"unauthorized-udp",    "counter",      "denied-unauthorized_udp"},
217   {"unexpected-packets",  "dns_answer",   "unexpected"}
218   /* {"uptime", "", ""} */
219 }; /* }}} */
220 int lookup_table_length = STATIC_ARRAY_SIZE (lookup_table);
221
222 static llist_t *list = NULL;
223
224 #define PDNS_LOCAL_SOCKPATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-powerdns"
225 static char *local_sockpath = NULL;
226
227 /* TODO: Do this before 4.4:
228  * - Recursor:
229  *   - Complete list of known pdns -> collectd mappings.
230  * - Update the collectd.conf(5) manpage.
231  *
232  * -octo
233  */
234
235 /* <http://doc.powerdns.com/recursor-stats.html> */
236 static void submit (const char *plugin_instance, /* {{{ */
237     const char *pdns_type, const char *value)
238 {
239   value_list_t vl = VALUE_LIST_INIT;
240   value_t values[1];
241
242   const char *type = NULL;
243   const char *type_instance = NULL;
244   const data_set_t *ds;
245
246   int i;
247
248   for (i = 0; i < lookup_table_length; i++)
249     if (strcmp (lookup_table[i].name, pdns_type) == 0)
250       break;
251
252   if (lookup_table[i].type == NULL)
253     return;
254
255   if (i >= lookup_table_length)
256   {
257     DEBUG ("powerdns plugin: submit: Not found in lookup table: %s = %s;",
258         pdns_type, value);
259     return;
260   }
261
262   type = lookup_table[i].type;
263   type_instance = lookup_table[i].type_instance;
264
265   ds = plugin_get_ds (type);
266   if (ds == NULL)
267   {
268     ERROR ("powerdns plugin: The lookup table returned type `%s', "
269         "but I cannot find it via `plugin_get_ds'.",
270         type);
271     return;
272   }
273
274   if (ds->ds_num != 1)
275   {
276     ERROR ("powerdns plugin: type `%s' has %i data sources, "
277         "but I can only handle one.",
278         type, ds->ds_num);
279     return;
280   }
281
282   if (ds->ds[0].type == DS_TYPE_GAUGE)
283   {
284     char *endptr = NULL;
285
286     values[0].gauge = strtod (value, &endptr);
287
288     if (endptr == value)
289     {
290       ERROR ("powerdns plugin: Cannot convert `%s' "
291           "to a floating point number.", value);
292       return;
293     }
294   }
295   else
296   {
297     char *endptr = NULL;
298
299     values[0].counter = strtoll (value, &endptr, 0);
300     if (endptr == value)
301     {
302       ERROR ("powerdns plugin: Cannot convert `%s' "
303           "to an integer number.", value);
304       return;
305     }
306   }
307
308   vl.values = values;
309   vl.values_len = 1;
310   vl.time = time (NULL);
311   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
312   sstrncpy (vl.plugin, "powerdns", sizeof (vl.plugin));
313   if (type_instance != NULL)
314     sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
315   sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
316
317   plugin_dispatch_values (type, &vl);
318 } /* }}} static void submit */
319
320 static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */
321     char **ret_buffer,
322     size_t *ret_buffer_size)
323 {
324   int sd;
325   int status;
326
327   char temp[4096];
328   char *buffer = NULL;
329   size_t buffer_size = 0;
330
331   struct sockaddr_un sa_unix;
332
333   sd = socket (PF_UNIX, item->socktype, 0);
334   if (sd < 0)
335   {
336     FUNC_ERROR ("socket");
337     return (-1);
338   }
339
340   memset (&sa_unix, 0, sizeof (sa_unix));
341   sa_unix.sun_family = AF_UNIX;
342   strncpy (sa_unix.sun_path,
343       (local_sockpath != NULL) ? local_sockpath : PDNS_LOCAL_SOCKPATH,
344       sizeof (sa_unix.sun_path));
345   sa_unix.sun_path[sizeof (sa_unix.sun_path) - 1] = 0;
346
347   status = unlink (sa_unix.sun_path);
348   if ((status != 0) && (errno != ENOENT))
349   {
350     FUNC_ERROR ("unlink");
351     close (sd);
352     return (-1);
353   }
354
355   do /* while (0) */
356   {
357     /* We need to bind to a specific path, because this is a datagram socket
358      * and otherwise the daemon cannot answer. */
359     status = bind (sd, (struct sockaddr *) &sa_unix, sizeof (sa_unix));
360     if (status != 0)
361     {
362       FUNC_ERROR ("bind");
363       break;
364     }
365
366     /* Make the socket writeable by the daemon.. */
367     status = chmod (sa_unix.sun_path, 0666);
368     if (status != 0)
369     {
370       FUNC_ERROR ("chmod");
371       break;
372     }
373
374     status = connect (sd, (struct sockaddr *) &item->sockaddr,
375         sizeof (item->sockaddr));
376     if (status != 0)
377     {
378       FUNC_ERROR ("connect");
379       break;
380     }
381
382     status = send (sd, item->command, strlen (item->command), 0);
383     if (status < 0)
384     {
385       FUNC_ERROR ("send");
386       break;
387     }
388
389     status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
390     if (status < 0)
391     {
392       FUNC_ERROR ("recv");
393       break;
394     }
395     status = 0;
396   } while (0);
397
398   close (sd);
399   unlink (sa_unix.sun_path);
400
401   if (status != 0)
402     return (-1);
403
404   buffer_size = status + 1;
405   buffer = (char *) malloc (buffer_size);
406   if (buffer == NULL)
407   {
408     FUNC_ERROR ("malloc");
409     return (-1);
410   }
411
412   memcpy (buffer, temp, status);
413   buffer[status] = 0;
414
415   *ret_buffer = buffer;
416   *ret_buffer_size = buffer_size;
417
418   return (0);
419 } /* }}} int powerdns_get_data_dgram */
420
421 static int powerdns_get_data_stream (list_item_t *item, /* {{{ */
422     char **ret_buffer,
423     size_t *ret_buffer_size)
424 {
425   int sd;
426   int status;
427
428   char temp[4096];
429   char *buffer = NULL;
430   size_t buffer_size = 0;
431
432   sd = socket (PF_UNIX, item->socktype, 0);
433   if (sd < 0)
434   {
435     FUNC_ERROR ("socket");
436     return (-1);
437   }
438
439   status = connect (sd, (struct sockaddr *) &item->sockaddr,
440       sizeof (item->sockaddr));
441   if (status != 0)
442   {
443     FUNC_ERROR ("connect");
444     close (sd);
445     return (-1);
446   }
447
448   /* strlen + 1, because we need to send the terminating NULL byte, too. */
449   status = send (sd, item->command, strlen (item->command) + 1,
450       /* flags = */ 0);
451   if (status < 0)
452   {
453     FUNC_ERROR ("send");
454     close (sd);
455     return (-1);
456   }
457
458   while (42)
459   {
460     char *buffer_new;
461
462     status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
463     if (status < 0)
464     {
465       FUNC_ERROR ("recv");
466       break;
467     }
468     else if (status == 0)
469       break;
470
471     buffer_new = (char *) realloc (buffer, buffer_size + status + 1);
472     if (buffer_new == NULL)
473     {
474       FUNC_ERROR ("realloc");
475       status = -1;
476       break;
477     }
478     buffer = buffer_new;
479
480     memcpy (buffer + buffer_size, temp, status);
481     buffer_size += status;
482     buffer[buffer_size] = 0;
483   } /* while (42) */
484   close (sd);
485   sd = -1;
486
487   if (status < 0)
488   {
489     sfree (buffer);
490   }
491   else
492   {
493     assert (status == 0);
494     *ret_buffer = buffer;
495     *ret_buffer_size = buffer_size;
496   }
497
498   return (status);
499 } /* }}} int powerdns_get_data_stream */
500
501 static int powerdns_get_data (list_item_t *item, char **ret_buffer,
502     size_t *ret_buffer_size)
503 {
504   if (item->socktype == SOCK_DGRAM)
505     return (powerdns_get_data_dgram (item, ret_buffer, ret_buffer_size));
506   else if (item->socktype == SOCK_STREAM)
507     return (powerdns_get_data_stream (item, ret_buffer, ret_buffer_size));
508   else
509   {
510     ERROR ("powerdns plugin: Unknown socket type: %i", (int) item->socktype);
511     return (-1);
512   }
513 } /* int powerdns_get_data */
514
515 static int powerdns_read_server (list_item_t *item) /* {{{ */
516 {
517   char *buffer = NULL;
518   size_t buffer_size = 0;
519   int status;
520
521   char *dummy;
522   char *saveptr;
523
524   char *key;
525   char *value;
526
527   if (item->command == NULL)
528     item->command = strdup ("SHOW *");
529   if (item->command == NULL)
530   {
531     ERROR ("powerdns plugin: strdup failed.");
532     return (-1);
533   }
534
535   status = powerdns_get_data (item, &buffer, &buffer_size);
536   if (status != 0)
537     return (-1);
538
539   /* 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, */
540   dummy = buffer;
541   saveptr = NULL;
542   while ((key = strtok_r (dummy, ",", &saveptr)) != NULL)
543   {
544     int i;
545
546     dummy = NULL;
547
548     value = strchr (key, '=');
549     if (value == NULL)
550       break;
551
552     *value = '\0';
553     value++;
554
555     if (value[0] == '\0')
556       continue;
557
558     /* Check if this item was requested. */
559     for (i = 0; i < item->fields_num; i++)
560       if (strcasecmp (key, item->fields[i]) == 0)
561         break;
562     if (i >= item->fields_num)
563       continue;
564
565     submit (item->instance, key, value);
566   } /* while (strtok_r) */
567
568   sfree (buffer);
569
570   return (0);
571 } /* }}} int powerdns_read_server */
572
573 /*
574  * powerdns_update_recursor_command
575  *
576  * Creates a string that holds the command to be sent to the recursor. This
577  * string is stores in the `command' member of the `list_item_t' passed to the
578  * function. This function is called by `powerdns_read_recursor'.
579  */
580 static int powerdns_update_recursor_command (list_item_t *li) /* {{{ */
581 {
582   char buffer[4096];
583   int status;
584
585   if (li != NULL)
586     return (0);
587
588   strcpy (buffer, "get ");
589   status = strjoin (&buffer[4], sizeof (buffer) - strlen ("get "),
590       li->fields, li->fields_num,
591       /* seperator = */ " ");
592   if (status < 0)
593   {
594     ERROR ("powerdns plugin: strjoin failed.");
595     return (-1);
596   }
597
598   li->command = strdup (buffer);
599   if (li->command == NULL)
600   {
601     ERROR ("powerdns plugin: strdup failed.");
602     return (-1);
603   }
604
605   return (0);
606 } /* }}} int powerdns_update_recursor_command */
607
608 static int powerdns_read_recursor (list_item_t *item) /* {{{ */
609 {
610   char *buffer = NULL;
611   size_t buffer_size = 0;
612   int status;
613
614   char *dummy;
615
616   char *keys_list;
617   char *key;
618   char *key_saveptr;
619   char *value;
620   char *value_saveptr;
621
622   if (item->command == NULL)
623   {
624     status = powerdns_update_recursor_command (item);
625     if (status != 0)
626     {
627       ERROR ("powerdns plugin: powerdns_update_recursor_command failed.");
628       return (-1);
629     }
630   }
631   assert (item->command != NULL);
632
633   status = powerdns_get_data (item, &buffer, &buffer_size);
634   if (status != 0)
635     return (-1);
636
637   keys_list = strdup (item->command);
638   if (keys_list == NULL)
639   {
640     FUNC_ERROR ("strdup");
641     sfree (buffer);
642     return (-1);
643   }
644
645   key_saveptr = NULL;
646   value_saveptr = NULL;
647
648   /* Skip the `get' at the beginning */
649   strtok_r (keys_list, " \t", &key_saveptr);
650
651   dummy = buffer;
652   while ((value = strtok_r (dummy, " \t\n\r", &value_saveptr)) != NULL)
653   {
654     dummy = NULL;
655
656     key = strtok_r (NULL, " \t", &key_saveptr);
657     if (key == NULL)
658       break;
659
660     submit (item->instance, key, value);
661   } /* while (strtok_r) */
662
663   sfree (buffer);
664   sfree (keys_list);
665
666   return (0);
667 } /* }}} int powerdns_read_recursor */
668
669 static int powerdns_config_add_string (const char *name, /* {{{ */
670     char **dest,
671     oconfig_item_t *ci)
672 {
673   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
674   {
675     WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
676         name);
677     return (-1);
678   }
679
680   sfree (*dest);
681   *dest = strdup (ci->values[0].value.string);
682   if (*dest == NULL)
683     return (-1);
684
685   return (0);
686 } /* }}} int powerdns_config_add_string */
687
688 static int powerdns_config_add_collect (list_item_t *li, /* {{{ */
689     oconfig_item_t *ci)
690 {
691   int i;
692   char **temp;
693
694   if (ci->values_num < 1)
695   {
696     WARNING ("powerdns plugin: The `Collect' option needs "
697         "at least one argument.");
698     return (-1);
699   }
700
701   for (i = 0; i < ci->values_num; i++)
702     if (ci->values[i].type != OCONFIG_TYPE_STRING)
703     {
704       WARNING ("powerdns plugin: Only string arguments are allowed to "
705           "the `Collect' option.");
706       return (-1);
707     }
708
709   temp = (char **) realloc (li->fields,
710       sizeof (char *) * (li->fields_num + ci->values_num));
711   if (temp == NULL)
712   {
713     WARNING ("powerdns plugin: realloc failed.");
714     return (-1);
715   }
716   li->fields = temp;
717
718   for (i = 0; i < ci->values_num; i++)
719   {
720     li->fields[li->fields_num] = strdup (ci->values[i].value.string);
721     if (li->fields[li->fields_num] == NULL)
722     {
723       WARNING ("powerdns plugin: strdup failed.");
724       continue;
725     }
726     li->fields_num++;
727   }
728
729   /* Invalidate a previously computed command */
730   sfree (li->command);
731
732   return (0);
733 } /* }}} int powerdns_config_add_collect */
734
735 static int powerdns_config_add_server (oconfig_item_t *ci) /* {{{ */
736 {
737   char *socket_temp;
738
739   list_item_t *item;
740   int status;
741   int i;
742
743   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
744   {
745     WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
746         ci->key);
747     return (-1);
748   }
749
750   item = (list_item_t *) malloc (sizeof (list_item_t));
751   if (item == NULL)
752   {
753     ERROR ("powerdns plugin: malloc failed.");
754     return (-1);
755   }
756   memset (item, '\0', sizeof (list_item_t));
757
758   item->instance = strdup (ci->values[0].value.string);
759   if (item->instance == NULL)
760   {
761     ERROR ("powerdns plugin: strdup failed.");
762     sfree (item);
763     return (-1);
764   }
765
766   /*
767    * Set default values for the members of list_item_t
768    */
769   if (strcasecmp ("Server", ci->key) == 0)
770   {
771     item->server_type = SRV_AUTHORATIVE;
772     item->func = powerdns_read_server;
773     item->socktype = SOCK_STREAM;
774     socket_temp = strdup (SERVER_SOCKET);
775   }
776   else if (strcasecmp ("Recursor", ci->key) == 0)
777   {
778     item->server_type = SRV_RECURSOR;
779     item->func = powerdns_read_recursor;
780     item->socktype = SOCK_DGRAM;
781     socket_temp = strdup (RECURSOR_SOCKET);
782   }
783   else
784   {
785     /* We must never get here.. */
786     assert (0);
787     return (-1);
788   }
789
790   status = 0;
791   for (i = 0; i < ci->children_num; i++)
792   {
793     oconfig_item_t *option = ci->children + i;
794
795     if (strcasecmp ("Collect", option->key) == 0)
796       status = powerdns_config_add_collect (item, option);
797     else if (strcasecmp ("Socket", option->key) == 0)
798       status = powerdns_config_add_string ("Socket", &socket_temp, option);
799     else
800     {
801       ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
802       status = -1;
803     }
804
805     if (status != 0)
806       break;
807   }
808
809   while (status == 0)
810   {
811     llentry_t *e;
812
813     if (socket_temp == NULL)
814     {
815       ERROR ("powerdns plugin: socket_temp == NULL.");
816       status = -1;
817       break;
818     }
819
820     if (item->command == NULL)
821     {
822       ERROR ("powerdns plugin: item->command == NULL.");
823       status = -1;
824       break;
825     }
826
827     item->sockaddr.sun_family = AF_UNIX;
828     sstrncpy (item->sockaddr.sun_path, socket_temp, UNIX_PATH_MAX);
829
830     e = llentry_create (item->instance, item);
831     if (e == NULL)
832     {
833       ERROR ("powerdns plugin: llentry_create failed.");
834       status = -1;
835       break;
836     }
837     llist_append (list, e);
838
839     break;
840   }
841
842   if (status != 0)
843   {
844     sfree (item);
845     return (-1);
846   }
847
848   DEBUG ("powerdns plugin: Add server: instance = %s;", item->instance);
849
850   return (0);
851 } /* }}} int powerdns_config_add_server */
852
853 static int powerdns_config (oconfig_item_t *ci) /* {{{ */
854 {
855   int i;
856
857   DEBUG ("powerdns plugin: powerdns_config (ci = %p);", (void *) ci);
858
859   if (list == NULL)
860   {
861     list = llist_create ();
862
863     if (list == NULL)
864     {
865       ERROR ("powerdns plugin: `llist_create' failed.");
866       return (-1);
867     }
868   }
869
870   for (i = 0; i < ci->children_num; i++)
871   {
872     oconfig_item_t *option = ci->children + i;
873
874     if ((strcasecmp ("Server", option->key) == 0)
875         || (strcasecmp ("Recursor", option->key) == 0))
876       powerdns_config_add_server (option);
877     if (strcasecmp ("LocalSocket", option->key) == 0)
878     {
879       char *temp = strdup (option->key);
880       if (temp == NULL)
881         return (1);
882       sfree (local_sockpath);
883       local_sockpath = temp;
884     }
885     else
886     {
887       ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
888     }
889   } /* for (i = 0; i < ci->children_num; i++) */
890
891   return (0);
892 } /* }}} int powerdns_config */
893
894 static int powerdns_read (void)
895 {
896   llentry_t *e;
897
898   for (e = llist_head (list); e != NULL; e = e->next)
899   {
900     list_item_t *item = e->value;
901     item->func (item);
902   }
903
904   return (0);
905 } /* static int powerdns_read */
906
907 static int powerdns_shutdown (void)
908 {
909   llentry_t *e;
910
911   if (list == NULL)
912     return (0);
913
914   for (e = llist_head (list); e != NULL; e = e->next)
915   {
916     list_item_t *item = (list_item_t *) e->value;
917     e->value = NULL;
918
919     sfree (item->instance);
920     sfree (item->command);
921     sfree (item);
922   }
923
924   llist_destroy (list);
925   list = NULL;
926
927   return (0);
928 } /* static int powerdns_shutdown */
929
930 void module_register (void)
931 {
932   plugin_register_complex_config ("powerdns", powerdns_config);
933   plugin_register_read ("powerdns", powerdns_read);
934   plugin_register_shutdown ("powerdns", powerdns_shutdown );
935 } /* void module_register */
936
937 /* vim: set sw=2 sts=2 ts=8 fdm=marker : */