Merge branch 'collectd-4.2' into collectd-4.3
[collectd.git] / src / iptables.c
index 84878fd..72b4481 100644 (file)
 #include "common.h"
 #include "plugin.h"
 #include "configfile.h"
-#include "utils_debug.h"
 
 #if HAVE_LIBIPTC_LIBIPTC_H
 # include <libiptc/libiptc.h>
 #endif
 
-#if HAVE_LIBIPTC_LIBIPTC_H
-# define IPTABLES_HAVE_READ 1
-#else
-# define IPTABLES_HAVE_READ 0
-#endif
-
-#define MODULE_NAME "iptables"
-#define BUFSIZE 512
-
 /*
  * (Module-)Global variables
  */
 
 /*
- * Removed packet count for now, should have config option if you want to save
- * them Although other collectd models don't seem to care much for options
- * eitherway for what to log
- */
-/* Limit to ~125MByte/s (~1GBit/s) */
-static char *ds_def[] =
-{
-       "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:134217728",
-       NULL
-};
-static int ds_num = 1;
-
-#if IPTABLES_HAVE_READ
-/*
  * Config format should be `Chain table chainname',
  * e. g. `Chain mangle incoming'
  */
-static char *config_keys[] =
+static const char *config_keys[] =
 {
        "Chain",
        NULL
@@ -93,7 +69,7 @@ typedef struct {
 static ip_chain_t **chain_list = NULL;
 static int chain_num = 0;
 
-static int iptables_config (char *key, char *value)
+static int iptables_config (const char *key, const char *value)
 {
        if (strcasecmp (key, "Chain") == 0)
        {
@@ -112,7 +88,9 @@ static int iptables_config (char *key, char *value)
                value_copy = strdup (value);
                if (value_copy == NULL)
                {
-                   syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
+                   char errbuf[1024];
+                   ERROR ("strdup failed: %s",
+                           sstrerror (errno, errbuf, sizeof (errbuf)));
                    return (1);
                }
 
@@ -128,9 +106,9 @@ static int iptables_config (char *key, char *value)
                chain = fields[1];
 
                table_len = strlen (table);
-               if (table_len >= sizeof(temp.table))
+               if ((unsigned int)table_len >= sizeof(temp.table))
                {
-                       syslog (LOG_ERR, "Table `%s' too long.", table);
+                       ERROR ("Table `%s' too long.", table);
                        free (value_copy);
                        return (1);
                }
@@ -138,9 +116,9 @@ static int iptables_config (char *key, char *value)
                temp.table[table_len] = '\0';
 
                chain_len = strlen (chain);
-               if (chain_len >= sizeof(temp.chain))
+               if ((unsigned int)chain_len >= sizeof(temp.chain))
                {
-                       syslog (LOG_ERR, "Chain `%s' too long.", chain);
+                       ERROR ("Chain `%s' too long.", chain);
                        free (value_copy);
                        return (1);
                }
@@ -159,8 +137,12 @@ static int iptables_config (char *key, char *value)
                    }
                    else
                    {
-                       strncpy (temp.rule.comment, comment,
-                               sizeof (temp.rule.comment) - 1);
+                       temp.rule.comment = strdup (comment);
+                       if (temp.rule.comment == NULL)
+                       {
+                           free (value_copy);
+                           return (1);
+                       }
                        temp.rule_type = RTYPE_COMMENT;
                    }
                }
@@ -180,22 +162,26 @@ static int iptables_config (char *key, char *value)
                list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
                if (list == NULL)
                {
-                       syslog (LOG_ERR, "realloc failed: %s", strerror (errno));
-                       return (1);
+                   char errbuf[1024];
+                   ERROR ("realloc failed: %s",
+                           sstrerror (errno, errbuf, sizeof (errbuf)));
+                   return (1);
                }
 
                chain_list = list;
                final = (ip_chain_t *) malloc( sizeof(temp) );
                if (final == NULL) 
                {
-                       syslog (LOG_ERR, "malloc failed: %s", strerror (errno));
-                       return (1);
+                   char errbuf[1024];
+                   ERROR ("malloc failed: %s",
+                           sstrerror (errno, errbuf, sizeof (errbuf)));
+                   return (1);
                }
                memcpy (final, &temp, sizeof (temp));
                chain_list[chain_num] = final;
                chain_num++;
 
-               DBG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
+               DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
        }
        else 
        {
@@ -203,65 +189,19 @@ static int iptables_config (char *key, char *value)
        }
 
        return (0);
-}
-#endif /* IPTABLES_HAVE_READ */
-
-static void iptables_write (char *host, char *orig_inst, char *val, char *type) 
-{
-    char *table;
-    char *inst;
-    char file[256];
-    int status;
-
-    table = strdup (orig_inst);
-    if (table == NULL)
-       return;
-    inst = strchr (table, ',');
-    if (inst == NULL)
-    {
-       free (table);
-       return;
-    }
-
-    *inst = '\0';
-    inst++;
-    if (*inst == '\0')
-    {
-       free (table);
-       return;
-    }
-
-    status = snprintf (file, sizeof (file), "iptables-%s/%s-%s.rrd",
-           table, type, inst);
-    free (table);
-    if ((status >= sizeof (file)) || (status < 1))
-       return;
+} /* int iptables_config */
 
