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 "/var/run/pdns.controlsocket"
49 #define SERVER_COMMAND "SHOW *"
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" /* }}} */
64 typedef struct list_item_s list_item_t;
68 int (*func) (list_item_t *item);
71 struct sockaddr_un sockaddr;
75 struct statname_lookup_s
81 typedef struct statname_lookup_s statname_lookup_t;
83 /* Description of statistics returned by the recursor: {{{
84 all-outqueries counts the number of outgoing UDP queries since starting
85 answers0-1 counts the number of queries answered within 1 milisecond
86 answers100-1000 counts the number of queries answered within 1 second
87 answers10-100 counts the number of queries answered within 100 miliseconds
88 answers1-10 counts the number of queries answered within 10 miliseconds
89 answers-slow counts the number of queries answered after 1 second
90 cache-entries shows the number of entries in the cache
91 cache-hits counts the number of cache hits since starting
92 cache-misses counts the number of cache misses since starting
93 chain-resends number of queries chained to existing outstanding query
94 client-parse-errors counts number of client packets that could not be parsed
95 concurrent-queries shows the number of MThreads currently running
96 dlg-only-drops number of records dropped because of delegation only setting
97 negcache-entries shows the number of entries in the Negative answer cache
98 noerror-answers counts the number of times it answered NOERROR since starting
99 nsspeeds-entries shows the number of entries in the NS speeds map
100 nsset-invalidations number of times an nsset was dropped because it no longer worked
101 nxdomain-answers counts the number of times it answered NXDOMAIN since starting
102 outgoing-timeouts counts the number of timeouts on outgoing UDP queries since starting
103 qa-latency shows the current latency average
104 questions counts all End-user initiated queries with the RD bit set
105 resource-limits counts number of queries that could not be performed because of resource limits
106 server-parse-errors counts number of server replied packets that could not be parsed
107 servfail-answers counts the number of times it answered SERVFAIL since starting
108 spoof-prevents number of times PowerDNS considered itself spoofed, and dropped the data
109 sys-msec number of CPU milliseconds spent in 'system' mode
110 tcp-client-overflow number of times an IP address was denied TCP access because it already had too many connections
111 tcp-outqueries counts the number of outgoing TCP queries since starting
112 tcp-questions counts all incoming TCP queries (since starting)
113 throttled-out counts the number of throttled outgoing UDP queries since starting
114 throttle-entries shows the number of entries in the throttle map
115 unauthorized-tcp number of TCP questions denied because of allow-from restrictions
116 unauthorized-udp number of UDP questions denied because of allow-from restrictions
117 unexpected-packets number of answers from remote servers that were unexpected (might point to spoofing)
118 uptime number of seconds process has been running (since 3.1.5)
119 user-msec number of CPU milliseconds spent in 'user' mode
122 statname_lookup_t lookup_table[] = /* {{{ */
124 /*********************
125 * Server statistics *
126 *********************/
128 {"recursing-questions", "dns_question", "recurse"},
129 {"tcp-queries", "dns_question", "tcp"},
130 {"udp-queries", "dns_question", "udp"},
133 {"recursing-answers", "dns_answer", "recurse"},
134 {"tcp-answers", "dns_answer", "tcp"},
135 {"udp-answers", "dns_answer", "udp"},
138 {"packetcache-hit", "cache_result", "packet-hit"},
139 {"packetcache-miss", "cache_result", "packet-miss"},
140 {"packetcache-size", "cache_size", "packet"},
141 {"query-cache-hit", "cache_result", "query-hit"},
142 {"query-cache-miss", "cache_result", "query-miss"},
145 {"latency", "latency", NULL},
148 {"corrupt-packets", "io_packets", "corrupt"},
149 {"deferred-cache-inserts", "counter", "cache-deferred_insert"},
150 {"deferred-cache-lookup", "counter", "cache-deferred_lookup"},
151 {"qsize-a", "cache_size", "answers"},
152 {"qsize-q", "cache_size", "questions"},
153 {"servfail-packets", "io_packets", "servfail"},
154 {"timedout-packets", "io_packets", "timeout"},
155 {"udp4-answers", "dns_answer", "udp4"},
156 {"udp4-queries", "dns_question", "queries-udp4"},
157 {"udp6-answers", "dns_answer", "udp6"},
158 {"udp6-queries", "dns_question", "queries-udp6"},
160 /***********************
161 * Recursor statistics *
162 ***********************/
163 /* Answers by return code */
164 {"noerror-answers", "dns_rcode", "NOERROR"},
165 {"nxdomain-answers", "dns_rcode", "NXDOMAIN"},
166 {"servfail-answers", "dns_rcode", "SERVFAIL"},
168 /* CPU utilization */
169 {"sys-msec", "cpu", "system"},
170 {"user-msec", "cpu", "user"},
172 /* Question-to-answer latency */
173 {"qa-latency", "latency", NULL},
176 {"cache-entries", "cache_size", NULL},
177 {"cache-hits", "cache_result", "hit"},
178 {"cache-misses", "cache_result", "miss"},
180 /* Total number of questions.. */
181 {"questions", "dns_qtype", "total"}
183 /* TODO: Add all recursor metrics here */
185 int lookup_table_length = STATIC_ARRAY_SIZE (lookup_table);
187 static llist_t *list = NULL;
189 #define PDNS_LOCAL_SOCKPATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-powerdns"
190 static char *local_sockpath = NULL;
192 /* TODO: Do this before 4.4:
193 * - Make list of ``interesting values'' configurable
194 * - Authorative server:
195 * - Store each element in a list or an array
196 * - Check values returned by `SHOW *' against that list.
198 * - Use the list to build a command to request the given values
199 * - Complete list of known pdns -> collectd mappings.
204 /* <http://doc.powerdns.com/recursor-stats.html> */
205 static void submit (const char *plugin_instance, /* {{{ */
206 const char *pdns_type, const char *value)
208 value_list_t vl = VALUE_LIST_INIT;
211 const char *type = NULL;
212 const char *type_instance = NULL;
213 const data_set_t *ds;
217 for (i = 0; i < lookup_table_length; i++)
218 if (strcmp (lookup_table[i].name, pdns_type) == 0)
221 if (lookup_table[i].type == NULL)
224 if (i >= lookup_table_length)
226 DEBUG ("powerdns plugin: submit: Not found in lookup table: %s = %s;",
231 type = lookup_table[i].type;
232 type_instance = lookup_table[i].type_instance;
234 ds = plugin_get_ds (type);
237 ERROR ("powerdns plugin: The lookup table returned type `%s', "
238 "but I cannot find it via `plugin_get_ds'.",
245 ERROR ("powerdns plugin: type `%s' has %i data sources, "
246 "but I can only handle one.",
251 if (ds->ds[0].type == DS_TYPE_GAUGE)
255 values[0].gauge = strtod (value, &endptr);
259 ERROR ("powerdns plugin: Cannot convert `%s' "
260 "to a floating point number.", value);
268 values[0].counter = strtoll (value, &endptr, 0);
271 ERROR ("powerdns plugin: Cannot convert `%s' "
272 "to an integer number.", value);
279 vl.time = time (NULL);
280 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
281 sstrncpy (vl.plugin, "powerdns", sizeof (vl.plugin));
282 if (type_instance != NULL)
283 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
284 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
286 plugin_dispatch_values (type, &vl);
287 } /* }}} static void submit */
289 static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */
291 size_t *ret_buffer_size)
298 size_t buffer_size = 0;
300 struct sockaddr_un sa_unix;
302 sd = socket (PF_UNIX, item->socktype, 0);
305 FUNC_ERROR ("socket");
309 memset (&sa_unix, 0, sizeof (sa_unix));
310 sa_unix.sun_family = AF_UNIX;
311 strncpy (sa_unix.sun_path,
312 (local_sockpath != NULL) ? local_sockpath : PDNS_LOCAL_SOCKPATH,
313 sizeof (sa_unix.sun_path));
314 sa_unix.sun_path[sizeof (sa_unix.sun_path) - 1] = 0;
316 status = unlink (sa_unix.sun_path);
317 if ((status != 0) && (errno != ENOENT))
319 FUNC_ERROR ("unlink");
326 /* We need to bind to a specific path, because this is a datagram socket
327 * and otherwise the daemon cannot answer. */
328 status = bind (sd, (struct sockaddr *) &sa_unix, sizeof (sa_unix));
335 /* Make the socket writeable by the daemon.. */
336 status = chmod (sa_unix.sun_path, 0666);
339 FUNC_ERROR ("chmod");
343 status = connect (sd, (struct sockaddr *) &item->sockaddr,
344 sizeof (item->sockaddr));
347 FUNC_ERROR ("connect");
351 status = send (sd, item->command, strlen (item->command), 0);
358 status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
368 unlink (sa_unix.sun_path);
373 buffer_size = status + 1;
374 buffer = (char *) malloc (buffer_size);
377 FUNC_ERROR ("malloc");
381 memcpy (buffer, temp, status);
384 *ret_buffer = buffer;
385 *ret_buffer_size = buffer_size;
388 } /* }}} int powerdns_get_data_dgram */
390 static int powerdns_get_data_stream (list_item_t *item, /* {{{ */
392 size_t *ret_buffer_size)
399 size_t buffer_size = 0;
401 sd = socket (PF_UNIX, item->socktype, 0);
404 FUNC_ERROR ("socket");
408 status = connect (sd, (struct sockaddr *) &item->sockaddr,
409 sizeof (item->sockaddr));
412 FUNC_ERROR ("connect");
417 /* strlen + 1, because we need to send the terminating NULL byte, too. */
418 status = send (sd, item->command, strlen (item->command) + 1,
431 status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
437 else if (status == 0)
440 buffer_new = (char *) realloc (buffer, buffer_size + status + 1);
441 if (buffer_new == NULL)
443 FUNC_ERROR ("realloc");
449 memcpy (buffer + buffer_size, temp, status);
450 buffer_size += status;
451 buffer[buffer_size] = 0;
462 assert (status == 0);
463 *ret_buffer = buffer;
464 *ret_buffer_size = buffer_size;
468 } /* }}} int powerdns_get_data_stream */
470 static int powerdns_get_data (list_item_t *item, char **ret_buffer,
471 size_t *ret_buffer_size)
473 if (item->socktype == SOCK_DGRAM)
474 return (powerdns_get_data_dgram (item, ret_buffer, ret_buffer_size));
475 else if (item->socktype == SOCK_STREAM)
476 return (powerdns_get_data_stream (item, ret_buffer, ret_buffer_size));
479 ERROR ("powerdns plugin: Unknown socket type: %i", (int) item->socktype);
482 } /* int powerdns_get_data */
484 static int powerdns_read_server (list_item_t *item) /* {{{ */
487 size_t buffer_size = 0;
496 status = powerdns_get_data (item, &buffer, &buffer_size);
500 /* 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, */
503 while ((key = strtok_r (dummy, ",", &saveptr)) != NULL)
507 value = strchr (key, '=');
514 if (value[0] == '\0')
517 submit (item->instance, key, value);
518 } /* while (strtok_r) */
523 } /* }}} int powerdns_read_server */
525 static int powerdns_read_recursor (list_item_t *item) /* {{{ */
528 size_t buffer_size = 0;
539 status = powerdns_get_data (item, &buffer, &buffer_size);
543 keys_list = strdup (item->command);
544 if (keys_list == NULL)
546 FUNC_ERROR ("strdup");
552 value_saveptr = NULL;
554 /* Skip the `get' at the beginning */
555 strtok_r (keys_list, " \t", &key_saveptr);
558 while ((value = strtok_r (dummy, " \t\n\r", &value_saveptr)) != NULL)
562 key = strtok_r (NULL, " \t", &key_saveptr);
566 submit (item->instance, key, value);
567 } /* while (strtok_r) */
573 } /* }}} int powerdns_read_recursor */
575 static int powerdns_config_add_string (const char *name, /* {{{ */
579 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
581 WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
587 *dest = strdup (ci->values[0].value.string);
592 } /* }}} int ctail_config_add_string */
594 static int powerdns_config_add_server (oconfig_item_t *ci) /* {{{ */
602 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
604 WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
609 item = (list_item_t *) malloc (sizeof (list_item_t));
612 ERROR ("powerdns plugin: malloc failed.");
615 memset (item, '\0', sizeof (list_item_t));
617 item->instance = strdup (ci->values[0].value.string);
618 if (item->instance == NULL)
620 ERROR ("powerdns plugin: strdup failed.");
626 * Set default values for the members of list_item_t
628 if (strcasecmp ("Server", ci->key) == 0)
630 item->func = powerdns_read_server;
631 item->command = strdup (SERVER_COMMAND);
632 item->socktype = SOCK_STREAM;
633 socket_temp = strdup (SERVER_SOCKET);
635 else if (strcasecmp ("Recursor", ci->key) == 0)
637 item->func = powerdns_read_recursor;
638 item->command = strdup (RECURSOR_COMMAND);
639 item->socktype = SOCK_DGRAM;
640 socket_temp = strdup (RECURSOR_SOCKET);
644 for (i = 0; i < ci->children_num; i++)
646 oconfig_item_t *option = ci->children + i;
648 if (strcasecmp ("Command", option->key) == 0)
649 status = powerdns_config_add_string ("Command", &item->command, option);
650 else if (strcasecmp ("Socket", option->key) == 0)
651 status = powerdns_config_add_string ("Socket", &socket_temp, option);
654 ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
666 if (socket_temp == NULL)
668 ERROR ("powerdns plugin: socket_temp == NULL.");
673 if (item->command == NULL)
675 ERROR ("powerdns plugin: item->command == NULL.");
680 item->sockaddr.sun_family = AF_UNIX;
681 sstrncpy (item->sockaddr.sun_path, socket_temp, UNIX_PATH_MAX);
683 e = llentry_create (item->instance, item);
686 ERROR ("powerdns plugin: llentry_create failed.");
690 llist_append (list, e);
701 DEBUG ("powerdns plugin: Add server: instance = %s;", item->instance);
704 } /* }}} int powerdns_config_add_server */
706 static int powerdns_config (oconfig_item_t *ci) /* {{{ */
710 DEBUG ("powerdns plugin: powerdns_config (ci = %p);", (void *) ci);
714 list = llist_create ();
718 ERROR ("powerdns plugin: `llist_create' failed.");
723 for (i = 0; i < ci->children_num; i++)
725 oconfig_item_t *option = ci->children + i;
727 if ((strcasecmp ("Server", option->key) == 0)
728 || (strcasecmp ("Recursor", option->key) == 0))
729 powerdns_config_add_server (option);
730 if (strcasecmp ("LocalSocket", option->key) == 0)
732 char *temp = strdup (option->key);
735 sfree (local_sockpath);
736 local_sockpath = temp;
740 ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
742 } /* for (i = 0; i < ci->children_num; i++) */
745 } /* }}} int powerdns_config */
747 static int powerdns_read (void)
751 for (e = llist_head (list); e != NULL; e = e->next)
753 list_item_t *item = e->value;
758 } /* static int powerdns_read */
760 static int powerdns_shutdown (void)
767 for (e = llist_head (list); e != NULL; e = e->next)
769 list_item_t *item = (list_item_t *) e->value;
772 sfree (item->instance);
773 sfree (item->command);
777 llist_destroy (list);
781 } /* static int powerdns_shutdown */
783 void module_register (void)
785 plugin_register_complex_config ("powerdns", powerdns_config);
786 plugin_register_read ("powerdns", powerdns_read);
787 plugin_register_shutdown ("powerdns", powerdns_shutdown );
788 } /* void module_register */
790 /* vim: set sw=2 sts=2 ts=8 fdm=marker : */