dns plugin: Use threads rather than two processes and a pipe.
[collectd.git] / src / dns.c
1 /**
2  * collectd - src/dns.c
3  * Copyright (C) 2006  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; either version 2 of the License, or (at your
8  * option) any later version.
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 verplant.org>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27 #include "utils_debug.h"
28
29 #if HAVE_PTHREAD_H
30 # include <pthread.h>
31 #endif
32
33 #if HAVE_SYS_POLL_H
34 # include <sys/poll.h>
35 #endif
36
37 #define MODULE_NAME "dns"
38
39 #if HAVE_LIBPCAP
40 # define NAMED_HAVE_CONFIG 1
41 #else
42 # define NAMED_HAVE_CONFIG 0
43 #endif
44
45 #if HAVE_LIBPCAP && HAVE_PTHREAD_H
46 # include "utils_dns.h"
47 # define NAMED_HAVE_READ 1
48 #else
49 # define NAMED_HAVE_READ 0
50 #endif
51
52 struct counter_list_s
53 {
54         unsigned int key;
55         unsigned int value;
56         struct counter_list_s *next;
57 };
58 typedef struct counter_list_s counter_list_t;
59
60 static char *traffic_file   = "dns/dns_traffic.rrd";
61 static char *qtype_file   = "dns/qtype-%s.rrd";
62 static char *opcode_file  = "dns/opcode-%s.rrd";
63 static char *rcode_file   = "dns/rcode-%s.rrd";
64
65 static char *traffic_ds_def[] =
66 {
67         /* Limit to 1GBit/s */
68         "DS:queries:COUNTER:"COLLECTD_HEARTBEAT":0:125000000",
69         "DS:responses:COUNTER:"COLLECTD_HEARTBEAT":0:125000000",
70         NULL
71 };
72 static int traffic_ds_num = 2;
73
74 static char *qtype_ds_def[] =
75 {
76         "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:65535",
77         NULL
78 };
79 static int qtype_ds_num = 1;
80
81 static char *opcode_ds_def[] =
82 {
83         "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:65535",
84         NULL
85 };
86 static int opcode_ds_num = 1;
87
88 static char *rcode_ds_def[] =
89 {
90         "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:65535",
91         NULL
92 };
93 static int rcode_ds_num = 1;
94
95 /* FIXME: Wouldn't other defines be better? -octo */
96 #if NAMED_HAVE_CONFIG
97 #if HAVE_LIBPCAP
98 static char *config_keys[] =
99 {
100         "Interface",
101         "IgnoreSource",
102         NULL
103 };
104 static int config_keys_num = 2;
105 #endif /* HAVE_LIBPCAP */
106 #endif /* NAMED_HAVE_CONFIG */
107
108 #if HAVE_LIBPCAP
109 #define PCAP_SNAPLEN 1460
110 static char   *pcap_device = NULL;
111
112 static unsigned int    tr_queries;
113 static unsigned int    tr_responses;
114 static counter_list_t *qtype_list;
115 static counter_list_t *opcode_list;
116 static counter_list_t *rcode_list;
117 #endif
118
119 #if HAVE_PTHREAD_H
120 static pthread_t       listen_thread;
121 static int             listen_thread_init = 0;
122 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
123 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
124 static pthread_mutex_t qtype_mutex   = PTHREAD_MUTEX_INITIALIZER;
125 static pthread_mutex_t opcode_mutex  = PTHREAD_MUTEX_INITIALIZER;
126 static pthread_mutex_t rcode_mutex   = PTHREAD_MUTEX_INITIALIZER;
127 #endif
128
129 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
130 {
131         counter_list_t *entry;
132
133         DBG ("counter_list_search (list = %p, key = %u)",
134                         (void *) *list, key);
135
136         for (entry = *list; entry != NULL; entry = entry->next)
137                 if (entry->key == key)
138                         break;
139
140         DBG ("return (%p)", (void *) entry);
141         return (entry);
142 }
143
144 static counter_list_t *counter_list_create (counter_list_t **list,
145                 unsigned int key, unsigned int value)
146 {
147         counter_list_t *entry;
148
149         DBG ("counter_list_create (list = %p, key = %u, value = %u)",
150                         (void *) *list, key, value);
151
152         entry = (counter_list_t *) malloc (sizeof (counter_list_t));
153         if (entry == NULL)
154                 return (NULL);
155
156         memset (entry, 0, sizeof (counter_list_t));
157         entry->key = key;
158         entry->value = value;
159
160         if (*list == NULL)
161         {
162                 *list = entry;
163         }
164         else
165         {
166                 counter_list_t *last;
167
168                 last = *list;
169                 while (last->next != NULL)
170                         last = last->next;
171
172                 last->next = entry;
173         }
174
175         DBG ("return (%p)", (void *) entry);
176         return (entry);
177 }
178
179 static void counter_list_add (counter_list_t **list,
180                 unsigned int key, unsigned int increment)
181 {
182         counter_list_t *entry;
183
184         DBG ("counter_list_add (list = %p, key = %u, increment = %u)",
185                         (void *) *list, key, increment);
186
187         entry = counter_list_search (list, key);
188
189         if (entry != NULL)
190         {
191                 entry->value += increment;
192         }
193         else
194         {
195                 counter_list_create (list, key, increment);
196         }
197         DBG ("return ()");
198 }
199
200 #if NAMED_HAVE_CONFIG
201 static int dns_config (char *key, char *value)
202 {
203 #if HAVE_LIBPCAP
204         if (strcasecmp (key, "Interface") == 0)
205         {
206                 if (pcap_device != NULL)
207                         free (pcap_device);
208                 if ((pcap_device = strdup (value)) == NULL)
209                         return (1);
210         }
211         else if (strcasecmp (key, "IgnoreSource") == 0)
212         {
213                 if (value != NULL)
214                         ignore_list_add_name (value);
215         }
216         else
217         {
218                 return (-1);
219         }
220
221         return (0);
222 #endif /* HAVE_LIBPCAP */
223 }
224 #endif /* NAMED_HAVE_CONFIG */
225
226 static void dns_child_callback (const rfc1035_header_t *dns)
227 {
228         if (dns->qr == 0)
229         {
230                 /* This is a query */
231                 pthread_mutex_lock (&traffic_mutex);
232                 tr_queries += dns->length;
233                 pthread_mutex_unlock (&traffic_mutex);
234
235                 pthread_mutex_lock (&qtype_mutex);
236                 counter_list_add (&qtype_list,  dns->qtype,  1);
237                 pthread_mutex_unlock (&qtype_mutex);
238         }
239         else
240         {
241                 /* This is a reply */
242                 pthread_mutex_lock (&traffic_mutex);
243                 tr_responses += dns->length;
244                 pthread_mutex_unlock (&traffic_mutex);
245
246                 pthread_mutex_lock (&rcode_mutex);
247                 counter_list_add (&rcode_list,  dns->rcode,  1);
248                 pthread_mutex_unlock (&rcode_mutex);
249         }
250
251         /* FIXME: Are queries, replies or both interesting? */
252         pthread_mutex_lock (&opcode_mutex);
253         counter_list_add (&opcode_list, dns->opcode, 1);
254         pthread_mutex_unlock (&opcode_mutex);
255 }
256
257 static void *dns_child_loop (void *dummy)
258 {
259         pcap_t *pcap_obj;
260         char    pcap_error[PCAP_ERRBUF_SIZE];
261         struct  bpf_program fp;
262
263         struct pollfd poll_fds[1];
264         int status;
265
266         /* Don't catch these signals */
267         /* FIXME: Really? */
268         signal (SIGINT, SIG_DFL);
269         signal (SIGTERM, SIG_DFL);
270
271         /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
272         DBG ("Creating PCAP object..");
273         pcap_obj = pcap_open_live (pcap_device,
274                         PCAP_SNAPLEN,
275                         0 /* Not promiscuous */,
276                         0 /* no read timeout */,
277                         pcap_error);
278         if (pcap_obj == NULL)
279         {
280                 syslog (LOG_ERR, "dns plugin: Opening interface `%s' "
281                                 "failed: %s",
282                                 (pcap_device != NULL) ? pcap_device : "any",
283                                 pcap_error);
284                 return (NULL);
285         }
286
287         memset (&fp, 0, sizeof (fp));
288         if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
289         {
290                 DBG ("pcap_compile failed");
291                 syslog (LOG_ERR, "dns plugin: pcap_compile failed");
292                 return (NULL);
293         }
294         if (pcap_setfilter (pcap_obj, &fp) < 0)
295         {
296                 DBG ("pcap_setfilter failed");
297                 syslog (LOG_ERR, "dns plugin: pcap_setfilter failed");
298                 return (NULL);
299         }
300
301         DBG ("PCAP object created.");
302
303         dnstop_set_pcap_obj (pcap_obj);
304         dnstop_set_callback (dns_child_callback);
305
306         /* Set up poll object */
307         poll_fds[0].fd = pcap_fileno (pcap_obj);
308         poll_fds[0].events = POLLIN | POLLPRI;
309
310         while (42)
311         {
312                 DBG ("poll (...)");
313                 status = poll (poll_fds, 1, -1 /* wait forever for a change */);
314
315                 /* Signals are not caught, but this is very handy when
316                  * attaching to the process with a debugger. -octo */
317                 if ((status < 0) && (errno == EINTR))
318                 {
319                         errno = 0;
320                         continue;
321                 }
322
323                 if (status < 0)
324                 {
325                         syslog (LOG_ERR, "dns plugin: poll(2) failed: %s",
326                                         strerror (errno));
327                         break;
328                 }
329
330                 if (poll_fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
331                 {
332                         DBG ("pcap-device closed. Exiting.");
333                         syslog (LOG_ERR, "dns plugin: pcap-device closed. Exiting.");
334                         break;
335                 }
336                 else if (poll_fds[0].revents & (POLLIN | POLLPRI))
337                 {
338                         status = pcap_dispatch (pcap_obj,
339                                         10 /* Only handle 10 packets at a time */,
340                                         handle_pcap /* callback */,
341                                         NULL /* Whatever this means.. */);
342                         if (status < 0)
343                         {
344                                 DBG ("pcap_dispatch failed: %s", pcap_geterr (pcap_obj));
345                                 syslog (LOG_ERR, "dns plugin: pcap_dispatch failed: %s",
346                                                 pcap_geterr (pcap_obj));
347                                 break;
348                         }
349                 }
350         } /* while (42) */
351
352         DBG ("child is exiting");
353
354         pcap_close (pcap_obj);
355         pthread_exit (NULL);
356
357         return (NULL);
358 } /* static void dns_child_loop (void) */
359
360 static void dns_init (void)
361 {
362 #if HAVE_LIBPCAP
363 #if HAVE_PTHREAD_H
364         /* clean up an old thread */
365         int status;
366
367         pthread_mutex_lock (&traffic_mutex);
368         tr_queries   = 0;
369         tr_responses = 0;
370         pthread_mutex_unlock (&traffic_mutex);
371
372         if (listen_thread_init != 0)
373                 return;
374
375         status = pthread_create (&listen_thread, NULL, dns_child_loop,
376                         (void *) 0);
377         if (status != 0)
378         {
379                 syslog (LOG_ERR, "dns plugin: pthread_create failed: %s",
380                                 strerror (status));
381                 return;
382         }
383
384         listen_thread_init = 1;
385 #endif
386 #endif
387 }
388
389 static void traffic_write (char *host, char *inst, char *val)
390 {
391         rrd_update_file (host, traffic_file, val,
392                         traffic_ds_def, traffic_ds_num);
393 }
394
395 static void qtype_write (char *host, char *inst, char *val)
396 {
397         char file[512];
398         int status;
399
400         status = snprintf (file, 512, qtype_file, inst);
401         if (status < 1)
402                 return;
403         else if (status >= 512)
404                 return;
405
406         rrd_update_file (host, file, val, qtype_ds_def, qtype_ds_num);
407 }
408
409 static void rcode_write (char *host, char *inst, char *val)
410 {
411         char file[512];
412         int status;
413
414         status = snprintf (file, 512, rcode_file, inst);
415         if (status < 1)
416                 return;
417         else if (status >= 512)
418                 return;
419
420         rrd_update_file (host, file, val, rcode_ds_def, rcode_ds_num);
421 }
422
423 static void opcode_write (char *host, char *inst, char *val)
424 {
425         char file[512];
426         int status;
427
428         status = snprintf (file, 512, opcode_file, inst);
429         if (status < 1)
430                 return;
431         else if (status >= 512)
432                 return;
433
434         rrd_update_file (host, file, val, opcode_ds_def, opcode_ds_num);
435 }
436
437 static void traffic_submit (unsigned int queries, unsigned int replies)
438 {
439         char buffer[64];
440         int  status;
441
442         status = snprintf (buffer, 64, "N:%u:%u", queries, replies);
443         if ((status < 1) || (status >= 64))
444                 return;
445
446         plugin_submit ("dns_traffic", "-", buffer);
447 }
448
449 static void qtype_submit (int qtype, unsigned int counter)
450 {
451         char inst[32];
452         char buffer[32];
453         int  status;
454
455         strncpy (inst, qtype_str (qtype), 32);
456         inst[31] = '\0';
457
458         status = snprintf (buffer, 32, "N:%u", counter);
459         if ((status < 1) || (status >= 32))
460                 return;
461
462         plugin_submit ("dns_qtype", inst, buffer);
463 }
464
465 static void rcode_submit (int rcode, unsigned int counter)
466 {
467         char inst[32];
468         char buffer[32];
469         int  status;
470
471         strncpy (inst, rcode_str (rcode), 32);
472         inst[31] = '\0';
473
474         status = snprintf (buffer, 32, "N:%u", counter);
475         if ((status < 1) || (status >= 32))
476                 return;
477
478         plugin_submit ("dns_rcode", inst, buffer);
479 }
480
481 static void opcode_submit (int opcode, unsigned int counter)
482 {
483         char inst[32];
484         char buffer[32];
485         int  status;
486
487         strncpy (inst, opcode_str (opcode), 32);
488         inst[31] = '\0';
489
490         status = snprintf (buffer, 32, "N:%u", counter);
491         if ((status < 1) || (status >= 32))
492                 return;
493
494         plugin_submit ("dns_opcode", inst, buffer);
495 }
496
497 #if NAMED_HAVE_READ
498 static void dns_read (void)
499 {
500         unsigned int keys[T_MAX];
501         unsigned int values[T_MAX];
502         int len;
503         int i;
504
505         counter_list_t *ptr;
506
507         pthread_mutex_lock (&traffic_mutex);
508         values[0] = tr_queries;
509         values[1] = tr_responses;
510         pthread_mutex_unlock (&traffic_mutex);
511         traffic_submit (values[0], values[1]);
512
513         pthread_mutex_lock (&qtype_mutex);
514         for (ptr = qtype_list, len = 0;
515                         (ptr != NULL) && (len < T_MAX);
516                         ptr = ptr->next, len++)
517         {
518                 keys[len]   = ptr->key;
519                 values[len] = ptr->value;
520         }
521         pthread_mutex_unlock (&qtype_mutex);
522
523         for (i = 0; i < len; i++)
524         {
525                 DBG ("qtype = %u; counter = %u;", keys[i], values[i]);
526                 qtype_submit (keys[i], values[i]);
527         }
528
529         pthread_mutex_lock (&opcode_mutex);
530         for (ptr = opcode_list, len = 0;
531                         (ptr != NULL) && (len < T_MAX);
532                         ptr = ptr->next, len++)
533         {
534                 keys[len]   = ptr->key;
535                 values[len] = ptr->value;
536         }
537         pthread_mutex_unlock (&opcode_mutex);
538
539         for (i = 0; i < len; i++)
540         {
541                 DBG ("opcode = %u; counter = %u;", keys[i], values[i]);
542                 opcode_submit (keys[i], values[i]);
543         }
544
545         pthread_mutex_lock (&rcode_mutex);
546         for (ptr = rcode_list, len = 0;
547                         (ptr != NULL) && (len < T_MAX);
548                         ptr = ptr->next, len++)
549         {
550                 keys[len]   = ptr->key;
551                 values[len] = ptr->value;
552         }
553         pthread_mutex_unlock (&rcode_mutex);
554
555         for (i = 0; i < len; i++)
556         {
557                 DBG ("rcode = %u; counter = %u;", keys[i], values[i]);
558                 rcode_submit (keys[i], values[i]);
559         }
560 }
561 #else /* if !NAMED_HAVE_READ */
562 # define dns_read NULL
563 #endif
564
565 void module_register (void)
566 {
567         plugin_register (MODULE_NAME, dns_init, dns_read, NULL);
568         plugin_register ("dns_traffic", NULL, NULL, traffic_write);
569         plugin_register ("dns_qtype", NULL, NULL, qtype_write);
570         plugin_register ("dns_rcode", NULL, NULL, rcode_write);
571         plugin_register ("dns_opcode", NULL, NULL, opcode_write);
572         cf_register (MODULE_NAME, dns_config, config_keys, config_keys_num);
573 }
574
575 #undef MODULE_NAME