dns plugin: Include <pcap-bpf.h> if available.
[collectd.git] / src / dns.c
index b2039a7..364b958 100644 (file)
--- a/src/dns.c
+++ b/src/dns.c
@@ -1,11 +1,11 @@
 /**
  * collectd - src/dns.c
- * Copyright (C) 2006  Florian octo Forster
+ * Copyright (C) 2006-2011  Florian octo Forster
+ * Copyright (C) 2009       Mirko Buffoni
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.
+ * Free Software Foundation; only version 2 of the License is applicable.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  *
  * Authors:
- *   Florian octo Forster <octo at verplant.org>
+ *   Florian octo Forster <octo at collectd.org>
+ *   Mirko Buffoni <briareos at eswat.org>
  **/
 
+#define _BSD_SOURCE
+
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
 #include "configfile.h"
-#include "utils_debug.h"
-#include "utils_dns.h"
 
-#define MODULE_NAME "dns"
+#include "utils_dns.h"
+#include <pthread.h>
+#include <poll.h>
 
-#if HAVE_LIBPCAP && HAVE_LIBPTHREAD
-# include <pthread.h>
-# include <pcap.h>
-# include <sys/poll.h>
-# define DNS_HAVE_READ 1
-#else
-# define DNS_HAVE_READ 0
+#include <pcap.h>
+#if HAVE_PCAP_BPF_H
+# include <pcap-bpf.h>
 #endif
 
 /*
  * Private data types
  */
-#if DNS_HAVE_READ
 struct counter_list_s
 {
        unsigned int key;
@@ -49,60 +47,24 @@ struct counter_list_s
        struct counter_list_s *next;
 };
 typedef struct counter_list_s counter_list_t;
-#endif
 
 /*
  * Private variables
  */
-static char *traffic_file   = "dns/dns_traffic.rrd";
-static char *qtype_file   = "dns/qtype-%s.rrd";
-static char *opcode_file  = "dns/opcode-%s.rrd";
-static char *rcode_file   = "dns/rcode-%s.rrd";
-
-static char *traffic_ds_def[] =
-{
-       /* Limit to 1GBit/s */
-       "DS:queries:COUNTER:"COLLECTD_HEARTBEAT":0:125000000",
-       "DS:responses:COUNTER:"COLLECTD_HEARTBEAT":0:125000000",
-       NULL
-};
-static int traffic_ds_num = 2;
-
-static char *qtype_ds_def[] =
-{
-       "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:65535",
-       NULL
-};
-static int qtype_ds_num = 1;
-
-static char *opcode_ds_def[] =
-{
-       "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:65535",
-       NULL
-};
-static int opcode_ds_num = 1;
-
-static char *rcode_ds_def[] =
-{
-       "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:65535",
-       NULL
-};
-static int rcode_ds_num = 1;
-
-#if DNS_HAVE_READ
-static char *config_keys[] =
+static const char *config_keys[] =
 {
        "Interface",
        "IgnoreSource",
-       NULL
+       "SelectNumericQueryTypes"
 };
-static int config_keys_num = 2;
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
+static int select_numeric_qtype = 1;
 
 #define PCAP_SNAPLEN 1460
 static char   *pcap_device = NULL;
 
-static unsigned int    tr_queries;
-static unsigned int    tr_responses;
+static counter_t       tr_queries;
+static counter_t       tr_responses;
 static counter_list_t *qtype_list;
 static counter_list_t *opcode_list;
 static counter_list_t *rcode_list;
@@ -114,24 +76,22 @@ static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t qtype_mutex   = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t opcode_mutex  = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t rcode_mutex   = PTHREAD_MUTEX_INITIALIZER;
-#endif /* DNS_HAVE_READ */
 
 /*
  * Private functions
  */
