1 /**
2 * collectd - src/powerdns.c
3 * Copyright (C) 2007-2008 C-Ware, Inc.
4 * Copyright (C) 2008 Florian Forster
5 *
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.
9 *
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.
14 *
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
18 *
19 * Author:
20 * Luke Heberling <lukeh at c-ware.com>
21 * Florian Forster <octo at verplant.org>
22 *
23 * DESCRIPTION
24 * Queries a PowerDNS control socket for statistics
25 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "utils_llist.h"
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/un.h>
42 #include <malloc.h>
44 #ifndef UNIX_PATH_MAX
45 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
46 #endif
47 #define FUNC_ERROR(func) do { char errbuf[1024]; ERROR ("powerdns plugin: %s failed: %s", func, sstrerror (errno, errbuf, sizeof (errbuf))); } while (0)
49 #define SERVER_SOCKET "/var/run/pdns.controlsocket"
50 #define SERVER_COMMAND "SHOW *"
52 #define RECURSOR_SOCKET "/var/run/pdns_recursor.controlsocket"
53 #define RECURSOR_COMMAND "get all-outqueries answers0-1 " /* {{{ */ \
54 "answers100-1000 answers10-100 answers1-10 answers-slow cache-entries " \
55 "cache-hits cache-misses chain-resends client-parse-errors " \
56 "concurrent-queries dlg-only-drops ipv6-outqueries negcache-entries " \
57 "noerror-answers nsset-invalidations nsspeeds-entries nxdomain-answers " \
58 "outgoing-timeouts qa-latency questions resource-limits " \
59 "server-parse-errors servfail-answers spoof-prevents sys-msec " \
60 "tcp-client-overflow tcp-outqueries tcp-questions throttled-out " \
61 "throttled-outqueries throttle-entries unauthorized-tcp unauthorized-udp " \
62 "unexpected-packets unreachables user-msec" /* }}} */
64 struct list_item_s;
65 typedef struct list_item_s list_item_t;
67 struct list_item_s
68 {
69 int (*func) (list_item_t *item);
70 char *instance;
71 char *command;
72 struct sockaddr_un sockaddr;
73 int socktype;
74 };
76 struct statname_lookup_s
77 {
78 char *name;
79 char *type;
80 char *type_instance;
81 };
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 milisecond
87 answers100-1000 counts the number of queries answered within 1 second
88 answers10-100 counts the number of queries answered within 100 miliseconds
89 answers1-10 counts the number of queries answered within 10 miliseconds
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
121 }}} */
123 statname_lookup_t lookup_table[] = /* {{{ */
124 {
125 /*
126 * Recursor statistics
127 */
128 /*
129 * corrupt-packets
130 * deferred-cache-inserts
131 * deferred-cache-lookup
132 * qsize-q
133 * servfail-packets
134 * timedout-packets
135 * udp4-answers
136 * udp4-queries
137 * udp6-answers
138 * udp6-queries
139 */
140 /* Questions */
141 {"recursing-questions", "dns_question", "recurse"},
142 {"tcp-queries", "dns_question", "tcp"},
143 {"udp-queries", "dns_question", "udp"},
145 /* Answers */
146 {"recursing-answers", "dns_answer", "recurse"},
147 {"tcp-answers", "dns_answer", "tcp"},
148 {"udp-answers", "dns_answer", "udp"},
150 /* Cache stuff */
151 {"packetcache-hit", "cache_result", "packet-hit"},
152 {"packetcache-miss", "cache_result", "packet-miss"},
153 {"packetcache-size", "cache_size", "packet"},
154 {"query-cache-hit", "cache_result", "query-hit"},
155 {"query-cache-miss", "cache_result", "query-miss"},
157 /* Latency */
158 {"latency", "latency", NULL},
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},
175 /* Cache */
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"}
182 }; /* }}} */
183 int lookup_table_length = STATIC_ARRAY_SIZE (lookup_table);
185 static llist_t *list = NULL;
187 #define PDNS_LOCAL_SOCKPATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-powerdns"
188 static char *local_sockpath = NULL;
190 /* <http://doc.powerdns.com/recursor-stats.html> */
191 static void submit (const char *plugin_instance, /* {{{ */
192 const char *pdns_type, const char *value)
193 {
194 value_list_t vl = VALUE_LIST_INIT;
195 value_t values[1];
197 const char *type = NULL;
198 const char *type_instance = NULL;
199 const data_set_t *ds;
201 int i;
203 for (i = 0; i < lookup_table_length; i++)
204 if (strcmp (lookup_table[i].name, pdns_type) == 0)
205 break;
207 if (lookup_table[i].type == NULL)
208 return;
210 if (i >= lookup_table_length)
211 {
212 DEBUG ("powerdns plugin: submit: Not found in lookup table: %s = %s;",
213 pdns_type, value);
214 return;
215 }
217 type = lookup_table[i].type;
218 type_instance = lookup_table[i].type_instance;
220 ds = plugin_get_ds (type);
221 if (ds == NULL)
222 {
223 ERROR ("powerdns plugin: The lookup table returned type `%s', "
224 "but I cannot find it via `plugin_get_ds'.",
225 type);
226 return;
227 }
229 if (ds->ds_num != 1)
230 {
231 ERROR ("powerdns plugin: type `%s' has %i data sources, "
232 "but I can only handle one.",
233 type, ds->ds_num);
234 return;
235 }
237 if (ds->ds[0].type == DS_TYPE_GAUGE)
238 {
239 char *endptr = NULL;
241 values[0].gauge = strtod (value, &endptr);
243 if (endptr == value)
244 {
245 ERROR ("powerdns plugin: Cannot convert `%s' "
246 "to a floating point number.", value);
247 return;
248 }
249 }
250 else
251 {
252 char *endptr = NULL;
254 values[0].counter = strtoll (value, &endptr, 0);
255 if (endptr == value)
256 {
257 ERROR ("powerdns plugin: Cannot convert `%s' "
258 "to an integer number.", value);
259 return;
260 }
261 }
263 vl.values = values;
264 vl.values_len = 1;
265 vl.time = time (NULL);
266 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
267 sstrncpy (vl.plugin, "powerdns", sizeof (vl.plugin));
268 if (type_instance != NULL)
269 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
270 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
272 plugin_dispatch_values (type, &vl);
273 } /* }}} static void submit */
275 static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */
276 char **ret_buffer,
277 size_t *ret_buffer_size)
278 {
279 int sd;
280 int status;
282 char temp[4096];
283 char *buffer = NULL;
284 size_t buffer_size = 0;
286 struct sockaddr_un sa_unix;
288 sd = socket (PF_UNIX, item->socktype, 0);
289 if (sd < 0)
290 {
291 FUNC_ERROR ("socket");
292 return (-1);
293 }
295 memset (&sa_unix, 0, sizeof (sa_unix));
296 sa_unix.sun_family = AF_UNIX;
297 strncpy (sa_unix.sun_path,
298 (local_sockpath != NULL) ? local_sockpath : PDNS_LOCAL_SOCKPATH,
299 sizeof (sa_unix.sun_path));
300 sa_unix.sun_path[sizeof (sa_unix.sun_path) - 1] = 0;
302 status = unlink (sa_unix.sun_path);
303 if ((status != 0) && (errno != ENOENT))
304 {
305 FUNC_ERROR ("unlink");
306 close (sd);
307 return (-1);
308 }
310 do /* while (0) */
311 {
312 /* We need to bind to a specific path, because this is a datagram socket
313 * and otherwise the daemon cannot answer. */
314 status = bind (sd, (struct sockaddr *) &sa_unix, sizeof (sa_unix));
315 if (status != 0)
316 {
317 FUNC_ERROR ("bind");
318 break;
319 }
321 /* Make the socket writeable by the daemon.. */
322 status = chmod (sa_unix.sun_path, 0666);
323 if (status != 0)
324 {
325 FUNC_ERROR ("chmod");
326 break;
327 }
329 status = connect (sd, (struct sockaddr *) &item->sockaddr,
330 sizeof (item->sockaddr));
331 if (status != 0)
332 {
333 FUNC_ERROR ("connect");
334 break;
335 }
337 status = send (sd, item->command, strlen (item->command), 0);
338 if (status < 0)
339 {
340 FUNC_ERROR ("send");
341 break;
342 }
344 status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
345 if (status < 0)
346 {
347 FUNC_ERROR ("recv");
348 break;
349 }
350 status = 0;
351 } while (0);
353 close (sd);
354 unlink (sa_unix.sun_path);
356 if (status != 0)
357 return (-1);
359 buffer_size = status + 1;
360 buffer = (char *) malloc (buffer_size);
361 if (buffer == NULL)
362 {
363 FUNC_ERROR ("malloc");
364 return (-1);
365 }
367 memcpy (buffer, temp, status);
368 buffer[status] = 0;
370 *ret_buffer = buffer;
371 *ret_buffer_size = buffer_size;
373 return (0);
374 } /* }}} int powerdns_get_data_dgram */
376 static int powerdns_get_data_stream (list_item_t *item, /* {{{ */
377 char **ret_buffer,
378 size_t *ret_buffer_size)
379 {
380 int sd;
381 int status;
383 char temp[4096];
384 char *buffer = NULL;
385 size_t buffer_size = 0;
387 sd = socket (PF_UNIX, item->socktype, 0);
388 if (sd < 0)
389 {
390 FUNC_ERROR ("socket");
391 return (-1);
392 }
394 status = connect (sd, (struct sockaddr *) &item->sockaddr,
395 sizeof (item->sockaddr));
396 if (status != 0)
397 {
398 FUNC_ERROR ("connect");
399 close (sd);
400 return (-1);
401 }
403 /* strlen + 1, because we need to send the terminating NULL byte, too. */
404 status = send (sd, item->command, strlen (item->command) + 1,
405 /* flags = */ 0);
406 if (status < 0)
407 {
408 FUNC_ERROR ("send");
409 close (sd);
410 return (-1);
411 }
413 while (42)
414 {
415 char *buffer_new;
417 status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
418 if (status < 0)
419 {
420 FUNC_ERROR ("recv");
421 break;
422 }
423 else if (status == 0)
424 break;
426 buffer_new = (char *) realloc (buffer, buffer_size + status + 1);
427 if (buffer_new == NULL)
428 {
429 FUNC_ERROR ("realloc");
430 status = -1;
431 break;
432 }
433 buffer = buffer_new;
435 memcpy (buffer + buffer_size, temp, status);
436 buffer_size += status;
437 buffer[buffer_size] = 0;
438 } /* while (42) */
439 close (sd);
440 sd = -1;
442 if (status < 0)
443 {
444 sfree (buffer);
445 }
446 else
447 {
448 assert (status == 0);
449 *ret_buffer = buffer;
450 *ret_buffer_size = buffer_size;
451 }
453 return (status);
454 } /* }}} int powerdns_get_data_stream */
456 static int powerdns_get_data (list_item_t *item, char **ret_buffer,
457 size_t *ret_buffer_size)
458 {
459 if (item->socktype == SOCK_DGRAM)
460 return (powerdns_get_data_dgram (item, ret_buffer, ret_buffer_size));
461 else if (item->socktype == SOCK_STREAM)
462 return (powerdns_get_data_stream (item, ret_buffer, ret_buffer_size));
463 else
464 {
465 ERROR ("powerdns plugin: Unknown socket type: %i", (int) item->socktype);
466 return (-1);
467 }
468 } /* int powerdns_get_data */
470 static int powerdns_read_server (list_item_t *item) /* {{{ */
471 {
472 char *buffer = NULL;
473 size_t buffer_size = 0;
474 int status;
476 char *dummy;
477 char *saveptr;
479 char *key;
480 char *value;
482 status = powerdns_get_data (item, &buffer, &buffer_size);
483 if (status != 0)
484 return (-1);
486 /* 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, */
487 dummy = buffer;
488 saveptr = NULL;
489 while ((key = strtok_r (dummy, ",", &saveptr)) != NULL)
490 {
491 dummy = NULL;
493 value = strchr (key, '=');
494 if (value == NULL)
495 break;
497 *value = '\0';
498 value++;
500 if (value[0] == '\0')
501 continue;
503 submit (item->instance, key, value);
504 } /* while (strtok_r) */
506 sfree (buffer);
508 return (0);
509 } /* }}} int powerdns_read_server */
511 static int powerdns_read_recursor (list_item_t *item) /* {{{ */
512 {
513 char *buffer = NULL;
514 size_t buffer_size = 0;
515 int status;
517 char *dummy;
519 char *keys_list;
520 char *key;
521 char *key_saveptr;
522 char *value;
523 char *value_saveptr;
525 status = powerdns_get_data (item, &buffer, &buffer_size);
526 if (status != 0)
527 return (-1);
529 keys_list = strdup (item->command);
530 if (keys_list == NULL)
531 {
532 FUNC_ERROR ("strdup");
533 sfree (buffer);
534 return (-1);
535 }
537 key_saveptr = NULL;
538 value_saveptr = NULL;
540 /* Skip the `get' at the beginning */
541 strtok_r (keys_list, " \t", &key_saveptr);
543 dummy = buffer;
544 while ((value = strtok_r (dummy, " \t\n\r", &value_saveptr)) != NULL)
545 {
546 dummy = NULL;
548 key = strtok_r (NULL, " \t", &key_saveptr);
549 if (key == NULL)
550 break;
552 submit (item->instance, key, value);
553 } /* while (strtok_r) */
555 sfree (buffer);
556 sfree (keys_list);
558 return (0);
559 } /* }}} int powerdns_read_recursor */
561 static int powerdns_config_add_string (const char *name, /* {{{ */
562 char **dest,
563 oconfig_item_t *ci)
564 {
565 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
566 {
567 WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
568 name);
569 return (-1);
570 }
572 sfree (*dest);
573 *dest = strdup (ci->values[0].value.string);
574 if (*dest == NULL)
575 return (-1);
577 return (0);
578 } /* }}} int ctail_config_add_string */
580 static int powerdns_config_add_server (oconfig_item_t *ci) /* {{{ */
581 {
582 char *socket_temp;
584 list_item_t *item;
585 int status;
586 int i;
588 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
589 {
590 WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
591 ci->key);
592 return (-1);
593 }
595 item = (list_item_t *) malloc (sizeof (list_item_t));
596 if (item == NULL)
597 {
598 ERROR ("powerdns plugin: malloc failed.");
599 return (-1);
600 }
601 memset (item, '\0', sizeof (list_item_t));
603 item->instance = strdup (ci->values[0].value.string);
604 if (item->instance == NULL)
605 {
606 ERROR ("powerdns plugin: strdup failed.");
607 sfree (item);
608 return (-1);
609 }
611 /*
612 * Set default values for the members of list_item_t
613 */
614 if (strcasecmp ("Server", ci->key) == 0)
615 {
616 item->func = powerdns_read_server;
617 item->command = strdup (SERVER_COMMAND);
618 item->socktype = SOCK_STREAM;
619 socket_temp = strdup (SERVER_SOCKET);
620 }
621 else if (strcasecmp ("Recursor", ci->key) == 0)
622 {
623 item->func = powerdns_read_recursor;
624 item->command = strdup (RECURSOR_COMMAND);
625 item->socktype = SOCK_DGRAM;
626 socket_temp = strdup (RECURSOR_SOCKET);
627 }
629 status = 0;
630 for (i = 0; i < ci->children_num; i++)
631 {
632 oconfig_item_t *option = ci->children + i;
634 if (strcasecmp ("Command", option->key) == 0)
635 status = powerdns_config_add_string ("Command", &item->command, option);
636 else if (strcasecmp ("Socket", option->key) == 0)
637 status = powerdns_config_add_string ("Socket", &socket_temp, option);
638 else
639 {
640 ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
641 status = -1;
642 }
644 if (status != 0)
645 break;
646 }
648 while (status == 0)
649 {
650 llentry_t *e;
652 if (socket_temp == NULL)
653 {
654 ERROR ("powerdns plugin: socket_temp == NULL.");
655 status = -1;
656 break;
657 }
659 if (item->command == NULL)
660 {
661 ERROR ("powerdns plugin: item->command == NULL.");
662 status = -1;
663 break;
664 }
666 item->sockaddr.sun_family = AF_UNIX;
667 sstrncpy (item->sockaddr.sun_path, socket_temp, UNIX_PATH_MAX);
669 e = llentry_create (item->instance, item);
670 if (e == NULL)
671 {
672 ERROR ("powerdns plugin: llentry_create failed.");
673 status = -1;
674 break;
675 }
676 llist_append (list, e);
678 break;
679 }
681 if (status != 0)
682 {
683 sfree (item);
684 return (-1);
685 }
687 DEBUG ("powerdns plugin: Add server: instance = %s;", item->instance);
689 return (0);
690 } /* }}} int powerdns_config_add_server */
692 static int powerdns_config (oconfig_item_t *ci) /* {{{ */
693 {
694 int i;
696 DEBUG ("powerdns plugin: powerdns_config (ci = %p);", (void *) ci);
698 if (list == NULL)
699 {
700 list = llist_create ();
702 if (list == NULL)
703 {
704 ERROR ("powerdns plugin: `llist_create' failed.");
705 return (-1);
706 }
707 }
709 for (i = 0; i < ci->children_num; i++)
710 {
711 oconfig_item_t *option = ci->children + i;
713 if ((strcasecmp ("Server", option->key) == 0)
714 || (strcasecmp ("Recursor", option->key) == 0))
715 powerdns_config_add_server (option);
716 if (strcasecmp ("LocalSocket", option->key) == 0)
717 {
718 char *temp = strdup (option->key);
719 if (temp == NULL)
720 return (1);
721 sfree (local_sockpath);
722 local_sockpath = temp;
723 }
724 else
725 {
726 ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
727 }
728 } /* for (i = 0; i < ci->children_num; i++) */
730 return (0);
731 } /* }}} int powerdns_config */
733 static int powerdns_read (void)
734 {
735 llentry_t *e;
737 for (e = llist_head (list); e != NULL; e = e->next)
738 {
739 list_item_t *item = e->value;
740 item->func (item);
741 }
743 return (0);
744 } /* static int powerdns_read */
746 static int powerdns_shutdown (void)
747 {
748 llentry_t *e;
750 if (list == NULL)
751 return (0);
753 for (e = llist_head (list); e != NULL; e = e->next)
754 {
755 list_item_t *item = (list_item_t *) e->value;
756 e->value = NULL;
758 sfree (item->instance);
759 sfree (item->command);
760 sfree (item);
761 }
763 llist_destroy (list);
764 list = NULL;
766 return (0);
767 } /* static int powerdns_shutdown */
769 void module_register (void)
770 {
771 plugin_register_complex_config ("powerdns", powerdns_config);
772 plugin_register_read ("powerdns", powerdns_read);
773 plugin_register_shutdown ("powerdns", powerdns_shutdown );
774 } /* void module_register */
776 /* vim: set sw=2 sts=2 ts=8 fdm=marker : */