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"
44 struct counter_list_s *next;
46 typedef struct counter_list_s counter_list_t;
51 static const char *config_keys[] =
55 "SelectNumericQueryTypes"
57 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
58 static int select_numeric_qtype = 1;
60 #define PCAP_SNAPLEN 1460
61 static char *pcap_device = NULL;
63 static derive_t tr_queries;
64 static derive_t tr_responses;
65 static counter_list_t *qtype_list;
66 static counter_list_t *opcode_list;
67 static counter_list_t *rcode_list;
69 static pthread_t listen_thread;
70 static int listen_thread_init = 0;
71 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
72 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
73 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
74 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
75 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
80 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
82 counter_list_t *entry;
84 for (entry = *list; entry != NULL; entry = entry->next)
85 if (entry->key == key)
91 static counter_list_t *counter_list_create (counter_list_t **list,
92 unsigned int key, unsigned int value)
94 counter_list_t *entry;
96 entry = calloc (1, sizeof (*entry));
101 entry->value = value;
109 counter_list_t *last;
112 while (last->next != NULL)
121 static void counter_list_add (counter_list_t **list,
122 unsigned int key, unsigned int increment)
124 counter_list_t *entry;
126 entry = counter_list_search (list, key);
130 entry->value += increment;
134 counter_list_create (list, key, increment);
138 static int dns_config (const char *key, const char *value)
140 if (strcasecmp (key, "Interface") == 0)
142 if (pcap_device != NULL)
144 if ((pcap_device = strdup (value)) == NULL)
147 else if (strcasecmp (key, "IgnoreSource") == 0)
150 ignore_list_add_name (value);
152 else if (strcasecmp (key, "SelectNumericQueryTypes") == 0)
154 if ((value != NULL) && IS_FALSE (value))
155 select_numeric_qtype = 0;
157 select_numeric_qtype = 1;
167 static void dns_child_callback (const rfc1035_header_t *dns)
171 /* This is a query */
173 if (!select_numeric_qtype)
175 const char *str = qtype_str(dns->qtype);
176 if ((str == NULL) || (str[0] == '#'))
180 pthread_mutex_lock (&traffic_mutex);
181 tr_queries += dns->length;
182 pthread_mutex_unlock (&traffic_mutex);
186 pthread_mutex_lock (&qtype_mutex);
187 counter_list_add (&qtype_list, dns->qtype, 1);
188 pthread_mutex_unlock (&qtype_mutex);
193 /* This is a reply */
194 pthread_mutex_lock (&traffic_mutex);
195 tr_responses += dns->length;
196 pthread_mutex_unlock (&traffic_mutex);
198 pthread_mutex_lock (&rcode_mutex);
199 counter_list_add (&rcode_list, dns->rcode, 1);
200 pthread_mutex_unlock (&rcode_mutex);
203 /* FIXME: Are queries, replies or both interesting? */
204 pthread_mutex_lock (&opcode_mutex);
205 counter_list_add (&opcode_list, dns->opcode, 1);
206 pthread_mutex_unlock (&opcode_mutex);
209 static int dns_run_pcap_loop (void)
212 char pcap_error[PCAP_ERRBUF_SIZE];
213 struct bpf_program fp;
217 /* Don't block any signals */
220 sigemptyset (&sigmask);
221 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
224 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
225 DEBUG ("dns plugin: Creating PCAP object..");
226 pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any",
228 0 /* Not promiscuous */,
229 (int) CDTIME_T_TO_MS (plugin_get_interval () / 2),
231 if (pcap_obj == NULL)
233 ERROR ("dns plugin: Opening interface `%s' "
235 (pcap_device != NULL) ? pcap_device : "any",
240 memset (&fp, 0, sizeof (fp));
241 status = pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0);
244 ERROR ("dns plugin: pcap_compile failed: %s",
245 pcap_statustostr (status));
249 status = pcap_setfilter (pcap_obj, &fp);
252 ERROR ("dns plugin: pcap_setfilter failed: %s",
253 pcap_statustostr (status));
257 DEBUG ("dns plugin: PCAP object created.");
259 dnstop_set_pcap_obj (pcap_obj);
260 dnstop_set_callback (dns_child_callback);
262 status = pcap_loop (pcap_obj,
263 -1 /* loop forever */,
264 handle_pcap /* callback */,
265 NULL /* user data */);
266 INFO ("dns plugin: pcap_loop exited with status %i.", status);
267 /* We need to handle "PCAP_ERROR" specially because libpcap currently
268 * doesn't return PCAP_ERROR_IFACE_NOT_UP for compatibility reasons. */
269 if (status == PCAP_ERROR)
270 status = PCAP_ERROR_IFACE_NOT_UP;
272 pcap_close (pcap_obj);
274 } /* int dns_run_pcap_loop */
276 static int dns_sleep_one_interval (void) /* {{{ */
279 struct timespec ts = { 0, 0 };
282 interval = plugin_get_interval ();
283 CDTIME_T_TO_TIMESPEC (interval, &ts);
287 struct timespec rem = { 0, 0 };
289 status = nanosleep (&ts, &rem);
292 else if ((errno == EINTR) || (errno == EAGAIN))
302 } /* }}} int dns_sleep_one_interval */
304 static void *dns_child_loop (__attribute__((unused)) void *dummy) /* {{{ */
310 status = dns_run_pcap_loop ();
311 if (status != PCAP_ERROR_IFACE_NOT_UP)
314 dns_sleep_one_interval ();
317 if (status != PCAP_ERROR_BREAK)
318 ERROR ("dns plugin: PCAP returned error %s.",
319 pcap_statustostr (status));
321 listen_thread_init = 0;
323 } /* }}} void *dns_child_loop */
325 static int dns_init (void)
327 /* clean up an old thread */
330 pthread_mutex_lock (&traffic_mutex);
333 pthread_mutex_unlock (&traffic_mutex);
335 if (listen_thread_init != 0)
338 status = plugin_thread_create (&listen_thread, NULL, dns_child_loop,
343 ERROR ("dns plugin: pthread_create failed: %s",
344 sstrerror (errno, errbuf, sizeof (errbuf)));
348 listen_thread_init = 1;
353 static void submit_derive (const char *type, const char *type_instance,
357 value_list_t vl = VALUE_LIST_INIT;
359 values[0].derive = value;
363 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
364 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
365 sstrncpy (vl.type, type, sizeof (vl.type));
366 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
368 plugin_dispatch_values (&vl);
369 } /* void submit_derive */
371 static void submit_octets (derive_t queries, derive_t responses)
374 value_list_t vl = VALUE_LIST_INIT;
376 values[0].derive = queries;
377 values[1].derive = responses;
381 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
382 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
383 sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
385 plugin_dispatch_values (&vl);
386 } /* void submit_octets */
388 static int dns_read (void)
390 unsigned int keys[T_MAX];
391 unsigned int values[T_MAX];
397 pthread_mutex_lock (&traffic_mutex);
398 values[0] = tr_queries;
399 values[1] = tr_responses;
400 pthread_mutex_unlock (&traffic_mutex);
402 if ((values[0] != 0) || (values[1] != 0))
403 submit_octets (values[0], values[1]);
405 pthread_mutex_lock (&qtype_mutex);
406 for (ptr = qtype_list, len = 0;
407 (ptr != NULL) && (len < T_MAX);
408 ptr = ptr->next, len++)
410 keys[len] = ptr->key;
411 values[len] = ptr->value;
413 pthread_mutex_unlock (&qtype_mutex);
415 for (i = 0; i < len; i++)
417 DEBUG ("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]);
418 submit_derive ("dns_qtype", qtype_str (keys[i]), values[i]);
421 pthread_mutex_lock (&opcode_mutex);
422 for (ptr = opcode_list, len = 0;
423 (ptr != NULL) && (len < T_MAX);
424 ptr = ptr->next, len++)
426 keys[len] = ptr->key;
427 values[len] = ptr->value;
429 pthread_mutex_unlock (&opcode_mutex);
431 for (i = 0; i < len; i++)
433 DEBUG ("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]);
434 submit_derive ("dns_opcode", opcode_str (keys[i]), values[i]);
437 pthread_mutex_lock (&rcode_mutex);
438 for (ptr = rcode_list, len = 0;
439 (ptr != NULL) && (len < T_MAX);
440 ptr = ptr->next, len++)
442 keys[len] = ptr->key;
443 values[len] = ptr->value;
445 pthread_mutex_unlock (&rcode_mutex);
447 for (i = 0; i < len; i++)
449 DEBUG ("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]);
450 submit_derive ("dns_rcode", rcode_str (keys[i]), values[i]);
456 void module_register (void)
458 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
459 plugin_register_init ("dns", dns_init);
460 plugin_register_read ("dns", dns_read);
461 } /* void module_register */