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