1 /**
2 * collectd - src/dns.c
3 * Copyright (C) 2006-2011 Florian octo Forster
4 * Copyright (C) 2009 Mirko Buffoni
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at collectd.org>
21 * Mirko Buffoni <briareos at eswat.org>
22 **/
24 #define _BSD_SOURCE
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
31 #include "utils_dns.h"
32 #include <pthread.h>
33 #include <poll.h>
35 #include <pcap.h>
36 #if HAVE_PCAP_BPF_H
37 # include <pcap-bpf.h>
38 #endif
40 /*
41 * Private data types
42 */
43 struct counter_list_s
44 {
45 unsigned int key;
46 unsigned int value;
47 struct counter_list_s *next;
48 };
49 typedef struct counter_list_s counter_list_t;
51 /*
52 * Private variables
53 */
54 static const char *config_keys[] =
55 {
56 "Interface",
57 "IgnoreSource",
58 "SelectNumericQueryTypes"
59 };
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
61 static int select_numeric_qtype = 1;
63 #define PCAP_SNAPLEN 1460
64 static char *pcap_device = NULL;
66 static counter_t tr_queries;
67 static counter_t tr_responses;
68 static counter_list_t *qtype_list;
69 static counter_list_t *opcode_list;
70 static counter_list_t *rcode_list;
72 static pthread_t listen_thread;
73 static int listen_thread_init = 0;
74 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
75 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
76 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
77 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
78 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
80 /*
81 * Private functions
82 */
83 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
84 {
85 counter_list_t *entry;
87 DEBUG ("counter_list_search (list = %p, key = %u)",
88 (void *) *list, key);
90 for (entry = *list; entry != NULL; entry = entry->next)
91 if (entry->key == key)
92 break;
94 DEBUG ("return (%p)", (void *) entry);
95 return (entry);
96 }
98 static counter_list_t *counter_list_create (counter_list_t **list,
99 unsigned int key, unsigned int value)
100 {
101 counter_list_t *entry;
103 DEBUG ("counter_list_create (list = %p, key = %u, value = %u)",
104 (void *) *list, key, value);
106 entry = (counter_list_t *) malloc (sizeof (counter_list_t));
107 if (entry == NULL)
108 return (NULL);
110 memset (entry, 0, sizeof (counter_list_t));
111 entry->key = key;
112 entry->value = value;
114 if (*list == NULL)
115 {
116 *list = entry;
117 }
118 else
119 {
120 counter_list_t *last;
122 last = *list;
123 while (last->next != NULL)
124 last = last->next;
126 last->next = entry;
127 }
129 DEBUG ("return (%p)", (void *) entry);
130 return (entry);
131 }
133 static void counter_list_add (counter_list_t **list,
134 unsigned int key, unsigned int increment)
135 {
136 counter_list_t *entry;
138 DEBUG ("counter_list_add (list = %p, key = %u, increment = %u)",
139 (void *) *list, key, increment);
141 entry = counter_list_search (list, key);
143 if (entry != NULL)
144 {
145 entry->value += increment;
146 }
147 else
148 {
149 counter_list_create (list, key, increment);
150 }
151 DEBUG ("return ()");
152 }
154 static int dns_config (const char *key, const char *value)
155 {
156 if (strcasecmp (key, "Interface") == 0)
157 {
158 if (pcap_device != NULL)
159 free (pcap_device);
160 if ((pcap_device = strdup (value)) == NULL)
161 return (1);
162 }
163 else if (strcasecmp (key, "IgnoreSource") == 0)
164 {
165 if (value != NULL)
166 ignore_list_add_name (value);
167 }
168 else if (strcasecmp (key, "SelectNumericQueryTypes") == 0)
169 {
170 if ((value != NULL) && IS_FALSE (value))
171 select_numeric_qtype = 0;
172 else
173 select_numeric_qtype = 1;
174 }
175 else
176 {
177 return (-1);
178 }
180 return (0);
181 }
183 static void dns_child_callback (const rfc1035_header_t *dns)
184 {
185 if (dns->qr == 0)
186 {
187 /* This is a query */
188 int skip = 0;
189 if (!select_numeric_qtype)
190 {
191 const char *str = qtype_str(dns->qtype);
192 if ((str == NULL) || (str[0] == '#'))
193 skip = 1;
194 }
196 pthread_mutex_lock (&traffic_mutex);
197 tr_queries += dns->length;
198 pthread_mutex_unlock (&traffic_mutex);
200 if (skip == 0)
201 {
202 pthread_mutex_lock (&qtype_mutex);
203 counter_list_add (&qtype_list, dns->qtype, 1);
204 pthread_mutex_unlock (&qtype_mutex);
205 }
206 }
207 else
208 {
209 /* This is a reply */
210 pthread_mutex_lock (&traffic_mutex);
211 tr_responses += dns->length;
212 pthread_mutex_unlock (&traffic_mutex);
214 pthread_mutex_lock (&rcode_mutex);
215 counter_list_add (&rcode_list, dns->rcode, 1);
216 pthread_mutex_unlock (&rcode_mutex);
217 }
219 /* FIXME: Are queries, replies or both interesting? */
220 pthread_mutex_lock (&opcode_mutex);
221 counter_list_add (&opcode_list, dns->opcode, 1);
222 pthread_mutex_unlock (&opcode_mutex);
223 }
225 static void *dns_child_loop (__attribute__((unused)) void *dummy)
226 {
227 pcap_t *pcap_obj;
228 char pcap_error[PCAP_ERRBUF_SIZE];
229 struct bpf_program fp;
231 int status;
233 /* Don't block any signals */
234 {
235 sigset_t sigmask;
236 sigemptyset (&sigmask);
237 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
238 }
240 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
241 DEBUG ("dns plugin: Creating PCAP object..");
242 pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any",
243 PCAP_SNAPLEN,
244 0 /* Not promiscuous */,
245 interval_g,
246 pcap_error);
247 if (pcap_obj == NULL)
248 {
249 ERROR ("dns plugin: Opening interface `%s' "
250 "failed: %s",
251 (pcap_device != NULL) ? pcap_device : "any",
252 pcap_error);
253 return (NULL);
254 }
256 memset (&fp, 0, sizeof (fp));
257 if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
258 {
259 ERROR ("dns plugin: pcap_compile failed");
260 return (NULL);
261 }
262 if (pcap_setfilter (pcap_obj, &fp) < 0)
263 {
264 ERROR ("dns plugin: pcap_setfilter failed");
265 return (NULL);
266 }
268 DEBUG ("PCAP object created.");
270 dnstop_set_pcap_obj (pcap_obj);
271 dnstop_set_callback (dns_child_callback);
273 status = pcap_loop (pcap_obj,
274 -1 /* loop forever */,
275 handle_pcap /* callback */,
276 NULL /* Whatever this means.. */);
277 if (status < 0)
278 ERROR ("dns plugin: Listener thread is exiting "
279 "abnormally: %s", pcap_geterr (pcap_obj));
281 DEBUG ("child is exiting");
283 pcap_close (pcap_obj);
284 listen_thread_init = 0;
285 pthread_exit (NULL);
287 return (NULL);
288 } /* static void dns_child_loop (void) */
290 static int dns_init (void)
291 {
292 /* clean up an old thread */
293 int status;
295 pthread_mutex_lock (&traffic_mutex);
296 tr_queries = 0;
297 tr_responses = 0;
298 pthread_mutex_unlock (&traffic_mutex);
300 if (listen_thread_init != 0)
301 return (-1);
303 status = pthread_create (&listen_thread, NULL, dns_child_loop,
304 (void *) 0);
305 if (status != 0)
306 {
307 char errbuf[1024];
308 ERROR ("dns plugin: pthread_create failed: %s",
309 sstrerror (errno, errbuf, sizeof (errbuf)));
310 return (-1);
311 }
313 listen_thread_init = 1;
315 return (0);
316 } /* int dns_init */
318 static void submit_counter (const char *type, const char *type_instance,
319 counter_t value)
320 {
321 value_t values[1];
322 value_list_t vl = VALUE_LIST_INIT;
324 values[0].counter = value;
326 vl.values = values;
327 vl.values_len = 1;
328 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
329 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
330 sstrncpy (vl.type, type, sizeof (vl.type));
331 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
333 plugin_dispatch_values (&vl);
334 } /* void submit_counter */
336 static void submit_octets (counter_t queries, counter_t responses)
337 {
338 value_t values[2];
339 value_list_t vl = VALUE_LIST_INIT;
341 values[0].counter = queries;
342 values[1].counter = responses;
344 vl.values = values;
345 vl.values_len = 2;
346 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
347 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
348 sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
350 plugin_dispatch_values (&vl);
351 } /* void submit_counter */
353 static int dns_read (void)
354 {
355 unsigned int keys[T_MAX];
356 unsigned int values[T_MAX];
357 int len;
358 int i;
360 counter_list_t *ptr;
362 pthread_mutex_lock (&traffic_mutex);
363 values[0] = tr_queries;
364 values[1] = tr_responses;
365 pthread_mutex_unlock (&traffic_mutex);
367 if ((values[0] != 0) || (values[1] != 0))
368 submit_octets (values[0], values[1]);
370 pthread_mutex_lock (&qtype_mutex);
371 for (ptr = qtype_list, len = 0;
372 (ptr != NULL) && (len < T_MAX);
373 ptr = ptr->next, len++)
374 {
375 keys[len] = ptr->key;
376 values[len] = ptr->value;
377 }
378 pthread_mutex_unlock (&qtype_mutex);
380 for (i = 0; i < len; i++)
381 {
382 DEBUG ("qtype = %u; counter = %u;", keys[i], values[i]);
383 submit_counter ("dns_qtype", qtype_str (keys[i]), values[i]);
384 }
386 pthread_mutex_lock (&opcode_mutex);
387 for (ptr = opcode_list, len = 0;
388 (ptr != NULL) && (len < T_MAX);
389 ptr = ptr->next, len++)
390 {
391 keys[len] = ptr->key;
392 values[len] = ptr->value;
393 }
394 pthread_mutex_unlock (&opcode_mutex);
396 for (i = 0; i < len; i++)
397 {
398 DEBUG ("opcode = %u; counter = %u;", keys[i], values[i]);
399 submit_counter ("dns_opcode", opcode_str (keys[i]), values[i]);
400 }
402 pthread_mutex_lock (&rcode_mutex);
403 for (ptr = rcode_list, len = 0;
404 (ptr != NULL) && (len < T_MAX);
405 ptr = ptr->next, len++)
406 {
407 keys[len] = ptr->key;
408 values[len] = ptr->value;
409 }
410 pthread_mutex_unlock (&rcode_mutex);
412 for (i = 0; i < len; i++)
413 {
414 DEBUG ("rcode = %u; counter = %u;", keys[i], values[i]);
415 submit_counter ("dns_rcode", rcode_str (keys[i]), values[i]);
416 }
418 return (0);
419 } /* int dns_read */
421 void module_register (void)
422 {
423 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
424 plugin_register_init ("dns", dns_init);
425 plugin_register_read ("dns", dns_read);
426 } /* void module_register */