2 * collectd - src/powerdns.c
3 * Copyright (C) 2007-2008 C-Ware, Inc.
4 * Copyright (C) 2008 Florian Forster
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.
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.
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
20 * Luke Heberling <lukeh at c-ware.com>
21 * Florian Forster <octo at collectd.org>
24 * Queries a PowerDNS control socket for statistics
30 #include "configfile.h"
31 #include "utils_llist.h"
39 #include <sys/types.h>
43 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
45 #define FUNC_ERROR(func) do { char errbuf[1024]; ERROR ("powerdns plugin: %s failed: %s", func, sstrerror (errno, errbuf, sizeof (errbuf))); } while (0)
47 #define SERVER_SOCKET LOCALSTATEDIR"/run/pdns.controlsocket"
48 #define SERVER_COMMAND "SHOW * \n"
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"
56 typedef struct list_item_s list_item_t;
65 int (*func) (list_item_t *item);
72 struct sockaddr_un sockaddr;
76 struct statname_lookup_s
82 typedef struct statname_lookup_s statname_lookup_t;
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
123 const char* const default_server_fields[] = /* {{{ */
132 "recursing-questions",
138 int default_server_fields_num = STATIC_ARRAY_SIZE (default_server_fields);
140 statname_lookup_t lookup_table[] = /* {{{ */
142 /*********************
143 * Server statistics *
144 *********************/
146 {"recursing-questions", "dns_question", "recurse"},
147 {"tcp-queries", "dns_question", "tcp"},
148 {"udp-queries", "dns_question", "udp"},
149 {"rd-queries", "dns_question", "rd"},
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"},
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"},
169 {"latency", "latency", NULL},
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"},
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"},
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"},
201 /* CPU utilization */
202 {"sys-msec", "cpu", "system"},
203 {"user-msec", "cpu", "user"},
205 /* Question-to-answer latency */
206 {"qa-latency", "latency", NULL},
209 {"cache-entries", "cache_size", NULL},
210 {"cache-hits", "cache_result", "hit"},
211 {"cache-misses", "cache_result", "miss"},
213 /* Total number of questions.. */
214 {"questions", "dns_qtype", "total"},
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}
244 int lookup_table_length = STATIC_ARRAY_SIZE (lookup_table);
246 static llist_t *list = NULL;
248 #define PDNS_LOCAL_SOCKPATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-powerdns"
249 static char *local_sockpath = NULL;
251 /* TODO: Do this before 4.4:
253 * - Complete list of known pdns -> collectd mappings.
254 * - Update the collectd.conf(5) manpage.
259 /* <http://doc.powerdns.com/recursor-stats.html> */
260 static void submit (const char *plugin_instance, /* {{{ */
261 const char *pdns_type, const char *value)
263 value_list_t vl = VALUE_LIST_INIT;
266 const char *type = NULL;
267 const char *type_instance = NULL;
268 const data_set_t *ds;
272 for (i = 0; i < lookup_table_length; i++)
273 if (strcmp (lookup_table[i].name, pdns_type) == 0)
276 if (i >= lookup_table_length)
278 INFO ("powerdns plugin: submit: Not found in lookup table: %s = %s;",
283 if (lookup_table[i].type == NULL)
286 type = lookup_table[i].type;
287 type_instance = lookup_table[i].type_instance;
289 ds = plugin_get_ds (type);
292 ERROR ("powerdns plugin: The lookup table returned type `%s', "
293 "but I cannot find it via `plugin_get_ds'.",
300 ERROR ("powerdns plugin: type `%s' has %zu data sources, "
301 "but I can only handle one.",
306 if (0 != parse_value (value, &values[0], ds->ds[0].type))
308 ERROR ("powerdns plugin: Cannot convert `%s' "
309 "to a number.", value);
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));
322 plugin_dispatch_values (&vl);
323 } /* }}} static void submit */
325 static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */
327 size_t *ret_buffer_size)
334 size_t buffer_size = 0;
336 struct sockaddr_un sa_unix;
338 struct timeval stv_timeout;
339 cdtime_t cdt_timeout;
341 sd = socket (PF_UNIX, item->socktype, 0);
344 FUNC_ERROR ("socket");
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));
354 status = unlink (sa_unix.sun_path);
355 if ((status != 0) && (errno != ENOENT))
357 FUNC_ERROR ("unlink");
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));
373 /* Make the socket writeable by the daemon.. */
374 status = chmod (sa_unix.sun_path, 0666);
377 FUNC_ERROR ("chmod");
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);
385 CDTIME_T_TO_TIMEVAL (cdt_timeout, &stv_timeout);
387 status = setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO, &stv_timeout, sizeof (stv_timeout));
390 FUNC_ERROR ("setsockopt");
394 status = connect (sd, (struct sockaddr *) &item->sockaddr,
395 sizeof (item->sockaddr));
398 FUNC_ERROR ("connect");
402 status = send (sd, item->command, strlen (item->command), 0);
409 status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
415 buffer_size = status + 1;
420 unlink (sa_unix.sun_path);
425 assert (buffer_size > 0);
426 buffer = (char *) malloc (buffer_size);
429 FUNC_ERROR ("malloc");
433 memcpy (buffer, temp, buffer_size - 1);
434 buffer[buffer_size - 1] = 0;
436 *ret_buffer = buffer;
437 *ret_buffer_size = buffer_size;
440 } /* }}} int powerdns_get_data_dgram */
442 static int powerdns_get_data_stream (list_item_t *item, /* {{{ */
444 size_t *ret_buffer_size)
451 size_t buffer_size = 0;
453 sd = socket (PF_UNIX, item->socktype, 0);
456 FUNC_ERROR ("socket");
460 struct timeval timeout;
463 status = setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof (timeout));
466 FUNC_ERROR ("setsockopt");
471 status = connect (sd, (struct sockaddr *) &item->sockaddr,
472 sizeof (item->sockaddr));
475 FUNC_ERROR ("connect");
480 /* strlen + 1, because we need to send the terminating NULL byte, too. */
481 status = send (sd, item->command, strlen (item->command) + 1,
494 status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
500 else if (status == 0)
503 buffer_new = (char *) realloc (buffer, buffer_size + status + 1);
504 if (buffer_new == NULL)
506 FUNC_ERROR ("realloc");
512 memcpy (buffer + buffer_size, temp, status);
513 buffer_size += status;
514 buffer[buffer_size] = 0;
524 assert (status == 0);
525 *ret_buffer = buffer;
526 *ret_buffer_size = buffer_size;
530 } /* }}} int powerdns_get_data_stream */
532 static int powerdns_get_data (list_item_t *item, char **ret_buffer,
533 size_t *ret_buffer_size)
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));
541 ERROR ("powerdns plugin: Unknown socket type: %i", (int) item->socktype);
544 } /* int powerdns_get_data */
546 static int powerdns_read_server (list_item_t *item) /* {{{ */
549 size_t buffer_size = 0;
558 const char* const *fields;
561 if (item->command == NULL)
562 item->command = strdup (SERVER_COMMAND);
563 if (item->command == NULL)
565 ERROR ("powerdns plugin: strdup failed.");
569 status = powerdns_get_data (item, &buffer, &buffer_size);
573 if (item->fields_num != 0)
575 fields = (const char* const *) item->fields;
576 fields_num = item->fields_num;
580 fields = default_server_fields;
581 fields_num = default_server_fields_num;
584 assert (fields != NULL);
585 assert (fields_num > 0);
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, */
590 while ((key = strtok_r (dummy, ",", &saveptr)) != NULL)
596 value = strchr (key, '=');
603 if (value[0] == '\0')
606 /* Check if this item was requested. */
607 for (i = 0; i < fields_num; i++)
608 if (strcasecmp (key, fields[i]) == 0)
613 submit (item->instance, key, value);
614 } /* while (strtok_r) */
619 } /* }}} int powerdns_read_server */
622 * powerdns_update_recursor_command
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'.
628 static int powerdns_update_recursor_command (list_item_t *li) /* {{{ */
636 if (li->fields_num < 1)
638 sstrncpy (buffer, RECURSOR_COMMAND, sizeof (buffer));
642 sstrncpy (buffer, "get ", sizeof (buffer));
643 status = strjoin (&buffer[strlen("get ")], sizeof (buffer) - strlen ("get "),
644 li->fields, li->fields_num,
645 /* seperator = */ " ");
648 ERROR ("powerdns plugin: strjoin failed.");
651 buffer[sizeof (buffer) - 1] = 0;
652 size_t len = strlen (buffer);
653 if (len < sizeof (buffer) - 2)
656 buffer[len++] = '\n';
657 buffer[len++] = '\0';
661 buffer[sizeof (buffer) - 1] = 0;
662 li->command = strdup (buffer);
663 if (li->command == NULL)
665 ERROR ("powerdns plugin: strdup failed.");
670 } /* }}} int powerdns_update_recursor_command */
672 static int powerdns_read_recursor (list_item_t *item) /* {{{ */
675 size_t buffer_size = 0;
686 if (item->command == NULL)
688 status = powerdns_update_recursor_command (item);
691 ERROR ("powerdns plugin: powerdns_update_recursor_command failed.");
695 DEBUG ("powerdns plugin: powerdns_read_recursor: item->command = %s;",
698 assert (item->command != NULL);
700 status = powerdns_get_data (item, &buffer, &buffer_size);
703 ERROR ("powerdns plugin: powerdns_get_data failed.");
707 keys_list = strdup (item->command);
708 if (keys_list == NULL)
710 FUNC_ERROR ("strdup");
716 value_saveptr = NULL;
718 /* Skip the `get' at the beginning */
719 strtok_r (keys_list, " \t", &key_saveptr);
722 while ((value = strtok_r (dummy, " \t\n\r", &value_saveptr)) != NULL)
726 key = strtok_r (NULL, " \t", &key_saveptr);
730 submit (item->instance, key, value);
731 } /* while (strtok_r) */
737 } /* }}} int powerdns_read_recursor */
739 static int powerdns_config_add_collect (list_item_t *li, /* {{{ */
745 if (ci->values_num < 1)
747 WARNING ("powerdns plugin: The `Collect' option needs "
748 "at least one argument.");
752 for (i = 0; i < ci->values_num; i++)
753 if (ci->values[i].type != OCONFIG_TYPE_STRING)
755 WARNING ("powerdns plugin: Only string arguments are allowed to "
756 "the `Collect' option.");
760 temp = (char **) realloc (li->fields,
761 sizeof (char *) * (li->fields_num + ci->values_num));
764 WARNING ("powerdns plugin: realloc failed.");
769 for (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)
774 WARNING ("powerdns plugin: strdup failed.");
780 /* Invalidate a previously computed command */
784 } /* }}} int powerdns_config_add_collect */
786 static int powerdns_config_add_server (oconfig_item_t *ci) /* {{{ */
794 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
796 WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
801 item = (list_item_t *) malloc (sizeof (list_item_t));
804 ERROR ("powerdns plugin: malloc failed.");
807 memset (item, '\0', sizeof (list_item_t));
809 item->instance = strdup (ci->values[0].value.string);
810 if (item->instance == NULL)
812 ERROR ("powerdns plugin: strdup failed.");
818 * Set default values for the members of list_item_t
820 if (strcasecmp ("Server", ci->key) == 0)
822 item->server_type = SRV_AUTHORITATIVE;
823 item->func = powerdns_read_server;
824 item->socktype = SOCK_STREAM;
825 socket_temp = strdup (SERVER_SOCKET);
827 else if (strcasecmp ("Recursor", ci->key) == 0)
829 item->server_type = SRV_RECURSOR;
830 item->func = powerdns_read_recursor;
831 item->socktype = SOCK_DGRAM;
832 socket_temp = strdup (RECURSOR_SOCKET);
836 /* We must never get here.. */
842 for (i = 0; i < ci->children_num; i++)
844 oconfig_item_t *option = ci->children + i;
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);
852 ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
864 if (socket_temp == NULL)
866 ERROR ("powerdns plugin: socket_temp == NULL.");
871 item->sockaddr.sun_family = AF_UNIX;
872 sstrncpy (item->sockaddr.sun_path, socket_temp,
873 sizeof (item->sockaddr.sun_path));
875 e = llentry_create (item->instance, item);
878 ERROR ("powerdns plugin: llentry_create failed.");
882 llist_append (list, e);
894 DEBUG ("powerdns plugin: Add server: instance = %s;", item->instance);
898 } /* }}} int powerdns_config_add_server */
900 static int powerdns_config (oconfig_item_t *ci) /* {{{ */
904 DEBUG ("powerdns plugin: powerdns_config (ci = %p);", (void *) ci);
908 list = llist_create ();
912 ERROR ("powerdns plugin: `llist_create' failed.");
917 for (i = 0; i < ci->children_num; i++)
919 oconfig_item_t *option = ci->children + i;
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)
926 if ((option->values_num != 1) || (option->values[0].type != OCONFIG_TYPE_STRING))
928 WARNING ("powerdns plugin: `%s' needs exactly one string argument.", option->key);
932 char *temp = strdup (option->values[0].value.string);
935 sfree (local_sockpath);
936 local_sockpath = temp;
941 ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
943 } /* for (i = 0; i < ci->children_num; i++) */
946 } /* }}} int powerdns_config */
948 static int powerdns_read (void)
952 for (e = llist_head (list); e != NULL; e = e->next)
954 list_item_t *item = e->value;
959 } /* static int powerdns_read */
961 static int powerdns_shutdown (void)
968 for (e = llist_head (list); e != NULL; e = e->next)
970 list_item_t *item = (list_item_t *) e->value;
973 sfree (item->instance);
974 sfree (item->command);
978 llist_destroy (list);
982 } /* static int powerdns_shutdown */
984 void module_register (void)
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 */
991 /* vim: set sw=2 sts=2 ts=8 fdm=marker : */