Merge branch 'collectd-4.0'
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Tue, 19 Jun 2007 19:43:42 +0000 (21:43 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Tue, 19 Jun 2007 19:43:42 +0000 (21:43 +0200)
1  2 
configure.in
src/Makefile.am
src/rrdtool.c

diff --combined configure.in
@@@ -1,5 -1,5 +1,5 @@@
  dnl Process this file with autoconf to produce a configure script.
- AC_INIT(collectd, 4.0.2)
+ AC_INIT(collectd, 4.0.3)
  AC_CONFIG_SRCDIR(src/collectd.c)
  AC_CONFIG_HEADERS(src/config.h)
  AM_INIT_AUTOMAKE(dist-bzip2)
@@@ -413,7 -413,17 +413,17 @@@ AC_CHECK_FUNCS(getgrgid getpwuid
  AC_CHECK_FUNCS(getifaddrs)
  
  # Check for NAN
- nan_type="none"
+ AC_ARG_WITH(nan-emulation, [AS_HELP_STRING([--with-nan-emulation], [use emulated NAN. For crosscompiling only.])],
+ [
+  if test "x$withval" = "xno"; then
+        nan_type="none"
+  else if test "x$withval" = "xyes"; then
+        nan_type="zero"
+  else
+        nan_type="$withval"
+  fi; fi
+ ],
+ [nan_type="none"])
  if test "x$nan_type" = "xnone"; then
    AC_CACHE_CHECK([whether NAN is defined by default],
      [have_nan_default],
@@@ -436,9 -446,7 +446,7 @@@ static float foo = NAN
    )
    if test "x$have_nan_default" = "xyes"
    then
-    nan_type="default"
-    AC_DEFINE(NAN_STATIC_DEFAULT, 1,
-      [Define if NAN is defined by default and can initialize static variables.])
+     nan_type="default"
    fi
  fi
  if test "x$nan_type" = "xnone"; then
@@@ -464,9 -472,7 +472,7 @@@ static float foo = NAN
    )
    if test "x$have_nan_isoc" = "xyes"
    then
-    nan_type="isoc99"
-    AC_DEFINE(NAN_STATIC_ISOC, 1,
-      [Define if NAN is defined by __USE_ISOC99 and can initialize static variables.])
+     nan_type="isoc99"
    fi
  fi
  if test "x$nan_type" = "xnone"; then
@@@ -498,14 -504,22 +504,22 @@@ static float foo = NAN
    )
    if test "x$have_nan_zero" = "xyes"
    then
-    nan_type="zero"
-    AC_DEFINE(NAN_ZERO_ZERO, 1,
-      [Define if NAN can be defined as (0.0 / 0.0)])
+     nan_type="zero"
    fi
  fi
