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