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 verplant.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 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
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"},
152 {"recursing-answers", "dns_answer", "recurse"},
153 {"tcp-answers", "dns_answer", "tcp"},
154 {"udp-answers", "dns_answer", "udp"},
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"},
164 {"latency", "latency", NULL},
167 {"corrupt-packets", "ipt_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", "ipt_packets", "servfail"},
173 {"timedout-packets", "ipt_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},
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", "", ""} */
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:
239 * - Complete list of known pdns -> collectd mappings.
240 * - Update the collectd.conf(5) manpage.
245 /* <http://doc.powerdns.com/recursor-stats.html> */
246 static void submit (const char *plugin_instance, /* {{{ */
247 const char *pdns_type, const char *value)
249 value_list_t vl = VALUE_LIST_INIT;
252 const char *type = NULL;
253 const char *type_instance = NULL;
254 const data_set_t *ds;
258 for (i = 0; i < lookup_table_length; i++)
259 if (strcmp (lookup_table[i].name, pdns_type) == 0)
262 if (lookup_table[i].type == NULL)
265 if (i >= lookup_table_length)
267 INFO ("powerdns plugin: submit: Not found in lookup table: %s = %s;",
272 type = lookup_table[i].type;
273 type_instance = lookup_table[i].type_instance;
275 ds = plugin_get_ds (type);
278 ERROR ("powerdns plugin: The lookup table returned type `%s', "
279 "but I cannot find it via `plugin_get_ds'.",
286 ERROR ("powerdns plugin: type `%s' has %i data sources, "
287 "but I can only handle one.",
292 if (0 != parse_value (value, &values[0], ds->ds[0].type))
294 ERROR ("powerdns plugin: Cannot convert `%s' "
295 "to a number.", value);
301 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
302 sstrncpy (vl.plugin, "powerdns", sizeof (vl.plugin));
303 sstrncpy (vl.type, type, sizeof (vl.type));
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));
308 plugin_dispatch_values (&vl);
309 } /* }}} static void submit */
311 static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */
313 size_t *ret_buffer_size)
320 size_t buffer_size = 0;
322 struct sockaddr_un sa_unix;
324 sd = socket (PF_UNIX, item->socktype, 0);
327 FUNC_ERROR ("socket");
331 memset (&sa_unix, 0, sizeof (sa_unix));
332 sa_unix.sun_family = AF_UNIX;
333 sstrncpy (sa_unix.sun_path,
334 (local_sockpath != NULL) ? local_sockpath : PDNS_LOCAL_SOCKPATH,
335 sizeof (sa_unix.sun_path));
337 status = unlink (sa_unix.sun_path);
338 if ((status != 0) && (errno != ENOENT))
340 FUNC_ERROR ("unlink");
347 /* We need to bind to a specific path, because this is a datagram socket
348 * and otherwise the daemon cannot answer. */
349 status = bind (sd, (struct sockaddr *) &sa_unix, sizeof (sa_unix));
356 /* Make the socket writeable by the daemon.. */
357 status = chmod (sa_unix.sun_path, 0666);
360 FUNC_ERROR ("chmod");
364 struct timeval timeout;
366 if (timeout.tv_sec < interval_g * 3 / 4)
367 timeout.tv_sec = interval_g * 3 / 4;
369 status = setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof (timeout));
372 FUNC_ERROR ("setsockopt");
376 status = connect (sd, (struct sockaddr *) &item->sockaddr,
377 sizeof (item->sockaddr));
380 FUNC_ERROR ("connect");
384 status = send (sd, item->command, strlen (item->command), 0);
391 status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
397 buffer_size = status + 1;
402 unlink (sa_unix.sun_path);
407 assert (buffer_size > 0);
408 buffer = (char *) malloc (buffer_size);
411 FUNC_ERROR ("malloc");
415 memcpy (buffer, temp, buffer_size - 1);
416 buffer[buffer_size - 1] = 0;
418 *ret_buffer = buffer;
419 *ret_buffer_size = buffer_size;
422 } /* }}} int powerdns_get_data_dgram */
424 static int powerdns_get_data_stream (list_item_t *item, /* {{{ */
426 size_t *ret_buffer_size)
433 size_t buffer_size = 0;
435 sd = socket (PF_UNIX, item->socktype, 0);
438 FUNC_ERROR ("socket");
442 struct timeval timeout;
445 status = setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof (timeout));
447 status = connect (sd, (struct sockaddr *) &item->sockaddr,
448 sizeof (item->sockaddr));
451 FUNC_ERROR ("connect");
456 /* strlen + 1, because we need to send the terminating NULL byte, too. */
457 status = send (sd, item->command, strlen (item->command) + 1,
470 status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
476 else if (status == 0)
479 buffer_new = (char *) realloc (buffer, buffer_size + status + 1);
480 if (buffer_new == NULL)
482 FUNC_ERROR ("realloc");
488 memcpy (buffer + buffer_size, temp, status);
489 buffer_size += status;
490 buffer[buffer_size] = 0;
501 assert (status == 0);
502 *ret_buffer = buffer;
503 *ret_buffer_size = buffer_size;
507 } /* }}} int powerdns_get_data_stream */
509 static int powerdns_get_data (list_item_t *item, char **ret_buffer,
510 size_t *ret_buffer_size)
512 if (item->socktype == SOCK_DGRAM)
513 return (powerdns_get_data_dgram (item, ret_buffer, ret_buffer_size));
514 else if (item->socktype == SOCK_STREAM)
515 return (powerdns_get_data_stream (item, ret_buffer, ret_buffer_size));
518 ERROR ("powerdns plugin: Unknown socket type: %i", (int) item->socktype);
521 } /* int powerdns_get_data */
523 static int powerdns_read_server (list_item_t *item) /* {{{ */
526 size_t buffer_size = 0;
535 const char* const *fields;
538 if (item->command == NULL)
539 item->command = strdup (SERVER_COMMAND);
540 if (item->command == NULL)
542 ERROR ("powerdns plugin: strdup failed.");
546 status = powerdns_get_data (item, &buffer, &buffer_size);
550 if (item->fields_num != 0)
552 fields = (const char* const *) item->fields;
553 fields_num = item->fields_num;
557 fields = default_server_fields;
558 fields_num = default_server_fields_num;
561 assert (fields != NULL);
562 assert (fields_num > 0);
564 /* 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 while ((key = strtok_r (dummy, ",", &saveptr)) != NULL)
573 value = strchr (key, '=');
580 if (value[0] == '\0')
583 /* Check if this item was requested. */
584 for (i = 0; i < fields_num; i++)
585 if (strcasecmp (key, fields[i]) == 0)
590 submit (item->instance, key, value);
591 } /* while (strtok_r) */
596 } /* }}} int powerdns_read_server */
599 * powerdns_update_recursor_command
601 * Creates a string that holds the command to be sent to the recursor. This
602 * string is stores in the `command' member of the `list_item_t' passed to the
603 * function. This function is called by `powerdns_read_recursor'.
605 static int powerdns_update_recursor_command (list_item_t *li) /* {{{ */
613 if (li->fields_num < 1)
615 sstrncpy (buffer, RECURSOR_COMMAND, sizeof (buffer));
619 sstrncpy (buffer, "get ", sizeof (buffer));
620 status = strjoin (&buffer[strlen("get ")], sizeof (buffer) - strlen ("get "),
621 li->fields, li->fields_num,
622 /* seperator = */ " ");
625 ERROR ("powerdns plugin: strjoin failed.");
628 buffer[sizeof (buffer) - 1] = 0;
629 int i = strlen (buffer);
630 if (i < sizeof (buffer) - 2)
638 buffer[sizeof (buffer) - 1] = 0;
639 li->command = strdup (buffer);
640 if (li->command == NULL)
642 ERROR ("powerdns plugin: strdup failed.");
647 } /* }}} int powerdns_update_recursor_command */
649 static int powerdns_read_recursor (list_item_t *item) /* {{{ */
652 size_t buffer_size = 0;
663 if (item->command == NULL)
665 status = powerdns_update_recursor_command (item);
668 ERROR ("powerdns plugin: powerdns_update_recursor_command failed.");
672 DEBUG ("powerdns plugin: powerdns_read_recursor: item->command = %s;",
675 assert (item->command != NULL);
677 status = powerdns_get_data (item, &buffer, &buffer_size);
680 ERROR ("powerdns plugin: powerdns_get_data failed.");
684 keys_list = strdup (item->command);
685 if (keys_list == NULL)
687 FUNC_ERROR ("strdup");
693 value_saveptr = NULL;
695 /* Skip the `get' at the beginning */
696 strtok_r (keys_list, " \t", &key_saveptr);
699 while ((value = strtok_r (dummy, " \t\n\r", &value_saveptr)) != NULL)
703 key = strtok_r (NULL, " \t", &key_saveptr);
707 submit (item->instance, key, value);
708 } /* while (strtok_r) */
714 } /* }}} int powerdns_read_recursor */
716 static int powerdns_config_add_string (const char *name, /* {{{ */
720 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
722 WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
728 *dest = strdup (ci->values[0].value.string);
733 } /* }}} int powerdns_config_add_string */
735 static int powerdns_config_add_collect (list_item_t *li, /* {{{ */
741 if (ci->values_num < 1)
743 WARNING ("powerdns plugin: The `Collect' option needs "
744 "at least one argument.");
748 for (i = 0; i < ci->values_num; i++)
749 if (ci->values[i].type != OCONFIG_TYPE_STRING)
751 WARNING ("powerdns plugin: Only string arguments are allowed to "
752 "the `Collect' option.");
756 temp = (char **) realloc (li->fields,
757 sizeof (char *) * (li->fields_num + ci->values_num));
760 WARNING ("powerdns plugin: realloc failed.");
765 for (i = 0; i < ci->values_num; i++)
767 li->fields[li->fields_num] = strdup (ci->values[i].value.string);
768 if (li->fields[li->fields_num] == NULL)
770 WARNING ("powerdns plugin: strdup failed.");
776 /* Invalidate a previously computed command */
780 } /* }}} int powerdns_config_add_collect */
782 static int powerdns_config_add_server (oconfig_item_t *ci) /* {{{ */
790 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
792 WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
797 item = (list_item_t *) malloc (sizeof (list_item_t));
800 ERROR ("powerdns plugin: malloc failed.");
803 memset (item, '\0', sizeof (list_item_t));
805 item->instance = strdup (ci->values[0].value.string);
806 if (item->instance == NULL)
808 ERROR ("powerdns plugin: strdup failed.");
814 * Set default values for the members of list_item_t
816 if (strcasecmp ("Server", ci->key) == 0)
818 item->server_type = SRV_AUTHORITATIVE;
819 item->func = powerdns_read_server;
820 item->socktype = SOCK_STREAM;
821 socket_temp = strdup (SERVER_SOCKET);
823 else if (strcasecmp ("Recursor", ci->key) == 0)
825 item->server_type = SRV_RECURSOR;
826 item->func = powerdns_read_recursor;
827 item->socktype = SOCK_DGRAM;
828 socket_temp = strdup (RECURSOR_SOCKET);
832 /* We must never get here.. */
838 for (i = 0; i < ci->children_num; i++)
840 oconfig_item_t *option = ci->children + i;
842 if (strcasecmp ("Collect", option->key) == 0)
843 status = powerdns_config_add_collect (item, option);
844 else if (strcasecmp ("Socket", option->key) == 0)
845 status = powerdns_config_add_string ("Socket", &socket_temp, option);
848 ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
860 if (socket_temp == NULL)
862 ERROR ("powerdns plugin: socket_temp == NULL.");
867 item->sockaddr.sun_family = AF_UNIX;
868 sstrncpy (item->sockaddr.sun_path, socket_temp,
869 sizeof (item->sockaddr.sun_path));
871 e = llentry_create (item->instance, item);
874 ERROR ("powerdns plugin: llentry_create failed.");
878 llist_append (list, e);
889 DEBUG ("powerdns plugin: Add server: instance = %s;", item->instance);
892 } /* }}} int powerdns_config_add_server */
894 static int powerdns_config (oconfig_item_t *ci) /* {{{ */
898 DEBUG ("powerdns plugin: powerdns_config (ci = %p);", (void *) ci);
902 list = llist_create ();
906 ERROR ("powerdns plugin: `llist_create' failed.");
911 for (i = 0; i < ci->children_num; i++)
913 oconfig_item_t *option = ci->children + i;
915 if ((strcasecmp ("Server", option->key) == 0)
916 || (strcasecmp ("Recursor", option->key) == 0))
917 powerdns_config_add_server (option);
918 else if (strcasecmp ("LocalSocket", option->key) == 0)
920 if ((option->values_num != 1) || (option->values[0].type != OCONFIG_TYPE_STRING))
922 WARNING ("powerdns plugin: `%s' needs exactly one string argument.", option->key);
926 char *temp = strdup (option->values[0].value.string);
929 sfree (local_sockpath);
930 local_sockpath = temp;
935 ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
937 } /* for (i = 0; i < ci->children_num; i++) */
940 } /* }}} int powerdns_config */
942 static int powerdns_read (void)
946 for (e = llist_head (list); e != NULL; e = e->next)
948 list_item_t *item = e->value;
953 } /* static int powerdns_read */
955 static int powerdns_shutdown (void)
962 for (e = llist_head (list); e != NULL; e = e->next)
964 list_item_t *item = (list_item_t *) e->value;
967 sfree (item->instance);
968 sfree (item->command);
972 llist_destroy (list);
976 } /* static int powerdns_shutdown */
978 void module_register (void)
980 plugin_register_complex_config ("powerdns", powerdns_config);
981 plugin_register_read ("powerdns", powerdns_read);
982 plugin_register_shutdown ("powerdns", powerdns_shutdown );
983 } /* void module_register */
985 /* vim: set sw=2 sts=2 ts=8 fdm=marker : */