src/utils_avltree.[ch]: Rename all types and functions to use the prefix `c_avl_'.
[collectd.git] / src / network.c
index b59b16d..a426214 100644 (file)
@@ -23,6 +23,7 @@
 #include "plugin.h"
 #include "common.h"
 #include "configfile.h"
+#include "utils_avltree.h"
 
 #include "network.h"
 
@@ -140,14 +141,16 @@ typedef struct part_values_s part_values_t;
  */
 static const char *config_keys[] =
 {
+       "CacheFlush",
        "Listen",
        "Server",
        "TimeToLive",
-       NULL
+       "Forward"
 };
-static int config_keys_num = 3;
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
 static int network_config_ttl = 0;
+static int network_config_forward = 0;
 
 static sockent_t *sending_sockets = NULL;
 
@@ -159,12 +162,132 @@ static int listen_loop = 0;
 static char         send_buffer[BUFF_SIZE];
 static char        *send_buffer_ptr;
 static int          send_buffer_fill;
-static value_list_t send_buffer_vl = VALUE_LIST_INIT;
+static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
 static char         send_buffer_type[DATA_MAX_NAME_LEN];
+static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
+
+static c_avl_tree_t      *cache_tree = NULL;
+static pthread_mutex_t  cache_lock = PTHREAD_MUTEX_INITIALIZER;
+static time_t           cache_flush_last;
+static int              cache_flush_interval = 1800;
 
 /*
  * Private functions
  */
+static int cache_flush (void)
+{
+       char **keys = NULL;
+       int    keys_num = 0;
+
+       char **tmp;
+       int    i;
+
+       char   *key;
+       time_t *value;
+       c_avl_iterator_t *iter;
+
+       time_t curtime = time (NULL);
+
+       iter = c_avl_get_iterator (cache_tree);
+       while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
+       {
+               if ((curtime - *value) <= cache_flush_interval)
+                       continue;
+               tmp = (char **) realloc (keys,
+                               (keys_num + 1) * sizeof (char *));
+               if (tmp == NULL)
+               {
+                       sfree (keys);
+                       c_avl_iterator_destroy (iter);
+                       ERROR ("network plugin: cache_flush: realloc"
+                                       " failed.");
+                       return (-1);
+               }
+               keys = tmp;
+               keys[keys_num] = key;
+               keys_num++;
+       } /* while (c_avl_iterator_next) */
+       c_avl_iterator_destroy (iter);
+
+       for (i = 0; i < keys_num; i++)
+       {
+               if (c_avl_remove (cache_tree, keys[i], (void *) &key,
+                                       (void *) &value) != 0)
+               {
+                       WARNING ("network plugin: cache_flush: c_avl_remove"
+                                       " (%s) failed.", keys[i]);
+                       continue;
+               }
+
+               sfree (key);
+               sfree (value);
+       }
+
+       sfree (keys);
+
+       DEBUG ("network plugin: cache_flush: Removed %i %s",
+                       keys_num, (keys_num == 1) ? "entry" : "entries");
+       cache_flush_last = curtime;
+       return (0);
+} /* int cache_flush */
+
+static int cache_check (const char *type, const value_list_t *vl)
+{
+       char key[1024];
+       time_t *value = NULL;
+       int retval = -1;
+
+       if (cache_tree == NULL)
+               return (-1);
+
+       if (format_name (key, sizeof (key), vl->host, vl->plugin,
+                               vl->plugin_instance, type, vl->type_instance))
+               return (-1);
+
+       pthread_mutex_lock (&cache_lock);
+
+       if (c_avl_get (cache_tree, key, (void *) &value) == 0)
+       {
+               if (*value < vl->time)
+               {
+                       *value = vl->time;
+                       retval = 0;
+               }
+               else
+               {
+                       DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
+                                       (int) *value, (int) vl->time);
+                       retval = 1;
+               }
+       }
+       else
+       {
+               char *key_copy = strdup (key);
+               value = malloc (sizeof (time_t));
+               if ((key_copy != NULL) && (value != NULL))
+               {
+                       *value = vl->time;
+                       c_avl_insert (cache_tree, key_copy, value);
+                       retval = 0;
+               }
+               else
+               {
+                       sfree (key_copy);
+                       sfree (value);
+               }
+       }
+
+       if ((time (NULL) - cache_flush_last) > cache_flush_interval)
+               cache_flush ();
+
+       pthread_mutex_unlock (&cache_lock);
+
+       DEBUG ("network plugin: cache_check: key = %s; time = %i; retval = %i",
+                       key, (int) vl->time, retval);
+
+       return (retval);
+} /* int cache_check */
+
 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
                const data_set_t *ds, const value_list_t *vl)
 {
@@ -262,7 +385,8 @@ static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
 
        if (buffer_len < (15))
        {
-               DEBUG ("packet is too short: buffer_len = %i", buffer_len);
+               DEBUG ("network plugin: packet is too short: buffer_len = %i",
+                               buffer_len);
                return (-1);
        }
 
@@ -328,7 +452,8 @@ static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
        uint16_t h_length;
        uint16_t h_type;
 
-       DEBUG ("ret_buffer = %p; ret_buffer_len = %i; output = %p; output_len = %i;",
+       DEBUG ("network plugin: parse_part_string: ret_buffer = %p;"
+                       " ret_buffer_len = %i; output = %p; output_len = %i;",
                        *ret_buffer, *ret_buffer_len,
                        (void *) output, output_len);
 
@@ -337,7 +462,8 @@ static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
        h_length = ntohs (ps.head->length);
        h_type = ntohs (ps.head->type);
 
-       DEBUG ("length = %hu; type = %hu;", h_length, h_type);
+       DEBUG ("network plugin: parse_part_string: length = %hu; type = %hu;",
+                       h_length, h_type);
 
        if (buffer_len < h_length)
        {
@@ -364,7 +490,7 @@ static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
        }
        strcpy (output, ps.value);
 
-       DEBUG ("output = %s", output);
+       DEBUG ("network plugin: parse_part_string: output = %s", output);
 
        *ret_buffer = (void *) (buffer + h_length);
        *ret_buffer_len = buffer_len - h_length;
@@ -380,7 +506,8 @@ static int parse_packet (void *buffer, int buffer_len)
        value_list_t vl = VALUE_LIST_INIT;
        char type[DATA_MAX_NAME_LEN];
 
-       DEBUG ("buffer = %p; buffer_len = %i;", buffer, buffer_len);
+       DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;",
+                       buffer, buffer_len);
 
        memset (&vl, '\0', sizeof (vl));
        memset (&type, '\0', sizeof (type));
@@ -392,8 +519,11 @@ static int parse_packet (void *buffer, int buffer_len)
 
                if (ntohs (header->length) > buffer_len)
                        break;
+               /* Assure that this loop terminates eventually */
+               if (ntohs (header->length) < 4)
+                       break;
 
-               if (header->type == htons (TYPE_VALUES))
+               if (ntohs (header->type) == TYPE_VALUES)
                {
                        status = parse_part_values (&buffer, &buffer_len,
                                        &vl.values, &vl.values_len);
@@ -407,52 +537,68 @@ static int parse_packet (void *buffer, int buffer_len)
                        if ((vl.time > 0)
                                        && (strlen (vl.host) > 0)
                                        && (strlen (vl.plugin) > 0)
-                                       && (strlen (type) > 0))
+                                       && (strlen (type) > 0)
+                                       && (cache_check (type, &vl) == 0))
                        {
-                               DEBUG ("dispatching values");
+                               DEBUG ("network plugin: parse_packet:"
+                                               " dispatching values");
                                plugin_dispatch_values (type, &vl);
                        }
                        else
                        {
-                               DEBUG ("NOT dispatching values");
+                               DEBUG ("network plugin: parse_packet:"
+                                               " NOT dispatching values");
                        }
                }
-               else if (header->type == ntohs (TYPE_TIME))
+               else if (ntohs (header->type) == TYPE_TIME)
                {
                        uint64_t tmp = 0;
                        status = parse_part_number (&buffer, &buffer_len, &tmp);
                        if (status == 0)
                                vl.time = (time_t) tmp;
                }
-               else if (header->type == ntohs (TYPE_HOST))
+               else if (ntohs (header->type) == TYPE_INTERVAL)
+               {
+                       uint64_t tmp = 0;
+                       status = parse_part_number (&buffer, &buffer_len, &tmp);
+                       if (status == 0)
+                               vl.interval = (int) tmp;
+               }
+               else if (ntohs (header->type) == TYPE_HOST)
                {
                        status = parse_part_string (&buffer, &buffer_len,
                                        vl.host, sizeof (vl.host));
+                       DEBUG ("network plugin: parse_packet: vl.host = %s", vl.host);
                }
-               else if (header->type == ntohs (TYPE_PLUGIN))
+               else if (ntohs (header->type) == TYPE_PLUGIN)
                {
                        status = parse_part_string (&buffer, &buffer_len,
                                        vl.plugin, sizeof (vl.plugin));
+                       DEBUG ("network plugin: parse_packet: vl.plugin = %s", vl.plugin);
                }
-               else if (header->type == ntohs (TYPE_PLUGIN_INSTANCE))
+               else if (ntohs (header->type) == TYPE_PLUGIN_INSTANCE)
                {
                        status = parse_part_string (&buffer, &buffer_len,
                                        vl.plugin_instance, sizeof (vl.plugin_instance));
+                       DEBUG ("network plugin: parse_packet: vl.plugin_instance = %s", vl.plugin_instance);
                }
-               else if (header->type == ntohs (TYPE_TYPE))
+               else if (ntohs (header->type) == TYPE_TYPE)
                {
                        status = parse_part_string (&buffer, &buffer_len,
                                        type, sizeof (type));
+                       DEBUG ("network plugin: parse_packet: type = %s", type);
                }
-               else if (header->type == ntohs (TYPE_TYPE_INSTANCE))
+               else if (ntohs (header->type) == TYPE_TYPE_INSTANCE)
                {
                        status = parse_part_string (&buffer, &buffer_len,
                                        vl.type_instance, sizeof (vl.type_instance));
+                       DEBUG ("network plugin: parse_packet: vl.type_instance = %s", vl.type_instance);
                }
                else
                {
-                       DEBUG ("Unknown part type: 0x%0hx", header->type);
-                       buffer = ((char *) buffer) + header->length;
+                       DEBUG ("network plugin: parse_packet: Unknown part"
+                                       " type: 0x%0hx", ntohs (header->type));
+                       buffer = ((char *) buffer) + ntohs (header->length);
                }
        } /* while (buffer_len > sizeof (part_header_t)) */
 
@@ -535,7 +681,7 @@ static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
 
 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
 {
-       int loop = 1;
+       int loop = 0;
 
        DEBUG ("fd = %i; calling `bind'", se->fd);
 