-    rrd_update_file (host, file, val, ds_def, ds_num);
-} /* void iptables_write */
-
-static void iptables_write_bytes (char *host, char *inst, char *val)
-{
-    iptables_write (host, inst, val, "ipt_bytes");
-}
-
-static void iptables_write_packets (char *host, char *inst, char *val)
-{
-    iptables_write (host, inst, val, "ipt_packets");
-}
-
-#if IPTABLES_HAVE_READ
+/* This needs to return `int' for IPT_MATCH_ITERATE to work. */
 static int submit_match (const struct ipt_entry_match *match,
                const struct ipt_entry *entry,
                const ip_chain_t *chain,
                int rule_num) 
 {
-    char inst[64];
-    char value[64];
     int status;
+    value_t values[1];
+    value_list_t vl = VALUE_LIST_INIT;
 
-    /* Only log rules that have a comment, although could probably also do
-     * numerical targets sometime */
+    /* Select the rules to collect */
     if (chain->rule_type == RTYPE_NUM)
     {
        if (chain->rule.num != rule_num)
@@ -270,46 +210,46 @@ static int submit_match (const struct ipt_entry_match *match,
     else
     {
        if (strcmp (match->u.user.name, "comment") != 0)
-           return 0;
+           return (0);
        if ((chain->rule_type == RTYPE_COMMENT)
                && (strcmp (chain->rule.comment, (char *) match->data) != 0))
            return (0);
     }
 
+    vl.values = values;
+    vl.values_len = 1;
+    vl.time = time (NULL);
+    strcpy (vl.host, hostname_g);
+    strcpy (vl.plugin, "iptables");
+
+    status = snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
+           "%s-%s", chain->table, chain->chain);
+    if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
+       return (0);
+
     if (chain->name[0] != '\0')
     {
-       status = snprintf (inst, sizeof (inst), "%s-%s,%s",
-               chain->table, chain->chain, chain->name);
+       strncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
     }
     else
     {
        if (chain->rule_type == RTYPE_NUM)
-           status = snprintf (inst, sizeof (inst), "%s-%s,%i",
-               chain->table, chain->chain, chain->rule.num);
+           snprintf (vl.type_instance, sizeof (vl.type_instance),
+                   "%i", chain->rule.num);
        else
-           status = snprintf (inst, sizeof (inst), "%s-%s,%s",
-               chain->table, chain->chain, match->data);
-
-       if ((status >= sizeof (inst)) || (status < 1))
-           return (0);
+           strncpy (vl.type_instance, (char *) match->data,
+                   sizeof (vl.type_instance));
     }
+    vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
 
-    status = snprintf (value, sizeof (value), "%u:%lld",
-           (unsigned int) curtime,
-           entry->counters.bcnt);
-    if ((status >= sizeof (value)) || (status < 1))
-       return 0;
-    plugin_submit ("ipt_bytes", inst, value);
+    values[0].counter = (counter_t) entry->counters.bcnt;
+    plugin_dispatch_values ("ipt_bytes", &vl);
 
-    status = snprintf (value, sizeof (value), "%u:%lld",
-           (unsigned int) curtime,
-           entry->counters.pcnt);
-    if ((status >= sizeof (value)) || (status < 1))
-       return 0;
-    plugin_submit ("ipt_packets", inst, value);
+    values[0].counter = (counter_t) entry->counters.pcnt;
+    plugin_dispatch_values ("ipt_packets", &vl);
 
-    return 0;
-} /* int submit_match */
+    return (0);
+} /* void submit_match */
 
 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
     const struct ipt_entry *entry;
