3 * Copyright (C) 2006-2011 Florian octo Forster
4 * Copyright (C) 2009 Mirko Buffoni
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.
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 collectd.org>
21 * Mirko Buffoni <briareos at eswat.org>
29 #include "configfile.h"
31 #include "utils_dns.h"
37 # include <pcap-bpf.h>
47 struct counter_list_s *next;
49 typedef struct counter_list_s counter_list_t;
54 static const char *config_keys[] =
58 "SelectNumericQueryTypes"
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
61 static int select_numeric_qtype = 1;
63 #define PCAP_SNAPLEN 1460
64 static char *pcap_device = NULL;
66 static derive_t tr_queries;
67 static derive_t tr_responses;
68 static counter_list_t *qtype_list;
69 static counter_list_t *opcode_list;
70 static counter_list_t *rcode_list;
72 static pthread_t listen_thread;
73 static int listen_thread_init = 0;
74 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
75 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
76 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
77 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
78 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
83 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
85 counter_list_t *entry;
87 for (entry = *list; entry != NULL; entry = entry->next)
88 if (entry->key == key)
94 static counter_list_t *counter_list_create (counter_list_t **list,
95 unsigned int key, unsigned int value)
97 counter_list_t *entry;
99 entry = (counter_list_t *) malloc (sizeof (counter_list_t));
103 memset (entry, 0, sizeof (counter_list_t));
105 entry->value = value;
113 counter_list_t *last;
116 while (last->next != NULL)
125 static void counter_list_add (counter_list_t **list,
126 unsigned int key, unsigned int increment)
128 counter_list_t *entry;
130 entry = counter_list_search (list, key);
134 entry->value += increment;
138 counter_list_create (list, key, increment);
142 static int dns_config (const char *key, const char *value)
144 if (strcasecmp (key, "Interface") == 0)
146 if (pcap_device != NULL)
148 if ((pcap_device = strdup (value)) == NULL)
151 else if (strcasecmp (key, "IgnoreSource") == 0)
154 ignore_list_add_name (value);
156 else if (strcasecmp (key, "SelectNumericQueryTypes") == 0)
158 if ((value != NULL) && IS_FALSE (value))
159 select_numeric_qtype = 0;
161 select_numeric_qtype = 1;
171 static void dns_child_callback (const rfc1035_header_t *dns)
175 /* This is a query */
177 if (!select_numeric_qtype)
179 const char *str = qtype_str(dns->qtype);
180 if ((str == NULL) || (str[0] == '#'))
184 pthread_mutex_lock (&traffic_mutex);
185 tr_queries += dns->length;
186 pthread_mutex_unlock (&traffic_mutex);
190 pthread_mutex_lock (&qtype_mutex);
191 counter_list_add (&qtype_list, dns->qtype, 1);
192 pthread_mutex_unlock (&qtype_mutex);
197 /* This is a reply */
198 pthread_mutex_lock (&traffic_mutex);
199 tr_responses += dns->length;
200 pthread_mutex_unlock (&traffic_mutex);
202 pthread_mutex_lock (&rcode_mutex);
203 counter_list_add (&rcode_list, dns->rcode, 1);
204 pthread_mutex_unlock (&rcode_mutex);
207 /* FIXME: Are queries, replies or both interesting? */
208 pthread_mutex_lock (&opcode_mutex);
209 counter_list_add (&opcode_list, dns->opcode, 1);
210 pthread_mutex_unlock (&opcode_mutex);
213 static void *dns_child_loop (__attribute__((unused)) void *dummy)
216 char pcap_error[PCAP_ERRBUF_SIZE];
217 struct bpf_program fp;
221 /* Don't block any signals */
224 sigemptyset (&sigmask);
225 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
228 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
229 DEBUG ("dns plugin: Creating PCAP object..");
230 pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any",
232 0 /* Not promiscuous */,
233 (int) CDTIME_T_TO_MS (interval_g / 2),
235 if (pcap_obj == NULL)
237 ERROR ("dns plugin: Opening interface `%s' "
239 (pcap_device != NULL) ? pcap_device : "any",
244 memset (&fp, 0, sizeof (fp));
245 if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
247 ERROR ("dns plugin: pcap_compile failed");
250 if (pcap_setfilter (pcap_obj, &fp) < 0)
252 ERROR ("dns plugin: pcap_setfilter failed");
256 DEBUG ("dns plugin: PCAP object created.");
258 dnstop_set_pcap_obj (pcap_obj);
259 dnstop_set_callback (dns_child_callback);
261 status = pcap_loop (pcap_obj,
262 -1 /* loop forever */,
263 handle_pcap /* callback */,
264 NULL /* Whatever this means.. */);
266 ERROR ("dns plugin: Listener thread is exiting "
267 "abnormally: %s", pcap_geterr (pcap_obj));
269 DEBUG ("dns plugin: Child is exiting.");
271 pcap_close (pcap_obj);
272 listen_thread_init = 0;
276 } /* static void dns_child_loop (void) */
278 static int dns_init (void)
280 /* clean up an old thread */
283 pthread_mutex_lock (&traffic_mutex);
286 pthread_mutex_unlock (&traffic_mutex);
288 if (listen_thread_init != 0)
291 status = pthread_create (&listen_thread, NULL, dns_child_loop,
296 ERROR ("dns plugin: pthread_create failed: %s",
297 sstrerror (errno, errbuf, sizeof (errbuf)));
301 listen_thread_init = 1;
306 static void submit_derive (const char *type, const char *type_instance,
310 value_list_t vl = VALUE_LIST_INIT;
312 values[0].derive = value;
316 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
317 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
318 sstrncpy (vl.type, type, sizeof (vl.type));
319 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
321 plugin_dispatch_values (&vl);
322 } /* void submit_derive */
324 static void submit_octets (derive_t queries, derive_t responses)
327 value_list_t vl = VALUE_LIST_INIT;
329 values[0].derive = queries;
330 values[1].derive = responses;
334 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
335 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
336 sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
338 plugin_dispatch_values (&vl);
339 } /* void submit_octets */
341 static int dns_read (void)
343 unsigned int keys[T_MAX];
344 unsigned int values[T_MAX];
350 pthread_mutex_lock (&traffic_mutex);
351 values[0] = tr_queries;
352 values[1] = tr_responses;
353 pthread_mutex_unlock (&traffic_mutex);
355 if ((values[0] != 0) || (values[1] != 0))
356 submit_octets (values[0], values[1]);
358 pthread_mutex_lock (&qtype_mutex);
359 for (ptr = qtype_list, len = 0;
360 (ptr != NULL) && (len < T_MAX);
361 ptr = ptr->next, len++)
363 keys[len] = ptr->key;
364 values[len] = ptr->value;
366 pthread_mutex_unlock (&qtype_mutex);
368 for (i = 0; i < len; i++)
370 DEBUG ("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]);
371 submit_derive ("dns_qtype", qtype_str (keys[i]), values[i]);
374 pthread_mutex_lock (&opcode_mutex);
375 for (ptr = opcode_list, len = 0;
376 (ptr != NULL) && (len < T_MAX);
377 ptr = ptr->next, len++)
379 keys[len] = ptr->key;
380 values[len] = ptr->value;
382 pthread_mutex_unlock (&opcode_mutex);
384 for (i = 0; i < len; i++)
386 DEBUG ("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]);
387 submit_derive ("dns_opcode", opcode_str (keys[i]), values[i]);
390 pthread_mutex_lock (&rcode_mutex);
391 for (ptr = rcode_list, len = 0;
392 (ptr != NULL) && (len < T_MAX);
393 ptr = ptr->next, len++)
395 keys[len] = ptr->key;
396 values[len] = ptr->value;
398 pthread_mutex_unlock (&rcode_mutex);
400 for (i = 0; i < len; i++)
402 DEBUG ("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]);
403 submit_derive ("dns_rcode", rcode_str (keys[i]), values[i]);
409 void module_register (void)
411 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
412 plugin_register_init ("dns", dns_init);
413 plugin_register_read ("dns", dns_read);
414 } /* void module_register */