@@ -909,7 +1055,7 @@ static void network_send_buffer (const char *buffer, int buffer_len)
        sockent_t *se;
        int status;
 
-       DEBUG ("buffer_len = %i", buffer_len);
+       DEBUG ("network plugin: network_send_buffer: buffer_len = %i", buffer_len);
 
        for (se = sending_sockets; se != NULL; se = se->next)
        {
@@ -937,13 +1083,14 @@ static int add_to_buffer (char *buffer, int buffer_size,
                value_list_t *vl_def, char *type_def,
                const data_set_t *ds, const value_list_t *vl)
 {
+       char *buffer_orig = buffer;
+
        if (strcmp (vl_def->host, vl->host) != 0)
        {
                if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
                                        vl->host, strlen (vl->host)) != 0)
                        return (-1);
                strcpy (vl_def->host, vl->host);
-               DEBUG ("host = %s", vl->host);
        }
 
        if (vl_def->time != vl->time)
@@ -952,7 +1099,14 @@ static int add_to_buffer (char *buffer, int buffer_size,
                                        (uint64_t) vl->time))
                        return (-1);
                vl_def->time = vl->time;
-               DEBUG ("time = %u", (unsigned int) vl->time);
+       }
+
+       if (vl_def->interval != vl->interval)
+       {
+               if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
+                                       (uint64_t) vl->interval))
+                       return (-1);
+               vl_def->interval = vl->interval;
        }
 
        if (strcmp (vl_def->plugin, vl->plugin) != 0)
