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>
40 #include <sys/socket.h>
44 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
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 * \n"
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\n"
57 typedef struct list_item_s list_item_t;
66 int (*func) (list_item_t *item);
73 struct sockaddr_un sockaddr;
77 struct statname_lookup_s
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 millisecond
88 answers100-1000 counts the number of queries answered within 1 second
89 answers10-100 counts the number of queries answered within 100 milliseconds
90 answers1-10 counts the number of queries answered within 10 milliseconds
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
124 const char* const default_server_fields[] = /* {{{ */
133 "recursing-questions",
139 int default_server_fields_num = STATIC_ARRAY_SIZE (default_server_fields);
141 statname_lookup_t lookup_table[] = /* {{{ */
143 /*********************
144 * Server statistics *
145 *********************/
147 {"recursing-questions", "dns_question", "recurse"},
148 {"tcp-queries", "dns_question", "tcp"},
149 {"udp-queries", "dns_question", "udp"},
150 {"rd-queries", "dns_question", "rd"},
153 {"recursing-answers", "dns_answer", "recurse"},
154 {"tcp-answers", "dns_answer", "tcp"},
155 {"udp-answers", "dns_answer", "udp"},
156 {"recursion-unanswered", "dns_answer", "recursion-unanswered"},
157 {"udp-answers-bytes", "total_bytes", "udp-answers-bytes"},
160 {"packetcache-hit", "cache_result", "packet-hit"},
161 {"packetcache-miss", "cache_result", "packet-miss"},
162 {"packetcache-size", "cache_size", "packet"},
163 {"key-cache-size", "cache_size", "key"},
164 {"meta-cache-size", "cache_size", "meta"},
165 {"signature-cache-size", "cache_size", "signature"},
166 {"query-cache-hit", "cache_result", "query-hit"},
167 {"query-cache-miss", "cache_result", "query-miss"},
170 {"latency", "latency", NULL},
173 {"dnsupdate-answers", "dns_answer", "dnsupdate-answer"},
174 {"dnsupdate-changes", "dns_question", "dnsupdate-changes"},
175 {"dnsupdate-queries", "dns_question", "dnsupdate-queries"},
176 {"dnsupdate-refused", "dns_answer", "dnsupdate-refused"},
179 {"corrupt-packets", "ipt_packets", "corrupt"},
180 {"deferred-cache-inserts", "counter", "cache-deferred_insert"},
181 {"deferred-cache-lookup", "counter", "cache-deferred_lookup"},
182 {"qsize-a", "cache_size", "answers"},
183 {"qsize-q", "cache_size", "questions"},
184 {"servfail-packets", "ipt_packets", "servfail"},
185 {"timedout-packets", "ipt_packets", "timeout"},
186 {"udp4-answers", "dns_answer", "udp4"},
187 {"udp4-queries", "dns_question", "queries-udp4"},
188 {"udp6-answers", "dns_answer", "udp6"},
189 {"udp6-queries", "dns_question", "queries-udp6"},
190 {"security-status", "dns_question", "security-status"},
191 {"udp-do-queries", "dns_question", "udp-do_queries"},
192 {"signatures", "counter", "signatures"},
194 /***********************
195 * Recursor statistics *
196 ***********************/
197 /* Answers by return code */
198 {"noerror-answers", "dns_rcode", "NOERROR"},
199 {"nxdomain-answers", "dns_rcode", "NXDOMAIN"},
200 {"servfail-answers", "dns_rcode", "SERVFAIL"},
202 /* CPU utilization */
203 {"sys-msec", "cpu", "system"},
204 {"user-msec", "cpu", "user"},
206 /* Question-to-answer latency */
207 {"qa-latency", "latency", NULL},
210 {"cache-entries", "cache_size", NULL},
211 {"cache-hits", "cache_result", "hit"},
212 {"cache-misses", "cache_result", "miss"},
214 /* Total number of questions.. */
215 {"questions", "dns_qtype", "total"},
217 /* All the other stuff.. */
218 {"all-outqueries", "dns_question", "outgoing"},
219 {"answers0-1", "dns_answer", "0_1"},
220 {"answers1-10", "dns_answer", "1_10"},
221 {"answers10-100", "dns_answer", "10_100"},
222 {"answers100-1000", "dns_answer", "100_1000"},
223 {"answers-slow", "dns_answer", "slow"},
224 {"chain-resends", "dns_question", "chained"},
225 {"client-parse-errors", "counter", "drops-client_parse_error"},
226 {"concurrent-queries", "dns_question", "concurrent"},
227 {"dlg-only-drops", "counter", "drops-delegation_only"},
228 {"negcache-entries", "cache_size", "negative"},
229 {"nsspeeds-entries", "gauge", "entries-ns_speeds"},
230 {"nsset-invalidations", "counter", "ns_set_invalidation"},
231 {"outgoing-timeouts", "counter", "drops-timeout_outgoing"},
232 {"resource-limits", "counter", "drops-resource_limit"},
233 {"server-parse-errors", "counter", "drops-server_parse_error"},
234 {"spoof-prevents", "counter", "drops-spoofed"},
235 {"tcp-client-overflow", "counter", "denied-client_overflow_tcp"},
236 {"tcp-outqueries", "dns_question", "outgoing-tcp"},
237 {"tcp-questions", "dns_question", "incoming-tcp"},
238 {"throttled-out", "dns_question", "outgoing-throttled"},
239 {"throttle-entries", "gauge", "entries-throttle"},
240 {"unauthorized-tcp", "counter", "denied-unauthorized_tcp"},
241 {"unauthorized-udp", "counter", "denied-unauthorized_udp"},
242 {"unexpected-packets", "dns_answer", "unexpected"},
243 {"uptime", "uptime", NULL}
245 int lookup_table_length = STATIC_ARRAY_SIZE (lookup_table);
247 static llist_t *list = NULL;
249 #define PDNS_LOCAL_SOCKPATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-powerdns"
250 static char *local_sockpath = NULL;
252 /* TODO: Do this before 4.4:
254 * - Complete list of known pdns -> collectd mappings.
255 * - Update the collectd.conf(5) manpage.
260 /* <http://doc.powerdns.com/recursor-stats.html> */
261 static void submit (const char *plugin_instance, /* {{{ */
262 const char *pdns_type, const char *value)
264 value_list_t vl = VALUE_LIST_INIT;
267 const char *type = NULL;
268 const char *type_instance = NULL;
269 const data_set_t *ds;
273 for (i = 0; i < lookup_table_length; i++)
274 if (strcmp (lookup_table[i].name, pdns_type) == 0)
277 if (i >= lookup_table_length)
279 INFO ("powerdns plugin: submit: Not found in lookup table: %s = %s;",
284 if (lookup_table[i].type == NULL)
287 type = lookup_table[i].type;
288 type_instance = lookup_table[i].type_instance;
290 ds = plugin_get_ds (type);
293 ERROR ("powerdns plugin: The lookup table returned type `%s', "
294 "but I cannot find it via `plugin_get_ds'.",
301 ERROR ("powerdns plugin: type `%s' has %zu data sources, "
302 "but I can only handle one.",
307 if (0 != parse_value (value, &values[0], ds->ds[0].type))
309 ERROR ("powerdns plugin: Cannot convert `%s' "
310 "to a number.", value);
316 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
317 sstrncpy (vl.plugin, "powerdns", sizeof (vl.plugin));
318 sstrncpy (vl.type, type, sizeof (vl.type));
319 if (type_instance != NULL)
320 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
321 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
323 plugin_dispatch_values (&vl);
324 } /* }}} static void submit */
326 static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */
328 size_t *ret_buffer_size)
335 size_t buffer_size = 0;
337 struct sockaddr_un sa_unix;
339 struct timeval stv_timeout;
340 cdtime_t cdt_timeout;
342 sd = socket (PF_UNIX, item->socktype, 0);
345 FUNC_ERROR ("socket");
349 memset (&sa_unix, 0, sizeof (sa_unix));
350 sa_unix.sun_family = AF_UNIX;
351 sstrncpy (sa_unix.sun_path,
352 (local_sockpath != NULL) ? local_sockpath : PDNS_LOCAL_SOCKPATH,
353 sizeof (sa_unix.sun_path));
355 status = unlink (sa_unix.sun_path);
356 if ((status != 0) && (errno != ENOENT))
358 FUNC_ERROR ("unlink");
365 /* We need to bind to a specific path, because this is a datagram socket
366 * and otherwise the daemon cannot answer. */
367 status = bind (sd, (struct sockaddr *) &sa_unix, sizeof (sa_unix));
374 /* Make the socket writeable by the daemon.. */
375 status = chmod (sa_unix.sun_path, 0666);
378 FUNC_ERROR ("chmod");
382 cdt_timeout = plugin_get_interval () * 3 / 4;
383 if (cdt_timeout < TIME_T_TO_CDTIME_T (2))
384 cdt_timeout = TIME_T_TO_CDTIME_T (2);
386 CDTIME_T_TO_TIMEVAL (cdt_timeout, &stv_timeout);
388 status = setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO, &stv_timeout, sizeof (stv_timeout));
391 FUNC_ERROR ("setsockopt");
395 status = connect (sd, (struct sockaddr *) &item->sockaddr,
396 sizeof (item->sockaddr));
399 FUNC_ERROR ("connect");
403 status = send (sd, item->command, strlen (item->command), 0);
410 status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
416 buffer_size = status + 1;
421 unlink (sa_unix.sun_path);
426 assert (buffer_size > 0);
427 buffer = (char *) malloc (buffer_size);
430 FUNC_ERROR ("malloc");
434 memcpy (buffer, temp, buffer_size - 1);
435 buffer[buffer_size - 1] = 0;
437 *ret_buffer = buffer;
438 *ret_buffer_size = buffer_size;
441 } /* }}} int powerdns_get_data_dgram */
443 static int powerdns_get_data_stream (list_item_t *item, /* {{{ */
445 size_t *ret_buffer_size)
452 size_t buffer_size = 0;
454 sd = socket (PF_UNIX, item->socktype, 0);
457 FUNC_ERROR ("socket");
461 struct timeval timeout;
464 status = setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof (timeout));
467 FUNC_ERROR ("setsockopt");
472 status = connect (sd, (struct sockaddr *) &item->sockaddr,
473 sizeof (item->sockaddr));
476 FUNC_ERROR ("connect");
481 /* strlen + 1, because we need to send the terminating NULL byte, too. */
482 status = send (sd, item->command, strlen (item->command) + 1,
495 status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
501 else if (status == 0)
504 buffer_new = (char *) realloc (buffer, buffer_size + status + 1);
505 if (buffer_new == NULL)
507 FUNC_ERROR ("realloc");
513 memcpy (buffer + buffer_size, temp, status);
514 buffer_size += status;
515 buffer[buffer_size] = 0;
525 assert (status == 0);
526 *ret_buffer = buffer;
527 *ret_buffer_size = buffer_size;
531 } /* }}} int powerdns_get_data_stream */
533 static int powerdns_get_data (list_item_t *item, char **ret_buffer,
534 size_t *ret_buffer_size)
536 if (item->socktype == SOCK_DGRAM)
537 return (powerdns_get_data_dgram (item, ret_buffer, ret_buffer_size));
538 else if (item->socktype == SOCK_STREAM)
539 return (powerdns_get_data_stream (item, ret_buffer, ret_buffer_size));
542 ERROR ("powerdns plugin: Unknown socket type: %i", (int) item->socktype);
545 } /* int powerdns_get_data */
547 static int powerdns_read_server (list_item_t *item) /* {{{ */
550 size_t buffer_size = 0;
559 const char* const *fields;
562 if (item->command == NULL)
563 item->command = strdup (SERVER_COMMAND);
564 if (item->command == NULL)
566 ERROR ("powerdns plugin: strdup failed.");
570 status = powerdns_get_data (item, &buffer, &buffer_size);
574 if (item->fields_num != 0)
576 fields = (const char* const *) item->fields;
577 fields_num = item->fields_num;
581 fields = default_server_fields;
582 fields_num = default_server_fields_num;
585 assert (fields != NULL);
586 assert (fields_num > 0);
588 /* 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, */
591 while ((key = strtok_r (dummy, ",", &saveptr)) != NULL)
597 value = strchr (key, '=');
604 if (value[0] == '\0')
607 /* Check if this item was requested. */
608 for (i = 0; i < fields_num; i++)
609 if (strcasecmp (key, fields[i]) == 0)
614 submit (item->instance, key, value);
615 } /* while (strtok_r) */
620 } /* }}} int powerdns_read_server */
623 * powerdns_update_recursor_command
625 * Creates a string that holds the command to be sent to the recursor. This
626 * string is stores in the `command' member of the `list_item_t' passed to the
627 * function. This function is called by `powerdns_read_recursor'.
629 static int powerdns_update_recursor_command (list_item_t *li) /* {{{ */
637 if (li->fields_num < 1)
639 sstrncpy (buffer, RECURSOR_COMMAND, sizeof (buffer));
643 sstrncpy (buffer, "get ", sizeof (buffer));
644 status = strjoin (&buffer[strlen("get ")], sizeof (buffer) - strlen ("get "),
645 li->fields, li->fields_num,
646 /* seperator = */ " ");
649 ERROR ("powerdns plugin: strjoin failed.");
652 buffer[sizeof (buffer) - 1] = 0;
653 size_t len = strlen (buffer);
654 if (len < sizeof (buffer) - 2)
657 buffer[len++] = '\n';
658 buffer[len++] = '\0';
662 buffer[sizeof (buffer) - 1] = 0;
663 li->command = strdup (buffer);
664 if (li->command == NULL)
666 ERROR ("powerdns plugin: strdup failed.");
671 } /* }}} int powerdns_update_recursor_command */
673 static int powerdns_read_recursor (list_item_t *item) /* {{{ */
676 size_t buffer_size = 0;
687 if (item->command == NULL)
689 status = powerdns_update_recursor_command (item);
692 ERROR ("powerdns plugin: powerdns_update_recursor_command failed.");
696 DEBUG ("powerdns plugin: powerdns_read_recursor: item->command = %s;",
699 assert (item->command != NULL);
701 status = powerdns_get_data (item, &buffer, &buffer_size);
704 ERROR ("powerdns plugin: powerdns_get_data failed.");
708 keys_list = strdup (item->command);
709 if (keys_list == NULL)
711 FUNC_ERROR ("strdup");
717 value_saveptr = NULL;
719 /* Skip the `get' at the beginning */
720 strtok_r (keys_list, " \t", &key_saveptr);
723 while ((value = strtok_r (dummy, " \t\n\r", &value_saveptr)) != NULL)
727 key = strtok_r (NULL, " \t", &key_saveptr);
731 submit (item->instance, key, value);
732 } /* while (strtok_r) */
738 } /* }}} int powerdns_read_recursor */
740 static int powerdns_config_add_collect (list_item_t *li, /* {{{ */
746 if (ci->values_num < 1)
748 WARNING ("powerdns plugin: The `Collect' option needs "
749 "at least one argument.");
753 for (i = 0; i < ci->values_num; i++)
754 if (ci->values[i].type != OCONFIG_TYPE_STRING)
756 WARNING ("powerdns plugin: Only string arguments are allowed to "
757 "the `Collect' option.");
761 temp = (char **) realloc (li->fields,
762 sizeof (char *) * (li->fields_num + ci->values_num));
765 WARNING ("powerdns plugin: realloc failed.");
770 for (i = 0; i < ci->values_num; i++)
772 li->fields[li->fields_num] = strdup (ci->values[i].value.string);
773 if (li->fields[li->fields_num] == NULL)
775 WARNING ("powerdns plugin: strdup failed.");
781 /* Invalidate a previously computed command */
785 } /* }}} int powerdns_config_add_collect */
787 static int powerdns_config_add_server (oconfig_item_t *ci) /* {{{ */
795 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
797 WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
802 item = (list_item_t *) malloc (sizeof (list_item_t));
805 ERROR ("powerdns plugin: malloc failed.");
808 memset (item, '\0', sizeof (list_item_t));
810 item->instance = strdup (ci->values[0].value.string);
811 if (item->instance == NULL)
813 ERROR ("powerdns plugin: strdup failed.");
819 * Set default values for the members of list_item_t
821 if (strcasecmp ("Server", ci->key) == 0)
823 item->server_type = SRV_AUTHORITATIVE;
824 item->func = powerdns_read_server;
825 item->socktype = SOCK_STREAM;
826 socket_temp = strdup (SERVER_SOCKET);
828 else if (strcasecmp ("Recursor", ci->key) == 0)
830 item->server_type = SRV_RECURSOR;
831 item->func = powerdns_read_recursor;
832 item->socktype = SOCK_DGRAM;
833 socket_temp = strdup (RECURSOR_SOCKET);
837 /* We must never get here.. */
843 for (i = 0; i < ci->children_num; i++)
845 oconfig_item_t *option = ci->children + i;
847 if (strcasecmp ("Collect", option->key) == 0)
848 status = powerdns_config_add_collect (item, option);
849 else if (strcasecmp ("Socket", option->key) == 0)
850 status = cf_util_get_string (option, &socket_temp);
853 ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
865 if (socket_temp == NULL)
867 ERROR ("powerdns plugin: socket_temp == NULL.");
872 item->sockaddr.sun_family = AF_UNIX;
873 sstrncpy (item->sockaddr.sun_path, socket_temp,
874 sizeof (item->sockaddr.sun_path));
876 e = llentry_create (item->instance, item);
879 ERROR ("powerdns plugin: llentry_create failed.");
883 llist_append (list, e);
895 DEBUG ("powerdns plugin: Add server: instance = %s;", item->instance);
899 } /* }}} int powerdns_config_add_server */
901 static int powerdns_config (oconfig_item_t *ci) /* {{{ */
905 DEBUG ("powerdns plugin: powerdns_config (ci = %p);", (void *) ci);
909 list = llist_create ();
913 ERROR ("powerdns plugin: `llist_create' failed.");
918 for (i = 0; i < ci->children_num; i++)
920 oconfig_item_t *option = ci->children + i;
922 if ((strcasecmp ("Server", option->key) == 0)
923 || (strcasecmp ("Recursor", option->key) == 0))
924 powerdns_config_add_server (option);
925 else if (strcasecmp ("LocalSocket", option->key) == 0)
927 if ((option->values_num != 1) || (option->values[0].type != OCONFIG_TYPE_STRING))
929 WARNING ("powerdns plugin: `%s' needs exactly one string argument.", option->key);
933 char *temp = strdup (option->values[0].value.string);
936 sfree (local_sockpath);
937 local_sockpath = temp;
942 ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
944 } /* for (i = 0; i < ci->children_num; i++) */
947 } /* }}} int powerdns_config */
949 static int powerdns_read (void)
953 for (e = llist_head (list); e != NULL; e = e->next)
955 list_item_t *item = e->value;
960 } /* static int powerdns_read */
962 static int powerdns_shutdown (void)
969 for (e = llist_head (list); e != NULL; e = e->next)
971 list_item_t *item = (list_item_t *) e->value;
974 sfree (item->instance);
975 sfree (item->command);
979 llist_destroy (list);
983 } /* static int powerdns_shutdown */
985 void module_register (void)
987 plugin_register_complex_config ("powerdns", powerdns_config);
988 plugin_register_read ("powerdns", powerdns_read);
989 plugin_register_shutdown ("powerdns", powerdns_shutdown );
990 } /* void module_register */
992 /* vim: set sw=2 sts=2 ts=8 fdm=marker : */