2 * collectd - src/named.c
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 "named"
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 = "named/qtype-%s.rrd";
57 static char *opcode_file = "named/opcode-%s.rrd";
58 static char *rcode_file = "named/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 counter_list_t *qtype_list;
99 static counter_list_t *opcode_list;
100 static counter_list_t *rcode_list;
103 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
105 counter_list_t *entry;
107 DBG ("counter_list_search (list = %p, key = %u)",
108 (void *) *list, key);
110 for (entry = *list; entry != NULL; entry = entry->next)
111 if (entry->key == key)
114 DBG ("return (%p)", (void *) entry);
118 static counter_list_t *counter_list_create (counter_list_t **list,
119 unsigned int key, unsigned int value)
121 counter_list_t *entry;
123 DBG ("counter_list_create (list = %p, key = %u, value = %u)",
124 (void *) *list, key, value);
126 entry = (counter_list_t *) malloc (sizeof (counter_list_t));
130 memset (entry, 0, sizeof (counter_list_t));
132 entry->value = value;
140 counter_list_t *last;
143 while (last->next != NULL)
149 DBG ("return (%p)", (void *) entry);
153 static void counter_list_add (counter_list_t **list,
154 unsigned int key, unsigned int increment)
156 counter_list_t *entry;
158 DBG ("counter_list_add (list = %p, key = %u, increment = %u)",
159 (void *) *list, key, increment);
161 entry = counter_list_search (list, key);
165 entry->value += increment;
169 counter_list_create (list, key, increment);
174 static int counter_list_send (counter_list_t *list, int fd)
177 unsigned int values[2 * T_MAX];
178 unsigned int values_num;
186 (cl != NULL) && (values_num < T_MAX);
189 values[2 * values_num] = cl->key;
190 values[(2 * values_num) + 1] = cl->value;
194 DBG ("swrite (fd = %i, values_num = %i)", fd, values_num);
195 if (swrite (fd, (const void *) &values_num, sizeof (values_num)) != 0)
197 DBG ("Writing to fd failed: %s", strerror (errno));
198 syslog (LOG_ERR, "named plugin: Writing to fd failed: %s",
206 DBG ("swrite (fd = %i, values = %p, size = %i)",
207 fd, (void *) values, (int) (sizeof (int) * values_num));
208 if (swrite (fd, (const void *) values, 2 * sizeof (int) * values_num) != 0)
210 DBG ("Writing to pipe failed: %s", strerror (errno));
211 syslog (LOG_ERR, "named plugin: Writing to pipe failed: %s",
218 #if NAMED_HAVE_CONFIG
219 static int named_config (char *key, char *value)
222 if (strcasecmp (key, "Interface") == 0)
224 if (pcap_device != NULL)
226 if ((pcap_device = strdup (value)) == NULL)
229 else if (strcasecmp (key, "IgnoreSource") == 0)
232 ignore_list_add_name (value);
240 #endif /* HAVE_LIBPCAP */
242 #endif /* NAMED_HAVE_CONFIG */
244 static void named_child_callback (const rfc1035_header_t *dns)
248 /* This is a query */
249 counter_list_add (&qtype_list, dns->qtype, 1);
253 /* This is a reply */
254 counter_list_add (&rcode_list, dns->rcode, 1);
257 /* FIXME: Are queries, replies or both interesting? */
258 counter_list_add (&opcode_list, dns->opcode, 1);
261 static void named_child_loop (void)
264 char pcap_error[PCAP_ERRBUF_SIZE];
265 struct bpf_program fp;
267 struct pollfd poll_fds[2];
270 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
271 DBG ("Creating PCAP object..");
272 pcap_obj = pcap_open_live (pcap_device,
274 0 /* Not promiscuous */,
275 0 /* no read timeout */,
277 if (pcap_obj == NULL)
279 syslog (LOG_ERR, "named plugin: Opening interface `%s' failed: %s",
280 (pcap_device != NULL) ? pcap_device : "any",
287 memset (&fp, 0, sizeof (fp));
288 if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
290 DBG ("pcap_compile failed");
291 syslog (LOG_ERR, "named plugin: pcap_compile failed");
296 if (pcap_setfilter (pcap_obj, &fp) < 0)
298 DBG ("pcap_setfilter failed");
299 syslog (LOG_ERR, "named plugin: pcap_setfilter failed");
305 DBG ("PCAP object created.");
307 dnstop_set_pcap_obj (pcap_obj);
308 dnstop_set_callback (named_child_callback);
310 /* Set up pipe end */
311 poll_fds[0].fd = pipe_fd;
312 poll_fds[0].events = POLLOUT;
314 /* Set up pcap device */
315 poll_fds[1].fd = pcap_fileno (pcap_obj);
316 poll_fds[1].events = POLLIN | POLLPRI;
321 status = poll (poll_fds, 2, -1 /* wait forever for a change */);
325 syslog (LOG_ERR, "named plugin: poll(2) failed: %s",
330 if (poll_fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
332 DBG ("Pipe closed. Exiting.");
333 syslog (LOG_NOTICE, "named plugin: Pipe closed. Exiting.");
336 else if (poll_fds[0].revents & POLLOUT)
338 DBG ("Sending data..");
339 counter_list_send (qtype_list, pipe_fd);
340 counter_list_send (opcode_list, pipe_fd);
341 counter_list_send (rcode_list, pipe_fd);
344 if (poll_fds[1].revents & (POLLERR | POLLHUP | POLLNVAL))
346 DBG ("pcap-device closed. Exiting.");
347 syslog (LOG_ERR, "named plugin: pcap-device closed. Exiting.");
350 else if (poll_fds[1].revents & (POLLIN | POLLPRI))
352 status = pcap_dispatch (pcap_obj,
353 10 /* Only handle 10 packets at a time */,
354 handle_pcap /* callback */,
355 NULL /* Whatever this means.. */);
358 DBG ("pcap_dispatch failed: %s", pcap_geterr (pcap_obj));
359 syslog (LOG_ERR, "named plugin: pcap_dispatch failed: %s",
360 pcap_geterr (pcap_obj));
366 DBG ("child is exiting");
370 pcap_close (pcap_obj);
371 } /* static void named_child_loop (void) */
373 static void named_init (void)
379 if (pipe (pipe_fds) != 0)
381 syslog (LOG_ERR, "named plugin: pipe(2) failed: %s",
390 syslog (LOG_ERR, "named plugin: fork(2) failed: %s",
396 else if (pid_child != 0)
398 /* parent: Close the writing end, keep the reading end. */
399 pipe_fd = pipe_fds[0];
404 /* child: Close the reading end, keep the writing end. */
405 pipe_fd = pipe_fds[1];
412 /* fcntl (pipe_fd, F_SETFL, O_NONBLOCK); */
416 static void qtype_write (char *host, char *inst, char *val)
421 status = snprintf (file, 512, qtype_file, inst);
424 else if (status >= 512)
427 rrd_update_file (host, file, val, qtype_ds_def, qtype_ds_num);
430 static void rcode_write (char *host, char *inst, char *val)
435 status = snprintf (file, 512, rcode_file, inst);
438 else if (status >= 512)
441 rrd_update_file (host, file, val, rcode_ds_def, rcode_ds_num);
444 static void opcode_write (char *host, char *inst, char *val)
449 status = snprintf (file, 512, opcode_file, inst);
452 else if (status >= 512)
455 rrd_update_file (host, file, val, opcode_ds_def, opcode_ds_num);
458 static void qtype_submit (int qtype, unsigned int counter)
464 strncpy (inst, qtype_str (qtype), 32);
467 status = snprintf (buffer, 32, "N:%u", counter);
468 if ((status < 1) || (status >= 32))
471 plugin_submit ("named_qtype", inst, buffer);
474 static void rcode_submit (int rcode, unsigned int counter)
480 strncpy (inst, rcode_str (rcode), 32);
483 status = snprintf (buffer, 32, "N:%u", counter);
484 if ((status < 1) || (status >= 32))
487 plugin_submit ("named_rcode", inst, buffer);
490 static void opcode_submit (int opcode, unsigned int counter)
496 strncpy (inst, opcode_str (opcode), 32);
499 status = snprintf (buffer, 32, "N:%u", counter);
500 if ((status < 1) || (status >= 32))
503 plugin_submit ("named_opcode", inst, buffer);
507 static unsigned int named_read_array (unsigned int *values)
509 unsigned int values_num;
514 if (sread (pipe_fd, (void *) &values_num, sizeof (values_num)) != 0)
516 DBG ("Reading from the pipe failed: %s",
518 syslog (LOG_ERR, "named plugin: Reading from the pipe failed: %s",
523 DBG ("sread (pipe_fd = %i, values_num = %u)", pipe_fd, values_num);
525 assert (values_num <= T_MAX);
530 if (sread (pipe_fd, (void *) values, 2 * sizeof (unsigned int) * values_num) != 0)
532 DBG ("Reading from the pipe failed: %s",
534 syslog (LOG_ERR, "named plugin: Reading from the pipe failed: %s",
543 static void named_read (void)
545 unsigned int values[2 * T_MAX];
546 unsigned int values_num;
552 values_num = named_read_array (values);
553 for (i = 0; i < values_num; i++)
555 DBG ("qtype = %u; counter = %u;", values[2 * i], values[(2 * i) + 1]);
556 qtype_submit (values[2 * i], values[(2 * i) + 1]);
559 values_num = named_read_array (values);
560 for (i = 0; i < values_num; i++)
562 DBG ("opcode = %u; counter = %u;", values[2 * i], values[(2 * i) + 1]);
563 opcode_submit (values[2 * i], values[(2 * i) + 1]);
566 values_num = named_read_array (values);
567 for (i = 0; i < values_num; i++)
569 DBG ("rcode = %u; counter = %u;", values[2 * i], values[(2 * i) + 1]);
570 rcode_submit (values[2 * i], values[(2 * i) + 1]);
573 #else /* if !NAMED_HAVE_READ */
574 # define named_read NULL
577 void module_register (void)
579 plugin_register (MODULE_NAME, named_init, named_read, NULL);
580 plugin_register ("named_qtype", NULL, NULL, qtype_write);
581 plugin_register ("named_rcode", NULL, NULL, rcode_write);
582 plugin_register ("named_opcode", NULL, NULL, opcode_write);
583 cf_register (MODULE_NAME, named_config, config_keys, config_keys_num);