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