1 /**
2 * collectd - src/dns.c
3 * Copyright (C) 2006,2007 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
27 #if HAVE_LIBPCAP && HAVE_LIBPTHREAD
28 # include "utils_dns.h"
29 # include <pthread.h>
30 # include <pcap.h>
31 # include <poll.h>
32 # define DNS_HAVE_READ 1
33 #else
34 # define DNS_HAVE_READ 0
35 #endif
37 /*
38 * Private data types
39 */
40 #if DNS_HAVE_READ
41 struct counter_list_s
42 {
43 unsigned int key;
44 unsigned int value;
45 struct counter_list_s *next;
46 };
47 typedef struct counter_list_s counter_list_t;
48 #endif
50 /*
51 * Private variables
52 */
53 static data_source_t octets_dsrc[2] =
54 {
55 {"queries", DS_TYPE_COUNTER, 0, 125000000.0},
56 {"responses", DS_TYPE_COUNTER, 0, 125000000.0}
57 };
59 static data_set_t octets_ds =
60 {
61 "dns_octets", 2, octets_dsrc
62 };
64 static data_source_t counter_dsrc[1] =
65 {
66 {"value", DS_TYPE_COUNTER, 0, 65535.0}
67 };
69 static data_set_t qtype_ds =
70 {
71 "dns_qtype", 1, counter_dsrc
72 };
74 static data_set_t opcode_ds =
75 {
76 "dns_opcode", 1, counter_dsrc
77 };
79 static data_set_t rcode_ds =
80 {
81 "dns_rcode", 1, counter_dsrc
82 };
84 #if DNS_HAVE_READ
85 static const char *config_keys[] =
86 {
87 "Interface",
88 "IgnoreSource",
89 NULL
90 };
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 */
111 /*
112 * Private functions
113 */
114 #if DNS_HAVE_READ
115 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
116 {
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)
124 break;
126 DEBUG ("return (%p)", (void *) entry);
127 return (entry);
128 }
130 static counter_list_t *counter_list_create (counter_list_t **list,
131 unsigned int key, unsigned int value)
132 {
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));
139 if (entry == NULL)
140 return (NULL);
142 memset (entry, 0, sizeof (counter_list_t));
143 entry->key = key;
144 entry->value = value;
146 if (*list == NULL)
147 {
148 *list = entry;
149 }
150 else
151 {
152 counter_list_t *last;
154 last = *list;
155 while (last->next != NULL)
156 last = last->next;
158 last->next = entry;
159 }
161 DEBUG ("return (%p)", (void *) entry);
162 return (entry);
163 }
165 static void counter_list_add (counter_list_t **list,
166 unsigned int key, unsigned int increment)
167 {
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);
175 if (entry != NULL)
176 {
177 entry->value += increment;
178 }
179 else
180 {
181 counter_list_create (list, key, increment);
182 }
183 DEBUG ("return ()");
184 }
186 static int dns_config (const char *key, const char *value)
187 {
188 if (strcasecmp (key, "Interface") == 0)
189 {
190 if (pcap_device != NULL)
191 free (pcap_device);
192 if ((pcap_device = strdup (value)) == NULL)
193 return (1);
194 }
195 else if (strcasecmp (key, "IgnoreSource") == 0)
196 {
197 if (value != NULL)
198 ignore_list_add_name (value);
199 }
200 else
201 {
202 return (-1);
203 }
205 return (0);
206 }
208 static void dns_child_callback (const rfc1035_header_t *dns)
209 {
210 if (dns->qr == 0)
211 {
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);
220 }
221 else
222 {
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);
231 }
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);
237 }
239 static void *dns_child_loop (void *dummy)
240 {
241 pcap_t *pcap_obj;
242 char pcap_error[PCAP_ERRBUF_SIZE];
243 struct bpf_program fp;
245 int status;
247 /* Don't block any signals */
248 {
249 sigset_t sigmask;
250 sigemptyset (&sigmask);
251 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
252 }
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,
257 PCAP_SNAPLEN,
258 0 /* Not promiscuous */,
259 atoi (COLLECTD_STEP),
260 pcap_error);
261 if (pcap_obj == NULL)
262 {
263 ERROR ("dns plugin: Opening interface `%s' "
264 "failed: %s",
265 (pcap_device != NULL) ? pcap_device : "any",
266 pcap_error);
267 return (NULL);
268 }
270 memset (&fp, 0, sizeof (fp));
271 if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
272 {
273 DEBUG ("pcap_compile failed");
274 ERROR ("dns plugin: pcap_compile failed");
275 return (NULL);
276 }
277 if (pcap_setfilter (pcap_obj, &fp) < 0)
278 {
279 DEBUG ("pcap_setfilter failed");
280 ERROR ("dns plugin: pcap_setfilter failed");
281 return (NULL);
282 }
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.. */);
293 if (status < 0)
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;
301 pthread_exit (NULL);
303 return (NULL);
304 } /* static void dns_child_loop (void) */
306 static int dns_init (void)
307 {
308 /* clean up an old thread */
309 int status;
311 pthread_mutex_lock (&traffic_mutex);
312 tr_queries = 0;
313 tr_responses = 0;
314 pthread_mutex_unlock (&traffic_mutex);
316 if (listen_thread_init != 0)
317 return (-1);
319 status = pthread_create (&listen_thread, NULL, dns_child_loop,
320 (void *) 0);
321 if (status != 0)
322 {
323 ERROR ("dns plugin: pthread_create failed: %s",
324 strerror (status));
325 return (-1);
326 }
328 listen_thread_init = 1;
330 return (0);
331 } /* int dns_init */
333 static void submit_counter (const char *type, const char *type_instance,
334 counter_t value)
335 {
336 value_t values[1];
337 value_list_t vl = VALUE_LIST_INIT;
339 values[0].counter = value;
341 vl.values = values;
342 vl.values_len = 1;
343 vl.time = time (NULL);
344 strcpy (vl.host, hostname_g);
345 strcpy (vl.plugin, "dns");
346 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
348 plugin_dispatch_values (type, &vl);
349 } /* void submit_counter */
351 static void submit_octets (counter_t queries, counter_t responses)
352 {
353 value_t values[2];
354 value_list_t vl = VALUE_LIST_INIT;
356 values[0].counter = queries;
357 values[1].counter = responses;
359 vl.values = values;
360 vl.values_len = 2;
361 vl.time = time (NULL);
362 strcpy (vl.host, hostname_g);
363 strcpy (vl.plugin, "dns");
365 plugin_dispatch_values ("dns_octets", &vl);
366 } /* void submit_counter */
368 static int dns_read (void)
369 {
370 unsigned int keys[T_MAX];
371 unsigned int values[T_MAX];
372 int len;
373 int i;
375 counter_list_t *ptr;
377 pthread_mutex_lock (&traffic_mutex);
378 values[0] = tr_queries;
379 values[1] = tr_responses;
380 pthread_mutex_unlock (&traffic_mutex);
381 submit_octets (values[0], values[1]);
383 pthread_mutex_lock (&qtype_mutex);
384 for (ptr = qtype_list, len = 0;
385 (ptr != NULL) && (len < T_MAX);
386 ptr = ptr->next, len++)
387 {
388 keys[len] = ptr->key;
389 values[len] = ptr->value;
390 }
391 pthread_mutex_unlock (&qtype_mutex);
393 for (i = 0; i < len; i++)
394 {
395 DEBUG ("qtype = %u; counter = %u;", keys[i], values[i]);
396 submit_counter ("dns_qtype", qtype_str (keys[i]), values[i]);
397 }
399 pthread_mutex_lock (&opcode_mutex);
400 for (ptr = opcode_list, len = 0;
401 (ptr != NULL) && (len < T_MAX);
402 ptr = ptr->next, len++)
403 {
404 keys[len] = ptr->key;
405 values[len] = ptr->value;
406 }
407 pthread_mutex_unlock (&opcode_mutex);
409 for (i = 0; i < len; i++)
410 {
411 DEBUG ("opcode = %u; counter = %u;", keys[i], values[i]);
412 submit_counter ("dns_opcode", opcode_str (keys[i]), values[i]);
413 }
415 pthread_mutex_lock (&rcode_mutex);
416 for (ptr = rcode_list, len = 0;
417 (ptr != NULL) && (len < T_MAX);
418 ptr = ptr->next, len++)
419 {
420 keys[len] = ptr->key;
421 values[len] = ptr->value;
422 }
423 pthread_mutex_unlock (&rcode_mutex);
425 for (i = 0; i < len; i++)
426 {
427 DEBUG ("rcode = %u; counter = %u;", keys[i], values[i]);
428 submit_counter ("dns_rcode", rcode_str (keys[i]), values[i]);
429 }
431 return (0);
432 } /* int dns_read */
433 #endif
435 void module_register (void)
436 {
437 plugin_register_data_set (&octets_ds);
438 plugin_register_data_set (&qtype_ds);
439 plugin_register_data_set (&opcode_ds);
440 plugin_register_data_set (&rcode_ds);
442 #if DNS_HAVE_READ
443 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
444 plugin_register_init ("dns", dns_init);
445 plugin_register_read ("dns", dns_read);
446 #endif
447 } /* void module_register */