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>
24 #define _DEFAULT_SOURCE
30 #include "configfile.h"
32 #include "utils_dns.h"
45 struct counter_list_s *next;
47 typedef struct counter_list_s counter_list_t;
52 static const char *config_keys[] =
56 "SelectNumericQueryTypes"
58 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
59 static int select_numeric_qtype = 1;
61 #define PCAP_SNAPLEN 1460
62 static char *pcap_device = NULL;
64 static derive_t tr_queries;
65 static derive_t tr_responses;
66 static counter_list_t *qtype_list;
67 static counter_list_t *opcode_list;
68 static counter_list_t *rcode_list;
70 static pthread_t listen_thread;
71 static int listen_thread_init = 0;
72 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
73 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
74 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
75 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
76 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
81 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
83 counter_list_t *entry;
85 for (entry = *list; entry != NULL; entry = entry->next)
86 if (entry->key == key)
92 static counter_list_t *counter_list_create (counter_list_t **list,
93 unsigned int key, unsigned int value)
95 counter_list_t *entry;
97 entry = (counter_list_t *) malloc (sizeof (counter_list_t));
101 memset (entry, 0, sizeof (counter_list_t));
103 entry->value = value;
111 counter_list_t *last;
114 while (last->next != NULL)
123 static void counter_list_add (counter_list_t **list,
124 unsigned int key, unsigned int increment)
126 counter_list_t *entry;
128 entry = counter_list_search (list, key);
132 entry->value += increment;
136 counter_list_create (list, key, increment);
140 static int dns_config (const char *key, const char *value)
142 if (strcasecmp (key, "Interface") == 0)
144 if (pcap_device != NULL)
146 if ((pcap_device = strdup (value)) == NULL)
149 else if (strcasecmp (key, "IgnoreSource") == 0)
152 ignore_list_add_name (value);
154 else if (strcasecmp (key, "SelectNumericQueryTypes") == 0)
156 if ((value != NULL) && IS_FALSE (value))
157 select_numeric_qtype = 0;
159 select_numeric_qtype = 1;
169 static void dns_child_callback (const rfc1035_header_t *dns)
173 /* This is a query */
175 if (!select_numeric_qtype)
177 const char *str = qtype_str(dns->qtype);
178 if ((str == NULL) || (str[0] == '#'))
182 pthread_mutex_lock (&traffic_mutex);
183 tr_queries += dns->length;
184 pthread_mutex_unlock (&traffic_mutex);
188 pthread_mutex_lock (&qtype_mutex);
189 counter_list_add (&qtype_list, dns->qtype, 1);
190 pthread_mutex_unlock (&qtype_mutex);
195 /* This is a reply */
196 pthread_mutex_lock (&traffic_mutex);
197 tr_responses += dns->length;
198 pthread_mutex_unlock (&traffic_mutex);
200 pthread_mutex_lock (&rcode_mutex);
201 counter_list_add (&rcode_list, dns->rcode, 1);
202 pthread_mutex_unlock (&rcode_mutex);
205 /* FIXME: Are queries, replies or both interesting? */
206 pthread_mutex_lock (&opcode_mutex);
207 counter_list_add (&opcode_list, dns->opcode, 1);
208 pthread_mutex_unlock (&opcode_mutex);
211 static int dns_run_pcap_loop (void)
214 char pcap_error[PCAP_ERRBUF_SIZE];
215 struct bpf_program fp;
219 /* Don't block any signals */
222 sigemptyset (&sigmask);
223 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
226 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
227 DEBUG ("dns plugin: Creating PCAP object..");
228 pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any",
230 0 /* Not promiscuous */,
231 (int) CDTIME_T_TO_MS (plugin_get_interval () / 2),
233 if (pcap_obj == NULL)
235 ERROR ("dns plugin: Opening interface `%s' "
237 (pcap_device != NULL) ? pcap_device : "any",
242 memset (&fp, 0, sizeof (fp));
243 status = pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0);
246 ERROR ("dns plugin: pcap_compile failed: %s",
247 pcap_statustostr (status));
251 status = pcap_setfilter (pcap_obj, &fp);
254 ERROR ("dns plugin: pcap_setfilter failed: %s",
255 pcap_statustostr (status));
259 DEBUG ("dns plugin: PCAP object created.");
261 dnstop_set_pcap_obj (pcap_obj);
262 dnstop_set_callback (dns_child_callback);
264 status = pcap_loop (pcap_obj,
265 -1 /* loop forever */,
266 handle_pcap /* callback */,
267 NULL /* user data */);
268 INFO ("dns plugin: pcap_loop exited with status %i.", status);
269 /* We need to handle "PCAP_ERROR" specially because libpcap currently
270 * doesn't return PCAP_ERROR_IFACE_NOT_UP for compatibility reasons. */
271 if (status == PCAP_ERROR)
272 status = PCAP_ERROR_IFACE_NOT_UP;
274 pcap_close (pcap_obj);
276 } /* int dns_run_pcap_loop */
278 static int dns_sleep_one_interval (void) /* {{{ */
281 struct timespec ts = { 0, 0 };
284 interval = plugin_get_interval ();
285 CDTIME_T_TO_TIMESPEC (interval, &ts);
289 struct timespec rem = { 0, 0 };
291 status = nanosleep (&ts, &rem);
294 else if ((errno == EINTR) || (errno == EAGAIN))
304 } /* }}} int dns_sleep_one_interval */
306 static void *dns_child_loop (__attribute__((unused)) void *dummy) /* {{{ */
312 status = dns_run_pcap_loop ();
313 if (status != PCAP_ERROR_IFACE_NOT_UP)
316 dns_sleep_one_interval ();
319 if (status != PCAP_ERROR_BREAK)
320 ERROR ("dns plugin: PCAP returned error %s.",
321 pcap_statustostr (status));
323 listen_thread_init = 0;
325 } /* }}} void *dns_child_loop */
327 static int dns_init (void)
329 /* clean up an old thread */
332 pthread_mutex_lock (&traffic_mutex);
335 pthread_mutex_unlock (&traffic_mutex);
337 if (listen_thread_init != 0)
340 status = plugin_thread_create (&listen_thread, NULL, dns_child_loop,
345 ERROR ("dns plugin: pthread_create failed: %s",
346 sstrerror (errno, errbuf, sizeof (errbuf)));
350 listen_thread_init = 1;
355 static void submit_derive (const char *type, const char *type_instance,
359 value_list_t vl = VALUE_LIST_INIT;
361 values[0].derive = value;
365 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
366 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
367 sstrncpy (vl.type, type, sizeof (vl.type));
368 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
370 plugin_dispatch_values (&vl);
371 } /* void submit_derive */
373 static void submit_octets (derive_t queries, derive_t responses)
376 value_list_t vl = VALUE_LIST_INIT;
378 values[0].derive = queries;
379 values[1].derive = responses;
383 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
384 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
385 sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
387 plugin_dispatch_values (&vl);
388 } /* void submit_octets */
390 static int dns_read (void)
392 unsigned int keys[T_MAX];
393 unsigned int values[T_MAX];
399 pthread_mutex_lock (&traffic_mutex);
400 values[0] = tr_queries;
401 values[1] = tr_responses;
402 pthread_mutex_unlock (&traffic_mutex);
404 if ((values[0] != 0) || (values[1] != 0))
405 submit_octets (values[0], values[1]);
407 pthread_mutex_lock (&qtype_mutex);
408 for (ptr = qtype_list, len = 0;
409 (ptr != NULL) && (len < T_MAX);
410 ptr = ptr->next, len++)
412 keys[len] = ptr->key;
413 values[len] = ptr->value;
415 pthread_mutex_unlock (&qtype_mutex);
417 for (i = 0; i < len; i++)
419 DEBUG ("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]);
420 submit_derive ("dns_qtype", qtype_str (keys[i]), values[i]);
423 pthread_mutex_lock (&opcode_mutex);
424 for (ptr = opcode_list, len = 0;
425 (ptr != NULL) && (len < T_MAX);
426 ptr = ptr->next, len++)
428 keys[len] = ptr->key;
429 values[len] = ptr->value;
431 pthread_mutex_unlock (&opcode_mutex);
433 for (i = 0; i < len; i++)
435 DEBUG ("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]);
436 submit_derive ("dns_opcode", opcode_str (keys[i]), values[i]);
439 pthread_mutex_lock (&rcode_mutex);
440 for (ptr = rcode_list, len = 0;
441 (ptr != NULL) && (len < T_MAX);
442 ptr = ptr->next, len++)
444 keys[len] = ptr->key;
445 values[len] = ptr->value;
447 pthread_mutex_unlock (&rcode_mutex);
449 for (i = 0; i < len; i++)
451 DEBUG ("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]);
452 submit_derive ("dns_rcode", rcode_str (keys[i]), values[i]);
458 void module_register (void)
460 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
461 plugin_register_init ("dns", dns_init);
462 plugin_register_read ("dns", dns_read);
463 } /* void module_register */