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