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