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
33 #include "utils_dns.h"
37 #ifdef HAVE_SYS_CAPABILITY_H
38 #include <sys/capability.h>
44 struct counter_list_s {
47 struct counter_list_s *next;
49 typedef struct counter_list_s counter_list_t;
54 static const char *config_keys[] = {"Interface", "IgnoreSource",
55 "SelectNumericQueryTypes"};
56 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
57 static int select_numeric_qtype = 1;
59 #define PCAP_SNAPLEN 1460
60 static char *pcap_device = NULL;
62 static derive_t tr_queries;
63 static derive_t tr_responses;
64 static counter_list_t *qtype_list;
65 static counter_list_t *opcode_list;
66 static counter_list_t *rcode_list;
68 static pthread_t listen_thread;
69 static int listen_thread_init = 0;
70 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
71 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
72 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
73 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
74 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
79 static counter_list_t *counter_list_search(counter_list_t **list,
81 counter_list_t *entry;
83 for (entry = *list; entry != NULL; entry = entry->next)
84 if (entry->key == key)
90 static counter_list_t *counter_list_create(counter_list_t **list,
93 counter_list_t *entry;
95 entry = calloc(1, sizeof(*entry));
100 entry->value = value;
105 counter_list_t *last;
108 while (last->next != NULL)
117 static void counter_list_add(counter_list_t **list, unsigned int key,
118 unsigned int increment) {
119 counter_list_t *entry;
121 entry = counter_list_search(list, key);
124 entry->value += increment;
126 counter_list_create(list, key, increment);
130 static int dns_config(const char *key, const char *value) {
131 if (strcasecmp(key, "Interface") == 0) {
132 if (pcap_device != NULL)
134 if ((pcap_device = strdup(value)) == NULL)
136 } else if (strcasecmp(key, "IgnoreSource") == 0) {
138 ignore_list_add_name(value);
139 } else if (strcasecmp(key, "SelectNumericQueryTypes") == 0) {
140 if ((value != NULL) && IS_FALSE(value))
141 select_numeric_qtype = 0;
143 select_numeric_qtype = 1;
151 static void dns_child_callback(const rfc1035_header_t *dns) {
153 /* This is a query */
155 if (!select_numeric_qtype) {
156 const char *str = qtype_str(dns->qtype);
157 if ((str == NULL) || (str[0] == '#'))
161 pthread_mutex_lock(&traffic_mutex);
162 tr_queries += dns->length;
163 pthread_mutex_unlock(&traffic_mutex);
166 pthread_mutex_lock(&qtype_mutex);
167 counter_list_add(&qtype_list, dns->qtype, 1);
168 pthread_mutex_unlock(&qtype_mutex);
171 /* This is a reply */
172 pthread_mutex_lock(&traffic_mutex);
173 tr_responses += dns->length;
174 pthread_mutex_unlock(&traffic_mutex);
176 pthread_mutex_lock(&rcode_mutex);
177 counter_list_add(&rcode_list, dns->rcode, 1);
178 pthread_mutex_unlock(&rcode_mutex);
181 /* FIXME: Are queries, replies or both interesting? */
182 pthread_mutex_lock(&opcode_mutex);
183 counter_list_add(&opcode_list, dns->opcode, 1);
184 pthread_mutex_unlock(&opcode_mutex);
187 static int dns_run_pcap_loop(void) {
189 char pcap_error[PCAP_ERRBUF_SIZE];
190 struct bpf_program fp = {0};
194 /* Don't block any signals */
197 sigemptyset(&sigmask);
198 pthread_sigmask(SIG_SETMASK, &sigmask, NULL);
201 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
202 DEBUG("dns plugin: Creating PCAP object..");
203 pcap_obj = pcap_open_live((pcap_device != NULL) ? pcap_device : "any",
204 PCAP_SNAPLEN, 0 /* Not promiscuous */,
205 (int)CDTIME_T_TO_MS(plugin_get_interval() / 2),
207 if (pcap_obj == NULL) {
208 ERROR("dns plugin: Opening interface `%s' "
210 (pcap_device != NULL) ? pcap_device : "any", pcap_error);
214 status = pcap_compile(pcap_obj, &fp, "udp port 53", 1, 0);
216 ERROR("dns plugin: pcap_compile failed: %s", pcap_statustostr(status));
220 status = pcap_setfilter(pcap_obj, &fp);
222 ERROR("dns plugin: pcap_setfilter failed: %s", pcap_statustostr(status));
226 DEBUG("dns plugin: PCAP object created.");
228 dnstop_set_pcap_obj(pcap_obj);
229 dnstop_set_callback(dns_child_callback);
231 status = pcap_loop(pcap_obj, -1 /* loop forever */,
232 handle_pcap /* callback */, NULL /* user data */);
233 INFO("dns plugin: pcap_loop exited with status %i.", status);
234 /* We need to handle "PCAP_ERROR" specially because libpcap currently
235 * doesn't return PCAP_ERROR_IFACE_NOT_UP for compatibility reasons. */
236 if (status == PCAP_ERROR)
237 status = PCAP_ERROR_IFACE_NOT_UP;
239 pcap_close(pcap_obj);
241 } /* int dns_run_pcap_loop */
243 static int dns_sleep_one_interval(void) /* {{{ */
245 struct timespec ts = CDTIME_T_TO_TIMESPEC(plugin_get_interval());
246 while (nanosleep(&ts, &ts) != 0) {
247 if ((errno == EINTR) || (errno == EAGAIN))
254 } /* }}} int dns_sleep_one_interval */
256 static void *dns_child_loop(__attribute__((unused)) void *dummy) /* {{{ */
261 status = dns_run_pcap_loop();
262 if (status != PCAP_ERROR_IFACE_NOT_UP)
265 dns_sleep_one_interval();
268 if (status != PCAP_ERROR_BREAK)
269 ERROR("dns plugin: PCAP returned error %s.", pcap_statustostr(status));
271 listen_thread_init = 0;
273 } /* }}} void *dns_child_loop */
275 static int dns_init(void) {
276 /* clean up an old thread */
279 pthread_mutex_lock(&traffic_mutex);
282 pthread_mutex_unlock(&traffic_mutex);
284 if (listen_thread_init != 0)
287 status = plugin_thread_create(&listen_thread, NULL, dns_child_loop, (void *)0,
291 ERROR("dns plugin: pthread_create failed: %s",
292 sstrerror(errno, errbuf, sizeof(errbuf)));
296 listen_thread_init = 1;
298 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_RAW)
299 if (check_capability(CAP_NET_RAW) != 0) {
301 WARNING("dns plugin: Running collectd as root, but the CAP_NET_RAW "
302 "capability is missing. The plugin's read function will probably "
303 "fail. Is your init system dropping capabilities?");
305 WARNING("dns plugin: collectd doesn't have the CAP_NET_RAW capability. "
306 "If you don't want to run collectd as root, try running \"setcap "
307 "cap_net_raw=ep\" on the collectd binary.");
314 static void submit_derive(const char *type, const char *type_instance,
316 value_list_t vl = VALUE_LIST_INIT;
318 vl.values = &(value_t){.derive = value};
320 sstrncpy(vl.plugin, "dns", sizeof(vl.plugin));
321 sstrncpy(vl.type, type, sizeof(vl.type));
322 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
324 plugin_dispatch_values(&vl);
325 } /* void submit_derive */
327 static void submit_octets(derive_t queries, derive_t responses) {
329 {.derive = queries}, {.derive = responses},
331 value_list_t vl = VALUE_LIST_INIT;
334 vl.values_len = STATIC_ARRAY_SIZE(values);
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) {
342 unsigned int keys[T_MAX];
343 unsigned int values[T_MAX];
348 pthread_mutex_lock(&traffic_mutex);
349 values[0] = tr_queries;
350 values[1] = tr_responses;
351 pthread_mutex_unlock(&traffic_mutex);
353 if ((values[0] != 0) || (values[1] != 0))
354 submit_octets(values[0], values[1]);
356 pthread_mutex_lock(&qtype_mutex);
357 for (ptr = qtype_list, len = 0; (ptr != NULL) && (len < T_MAX);
358 ptr = ptr->next, len++) {
359 keys[len] = ptr->key;
360 values[len] = ptr->value;
362 pthread_mutex_unlock(&qtype_mutex);
364 for (int i = 0; i < len; i++) {
365 DEBUG("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]);
366 submit_derive("dns_qtype", qtype_str(keys[i]), values[i]);
369 pthread_mutex_lock(&opcode_mutex);
370 for (ptr = opcode_list, len = 0; (ptr != NULL) && (len < T_MAX);
371 ptr = ptr->next, len++) {
372 keys[len] = ptr->key;
373 values[len] = ptr->value;
375 pthread_mutex_unlock(&opcode_mutex);
377 for (int i = 0; i < len; i++) {
378 DEBUG("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]);
379 submit_derive("dns_opcode", opcode_str(keys[i]), values[i]);
382 pthread_mutex_lock(&rcode_mutex);
383 for (ptr = rcode_list, len = 0; (ptr != NULL) && (len < T_MAX);
384 ptr = ptr->next, len++) {
385 keys[len] = ptr->key;
386 values[len] = ptr->value;
388 pthread_mutex_unlock(&rcode_mutex);
390 for (int i = 0; i < len; i++) {
391 DEBUG("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]);
392 submit_derive("dns_rcode", rcode_str(keys[i]), values[i]);
398 void module_register(void) {
399 plugin_register_config("dns", dns_config, config_keys, config_keys_num);
400 plugin_register_init("dns", dns_init);
401 plugin_register_read("dns", dns_read);
402 } /* void module_register */