3 * Copyright (C) 2006 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
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 * Florian octo Forster <octo at verplant.org>
26 #include "configfile.h"
27 #include "utils_debug.h"
30 # include <sys/poll.h>
33 #define MODULE_NAME "dns"
36 # define NAMED_HAVE_CONFIG 1
38 # define NAMED_HAVE_CONFIG 0
42 # include "utils_dns.h"
43 # define NAMED_HAVE_READ 1
45 # define NAMED_HAVE_READ 0
52 struct counter_list_s *next;
54 typedef struct counter_list_s counter_list_t;
56 static char *qtype_file = "dns/qtype-%s.rrd";
57 static char *opcode_file = "dns/opcode-%s.rrd";
58 static char *rcode_file = "dns/rcode-%s.rrd";
60 static char *qtype_ds_def[] =
62 "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:65535",
65 static int qtype_ds_num = 1;
67 static char *opcode_ds_def[] =
69 "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:65535",
72 static int opcode_ds_num = 1;
74 static char *rcode_ds_def[] =
76 "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:65535",
79 static int rcode_ds_num = 1;
83 static char *config_keys[] =
89 static int config_keys_num = 2;
90 #endif /* HAVE_LIBPCAP */
91 #endif /* NAMED_HAVE_CONFIG */
94 #define PCAP_SNAPLEN 1460
95 static char *pcap_device = NULL;
96 static int pipe_fd = -1;
98 static unsigned int tr_queries;
99 static unsigned int tr_responses;
100 static counter_list_t *qtype_list;
101 static counter_list_t *opcode_list;
102 static counter_list_t *rcode_list;
105 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
107 counter_list_t *entry;
109 DBG ("counter_list_search (list = %p, key = %u)",
110 (void *) *list, key);
112 for (entry = *list; entry != NULL; entry = entry->next)
113 if (entry->key == key)
116 DBG ("return (%p)", (void *) entry);
120 static counter_list_t *counter_list_create (counter_list_t **list,
121 unsigned int key, unsigned int value)
123 counter_list_t *entry;
125 DBG ("counter_list_create (list = %p, key = %u, value = %u)",
126 (void *) *list, key, value);
128 entry = (counter_list_t *) malloc (sizeof (counter_list_t));
132 memset (entry, 0, sizeof (counter_list_t));
134 entry->value = value;
142 counter_list_t *last;
145 while (last->next != NULL)
151 DBG ("return (%p)", (void *) entry);
155 static void counter_list_add (counter_list_t **list,
156 unsigned int key, unsigned int increment)
158 counter_list_t *entry;
160 DBG ("counter_list_add (list = %p, key = %u, increment = %u)",
161 (void *) *list, key, increment);
163 entry = counter_list_search (list, key);
167 entry->value += increment;
171 counter_list_create (list, key, increment);
176 static int counter_list_send (counter_list_t *list, int fd)
179 unsigned int values[2 * T_MAX];
180 unsigned int values_num;
188 (cl != NULL) && (values_num < T_MAX);
191 values[2 * values_num] = cl->key;
192 values[(2 * values_num) + 1] = cl->value;
196 DBG ("swrite (fd = %i, values_num = %i)", fd, values_num);
197 if (swrite (fd, (const void *) &values_num, sizeof (values_num)) != 0)
199 DBG ("Writing to fd failed: %s", strerror (errno));
200 syslog (LOG_ERR, "dns plugin: Writing to fd failed: %s",
208 DBG ("swrite (fd = %i, values = %p, size = %i)",
209 fd, (void *) values, (int) (sizeof (int) * values_num));
210 if (swrite (fd, (const void *) values, 2 * sizeof (int) * values_num) != 0)
212 DBG ("Writing to pipe failed: %s", strerror (errno));
213 syslog (LOG_ERR, "dns plugin: Writing to pipe failed: %s",
220 #if NAMED_HAVE_CONFIG
221 static int dns_config (char *key, char *value)
224 if (strcasecmp (key, "Interface") == 0)
226 if (pcap_device != NULL)
228 if ((pcap_device = strdup (value)) == NULL)
231 else if (strcasecmp (key, "IgnoreSource") == 0)
234 ignore_list_add_name (value);
242 #endif /* HAVE_LIBPCAP */
244 #endif /* NAMED_HAVE_CONFIG */
246 static void dns_child_callback (const rfc1035_header_t *dns)
250 /* This is a query */
251 tr_queries += dns->length;
252 counter_list_add (&qtype_list, dns->qtype, 1);
256 /* This is a reply */
257 tr_responses += dns->length;
258 counter_list_add (&rcode_list, dns->rcode, 1);
261 /* FIXME: Are queries, replies or both interesting? */
262 counter_list_add (&opcode_list, dns->opcode, 1);
265 static void dns_child_loop (void)
268 char pcap_error[PCAP_ERRBUF_SIZE];
269 struct bpf_program fp;
271 struct pollfd poll_fds[2];
274 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
275 DBG ("Creating PCAP object..");
276 pcap_obj = pcap_open_live (pcap_device,
278 0 /* Not promiscuous */,
279 0 /* no read timeout */,
281 if (pcap_obj == NULL)
283 syslog (LOG_ERR, "dns plugin: Opening interface `%s' failed: %s",
284 (pcap_device != NULL) ? pcap_device : "any",
291 memset (&fp, 0, sizeof (fp));
292 if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
294 DBG ("pcap_compile failed");
295 syslog (LOG_ERR, "dns plugin: pcap_compile failed");
300 if (pcap_setfilter (pcap_obj, &fp) < 0)
302 DBG ("pcap_setfilter failed");
303 syslog (LOG_ERR, "dns plugin: pcap_setfilter failed");
309 DBG ("PCAP object created.");
311 dnstop_set_pcap_obj (pcap_obj);
312 dnstop_set_callback (dns_child_callback);
314 /* Set up pipe end */
315 poll_fds[0].fd = pipe_fd;
316 poll_fds[0].events = POLLOUT;
318 /* Set up pcap device */
319 poll_fds[1].fd = pcap_fileno (pcap_obj);
320 poll_fds[1].events = POLLIN | POLLPRI;
325 status = poll (poll_fds, 2, -1 /* wait forever for a change */);
329 syslog (LOG_ERR, "dns plugin: poll(2) failed: %s",
334 if (poll_fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
336 DBG ("Pipe closed. Exiting.");
337 syslog (LOG_NOTICE, "dns plugin: Pipe closed. Exiting.");
340 else if (poll_fds[0].revents & POLLOUT)
342 DBG ("Sending data..");
344 DBG ("swrite (pipe_fd = %i, tr_queries = %i)", pipe_fd, tr_queries);
345 if (swrite (pipe_fd, (const void *) &tr_queries, sizeof (tr_queries)) != 0)
347 DBG ("Writing to pipe_fd failed: %s", strerror (errno));
348 syslog (LOG_ERR, "dns plugin: Writing to pipe_fd failed: %s",
353 DBG ("swrite (pipe_fd = %i, tr_responses = %i)", pipe_fd, tr_responses);
354 if (swrite (pipe_fd, (const void *) &tr_responses, sizeof (tr_responses)) != 0)
356 DBG ("Writing to pipe_fd failed: %s", strerror (errno));
357 syslog (LOG_ERR, "dns plugin: Writing to pipe_fd failed: %s",
362 counter_list_send (qtype_list, pipe_fd);
363 counter_list_send (opcode_list, pipe_fd);
364 counter_list_send (rcode_list, pipe_fd);
367 if (poll_fds[1].revents & (POLLERR | POLLHUP | POLLNVAL))
369 DBG ("pcap-device closed. Exiting.");
370 syslog (LOG_ERR, "dns plugin: pcap-device closed. Exiting.");
373 else if (poll_fds[1].revents & (POLLIN | POLLPRI))
375 status = pcap_dispatch (pcap_obj,
376 10 /* Only handle 10 packets at a time */,
377 handle_pcap /* callback */,
378 NULL /* Whatever this means.. */);
381 DBG ("pcap_dispatch failed: %s", pcap_geterr (pcap_obj));
382 syslog (LOG_ERR, "dns plugin: pcap_dispatch failed: %s",
383 pcap_geterr (pcap_obj));
389 DBG ("child is exiting");
393 pcap_close (pcap_obj);
394 } /* static void dns_child_loop (void) */
396 static void dns_init (void)
405 if (pipe (pipe_fds) != 0)
407 syslog (LOG_ERR, "dns plugin: pipe(2) failed: %s",
416 syslog (LOG_ERR, "dns plugin: fork(2) failed: %s",
422 else if (pid_child != 0)
424 /* parent: Close the writing end, keep the reading end. */
425 pipe_fd = pipe_fds[0];
430 /* child: Close the reading end, keep the writing end. */
431 pipe_fd = pipe_fds[1];
438 /* fcntl (pipe_fd, F_SETFL, O_NONBLOCK); */
442 static void qtype_write (char *host, char *inst, char *val)
447 status = snprintf (file, 512, qtype_file, inst);
450 else if (status >= 512)
453 rrd_update_file (host, file, val, qtype_ds_def, qtype_ds_num);
456 static void rcode_write (char *host, char *inst, char *val)
461 status = snprintf (file, 512, rcode_file, inst);
464 else if (status >= 512)
467 rrd_update_file (host, file, val, rcode_ds_def, rcode_ds_num);
470 static void opcode_write (char *host, char *inst, char *val)
475 status = snprintf (file, 512, opcode_file, inst);
478 else if (status >= 512)
481 rrd_update_file (host, file, val, opcode_ds_def, opcode_ds_num);
484 static void qtype_submit (int qtype, unsigned int counter)
490 strncpy (inst, qtype_str (qtype), 32);
493 status = snprintf (buffer, 32, "N:%u", counter);
494 if ((status < 1) || (status >= 32))
497 plugin_submit ("dns_qtype", inst, buffer);
500 static void rcode_submit (int rcode, unsigned int counter)
506 strncpy (inst, rcode_str (rcode), 32);
509 status = snprintf (buffer, 32, "N:%u", counter);
510 if ((status < 1) || (status >= 32))
513 plugin_submit ("dns_rcode", inst, buffer);
516 static void opcode_submit (int opcode, unsigned int counter)
522 strncpy (inst, opcode_str (opcode), 32);
525 status = snprintf (buffer, 32, "N:%u", counter);
526 if ((status < 1) || (status >= 32))
529 plugin_submit ("dns_opcode", inst, buffer);
533 static unsigned int dns_read_array (unsigned int *values)
535 unsigned int values_num;
540 if (sread (pipe_fd, (void *) &values_num, sizeof (values_num)) != 0)
542 DBG ("Reading from the pipe failed: %s",
544 syslog (LOG_ERR, "dns plugin: Reading from the pipe failed: %s",
549 DBG ("sread (pipe_fd = %i, values_num = %u)", pipe_fd, values_num);
551 assert (values_num <= T_MAX);
556 if (sread (pipe_fd, (void *) values, 2 * sizeof (unsigned int) * values_num) != 0)
558 DBG ("Reading from the pipe failed: %s",
560 syslog (LOG_ERR, "dns plugin: Reading from the pipe failed: %s",
569 static void dns_read (void)
571 unsigned int values[2 * T_MAX];
572 unsigned int values_num;
578 if (sread (pipe_fd, (void *) &tr_queries, sizeof (tr_queries)) != 0)
580 DBG ("Reading from the pipe failed: %s",
582 syslog (LOG_ERR, "dns plugin: Reading from the pipe failed: %s",
587 DBG ("sread (pipe_fd = %i, tr_queries = %u)", pipe_fd, tr_queries);
589 if (sread (pipe_fd, (void *) &tr_responses, sizeof (tr_responses)) != 0)
591 DBG ("Reading from the pipe failed: %s",
593 syslog (LOG_ERR, "dns plugin: Reading from the pipe failed: %s",
598 DBG ("sread (pipe_fd = %i, tr_responses = %u)", pipe_fd, tr_responses);
600 values_num = dns_read_array (values);
601 for (i = 0; i < values_num; i++)
603 DBG ("qtype = %u; counter = %u;", values[2 * i], values[(2 * i) + 1]);
604 qtype_submit (values[2 * i], values[(2 * i) + 1]);
607 values_num = dns_read_array (values);
608 for (i = 0; i < values_num; i++)
610 DBG ("opcode = %u; counter = %u;", values[2 * i], values[(2 * i) + 1]);
611 opcode_submit (values[2 * i], values[(2 * i) + 1]);
614 values_num = dns_read_array (values);
615 for (i = 0; i < values_num; i++)
617 DBG ("rcode = %u; counter = %u;", values[2 * i], values[(2 * i) + 1]);
618 rcode_submit (values[2 * i], values[(2 * i) + 1]);
621 #else /* if !NAMED_HAVE_READ */
622 # define dns_read NULL
625 void module_register (void)
627 plugin_register (MODULE_NAME, dns_init, dns_read, NULL);
628 plugin_register ("dns_qtype", NULL, NULL, qtype_write);
629 plugin_register ("dns_rcode", NULL, NULL, rcode_write);
630 plugin_register ("dns_opcode", NULL, NULL, opcode_write);
631 cf_register (MODULE_NAME, dns_config, config_keys, config_keys_num);