6a28856f69f0f17ede5d5de5589bfa6592329304
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>
43 #ifndef UNIX_PATH_MAX
44 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
45 #endif
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" /* }}} */
63 struct list_item_s;
64 typedef struct list_item_s list_item_t;
66 struct list_item_s
67 {
68 int (*func) (list_item_t *item);
69 char *instance;
70 char *command;
71 struct sockaddr_un sockaddr;
72 int socktype;
73 };
75 struct statname_lookup_s
76 {
77 char *name;
78 char *type;
79 char *type_instance;
80 };
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
120 }}} */
122 statname_lookup_t lookup_table[] = /* {{{ */
123 {
124 /*
125 * Recursor statistics
126 */
127 /*
128 * corrupt-packets
129 * deferred-cache-inserts
130 * deferred-cache-lookup
131 * qsize-q
132 * servfail-packets
133 * timedout-packets
134 * udp4-answers
135 * udp4-queries
136 * udp6-answers
137 * udp6-queries
138 */
139 /* Questions */
140 {"recursing-questions", "dns_question", "recurse"},
141 {"tcp-queries", "dns_question", "tcp"},
142 {"udp-queries", "dns_question", "udp"},
144 /* Answers */
145 {"recursing-answers", "dns_answer", "recurse"},
146 {"tcp-answers", "dns_answer", "tcp"},
147 {"udp-answers", "dns_answer", "udp"},
149 /* Cache stuff */
150 {"packetcache-hit", "cache_result", "packet-hit"},
151 {"packetcache-miss", "cache_result", "packet-miss"},
152 {"packetcache-size", "cache_size", "packet"},
153 {"query-cache-hit", "cache_result", "query-hit"},
154 {"query-cache-miss", "cache_result", "query-miss"},
156 /* Latency */
157 {"latency", "latency", NULL},
159 /*
160 * Recursor statistics
161 */
162 /* Answers by return code */
163 {"noerror-answers", "dns_rcode", "NOERROR"},
164 {"nxdomain-answers", "dns_rcode", "NXDOMAIN"},
165 {"servfail-answers", "dns_rcode", "SERVFAIL"},
167 /* CPU utilization */
168 {"sys-msec", "cpu", "system"},
169 {"user-msec", "cpu", "user"},
171 /* Question-to-answer latency */
172 {"qa-latency", "latency", NULL},
174 /* Cache */
175 {"cache-entries", "cache_size", NULL},
176 {"cache-hits", "cache_result", "hit"},
177 {"cache-misses", "cache_result", "miss"},
179 /* Total number of questions.. */
180 {"questions", "dns_qtype", "total"}
181 }; /* }}} */
182 int lookup_table_length = STATIC_ARRAY_SIZE (lookup_table);
184 static llist_t *list = NULL;
186 #define PDNS_LOCAL_SOCKPATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-powerdns"
187 static char *local_sockpath = NULL;
189 /* <http://doc.powerdns.com/recursor-stats.html> */
190 static void submit (const char *plugin_instance, /* {{{ */
191 const char *pdns_type, const char *value)
192 {
193 value_list_t vl = VALUE_LIST_INIT;
194 value_t values[1];
196 const char *type = NULL;
197 const char *type_instance = NULL;
198 const data_set_t *ds;
200 int i;
202 for (i = 0; i < lookup_table_length; i++)
203 if (strcmp (lookup_table[i].name, pdns_type) == 0)
204 break;
206 if (lookup_table[i].type == NULL)
207 return;
209 if (i >= lookup_table_length)
210 {
211 DEBUG ("powerdns plugin: submit: Not found in lookup table: %s = %s;",
212 pdns_type, value);
213 return;
214 }
216 type = lookup_table[i].type;
217 type_instance = lookup_table[i].type_instance;
219 ds = plugin_get_ds (type);
220 if (ds == NULL)
221 {
222 ERROR ("powerdns plugin: The lookup table returned type `%s', "
223 "but I cannot find it via `plugin_get_ds'.",
224 type);
225 return;
226 }
228 if (ds->ds_num != 1)
229 {
230 ERROR ("powerdns plugin: type `%s' has %i data sources, "
231 "but I can only handle one.",
232 type, ds->ds_num);
233 return;
234 }
236 if (ds->ds[0].type == DS_TYPE_GAUGE)
237 {
238 char *endptr = NULL;
240 values[0].gauge = strtod (value, &endptr);
242 if (endptr == value)
243 {
244 ERROR ("powerdns plugin: Cannot convert `%s' "
245 "to a floating point number.", value);
246 return;
247 }
248 }
249 else
250 {
251 char *endptr = NULL;
253 values[0].counter = strtoll (value, &endptr, 0);
254 if (endptr == value)
255 {
256 ERROR ("powerdns plugin: Cannot convert `%s' "
257 "to an integer number.", value);
258 return;
259 }
260 }
262 vl.values = values;
263 vl.values_len = 1;
264 vl.time = time (NULL);
265 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
266 sstrncpy (vl.plugin, "powerdns", sizeof (vl.plugin));
267 sstrncpy (vl.type, type, sizeof (vl.type));
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 (&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 sstrncpy (sa_unix.sun_path,
298 (local_sockpath != NULL) ? local_sockpath : PDNS_LOCAL_SOCKPATH,
299 sizeof (sa_unix.sun_path));
301 status = unlink (sa_unix.sun_path);
302 if ((status != 0) && (errno != ENOENT))
303 {
304 FUNC_ERROR ("unlink");
305 close (sd);
306 return (-1);
307 }
309 do /* while (0) */
310 {
311 /* We need to bind to a specific path, because this is a datagram socket
312 * and otherwise the daemon cannot answer. */
313 status = bind (sd, (struct sockaddr *) &sa_unix, sizeof (sa_unix));
314 if (status != 0)
315 {
316 FUNC_ERROR ("bind");
317 break;
318 }
320 /* Make the socket writeable by the daemon.. */
321 status = chmod (sa_unix.sun_path, 0666);
322 if (status != 0)
323 {
324 FUNC_ERROR ("chmod");
325 break;
326 }
328 status = connect (sd, (struct sockaddr *) &item->sockaddr,
329 sizeof (item->sockaddr));
330 if (status != 0)
331 {
332 FUNC_ERROR ("connect");
333 break;
334 }
336 status = send (sd, item->command, strlen (item->command), 0);
337 if (status < 0)
338 {
339 FUNC_ERROR ("send");
340 break;
341 }
343 status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
344 if (status < 0)
345 {
346 FUNC_ERROR ("recv");
347 break;
348 }
349 status = 0;
350 } while (0);
352 close (sd);
353 unlink (sa_unix.sun_path);
355 if (status != 0)
356 return (-1);
358 buffer_size = status + 1;
359 buffer = (char *) malloc (buffer_size);
360 if (buffer == NULL)
361 {
362 FUNC_ERROR ("malloc");
363 return (-1);
364 }
366 memcpy (buffer, temp, status);
367 buffer[status] = 0;
369 *ret_buffer = buffer;
370 *ret_buffer_size = buffer_size;
372 return (0);
373 } /* }}} int powerdns_get_data_dgram */
375 static int powerdns_get_data_stream (list_item_t *item, /* {{{ */
376 char **ret_buffer,
377 size_t *ret_buffer_size)
378 {
379 int sd;
380 int status;
382 char temp[4096];
383 char *buffer = NULL;
384 size_t buffer_size = 0;
386 sd = socket (PF_UNIX, item->socktype, 0);
387 if (sd < 0)
388 {
389 FUNC_ERROR ("socket");
390 return (-1);
391 }
393 status = connect (sd, (struct sockaddr *) &item->sockaddr,
394 sizeof (item->sockaddr));
395 if (status != 0)
396 {
397 FUNC_ERROR ("connect");
398 close (sd);
399 return (-1);
400 }
402 /* strlen + 1, because we need to send the terminating NULL byte, too. */
403 status = send (sd, item->command, strlen (item->command) + 1,
404 /* flags = */ 0);
405 if (status < 0)
406 {
407 FUNC_ERROR ("send");
408 close (sd);
409 return (-1);
410 }
412 while (42)
413 {
414 char *buffer_new;
416 status = recv (sd, temp, sizeof (temp), /* flags = */ 0);
417 if (status < 0)
418 {
419 FUNC_ERROR ("recv");
420 break;
421 }
422 else if (status == 0)
423 break;
425 buffer_new = (char *) realloc (buffer, buffer_size + status + 1);
426 if (buffer_new == NULL)
427 {
428 FUNC_ERROR ("realloc");
429 status = -1;
430 break;
431 }
432 buffer = buffer_new;
434 memcpy (buffer + buffer_size, temp, status);
435 buffer_size += status;
436 buffer[buffer_size] = 0;
437 } /* while (42) */
438 close (sd);
439 sd = -1;
441 if (status < 0)
442 {
443 sfree (buffer);
444 }
445 else
446 {
447 assert (status == 0);
448 *ret_buffer = buffer;
449 *ret_buffer_size = buffer_size;
450 }
452 return (status);
453 } /* }}} int powerdns_get_data_stream */
455 static int powerdns_get_data (list_item_t *item, char **ret_buffer,
456 size_t *ret_buffer_size)
457 {
458 if (item->socktype == SOCK_DGRAM)
459 return (powerdns_get_data_dgram (item, ret_buffer, ret_buffer_size));
460 else if (item->socktype == SOCK_STREAM)
461 return (powerdns_get_data_stream (item, ret_buffer, ret_buffer_size));
462 else
463 {
464 ERROR ("powerdns plugin: Unknown socket type: %i", (int) item->socktype);
465 return (-1);
466 }
467 } /* int powerdns_get_data */
469 static int powerdns_read_server (list_item_t *item) /* {{{ */
470 {
471 char *buffer = NULL;
472 size_t buffer_size = 0;
473 int status;
475 char *dummy;
476 char *saveptr;
478 char *key;
479 char *value;
481 status = powerdns_get_data (item, &buffer, &buffer_size);
482 if (status != 0)
483 return (-1);
485 /* 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, */
486 dummy = buffer;
487 saveptr = NULL;
488 while ((key = strtok_r (dummy, ",", &saveptr)) != NULL)
489 {
490 dummy = NULL;
492 value = strchr (key, '=');
493 if (value == NULL)
494 break;
496 *value = '\0';
497 value++;
499 if (value[0] == '\0')
500 continue;
502 submit (item->instance, key, value);
503 } /* while (strtok_r) */
505 sfree (buffer);
507 return (0);
508 } /* }}} int powerdns_read_server */
510 static int powerdns_read_recursor (list_item_t *item) /* {{{ */
511 {
512 char *buffer = NULL;
513 size_t buffer_size = 0;
514 int status;
516 char *dummy;
518 char *keys_list;
519 char *key;
520 char *key_saveptr;
521 char *value;
522 char *value_saveptr;
524 status = powerdns_get_data (item, &buffer, &buffer_size);
525 if (status != 0)
526 return (-1);
528 keys_list = strdup (item->command);
529 if (keys_list == NULL)
530 {
531 FUNC_ERROR ("strdup");
532 sfree (buffer);
533 return (-1);
534 }
536 key_saveptr = NULL;
537 value_saveptr = NULL;
539 /* Skip the `get' at the beginning */
540 strtok_r (keys_list, " \t", &key_saveptr);
542 dummy = buffer;
543 while ((value = strtok_r (dummy, " \t\n\r", &value_saveptr)) != NULL)
544 {
545 dummy = NULL;
547 key = strtok_r (NULL, " \t", &key_saveptr);
548 if (key == NULL)
549 break;
551 submit (item->instance, key, value);
552 } /* while (strtok_r) */
554 sfree (buffer);
555 sfree (keys_list);
557 return (0);
558 } /* }}} int powerdns_read_recursor */
560 static int powerdns_config_add_string (const char *name, /* {{{ */
561 char **dest,
562 oconfig_item_t *ci)
563 {
564 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
565 {
566 WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
567 name);
568 return (-1);
569 }
571 sfree (*dest);
572 *dest = strdup (ci->values[0].value.string);
573 if (*dest == NULL)
574 return (-1);
576 return (0);
577 } /* }}} int ctail_config_add_string */
579 static int powerdns_config_add_server (oconfig_item_t *ci) /* {{{ */
580 {
581 char *socket_temp;
583 list_item_t *item;
584 int status;
585 int i;
587 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
588 {
589 WARNING ("powerdns plugin: `%s' needs exactly one string argument.",
590 ci->key);
591 return (-1);
592 }
594 item = (list_item_t *) malloc (sizeof (list_item_t));
595 if (item == NULL)
596 {
597 ERROR ("powerdns plugin: malloc failed.");
598 return (-1);
599 }
600 memset (item, '\0', sizeof (list_item_t));
602 item->instance = strdup (ci->values[0].value.string);
603 if (item->instance == NULL)
604 {
605 ERROR ("powerdns plugin: strdup failed.");
606 sfree (item);
607 return (-1);
608 }
610 /*
611 * Set default values for the members of list_item_t
612 */
613 if (strcasecmp ("Server", ci->key) == 0)
614 {
615 item->func = powerdns_read_server;
616 item->command = strdup (SERVER_COMMAND);
617 item->socktype = SOCK_STREAM;
618 socket_temp = strdup (SERVER_SOCKET);
619 }
620 else if (strcasecmp ("Recursor", ci->key) == 0)
621 {
622 item->func = powerdns_read_recursor;
623 item->command = strdup (RECURSOR_COMMAND);
624 item->socktype = SOCK_DGRAM;
625 socket_temp = strdup (RECURSOR_SOCKET);
626 }
628 status = 0;
629 for (i = 0; i < ci->children_num; i++)
630 {
631 oconfig_item_t *option = ci->children + i;
633 if (strcasecmp ("Command", option->key) == 0)
634 status = powerdns_config_add_string ("Command", &item->command, option);
635 else if (strcasecmp ("Socket", option->key) == 0)
636 status = powerdns_config_add_string ("Socket", &socket_temp, option);
637 else
638 {
639 ERROR ("powerdns plugin: Option `%s' not allowed here.", option->key);
640 status = -1;
641 }
643 if (status != 0)
644 break;
645 }
647 while (status == 0)
648 {
649 llentry_t *e;
651 if (socket_temp == NULL)
652 {
653 ERROR ("powerdns plugin: socket_temp == NULL.");
654 status = -1;
655 break;
656 }
658 if (item->command == NULL)
659 {
660 ERROR ("powerdns plugin: item->command == NULL.");
661 status = -1;
662 break;
663 }
665 item->sockaddr.sun_family = AF_UNIX;
666 sstrncpy (item->sockaddr.sun_path, socket_temp,
667 sizeof (item->sockaddr.sun_path));
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 : */