53128396232b848c34645010dcbc64d36eac1281
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"
31 #include "configfile.h"
33 #include "utils_dns.h"
34 #include <poll.h>
36 #include <pcap.h>
38 #ifdef HAVE_SYS_CAPABILITY_H
39 # include <sys/capability.h>
40 #endif
42 /*
43 * Private data types
44 */
45 struct counter_list_s
46 {
47 unsigned int key;
48 unsigned int value;
49 struct counter_list_s *next;
50 };
51 typedef struct counter_list_s counter_list_t;
53 /*
54 * Private variables
55 */
56 static const char *config_keys[] =
57 {
58 "Interface",
59 "IgnoreSource",
60 "SelectNumericQueryTypes"
61 };
62 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
63 static int select_numeric_qtype = 1;
65 #define PCAP_SNAPLEN 1460
66 static char *pcap_device = NULL;
68 static derive_t tr_queries;
69 static derive_t tr_responses;
70 static counter_list_t *qtype_list;
71 static counter_list_t *opcode_list;
72 static counter_list_t *rcode_list;
74 static pthread_t listen_thread;
75 static int listen_thread_init = 0;
76 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
77 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
78 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
79 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
80 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
82 /*
83 * Private functions
84 */
85 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
86 {
87 counter_list_t *entry;
89 for (entry = *list; entry != NULL; entry = entry->next)
90 if (entry->key == key)
91 break;
93 return (entry);
94 }
96 static counter_list_t *counter_list_create (counter_list_t **list,
97 unsigned int key, unsigned int value)
98 {
99 counter_list_t *entry;
101 entry = calloc (1, sizeof (*entry));
102 if (entry == NULL)
103 return (NULL);
105 entry->key = key;
106 entry->value = value;
108 if (*list == NULL)
109 {
110 *list = entry;
111 }
112 else
113 {
114 counter_list_t *last;
116 last = *list;
117 while (last->next != NULL)
118 last = last->next;
120 last->next = entry;
121 }
123 return (entry);
124 }
126 static void counter_list_add (counter_list_t **list,
127 unsigned int key, unsigned int increment)
128 {
129 counter_list_t *entry;
131 entry = counter_list_search (list, key);
133 if (entry != NULL)
134 {
135 entry->value += increment;
136 }
137 else
138 {
139 counter_list_create (list, key, increment);
140 }
141 }
143 static int dns_config (const char *key, const char *value)
144 {
145 if (strcasecmp (key, "Interface") == 0)
146 {
147 if (pcap_device != NULL)
148 free (pcap_device);
149 if ((pcap_device = strdup (value)) == NULL)
150 return (1);
151 }
152 else if (strcasecmp (key, "IgnoreSource") == 0)
153 {
154 if (value != NULL)
155 ignore_list_add_name (value);
156 }
157 else if (strcasecmp (key, "SelectNumericQueryTypes") == 0)
158 {
159 if ((value != NULL) && IS_FALSE (value))
160 select_numeric_qtype = 0;
161 else
162 select_numeric_qtype = 1;
163 }
164 else
165 {
166 return (-1);
167 }
169 return (0);
170 }
172 static void dns_child_callback (const rfc1035_header_t *dns)
173 {
174 if (dns->qr == 0)
175 {
176 /* This is a query */
177 int skip = 0;
178 if (!select_numeric_qtype)
179 {
180 const char *str = qtype_str(dns->qtype);
181 if ((str == NULL) || (str[0] == '#'))
182 skip = 1;
183 }
185 pthread_mutex_lock (&traffic_mutex);
186 tr_queries += dns->length;
187 pthread_mutex_unlock (&traffic_mutex);
189 if (skip == 0)
190 {
191 pthread_mutex_lock (&qtype_mutex);
192 counter_list_add (&qtype_list, dns->qtype, 1);
193 pthread_mutex_unlock (&qtype_mutex);
194 }
195 }
196 else
197 {
198 /* This is a reply */
199 pthread_mutex_lock (&traffic_mutex);
200 tr_responses += dns->length;
201 pthread_mutex_unlock (&traffic_mutex);
203 pthread_mutex_lock (&rcode_mutex);
204 counter_list_add (&rcode_list, dns->rcode, 1);
205 pthread_mutex_unlock (&rcode_mutex);
206 }
208 /* FIXME: Are queries, replies or both interesting? */
209 pthread_mutex_lock (&opcode_mutex);
210 counter_list_add (&opcode_list, dns->opcode, 1);
211 pthread_mutex_unlock (&opcode_mutex);
212 }
214 static int dns_run_pcap_loop (void)
215 {
216 pcap_t *pcap_obj;
217 char pcap_error[PCAP_ERRBUF_SIZE];
218 struct bpf_program fp = { 0 };
220 int status;
222 /* Don't block any signals */
223 {
224 sigset_t sigmask;
225 sigemptyset (&sigmask);
226 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
227 }
229 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
230 DEBUG ("dns plugin: Creating PCAP object..");
231 pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any",
232 PCAP_SNAPLEN,
233 0 /* Not promiscuous */,
234 (int) CDTIME_T_TO_MS (plugin_get_interval () / 2),
235 pcap_error);
236 if (pcap_obj == NULL)
237 {
238 ERROR ("dns plugin: Opening interface `%s' "
239 "failed: %s",
240 (pcap_device != NULL) ? pcap_device : "any",
241 pcap_error);
242 return (PCAP_ERROR);
243 }
245 status = pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0);
246 if (status < 0)
247 {
248 ERROR ("dns plugin: pcap_compile failed: %s",
249 pcap_statustostr (status));
250 return (status);
251 }
253 status = pcap_setfilter (pcap_obj, &fp);
254 if (status < 0)
255 {
256 ERROR ("dns plugin: pcap_setfilter failed: %s",
257 pcap_statustostr (status));
258 return (status);
259 }
261 DEBUG ("dns plugin: PCAP object created.");
263 dnstop_set_pcap_obj (pcap_obj);
264 dnstop_set_callback (dns_child_callback);
266 status = pcap_loop (pcap_obj,
267 -1 /* loop forever */,
268 handle_pcap /* callback */,
269 NULL /* user data */);
270 INFO ("dns plugin: pcap_loop exited with status %i.", status);
271 /* We need to handle "PCAP_ERROR" specially because libpcap currently
272 * doesn't return PCAP_ERROR_IFACE_NOT_UP for compatibility reasons. */
273 if (status == PCAP_ERROR)
274 status = PCAP_ERROR_IFACE_NOT_UP;
276 pcap_close (pcap_obj);
277 return (status);
278 } /* int dns_run_pcap_loop */
280 static int dns_sleep_one_interval (void) /* {{{ */
281 {
282 cdtime_t interval;
283 struct timespec ts = { 0, 0 };
284 int status = 0;
286 interval = plugin_get_interval ();
287 CDTIME_T_TO_TIMESPEC (interval, &ts);
289 while (42)
290 {
291 struct timespec rem = { 0, 0 };
293 status = nanosleep (&ts, &rem);
294 if (status == 0)
295 break;
296 else if ((errno == EINTR) || (errno == EAGAIN))
297 {
298 ts = rem;
299 continue;
300 }
301 else
302 break;
303 }
305 return (status);
306 } /* }}} int dns_sleep_one_interval */
308 static void *dns_child_loop (__attribute__((unused)) void *dummy) /* {{{ */
309 {
310 int status;
312 while (42)
313 {
314 status = dns_run_pcap_loop ();
315 if (status != PCAP_ERROR_IFACE_NOT_UP)
316 break;
318 dns_sleep_one_interval ();
319 }
321 if (status != PCAP_ERROR_BREAK)
322 ERROR ("dns plugin: PCAP returned error %s.",
323 pcap_statustostr (status));
325 listen_thread_init = 0;
326 return (NULL);
327 } /* }}} void *dns_child_loop */
329 static int dns_init (void)
330 {
331 /* clean up an old thread */
332 int status;
334 pthread_mutex_lock (&traffic_mutex);
335 tr_queries = 0;
336 tr_responses = 0;
337 pthread_mutex_unlock (&traffic_mutex);
339 if (listen_thread_init != 0)
340 return (-1);
342 status = plugin_thread_create (&listen_thread, NULL, dns_child_loop,
343 (void *) 0);
344 if (status != 0)
345 {
346 char errbuf[1024];
347 ERROR ("dns plugin: pthread_create failed: %s",
348 sstrerror (errno, errbuf, sizeof (errbuf)));
349 return (-1);
350 }
352 listen_thread_init = 1;
354 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_RAW)
355 if (check_capability (CAP_NET_RAW) != 0)
356 {
357 if (getuid () == 0)
358 WARNING ("dns plugin: Running collectd as root, but the CAP_NET_RAW "
359 "capability is missing. The plugin's read function will probably "
360 "fail. Is your init system dropping capabilities ?");
361 else
362 WARNING ("dns plugin: collectd doesn't have the CAP_NET_RAW capability. "
363 "If you don't want to run collectd as root, try running \"setcap "
364 "cap_net_raw=ep\" on the collectd binary.");
365 }
366 #endif
368 return (0);
369 } /* int dns_init */
371 static void submit_derive (const char *type, const char *type_instance,
372 derive_t value)
373 {
374 value_t values[1];
375 value_list_t vl = VALUE_LIST_INIT;
377 values[0].derive = value;
379 vl.values = values;
380 vl.values_len = 1;
381 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
382 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
383 sstrncpy (vl.type, type, sizeof (vl.type));
384 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
386 plugin_dispatch_values (&vl);
387 } /* void submit_derive */
389 static void submit_octets (derive_t queries, derive_t responses)
390 {
391 value_t values[2];
392 value_list_t vl = VALUE_LIST_INIT;
394 values[0].derive = queries;
395 values[1].derive = responses;
397 vl.values = values;
398 vl.values_len = 2;
399 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
400 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
401 sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
403 plugin_dispatch_values (&vl);
404 } /* void submit_octets */
406 static int dns_read (void)
407 {
408 unsigned int keys[T_MAX];
409 unsigned int values[T_MAX];
410 int len;
412 counter_list_t *ptr;
414 pthread_mutex_lock (&traffic_mutex);
415 values[0] = tr_queries;
416 values[1] = tr_responses;
417 pthread_mutex_unlock (&traffic_mutex);
419 if ((values[0] != 0) || (values[1] != 0))
420 submit_octets (values[0], values[1]);
422 pthread_mutex_lock (&qtype_mutex);
423 for (ptr = qtype_list, len = 0;
424 (ptr != NULL) && (len < T_MAX);
425 ptr = ptr->next, len++)
426 {
427 keys[len] = ptr->key;
428 values[len] = ptr->value;
429 }
430 pthread_mutex_unlock (&qtype_mutex);
432 for (int i = 0; i < len; i++)
433 {
434 DEBUG ("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]);
435 submit_derive ("dns_qtype", qtype_str (keys[i]), values[i]);
436 }
438 pthread_mutex_lock (&opcode_mutex);
439 for (ptr = opcode_list, len = 0;
440 (ptr != NULL) && (len < T_MAX);
441 ptr = ptr->next, len++)
442 {
443 keys[len] = ptr->key;
444 values[len] = ptr->value;
445 }
446 pthread_mutex_unlock (&opcode_mutex);
448 for (int i = 0; i < len; i++)
449 {
450 DEBUG ("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]);
451 submit_derive ("dns_opcode", opcode_str (keys[i]), values[i]);
452 }
454 pthread_mutex_lock (&rcode_mutex);
455 for (ptr = rcode_list, len = 0;
456 (ptr != NULL) && (len < T_MAX);
457 ptr = ptr->next, len++)
458 {
459 keys[len] = ptr->key;
460 values[len] = ptr->value;
461 }
462 pthread_mutex_unlock (&rcode_mutex);
464 for (int i = 0; i < len; i++)
465 {
466 DEBUG ("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]);
467 submit_derive ("dns_rcode", rcode_str (keys[i]), values[i]);
468 }
470 return (0);
471 } /* int dns_read */
473 void module_register (void)
474 {
475 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
476 plugin_register_init ("dns", dns_init);
477 plugin_register_read ("dns", dns_read);
478 } /* void module_register */