Merge branch 'collectd-4.0' into collectd-4.1
authorFlorian Forster <octo@huhu.verplant.org>
Wed, 12 Sep 2007 09:15:54 +0000 (11:15 +0200)
committerFlorian Forster <octo@huhu.verplant.org>
Wed, 12 Sep 2007 09:15:54 +0000 (11:15 +0200)
Conflicts:

ChangeLog
configure.in

1  2 
ChangeLog
src/dns.c
src/ntpd.c
src/plugin.c
src/rrdtool.c
src/unixsock.c

diff --combined ChangeLog
+++ b/ChangeLog
@@@ -1,35 -1,12 +1,47 @@@
- 2007-09-10, Version 4.1.1
++2007-09-12, Version 4.1.1
 +      * Build system: The detection of `libnetlink' has been improved.
 +      * collectd: The documentation has been fixed in numerous places.
 +      * exec plugin: Setting the group under which to run a program has been
 +        fixed.
++      * collectd: The `sstrerror' function was improved to work correctly
++        with the broken GNU version of `strerror_r'.
++      * collectd: Write an error message to STDERR when loading of a plugin
++        fails.
++      * apcups plugin: Fix the `types' used to submit the values: They still
++        has an `apcups_' prefix which doesn't work anymore.
++      * rrdtool plugin: Create new RRD-files with the `begin' time set to
++        whatever the client thinks is `now'..
 +
 +2007-09-01, Version 4.1.0
 +      * Build system: The build system has been changed to automatically
 +        disable all plugins, which are missing dependencies. The dependency
 +        checking has been removed from the plugins themselves to remove
 +        redundancy.
 +      * Flexible interval: The interval of collected data is now sent along
 +        with the data itself over the network, so that the interval-settings
 +        of server and clients no longer needs to match.
 +      * netlink plugin: The new `netlink' plugin connects to the Linux
 +        kernel using a netlink socket and uses it to query information about
 +        interfaces, qdiscs and classes.
 +      * rrdtool plugin: The cache is now dumped to disk in an extra thread
 +        to not block data collection.
 +      * snmp plugin: The new `snmp' plugin can read values from SNMP enabled
 +        network devices, such as switches, routers, thermometers, rack
 +        monitoring servers, etc. The collectd-snmp(5) manpage documents this
 +        plugin.
 +      * unixsock plugin: Added the `LISTVAL' command.
 +      * xmms plugin: The new `xmms' plugin graphs the bitrate and frequency
 +        of music played with xmms.
 +
- 2007-09-10, Version 4.0.8
+ 2007-09-12, Version 4.0.8
        * collectd: The `sstrerror' function was improved to work correctly
          with the broken GNU version of `strerror_r'.
+       * collectd: Write an error message to STDERR when loading of a plugin
+         fails.
        * apcups plugin: Fix the `types' used to submit the values: They still
          has an `apcups_' prefix which doesn't work anymore.
+       * rrdtool plugin: Create new RRD-files with the `begin' time set to
+         whatever the client thinks is `now'..
  
  2007-08-26, Version 4.0.7
        * documentation: Some typos have been fixed and some information has
diff --combined src/dns.c
+++ b/src/dns.c
  #include "plugin.h"
  #include "configfile.h"
  
 -#if HAVE_LIBPCAP && HAVE_LIBPTHREAD
 -# include "utils_dns.h"
 -# include <pthread.h>
 -# include <pcap.h>
 -# include <poll.h>
 -# define DNS_HAVE_READ 1
 -#else
 -# define DNS_HAVE_READ 0
 -#endif
 +#include "utils_dns.h"
 +#include <pthread.h>
 +#include <pcap.h>
 +#include <poll.h>
  
  /*
   * Private data types
   */
 -#if DNS_HAVE_READ
  struct counter_list_s
  {
        unsigned int key;
        struct counter_list_s *next;
  };
  typedef struct counter_list_s counter_list_t;
 -#endif
  
  /*
   * Private variables
   */
 -#if DNS_HAVE_READ
  static const char *config_keys[] =
  {
        "Interface",
@@@ -67,10 -75,12 +67,10 @@@ static pthread_mutex_t traffic_mutex = 
  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;
@@@ -229,13 -239,11 +229,11 @@@ static void *dns_child_loop (void *dumm
        memset (&fp, 0, sizeof (fp));
        if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
        {
-               DEBUG ("pcap_compile failed");
                ERROR ("dns plugin: pcap_compile failed");
                return (NULL);
        }
        if (pcap_setfilter (pcap_obj, &fp) < 0)
        {
-               DEBUG ("pcap_setfilter failed");
                ERROR ("dns plugin: pcap_setfilter failed");
                return (NULL);
        }
@@@ -392,10 -400,13 +390,10 @@@ static int dns_read (void
  
        return (0);
  } /* int dns_read */
 -#endif
  
  void module_register (void)
  {
 -#if DNS_HAVE_READ
        plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
        plugin_register_init ("dns", dns_init);
        plugin_register_read ("dns", dns_read);
 -#endif
  } /* void module_register */
diff --combined src/ntpd.c
  #include "plugin.h"
  #include "configfile.h"
  
 -#if HAVE_SYS_SOCKET_H
 -# define NTPD_HAVE_READ 1
 -#else
 -# define NTPD_HAVE_READ 0
 -#endif
 -
  #if HAVE_STDINT_H
  # include <stdint.h>
  #endif
@@@ -54,6 -60,7 +54,6 @@@ static const char *config_keys[] 
  };
  static int config_keys_num = 2;
  
 -#if NTPD_HAVE_READ
  # define NTPD_DEFAULT_HOST "localhost"
  # define NTPD_DEFAULT_PORT "123"
  static int   sock_descr = -1;
@@@ -390,7 -397,6 +390,6 @@@ static int ntpd_connect (void
  
        if (sock_descr < 0)
        {
-               DEBUG ("Unable to connect to server.");
                ERROR ("ntpd plugin: Unable to connect to server.");
        }
  
@@@ -952,10 -958,13 +951,10 @@@ static int ntpd_read (void
  
        return (0);
  } /* int ntpd_read */
 -#endif /* NTPD_HAVE_READ */
  
  void module_register (void)
  {
 -#if NTPD_HAVE_READ
        plugin_register_config ("ntpd", ntpd_config,
                        config_keys, config_keys_num);
        plugin_register_read ("ntpd", ntpd_read);
 -#endif /* NTPD_HAVE_READ */
  } /* void module_register */
diff --combined src/plugin.c
@@@ -133,6 -133,7 +133,7 @@@ static int plugin_load_file (char *file
                const char *error = lt_dlerror ();
  
                ERROR ("lt_dlopen failed: %s", error);
+               fprintf (stderr, "lt_dlopen failed: %s\n", error);
                return (1);
        }
  
@@@ -193,7 -194,7 +194,7 @@@ static void *plugin_read_thread (void *
                                        rf->wait_time = 86400;
  
                                NOTICE ("read-function of plugin `%s' "
 -                                              "failed. Will syspend it for %i "
 +                                              "failed. Will suspend it for %i "
                                                "seconds.", le->key, rf->wait_left);
                        }
                        else
