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"
46 struct counter_list_s *next;
48 typedef struct counter_list_s counter_list_t;
53 static const char *config_keys[] =
57 "SelectNumericQueryTypes"
59 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
60 static int select_numeric_qtype = 1;
62 #define PCAP_SNAPLEN 1460
63 static char *pcap_device = NULL;
65 static derive_t tr_queries;
66 static derive_t tr_responses;
67 static counter_list_t *qtype_list;
68 static counter_list_t *opcode_list;
69 static counter_list_t *rcode_list;
71 static pthread_t listen_thread;
72 static int listen_thread_init = 0;
73 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
74 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
75 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
76 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
77 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
82 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
84 counter_list_t *entry;
86 for (entry = *list; entry != NULL; entry = entry->next)
87 if (entry->key == key)
93 static counter_list_t *counter_list_create (counter_list_t **list,
94 unsigned int key, unsigned int value)
96 counter_list_t *entry;
98 entry = (counter_list_t *) malloc (sizeof (counter_list_t));
102 memset (entry, 0, sizeof (counter_list_t));
104 entry->value = value;
112 counter_list_t *last;
115 while (last->next != NULL)
124 static void counter_list_add (counter_list_t **list,
125 unsigned int key, unsigned int increment)
127 counter_list_t *entry;
129 entry = counter_list_search (list, key);
133 entry->value += increment;
137 counter_list_create (list, key, increment);
141 static int dns_config (const char *key, const char *value)
143 if (strcasecmp (key, "Interface") == 0)
145 if (pcap_device != NULL)
147 if ((pcap_device = strdup (value)) == NULL)
150 else if (strcasecmp (key, "IgnoreSource") == 0)
153 ignore_list_add_name (value);
155 else if (strcasecmp (key, "SelectNumericQueryTypes") == 0)
157 if ((value != NULL) && IS_FALSE (value))
158 select_numeric_qtype = 0;
160 select_numeric_qtype = 1;
170 static void dns_child_callback (const rfc1035_header_t *dns)
174 /* This is a query */
176 if (!select_numeric_qtype)
178 const char *str = qtype_str(dns->qtype);
179 if ((str == NULL) || (str[0] == '#'))
183 pthread_mutex_lock (&traffic_mutex);
184 tr_queries += dns->length;
185 pthread_mutex_unlock (&traffic_mutex);
189 pthread_mutex_lock (&qtype_mutex);
190 counter_list_add (&qtype_list, dns->qtype, 1);
191 pthread_mutex_unlock (&qtype_mutex);
196 /* This is a reply */
197 pthread_mutex_lock (&traffic_mutex);
198 tr_responses += dns->length;
199 pthread_mutex_unlock (&traffic_mutex);
201 pthread_mutex_lock (&rcode_mutex);
202 counter_list_add (&rcode_list, dns->rcode, 1);
203 pthread_mutex_unlock (&rcode_mutex);
206 /* FIXME: Are queries, replies or both interesting? */
207 pthread_mutex_lock (&opcode_mutex);
208 counter_list_add (&opcode_list, dns->opcode, 1);
209 pthread_mutex_unlock (&opcode_mutex);
212 static int dns_run_pcap_loop (void)
215 char pcap_error[PCAP_ERRBUF_SIZE];
216 struct bpf_program fp;
220 /* Don't block any signals */
223 sigemptyset (&sigmask);
224 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
227 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
228 DEBUG ("dns plugin: Creating PCAP object..");
229 pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any",
231 0 /* Not promiscuous */,
232 (int) CDTIME_T_TO_MS (plugin_get_interval () / 2),
234 if (pcap_obj == NULL)
236 ERROR ("dns plugin: Opening interface `%s' "
238 (pcap_device != NULL) ? pcap_device : "any",
243 memset (&fp, 0, sizeof (fp));
244 status = pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0);
247 ERROR ("dns plugin: pcap_compile failed: %s",
248 pcap_statustostr (status));
252 status = pcap_setfilter (pcap_obj, &fp);
255 ERROR ("dns plugin: pcap_setfilter failed: %s",
256 pcap_statustostr (status));
260 DEBUG ("dns plugin: PCAP object created.");
262 dnstop_set_pcap_obj (pcap_obj);
263 dnstop_set_callback (dns_child_callback);
265 status = pcap_loop (pcap_obj,
266 -1 /* loop forever */,
267 handle_pcap /* callback */,
268 NULL /* user data */);
269 INFO ("dns plugin: pcap_loop exited with status %i.", status);
270 /* We need to handle "PCAP_ERROR" specially because libpcap currently
271 * doesn't return PCAP_ERROR_IFACE_NOT_UP for compatibility reasons. */
272 if (status == PCAP_ERROR)
273 status = PCAP_ERROR_IFACE_NOT_UP;
275 pcap_close (pcap_obj);
277 } /* int dns_run_pcap_loop */
279 static int dns_sleep_one_interval (void) /* {{{ */
282 struct timespec ts = { 0, 0 };
285 interval = plugin_get_interval ();
286 CDTIME_T_TO_TIMESPEC (interval, &ts);
290 struct timespec rem = { 0, 0 };
292 status = nanosleep (&ts, &rem);
295 else if ((errno == EINTR) || (errno == EAGAIN))
305 } /* }}} int dns_sleep_one_interval */
307 static void *dns_child_loop (__attribute__((unused)) void *dummy) /* {{{ */
313 status = dns_run_pcap_loop ();
314 if (status != PCAP_ERROR_IFACE_NOT_UP)
317 dns_sleep_one_interval ();
320 if (status != PCAP_ERROR_BREAK)
321 ERROR ("dns plugin: PCAP returned error %s.",
322 pcap_statustostr (status));
324 listen_thread_init = 0;
326 } /* }}} void *dns_child_loop */
328 static int dns_init (void)
330 /* clean up an old thread */
333 pthread_mutex_lock (&traffic_mutex);
336 pthread_mutex_unlock (&traffic_mutex);
338 if (listen_thread_init != 0)
341 status = plugin_thread_create (&listen_thread, NULL, dns_child_loop,
346 ERROR ("dns plugin: pthread_create failed: %s",
347 sstrerror (errno, errbuf, sizeof (errbuf)));
351 listen_thread_init = 1;
356 static void submit_derive (const char *type, const char *type_instance,
360 value_list_t vl = VALUE_LIST_INIT;
362 values[0].derive = value;
366 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
367 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
368 sstrncpy (vl.type, type, sizeof (vl.type));
369 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
371 plugin_dispatch_values (&vl);
372 } /* void submit_derive */
374 static void submit_octets (derive_t queries, derive_t responses)
377 value_list_t vl = VALUE_LIST_INIT;
379 values[0].derive = queries;
380 values[1].derive = responses;
384 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
385 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
386 sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
388 plugin_dispatch_values (&vl);
389 } /* void submit_octets */
391 static int dns_read (void)
393 unsigned int keys[T_MAX];
394 unsigned int values[T_MAX];
400 pthread_mutex_lock (&traffic_mutex);
401 values[0] = tr_queries;
402 values[1] = tr_responses;
403 pthread_mutex_unlock (&traffic_mutex);
405 if ((values[0] != 0) || (values[1] != 0))
406 submit_octets (values[0], values[1]);
408 pthread_mutex_lock (&qtype_mutex);
409 for (ptr = qtype_list, len = 0;
410 (ptr != NULL) && (len < T_MAX);
411 ptr = ptr->next, len++)
413 keys[len] = ptr->key;
414 values[len] = ptr->value;
416 pthread_mutex_unlock (&qtype_mutex);
418 for (i = 0; i < len; i++)
420 DEBUG ("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]);
421 submit_derive ("dns_qtype", qtype_str (keys[i]), values[i]);
424 pthread_mutex_lock (&opcode_mutex);
425 for (ptr = opcode_list, len = 0;
426 (ptr != NULL) && (len < T_MAX);
427 ptr = ptr->next, len++)
429 keys[len] = ptr->key;
430 values[len] = ptr->value;
432 pthread_mutex_unlock (&opcode_mutex);
434 for (i = 0; i < len; i++)
436 DEBUG ("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]);
437 submit_derive ("dns_opcode", opcode_str (keys[i]), values[i]);
440 pthread_mutex_lock (&rcode_mutex);
441 for (ptr = rcode_list, len = 0;
442 (ptr != NULL) && (len < T_MAX);
443 ptr = ptr->next, len++)
445 keys[len] = ptr->key;
446 values[len] = ptr->value;
448 pthread_mutex_unlock (&rcode_mutex);
450 for (i = 0; i < len; i++)
452 DEBUG ("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]);
453 submit_derive ("dns_rcode", rcode_str (keys[i]), values[i]);
459 void module_register (void)
461 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
462 plugin_register_init ("dns", dns_init);
463 plugin_register_read ("dns", dns_read);
464 } /* void module_register */