@@ -961,7 +1115,6 @@ static int add_to_buffer (char *buffer, int buffer_size,
                                        vl->plugin, strlen (vl->plugin)) != 0)
                        return (-1);
                strcpy (vl_def->plugin, vl->plugin);
-               DEBUG ("plugin = %s", vl->plugin);
        }
 
        if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
@@ -971,7 +1124,6 @@ static int add_to_buffer (char *buffer, int buffer_size,
                                        strlen (vl->plugin_instance)) != 0)
                        return (-1);
                strcpy (vl_def->plugin_instance, vl->plugin_instance);
-               DEBUG ("plugin_instance = %s", vl->plugin_instance);
        }
 
        if (strcmp (type_def, ds->type) != 0)
@@ -980,27 +1132,28 @@ static int add_to_buffer (char *buffer, int buffer_size,
                                        ds->type, strlen (ds->type)) != 0)
                        return (-1);
                strcpy (type_def, ds->type);
-               DEBUG ("type = %s", ds->type);
        }
 
        if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
        {
-               if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
+               if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
                                        vl->type_instance,
                                        strlen (vl->type_instance)) != 0)
                        return (-1);
                strcpy (vl_def->type_instance, vl->type_instance);
-               DEBUG ("type_instance = %s", vl->type_instance);
        }
        
        if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
                return (-1);
 
