Code

Removed some legacy calls to DEBUG().
[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  **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
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
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
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;
62 #define PCAP_SNAPLEN 1460
63 static char   *pcap_device = NULL;
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;
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 */
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;
88         DEBUG ("counter_list_search (list = %p, key = %u)",
89                         (void *) *list, key);
91         for (entry = *list; entry != NULL; entry = entry->next)
92                 if (entry->key == key)
93                         break;
95         DEBUG ("return (%p)", (void *) entry);
96         return (entry);
97 }
99 static counter_list_t *counter_list_create (counter_list_t **list,
100                 unsigned int key, unsigned int value)
102         counter_list_t *entry;
104         DEBUG ("counter_list_create (list = %p, key = %u, value = %u)",
105                         (void *) *list, key, value);
107         entry = (counter_list_t *) malloc (sizeof (counter_list_t));
108         if (entry == NULL)
109                 return (NULL);
111         memset (entry, 0, sizeof (counter_list_t));
112         entry->key = key;
113         entry->value = value;
115         if (*list == NULL)
116         {
117                 *list = entry;
118         }
119         else
120         {
121                 counter_list_t *last;
123                 last = *list;
124                 while (last->next != NULL)
125                         last = last->next;
127                 last->next = entry;
128         }
130         DEBUG ("return (%p)", (void *) entry);
131         return (entry);
134 static void counter_list_add (counter_list_t **list,
135                 unsigned int key, unsigned int increment)
137         counter_list_t *entry;
139         DEBUG ("counter_list_add (list = %p, key = %u, increment = %u)",
140                         (void *) *list, key, increment);
142         entry = counter_list_search (list, key);
144         if (entry != NULL)
145         {
146                 entry->value += increment;
147         }
148         else
149         {
150                 counter_list_create (list, key, increment);
151         }
152         DEBUG ("return ()");
155 static int dns_config (const char *key, const char *value)
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         }
174         return (0);
177 static void dns_child_callback (const rfc1035_header_t *dns)
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);
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);
197                 pthread_mutex_lock (&rcode_mutex);
198                 counter_list_add (&rcode_list,  dns->rcode,  1);
199                 pthread_mutex_unlock (&rcode_mutex);
200         }
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);
208 static void *dns_child_loop (void *dummy)
210         pcap_t *pcap_obj;
211         char    pcap_error[PCAP_ERRBUF_SIZE];
212         struct  bpf_program fp;
214         int status;
216         /* Don't block any signals */
217         {
218                 sigset_t sigmask;
219                 sigemptyset (&sigmask);
220                 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
221         }
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         }
239         memset (&fp, 0, sizeof (fp));
240         if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
241         {
242                 ERROR ("dns plugin: pcap_compile failed");
243                 return (NULL);
244         }
245         if (pcap_setfilter (pcap_obj, &fp) < 0)
246         {
247                 ERROR ("dns plugin: pcap_setfilter failed");
248                 return (NULL);
249         }
251         DEBUG ("PCAP object created.");
253         dnstop_set_pcap_obj (pcap_obj);
254         dnstop_set_callback (dns_child_callback);
256         status = pcap_loop (pcap_obj,
257                         -1 /* loop forever */,
258                         handle_pcap /* callback */,
259                         NULL /* Whatever this means.. */);
260         if (status < 0)
261                 ERROR ("dns plugin: Listener thread is exiting "
262                                 "abnormally: %s", pcap_geterr (pcap_obj));
264         DEBUG ("child is exiting");
266         pcap_close (pcap_obj);
267         listen_thread_init = 0;
268         pthread_exit (NULL);
270         return (NULL);
271 } /* static void dns_child_loop (void) */
273 static int dns_init (void)
275         /* clean up an old thread */
276         int status;
278         pthread_mutex_lock (&traffic_mutex);
279         tr_queries   = 0;
280         tr_responses = 0;
281         pthread_mutex_unlock (&traffic_mutex);
283         if (listen_thread_init != 0)
284                 return (-1);
286         status = pthread_create (&listen_thread, NULL, dns_child_loop,
287                         (void *) 0);
288         if (status != 0)
289         {
290                 char errbuf[1024];
291                 ERROR ("dns plugin: pthread_create failed: %s",
292                                 sstrerror (errno, errbuf, sizeof (errbuf)));
293                 return (-1);
294         }
296         listen_thread_init = 1;
298         return (0);
299 } /* int dns_init */
301 static void submit_counter (const char *type, const char *type_instance,
302                 counter_t value)
304         value_t values[1];
305         value_list_t vl = VALUE_LIST_INIT;
307         values[0].counter = value;
309         vl.values = values;
310         vl.values_len = 1;
311         vl.time = time (NULL);
312         strcpy (vl.host, hostname_g);
313         strcpy (vl.plugin, "dns");
314         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
316         plugin_dispatch_values (type, &vl);
317 } /* void submit_counter */
319 static void submit_octets (counter_t queries, counter_t responses)
321         value_t values[2];
322         value_list_t vl = VALUE_LIST_INIT;
324         values[0].counter = queries;
325         values[1].counter = responses;
327         vl.values = values;
328         vl.values_len = 2;
329         vl.time = time (NULL);
330         strcpy (vl.host, hostname_g);
331         strcpy (vl.plugin, "dns");
333         plugin_dispatch_values ("dns_octets", &vl);
334 } /* void submit_counter */
336 static int dns_read (void)
338         unsigned int keys[T_MAX];
339         unsigned int values[T_MAX];
340         int len;
341         int i;
343         counter_list_t *ptr;
345         pthread_mutex_lock (&traffic_mutex);
346         values[0] = tr_queries;
347         values[1] = tr_responses;
348         pthread_mutex_unlock (&traffic_mutex);
350         if ((values[0] != 0) || (values[1] != 0))
351                 submit_octets (values[0], values[1]);
353         pthread_mutex_lock (&qtype_mutex);
354         for (ptr = qtype_list, len = 0;
355                         (ptr != NULL) && (len < T_MAX);
356                         ptr = ptr->next, len++)
357         {
358                 keys[len]   = ptr->key;
359                 values[len] = ptr->value;
360         }
361         pthread_mutex_unlock (&qtype_mutex);
363         for (i = 0; i < len; i++)
364         {
365                 DEBUG ("qtype = %u; counter = %u;", keys[i], values[i]);
366                 submit_counter ("dns_qtype", qtype_str (keys[i]), values[i]);
367         }
369         pthread_mutex_lock (&opcode_mutex);
370         for (ptr = opcode_list, len = 0;
371                         (ptr != NULL) && (len < T_MAX);
372                         ptr = ptr->next, len++)
373         {
374                 keys[len]   = ptr->key;
375                 values[len] = ptr->value;
376         }
377         pthread_mutex_unlock (&opcode_mutex);
379         for (i = 0; i < len; i++)
380         {
381                 DEBUG ("opcode = %u; counter = %u;", keys[i], values[i]);
382                 submit_counter ("dns_opcode", opcode_str (keys[i]), values[i]);
383         }
385         pthread_mutex_lock (&rcode_mutex);
386         for (ptr = rcode_list, len = 0;
387                         (ptr != NULL) && (len < T_MAX);
388                         ptr = ptr->next, len++)
389         {
390                 keys[len]   = ptr->key;
391                 values[len] = ptr->value;
392         }
393         pthread_mutex_unlock (&rcode_mutex);
395         for (i = 0; i < len; i++)
396         {
397                 DEBUG ("rcode = %u; counter = %u;", keys[i], values[i]);
398                 submit_counter ("dns_rcode", rcode_str (keys[i]), values[i]);
399         }
401         return (0);
402 } /* int dns_read */
403 #endif
405 void module_register (void)
407 #if DNS_HAVE_READ
408         plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
409         plugin_register_init ("dns", dns_init);
410         plugin_register_read ("dns", dns_read);
411 #endif
412 } /* void module_register */