-#if DNS_HAVE_READ
 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
 {
        counter_list_t *entry;
 
-       DBG ("counter_list_search (list = %p, key = %u)",
+       DEBUG ("counter_list_search (list = %p, key = %u)",
                        (void *) *list, key);
 
        for (entry = *list; entry != NULL; entry = entry->next)
                if (entry->key == key)
                        break;
 
-       DBG ("return (%p)", (void *) entry);
+       DEBUG ("return (%p)", (void *) entry);
        return (entry);
 }
 
@@ -140,7 +100,7 @@ static counter_list_t *counter_list_create (counter_list_t **list,
 {
        counter_list_t *entry;
 
-       DBG ("counter_list_create (list = %p, key = %u, value = %u)",
+       DEBUG ("counter_list_create (list = %p, key = %u, value = %u)",
                        (void *) *list, key, value);
 
        entry = (counter_list_t *) malloc (sizeof (counter_list_t));
@@ -166,7 +126,7 @@ static counter_list_t *counter_list_create (counter_list_t **list,
                last->next = entry;
        }
 
-       DBG ("return (%p)", (void *) entry);
+       DEBUG ("return (%p)", (void *) entry);
        return (entry);
 }
 
@@ -175,7 +135,7 @@ static void counter_list_add (counter_list_t **list,
 {
        counter_list_t *entry;
 
-       DBG ("counter_list_add (list = %p, key = %u, increment = %u)",
+       DEBUG ("counter_list_add (list = %p, key = %u, increment = %u)",
                        (void *) *list, key, increment);
 
        entry = counter_list_search (list, key);
@@ -188,10 +148,10 @@ static void counter_list_add (counter_list_t **list,
        {
                counter_list_create (list, key, increment);
        }
-       DBG ("return ()");
+       DEBUG ("return ()");
 }
 
-static int dns_config (char *key, char *value)
+static int dns_config (const char *key, const char *value)
 {
        if (strcasecmp (key, "Interface") == 0)
        {
@@ -205,6 +165,13 @@ static int dns_config (char *key, char *value)
                if (value != NULL)
                        ignore_list_add_name (value);
        }
+       else if (strcasecmp (key, "SelectNumericQueryTypes") == 0)
+       {
+               if ((value != NULL) && IS_FALSE (value))
+                       select_numeric_qtype = 0;
+               else
+                       select_numeric_qtype = 1;
+       }
        else
        {
                return (-1);
@@ -218,13 +185,24 @@ static void dns_child_callback (const rfc1035_header_t *dns)
        if (dns->qr == 0)
        {
                /* This is a query */
+               int skip = 0;
+               if (!select_numeric_qtype)
+               {
+                       const char *str = qtype_str(dns->qtype);
+                       if ((str == NULL) || (str[0] == '#'))
+                               skip = 1;
+               }
+
                pthread_mutex_lock (&traffic_mutex);
                tr_queries += dns->length;
                pthread_mutex_unlock (&traffic_mutex);
 
-               pthread_mutex_lock (&qtype_mutex);
-               counter_list_add (&qtype_list,  dns->qtype,  1);
-               pthread_mutex_unlock (&qtype_mutex);
+               if (skip == 0)
+               {
+                       pthread_mutex_lock (&qtype_mutex);
+                       counter_list_add (&qtype_list, dns->qtype,  1);
+                       pthread_mutex_unlock (&qtype_mutex);
+               }
        }
        else
        {
@@ -244,13 +222,12 @@ static void dns_child_callback (const rfc1035_header_t *dns)
        pthread_mutex_unlock (&opcode_mutex);
 }
 
-static void *dns_child_loop (void *dummy)
+static void *dns_child_loop (__attribute__((unused)) void *dummy)
 {
        pcap_t *pcap_obj;
        char    pcap_error[PCAP_ERRBUF_SIZE];
        struct  bpf_program fp;
 
-       struct pollfd poll_fds[1];
        int status;
 
        /* Don't block any signals */
@@ -261,15 +238,15 @@ static void *dns_child_loop (void *dummy)
        }
 
        /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
-       DBG ("Creating PCAP object..");
-       pcap_obj = pcap_open_live (pcap_device,
+       DEBUG ("dns plugin: Creating PCAP object..");
+       pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any",
                        PCAP_SNAPLEN,
                        0 /* Not promiscuous */,
-                       0 /* no read timeout */,
+                       interval_g,
                        pcap_error);
        if (pcap_obj == NULL)
        {
-               syslog (LOG_ERR, "dns plugin: Opening interface `%s' "
+               ERROR ("dns plugin: Opening interface `%s' "
                                "failed: %s",
                                (pcap_device != NULL) ? pcap_device : "any",
                                pcap_error);
@@ -279,80 +256,39 @@ static void *dns_child_loop (void *dummy)
        memset (&fp, 0, sizeof (fp));
        if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
        {
-               DBG ("pcap_compile failed");
-               syslog (LOG_ERR, "dns plugin: pcap_compile failed");
+               ERROR ("dns plugin: pcap_compile failed");
                return (NULL);
        }
        if (pcap_setfilter (pcap_obj, &fp) < 0)
        {
-               DBG ("pcap_setfilter failed");
-               syslog (LOG_ERR, "dns plugin: pcap_setfilter failed");
+               ERROR ("dns plugin: pcap_setfilter failed");
                return (NULL);
        }
 
-       DBG ("PCAP object created.");
+       DEBUG ("PCAP object created.");
 
        dnstop_set_pcap_obj (pcap_obj);
        dnstop_set_callback (dns_child_callback);
 
-       /* Set up poll object */
-       poll_fds[0].fd = pcap_fileno (pcap_obj);
-       poll_fds[0].events = POLLIN | POLLPRI;
+       status = pcap_loop (pcap_obj,
+                       -1 /* loop forever */,
+                       handle_pcap /* callback */,
+                       NULL /* Whatever this means.. */);
+       if (status < 0)
+               ERROR ("dns plugin: Listener thread is exiting "
+                               "abnormally: %s", pcap_geterr (pcap_obj));
 
-       while (42)
-       {
-               DBG ("poll (...)");
-               status = poll (poll_fds, 1, -1 /* wait forever for a change */);
-
-               /* Signals are not caught, but this is very handy when
-                * attaching to the process with a debugger. -octo */
-               if ((status < 0) && (errno == EINTR))
-               {
-                       errno = 0;
-                       continue;
-               }
-
-               if (status < 0)
-               {
-                       syslog (LOG_ERR, "dns plugin: poll(2) failed: %s",
-                                       strerror (errno));
-                       break;
-               }
-
-               if (poll_fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
-               {
-                       DBG ("pcap-device closed. Exiting.");
-                       syslog (LOG_ERR, "dns plugin: pcap-device closed. Exiting.");
-                       break;
-               }
-               else if (poll_fds[0].revents & (POLLIN | POLLPRI))
-               {
-                       status = pcap_dispatch (pcap_obj,
-                                       10 /* Only handle 10 packets at a time */,
-                                       handle_pcap /* callback */,
-                                       NULL /* Whatever this means.. */);
-                       if (status < 0)
-                       {
-                               DBG ("pcap_dispatch failed: %s", pcap_geterr (pcap_obj));
-                               syslog (LOG_ERR, "dns plugin: pcap_dispatch failed: %s",
-                                               pcap_geterr (pcap_obj));
-                               break;
-                       }
-               }
-       } /* while (42) */
-
-       DBG ("child is exiting");
+       DEBUG ("child is exiting");
 
        pcap_close (pcap_obj);
+       listen_thread_init = 0;
        pthread_exit (NULL);
 
        return (NULL);
 } /* static void dns_child_loop (void) */
-#endif /* DNS_HAVE_READ */
 
-static void dns_init (void)
+static int dns_init (void)
 {
-#if DNS_HAVE_READ
        /* clean up an old thread */
        int status;
 
@@ -362,131 +298,59 @@ static void dns_init (void)
        pthread_mutex_unlock (&traffic_mutex);
 
        if (listen_thread_init != 0)
-               return;
+               return (-1);
 
        status = pthread_create (&listen_thread, NULL, dns_child_loop,
                        (void *) 0);
        if (status != 0)
        {
-               syslog (LOG_ERR, "dns plugin: pthread_create failed: %s",
-                               strerror (status));
-               return;
+               char errbuf[1024];
+               ERROR ("dns plugin: pthread_create failed: %s",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+               return (-1);
        }
 
        listen_thread_init = 1;
-#endif /* DNS_HAVE_READ */
-}
 
-static void traffic_write (char *host, char *inst, char *val)
-{
-       rrd_update_file (host, traffic_file, val,
-                       traffic_ds_def, traffic_ds_num);
-}
-
-static void qtype_write (char *host, char *inst, char *val)
-{
-       char file[512];
-       int status;
-
-       status = snprintf (file, 512, qtype_file, inst);
-       if (status < 1)
-               return;
-       else if (status >= 512)
-               return;
-
-       rrd_update_file (host, file, val, qtype_ds_def, qtype_ds_num);
-}
-
-static void rcode_write (char *host, char *inst, char *val)
-{
-       char file[512];
-       int status;
-
-       status = snprintf (file, 512, rcode_file, inst);
-       if (status < 1)
-               return;
-       else if (status >= 512)
-               return;
-
-       rrd_update_file (host, file, val, rcode_ds_def, rcode_ds_num);
-}
-
-static void opcode_write (char *host, char *inst, char *val)
-{
-       char file[512];
-       int status;
-
-       status = snprintf (file, 512, opcode_file, inst);
-       if (status < 1)
-               return;
-       else if (status >= 512)
-               return;
-
-       rrd_update_file (host, file, val, opcode_ds_def, opcode_ds_num);
-}
-
-#if DNS_HAVE_READ
-static void traffic_submit (unsigned int queries, unsigned int replies)
-{
-       char buffer[64];
-       int  status;
-
-       status = snprintf (buffer, 64, "N:%u:%u", queries, replies);
-       if ((status < 1) || (status >= 64))
-               return;
-
-       plugin_submit ("dns_traffic", "-", buffer);
-}
+       return (0);
+} /* int dns_init */
 
-static void qtype_submit (int qtype, unsigned int counter)
+static void submit_counter (const char *type, const char *type_instance,
+               counter_t value)
 {
-       char inst[32];
-       char buffer[32];
-       int  status;
+       value_t values[1];
+       value_list_t vl = VALUE_LIST_INIT;
 
-       strncpy (inst, qtype_str (qtype), 32);
-       inst[31] = '\0';
+       values[0].counter = value;
 
-       status = snprintf (buffer, 32, "N:%u", counter);
-       if ((status < 1) || (status >= 32))
-               return;
+       vl.values = values;
+       vl.values_len = 1;
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-       plugin_submit ("dns_qtype", inst, buffer);
-}
+       plugin_dispatch_values (&vl);
+} /* void submit_counter */
 
-static void rcode_submit (int rcode, unsigned int counter)
+static void submit_octets (counter_t queries, counter_t responses)
 {
-       char inst[32];
-       char buffer[32];
-       int  status;
+       value_t values[2];
+       value_list_t vl = VALUE_LIST_INIT;
 
-       strncpy (inst, rcode_str (rcode), 32);
-       inst[31] = '\0';
+       values[0].counter = queries;
+       values[1].counter = responses;
 
-       status = snprintf (buffer, 32, "N:%u", counter);
-       if ((status < 1) || (status >= 32))
-               return;
+       vl.values = values;
+       vl.values_len = 2;
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
+       sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
 
-       plugin_submit ("dns_rcode", inst, buffer);
-}
+       plugin_dispatch_values (&vl);
+} /* void submit_counter */
 
-static void opcode_submit (int opcode, unsigned int counter)
-{
-       char inst[32];
-       char buffer[32];
-       int  status;
-
-       strncpy (inst, opcode_str (opcode), 32);
-       inst[31] = '\0';
-
-       status = snprintf (buffer, 32, "N:%u", counter);
-       if ((status < 1) || (status >= 32))
-               return;
-
-       plugin_submit ("dns_opcode", inst, buffer);
-}
-
-static void dns_read (void)
+static int dns_read (void)
 {
        unsigned int keys[T_MAX];
        unsigned int values[T_MAX];
@@ -499,7 +363,9 @@ static void dns_read (void)
        values[0] = tr_queries;
        values[1] = tr_responses;
        pthread_mutex_unlock (&traffic_mutex);
-       traffic_submit (values[0], values[1]);
+
+       if ((values[0] != 0) || (values[1] != 0))
+               submit_octets (values[0], values[1]);
 
        pthread_mutex_lock (&qtype_mutex);
        for (ptr = qtype_list, len = 0;
@@ -513,8 +379,8 @@ static void dns_read (void)
 
        for (i = 0; i < len; i++)
        {
-               DBG ("qtype = %u; counter = %u;", keys[i], values[i]);
-               qtype_submit (keys[i], values[i]);
+               DEBUG ("qtype = %u; counter = %u;", keys[i], values[i]);
+               submit_counter ("dns_qtype", qtype_str (keys[i]), values[i]);
        }
 
        pthread_mutex_lock (&opcode_mutex);
@@ -529,8 +395,8 @@ static void dns_read (void)
 
        for (i = 0; i < len; i++)
        {
-               DBG ("opcode = %u; counter = %u;", keys[i], values[i]);
-               opcode_submit (keys[i], values[i]);
+               DEBUG ("opcode = %u; counter = %u;", keys[i], values[i]);
+               submit_counter ("dns_opcode", opcode_str (keys[i]), values[i]);
        }
 
        pthread_mutex_lock (&rcode_mutex);
@@ -545,24 +411,16 @@ static void dns_read (void)
 
        for (i = 0; i < len; i++)
        {
-               DBG ("rcode = %u; counter = %u;", keys[i], values[i]);
-               rcode_submit (keys[i], values[i]);
+               DEBUG ("rcode = %u; counter = %u;", keys[i], values[i]);
+               submit_counter ("dns_rcode", rcode_str (keys[i]), values[i]);
        }
-}
-#else /* if !DNS_HAVE_READ */
-# define dns_read NULL
-#endif
+
+       return (0);
+} /* int dns_read */
 
 void module_register (void)
 {
-       plugin_register (MODULE_NAME, dns_init, dns_read, NULL);
-       plugin_register ("dns_traffic", NULL, NULL, traffic_write);
-       plugin_register ("dns_qtype", NULL, NULL, qtype_write);
-       plugin_register ("dns_rcode", NULL, NULL, rcode_write);
-       plugin_register ("dns_opcode", NULL, NULL, opcode_write);
-#if DNS_HAVE_READ
-       cf_register (MODULE_NAME, dns_config, config_keys, config_keys_num);
-#endif
-}
-
-#undef MODULE_NAME
+       plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
+       plugin_register_init ("dns", dns_init);
+       plugin_register_read ("dns", dns_read);
+} /* void module_register */