[collectd] [PATCH] An option for the csv plugin to pretty print dates

Chris Procter cprocter at redhat.com
Wed Apr 11 00:20:27 CEST 2012


Hi
This is a little patch to the csv plugin to add a "Date" directive which when set to true will pretty print the date stamp to the csv files if set to false (the default) they will continue to be printed in the traditional format.

Example:
<Plugin csv>
        DataDir "/opt/collectd/var/lib/collectd/csv"
        StoreRates true
        Date true
</Plugin>

Produces files that look like:
Tue Apr 10 23:11:54 2012,2414174208.000000
Tue Apr 10 23:12:04 2012,2417004544.000000
Tue Apr 10 23:12:14 2012,2413944832.000000

The idea is to make the csv files a little more human readable for those times your investigating directly rather then feeding the output to a graphing or monitoring tool.


chris



Signed-off-by: chris procter <cprocter at redhat.com>
---
 src/csv.c |   36 ++++++++++++++++++++++++++++++++----
 1 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/src/csv.c b/src/csv.c
index 02d62c1..f9fb6fd 100644
--- a/src/csv.c
+++ b/src/csv.c
@@ -27,19 +27,23 @@
 #include "utils_cache.h"
 #include "utils_parse_option.h"
 
+#include <time.h>
+
 /*
  * Private variables
  */
 static const char *config_keys[] =
 {
 	"DataDir",
-	"StoreRates"
+	"StoreRates",
+	"Date"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
 static char *datadir   = NULL;
 static int store_rates = 0;
 static int use_stdio   = 0;
+static int showdates = 0;
 
 static int value_list_to_string (char *buffer, int buffer_len,
 		const data_set_t *ds, const value_list_t *vl)
@@ -48,13 +52,23 @@ static int value_list_to_string (char *buffer, int buffer_len,
 	int status;
 	int i;
 	gauge_t *rates = NULL;
+	time_t timestamp;
 
 	assert (0 == strcmp (ds->type, vl->type));
 
 	memset (buffer, '\0', buffer_len);
 
-	status = ssnprintf (buffer, buffer_len, "%.3f",
-			CDTIME_T_TO_DOUBLE (vl->time));
+	if(showdates == 0)
+	{
+		status = ssnprintf (buffer, buffer_len, "%.3f", CDTIME_T_TO_DOUBLE (vl->time));
+	}
+	else
+	{
+		timestamp = CDTIME_T_TO_TIME_T(vl->time);
+		status = ssnprintf (buffer, buffer_len, "%s",ctime(&timestamp));
+		status--;
+	}
+
 	if ((status < 1) || (status >= buffer_len))
 		return (-1);
 	offset = status;
@@ -203,7 +217,14 @@ static int csv_create_file (const char *filename, const data_set_t *ds)
 		return (-1);
 	}
 
-	fprintf (csv, "epoch");
+	if(showdates == 0)
+	{
+		fprintf (csv, "epoch");
+	}
+	else
+	{
+		fprintf (csv, "date");
+	}
 	for (i = 0; i < ds->ds_num; i++)
 		fprintf (csv, ",%s", ds->ds[i].name);
 
@@ -252,6 +273,13 @@ static int csv_config (const char *key, const char *value)
 		else
 			store_rates = 0;
 	}
+	else if (strcasecmp ("Date", key) == 0)
+	{
+		if (IS_TRUE (value))
+			showdates = 1;
+		else
+			showdates = 0;
+	}
 	else
 	{
 		return (-1);
-- 
1.7.1



More information about the collectd mailing list