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