c8e794eb032f83dd786455767819214548c758a3
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 _DEFAULT_SOURCE
25 #define _BSD_SOURCE
27 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
32 #include "utils_dns.h"
33 #include <poll.h>
35 #include <pcap.h>
37 #ifdef HAVE_SYS_CAPABILITY_H
38 # include <sys/capability.h>
39 #endif
41 /*
42 * Private data types
43 */
44 struct counter_list_s
45 {
46 unsigned int key;
47 unsigned int value;
48 struct counter_list_s *next;
49 };
50 typedef struct counter_list_s counter_list_t;
52 /*
53 * Private variables
54 */
55 static const char *config_keys[] =
56 {
57 "Interface",
58 "IgnoreSource",
59 "SelectNumericQueryTypes"
60 };
61 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
62 static int select_numeric_qtype = 1;
64 #define PCAP_SNAPLEN 1460
65 static char *pcap_device = NULL;
67 static derive_t tr_queries;
68 static derive_t tr_responses;
69 static counter_list_t *qtype_list;
70 static counter_list_t *opcode_list;
71 static counter_list_t *rcode_list;
73 static pthread_t listen_thread;
74 static int listen_thread_init = 0;
75 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
76 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
77 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
78 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
79 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
81 /*
82 * Private functions
83 */
84 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
85 {
86 counter_list_t *entry;
88 for (entry = *list; entry != NULL; entry = entry->next)
89 if (entry->key == key)
90 break;
92 return (entry);
93 }
95 static counter_list_t *counter_list_create (counter_list_t **list,
96 unsigned int key, unsigned int value)
97 {
98 counter_list_t *entry;
100 entry = calloc (1, sizeof (*entry));
101 if (entry == NULL)
102 return (NULL);
104 entry->key = key;
105 entry->value = value;
107 if (*list == NULL)
108 {
109 *list = entry;
110 }
111 else
112 {
113 counter_list_t *last;
115 last = *list;
116 while (last->next != NULL)
117 last = last->next;
119 last->next = entry;
120 }
122 return (entry);
123 }
125 static void counter_list_add (counter_list_t **list,
126 unsigned int key, unsigned int increment)
127 {
128 counter_list_t *entry;
130 entry = counter_list_search (list, key);
132 if (entry != NULL)
133 {
134 entry->value += increment;
135 }
136 else
137 {
138 counter_list_create (list, key, increment);
139 }
140 }
142 static int dns_config (const char *key, const char *value)
143 {
144 if (strcasecmp (key, "Interface") == 0)
145 {
146 if (pcap_device != NULL)
147 free (pcap_device);
148 if ((pcap_device = strdup (value)) == NULL)
149 return (1);
150 }
151 else if (strcasecmp (key, "IgnoreSource") == 0)
152 {
153 if (value != NULL)
154 ignore_list_add_name (value);
155 }
156 else if (strcasecmp (key, "SelectNumericQueryTypes") == 0)
157 {
158 if ((value != NULL) && IS_FALSE (value))
159 select_numeric_qtype = 0;
160 else
161 select_numeric_qtype = 1;
162 }
163 else
164 {
165 return (-1);
166 }
168 return (0);
169 }
171 static void dns_child_callback (const rfc1035_header_t *dns)
172 {
173 if (dns->qr == 0)
174 {
175 /* This is a query */
176 int skip = 0;
177 if (!select_numeric_qtype)
178 {
179 const char *str = qtype_str(dns->qtype);
180 if ((str == NULL) || (str[0] == '#'))
181 skip = 1;
182 }
184 pthread_mutex_lock (&traffic_mutex);
185 tr_queries += dns->length;
186 pthread_mutex_unlock (&traffic_mutex);
188 if (skip == 0)
189 {
190 pthread_mutex_lock (&qtype_mutex);
191 counter_list_add (&qtype_list, dns->qtype, 1);
192 pthread_mutex_unlock (&qtype_mutex);
193 }
194 }
195 else
196 {
197 /* This is a reply */
198 pthread_mutex_lock (&traffic_mutex);
199 tr_responses += dns->length;
200 pthread_mutex_unlock (&traffic_mutex);
202 pthread_mutex_lock (&rcode_mutex);
203 counter_list_add (&rcode_list, dns->rcode, 1);
204 pthread_mutex_unlock (&rcode_mutex);
205 }
207 /* FIXME: Are queries, replies or both interesting? */
208 pthread_mutex_lock (&opcode_mutex);
209 counter_list_add (&opcode_list, dns->opcode, 1);
210 pthread_mutex_unlock (&opcode_mutex);
211 }
213 static int dns_run_pcap_loop (void)
214 {
215 pcap_t *pcap_obj;
216 char pcap_error[PCAP_ERRBUF_SIZE];
217 struct bpf_program fp = { 0 };
219 int status;
221 /* Don't block any signals */
222 {
223 sigset_t sigmask;
224 sigemptyset (&sigmask);
225 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
226 }
228 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
229 DEBUG ("dns plugin: Creating PCAP object..");
230 pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any",
231 PCAP_SNAPLEN,
232 0 /* Not promiscuous */,
233 (int) CDTIME_T_TO_MS (plugin_get_interval () / 2),
234 pcap_error);
235 if (pcap_obj == NULL)
236 {
237 ERROR ("dns plugin: Opening interface `%s' "
238 "failed: %s",
239 (pcap_device != NULL) ? pcap_device : "any",
240 pcap_error);
241 return (PCAP_ERROR);
242 }
244 status = pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0);
245 if (status < 0)
246 {
247 ERROR ("dns plugin: pcap_compile failed: %s",
248 pcap_statustostr (status));
249 return (status);
250 }
252 status = pcap_setfilter (pcap_obj, &fp);
253 if (status < 0)
254 {
255 ERROR ("dns plugin: pcap_setfilter failed: %s",
256 pcap_statustostr (status));
257 return (status);
258 }
260 DEBUG ("dns plugin: PCAP object created.");
262 dnstop_set_pcap_obj (pcap_obj);
263 dnstop_set_callback (dns_child_callback);
265 status = pcap_loop (pcap_obj,
266 -1 /* loop forever */,
267 handle_pcap /* callback */,
268 NULL /* user data */);
269 INFO ("dns plugin: pcap_loop exited with status %i.", status);
270 /* We need to handle "PCAP_ERROR" specially because libpcap currently
271 * doesn't return PCAP_ERROR_IFACE_NOT_UP for compatibility reasons. */
272 if (status == PCAP_ERROR)
273 status = PCAP_ERROR_IFACE_NOT_UP;
275 pcap_close (pcap_obj);
276 return (status);
277 } /* int dns_run_pcap_loop */
279 static int dns_sleep_one_interval (void) /* {{{ */
280 {
281 struct timespec ts = CDTIME_T_TO_TIMESPEC (plugin_get_interval ());
282 while (nanosleep (&ts, &ts) != 0)
283 {
284 if ((errno == EINTR) || (errno == EAGAIN))
285 continue;
287 return (errno);
288 }
290 return (0);
291 } /* }}} int dns_sleep_one_interval */
293 static void *dns_child_loop (__attribute__((unused)) void *dummy) /* {{{ */
294 {
295 int status;
297 while (42)
298 {
299 status = dns_run_pcap_loop ();
300 if (status != PCAP_ERROR_IFACE_NOT_UP)
301 break;
303 dns_sleep_one_interval ();
304 }
306 if (status != PCAP_ERROR_BREAK)
307 ERROR ("dns plugin: PCAP returned error %s.",
308 pcap_statustostr (status));
310 listen_thread_init = 0;
311 return (NULL);
312 } /* }}} void *dns_child_loop */
314 static int dns_init (void)
315 {
316 /* clean up an old thread */
317 int status;
319 pthread_mutex_lock (&traffic_mutex);
320 tr_queries = 0;
321 tr_responses = 0;
322 pthread_mutex_unlock (&traffic_mutex);
324 if (listen_thread_init != 0)
325 return (-1);
327 status = plugin_thread_create (&listen_thread, NULL, dns_child_loop,
328 (void *) 0);
329 if (status != 0)
330 {
331 char errbuf[1024];
332 ERROR ("dns plugin: pthread_create failed: %s",
333 sstrerror (errno, errbuf, sizeof (errbuf)));
334 return (-1);
335 }
337 listen_thread_init = 1;
339 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_RAW)
340 if (check_capability (CAP_NET_RAW) != 0)
341 {
342 if (getuid () == 0)
343 WARNING ("dns plugin: Running collectd as root, but the CAP_NET_RAW "
344 "capability is missing. The plugin's read function will probably "
345 "fail. Is your init system dropping capabilities?");
346 else
347 WARNING ("dns plugin: collectd doesn't have the CAP_NET_RAW capability. "
348 "If you don't want to run collectd as root, try running \"setcap "
349 "cap_net_raw=ep\" on the collectd binary.");
350 }
351 #endif
353 return (0);
354 } /* int dns_init */
356 static void submit_derive (const char *type, const char *type_instance,
357 derive_t value)
358 {
359 value_list_t vl = VALUE_LIST_INIT;
361 vl.values = &(value_t) { .derive = value };
362 vl.values_len = 1;
363 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
364 sstrncpy (vl.type, type, sizeof (vl.type));
365 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
367 plugin_dispatch_values (&vl);
368 } /* void submit_derive */
370 static void submit_octets (derive_t queries, derive_t responses)
371 {
372 value_t values[] = {
373 { .derive = queries },
374 { .derive = responses },
375 };
376 value_list_t vl = VALUE_LIST_INIT;
378 vl.values = values;
379 vl.values_len = STATIC_ARRAY_SIZE (values);
380 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
381 sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
383 plugin_dispatch_values (&vl);
384 } /* void submit_octets */
386 static int dns_read (void)
387 {
388 unsigned int keys[T_MAX];
389 unsigned int values[T_MAX];
390 int len;
392 counter_list_t *ptr;
394 pthread_mutex_lock (&traffic_mutex);
395 values[0] = tr_queries;
396 values[1] = tr_responses;
397 pthread_mutex_unlock (&traffic_mutex);
399 if ((values[0] != 0) || (values[1] != 0))
400 submit_octets (values[0], values[1]);
402 pthread_mutex_lock (&qtype_mutex);
403 for (ptr = qtype_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 (&qtype_mutex);
412 for (int i = 0; i < len; i++)
413 {
414 DEBUG ("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]);
415 submit_derive ("dns_qtype", qtype_str (keys[i]), values[i]);
416 }
418 pthread_mutex_lock (&opcode_mutex);
419 for (ptr = opcode_list, len = 0;
420 (ptr != NULL) && (len < T_MAX);
421 ptr = ptr->next, len++)
422 {
423 keys[len] = ptr->key;
424 values[len] = ptr->value;
425 }
426 pthread_mutex_unlock (&opcode_mutex);
428 for (int i = 0; i < len; i++)
429 {
430 DEBUG ("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]);
431 submit_derive ("dns_opcode", opcode_str (keys[i]), values[i]);
432 }
434 pthread_mutex_lock (&rcode_mutex);
435 for (ptr = rcode_list, len = 0;
436 (ptr != NULL) && (len < T_MAX);
437 ptr = ptr->next, len++)
438 {
439 keys[len] = ptr->key;
440 values[len] = ptr->value;
441 }
442 pthread_mutex_unlock (&rcode_mutex);
444 for (int i = 0; i < len; i++)
445 {
446 DEBUG ("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]);
447 submit_derive ("dns_rcode", rcode_str (keys[i]), values[i]);
448 }
450 return (0);
451 } /* int dns_read */
453 void module_register (void)
454 {
455 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
456 plugin_register_init ("dns", dns_init);
457 plugin_register_read ("dns", dns_read);
458 } /* void module_register */