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