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"
45 struct counter_list_s *next;
47 typedef struct counter_list_s counter_list_t;
52 static const char *config_keys[] =
56 "SelectNumericQueryTypes"
58 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
59 static int select_numeric_qtype = 1;
61 #define PCAP_SNAPLEN 1460
62 static char *pcap_device = NULL;
64 static derive_t tr_queries;
65 static derive_t tr_responses;
66 static counter_list_t *qtype_list;
67 static counter_list_t *opcode_list;
68 static counter_list_t *rcode_list;
70 static pthread_t listen_thread;
71 static int listen_thread_init = 0;
72 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
73 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
74 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
75 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
76 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
81 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
83 counter_list_t *entry;
85 for (entry = *list; entry != NULL; entry = entry->next)
86 if (entry->key == key)
92 static counter_list_t *counter_list_create (counter_list_t **list,
93 unsigned int key, unsigned int value)
95 counter_list_t *entry;
97 entry = calloc (1, sizeof (*entry));
102 entry->value = value;
110 counter_list_t *last;
113 while (last->next != NULL)
122 static void counter_list_add (counter_list_t **list,
123 unsigned int key, unsigned int increment)
125 counter_list_t *entry;
127 entry = counter_list_search (list, key);
131 entry->value += increment;
135 counter_list_create (list, key, increment);
139 static int dns_config (const char *key, const char *value)
141 if (strcasecmp (key, "Interface") == 0)
143 if (pcap_device != NULL)
145 if ((pcap_device = strdup (value)) == NULL)
148 else if (strcasecmp (key, "IgnoreSource") == 0)
151 ignore_list_add_name (value);
153 else if (strcasecmp (key, "SelectNumericQueryTypes") == 0)
155 if ((value != NULL) && IS_FALSE (value))
156 select_numeric_qtype = 0;
158 select_numeric_qtype = 1;
168 static void dns_child_callback (const rfc1035_header_t *dns)
172 /* This is a query */
174 if (!select_numeric_qtype)
176 const char *str = qtype_str(dns->qtype);
177 if ((str == NULL) || (str[0] == '#'))
181 pthread_mutex_lock (&traffic_mutex);
182 tr_queries += dns->length;
183 pthread_mutex_unlock (&traffic_mutex);
187 pthread_mutex_lock (&qtype_mutex);
188 counter_list_add (&qtype_list, dns->qtype, 1);
189 pthread_mutex_unlock (&qtype_mutex);
194 /* This is a reply */
195 pthread_mutex_lock (&traffic_mutex);
196 tr_responses += dns->length;
197 pthread_mutex_unlock (&traffic_mutex);
199 pthread_mutex_lock (&rcode_mutex);
200 counter_list_add (&rcode_list, dns->rcode, 1);
201 pthread_mutex_unlock (&rcode_mutex);
204 /* FIXME: Are queries, replies or both interesting? */
205 pthread_mutex_lock (&opcode_mutex);
206 counter_list_add (&opcode_list, dns->opcode, 1);
207 pthread_mutex_unlock (&opcode_mutex);
210 static int dns_run_pcap_loop (void)
213 char pcap_error[PCAP_ERRBUF_SIZE];
214 struct bpf_program fp;
218 /* Don't block any signals */
221 sigemptyset (&sigmask);
222 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
225 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
226 DEBUG ("dns plugin: Creating PCAP object..");
227 pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any",
229 0 /* Not promiscuous */,
230 (int) CDTIME_T_TO_MS (plugin_get_interval () / 2),
232 if (pcap_obj == NULL)
234 ERROR ("dns plugin: Opening interface `%s' "
236 (pcap_device != NULL) ? pcap_device : "any",
241 memset (&fp, 0, sizeof (fp));
242 status = pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0);
245 ERROR ("dns plugin: pcap_compile failed: %s",
246 pcap_statustostr (status));
250 status = pcap_setfilter (pcap_obj, &fp);
253 ERROR ("dns plugin: pcap_setfilter failed: %s",
254 pcap_statustostr (status));
258 DEBUG ("dns plugin: PCAP object created.");
260 dnstop_set_pcap_obj (pcap_obj);
261 dnstop_set_callback (dns_child_callback);
263 status = pcap_loop (pcap_obj,
264 -1 /* loop forever */,
265 handle_pcap /* callback */,
266 NULL /* user data */);
267 INFO ("dns plugin: pcap_loop exited with status %i.", status);
268 /* We need to handle "PCAP_ERROR" specially because libpcap currently
269 * doesn't return PCAP_ERROR_IFACE_NOT_UP for compatibility reasons. */
270 if (status == PCAP_ERROR)
271 status = PCAP_ERROR_IFACE_NOT_UP;
273 pcap_close (pcap_obj);
275 } /* int dns_run_pcap_loop */
277 static int dns_sleep_one_interval (void) /* {{{ */
280 struct timespec ts = { 0, 0 };
283 interval = plugin_get_interval ();
284 CDTIME_T_TO_TIMESPEC (interval, &ts);
288 struct timespec rem = { 0, 0 };
290 status = nanosleep (&ts, &rem);
293 else if ((errno == EINTR) || (errno == EAGAIN))
303 } /* }}} int dns_sleep_one_interval */
305 static void *dns_child_loop (__attribute__((unused)) void *dummy) /* {{{ */
311 status = dns_run_pcap_loop ();
312 if (status != PCAP_ERROR_IFACE_NOT_UP)
315 dns_sleep_one_interval ();
318 if (status != PCAP_ERROR_BREAK)
319 ERROR ("dns plugin: PCAP returned error %s.",
320 pcap_statustostr (status));
322 listen_thread_init = 0;
324 } /* }}} void *dns_child_loop */
326 static int dns_init (void)
328 /* clean up an old thread */
331 pthread_mutex_lock (&traffic_mutex);
334 pthread_mutex_unlock (&traffic_mutex);
336 if (listen_thread_init != 0)
339 status = plugin_thread_create (&listen_thread, NULL, dns_child_loop,
344 ERROR ("dns plugin: pthread_create failed: %s",
345 sstrerror (errno, errbuf, sizeof (errbuf)));
349 listen_thread_init = 1;
354 static void submit_derive (const char *type, const char *type_instance,
358 value_list_t vl = VALUE_LIST_INIT;
360 values[0].derive = value;
364 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
365 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
366 sstrncpy (vl.type, type, sizeof (vl.type));
367 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
369 plugin_dispatch_values (&vl);
370 } /* void submit_derive */
372 static void submit_octets (derive_t queries, derive_t responses)
375 value_list_t vl = VALUE_LIST_INIT;
377 values[0].derive = queries;
378 values[1].derive = responses;
382 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
383 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
384 sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
386 plugin_dispatch_values (&vl);
387 } /* void submit_octets */
389 static int dns_read (void)
391 unsigned int keys[T_MAX];
392 unsigned int values[T_MAX];
398 pthread_mutex_lock (&traffic_mutex);
399 values[0] = tr_queries;
400 values[1] = tr_responses;
401 pthread_mutex_unlock (&traffic_mutex);
403 if ((values[0] != 0) || (values[1] != 0))
404 submit_octets (values[0], values[1]);
406 pthread_mutex_lock (&qtype_mutex);
407 for (ptr = qtype_list, len = 0;
408 (ptr != NULL) && (len < T_MAX);
409 ptr = ptr->next, len++)
411 keys[len] = ptr->key;
412 values[len] = ptr->value;
414 pthread_mutex_unlock (&qtype_mutex);
416 for (i = 0; i < len; i++)
418 DEBUG ("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]);
419 submit_derive ("dns_qtype", qtype_str (keys[i]), values[i]);
422 pthread_mutex_lock (&opcode_mutex);
423 for (ptr = opcode_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 (&opcode_mutex);
432 for (i = 0; i < len; i++)
434 DEBUG ("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]);
435 submit_derive ("dns_opcode", opcode_str (keys[i]), values[i]);
438 pthread_mutex_lock (&rcode_mutex);
439 for (ptr = rcode_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 (&rcode_mutex);
448 for (i = 0; i < len; i++)
450 DEBUG ("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]);
451 submit_derive ("dns_rcode", rcode_str (keys[i]), values[i]);
457 void module_register (void)
459 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
460 plugin_register_init ("dns", dns_init);
461 plugin_register_read ("dns", dns_read);
462 } /* void module_register */