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 #include "utils_dns.h"
28 #include <pthread.h>
29 #include <pcap.h>
30 #include <poll.h>
32 /*
33 * Private data types
34 */
35 struct counter_list_s
36 {
37 unsigned int key;
38 unsigned int value;
39 struct counter_list_s *next;
40 };
41 typedef struct counter_list_s counter_list_t;
43 /*
44 * Private variables
45 */
46 static const char *config_keys[] =
47 {
48 "Interface",
49 "IgnoreSource",
50 NULL
51 };
52 static int config_keys_num = 2;
54 #define PCAP_SNAPLEN 1460
55 static char *pcap_device = NULL;
57 static counter_t tr_queries;
58 static counter_t tr_responses;
59 static counter_list_t *qtype_list;
60 static counter_list_t *opcode_list;
61 static counter_list_t *rcode_list;
63 static pthread_t listen_thread;
64 static int listen_thread_init = 0;
65 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
66 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
67 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
68 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
69 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
71 /*
72 * Private functions
73 */
74 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
75 {
76 counter_list_t *entry;
78 DEBUG ("counter_list_search (list = %p, key = %u)",
79 (void *) *list, key);
81 for (entry = *list; entry != NULL; entry = entry->next)
82 if (entry->key == key)
83 break;
85 DEBUG ("return (%p)", (void *) entry);
86 return (entry);
87 }
89 static counter_list_t *counter_list_create (counter_list_t **list,
90 unsigned int key, unsigned int value)
91 {
92 counter_list_t *entry;
94 DEBUG ("counter_list_create (list = %p, key = %u, value = %u)",
95 (void *) *list, key, value);
97 entry = (counter_list_t *) malloc (sizeof (counter_list_t));
98 if (entry == NULL)
99 return (NULL);
101 memset (entry, 0, sizeof (counter_list_t));
102 entry->key = key;
103 entry->value = value;
105 if (*list == NULL)
106 {
107 *list = entry;
108 }
109 else
110 {
111 counter_list_t *last;
113 last = *list;
114 while (last->next != NULL)
115 last = last->next;
117 last->next = entry;
118 }
120 DEBUG ("return (%p)", (void *) entry);
121 return (entry);
122 }
124 static void counter_list_add (counter_list_t **list,
125 unsigned int key, unsigned int increment)
126 {
127 counter_list_t *entry;
129 DEBUG ("counter_list_add (list = %p, key = %u, increment = %u)",
130 (void *) *list, key, increment);
132 entry = counter_list_search (list, key);
134 if (entry != NULL)
135 {
136 entry->value += increment;
137 }
138 else
139 {
140 counter_list_create (list, key, increment);
141 }
142 DEBUG ("return ()");
143 }
145 static int dns_config (const char *key, const char *value)
146 {
147 if (strcasecmp (key, "Interface") == 0)
148 {
149 if (pcap_device != NULL)
150 free (pcap_device);
151 if ((pcap_device = strdup (value)) == NULL)
152 return (1);
153 }
154 else if (strcasecmp (key, "IgnoreSource") == 0)
155 {
156 if (value != NULL)
157 ignore_list_add_name (value);
158 }
159 else
160 {
161 return (-1);
162 }
164 return (0);
165 }
167 static void dns_child_callback (const rfc1035_header_t *dns)
168 {
169 if (dns->qr == 0)
170 {
171 /* This is a query */
172 pthread_mutex_lock (&traffic_mutex);
173 tr_queries += dns->length;
174 pthread_mutex_unlock (&traffic_mutex);
176 pthread_mutex_lock (&qtype_mutex);
177 counter_list_add (&qtype_list, dns->qtype, 1);
178 pthread_mutex_unlock (&qtype_mutex);
179 }
180 else
181 {
182 /* This is a reply */
183 pthread_mutex_lock (&traffic_mutex);
184 tr_responses += dns->length;
185 pthread_mutex_unlock (&traffic_mutex);
187 pthread_mutex_lock (&rcode_mutex);
188 counter_list_add (&rcode_list, dns->rcode, 1);
189 pthread_mutex_unlock (&rcode_mutex);
190 }
192 /* FIXME: Are queries, replies or both interesting? */
193 pthread_mutex_lock (&opcode_mutex);
194 counter_list_add (&opcode_list, dns->opcode, 1);
195 pthread_mutex_unlock (&opcode_mutex);
196 }
198 static void *dns_child_loop (void *dummy)
199 {
200 pcap_t *pcap_obj;
201 char pcap_error[PCAP_ERRBUF_SIZE];
202 struct bpf_program fp;
204 int status;
206 /* Don't block any signals */
207 {
208 sigset_t sigmask;
209 sigemptyset (&sigmask);
210 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
211 }
213 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
214 DEBUG ("Creating PCAP object..");
215 pcap_obj = pcap_open_live (pcap_device,
216 PCAP_SNAPLEN,
217 0 /* Not promiscuous */,
218 interval_g,
219 pcap_error);
220 if (pcap_obj == NULL)
221 {
222 ERROR ("dns plugin: Opening interface `%s' "
223 "failed: %s",
224 (pcap_device != NULL) ? pcap_device : "any",
225 pcap_error);
226 return (NULL);
227 }
229 memset (&fp, 0, sizeof (fp));
230 if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
231 {
232 ERROR ("dns plugin: pcap_compile failed");
233 return (NULL);
234 }
235 if (pcap_setfilter (pcap_obj, &fp) < 0)
236 {
237 ERROR ("dns plugin: pcap_setfilter failed");
238 return (NULL);
239 }
241 DEBUG ("PCAP object created.");
243 dnstop_set_pcap_obj (pcap_obj);
244 dnstop_set_callback (dns_child_callback);
246 status = pcap_loop (pcap_obj,
247 -1 /* loop forever */,
248 handle_pcap /* callback */,
249 NULL /* Whatever this means.. */);
250 if (status < 0)
251 ERROR ("dns plugin: Listener thread is exiting "
252 "abnormally: %s", pcap_geterr (pcap_obj));
254 DEBUG ("child is exiting");
256 pcap_close (pcap_obj);
257 listen_thread_init = 0;
258 pthread_exit (NULL);
260 return (NULL);
261 } /* static void dns_child_loop (void) */
263 static int dns_init (void)
264 {
265 /* clean up an old thread */
266 int status;
268 pthread_mutex_lock (&traffic_mutex);
269 tr_queries = 0;
270 tr_responses = 0;
271 pthread_mutex_unlock (&traffic_mutex);
273 if (listen_thread_init != 0)
274 return (-1);
276 status = pthread_create (&listen_thread, NULL, dns_child_loop,
277 (void *) 0);
278 if (status != 0)
279 {
280 char errbuf[1024];
281 ERROR ("dns plugin: pthread_create failed: %s",
282 sstrerror (errno, errbuf, sizeof (errbuf)));
283 return (-1);
284 }
286 listen_thread_init = 1;
288 return (0);
289 } /* int dns_init */
291 static void submit_counter (const char *type, const char *type_instance,
292 counter_t value)
293 {
294 value_t values[1];
295 value_list_t vl = VALUE_LIST_INIT;
297 values[0].counter = value;
299 vl.values = values;
300 vl.values_len = 1;
301 vl.time = time (NULL);
302 strcpy (vl.host, hostname_g);
303 strcpy (vl.plugin, "dns");
304 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
306 plugin_dispatch_values (type, &vl);
307 } /* void submit_counter */
309 static void submit_octets (counter_t queries, counter_t responses)
310 {
311 value_t values[2];
312 value_list_t vl = VALUE_LIST_INIT;
314 values[0].counter = queries;
315 values[1].counter = responses;
317 vl.values = values;
318 vl.values_len = 2;
319 vl.time = time (NULL);
320 strcpy (vl.host, hostname_g);
321 strcpy (vl.plugin, "dns");
323 plugin_dispatch_values ("dns_octets", &vl);
324 } /* void submit_counter */
326 static int dns_read (void)
327 {
328 unsigned int keys[T_MAX];
329 unsigned int values[T_MAX];
330 int len;
331 int i;
333 counter_list_t *ptr;
335 pthread_mutex_lock (&traffic_mutex);
336 values[0] = tr_queries;
337 values[1] = tr_responses;
338 pthread_mutex_unlock (&traffic_mutex);
340 if ((values[0] != 0) || (values[1] != 0))
341 submit_octets (values[0], values[1]);
343 pthread_mutex_lock (&qtype_mutex);
344 for (ptr = qtype_list, len = 0;
345 (ptr != NULL) && (len < T_MAX);
346 ptr = ptr->next, len++)
347 {
348 keys[len] = ptr->key;
349 values[len] = ptr->value;
350 }
351 pthread_mutex_unlock (&qtype_mutex);
353 for (i = 0; i < len; i++)
354 {
355 DEBUG ("qtype = %u; counter = %u;", keys[i], values[i]);
356 submit_counter ("dns_qtype", qtype_str (keys[i]), values[i]);
357 }
359 pthread_mutex_lock (&opcode_mutex);
360 for (ptr = opcode_list, len = 0;
361 (ptr != NULL) && (len < T_MAX);
362 ptr = ptr->next, len++)
363 {
364 keys[len] = ptr->key;
365 values[len] = ptr->value;
366 }
367 pthread_mutex_unlock (&opcode_mutex);
369 for (i = 0; i < len; i++)
370 {
371 DEBUG ("opcode = %u; counter = %u;", keys[i], values[i]);
372 submit_counter ("dns_opcode", opcode_str (keys[i]), values[i]);
373 }
375 pthread_mutex_lock (&rcode_mutex);
376 for (ptr = rcode_list, len = 0;
377 (ptr != NULL) && (len < T_MAX);
378 ptr = ptr->next, len++)
379 {
380 keys[len] = ptr->key;
381 values[len] = ptr->value;
382 }
383 pthread_mutex_unlock (&rcode_mutex);
385 for (i = 0; i < len; i++)
386 {
387 DEBUG ("rcode = %u; counter = %u;", keys[i], values[i]);
388 submit_counter ("dns_rcode", rcode_str (keys[i]), values[i]);
389 }
391 return (0);
392 } /* int dns_read */
394 void module_register (void)
395 {
396 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
397 plugin_register_init ("dns", dns_init);
398 plugin_register_read ("dns", dns_read);
399 } /* void module_register */