@@ -319,7 +259,7 @@ static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
     entry = iptc_first_rule( chain->chain, handle );
     if (entry == NULL)
     {
-       DBG ("iptc_first_rule failed: %s", iptc_strerror (errno));
+       DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
        return;
     }
 
@@ -341,10 +281,10 @@ static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
 }
 
 
-static void iptables_read (void)
+static int iptables_read (void)
 {
     int i;
-    static complain_t complaint;
+    int num_failures = 0;
 
     /* Init the iptc handle structure and query the correct table */    
     for (i = 0; i < chain_num; i++)
@@ -355,41 +295,50 @@ static void iptables_read (void)
        chain = chain_list[i];
        if (!chain)
        {
-           DBG ("chain == NULL");
+           DEBUG ("iptables plugin: chain == NULL");
            continue;
        }
 
-       handle = iptc_init( chain->table );
+       handle = iptc_init (chain->table);
        if (!handle)
        {
-           DBG ("iptc_init (%s) failed: %s", chain->table, iptc_strerror (errno));
-           plugin_complain (LOG_ERR, &complaint, "iptc_init (%s) failed: %s",
+           ERROR ("iptables plugin: iptc_init (%s) failed: %s",
                    chain->table, iptc_strerror (errno));
+           num_failures++;
            continue;
        }
-       plugin_relief (LOG_INFO, &complaint, "iptc_init (%s) succeeded",
-               chain->table);
 
        submit_chain (&handle, chain);
        iptc_free (&handle);
+    } /* for (i = 0 .. chain_num) */
+
+    return ((num_failures < chain_num) ? 0 : -1);
+} /* int iptables_read */
+
+static int iptables_shutdown (void)
+{
+    int i;
+
+    for (i = 0; i < chain_num; i++)
+    {
+       if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
+       {
+           sfree (chain_list[i]->rule.comment);
+       }
+       sfree (chain_list[i]);
     }
-}
-#else /* !IPTABLES_HAVE_READ */
-# define iptables_read NULL
-#endif
+    sfree (chain_list);
+
+    return (0);
+} /* int iptables_shutdown */
 
 void module_register (void)
 {
-    plugin_register ("ipt_bytes", NULL, NULL, iptables_write_bytes);
-    plugin_register ("ipt_packets", NULL, NULL, iptables_write_packets);
-#if IPTABLES_HAVE_READ
-    plugin_register (MODULE_NAME, NULL, iptables_read, NULL);
-    cf_register (MODULE_NAME, iptables_config, config_keys, config_keys_num);
-#endif
-}
-
-#undef BUFSIZE
-#undef MODULE_NAME
+    plugin_register_config ("iptables", iptables_config,
+           config_keys, config_keys_num);
+    plugin_register_read ("iptables", iptables_read);
+    plugin_register_shutdown ("iptables", iptables_shutdown);
+} /* void module_register */
 
 /*
  * vim:shiftwidth=4:softtabstop=4:tabstop=8