[collectd] [PATCH] - flush rrdtool cache to disk on SIGUSR1

bd at bc-bd.org bd at bc-bd.org
Mon Feb 25 19:58:31 CET 2008


A small patch enabling collectd to write the rrdtool cache onto
the disk upon receiving SIGUSR1.

Using the cache feature greatly reduced IO on my machine, with
the known tradeoff that f.e. collection.cgi "misses" the last N
seconds of data.

To be able to use a) the cache and b) access up-to-date data when
you need it I wrote this small patch.

---
 src/rrdtool.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/src/rrdtool.c b/src/rrdtool.c
index 024b04e..65acbe3 100644
--- a/src/rrdtool.c
+++ b/src/rrdtool.c
@@ -25,6 +25,7 @@
 #include "utils_avltree.h"
 
 #include <rrd.h>
+#include <signal.h>
 
 #if HAVE_PTHREAD_H
 # include <pthread.h>
@@ -120,10 +121,20 @@ static pthread_mutex_t librrd_lock = PTHREAD_MUTEX_INITIALIZER;
 
 static int do_shutdown = 0;
 
+static struct sigaction sigUsr1Action;
+
 /* * * * * * * * * *
  * WARNING:  Magic *
  * * * * * * * * * */
 
+static void rrd_cache_flush (int timeout);
+
+static void sigUsr1Handler (int signal)
+{
+	DEBUG("sigUsr1Handler");
+	rrd_cache_flush(0);
+}
+
 static void rra_free (int rra_num, char **rra_def)
 {
 	int i;
@@ -1143,6 +1154,16 @@ static int rrd_init (void)
 		return (-1);
 	}
 
+	memset (&sigUsr1Action, '\0', sizeof (sigUsr1Action));
+	sigUsr1Action.sa_handler = sigUsr1Handler;
+	status = sigaction (SIGUSR1, &sigUsr1Action, NULL);
+
+	if (status != 0)
+	{
+		ERROR ("rrdtool plugin: Cannot hook SIGUSR1.");
+		return (-1);
+	}
+
 	DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
 			" heartbeat = %i; rrarows = %i; xff = %lf;",
 			(datadir == NULL) ? "(null)" : datadir,
-- 
1.4.4.4




More information about the collectd mailing list