treewide: replace memset to 0 with initializers
[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 _DEFAULT_SOURCE
25 #define _BSD_SOURCE
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31
32 #include "utils_dns.h"
33 #include <poll.h>
34
35 #include <pcap.h>
36
37 /*
38  * Private data types
39  */
40 struct counter_list_s
41 {
42         unsigned int key;
43         unsigned int value;
44         struct counter_list_s *next;
45 };
46 typedef struct counter_list_s counter_list_t;
47
48 /*
49  * Private variables
50  */
51 static const char *config_keys[] =
52 {
53         "Interface",
54         "IgnoreSource",
55         "SelectNumericQueryTypes"
56 };
57 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
58 static int select_numeric_qtype = 1;
59
60 #define PCAP_SNAPLEN 1460
61 static char   *pcap_device = NULL;
62
63 static derive_t       tr_queries;
64 static derive_t       tr_responses;
65 static counter_list_t *qtype_list;
66 static counter_list_t *opcode_list;
67 static counter_list_t *rcode_list;
68
69 static pthread_t       listen_thread;
70 static int             listen_thread_init = 0;
71 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
72 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
73 static pthread_mutex_t qtype_mutex   = PTHREAD_MUTEX_INITIALIZER;
74 static pthread_mutex_t opcode_mutex  = PTHREAD_MUTEX_INITIALIZER;
75 static pthread_mutex_t rcode_mutex   = PTHREAD_MUTEX_INITIALIZER;
76
77 /*
78  * Private functions
79  */
80 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
81 {
82         counter_list_t *entry;
83
84         for (entry = *list; entry != NULL; entry = entry->next)
85                 if (entry->key == key)
86                         break;
87
88         return (entry);
89 }
90
91 static counter_list_t *counter_list_create (counter_list_t **list,
92                 unsigned int key, unsigned int value)
93 {
94         counter_list_t *entry;
95
96         entry = calloc (1, sizeof (*entry));
97         if (entry == NULL)
98                 return (NULL);
99
100         entry->key = key;
101         entry->value = value;
102
103         if (*list == NULL)
104         {
105                 *list = entry;
106         }
107         else
108         {
109                 counter_list_t *last;
110
111                 last = *list;
112                 while (last->next != NULL)
113                         last = last->next;
114
115                 last->next = entry;
116         }
117
118         return (entry);
119 }
120
121 static void counter_list_add (counter_list_t **list,
122                 unsigned int key, unsigned int increment)
123 {
124         counter_list_t *entry;
125
126         entry = counter_list_search (list, key);
127
128         if (entry != NULL)
129         {
130                 entry->value += increment;
131         }
132         else
133         {
134                 counter_list_create (list, key, increment);
135         }
136 }
137
138 static int dns_config (const char *key, const char *value)
139 {
140         if (strcasecmp (key, "Interface") == 0)
141         {
142                 if (pcap_device != NULL)
143                         free (pcap_device);
144                 if ((pcap_device = strdup (value)) == NULL)
145                         return (1);
146         }
147         else if (strcasecmp (key, "IgnoreSource") == 0)
148         {
149                 if (value != NULL)
150                         ignore_list_add_name (value);
151         }
152         else if (strcasecmp (key, "SelectNumericQueryTypes") == 0)
153         {
154                 if ((value != NULL) && IS_FALSE (value))
155                         select_numeric_qtype = 0;
156                 else
157                         select_numeric_qtype = 1;
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                 int skip = 0;
173                 if (!select_numeric_qtype)
174                 {
175                         const char *str = qtype_str(dns->qtype);
176                         if ((str == NULL) || (str[0] == '#'))
177                                 skip = 1;
178                 }
179
180                 pthread_mutex_lock (&traffic_mutex);
181                 tr_queries += dns->length;
182                 pthread_mutex_unlock (&traffic_mutex);
183
184                 if (skip == 0)
185                 {
186                         pthread_mutex_lock (&qtype_mutex);
187                         counter_list_add (&qtype_list, dns->qtype,  1);
188                         pthread_mutex_unlock (&qtype_mutex);
189                 }
190         }
191         else
192         {
193                 /* This is a reply */
194                 pthread_mutex_lock (&traffic_mutex);
195                 tr_responses += dns->length;
196                 pthread_mutex_unlock (&traffic_mutex);
197
198                 pthread_mutex_lock (&rcode_mutex);
199                 counter_list_add (&rcode_list,  dns->rcode,  1);
200                 pthread_mutex_unlock (&rcode_mutex);
201         }
202
203         /* FIXME: Are queries, replies or both interesting? */
204         pthread_mutex_lock (&opcode_mutex);
205         counter_list_add (&opcode_list, dns->opcode, 1);
206         pthread_mutex_unlock (&opcode_mutex);
207 }
208
209 static int dns_run_pcap_loop (void)
210 {
211         pcap_t *pcap_obj;
212         char    pcap_error[PCAP_ERRBUF_SIZE];
213         struct  bpf_program fp = { 0 };
214
215         int status;
216
217         /* Don't block any signals */
218         {
219                 sigset_t sigmask;
220                 sigemptyset (&sigmask);
221                 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
222         }
223
224         /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
225         DEBUG ("dns plugin: Creating PCAP object..");
226         pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any",
227                         PCAP_SNAPLEN,
228                         0 /* Not promiscuous */,
229                         (int) CDTIME_T_TO_MS (plugin_get_interval () / 2),
230                         pcap_error);
231         if (pcap_obj == NULL)
232         {
233                 ERROR ("dns plugin: Opening interface `%s' "
234                                 "failed: %s",
235                                 (pcap_device != NULL) ? pcap_device : "any",
236                                 pcap_error);
237                 return (PCAP_ERROR);
238         }
239
240         status = pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0);
241         if (status < 0)
242         {
243                 ERROR ("dns plugin: pcap_compile failed: %s",
244                                 pcap_statustostr (status));
245                 return (status);
246         }
247
248         status = pcap_setfilter (pcap_obj, &fp);
249         if (status < 0)
250         {
251                 ERROR ("dns plugin: pcap_setfilter failed: %s",
252                                 pcap_statustostr (status));
253                 return (status);
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 /* user data */);
265         INFO ("dns plugin: pcap_loop exited with status %i.", status);
266         /* We need to handle "PCAP_ERROR" specially because libpcap currently
267          * doesn't return PCAP_ERROR_IFACE_NOT_UP for compatibility reasons. */
268         if (status == PCAP_ERROR)
269                 status = PCAP_ERROR_IFACE_NOT_UP;
270
271         pcap_close (pcap_obj);
272         return (status);
273 } /* int dns_run_pcap_loop */
274
275 static int dns_sleep_one_interval (void) /* {{{ */
276 {
277         cdtime_t interval;
278         struct timespec ts = { 0, 0 };
279         int status = 0;
280
281         interval = plugin_get_interval ();
282         CDTIME_T_TO_TIMESPEC (interval, &ts);
283
284         while (42)
285         {
286                 struct timespec rem = { 0, 0 };
287
288                 status = nanosleep (&ts, &rem);
289                 if (status == 0)
290                         break;
291                 else if ((errno == EINTR) || (errno == EAGAIN))
292                 {
293                         ts = rem;
294                         continue;
295                 }
296                 else
297                         break;
298         }
299
300         return (status);
301 } /* }}} int dns_sleep_one_interval */
302
303 static void *dns_child_loop (__attribute__((unused)) void *dummy) /* {{{ */
304 {
305         int status;
306
307         while (42)
308         {
309                 status = dns_run_pcap_loop ();
310                 if (status != PCAP_ERROR_IFACE_NOT_UP)
311                         break;
312
313                 dns_sleep_one_interval ();
314         }
315
316         if (status != PCAP_ERROR_BREAK)
317                 ERROR ("dns plugin: PCAP returned error %s.",
318                                 pcap_statustostr (status));
319
320         listen_thread_init = 0;
321         return (NULL);
322 } /* }}} void *dns_child_loop */
323
324 static int dns_init (void)
325 {
326         /* clean up an old thread */
327         int status;
328
329         pthread_mutex_lock (&traffic_mutex);
330         tr_queries   = 0;
331         tr_responses = 0;
332         pthread_mutex_unlock (&traffic_mutex);
333
334         if (listen_thread_init != 0)
335                 return (-1);
336
337         status = plugin_thread_create (&listen_thread, NULL, dns_child_loop,
338                         (void *) 0);
339         if (status != 0)
340         {
341                 char errbuf[1024];
342                 ERROR ("dns plugin: pthread_create failed: %s",
343                                 sstrerror (errno, errbuf, sizeof (errbuf)));
344                 return (-1);
345         }
346
347         listen_thread_init = 1;
348
349         return (0);
350 } /* int dns_init */
351
352 static void submit_derive (const char *type, const char *type_instance,
353                 derive_t value)
354 {
355         value_t values[1];
356         value_list_t vl = VALUE_LIST_INIT;
357
358         values[0].derive = value;
359
360         vl.values = values;
361         vl.values_len = 1;
362         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
363         sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
364         sstrncpy (vl.type, type, sizeof (vl.type));
365         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
366
367         plugin_dispatch_values (&vl);
368 } /* void submit_derive */
369
370 static void submit_octets (derive_t queries, derive_t responses)
371 {
372         value_t values[2];
373         value_list_t vl = VALUE_LIST_INIT;
374
375         values[0].derive = queries;
376         values[1].derive = responses;
377
378         vl.values = values;
379         vl.values_len = 2;
380         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
381         sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
382         sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
383
384         plugin_dispatch_values (&vl);
385 } /* void submit_octets */
386
387 static int dns_read (void)
388 {
389         unsigned int keys[T_MAX];
390         unsigned int values[T_MAX];
391         int len;
392         int i;
393
394         counter_list_t *ptr;
395
396         pthread_mutex_lock (&traffic_mutex);
397         values[0] = tr_queries;
398         values[1] = tr_responses;
399         pthread_mutex_unlock (&traffic_mutex);
400
401         if ((values[0] != 0) || (values[1] != 0))
402                 submit_octets (values[0], values[1]);
403
404         pthread_mutex_lock (&qtype_mutex);
405         for (ptr = qtype_list, len = 0;
406                         (ptr != NULL) && (len < T_MAX);
407                         ptr = ptr->next, len++)
408         {
409                 keys[len]   = ptr->key;
410                 values[len] = ptr->value;
411         }
412         pthread_mutex_unlock (&qtype_mutex);
413
414         for (i = 0; i < len; i++)
415         {
416                 DEBUG ("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]);
417                 submit_derive ("dns_qtype", qtype_str (keys[i]), values[i]);
418         }
419
420         pthread_mutex_lock (&opcode_mutex);
421         for (ptr = opcode_list, len = 0;
422                         (ptr != NULL) && (len < T_MAX);
423                         ptr = ptr->next, len++)
424         {
425                 keys[len]   = ptr->key;
426                 values[len] = ptr->value;
427         }
428         pthread_mutex_unlock (&opcode_mutex);
429
430         for (i = 0; i < len; i++)
431         {
432                 DEBUG ("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]);
433                 submit_derive ("dns_opcode", opcode_str (keys[i]), values[i]);
434         }
435
436         pthread_mutex_lock (&rcode_mutex);
437         for (ptr = rcode_list, len = 0;
438                         (ptr != NULL) && (len < T_MAX);
439                         ptr = ptr->next, len++)
440         {
441                 keys[len]   = ptr->key;
442                 values[len] = ptr->value;
443         }
444         pthread_mutex_unlock (&rcode_mutex);
445
446         for (i = 0; i < len; i++)
447         {
448                 DEBUG ("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]);
449                 submit_derive ("dns_rcode", rcode_str (keys[i]), values[i]);
450         }
451
452         return (0);
453 } /* int dns_read */
454
455 void module_register (void)
456 {
457         plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
458         plugin_register_init ("dns", dns_init);
459         plugin_register_read ("dns", dns_read);
460 } /* void module_register */