Merge branch 'collectd-4.10'
[collectd.git] / src / dns.c
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  **/
23
24 #define _BSD_SOURCE
25
26 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.h"
30
31 #include "utils_dns.h"
32 #include <pthread.h>
33 #include <poll.h>
34
35 #include <pcap.h>
36 #if HAVE_PCAP_BPF_H
37 # include <pcap-bpf.h>
38 #endif
39
40 /*
41  * Private data types
42  */
43 struct counter_list_s
44 {
45         unsigned int key;
46         unsigned int value;
47         struct counter_list_s *next;
48 };
49 typedef struct counter_list_s counter_list_t;
50
51 /*
52  * Private variables
53  */
54 static const char *config_keys[] =
55 {
56         "Interface",
57         "IgnoreSource",
58         "SelectNumericQueryTypes"
59 };
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
61 static int select_numeric_qtype = 1;
62
63 #define PCAP_SNAPLEN 1460
64 static char   *pcap_device = NULL;
65
66 static derive_t       tr_queries;
67 static derive_t       tr_responses;
68 static counter_list_t *qtype_list;
69 static counter_list_t *opcode_list;
70 static counter_list_t *rcode_list;
71
72 static pthread_t       listen_thread;
73 static int             listen_thread_init = 0;
74 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
75 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
76 static pthread_mutex_t qtype_mutex   = PTHREAD_MUTEX_INITIALIZER;
77 static pthread_mutex_t opcode_mutex  = PTHREAD_MUTEX_INITIALIZER;
78 static pthread_mutex_t rcode_mutex   = PTHREAD_MUTEX_INITIALIZER;
79
80 /*
81  * Private functions
82  */
83 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
84 {
85         counter_list_t *entry;
86
87         for (entry = *list; entry != NULL; entry = entry->next)
88                 if (entry->key == key)
89                         break;
90
91         return (entry);
92 }
93
94 static counter_list_t *counter_list_create (counter_list_t **list,
95                 unsigned int key, unsigned int value)
96 {
97         counter_list_t *entry;
98
99         entry = (counter_list_t *) malloc (sizeof (counter_list_t));
100         if (entry == NULL)
101                 return (NULL);
102
103         memset (entry, 0, sizeof (counter_list_t));
104         entry->key = key;
105         entry->value = value;
106
107         if (*list == NULL)
108         {
109                 *list = entry;
110         }
111         else
112         {
113                 counter_list_t *last;
114
115                 last = *list;
116                 while (last->next != NULL)
117                         last = last->next;
118
119                 last->next = entry;
120         }
121
122         return (entry);
123 }
124
125 static void counter_list_add (counter_list_t **list,
126                 unsigned int key, unsigned int increment)
127 {
128         counter_list_t *entry;
129
130         entry = counter_list_search (list, key);
131
132         if (entry != NULL)
133         {
134                 entry->value += increment;
135         }
136         else
137         {
138                 counter_list_create (list, key, increment);
139         }
140 }
141
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         }
167
168         return (0);
169 }
170
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                 }
183
184                 pthread_mutex_lock (&traffic_mutex);
185                 tr_queries += dns->length;
186                 pthread_mutex_unlock (&traffic_mutex);
187
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);
201
202                 pthread_mutex_lock (&rcode_mutex);
203                 counter_list_add (&rcode_list,  dns->rcode,  1);
204                 pthread_mutex_unlock (&rcode_mutex);
205         }
206
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 }
212
213 static void *dns_child_loop (__attribute__((unused)) void *dummy)
214 {
215         pcap_t *pcap_obj;
216         char    pcap_error[PCAP_ERRBUF_SIZE];
217         struct  bpf_program fp;
218
219         int status;
220
221         /* Don't block any signals */
222         {
223                 sigset_t sigmask;
224                 sigemptyset (&sigmask);
225                 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
226         }
227
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 (interval_g / 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 (NULL);
242         }
243
244         memset (&fp, 0, sizeof (fp));
245         if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
246         {
247                 ERROR ("dns plugin: pcap_compile failed");
248                 return (NULL);
249         }
250         if (pcap_setfilter (pcap_obj, &fp) < 0)
251         {
252                 ERROR ("dns plugin: pcap_setfilter failed");
253                 return (NULL);
254         }
255
256         DEBUG ("dns plugin: PCAP object created.");
257
258         dnstop_set_pcap_obj (pcap_obj);
259         dnstop_set_callback (dns_child_callback);
260
261         status = pcap_loop (pcap_obj,
262                         -1 /* loop forever */,
263                         handle_pcap /* callback */,
264                         NULL /* Whatever this means.. */);
265         if (status < 0)
266                 ERROR ("dns plugin: Listener thread is exiting "
267                                 "abnormally: %s", pcap_geterr (pcap_obj));
268
269         DEBUG ("dns plugin: Child is exiting.");
270
271         pcap_close (pcap_obj);
272         listen_thread_init = 0;
273         pthread_exit (NULL);
274
275         return (NULL);
276 } /* static void dns_child_loop (void) */
277
278 static int dns_init (void)
279 {
280         /* clean up an old thread */
281         int status;
282
283         pthread_mutex_lock (&traffic_mutex);
284         tr_queries   = 0;
285         tr_responses = 0;
286         pthread_mutex_unlock (&traffic_mutex);
287
288         if (listen_thread_init != 0)
289                 return (-1);
290
291         status = pthread_create (&listen_thread, NULL, dns_child_loop,
292                         (void *) 0);
293         if (status != 0)
294         {
295                 char errbuf[1024];
296                 ERROR ("dns plugin: pthread_create failed: %s",
297                                 sstrerror (errno, errbuf, sizeof (errbuf)));
298                 return (-1);
299         }
300
301         listen_thread_init = 1;
302
303         return (0);
304 } /* int dns_init */
305
306 static void submit_derive (const char *type, const char *type_instance,
307                 derive_t value)
308 {
309         value_t values[1];
310         value_list_t vl = VALUE_LIST_INIT;
311
312         values[0].derive = value;
313
314         vl.values = values;
315         vl.values_len = 1;
316         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
317         sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
318         sstrncpy (vl.type, type, sizeof (vl.type));
319         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
320
321         plugin_dispatch_values (&vl);
322 } /* void submit_derive */
323
324 static void submit_octets (derive_t queries, derive_t responses)
325 {
326         value_t values[2];
327         value_list_t vl = VALUE_LIST_INIT;
328
329         values[0].derive = queries;
330         values[1].derive = responses;
331
332         vl.values = values;
333         vl.values_len = 2;
334         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
335         sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
336         sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
337
338         plugin_dispatch_values (&vl);
339 } /* void submit_octets */
340
341 static int dns_read (void)
342 {
343         unsigned int keys[T_MAX];
344         unsigned int values[T_MAX];
345         int len;
346         int i;
347
348         counter_list_t *ptr;
349
350         pthread_mutex_lock (&traffic_mutex);
351         values[0] = tr_queries;
352         values[1] = tr_responses;
353         pthread_mutex_unlock (&traffic_mutex);
354
355         if ((values[0] != 0) || (values[1] != 0))
356                 submit_octets (values[0], values[1]);
357
358         pthread_mutex_lock (&qtype_mutex);
359         for (ptr = qtype_list, len = 0;
360                         (ptr != NULL) && (len < T_MAX);
361                         ptr = ptr->next, len++)
362         {
363                 keys[len]   = ptr->key;
364                 values[len] = ptr->value;
365         }
366         pthread_mutex_unlock (&qtype_mutex);
367
368         for (i = 0; i < len; i++)
369         {
370                 DEBUG ("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]);
371                 submit_derive ("dns_qtype", qtype_str (keys[i]), values[i]);
372         }
373
374         pthread_mutex_lock (&opcode_mutex);
375         for (ptr = opcode_list, len = 0;
376                         (ptr != NULL) && (len < T_MAX);
377                         ptr = ptr->next, len++)
378         {
379                 keys[len]   = ptr->key;
380                 values[len] = ptr->value;
381         }
382         pthread_mutex_unlock (&opcode_mutex);
383
384         for (i = 0; i < len; i++)
385         {
386                 DEBUG ("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]);
387                 submit_derive ("dns_opcode", opcode_str (keys[i]), values[i]);
388         }
389
390         pthread_mutex_lock (&rcode_mutex);
391         for (ptr = rcode_list, len = 0;
392                         (ptr != NULL) && (len < T_MAX);
393                         ptr = ptr->next, len++)
394         {
395                 keys[len]   = ptr->key;
396                 values[len] = ptr->value;
397         }
398         pthread_mutex_unlock (&rcode_mutex);
399
400         for (i = 0; i < len; i++)
401         {
402                 DEBUG ("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]);
403                 submit_derive ("dns_rcode", rcode_str (keys[i]), values[i]);
404         }
405
406         return (0);
407 } /* int dns_read */
408
409 void module_register (void)
410 {
411         plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
412         plugin_register_init ("dns", dns_init);
413         plugin_register_read ("dns", dns_read);
414 } /* void module_register */