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