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 interval_g,
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 char errbuf[1024];
324 ERROR ("dns plugin: pthread_create failed: %s",
325 sstrerror (errno, errbuf, sizeof (errbuf)));
326 return (-1);
327 }
329 listen_thread_init = 1;
331 return (0);
332 } /* int dns_init */
334 static void submit_counter (const char *type, const char *type_instance,
335 counter_t value)
336 {
337 value_t values[1];
338 value_list_t vl = VALUE_LIST_INIT;
340 values[0].counter = value;
342 vl.values = values;
343 vl.values_len = 1;
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)
353 {
354 value_t values[2];
355 value_list_t vl = VALUE_LIST_INIT;
357 values[0].counter = queries;
358 values[1].counter = responses;
360 vl.values = values;
361 vl.values_len = 2;
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)
370 {
371 unsigned int keys[T_MAX];
372 unsigned int values[T_MAX];
373 int len;
374 int i;
376 counter_list_t *ptr;
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++)
388 {
389 keys[len] = ptr->key;
390 values[len] = ptr->value;
391 }
392 pthread_mutex_unlock (&qtype_mutex);
394 for (i = 0; i < len; i++)
395 {
396 DEBUG ("qtype = %u; counter = %u;", keys[i], values[i]);
397 submit_counter ("dns_qtype", qtype_str (keys[i]), values[i]);
398 }
400 pthread_mutex_lock (&opcode_mutex);
401 for (ptr = opcode_list, len = 0;
402 (ptr != NULL) && (len < T_MAX);
403 ptr = ptr->next, len++)
404 {
405 keys[len] = ptr->key;
406 values[len] = ptr->value;
407 }
408 pthread_mutex_unlock (&opcode_mutex);
410 for (i = 0; i < len; i++)
411 {
412 DEBUG ("opcode = %u; counter = %u;", keys[i], values[i]);
413 submit_counter ("dns_opcode", opcode_str (keys[i]), values[i]);
414 }
416 pthread_mutex_lock (&rcode_mutex);
417 for (ptr = rcode_list, len = 0;
418 (ptr != NULL) && (len < T_MAX);
419 ptr = ptr->next, len++)
420 {
421 keys[len] = ptr->key;
422 values[len] = ptr->value;
423 }
424 pthread_mutex_unlock (&rcode_mutex);
426 for (i = 0; i < len; i++)
427 {
428 DEBUG ("rcode = %u; counter = %u;", keys[i], values[i]);
429 submit_counter ("dns_rcode", rcode_str (keys[i]), values[i]);
430 }
432 return (0);
433 } /* int dns_read */
434 #endif
436 void module_register (modreg_e load)
437 {
438 if (load & MR_DATASETS)
439 {
440 plugin_register_data_set (&octets_ds);
441 plugin_register_data_set (&qtype_ds);
442 plugin_register_data_set (&opcode_ds);
443 plugin_register_data_set (&rcode_ds);
444 }
446 #if DNS_HAVE_READ
447 if (load & MR_READ)
448 {
449 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
450 plugin_register_init ("dns", dns_init);
451 plugin_register_read ("dns", dns_read);
452 }
453 #endif
454 } /* void module_register */