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
31 #include "configfile.h"
33 #include "utils_dns.h"
38 #ifdef HAVE_SYS_CAPABILITY_H
39 # include <sys/capability.h>
49 struct counter_list_s *next;
51 typedef struct counter_list_s counter_list_t;
56 static const char *config_keys[] =
60 "SelectNumericQueryTypes"
62 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
63 static int select_numeric_qtype = 1;
65 #define PCAP_SNAPLEN 1460
66 static char *pcap_device = NULL;
68 static derive_t tr_queries;
69 static derive_t tr_responses;
70 static counter_list_t *qtype_list;
71 static counter_list_t *opcode_list;
72 static counter_list_t *rcode_list;
74 static pthread_t listen_thread;
75 static int listen_thread_init = 0;
76 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
77 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
78 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
79 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
80 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
85 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
87 counter_list_t *entry;
89 for (entry = *list; entry != NULL; entry = entry->next)
90 if (entry->key == key)
96 static counter_list_t *counter_list_create (counter_list_t **list,
97 unsigned int key, unsigned int value)
99 counter_list_t *entry;
101 entry = calloc (1, sizeof (*entry));
106 entry->value = value;
114 counter_list_t *last;
117 while (last->next != NULL)
126 static void counter_list_add (counter_list_t **list,
127 unsigned int key, unsigned int increment)
129 counter_list_t *entry;
131 entry = counter_list_search (list, key);
135 entry->value += increment;
139 counter_list_create (list, key, increment);
143 static int dns_config (const char *key, const char *value)
145 if (strcasecmp (key, "Interface") == 0)
147 if (pcap_device != NULL)
149 if ((pcap_device = strdup (value)) == NULL)
152 else if (strcasecmp (key, "IgnoreSource") == 0)
155 ignore_list_add_name (value);
157 else if (strcasecmp (key, "SelectNumericQueryTypes") == 0)
159 if ((value != NULL) && IS_FALSE (value))
160 select_numeric_qtype = 0;
162 select_numeric_qtype = 1;
172 static void dns_child_callback (const rfc1035_header_t *dns)
176 /* This is a query */
178 if (!select_numeric_qtype)
180 const char *str = qtype_str(dns->qtype);
181 if ((str == NULL) || (str[0] == '#'))
185 pthread_mutex_lock (&traffic_mutex);
186 tr_queries += dns->length;
187 pthread_mutex_unlock (&traffic_mutex);
191 pthread_mutex_lock (&qtype_mutex);
192 counter_list_add (&qtype_list, dns->qtype, 1);
193 pthread_mutex_unlock (&qtype_mutex);
198 /* This is a reply */
199 pthread_mutex_lock (&traffic_mutex);
200 tr_responses += dns->length;
201 pthread_mutex_unlock (&traffic_mutex);
203 pthread_mutex_lock (&rcode_mutex);
204 counter_list_add (&rcode_list, dns->rcode, 1);
205 pthread_mutex_unlock (&rcode_mutex);
208 /* FIXME: Are queries, replies or both interesting? */
209 pthread_mutex_lock (&opcode_mutex);
210 counter_list_add (&opcode_list, dns->opcode, 1);
211 pthread_mutex_unlock (&opcode_mutex);
214 static int dns_run_pcap_loop (void)
217 char pcap_error[PCAP_ERRBUF_SIZE];
218 struct bpf_program fp = { 0 };
222 /* Don't block any signals */
225 sigemptyset (&sigmask);
226 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
229 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
230 DEBUG ("dns plugin: Creating PCAP object..");
231 pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any",
233 0 /* Not promiscuous */,
234 (int) CDTIME_T_TO_MS (plugin_get_interval () / 2),
236 if (pcap_obj == NULL)
238 ERROR ("dns plugin: Opening interface `%s' "
240 (pcap_device != NULL) ? pcap_device : "any",
245 status = pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0);
248 ERROR ("dns plugin: pcap_compile failed: %s",
249 pcap_statustostr (status));
253 status = pcap_setfilter (pcap_obj, &fp);
256 ERROR ("dns plugin: pcap_setfilter failed: %s",
257 pcap_statustostr (status));
261 DEBUG ("dns plugin: PCAP object created.");
263 dnstop_set_pcap_obj (pcap_obj);
264 dnstop_set_callback (dns_child_callback);
266 status = pcap_loop (pcap_obj,
267 -1 /* loop forever */,
268 handle_pcap /* callback */,
269 NULL /* user data */);
270 INFO ("dns plugin: pcap_loop exited with status %i.", status);
271 /* We need to handle "PCAP_ERROR" specially because libpcap currently
272 * doesn't return PCAP_ERROR_IFACE_NOT_UP for compatibility reasons. */
273 if (status == PCAP_ERROR)
274 status = PCAP_ERROR_IFACE_NOT_UP;
276 pcap_close (pcap_obj);
278 } /* int dns_run_pcap_loop */
280 static int dns_sleep_one_interval (void) /* {{{ */
283 struct timespec ts = { 0, 0 };
286 interval = plugin_get_interval ();
287 CDTIME_T_TO_TIMESPEC (interval, &ts);
291 struct timespec rem = { 0, 0 };
293 status = nanosleep (&ts, &rem);
296 else if ((errno == EINTR) || (errno == EAGAIN))
306 } /* }}} int dns_sleep_one_interval */
308 static void *dns_child_loop (__attribute__((unused)) void *dummy) /* {{{ */
314 status = dns_run_pcap_loop ();
315 if (status != PCAP_ERROR_IFACE_NOT_UP)
318 dns_sleep_one_interval ();
321 if (status != PCAP_ERROR_BREAK)
322 ERROR ("dns plugin: PCAP returned error %s.",
323 pcap_statustostr (status));
325 listen_thread_init = 0;
327 } /* }}} void *dns_child_loop */
329 static int dns_init (void)
331 /* clean up an old thread */
334 pthread_mutex_lock (&traffic_mutex);
337 pthread_mutex_unlock (&traffic_mutex);
339 if (listen_thread_init != 0)
342 status = plugin_thread_create (&listen_thread, NULL, dns_child_loop,
347 ERROR ("dns plugin: pthread_create failed: %s",
348 sstrerror (errno, errbuf, sizeof (errbuf)));
352 listen_thread_init = 1;
354 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_RAW)
355 if (check_capability (CAP_NET_RAW) != 0)
358 WARNING ("dns plugin: Running collectd as root, but the CAP_NET_RAW "
359 "capability is missing. The plugin's read function will probably "
360 "fail. Is your init system dropping capabilities?");
362 WARNING ("dns plugin: collectd doesn't have the CAP_NET_RAW capability. "
363 "If you don't want to run collectd as root, try running \"setcap "
364 "cap_net_raw=ep\" on the collectd binary.");
371 static void submit_derive (const char *type, const char *type_instance,
375 value_list_t vl = VALUE_LIST_INIT;
377 values[0].derive = value;
381 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
382 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
383 sstrncpy (vl.type, type, sizeof (vl.type));
384 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
386 plugin_dispatch_values (&vl);
387 } /* void submit_derive */
389 static void submit_octets (derive_t queries, derive_t responses)
392 value_list_t vl = VALUE_LIST_INIT;
394 values[0].derive = queries;
395 values[1].derive = responses;
399 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
400 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
401 sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
403 plugin_dispatch_values (&vl);
404 } /* void submit_octets */
406 static int dns_read (void)
408 unsigned int keys[T_MAX];
409 unsigned int values[T_MAX];
414 pthread_mutex_lock (&traffic_mutex);
415 values[0] = tr_queries;
416 values[1] = tr_responses;
417 pthread_mutex_unlock (&traffic_mutex);
419 if ((values[0] != 0) || (values[1] != 0))
420 submit_octets (values[0], values[1]);
422 pthread_mutex_lock (&qtype_mutex);
423 for (ptr = qtype_list, len = 0;
424 (ptr != NULL) && (len < T_MAX);
425 ptr = ptr->next, len++)
427 keys[len] = ptr->key;
428 values[len] = ptr->value;
430 pthread_mutex_unlock (&qtype_mutex);
432 for (int i = 0; i < len; i++)
434 DEBUG ("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]);
435 submit_derive ("dns_qtype", qtype_str (keys[i]), values[i]);
438 pthread_mutex_lock (&opcode_mutex);
439 for (ptr = opcode_list, len = 0;
440 (ptr != NULL) && (len < T_MAX);
441 ptr = ptr->next, len++)
443 keys[len] = ptr->key;
444 values[len] = ptr->value;
446 pthread_mutex_unlock (&opcode_mutex);
448 for (int i = 0; i < len; i++)
450 DEBUG ("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]);
451 submit_derive ("dns_opcode", opcode_str (keys[i]), values[i]);
454 pthread_mutex_lock (&rcode_mutex);
455 for (ptr = rcode_list, len = 0;
456 (ptr != NULL) && (len < T_MAX);
457 ptr = ptr->next, len++)
459 keys[len] = ptr->key;
460 values[len] = ptr->value;
462 pthread_mutex_unlock (&rcode_mutex);
464 for (int i = 0; i < len; i++)
466 DEBUG ("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]);
467 submit_derive ("dns_rcode", rcode_str (keys[i]), values[i]);
473 void module_register (void)
475 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
476 plugin_register_init ("dns", dns_init);
477 plugin_register_read ("dns", dns_read);
478 } /* void module_register */