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