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"
29 #define MODULE_NAME "dns"
31 #if HAVE_LIBPCAP && HAVE_LIBPTHREAD
32 # include "utils_dns.h"
35 # include <sys/poll.h>
36 # define DNS_HAVE_READ 1
38 # define DNS_HAVE_READ 0
49 struct counter_list_s *next;
51 typedef struct counter_list_s counter_list_t;
57 static char *traffic_file = "dns/dns_traffic.rrd";
58 static char *qtype_file = "dns/qtype-%s.rrd";
59 static char *opcode_file = "dns/opcode-%s.rrd";
60 static char *rcode_file = "dns/rcode-%s.rrd";
62 static char *traffic_ds_def[] =
64 /* Limit to 1GBit/s */
65 "DS:queries:COUNTER:"COLLECTD_HEARTBEAT":0:125000000",
66 "DS:responses:COUNTER:"COLLECTD_HEARTBEAT":0:125000000",
69 static int traffic_ds_num = 2;
71 static char *qtype_ds_def[] =
73 "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:65535",
76 static int qtype_ds_num = 1;
78 static char *opcode_ds_def[] =
80 "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:65535",
83 static int opcode_ds_num = 1;
85 static char *rcode_ds_def[] =
87 "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:65535",
90 static int rcode_ds_num = 1;
93 static char *config_keys[] =
99 static int config_keys_num = 2;
101 #define PCAP_SNAPLEN 1460
102 static char *pcap_device = NULL;
104 static unsigned int tr_queries;
105 static unsigned int tr_responses;
106 static counter_list_t *qtype_list;
107 static counter_list_t *opcode_list;
108 static counter_list_t *rcode_list;
110 static pthread_t listen_thread;
111 static int listen_thread_init = 0;
112 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
113 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
114 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
115 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
116 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
117 #endif /* DNS_HAVE_READ */
123 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
125 counter_list_t *entry;
127 DBG ("counter_list_search (list = %p, key = %u)",
128 (void *) *list, key);
130 for (entry = *list; entry != NULL; entry = entry->next)
131 if (entry->key == key)
134 DBG ("return (%p)", (void *) entry);
138 static counter_list_t *counter_list_create (counter_list_t **list,
139 unsigned int key, unsigned int value)
141 counter_list_t *entry;
143 DBG ("counter_list_create (list = %p, key = %u, value = %u)",
144 (void *) *list, key, value);
146 entry = (counter_list_t *) malloc (sizeof (counter_list_t));
150 memset (entry, 0, sizeof (counter_list_t));
152 entry->value = value;
160 counter_list_t *last;
163 while (last->next != NULL)
169 DBG ("return (%p)", (void *) entry);
173 static void counter_list_add (counter_list_t **list,
174 unsigned int key, unsigned int increment)
176 counter_list_t *entry;
178 DBG ("counter_list_add (list = %p, key = %u, increment = %u)",
179 (void *) *list, key, increment);
181 entry = counter_list_search (list, key);
185 entry->value += increment;
189 counter_list_create (list, key, increment);
194 static int dns_config (char *key, char *value)
196 if (strcasecmp (key, "Interface") == 0)
198 if (pcap_device != NULL)
200 if ((pcap_device = strdup (value)) == NULL)
203 else if (strcasecmp (key, "IgnoreSource") == 0)
206 ignore_list_add_name (value);
216 static void dns_child_callback (const rfc1035_header_t *dns)
220 /* This is a query */
221 pthread_mutex_lock (&traffic_mutex);
222 tr_queries += dns->length;
223 pthread_mutex_unlock (&traffic_mutex);
225 pthread_mutex_lock (&qtype_mutex);
226 counter_list_add (&qtype_list, dns->qtype, 1);
227 pthread_mutex_unlock (&qtype_mutex);
231 /* This is a reply */
232 pthread_mutex_lock (&traffic_mutex);
233 tr_responses += dns->length;
234 pthread_mutex_unlock (&traffic_mutex);
236 pthread_mutex_lock (&rcode_mutex);
237 counter_list_add (&rcode_list, dns->rcode, 1);
238 pthread_mutex_unlock (&rcode_mutex);
241 /* FIXME: Are queries, replies or both interesting? */
242 pthread_mutex_lock (&opcode_mutex);
243 counter_list_add (&opcode_list, dns->opcode, 1);
244 pthread_mutex_unlock (&opcode_mutex);
247 static void *dns_child_loop (void *dummy)
250 char pcap_error[PCAP_ERRBUF_SIZE];
251 struct bpf_program fp;
255 /* Don't block any signals */
258 sigemptyset (&sigmask);
259 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
262 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
263 DBG ("Creating PCAP object..");
264 pcap_obj = pcap_open_live (pcap_device,
266 0 /* Not promiscuous */,
267 atoi (COLLECTD_STEP),
269 if (pcap_obj == NULL)
271 syslog (LOG_ERR, "dns plugin: Opening interface `%s' "
273 (pcap_device != NULL) ? pcap_device : "any",
278 memset (&fp, 0, sizeof (fp));
279 if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
281 DBG ("pcap_compile failed");
282 syslog (LOG_ERR, "dns plugin: pcap_compile failed");
285 if (pcap_setfilter (pcap_obj, &fp) < 0)
287 DBG ("pcap_setfilter failed");
288 syslog (LOG_ERR, "dns plugin: pcap_setfilter failed");
292 DBG ("PCAP object created.");
294 dnstop_set_pcap_obj (pcap_obj);
295 dnstop_set_callback (dns_child_callback);
297 status = pcap_loop (pcap_obj,
298 -1 /* loop forever */,
299 handle_pcap /* callback */,
300 NULL /* Whatever this means.. */);
302 syslog (LOG_ERR, "dns plugin: Listener thread is exiting "
303 "abnormally: %s", pcap_geterr (pcap_obj));
305 DBG ("child is exiting");
307 pcap_close (pcap_obj);
308 listen_thread_init = 0;
312 } /* static void dns_child_loop (void) */
313 #endif /* DNS_HAVE_READ */
315 static void dns_init (void)
318 /* clean up an old thread */
321 pthread_mutex_lock (&traffic_mutex);
324 pthread_mutex_unlock (&traffic_mutex);
326 if (listen_thread_init != 0)
329 status = pthread_create (&listen_thread, NULL, dns_child_loop,
333 syslog (LOG_ERR, "dns plugin: pthread_create failed: %s",
338 listen_thread_init = 1;
339 #endif /* DNS_HAVE_READ */
342 static void traffic_write (char *host, char *inst, char *val)
344 rrd_update_file (host, traffic_file, val,
345 traffic_ds_def, traffic_ds_num);
348 static void qtype_write (char *host, char *inst, char *val)
353 status = snprintf (file, 512, qtype_file, inst);
356 else if (status >= 512)
359 rrd_update_file (host, file, val, qtype_ds_def, qtype_ds_num);
362 static void rcode_write (char *host, char *inst, char *val)
367 status = snprintf (file, 512, rcode_file, inst);
370 else if (status >= 512)
373 rrd_update_file (host, file, val, rcode_ds_def, rcode_ds_num);
376 static void opcode_write (char *host, char *inst, char *val)
381 status = snprintf (file, 512, opcode_file, inst);
384 else if (status >= 512)
387 rrd_update_file (host, file, val, opcode_ds_def, opcode_ds_num);
391 static void traffic_submit (unsigned int queries, unsigned int replies)
396 status = snprintf (buffer, 64, "N:%u:%u", queries, replies);
397 if ((status < 1) || (status >= 64))
400 plugin_submit ("dns_traffic", "-", buffer);
403 static void qtype_submit (int qtype, unsigned int counter)
409 strncpy (inst, qtype_str (qtype), 32);
412 status = snprintf (buffer, 32, "N:%u", counter);
413 if ((status < 1) || (status >= 32))
416 plugin_submit ("dns_qtype", inst, buffer);
419 static void rcode_submit (int rcode, unsigned int counter)
425 strncpy (inst, rcode_str (rcode), 32);
428 status = snprintf (buffer, 32, "N:%u", counter);
429 if ((status < 1) || (status >= 32))
432 plugin_submit ("dns_rcode", inst, buffer);
435 static void opcode_submit (int opcode, unsigned int counter)
441 strncpy (inst, opcode_str (opcode), 32);
444 status = snprintf (buffer, 32, "N:%u", counter);
445 if ((status < 1) || (status >= 32))
448 plugin_submit ("dns_opcode", inst, buffer);
451 static void dns_read (void)
453 unsigned int keys[T_MAX];
454 unsigned int values[T_MAX];
460 pthread_mutex_lock (&traffic_mutex);
461 values[0] = tr_queries;
462 values[1] = tr_responses;
463 pthread_mutex_unlock (&traffic_mutex);
464 traffic_submit (values[0], values[1]);
466 pthread_mutex_lock (&qtype_mutex);
467 for (ptr = qtype_list, len = 0;
468 (ptr != NULL) && (len < T_MAX);
469 ptr = ptr->next, len++)
471 keys[len] = ptr->key;
472 values[len] = ptr->value;
474 pthread_mutex_unlock (&qtype_mutex);
476 for (i = 0; i < len; i++)
478 DBG ("qtype = %u; counter = %u;", keys[i], values[i]);
479 qtype_submit (keys[i], values[i]);
482 pthread_mutex_lock (&opcode_mutex);
483 for (ptr = opcode_list, len = 0;
484 (ptr != NULL) && (len < T_MAX);
485 ptr = ptr->next, len++)
487 keys[len] = ptr->key;
488 values[len] = ptr->value;
490 pthread_mutex_unlock (&opcode_mutex);
492 for (i = 0; i < len; i++)
494 DBG ("opcode = %u; counter = %u;", keys[i], values[i]);
495 opcode_submit (keys[i], values[i]);
498 pthread_mutex_lock (&rcode_mutex);
499 for (ptr = rcode_list, len = 0;
500 (ptr != NULL) && (len < T_MAX);
501 ptr = ptr->next, len++)
503 keys[len] = ptr->key;
504 values[len] = ptr->value;
506 pthread_mutex_unlock (&rcode_mutex);
508 for (i = 0; i < len; i++)
510 DBG ("rcode = %u; counter = %u;", keys[i], values[i]);
511 rcode_submit (keys[i], values[i]);
514 #else /* if !DNS_HAVE_READ */
515 # define dns_read NULL
518 void module_register (void)
520 plugin_register (MODULE_NAME, dns_init, dns_read, NULL);
521 plugin_register ("dns_traffic", NULL, NULL, traffic_write);
522 plugin_register ("dns_qtype", NULL, NULL, qtype_write);
523 plugin_register ("dns_rcode", NULL, NULL, rcode_write);
524 plugin_register ("dns_opcode", NULL, NULL, opcode_write);
526 cf_register (MODULE_NAME, dns_config, config_keys, config_keys_num);