@@@ -355,6 -356,10 +356,10 @@@ int plugin_load (const char *type
                        ret = 0;
                        break;
                }
+               else
+               {
+                       fprintf (stderr, "Unable to load plugin %s.\n", type);
+               }
        }
  
        closedir (dh);
@@@ -373,12 -378,6 +378,12 @@@ int plugin_register_config (const char 
        return (0);
  } /* int plugin_register_config */
  
 +int plugin_register_complex_config (const char *type,
 +              int (*callback) (oconfig_item_t *))
 +{
 +      return (cf_register_complex (type, callback));
 +} /* int plugin_register_complex_config */
 +
  int plugin_register_init (const char *name,
                int (*callback) (void))
  {
@@@ -463,12 -462,6 +468,12 @@@ int plugin_unregister_config (const cha
        return (0);
  } /* int plugin_unregister_config */
  
 +int plugin_unregister_complex_config (const char *name)
 +{
 +      cf_unregister_complex (name);
 +      return (0);
 +} /* int plugin_unregister_complex_config */
 +
  int plugin_unregister_init (const char *name)
  {
        return (plugin_unregister (list_init, name));
@@@ -647,12 -640,10 +652,12 @@@ int plugin_dispatch_values (const char 
  
        ds = (data_set_t *) le->value;
  
 -      DEBUG ("plugin: plugin_dispatch_values: time = %u; host = %s; "
 -                      "plugin = %s; plugin_instance = %s; type = %s; "
 -                      "type_instance = %s;",
 -                      (unsigned int) vl->time, vl->host,
 +      DEBUG ("plugin: plugin_dispatch_values: time = %u; interval = %i; "
 +                      "host = %s; "
 +                      "plugin = %s; plugin_instance = %s; "
 +                      "type = %s; type_instance = %s;",
 +                      (unsigned int) vl->time, vl->interval,
 +                      vl->host,
                        vl->plugin, vl->plugin_instance,
                        ds->type, vl->type_instance);
  
diff --combined src/rrdtool.c
@@@ -37,21 -37,9 +37,21 @@@ struct rrd_cache_
        char **values;
        time_t first_value;
        time_t last_value;
 +      enum
 +      {
 +              FLAG_NONE   = 0x00,
 +              FLAG_QUEUED = 0x01
 +      } flags;
  };
  typedef struct rrd_cache_s rrd_cache_t;
  
 +struct rrd_queue_s
 +{
 +      char *filename;
 +      struct rrd_queue_s *next;
 +};
 +typedef struct rrd_queue_s rrd_queue_t;
 +
  /*
   * Private variables
   */
@@@ -89,50 -77,25 +89,50 @@@ static const char *config_keys[] 
  };
  static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
  
 +/* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
 + * is zero a default, depending on the `interval' member of the value list is
 + * being used. */
  static char   *datadir   = NULL;
  static int     stepsize  = 0;
  static int     heartbeat = 0;
  static int     rrarows   = 1200;
  static double  xff       = 0.1;
  
 +/* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
 + * ALWAYS lock `cache_lock' first! */
  static int         cache_timeout = 0;
  static int         cache_flush_timeout = 0;
  static time_t      cache_flush_last;
  static avl_tree_t *cache = NULL;
  static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
  
 +static rrd_queue_t    *queue_head = NULL;
 +static rrd_queue_t    *queue_tail = NULL;
 +static pthread_t       queue_thread = 0;
 +static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
 +static pthread_cond_t  queue_cond = PTHREAD_COND_INITIALIZER;
 +
 +static int do_shutdown = 0;
 +
  /* * * * * * * * * *
   * WARNING:  Magic *
   * * * * * * * * * */
 -static int rra_get (char ***ret)
 +
 +static void rra_free (int rra_num, char **rra_def)
 +{
 +      int i;
 +
 +      for (i = 0; i < rra_num; i++)
 +      {
 +              sfree (rra_def[i]);
 +      }
 +      sfree (rra_def);
 +} /* void rra_free */
 +
 +static int rra_get (char ***ret, const value_list_t *vl)
  {
 -      static char **rra_def = NULL;
 -      static int rra_num = 0;
 +      char **rra_def;
 +      int rra_num;
  
        int *rts;
        int  rts_num;
  
        char buffer[64];
  
 -      if ((rra_num != 0) && (rra_def != NULL))
 +      /* The stepsize we use here: If it is user-set, use it. If not, use the
 +       * interval of the value-list. */
 +      int ss;
 +
 +      if (rrarows <= 0)
 +      {
 +              *ret = NULL;
 +              return (-1);
 +      }
 +
 +      ss = (stepsize > 0) ? stepsize : vl->interval;
 +      if (ss <= 0)
        {
 -              *ret = rra_def;
 -              return (rra_num);
 +              *ret = NULL;
 +              return (-1);
        }
  
        /* Use the configured timespans or fall back to the built-in defaults */
        if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
                return (-1);
        memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
 -
 -      if ((stepsize <= 0) || (rrarows <= 0))
 -      {
 -              *ret = NULL;
 -              return (-1);
 -      }
 +      rra_num = 0;
  
        cdp_len = 0;
        for (i = 0; i < rts_num; i++)
        {
                span = rts[i];
  
 -              if ((span / stepsize) < rrarows)
 +              if ((span / ss) < rrarows)
                        continue;
  
                if (cdp_len == 0)
                        cdp_len = 1;
                else
                        cdp_len = (int) floor (((double) span)
 -                                      / ((double) (rrarows * stepsize)));
 +                                      / ((double) (rrarows * ss)));
  
                cdp_num = (int) ceil (((double) span)
 -                              / ((double) (cdp_len * stepsize)));
 +                              / ((double) (cdp_len * ss)));
  
                for (j = 0; j < rra_types_num; j++)
                {
  
        *ret = rra_def;
        return (rra_num);
 -}
 +} /* int rra_get */
  
  static void ds_free (int ds_num, char **ds_def)
  {
        free (ds_def);
  }
  
 -static int ds_get (char ***ret, const data_set_t *ds)
 +static int ds_get (char ***ret, const data_set_t *ds, const value_list_t *vl)
  {
        char **ds_def;
        int ds_num;
  
                status = snprintf (buffer, sizeof (buffer),
                                "DS:%s:%s:%i:%s:%s",
 -                              d->name, type, heartbeat,
 +                              d->name, type,
 +                              (heartbeat > 0) ? heartbeat : (2 * vl->interval),
                                min, max);
                if ((status < 1) || (status >= sizeof (buffer)))
                        break;
@@@ -337,24 -293,25 +337,25 @@@ static int rrd_create_file (char *filen
        int ds_num;
        int i, j;
        char stepsize_str[16];
+       char begin_str[16];
        int status = 0;
  
        if (check_create_dir (filename))
                return (-1);
  
 -      if ((rra_num = rra_get (&rra_def)) < 1)
 +      if ((rra_num = rra_get (&rra_def, vl)) < 1)
        {
                ERROR ("rrd_create_file failed: Could not calculate RRAs");
                return (-1);
        }
  
 -      if ((ds_num = ds_get (&ds_def, ds)) < 1)
 +      if ((ds_num = ds_get (&ds_def, ds, vl)) < 1)
        {
                ERROR ("rrd_create_file failed: Could not calculate DSes");
                return (-1);
        }
  
-       argc = ds_num + rra_num + 4;
+       argc = ds_num + rra_num + 6;
  
        if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
        {
        }
  
        status = snprintf (stepsize_str, sizeof (stepsize_str),
 -                      "%i", stepsize);
 +                      "%i", (stepsize > 0) ? stepsize : vl->interval);
        if ((status < 1) || (status >= sizeof (stepsize_str)))
        {
                ERROR ("rrdtool plugin: snprintf failed.");
 +              free (argv);
 +              ds_free (ds_num, ds_def);
 +              rra_free (rra_num, rra_def);
                return (-1);
        }
  
+       assert (vl->time > 10);
+       status = snprintf (begin_str, sizeof (begin_str),
+                       "%llu", (unsigned long long) (vl->time - 10));
+       if ((status < 1) || (status >= sizeof (begin_str)))
+       {
+               ERROR ("rrdtool plugin: snprintf failed.");
+               return (-1);
+       }
        argv[0] = "create";
        argv[1] = filename;
-       argv[2] = "-s";
-       argv[3] = stepsize_str;
+       argv[2] = "-b";
+       argv[3] = begin_str;
+       argv[4] = "-s";
+       argv[5] = stepsize_str;
  
-       j = 4;
+       j = 6;
        for (i = 0; i < ds_num; i++)
                argv[j++] = ds_def[i];
        for (i = 0; i < rra_num; i++)
  
        free (argv);
        ds_free (ds_num, ds_def);
 +      rra_free (rra_num, rra_def);
  
        return (status);
  }
@@@ -482,143 -446,138 +494,143 @@@ static int value_list_to_filename (cha
        return (0);
  } /* int value_list_to_filename */
  
 -static rrd_cache_t *rrd_cache_insert (const char *filename,
 -              const char *value, time_t value_time)
 +static int rrd_write_to_file (char *filename, char **values, int values_num)
  {
 -      rrd_cache_t *rc = NULL;
 -      int new_rc = 0;
 +      char **argv;
 +      int argc;
 +      int status;
  
 -      if (cache != NULL)
 -              avl_get (cache, filename, (void *) &rc);
 +      if (values_num < 1)
 +              return (0);
  
 -      if (rc == NULL)
 -      {
 -              rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
 -              if (rc == NULL)
 -                      return (NULL);
 -              rc->values_num = 0;
 -              rc->values = NULL;
 -              rc->first_value = 0;
 -              rc->last_value = 0;
 -              new_rc = 1;
 -      }
 +      argc = values_num + 2;
 +      argv = (char **) malloc ((argc + 1) * sizeof (char *));
 +      if (argv == NULL)
 +              return (-1);
  
 -      if (rc->last_value >= value_time)
 +      argv[0] = "update";
 +      argv[1] = filename;
 +      memcpy (argv + 2, values, values_num * sizeof (char *));
 +      argv[argc] = NULL;
 +
 +      DEBUG ("rrd_update (argc = %i, argv = %p)", argc, (void *) argv);
 +
 +      optind = 0; /* bug in librrd? */
 +      rrd_clear_error ();
 +      status = rrd_update (argc, argv);
 +      if (status != 0)
        {
 -              WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
 -                              (unsigned int) rc->last_value,
 -                              (unsigned int) value_time);
 -              return (NULL);
 +              WARNING ("rrd_update failed: %s: %s",
 +                              filename, rrd_get_error ());
 +              status = -1;
        }
  
 -      rc->values = (char **) realloc ((void *) rc->values,
 -                      (rc->values_num + 1) * sizeof (char *));
 -      if (rc->values == NULL)
 +      sfree (argv);
 +
 +      return (status);
 +} /* int rrd_write_cache_entry */
 +
 +static void *rrd_queue_thread (void *data)
 +{
 +      while (42)
        {
 -              char errbuf[1024];
 -              ERROR ("rrdtool plugin: realloc failed: %s",
 -                              sstrerror (errno, errbuf, sizeof (errbuf)));
 -              if (cache != NULL)
 +              rrd_queue_t *queue_entry;
 +              rrd_cache_t *cache_entry;
 +              char **values;
 +              int    values_num;
 +              int    i;
 +
 +              /* XXX: If you need to lock both, cache_lock and queue_lock, at
 +               * the same time, ALWAYS lock `cache_lock' first! */
 +
 +              /* wait until an entry is available */
 +              pthread_mutex_lock (&queue_lock);
 +              while ((queue_head == NULL) && (do_shutdown == 0))
 +                      pthread_cond_wait (&queue_cond, &queue_lock);
 +
 +              /* We're in the shutdown phase */
 +              if (queue_head == NULL)
                {
 -                      void *cache_key = NULL;
 -                      avl_remove (cache, filename, &cache_key, NULL);
 -                      sfree (cache_key);
 +                      pthread_mutex_unlock (&queue_lock);
 +                      break;
                }
 -              free (rc);
 -              return (NULL);
 -      }
  
 -      rc->values[rc->values_num] = strdup (value);
 -      if (rc->values[rc->values_num] != NULL)
 -              rc->values_num++;
 +              /* Dequeue the first entry */
 +              queue_entry = queue_head;
 +              if (queue_head == queue_tail)
 +                      queue_head = queue_tail = NULL;
 +              else
 +                      queue_head = queue_head->next;
  
 -      if (rc->values_num == 1)
 -              rc->first_value = value_time;
 -      rc->last_value = value_time;
 +              /* Unlock the queue again */
 +              pthread_mutex_unlock (&queue_lock);
  
 -      /* Insert if this is the first value */
 -      if ((cache != NULL) && (new_rc == 1))
 -      {
 -              void *cache_key = strdup (filename);
 +              /* We now need the cache lock so the entry isn't updated while
 +               * we make a copy of it's values */
 +              pthread_mutex_lock (&cache_lock);
  
 -              if (cache_key == NULL)
 -              {
 -                      char errbuf[1024];
 -                      ERROR ("rrdtool plugin: strdup failed: %s",
 -                                      sstrerror (errno, errbuf, sizeof (errbuf)));
 -                      sfree (rc->values[0]);
 -                      sfree (rc->values);
 -                      sfree (rc);
 -                      return (NULL);
 -              }
 +              avl_get (cache, queue_entry->filename, (void *) &cache_entry);
  
 -              avl_insert (cache, cache_key, rc);
 -      }
 +              values = cache_entry->values;
 +              values_num = cache_entry->values_num;
  
 -      DEBUG ("rrd_cache_insert (%s, %s, %u) = %p", filename, value,
 -                      (unsigned int) value_time, (void *) rc);
 +              cache_entry->values = NULL;
 +              cache_entry->values_num = 0;
 +              cache_entry->flags = FLAG_NONE;
  
 -      return (rc);
 -} /* rrd_cache_t *rrd_cache_insert */
 +              pthread_mutex_unlock (&cache_lock);
  
 -static int rrd_write_cache_entry (const char *filename, rrd_cache_t *rc)
 -{
 -      char **argv;
 -      int    argc;
 +              /* Write the values to the RRD-file */
 +              rrd_write_to_file (queue_entry->filename, values, values_num);
  
 -      char *fn;
 -      int status;
 +              for (i = 0; i < values_num; i++)
 +              {
 +                      sfree (values[i]);
 +              }
 +              sfree (values);
 +              sfree (queue_entry->filename);
 +              sfree (queue_entry);
 +      } /* while (42) */
  
 -      int i;
 +      pthread_mutex_lock (&cache_lock);
 +      avl_destroy (cache);
 +      cache = NULL;
 +      pthread_mutex_unlock (&cache_lock);
  
 -      if (rc->values_num < 1)
 -              return (0);
 +      pthread_exit ((void *) 0);
 +      return ((void *) 0);
 +} /* void *rrd_queue_thread */
  
 -      argc = rc->values_num + 2;
 -      argv = (char **) malloc ((argc + 1) * sizeof (char *));
 -      if (argv == NULL)
 +static int rrd_queue_cache_entry (const char *filename)
 +{
 +      rrd_queue_t *queue_entry;
 +
 +      queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
 +      if (queue_entry == NULL)
                return (-1);
  
 -      fn = strdup (filename);
 -      if (fn == NULL)
 +      queue_entry->filename = strdup (filename);
 +      if (queue_entry->filename == NULL)
        {
 -              free (argv);
 +              free (queue_entry);
                return (-1);
        }
  
 -      argv[0] = "update";
 -      argv[1] = fn;
 -      memcpy (argv + 2, rc->values, rc->values_num * sizeof (char *));
 -      argv[argc] = NULL;
 -
 -      DEBUG ("rrd_update (argc = %i, argv = %p)", argc, (void *) argv);
 +      queue_entry->next = NULL;
  
 -      optind = 0; /* bug in librrd? */
 -      rrd_clear_error ();
 -      status = rrd_update (argc, argv);
 -      if (status != 0)
 -      {
 -              WARNING ("rrd_update failed: %s: %s",
 -                              filename, rrd_get_error ());
 -              status = -1;
 -      }
 +      pthread_mutex_lock (&queue_lock);
 +      if (queue_tail == NULL)
 +              queue_head = queue_entry;
 +      else
 +              queue_tail->next = queue_entry;
 +      queue_tail = queue_entry;
 +      pthread_cond_signal (&queue_cond);
 +      pthread_mutex_unlock (&queue_lock);
  
 -      free (argv);
 -      free (fn);
 -      /* Free the value list of `rc' */
 -      for (i = 0; i < rc->values_num; i++)
 -              free (rc->values[i]);
 -      free (rc->values);
 -      rc->values = NULL;
 -      rc->values_num = 0;
 +      DEBUG ("rrdtool plugin: Put `%s' into the update queue", filename);
  
 -      return (status);
 -} /* int rrd_write_cache_entry */
 +      return (0);
 +} /* int rrd_queue_cache_entry */
  
  static void rrd_cache_flush (int timeout)
  {
        avl_iterator_t *iter;
        int i;
  
 -      if (cache == NULL)
 -              return;
 -
        DEBUG ("Flushing cache, timeout = %i", timeout);
  
        now = time (NULL);
        while (avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
        {
                DEBUG ("key = %s; age = %i;", key, now - rc->first_value);
 -              if ((now - rc->first_value) >= timeout)
 +
 +              if (rc->flags == FLAG_QUEUED)
 +                      continue;
 +              else if ((now - rc->first_value) < timeout)
 +                      continue;
 +              else if (rc->values_num > 0)
 +              {
 +                      if (rrd_queue_cache_entry (key) == 0)
 +                              rc->flags = FLAG_QUEUED;
 +              }
 +              else /* ancient and no values -> waste of memory */
                {
                        keys = (char **) realloc ((void *) keys,
                                        (keys_num + 1) * sizeof (char *));
                        continue;
                }
  
 -              rrd_write_cache_entry (keys[i], rc);
 -              /* rc's value-list is free's by `rrd_write_cache_entry' */
 +              assert (rc->values == NULL);
 +              assert (rc->values_num == 0);
 +
                sfree (rc);
                sfree (key);
                keys[i] = NULL;
        cache_flush_last = now;
  } /* void rrd_cache_flush */
  
 +static int rrd_cache_insert (const char *filename,
 +              const char *value, time_t value_time)
 +{
 +      rrd_cache_t *rc = NULL;
 +      int new_rc = 0;
 +      char **values_new;
 +
 +      pthread_mutex_lock (&cache_lock);
 +
 +      avl_get (cache, filename, (void *) &rc);
 +
 +      if (rc == NULL)
 +      {
 +              rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
 +              if (rc == NULL)
 +                      return (-1);
 +              rc->values_num = 0;
 +              rc->values = NULL;
 +              rc->first_value = 0;
 +              rc->last_value = 0;
 +              rc->flags = FLAG_NONE;
 +              new_rc = 1;
 +      }
 +
 +      if (rc->last_value >= value_time)
 +      {
 +              pthread_mutex_unlock (&cache_lock);
 +              WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
 +                              (unsigned int) rc->last_value,
 +                              (unsigned int) value_time);
 +              return (-1);
 +      }
 +
 +      values_new = (char **) realloc ((void *) rc->values,
 +                      (rc->values_num + 1) * sizeof (char *));
 +      if (values_new == NULL)
 +      {
 +              char errbuf[1024];
 +              void *cache_key = NULL;
 +
 +              sstrerror (errno, errbuf, sizeof (errbuf));
 +
 +              avl_remove (cache, filename, &cache_key, NULL);
 +              pthread_mutex_unlock (&cache_lock);
 +
 +              ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
 +
 +              sfree (cache_key);
 +              sfree (rc->values);
 +              sfree (rc);
 +              return (-1);
 +      }
 +      rc->values = values_new;
 +
 +      rc->values[rc->values_num] = strdup (value);
 +      if (rc->values[rc->values_num] != NULL)
 +              rc->values_num++;
 +
 +      if (rc->values_num == 1)
 +              rc->first_value = value_time;
 +      rc->last_value = value_time;
 +
 +      /* Insert if this is the first value */
 +      if (new_rc == 1)
 +      {
 +              void *cache_key = strdup (filename);
 +
 +              if (cache_key == NULL)
 +              {
 +                      char errbuf[1024];
 +                      sstrerror (errno, errbuf, sizeof (errbuf));
 +
 +                      pthread_mutex_unlock (&cache_lock);
 +
 +                      ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
 +
 +                      sfree (rc->values[0]);
 +                      sfree (rc->values);
 +                      sfree (rc);
 +                      return (-1);
 +              }
 +
 +              avl_insert (cache, cache_key, rc);
 +      }
 +
 +      DEBUG ("rrd_cache_insert (%s, %s, %u) = %p", filename, value,
 +                      (unsigned int) value_time, (void *) rc);
 +
 +      if ((rc->last_value - rc->first_value) >= cache_timeout)
 +      {
 +              /* XXX: If you need to lock both, cache_lock and queue_lock, at
 +               * the same time, ALWAYS lock `cache_lock' first! */
 +              if (rc->flags != FLAG_QUEUED)
 +              {
 +                      if (rrd_queue_cache_entry (filename) == 0)
 +                              rc->flags = FLAG_QUEUED;
 +              }
 +              else
 +              {
 +                      DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
 +              }
 +      }
 +
 +      if ((cache_timeout > 0) &&
 +                      ((time (NULL) - cache_flush_last) > cache_flush_timeout))
 +              rrd_cache_flush (cache_flush_timeout);
 +
 +
 +      pthread_mutex_unlock (&cache_lock);
 +
 +      return (0);
 +} /* int rrd_cache_insert */
 +
  static int rrd_write (const data_set_t *ds, const value_list_t *vl)
  {
        struct stat  statbuf;
        char         filename[512];
        char         values[512];
 -      rrd_cache_t *rc;
 -      time_t       now;
 +      int          status;
  
        if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
                return (-1);
                return (-1);
        }
  
 -      pthread_mutex_lock (&cache_lock);
 -      rc = rrd_cache_insert (filename, values, vl->time);
 -      if (rc == NULL)
 -      {
 -              pthread_mutex_unlock (&cache_lock);
 -              return (-1);
 -      }
 -
 -      if (cache == NULL)
 -      {
 -              rrd_write_cache_entry (filename, rc);
 -              /* rc's value-list is free's by `rrd_write_cache_entry' */
 -              sfree (rc);
 -              pthread_mutex_unlock (&cache_lock);
 -              return (0);
 -      }
 -
 -      now = time (NULL);
 -
 -      DEBUG ("age (%s) = %i", filename, now - rc->first_value);
 +      status = rrd_cache_insert (filename, values, vl->time);
  
 -      /* `rc' is not free'd here, because we'll likely reuse it. If not, then
 -       * the next flush will remove this entry.  */
 -      if ((now - rc->first_value) >= cache_timeout)
 -              rrd_write_cache_entry (filename, rc);
 -
 -      if ((now - cache_flush_last) >= cache_flush_timeout)
 -              rrd_cache_flush (cache_flush_timeout);
 -
 -      pthread_mutex_unlock (&cache_lock);
 -      return (0);
 +      return (status);
  } /* int rrd_write */
  
  static int rrd_config (const char *key, const char *value)
        }
        else if (strcasecmp ("StepSize", key) == 0)
        {
 -              int tmp = atoi (value);
 -              if (tmp <= 0)
 -              {
 -                      fprintf (stderr, "rrdtool: `StepSize' must "
 -                                      "be greater than 0.\n");
 -                      return (1);
 -              }
 -              stepsize = tmp;
 +              stepsize = atoi (value);
 +              if (stepsize < 0)
 +                      stepsize = 0;
        }
        else if (strcasecmp ("HeartBeat", key) == 0)
        {
 -              int tmp = atoi (value);
 -              if (tmp <= 0)
 -              {
 -                      fprintf (stderr, "rrdtool: `HeartBeat' must "
 -                                      "be greater than 0.\n");
 -                      return (1);
 -              }
 -              heartbeat = tmp;
 +              heartbeat = atoi (value);
 +              if (heartbeat < 0)
 +                      heartbeat = 0;
        }
        else if (strcasecmp ("RRARows", key) == 0)
        {
@@@ -968,67 -845,47 +980,67 @@@ static int rrd_shutdown (void
  {
        pthread_mutex_lock (&cache_lock);
        rrd_cache_flush (-1);
 -      if (cache != NULL)
 -              avl_destroy (cache);
 -      cache = NULL;
        pthread_mutex_unlock (&cache_lock);
  
 +      pthread_mutex_lock (&queue_lock);
 +      do_shutdown = 1;
 +      pthread_cond_signal (&queue_cond);
 +      pthread_mutex_unlock (&queue_lock);
 +
        return (0);
  } /* int rrd_shutdown */
  
  static int rrd_init (void)
  {
 -      if (stepsize <= 0)
 -              stepsize = interval_g;
 +      int status;
 +
 +      if (stepsize < 0)
 +              stepsize = 0;
        if (heartbeat <= 0)
 -              heartbeat = 2 * interval_g;
 +      {
 +              if (stepsize > 0)
 +                      heartbeat = 2 * stepsize;
 +              else
 +                      heartbeat = 0;
 +      }
  
 -      if (heartbeat < interval_g)
 +      if ((heartbeat > 0) && (heartbeat < interval_g))
                WARNING ("rrdtool plugin: Your `heartbeat' is "
                                "smaller than your `interval'. This will "
                                "likely cause problems.");
 -      else if (stepsize < interval_g)
 +      else if ((stepsize > 0) && (stepsize < interval_g))
                WARNING ("rrdtool plugin: Your `stepsize' is "
                                "smaller than your `interval'. This will "
                                "create needlessly big RRD-files.");
  
 +      /* Set the cache up */
        pthread_mutex_lock (&cache_lock);
 +
 +      cache = avl_create ((int (*) (const void *, const void *)) strcmp);
 +      if (cache == NULL)
 +      {
 +              ERROR ("rrdtool plugin: avl_create failed.");
 +              return (-1);
 +      }
 +
 +      cache_flush_last = time (NULL);
        if (cache_timeout < 2)
        {
                cache_timeout = 0;
                cache_flush_timeout = 0;
        }
 -      else
 -      {
 -              if (cache_flush_timeout < cache_timeout)
 -                      cache_flush_timeout = 10 * cache_timeout;
 +      else if (cache_flush_timeout < cache_timeout)
 +              cache_flush_timeout = 10 * cache_timeout;
  
 -              cache = avl_create ((int (*) (const void *, const void *)) strcmp);
 -              cache_flush_last = time (NULL);
 -              plugin_register_shutdown ("rrdtool", rrd_shutdown);
 -      }
        pthread_mutex_unlock (&cache_lock);
  
 +      status = pthread_create (&queue_thread, NULL, rrd_queue_thread, NULL);
 +      if (status != 0)
 +      {
 +              ERROR ("rrdtool plugin: Cannot create queue-thread.");
 +              return (-1);
 +      }
 +
        DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
                        " heartbeat = %i; rrarows = %i; xff = %lf;",
                        (datadir == NULL) ? "(null)" : datadir,
@@@ -1043,5 -900,4 +1055,5 @@@ void module_register (void
                        config_keys, config_keys_num);
        plugin_register_init ("rrdtool", rrd_init);
        plugin_register_write ("rrdtool", rrd_write);
 +      plugin_register_shutdown ("rrdtool", rrd_shutdown);
  }
diff --combined src/unixsock.c
@@@ -23,7 -23,6 +23,7 @@@
  #include "common.h"
  #include "plugin.h"
  #include "configfile.h"
 +#include "utils_cmd_putval.h"
  
  /* Folks without pthread will need to disable this plugin. */
  #include <pthread.h>
@@@ -37,7 -36,7 +37,7 @@@
  # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
  #endif
  
 -#define US_DEFAULT_PATH PREFIX"/var/run/"PACKAGE_NAME"-unixsock"
 +#define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
  
  /*
   * Private data structures
@@@ -330,7 -329,7 +330,7 @@@ static void cache_flush (int max_age
        } /* while (this != NULL) */
  
        pthread_mutex_unlock (&cache_lock);
 -} /* int cache_flush */
 +} /* void cache_flush */
  
  static int us_open_socket (void)
  {
                        sizeof (sa.sun_path) - 1);
        /* unlink (sa.sun_path); */
  
