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