-       return (buffer_size);
+       return (buffer - buffer_orig);
 } /* int add_to_buffer */
 
 static void flush_buffer (void)
 {
+       DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
+                       send_buffer_fill);
+
        network_send_buffer (send_buffer, send_buffer_fill);
        send_buffer_ptr  = send_buffer;
        send_buffer_fill = 0;
@@ -1011,13 +1164,24 @@ static void flush_buffer (void)
 static int network_write (const data_set_t *ds, const value_list_t *vl)
 {
        int status;
-       /* TODO: lock buffer */
+
+       /* If the value is already in the cache, we have received it via the
+        * network. We write it again if forwarding is activated. It's then in
+        * the cache and should we receive it again we will ignore it. */
+       status = cache_check (ds->type, vl);
+       if ((network_config_forward == 0)
+                       && (status != 0))
+               return (0);
+
+       pthread_mutex_lock (&send_buffer_lock);
+
        status = add_to_buffer (send_buffer_ptr,
                        sizeof (send_buffer) - send_buffer_fill,
                        &send_buffer_vl, send_buffer_type,
                        ds, vl);
        if (status >= 0)
        {
+               /* status == bytes added to the buffer */
                send_buffer_fill += status;
                send_buffer_ptr  += status;
        }
@@ -1046,7 +1210,8 @@ static int network_write (const data_set_t *ds, const value_list_t *vl)
        {
                flush_buffer ();
        }
-       /* TODO: unlock buffer */
+
+       pthread_mutex_unlock (&send_buffer_lock);
 
        return ((status < 0) ? -1 : 0);
 } /* int network_write */
@@ -1072,7 +1237,11 @@ static int network_config (const char *key, const char *val)
                                && (fields_num != 2))
                        return (1);
                else if (fields_num == 2)
+               {
+                       if ((service = strchr (fields[1], '.')) != NULL)
+                               *service = '\0';
                        service = fields[1];
+               }
                node = fields[0];
 
                if (strcasecmp ("Listen", key) == 0)
@@ -1088,26 +1257,66 @@ static int network_config (const char *key, const char *val)
                else
                        return (1);
        }
+       else if (strcasecmp ("Forward", key) == 0)
+       {
+               if ((strcasecmp ("true", val) == 0)
+                               || (strcasecmp ("yes", val) == 0)
+                               || (strcasecmp ("on", val) == 0))
+                       network_config_forward = 1;
+               else
+                       network_config_forward = 0;
+       }
+       else if (strcasecmp ("CacheFlush", key) == 0)
+       {
+               int tmp = atoi (val);
+               if (tmp > 0)
+                       cache_flush_interval = tmp;
+               else return (1);
+       }
        else
        {
                return (-1);
        }
        return (0);
-}
+} /* int network_config */
 
 static int network_shutdown (void)
 {
-       DEBUG ("Shutting down.");
-
        listen_loop++;
 
-       pthread_kill (listen_thread, SIGTERM);
-       pthread_join (listen_thread, NULL /* no return value */);
+       if (listen_thread != (pthread_t) 0)
+       {
+               pthread_kill (listen_thread, SIGTERM);
+               pthread_join (listen_thread, NULL /* no return value */);
+               listen_thread = (pthread_t) 0;
+       }
+
+       if (send_buffer_fill > 0)
+               flush_buffer ();
+
+       if (cache_tree != NULL)
+       {
+               void *key;
+               void *value;
 
-       listen_thread = 0;
+               while (c_avl_pick (cache_tree, &key, &value) == 0)
+               {
+                       sfree (key);
+                       sfree (value);
+               }
+               c_avl_destroy (cache_tree);
+               cache_tree = NULL;
+       }
+
+       /* TODO: Close `sending_sockets' */
+
+       plugin_unregister_config ("network");
+       plugin_unregister_init ("network");
+       plugin_unregister_write ("network");
+       plugin_unregister_shutdown ("network");
 
        return (0);
-}
+} /* int network_shutdown */
 
 static int network_init (void)
 {
@@ -1118,6 +1327,9 @@ static int network_init (void)
        memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
        memset (send_buffer_type, '\0', sizeof (send_buffer_type));
 
+       cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
+       cache_flush_last = time (NULL);
+
        /* setup socket(s) and so on */
        if (sending_sockets != NULL)
                plugin_register_write ("network", network_write);
@@ -1145,4 +1357,4 @@ void module_register (void)
        plugin_register_config ("network", network_config,
                        config_keys, config_keys_num);
        plugin_register_init   ("network", network_init);
-}
+} /* void module_register */