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
32 #include "utils_dns.h"
37 #ifdef HAVE_SYS_CAPABILITY_H
38 # include <sys/capability.h>
48 struct counter_list_s *next;
50 typedef struct counter_list_s counter_list_t;
55 static const char *config_keys[] =
59 "SelectNumericQueryTypes"
61 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
62 static int select_numeric_qtype = 1;
64 #define PCAP_SNAPLEN 1460
65 static char *pcap_device = NULL;
67 static derive_t tr_queries;
68 static derive_t tr_responses;
69 static counter_list_t *qtype_list;
70 static counter_list_t *opcode_list;
71 static counter_list_t *rcode_list;
73 static pthread_t listen_thread;
74 static int listen_thread_init = 0;
75 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
76 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
77 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
78 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
79 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
84 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
86 counter_list_t *entry;
88 for (entry = *list; entry != NULL; entry = entry->next)
89 if (entry->key == key)
95 static counter_list_t *counter_list_create (counter_list_t **list,
96 unsigned int key, unsigned int value)
98 counter_list_t *entry;
100 entry = calloc (1, sizeof (*entry));
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 int dns_run_pcap_loop (void)
216 char pcap_error[PCAP_ERRBUF_SIZE];
217 struct bpf_program fp = { 0 };
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 (plugin_get_interval () / 2),
235 if (pcap_obj == NULL)
237 ERROR ("dns plugin: Opening interface `%s' "
239 (pcap_device != NULL) ? pcap_device : "any",
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;
353 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_RAW)
354 if (check_capability (CAP_NET_RAW) != 0)
357 WARNING ("dns plugin: Running collectd as root, but the CAP_NET_RAW "
358 "capability is missing. The plugin's read function will probably "
359 "fail. Is your init system dropping capabilities?");
361 WARNING ("dns plugin: collectd doesn't have the CAP_NET_RAW capability. "
362 "If you don't want to run collectd as root, try running \"setcap "
363 "cap_net_raw=ep\" on the collectd binary.");
370 static void submit_derive (const char *type, const char *type_instance,
374 value_list_t vl = VALUE_LIST_INIT;
376 values[0].derive = value;
380 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
381 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
382 sstrncpy (vl.type, type, sizeof (vl.type));
383 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
385 plugin_dispatch_values (&vl);
386 } /* void submit_derive */
388 static void submit_octets (derive_t queries, derive_t responses)
391 value_list_t vl = VALUE_LIST_INIT;
393 values[0].derive = queries;
394 values[1].derive = responses;
398 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
399 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
400 sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
402 plugin_dispatch_values (&vl);
403 } /* void submit_octets */
405 static int dns_read (void)
407 unsigned int keys[T_MAX];
408 unsigned int values[T_MAX];
413 pthread_mutex_lock (&traffic_mutex);
414 values[0] = tr_queries;
415 values[1] = tr_responses;
416 pthread_mutex_unlock (&traffic_mutex);
418 if ((values[0] != 0) || (values[1] != 0))
419 submit_octets (values[0], values[1]);
421 pthread_mutex_lock (&qtype_mutex);
422 for (ptr = qtype_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 (&qtype_mutex);
431 for (int i = 0; i < len; i++)
433 DEBUG ("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]);
434 submit_derive ("dns_qtype", qtype_str (keys[i]), values[i]);
437 pthread_mutex_lock (&opcode_mutex);
438 for (ptr = opcode_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 (&opcode_mutex);
447 for (int i = 0; i < len; i++)
449 DEBUG ("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]);
450 submit_derive ("dns_opcode", opcode_str (keys[i]), values[i]);
453 pthread_mutex_lock (&rcode_mutex);
454 for (ptr = rcode_list, len = 0;
455 (ptr != NULL) && (len < T_MAX);
456 ptr = ptr->next, len++)
458 keys[len] = ptr->key;
459 values[len] = ptr->value;
461 pthread_mutex_unlock (&rcode_mutex);
463 for (int i = 0; i < len; i++)
465 DEBUG ("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]);
466 submit_derive ("dns_rcode", rcode_str (keys[i]), values[i]);
472 void module_register (void)
474 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
475 plugin_register_init ("dns", dns_init);
476 plugin_register_read ("dns", dns_read);
477 } /* void module_register */