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 #include <pcap-bpf.h>
38 /*
39 * Private data types
40 */
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;
49 /*
50 * Private variables
51 */
52 static const char *config_keys[] =
53 {
54 "Interface",
55 "IgnoreSource",
56 "SelectNumericQueryTypes"
57 };
58 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
59 static int select_numeric_qtype = 1;
61 #define PCAP_SNAPLEN 1460
62 static char *pcap_device = NULL;
64 static derive_t tr_queries;
65 static derive_t tr_responses;
66 static counter_list_t *qtype_list;
67 static counter_list_t *opcode_list;
68 static counter_list_t *rcode_list;
70 static pthread_t listen_thread;
71 static int listen_thread_init = 0;
72 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
73 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
74 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
75 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
76 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
78 /*
79 * Private functions
80 */
81 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
82 {
83 counter_list_t *entry;
85 for (entry = *list; entry != NULL; entry = entry->next)
86 if (entry->key == key)
87 break;
89 return (entry);
90 }
92 static counter_list_t *counter_list_create (counter_list_t **list,
93 unsigned int key, unsigned int value)
94 {
95 counter_list_t *entry;
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 return (entry);
121 }
123 static void counter_list_add (counter_list_t **list,
124 unsigned int key, unsigned int increment)
125 {
126 counter_list_t *entry;
128 entry = counter_list_search (list, key);
130 if (entry != NULL)
131 {
132 entry->value += increment;
133 }
134 else
135 {
136 counter_list_create (list, key, increment);
137 }
138 }
140 static int dns_config (const char *key, const char *value)
141 {
142 if (strcasecmp (key, "Interface") == 0)
143 {
144 if (pcap_device != NULL)
145 free (pcap_device);
146 if ((pcap_device = strdup (value)) == NULL)
147 return (1);
148 }
149 else if (strcasecmp (key, "IgnoreSource") == 0)
150 {
151 if (value != NULL)
152 ignore_list_add_name (value);
153 }
154 else if (strcasecmp (key, "SelectNumericQueryTypes") == 0)
155 {
156 if ((value != NULL) && IS_FALSE (value))
157 select_numeric_qtype = 0;
158 else
159 select_numeric_qtype = 1;
160 }
161 else
162 {
163 return (-1);
164 }
166 return (0);
167 }
169 static void dns_child_callback (const rfc1035_header_t *dns)
170 {
171 if (dns->qr == 0)
172 {
173 /* This is a query */
174 int skip = 0;
175 if (!select_numeric_qtype)
176 {
177 const char *str = qtype_str(dns->qtype);
178 if ((str == NULL) || (str[0] == '#'))
179 skip = 1;
180 }
182 pthread_mutex_lock (&traffic_mutex);
183 tr_queries += dns->length;
184 pthread_mutex_unlock (&traffic_mutex);
186 if (skip == 0)
187 {
188 pthread_mutex_lock (&qtype_mutex);
189 counter_list_add (&qtype_list, dns->qtype, 1);
190 pthread_mutex_unlock (&qtype_mutex);
191 }
192 }
193 else
194 {
195 /* This is a reply */
196 pthread_mutex_lock (&traffic_mutex);
197 tr_responses += dns->length;
198 pthread_mutex_unlock (&traffic_mutex);
200 pthread_mutex_lock (&rcode_mutex);
201 counter_list_add (&rcode_list, dns->rcode, 1);
202 pthread_mutex_unlock (&rcode_mutex);
203 }
205 /* FIXME: Are queries, replies or both interesting? */
206 pthread_mutex_lock (&opcode_mutex);
207 counter_list_add (&opcode_list, dns->opcode, 1);
208 pthread_mutex_unlock (&opcode_mutex);
209 }
211 static int dns_run_pcap_loop (void)
212 {
213 pcap_t *pcap_obj;
214 char pcap_error[PCAP_ERRBUF_SIZE];
215 struct bpf_program fp;
217 int status;
219 /* Don't block any signals */
220 {
221 sigset_t sigmask;
222 sigemptyset (&sigmask);
223 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
224 }
226 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
227 DEBUG ("dns plugin: Creating PCAP object..");
228 pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any",
229 PCAP_SNAPLEN,
230 0 /* Not promiscuous */,
231 (int) CDTIME_T_TO_MS (plugin_get_interval () / 2),
232 pcap_error);
233 if (pcap_obj == NULL)
234 {
235 ERROR ("dns plugin: Opening interface `%s' "
236 "failed: %s",
237 (pcap_device != NULL) ? pcap_device : "any",
238 pcap_error);
239 return (PCAP_ERROR);
240 }
242 memset (&fp, 0, sizeof (fp));
243 status = pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0);
244 if (status < 0)
245 {
246 ERROR ("dns plugin: pcap_compile failed: %s",
247 pcap_statustostr (status));
248 return (status);
249 }
251 status = pcap_setfilter (pcap_obj, &fp);
252 if (status < 0)
253 {
254 ERROR ("dns plugin: pcap_setfilter failed: %s",
255 pcap_statustostr (status));
256 return (status);
257 }
259 DEBUG ("dns plugin: PCAP object created.");
261 dnstop_set_pcap_obj (pcap_obj);
262 dnstop_set_callback (dns_child_callback);
264 status = pcap_loop (pcap_obj,
265 -1 /* loop forever */,
266 handle_pcap /* callback */,
267 NULL /* user data */);
268 INFO ("dns plugin: pcap_loop exited with status %i.", status);
269 /* We need to handle "PCAP_ERROR" specially because libpcap currently
270 * doesn't return PCAP_ERROR_IFACE_NOT_UP for compatibility reasons. */
271 if (status == PCAP_ERROR)
272 status = PCAP_ERROR_IFACE_NOT_UP;
274 pcap_close (pcap_obj);
275 return (status);
276 } /* int dns_run_pcap_loop */
278 static void *dns_child_loop (__attribute__((unused)) void *dummy) /* {{{ */
279 {
280 int status = PCAP_ERROR_IFACE_NOT_UP;
282 while (status == PCAP_ERROR_IFACE_NOT_UP)
283 status = dns_run_pcap_loop ();
284 if (status != PCAP_ERROR_BREAK)
285 ERROR ("dns plugin: PCAP returned error %s.",
286 pcap_statustostr (status));
288 listen_thread_init = 0;
289 return (NULL);
290 } /* }}} void *dns_child_loop */
292 static int dns_init (void)
293 {
294 /* clean up an old thread */
295 int status;
297 pthread_mutex_lock (&traffic_mutex);
298 tr_queries = 0;
299 tr_responses = 0;
300 pthread_mutex_unlock (&traffic_mutex);
302 if (listen_thread_init != 0)
303 return (-1);
305 status = plugin_thread_create (&listen_thread, NULL, dns_child_loop,
306 (void *) 0);
307 if (status != 0)
308 {
309 char errbuf[1024];
310 ERROR ("dns plugin: pthread_create failed: %s",
311 sstrerror (errno, errbuf, sizeof (errbuf)));
312 return (-1);
313 }
315 listen_thread_init = 1;
317 return (0);
318 } /* int dns_init */
320 static void submit_derive (const char *type, const char *type_instance,
321 derive_t value)
322 {
323 value_t values[1];
324 value_list_t vl = VALUE_LIST_INIT;
326 values[0].derive = value;
328 vl.values = values;
329 vl.values_len = 1;
330 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
331 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
332 sstrncpy (vl.type, type, sizeof (vl.type));
333 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
335 plugin_dispatch_values (&vl);
336 } /* void submit_derive */
338 static void submit_octets (derive_t queries, derive_t responses)
339 {
340 value_t values[2];
341 value_list_t vl = VALUE_LIST_INIT;
343 values[0].derive = queries;
344 values[1].derive = responses;
346 vl.values = values;
347 vl.values_len = 2;
348 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
349 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
350 sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
352 plugin_dispatch_values (&vl);
353 } /* void submit_octets */
355 static int dns_read (void)
356 {
357 unsigned int keys[T_MAX];
358 unsigned int values[T_MAX];
359 int len;
360 int i;
362 counter_list_t *ptr;
364 pthread_mutex_lock (&traffic_mutex);
365 values[0] = tr_queries;
366 values[1] = tr_responses;
367 pthread_mutex_unlock (&traffic_mutex);
369 if ((values[0] != 0) || (values[1] != 0))
370 submit_octets (values[0], values[1]);
372 pthread_mutex_lock (&qtype_mutex);
373 for (ptr = qtype_list, len = 0;
374 (ptr != NULL) && (len < T_MAX);
375 ptr = ptr->next, len++)
376 {
377 keys[len] = ptr->key;
378 values[len] = ptr->value;
379 }
380 pthread_mutex_unlock (&qtype_mutex);
382 for (i = 0; i < len; i++)
383 {
384 DEBUG ("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]);
385 submit_derive ("dns_qtype", qtype_str (keys[i]), values[i]);
386 }
388 pthread_mutex_lock (&opcode_mutex);
389 for (ptr = opcode_list, len = 0;
390 (ptr != NULL) && (len < T_MAX);
391 ptr = ptr->next, len++)
392 {
393 keys[len] = ptr->key;
394 values[len] = ptr->value;
395 }
396 pthread_mutex_unlock (&opcode_mutex);
398 for (i = 0; i < len; i++)
399 {
400 DEBUG ("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]);
401 submit_derive ("dns_opcode", opcode_str (keys[i]), values[i]);
402 }
404 pthread_mutex_lock (&rcode_mutex);
405 for (ptr = rcode_list, len = 0;
406 (ptr != NULL) && (len < T_MAX);
407 ptr = ptr->next, len++)
408 {
409 keys[len] = ptr->key;
410 values[len] = ptr->value;
411 }
412 pthread_mutex_unlock (&rcode_mutex);
414 for (i = 0; i < len; i++)
415 {
416 DEBUG ("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]);
417 submit_derive ("dns_rcode", rcode_str (keys[i]), values[i]);
418 }
420 return (0);
421 } /* int dns_read */
423 void module_register (void)
424 {
425 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
426 plugin_register_init ("dns", dns_init);
427 plugin_register_read ("dns", dns_read);
428 } /* void module_register */