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