- if test "x$nan_type" = "xnone"; then
+ if test "x$nan_type" = "xdefault"; then
+   AC_DEFINE(NAN_STATIC_DEFAULT, 1,
+     [Define if NAN is defined by default and can initialize static variables.])
+ else if test "x$nan_type" = "xisoc99"; then
+   AC_DEFINE(NAN_STATIC_ISOC, 1,
+     [Define if NAN is defined by __USE_ISOC99 and can initialize static variables.])
+ else if test "x$nan_type" = "xzero"; then
+   AC_DEFINE(NAN_ZERO_ZERO, 1,
+     [Define if NAN can be defined as (0.0 / 0.0)])
+ else
    AC_MSG_ERROR([Didn't find out how to statically initialize variables to NAN. Sorry.])
- fi
+ fi; fi; fi
  
  # For mount interface
  #AC_CHECK_FUNCS(getfsent getvfsent)
  fi
  AM_CONDITIONAL(BUILD_WITH_LIBIPTC, test "x$with_libiptc" = "xyes")
  
 +AC_ARG_WITH(libnetsnmp, [AS_HELP_STRING([--with-libnetsnmp@<:@=PREFIX@:>@], [Path to the Net-SNMPD library.])],
 +[
 +      if test "x$withval" != "xno" && test "x$withval" != "xyes"
 +      then
 +              LDFLAGS="$LDFLAGS -L$withval/lib"
 +              CPPFLAGS="$CPPFLAGS -I$withval/include"
 +              with_libnetsnmp="yes"
 +      fi
 +],
 +[with_libnetsnmp="yes"])
 +if test "x$with_libnetsnmp" = "xyes"
 +then
 +      AC_CHECK_LIB(netsnmp, init_snmp,
 +      [
 +              AC_DEFINE(HAVE_LIBSNMP, 1, [Define to 1 if you have the Net-SNMP library (-lnetsnmp).])
 +      ], [with_libnetsnmp="no (libnetsnmp not found)"])
 +fi
 +if test "x$with_libnetsnmp" = "xyes"
 +then
 +      AC_CHECK_HEADERS(net-snmp/net-snmp-config.h,
 +      [
 +              AC_DEFINE(HAVE_NET_SNMP_NET_SNMP_CONFIG_H, 1, [Define to 1 if you have the <net-snmp/net-snmp-config.h> header file.])
 +      ], [with_libnetsnmp="no (net-snmp/net-snmp-config.h not found)"])
 +fi
 +AM_CONDITIONAL(BUILD_WITH_LIBNETSNMP, test "x$with_libnetsnmp" = "xyes")
 +
  AC_ARG_WITH(libupsclient, [AS_HELP_STRING([--with-libupsclient@<:@=PREFIX@:>@], [Path to libupsclient.])],
  [
        if test "x$withval" != "xno" && test "x$withval" != "xyes"
  fi
  AM_CONDITIONAL(BUILD_WITH_LIBUPSCLIENT, test "x$with_libupsclient" = "xyes")
  
 +### BEGIN of check for libxmms ###
 +with_xmms_config="xmms-config"
 +with_xmms_prefix=0
 +with_xmms_cflags=""
 +with_xmms_libs=""
 +AC_ARG_WITH(libxmms, [AS_HELP_STRING([--with-libxmms@<:@=PREFIX@:>@], [Path to libxmms.])],
 +[
 +      if test "x$withval" != "xno" -a "x$withval" != "xyes"
 +      then
 +              if test -x "$withval/bin/xmms-config"
 +              then
 +                      with_xmms_config="$withval/bin/xmms-config"
 +                      with_xmms_prefix=1
 +              fi
 +      fi
 +      if test "x$withval" = "xno"
 +      then
 +              with_libxmms="no"
 +      else
 +              with_libxmms="yes"
 +      fi
 +],
 +[
 +      with_libxmms="yes"
 +])
 +if test "x$with_libxmms" = "xyes"
 +then
 +      with_xmms_cflags=`$with_xmms_config --cflags 2>/dev/null`
 +      xmms_config_status=$?
 +
 +      if test $xmms_config_status -ne 0
 +      then
 +              with_libxmms="no"
 +      fi
 +fi
 +if test "x$with_libxmms" = "xyes"
 +then
 +      with_xmms_libs=`$with_xmms_config --libs 2>/dev/null`
 +      xmms_config_status=$?
 +
 +      if test $xmms_config_status -ne 0
 +      then
 +              with_libxmms="no"
 +      fi
 +fi
 +if test "x$with_libxmms" = "xyes"
 +then
 +      AC_CHECK_LIB(xmms, xmms_remote_get_info,
 +      [
 +              BUILD_WITH_LIBXMMS_CFLAGS="$with_xmms_cflags"
 +              BUILD_WITH_LIBXMMS_LIBS="$with_xmms_libs"
 +              AC_SUBST(BUILD_WITH_LIBXMMS_CFLAGS)
 +              AC_SUBST(BUILD_WITH_LIBXMMS_LIBS)
 +      ],
 +      [
 +              with_libxmms="no"
 +      ],
 +      [$with_xmms_libs])
 +fi
 +with_libxmms_numeric=0
 +if test "x$with_libxmms" = "xyes"
 +then
 +      with_libxmms_numeric=1
 +fi
 +AC_DEFINE_UNQUOTED(HAVE_LIBXMMS, [$with_libxmms_numeric], [Define to 1 if you have the 'xmms' library (-lxmms).])
 +AM_CONDITIONAL(BUILD_WITH_LIBXMMS, test "x$with_libxmms" = "xyes")
 +### END of check for libxmms ###
 +
  # Check for enabled/disabled features
  #
  
@@@ -1443,7 -1363,6 +1457,7 @@@ AC_COLLECTD([ping],      [disable], [mo
  AC_COLLECTD([processes], [disable], [module], [processes statistics])
  AC_COLLECTD([sensors],   [disable], [module], [lm_sensors statistics])
  AC_COLLECTD([serial],    [disable], [module], [serial statistics])
 +AC_COLLECTD([snmp],      [disable], [module], [serial statistics])
  AC_COLLECTD([logfile],   [disable], [module], [logfile log facility])
  AC_COLLECTD([swap],      [disable], [module], [swap statistics])
  AC_COLLECTD([syslog],    [disable], [module], [syslog log facility])
@@@ -1452,7 -1371,6 +1466,7 @@@ AC_COLLECTD([unixsock],  [disable], [mo
  AC_COLLECTD([users],     [disable], [module], [user count statistics])
  AC_COLLECTD([vserver],   [disable], [module], [vserver statistics])
  AC_COLLECTD([wireless],  [disable], [module], [wireless link statistics])
 +AC_COLLECTD([xmms],      [disable], [module], [wireless link statistics])
  
  AC_OUTPUT(Makefile src/Makefile src/collectd.conf src/liboconfig/Makefile src/liboping/Makefile)
  
@@@ -1472,7 -1390,6 +1486,7 @@@ Configuration
      libiptc . . . . . . $with_libiptc
      libkstat  . . . . . $with_kstat
      libmysql  . . . . . $with_libmysql
 +    libnetsnmp  . . . . $with_libnetsnmp
      liboconfig  . . . . $with_liboconfig
      liboping  . . . . . $with_liboping
      libpcap . . . . . . $with_libpcap
      libsensors  . . . . $with_lm_sensors
      libstatgrab . . . . $with_libstatgrab
      libupsclient  . . . $with_libupsclient
 +    libxmms . . . . . . $with_libxmms
  
    Features:
      daemon mode . . . . $enable_daemon
      processes . . . . . $enable_processes
      sensors . . . . . . $enable_sensors
      serial  . . . . . . $enable_serial
 +    snmp  . . . . . . . $enable_snmp
      swap  . . . . . . . $enable_swap
      syslog  . . . . . . $enable_syslog
      tape  . . . . . . . $enable_tape
      users . . . . . . . $enable_users
      vserver . . . . . . $enable_vserver
      wireless  . . . . . $enable_wireless
 +    xmms  . . . . . . . $enable_xmms
  
  EOF
diff --combined src/Makefile.am
@@@ -25,7 -25,6 +25,7 @@@ collectd_SOURCES = collectd.c collectd.
  collectd_CPPFLAGS = $(LTDLINCL)
  collectd_CPPFLAGS += -DPREFIX='"${prefix}"'
  collectd_CPPFLAGS += -DCONFIGFILE='"${sysconfdir}/${PACKAGE_NAME}.conf"'
 +collectd_CPPFLAGS += -DLOCALSTATEDIR='"${localstatedir}"'
  collectd_CPPFLAGS += -DPKGLOCALSTATEDIR='"${localstatedir}/lib/${PACKAGE_NAME}"'
  if BUILD_FEATURE_DAEMON
  collectd_CPPFLAGS += -DPIDFILE='"${localstatedir}/run/${PACKAGE_NAME}.pid"'
@@@ -472,17 -471,6 +472,17 @@@ collectd_LDADD += "-dlopen" serial.l
  collectd_DEPENDENCIES += serial.la
  endif
  
 +if BUILD_MODULE_SNMP
 +pkglib_LTLIBRARIES += snmp.la
 +snmp_la_SOURCES = snmp.c
 +snmp_la_LDFLAGS = -module -avoid-version
 +if BUILD_WITH_LIBNETSNMP
 +snmp_la_LDFLAGS += -lnetsnmp
 +endif
 +collectd_LDADD += "-dlopen" snmp.la
 +collectd_DEPENDENCIES += snmp.la
 +endif
 +
  if BUILD_MODULE_SWAP
  pkglib_LTLIBRARIES += swap.la
  swap_la_SOURCES = swap.c
@@@ -531,7 -519,7 +531,7 @@@ endi
  if BUILD_MODULE_UNIXSOCK
  pkglib_LTLIBRARIES += unixsock.la
  unixsock_la_SOURCES = unixsock.c
 -unixsock_la_CPPFLAGS = -DPREFIX='"${prefix}"'
 +unixsock_la_CPPFLAGS = -DLOCALSTATEDIR='"${localstatedir}"'
  unixsock_la_LDFLAGS = -module -avoid-version -lpthread
  collectd_LDADD += "-dlopen" unixsock.la
  collectd_DEPENDENCIES += unixsock.la
@@@ -561,23 -549,9 +561,23 @@@ collectd_LDADD += "-dlopen" wireless.l
  collectd_DEPENDENCIES += wireless.la
  endif
  
 +if BUILD_MODULE_XMMS
 +pkglib_LTLIBRARIES += xmms.la
 +xmms_la_SOURCES = xmms.c
 +xmms_la_CFLAGS = 
 +xmms_la_LDFLAGS = -module -avoid-version
 +if BUILD_WITH_LIBXMMS
 +xmms_la_CFLAGS += $(BUILD_WITH_LIBXMMS_CFLAGS)
 +xmms_la_LDFLAGS += $(BUILD_WITH_LIBXMMS_LIBS)
 +endif
 +collectd_LDADD += "-dlopen" xmms.la
 +collectd_DEPENDENCIES += xmms.la
 +endif
 +
 +
  dist_man_MANS = collectd.1 collectd-nagios.1 collectd.conf.5 \
                collectd-email.5 collectd-exec.5 collectd-perl.5 \
 -              collectd-unixsock.5
 +              collectd-snmp.5 collectd-unixsock.5
  
  #collectd_1_SOURCES = collectd.pod
  
@@@ -597,4 -571,4 +597,4 @@@ install-exec-hook
        else \
                $(INSTALL) -m 0640 collectd.conf $(DESTDIR)$(sysconfdir)/collectd.conf; \
        fi; \
-       cp -f types.db $(DESTDIR)$(pkglibdir)/;
+       cp -f $(srcdir)/types.db $(DESTDIR)$(pkglibdir)/;
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;
        return (ds_num);
  }
  
 -static int rrd_create_file (char *filename, const data_set_t *ds)
 +static int rrd_create_file (char *filename, const data_set_t *ds, const value_list_t *vl)
  {
        char **argv;
        int argc;
        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);
        }
  
        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);
        }
  
  
        free (argv);
        ds_free (ds_num, ds_def);
 +      rra_free (rra_num, rra_def);
  
        return (status);
  }
@@@ -409,7 -361,7 +409,7 @@@ static int value_list_to_string (char *
        int status;
        int i;
  
-       memset (buffer, '\0', sizeof (buffer_len));
+       memset (buffer, '\0', buffer_len);
  
        status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
        if ((status < 1) || (status >= buffer_len))
@@@ -482,143 -434,138 +482,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);
        {
                if (errno == ENOENT)
                {
 -                      if (rrd_create_file (filename, ds))
 +                      if (rrd_create_file (filename, ds, vl))
                                return (-1);
                }
                else
                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 -833,47 +968,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 -888,4 +1043,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);
  }