+       DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
        status = bind (sock_fd, (struct sockaddr *) &sa, sizeof (sa));
        if (status != 0)
        {
                char errbuf[1024];
                sstrerror (errno, errbuf, sizeof (errbuf));
-               DEBUG ("bind failed: %s; sa.sun_path = %s", errbuf, sa.sun_path);
                ERROR ("unixsock plugin: bind failed: %s", errbuf);
                close (sock_fd);
                sock_fd = -1;
@@@ -488,55 -488,148 +489,55 @@@ static int us_handle_getval (FILE *fh, 
        return (0);
  } /* int us_handle_getval */
  
 -static int us_handle_putval (FILE *fh, char **fields, int fields_num)
 +static int us_handle_listval (FILE *fh, char **fields, int fields_num)
  {
 -      char *hostname;
 -      char *plugin;
 -      char *plugin_instance;
 -      char *type;
 -      char *type_instance;
 -      int   status;
 -      int   i;
 -
 -      const data_set_t *ds;
 -      value_list_t vl = VALUE_LIST_INIT;
 -
 -      char **value_ptr;
 +      char buffer[1024];
 +      char **value_list = NULL;
 +      int value_list_len = 0;
 +      value_cache_t *entry;
 +      int i;
  
 -      if (fields_num != 3)
 +      if (fields_num != 1)
        {
 -              DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num);
 -              fprintf (fh, "-1 Wrong number of fields: Got %i, expected 3.\n",
 +              DEBUG ("unixsock plugin: us_handle_listval: "
 +                              "Wrong number of fields: %i", fields_num);
 +              fprintf (fh, "-1 Wrong number of fields: Got %i, expected 1.\n",
                                fields_num);
                fflush (fh);
                return (-1);
        }
  
 -      status = parse_identifier (fields[1], &hostname,
 -                      &plugin, &plugin_instance,
 -                      &type, &type_instance);
 -      if (status != 0)
 -      {
 -              DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]);
 -              fprintf (fh, "-1 Cannot parse identifier.\n");
 -              fflush (fh);
 -              return (-1);
 -      }
 -
 -      if ((strlen (hostname) >= sizeof (vl.host))
 -                      || (strlen (plugin) >= sizeof (vl.plugin))
 -                      || ((plugin_instance != NULL)
 -                              && (strlen (plugin_instance) >= sizeof (vl.plugin_instance)))
 -                      || ((type_instance != NULL)
 -                              && (strlen (type_instance) >= sizeof (vl.type_instance))))
 -      {
 -              fprintf (fh, "-1 Identifier too long.");
 -              return (-1);
 -      }
 -
 -      strcpy (vl.host, hostname);
 -      strcpy (vl.plugin, plugin);
 -      if (plugin_instance != NULL)
 -              strcpy (vl.plugin_instance, plugin_instance);
 -      if (type_instance != NULL)
 -              strcpy (vl.type_instance, type_instance);
 -
 -      { /* parse the time */
 -              char *t = fields[2];
 -              char *v = strchr (t, ':');
 -              if (v == NULL)
 -              {
 -                      fprintf (fh, "-1 No time found.");
 -                      return (-1);
 -              }
 -              *v = '\0'; v++;
 -
 -              vl.time = (time_t) atoi (t);
 -              if (vl.time == 0)
 -                      vl.time = time (NULL);
 -
 -              fields[2] = v;
 -      }
 -
 -      ds = plugin_get_ds (type);
 -      if (ds == NULL)
 -              return (-1);
 -
 -      value_ptr = (char **) calloc (ds->ds_num, sizeof (char *));
 -      if (value_ptr == NULL)
 -      {
 -              fprintf (fh, "-1 calloc failed.");
 -              return (-1);
 -      }
 -
 -      { /* parse the value-list. It's colon-separated. */
 -              char *dummy;
 -              char *ptr;
 -              char *saveptr;
 -
 -              i = 0;
 -              dummy = fields[2];
 -              saveptr = NULL;
 -              while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
 -              {
 -                      dummy = NULL;
 -                      if (i >= ds->ds_num)
 -                      {
 -                              i = ds->ds_num + 1;
 -                              break;
 -                      }
 -                      value_ptr[i] = ptr;
 -                      i++;
 -              }
 -
 -              if (i != ds->ds_num)
 -              {
 -                      sfree (value_ptr);
 -                      fprintf (fh, "-1 Number of values incorrect: Got %i, "
 -                                      "expected %i.", i, ds->ds_num);
 -                      return (-1);
 -              }
 -      } /* done parsing the value-list */
 -
 -      vl.values_len = ds->ds_num;
 -      vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
 -      if (vl.values == NULL)
 -      {
 -              sfree (value_ptr);
 -              fprintf (fh, "-1 malloc failed.");
 -              return (-1);
 -      }
 -      DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
 +      pthread_mutex_lock (&cache_lock);
  
 -      for (i = 0; i < ds->ds_num; i++)
 +      for (entry = cache_head; entry != NULL; entry = entry->next)
        {
 -              if (strcmp (value_ptr[i], "U") == 0)
 -                      vl.values[i].gauge = NAN;
 -              else if (ds->ds[i].type == DS_TYPE_COUNTER)
 -                      vl.values[i].counter = atoll (value_ptr[i]);
 -              else if (ds->ds[i].type == DS_TYPE_GAUGE)
 -                      vl.values[i].gauge = atof (value_ptr[i]);
 -      } /* for (i = 2 .. fields_num) */
 +              char **tmp;
 +
 +              snprintf (buffer, sizeof (buffer), "%u %s\n",
 +                              (unsigned int) entry->time, entry->name);
 +              buffer[sizeof (buffer) - 1] = '\0';
 +              
 +              tmp = realloc (value_list, sizeof (char *) * (value_list_len + 1));
 +              if (tmp == NULL)
 +                      continue;
 +              value_list = tmp;
  
 -      plugin_dispatch_values (type, &vl);
 +              value_list[value_list_len] = strdup (buffer);
  
 -      DEBUG ("value_ptr = 0x%p; vl.values = 0x%p;", (void *) value_ptr, (void *) vl.values);
 +              if (value_list[value_list_len] != NULL)
 +                      value_list_len++;
 +      } /* for (entry) */
  
 -      sfree (value_ptr);
 -      sfree (vl.values); 
 +      pthread_mutex_unlock (&cache_lock);
  
 -      fprintf (fh, "0 Success\n");
 +      DEBUG ("unixsock plugin: us_handle_listval: value_list_len = %i", value_list_len);
 +      fprintf (fh, "%i Values found\n", value_list_len);
 +      for (i = 0; i < value_list_len; i++)
 +              fputs (value_list[i], fh);
        fflush (fh);
  
        return (0);
 -} /* int us_handle_putval */
 +} /* int us_handle_listval */
  
  static void *us_handle_client (void *arg)
  {
                }
                else if (strcasecmp (fields[0], "putval") == 0)
                {
 -                      us_handle_putval (fh, fields, fields_num);
 +                      handle_putval (fh, fields, fields_num);
 +              }
 +              else if (strcasecmp (fields[0], "listval") == 0)
 +              {
 +                      us_handle_listval (fh, fields, fields_num);
                }
                else
                {