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;
73 int (*func) (list_item_t *item);
80 struct sockaddr_un sockaddr;
84 struct statname_lookup_s
90 typedef struct statname_lookup_s statname_lookup_t;
92 /* Description of statistics returned by the recursor: {{{
93 all-outqueries counts the number of outgoing UDP queries since starting
94 answers0-1 counts the number of queries answered within 1 milisecond
95 answers100-1000 counts the number of queries answered within 1 second
96 answers10-100 counts the number of queries answered within 100 miliseconds
97 answers1-10 counts the number of queries answered within 10 miliseconds
98 answers-slow counts the number of queries answered after 1 second
99 cache-entries shows the number of entries in the cache
100 cache-hits counts the number of cache hits since starting
101 cache-misses counts the number of cache misses since starting
102 chain-resends number of queries chained to existing outstanding query
103 client-parse-errors counts number of client packets that could not be parsed
104 concurrent-queries shows the number of MThreads currently running
105 dlg-only-drops number of records dropped because of delegation only setting
106 negcache-entries shows the number of entries in the Negative answer cache
107 noerror-answers counts the number of times it answered NOERROR since starting
108 nsspeeds-entries shows the number of entries in the NS speeds map
109 nsset-invalidations number of times an nsset was dropped because it no longer worked
110 nxdomain-answers counts the number of times it answered NXDOMAIN since starting
111 outgoing-timeouts counts the number of timeouts on outgoing UDP queries since starting
112 qa-latency shows the current latency average
113 questions counts all End-user initiated queries with the RD bit set
114 resource-limits counts number of queries that could not be performed because of resource limits
115 server-parse-errors counts number of server replied packets that could not be parsed
116 servfail-answers counts the number of times it answered SERVFAIL since starting
117 spoof-prevents number of times PowerDNS considered itself spoofed, and dropped the data
118 sys-msec number of CPU milliseconds spent in 'system' mode
119 tcp-client-overflow number of times an IP address was denied TCP access because it already had too many connections
120 tcp-outqueries counts the number of outgoing TCP queries since starting
121 tcp-questions counts all incoming TCP queries (since starting)
122 throttled-out counts the number of throttled outgoing UDP queries since starting
123 throttle-entries shows the number of entries in the throttle map
124 unauthorized-tcp number of TCP questions denied because of allow-from restrictions
125 unauthorized-udp number of UDP questions denied because of allow-from restrictions
126 unexpected-packets number of answers from remote servers that were unexpected (might point to spoofing)
127 uptime number of seconds process has been running (since 3.1.5)
128 user-msec number of CPU milliseconds spent in 'user' mode
131 statname_lookup_t lookup_table[] = /* {{{ */
133 /*********************
134 * Server statistics *
135 *********************/
137 {"recursing-questions", "dns_question", "recurse"},
138 {"tcp-queries", "dns_question", "tcp"},
139 {"udp-queries", "dns_question", "udp"},
142 {"recursing-answers", "dns_answer", "recurse"},
143 {"tcp-answers", "dns_answer", "tcp"},
144 {"udp-answers", "dns_answer", "udp"},
147 {"packetcache-hit", "cache_result", "packet-hit"},
148 {"packetcache-miss", "cache_result", "packet-miss"},
149 {"packetcache-size", "cache_size", "packet"},
150 {"query-cache-hit", "cache_result", "query-hit"},
151 {"query-cache-miss", "cache_result", "query-miss"},
154 {"latency", "latency", NULL},
157 {"corrupt-packets", "io_packets", "corrupt"},
158 {"deferred-cache-inserts", "counter", "cache-deferred_insert"},
159 {"deferred-cache-lookup", "counter", "cache-deferred_lookup"},
160 {"qsize-a", "cache_size", "answers"},
161 {"qsize-q", "cache_size", "questions"},
162 {"servfail-packets", "io_packets", "servfail"},
163 {"timedout-packets", "io_packets", "timeout"},
164 {"udp4-answers", "dns_answer", "udp4"},
165 {"udp4-queries", "dns_question", "queries-udp4"},
166 {"udp6-answers", "dns_answer", "udp6"},
167 {"udp6-queries", "dns_question", "queries-udp6"},
169 /***********************
170 * Recursor statistics *
171 ***********************/
172 /* Answers by return code */
173 {"noerror-answers", "dns_rcode", "NOERROR"},
174 {"nxdomain-answers", "dns_rcode", "NXDOMAIN"},
175 {"servfail-answers", "dns_rcode", "SERVFAIL"},
177 /* CPU utilization */
178 {"sys-msec", "cpu", "system"},
179 {"user-msec", "cpu", "user"},
181 /* Question-to-answer latency */
182 {"qa-latency", "latency", NULL},
185 {"cache-entries", "cache_size", NULL},
186 {"cache-hits", "cache_result", "hit"},
187 {"cache-misses", "cache_result", "miss"},
189 /* Total number of questions.. */
190 {"questions", "dns_qtype", "total"},
192 /* All the other stuff.. */
193 {"all-outqueries", "dns_question", "outgoing"},
194 {"answers0-1", "dns_answer", "0_1"},
195 {"answers1-10", "dns_answer", "1_10"},
196 {"answers10-100", "dns_answer", "10_100"},
197 {"answers100-1000", "dns_answer", "100_1000"},
198 {"answers-slow", "dns_answer", "slow"},
199 {"chain-resends", "dns_question", "chained"},
200 {"client-parse-errors", "counter", "drops-client_parse_error"},
201 {"concurrent-queries", "dns_question", "concurrent"},
202 {"dlg-only-drops", "counter", "drops-delegation_only"},
203 {"negcache-entries", "cache_size", "negative"},
204 {"nsspeeds-entries", "gauge", "entries-ns_speeds"},
205 {"nsset-invalidations", "counter", "ns_set_invalidation"},
206 {"outgoing-timeouts", "counter", "drops-timeout_outgoing"},
207 {"resource-limits", "counter", "drops-resource_limit"},
208 {"server-parse-errors", "counter", "drops-server_parse_error"},
209 {"spoof-prevents", "counter", "drops-spoofed"},
210 {"tcp-client-overflow", "counter", "denied-client_overflow_tcp"},
211 {"tcp-outqueries", "dns_question", "outgoing-tcp"},
212 {"tcp-questions", "dns_question", "incoming-tcp"},
213 {"throttled-out", "dns_question", "outgoing-throttled"},
214 {"throttle-entries", "gauge", "entries-throttle"},
215 {"unauthorized-tcp", "counter", "denied-unauthorized_tcp"},
216 {"unauthorized-udp", "counter", "denied-unauthorized_udp"},
217 {"unexpected-packets", "dns_answer", "unexpected"}
218 /* {"uptime", "", ""} */
220 int lookup_table_length = STATIC_ARRAY_SIZE (lookup_table);
222 static llist_t *list = NULL;
224 #define PDNS_LOCAL_SOCKPATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-powerdns"
225 static char *local_sockpath = NULL;
227 /* TODO: Do this before 4.4:
229 * - Complete list of known pdns -> collectd mappings.
230 * - Update the collectd.conf(5) manpage.
235 /* <http://doc.powerdns.com/recursor-stats.html> */
236 static void submit (const char *plugin_instance, /* {{{ */
237 const char *pdns_type, const char *value)
239 value_list_t vl = VALUE_LIST_INIT;
242 const char *type = NULL;
243 const char *type_instance = NULL;
244 const data_set_t *ds;
248 for (i = 0; i < lookup_table_length; i++)
249 if (strcmp (lookup_table[i].name, pdns_type) == 0)
252 if (lookup_table[i].type == NULL)
255 if (i >= lookup_table_length)
257 DEBUG ("powerdns plugin: submit: Not found in lookup table: %s = %s;",
262 type = lookup_table[i].type;
263 type_instance = lookup_table[i].type_instance;
265 ds = plugin_get_ds (type);
268 ERROR ("powerdns plugin: The lookup table returned type `%s', "
269 "but I cannot find it via `plugin_get_ds'.",
276 ERROR ("powerdns plugin: type `%s' has %i data sources, "
277 "but I can only handle one.",
282 if (ds->ds[0].type == DS_TYPE_GAUGE)
286 values[0].gauge = strtod (value, &endptr);
290 ERROR ("powerdns plugin: Cannot convert `%s' "
291 "to a floating point number.", value);
299 values[0].counter = strtoll (value, &endptr, 0);
302 ERROR ("powerdns plugin: Cannot convert `%s' "
303 "to an integer number.", value);
310 vl.time = time (NULL);
311 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
312 sstrncpy (vl.plugin, "powerdns", sizeof (vl.plugin));
313 if (type_instance != NULL)
314 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
315 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
317 plugin_dispatch_values (type, &vl);
318 } /* }}} static void submit */
320 static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */
322 size_t *ret_buffer_size)
329 size_t buffer_size = 0;
331 struct sockaddr_un sa_unix;
333 sd = socket (PF_UNIX, item->socktype, 0);
336 FUNC_ERROR ("socket");
340 memset (&sa_unix, 0, sizeof (sa_unix));
341 sa_unix.sun_family = AF_UNIX;
342 strncpy (sa_unix.sun_path,
343 (local_sockpath != NULL) ? local_sockpath : PDNS_LOCAL_SOCKPATH,
344 sizeof (sa_unix.sun_path));
345 sa_unix.sun_path[sizeof (sa_unix.sun_path) - 1] = 0;
347 status = unlink (sa_unix.sun_path);
348 if ((status != 0) && (errno != ENOENT))
350 FUNC_ERROR ("unlink");
357 /* We need to bind to a specific path, because this is a datagram socket
358 * and otherwise the daemon cannot answer. */
359 status = bind (sd, (struct sockaddr *) &sa_unix, sizeof (sa_unix));
366 /* Make the socket writeable by the daemon.. */
367 status = chmod (sa_unix.sun_path, 0666);
370 FUNC_ERROR ("chmod");
374 status = connect (sd, (struct sockaddr *) &item->sockaddr,
375 sizeof (item->sockaddr));
378 FUNC_ERROR ("connect");
382 status = send (sd, item->command, strlen (item->command), 0);
389 status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
399 unlink (sa_unix.sun_path);
404 buffer_size = status + 1;
405 buffer = (char *) malloc (buffer_size);
408 FUNC_ERROR ("malloc");
412 memcpy (buffer, temp, status);
415 *ret_buffer = buffer;
416 *ret_buffer_size = buffer_size;
419 } /* }}} int powerdns_get_data_dgram */
421 static int powerdns_get_data_stream (list_item_t *item, /* {{{ */
423 size_t *ret_buffer_size)
430 size_t buffer_size = 0;
432 sd = socket (PF_UNIX, item->socktype, 0);
435 FUNC_ERROR ("socket");
439 status = connect (sd, (struct sockaddr *) &item->sockaddr,
440 sizeof (item->sockaddr));
443 FUNC_ERROR ("connect");
448 /* strlen + 1, because we need to send the terminating NULL byte, too. */
449 status = send (sd, item->command, strlen (item->command) + 1,
462 status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
468 else if (status == 0)
471 buffer_new = (char *) realloc (buffer, buffer_size + status + 1);
472 if (buffer_new == NULL)
474 FUNC_ERROR ("realloc");
480 memcpy (buffer + buffer_size, temp, status);
481 buffer_size += status;
482 buffer[buffer_size] = 0;
493 assert (status == 0);
494 *ret_buffer = buffer;
495 *ret_buffer_size = buffer_size;
499 } /* }}} int powerdns_get_data_stream */
501 static int powerdns_get_data (list_item_t *item, char **ret_buffer,
502 size_t *ret_buffer_size)
504 if (item->socktype == SOCK_DGRAM)
505 return (powerdns_get_data_dgram (item, ret_buffer, ret_buffer_size));
506 else if (item->socktype == SOCK_STREAM)
507 return (powerdns_get_data_stream (item, ret_buffer, ret_buffer_size));
510 ERROR ("powerdns plugin: Unknown socket type: %i", (int) item->socktype);
513 } /* int powerdns_get_data */
515 static int powerdns_read_server (list_item_t *item) /* {{{ */
518 size_t buffer_size = 0;
527 if (item->command == NULL)
528 item->command = strdup ("SHOW *");
529 if (item->command == NULL)
531 ERROR ("powerdns plugin: strdup failed.");
535 status = powerdns_get_data (item, &buffer, &buffer_size);
539 /* 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, */
542 while ((key = strtok_r (dummy, ",", &saveptr)) != NULL)
548 value = strchr (key, '=');
555 if (value[0] == '\0')
558 /* Check if this item was requested. */
559 for (i = 0; i < item->fields_num; i++)
560 if (strcasecmp (key, item->fields[i]) == 0)
562 if (i >= item->fields_num)
565 submit (item->instance, key, value);
566 } /* while (strtok_r) */
571 } /* }}} int powerdns_read_server */
574 * powerdns_update_recursor_command
576 * Creates a string that holds the command to be sent to the recursor. This
577 * string is stores in the `command' member of the `list_item_t' passed to the
578 * function. This function is called by `powerdns_read_recursor'.
580 static int powerdns_update_recursor_command (list_item_t *li) /* {{{ */
588 strcpy (buffer, "get ");
589 status = strjoin (&buffer[4], sizeof (buffer) - strlen ("get "),
590 li->fields, li->fields_num,
591 /* seperator = */ " ");
594 ERROR ("powerdns plugin: strjoin failed.");
598 li->command = strdup (buffer);
599 if (li->command == NULL)
601 ERROR ("powerdns plugin: strdup failed.");
606 } /* }}} int powerdns_update_recursor_command */
608 static int powerdns_read_recursor (list_item_t *item) /* {{{ */
611 size_t buffer_size = 0;
622 if (item->command == NULL)
624 status = powerdns_update_recursor_command (item);
627 ERROR ("powerdns plugin: powerdns_update_recursor_command failed.");
631 assert (item->command != NULL);
633 status = powerdns_get_data (item, &buffer, &buffer_size);
637 keys_list = strdup (item->command);
638 if (keys_list == NULL)
640 FUNC_ERROR ("strdup");
646 value_saveptr = NULL;
648 /* Skip the `get' at the beginning */
649 strtok_r (keys_list, " \t", &key_saveptr);
652 while ((value = strtok_r (dummy, " \t\n\r", &value_saveptr)) != NULL)
656 key = strtok_r (NULL, " \t", &key_saveptr);
660 submit (item->instance, key, value);
661 } /* while (strtok_r) */
667 } /* }}} int powerdns_read_recursor */
669 static int powerdns_config_add_string (const char *name, /* {{{ */
673 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
675 WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
681 *dest = strdup (ci->values[0].value.string);
686 } /* }}} int powerdns_config_add_string */
688 static int powerdns_config_add_collect (list_item_t *li, /* {{{ */
694 if (ci->values_num < 1)
696 WARNING ("powerdns plugin: The `Collect' option needs "
697 "at least one argument.");
701 for (i = 0; i < ci->values_num; i++)
702 if (ci->values[i].type != OCONFIG_TYPE_STRING)
704 WARNING ("powerdns plugin: Only string arguments are allowed to "
705 "the `Collect' option.");
709 temp = (char **) realloc (li->fields,
710 sizeof (char *) * (li->fields_num + ci->values_num));
713 WARNING ("powerdns plugin: realloc failed.");
718 for (i = 0; i < ci->values_num; i++)
720 li->fields[li->fields_num] = strdup (ci->values[i].value.string);
721 if (li->fields[li->fields_num] == NULL)
723 WARNING ("powerdns plugin: strdup failed.");
729 /* Invalidate a previously computed command */
733 } /* }}} int powerdns_config_add_collect */
735 static int powerdns_config_add_server (oconfig_item_t *ci) /* {{{ */
743 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
745 WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
750 item = (list_item_t *) malloc (sizeof (list_item_t));
753 ERROR ("powerdns plugin: malloc failed.");
756 memset (item, '\0', sizeof (list_item_t));
758 item->instance = strdup (ci->values[0].value.string);
759 if (item->instance == NULL)
761 ERROR ("powerdns plugin: strdup failed.");
767 * Set default values for the members of list_item_t
769 if (strcasecmp ("Server", ci->key) == 0)
771 item->server_type = SRV_AUTHORATIVE;
772 item->func = powerdns_read_server;
773 item->socktype = SOCK_STREAM;
774 socket_temp = strdup (SERVER_SOCKET);
776 else if (strcasecmp ("Recursor", ci->key) == 0)
778 item->server_type = SRV_RECURSOR;
779 item->func = powerdns_read_recursor;
780 item->socktype = SOCK_DGRAM;
781 socket_temp = strdup (RECURSOR_SOCKET);
785 /* We must never get here.. */
791 for (i = 0; i < ci->children_num; i++)
793 oconfig_item_t *option = ci->children + i;
795 if (strcasecmp ("Collect", option->key) == 0)
796 status = powerdns_config_add_collect (item, option);
797 else if (strcasecmp ("Socket", option->key) == 0)
798 status = powerdns_config_add_string ("Socket", &socket_temp, option);
801 ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
813 if (socket_temp == NULL)
815 ERROR ("powerdns plugin: socket_temp == NULL.");
820 if (item->command == NULL)
822 ERROR ("powerdns plugin: item->command == NULL.");
827 item->sockaddr.sun_family = AF_UNIX;
828 sstrncpy (item->sockaddr.sun_path, socket_temp, UNIX_PATH_MAX);
830 e = llentry_create (item->instance, item);
833 ERROR ("powerdns plugin: llentry_create failed.");
837 llist_append (list, e);
848 DEBUG ("powerdns plugin: Add server: instance = %s;", item->instance);
851 } /* }}} int powerdns_config_add_server */
853 static int powerdns_config (oconfig_item_t *ci) /* {{{ */
857 DEBUG ("powerdns plugin: powerdns_config (ci = %p);", (void *) ci);
861 list = llist_create ();
865 ERROR ("powerdns plugin: `llist_create' failed.");
870 for (i = 0; i < ci->children_num; i++)
872 oconfig_item_t *option = ci->children + i;
874 if ((strcasecmp ("Server", option->key) == 0)
875 || (strcasecmp ("Recursor", option->key) == 0))
876 powerdns_config_add_server (option);
877 if (strcasecmp ("LocalSocket", option->key) == 0)
879 char *temp = strdup (option->key);
882 sfree (local_sockpath);
883 local_sockpath = temp;
887 ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
889 } /* for (i = 0; i < ci->children_num; i++) */
892 } /* }}} int powerdns_config */
894 static int powerdns_read (void)
898 for (e = llist_head (list); e != NULL; e = e->next)
900 list_item_t *item = e->value;
905 } /* static int powerdns_read */
907 static int powerdns_shutdown (void)
914 for (e = llist_head (list); e != NULL; e = e->next)
916 list_item_t *item = (list_item_t *) e->value;
919 sfree (item->instance);
920 sfree (item->command);
924 llist_destroy (list);
928 } /* static int powerdns_shutdown */
930 void module_register (void)
932 plugin_register_complex_config ("powerdns", powerdns_config);
933 plugin_register_read ("powerdns", powerdns_read);
934 plugin_register_shutdown ("powerdns", powerdns_shutdown );
935 } /* void module_register */
937 /* vim: set sw=2 sts=2 ts=8 fdm=marker : */