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"
26 #include "utils_debug.h"
28 #if HAVE_LIBPCAP && HAVE_LIBPTHREAD
29 # include "utils_dns.h"
33 # define DNS_HAVE_READ 1
35 # define DNS_HAVE_READ 0
46 struct counter_list_s *next;
48 typedef struct counter_list_s counter_list_t;
54 static data_source_t octets_dsrc[2] =
56 {"queries", DS_TYPE_COUNTER, 0, 125000000.0},
57 {"responses", DS_TYPE_COUNTER, 0, 125000000.0}
60 static data_set_t octets_ds =
62 "dns_octets", 2, octets_dsrc
65 static data_source_t counter_dsrc[1] =
67 {"value", DS_TYPE_COUNTER, 0, 65535.0}
70 static data_set_t qtype_ds =
72 "dns_qtype", 1, counter_dsrc
75 static data_set_t opcode_ds =
77 "dns_opcode", 1, counter_dsrc
80 static data_set_t rcode_ds =
82 "dns_rcode", 1, counter_dsrc
86 static const char *config_keys[] =
92 static int config_keys_num = 2;
94 #define PCAP_SNAPLEN 1460
95 static char *pcap_device = NULL;
97 static counter_t tr_queries;
98 static counter_t tr_responses;
99 static counter_list_t *qtype_list;
100 static counter_list_t *opcode_list;
101 static counter_list_t *rcode_list;
103 static pthread_t listen_thread;
104 static int listen_thread_init = 0;
105 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
106 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
107 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
108 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
109 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
110 #endif /* DNS_HAVE_READ */
116 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
118 counter_list_t *entry;
120 DBG ("counter_list_search (list = %p, key = %u)",
121 (void *) *list, key);
123 for (entry = *list; entry != NULL; entry = entry->next)
124 if (entry->key == key)
127 DBG ("return (%p)", (void *) entry);
131 static counter_list_t *counter_list_create (counter_list_t **list,
132 unsigned int key, unsigned int value)
134 counter_list_t *entry;
136 DBG ("counter_list_create (list = %p, key = %u, value = %u)",
137 (void *) *list, key, value);
139 entry = (counter_list_t *) malloc (sizeof (counter_list_t));
143 memset (entry, 0, sizeof (counter_list_t));
145 entry->value = value;
153 counter_list_t *last;
156 while (last->next != NULL)
162 DBG ("return (%p)", (void *) entry);
166 static void counter_list_add (counter_list_t **list,
167 unsigned int key, unsigned int increment)
169 counter_list_t *entry;
171 DBG ("counter_list_add (list = %p, key = %u, increment = %u)",
172 (void *) *list, key, increment);
174 entry = counter_list_search (list, key);
178 entry->value += increment;
182 counter_list_create (list, key, increment);
187 static int dns_config (const char *key, const char *value)
189 if (strcasecmp (key, "Interface") == 0)
191 if (pcap_device != NULL)
193 if ((pcap_device = strdup (value)) == NULL)
196 else if (strcasecmp (key, "IgnoreSource") == 0)
199 ignore_list_add_name (value);
209 static void dns_child_callback (const rfc1035_header_t *dns)
213 /* This is a query */
214 pthread_mutex_lock (&traffic_mutex);
215 tr_queries += dns->length;
216 pthread_mutex_unlock (&traffic_mutex);
218 pthread_mutex_lock (&qtype_mutex);
219 counter_list_add (&qtype_list, dns->qtype, 1);
220 pthread_mutex_unlock (&qtype_mutex);
224 /* This is a reply */
225 pthread_mutex_lock (&traffic_mutex);
226 tr_responses += dns->length;
227 pthread_mutex_unlock (&traffic_mutex);
229 pthread_mutex_lock (&rcode_mutex);
230 counter_list_add (&rcode_list, dns->rcode, 1);
231 pthread_mutex_unlock (&rcode_mutex);
234 /* FIXME: Are queries, replies or both interesting? */
235 pthread_mutex_lock (&opcode_mutex);
236 counter_list_add (&opcode_list, dns->opcode, 1);
237 pthread_mutex_unlock (&opcode_mutex);
240 static void *dns_child_loop (void *dummy)
243 char pcap_error[PCAP_ERRBUF_SIZE];
244 struct bpf_program fp;
248 /* Don't block any signals */
251 sigemptyset (&sigmask);
252 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
255 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
256 DBG ("Creating PCAP object..");
257 pcap_obj = pcap_open_live (pcap_device,
259 0 /* Not promiscuous */,
260 atoi (COLLECTD_STEP),
262 if (pcap_obj == NULL)
264 syslog (LOG_ERR, "dns plugin: Opening interface `%s' "
266 (pcap_device != NULL) ? pcap_device : "any",
271 memset (&fp, 0, sizeof (fp));
272 if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
274 DBG ("pcap_compile failed");
275 syslog (LOG_ERR, "dns plugin: pcap_compile failed");
278 if (pcap_setfilter (pcap_obj, &fp) < 0)
280 DBG ("pcap_setfilter failed");
281 syslog (LOG_ERR, "dns plugin: pcap_setfilter failed");
285 DBG ("PCAP object created.");
287 dnstop_set_pcap_obj (pcap_obj);
288 dnstop_set_callback (dns_child_callback);
290 status = pcap_loop (pcap_obj,
291 -1 /* loop forever */,
292 handle_pcap /* callback */,
293 NULL /* Whatever this means.. */);
295 syslog (LOG_ERR, "dns plugin: Listener thread is exiting "
296 "abnormally: %s", pcap_geterr (pcap_obj));
298 DBG ("child is exiting");
300 pcap_close (pcap_obj);
301 listen_thread_init = 0;
305 } /* static void dns_child_loop (void) */
307 static int dns_init (void)
309 /* clean up an old thread */
312 pthread_mutex_lock (&traffic_mutex);
315 pthread_mutex_unlock (&traffic_mutex);
317 if (listen_thread_init != 0)
320 status = pthread_create (&listen_thread, NULL, dns_child_loop,
324 syslog (LOG_ERR, "dns plugin: pthread_create failed: %s",
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);
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);
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 DBG ("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 DBG ("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 DBG ("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 */