3 * Copyright (C) 2006,2007 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
25 #include "configfile.h"
27 #if HAVE_LIBPCAP && HAVE_LIBPTHREAD
28 # include "utils_dns.h"
32 # define DNS_HAVE_READ 1
34 # define DNS_HAVE_READ 0
45 struct counter_list_s *next;
47 typedef struct counter_list_s counter_list_t;
53 static data_source_t octets_dsrc[2] =
55 {"queries", DS_TYPE_COUNTER, 0, 125000000.0},
56 {"responses", DS_TYPE_COUNTER, 0, 125000000.0}
59 static data_set_t octets_ds =
61 "dns_octets", 2, octets_dsrc
64 static data_source_t counter_dsrc[1] =
66 {"value", DS_TYPE_COUNTER, 0, 65535.0}
69 static data_set_t qtype_ds =
71 "dns_qtype", 1, counter_dsrc
74 static data_set_t opcode_ds =
76 "dns_opcode", 1, counter_dsrc
79 static data_set_t rcode_ds =
81 "dns_rcode", 1, counter_dsrc
85 static const char *config_keys[] =
91 static int config_keys_num = 2;
93 #define PCAP_SNAPLEN 1460
94 static char *pcap_device = NULL;
96 static counter_t tr_queries;
97 static counter_t tr_responses;
98 static counter_list_t *qtype_list;
99 static counter_list_t *opcode_list;
100 static counter_list_t *rcode_list;
102 static pthread_t listen_thread;
103 static int listen_thread_init = 0;
104 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
105 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
106 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
107 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
108 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
109 #endif /* DNS_HAVE_READ */
115 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
117 counter_list_t *entry;
119 DEBUG ("counter_list_search (list = %p, key = %u)",
120 (void *) *list, key);
122 for (entry = *list; entry != NULL; entry = entry->next)
123 if (entry->key == key)
126 DEBUG ("return (%p)", (void *) entry);
130 static counter_list_t *counter_list_create (counter_list_t **list,
131 unsigned int key, unsigned int value)
133 counter_list_t *entry;
135 DEBUG ("counter_list_create (list = %p, key = %u, value = %u)",
136 (void *) *list, key, value);
138 entry = (counter_list_t *) malloc (sizeof (counter_list_t));
142 memset (entry, 0, sizeof (counter_list_t));
144 entry->value = value;
152 counter_list_t *last;
155 while (last->next != NULL)
161 DEBUG ("return (%p)", (void *) entry);
165 static void counter_list_add (counter_list_t **list,
166 unsigned int key, unsigned int increment)
168 counter_list_t *entry;
170 DEBUG ("counter_list_add (list = %p, key = %u, increment = %u)",
171 (void *) *list, key, increment);
173 entry = counter_list_search (list, key);
177 entry->value += increment;
181 counter_list_create (list, key, increment);
186 static int dns_config (const char *key, const char *value)
188 if (strcasecmp (key, "Interface") == 0)
190 if (pcap_device != NULL)
192 if ((pcap_device = strdup (value)) == NULL)
195 else if (strcasecmp (key, "IgnoreSource") == 0)
198 ignore_list_add_name (value);
208 static void dns_child_callback (const rfc1035_header_t *dns)
212 /* This is a query */
213 pthread_mutex_lock (&traffic_mutex);
214 tr_queries += dns->length;
215 pthread_mutex_unlock (&traffic_mutex);
217 pthread_mutex_lock (&qtype_mutex);
218 counter_list_add (&qtype_list, dns->qtype, 1);
219 pthread_mutex_unlock (&qtype_mutex);
223 /* This is a reply */
224 pthread_mutex_lock (&traffic_mutex);
225 tr_responses += dns->length;
226 pthread_mutex_unlock (&traffic_mutex);
228 pthread_mutex_lock (&rcode_mutex);
229 counter_list_add (&rcode_list, dns->rcode, 1);
230 pthread_mutex_unlock (&rcode_mutex);
233 /* FIXME: Are queries, replies or both interesting? */
234 pthread_mutex_lock (&opcode_mutex);
235 counter_list_add (&opcode_list, dns->opcode, 1);
236 pthread_mutex_unlock (&opcode_mutex);
239 static void *dns_child_loop (void *dummy)
242 char pcap_error[PCAP_ERRBUF_SIZE];
243 struct bpf_program fp;
247 /* Don't block any signals */
250 sigemptyset (&sigmask);
251 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
254 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
255 DEBUG ("Creating PCAP object..");
256 pcap_obj = pcap_open_live (pcap_device,
258 0 /* Not promiscuous */,
259 atoi (COLLECTD_STEP),
261 if (pcap_obj == NULL)
263 ERROR ("dns plugin: Opening interface `%s' "
265 (pcap_device != NULL) ? pcap_device : "any",
270 memset (&fp, 0, sizeof (fp));
271 if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
273 DEBUG ("pcap_compile failed");
274 ERROR ("dns plugin: pcap_compile failed");
277 if (pcap_setfilter (pcap_obj, &fp) < 0)
279 DEBUG ("pcap_setfilter failed");
280 ERROR ("dns plugin: pcap_setfilter failed");
284 DEBUG ("PCAP object created.");
286 dnstop_set_pcap_obj (pcap_obj);
287 dnstop_set_callback (dns_child_callback);
289 status = pcap_loop (pcap_obj,
290 -1 /* loop forever */,
291 handle_pcap /* callback */,
292 NULL /* Whatever this means.. */);
294 ERROR ("dns plugin: Listener thread is exiting "
295 "abnormally: %s", pcap_geterr (pcap_obj));
297 DEBUG ("child is exiting");
299 pcap_close (pcap_obj);
300 listen_thread_init = 0;
304 } /* static void dns_child_loop (void) */
306 static int dns_init (void)
308 /* clean up an old thread */
311 pthread_mutex_lock (&traffic_mutex);
314 pthread_mutex_unlock (&traffic_mutex);
316 if (listen_thread_init != 0)
319 status = pthread_create (&listen_thread, NULL, dns_child_loop,
324 ERROR ("dns plugin: pthread_create failed: %s",
325 sstrerror (errno, errbuf, sizeof (errbuf)));
329 listen_thread_init = 1;
334 static void submit_counter (const char *type, const char *type_instance,
338 value_list_t vl = VALUE_LIST_INIT;
340 values[0].counter = value;
344 vl.time = time (NULL);
345 strcpy (vl.host, hostname_g);
346 strcpy (vl.plugin, "dns");
347 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
349 plugin_dispatch_values (type, &vl);
350 } /* void submit_counter */
352 static void submit_octets (counter_t queries, counter_t responses)
355 value_list_t vl = VALUE_LIST_INIT;
357 values[0].counter = queries;
358 values[1].counter = responses;
362 vl.time = time (NULL);
363 strcpy (vl.host, hostname_g);
364 strcpy (vl.plugin, "dns");
366 plugin_dispatch_values ("dns_octets", &vl);
367 } /* void submit_counter */
369 static int dns_read (void)
371 unsigned int keys[T_MAX];
372 unsigned int values[T_MAX];
378 pthread_mutex_lock (&traffic_mutex);
379 values[0] = tr_queries;
380 values[1] = tr_responses;
381 pthread_mutex_unlock (&traffic_mutex);
382 submit_octets (values[0], values[1]);
384 pthread_mutex_lock (&qtype_mutex);
385 for (ptr = qtype_list, len = 0;
386 (ptr != NULL) && (len < T_MAX);
387 ptr = ptr->next, len++)
389 keys[len] = ptr->key;
390 values[len] = ptr->value;
392 pthread_mutex_unlock (&qtype_mutex);
394 for (i = 0; i < len; i++)
396 DEBUG ("qtype = %u; counter = %u;", keys[i], values[i]);
397 submit_counter ("dns_qtype", qtype_str (keys[i]), values[i]);
400 pthread_mutex_lock (&opcode_mutex);
401 for (ptr = opcode_list, len = 0;
402 (ptr != NULL) && (len < T_MAX);
403 ptr = ptr->next, len++)
405 keys[len] = ptr->key;
406 values[len] = ptr->value;
408 pthread_mutex_unlock (&opcode_mutex);
410 for (i = 0; i < len; i++)
412 DEBUG ("opcode = %u; counter = %u;", keys[i], values[i]);
413 submit_counter ("dns_opcode", opcode_str (keys[i]), values[i]);
416 pthread_mutex_lock (&rcode_mutex);
417 for (ptr = rcode_list, len = 0;
418 (ptr != NULL) && (len < T_MAX);
419 ptr = ptr->next, len++)
421 keys[len] = ptr->key;
422 values[len] = ptr->value;
424 pthread_mutex_unlock (&rcode_mutex);
426 for (i = 0; i < len; i++)
428 DEBUG ("rcode = %u; counter = %u;", keys[i], values[i]);
429 submit_counter ("dns_rcode", rcode_str (keys[i]), values[i]);
436 void module_register (void)
438 plugin_register_data_set (&octets_ds);
439 plugin_register_data_set (&qtype_ds);
440 plugin_register_data_set (&opcode_ds);
441 plugin_register_data_set (&rcode_ds);
444 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
445 plugin_register_init ("dns", dns_init);
446 plugin_register_read ("dns", dns_read);
448 } /* void module_register */