Re: [collectd] StoreRates option for Network plugin

2014-04-25 Thread Edson Marquezani Filho
Well, I've managed to do that myself (in part because I'm very
impacient, in part because I wanted to have a bit of fun.)

The patch based on the latest version (5.4.1) is attached. It adds the
StoreRate option for Network (only in global scope), using the
function uc_get_rates from utils_cache.c to calculate it.

I based my modifications on the CSV plugin, trying to be as clean as I
could. The patch itself is short, so I think I was. Also tried to
follow the current code style.

I compared the values from Network and CSV output, all they seem to
generate the same values. It's working here apparently well, but I
have not tested it for a long time yet.

Be aware that I'm not a professional programmer (I'm a sysadmin) and
some things may not be perfectly correct. One attention point is that,
based on CSV plugin, I followed a rule of only calculating rates for
values which are not of GAUGE type. I don't fully understand the
reason for that, but once the data is the same for any output plugin,
I assumed it was right for Network too.

I don't know whether this patch is going to be accepted or not, but I
think I'll use it, anyway (at least until this feature is available on
upstream), and I hope it helps those who have the same need I do.

Thanks.

On Thu, Apr 24, 2014 at 6:26 PM, Edson Marquezani Filho
edsonmarquez...@gmail.com wrote:
 Hello, everyone.

 I'm new with Collectd but I'm finding it an amazing tool, very useful.

 I'm trying to put up a simple setup here with it and Logstash, to send
 metrics to an specific system we have in the company I work for.
 Everything fine so far, except that I can't obtain rate values for
 CPU. I'm using the network plugin to connect with Logstash, and it
 does not have the StoreRates option that all write plugins seem to
 have.

 Is that feature in the plans for a near future? (I couldn't find much
 about that on Internet, as if no one needed it like me.)

 I know that I could get around it writing to a CSV file and reading it
 from logstash but I really wouldn't like to, considering that Logstash
 has even an specific output type for Collectd.

 Thank you.
diff -pur collectd-5.4.1/src/network.c collectd-5.4.1-mine/src/network.c
--- collectd-5.4.1/src/network.c	2014-01-26 06:09:23.532559941 -0200
+++ collectd-5.4.1-mine/src/network.c	2014-04-25 18:23:29.610992169 -0300
@@ -324,6 +324,8 @@ static derive_t stats_values_sent = 0;
 static derive_t stats_values_not_sent = 0;
 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
 
+static int store_rates = 0;
+
 /*
  * Private functions
  */
@@ -601,9 +603,11 @@ static int write_part_values (char **ret
 	uint16_t  pkg_num_values;
 	uint8_t  *pkg_values_types;
 	value_t  *pkg_values;
+	gauge_t  *rates = NULL;
 
 	int offset;
 	int i;
+	
 
 	num_values = vl-values_len;
 	packet_len = sizeof (part_header_t) + sizeof (uint16_t)
@@ -636,32 +640,47 @@ static int write_part_values (char **ret
 	for (i = 0; i  num_values; i++)
 	{
 		pkg_values_types[i] = (uint8_t) ds-ds[i].type;
-		switch (ds-ds[i].type)
-		{
-			case DS_TYPE_COUNTER:
-pkg_values[i].counter = htonll (vl-values[i].counter);
-break;
-
-			case DS_TYPE_GAUGE:
-pkg_values[i].gauge = htond (vl-values[i].gauge);
-break;
-
-			case DS_TYPE_DERIVE:
-pkg_values[i].derive = htonll (vl-values[i].derive);
-break;
 
-			case DS_TYPE_ABSOLUTE:
-pkg_values[i].absolute = htonll (vl-values[i].absolute);
-break;
-
-			default:
-free (pkg_values_types);
-free (pkg_values);
-ERROR (network plugin: write_part_values: 
-		Unknown data source type: %i,
-		ds-ds[i].type);
+		if (ds-ds[i].type == DS_TYPE_GAUGE)
+		{
+			pkg_values[i].gauge = htond (vl-values[i].gauge);
+		}
+		else if (store_rates == 1)
+		{
+			if (rates == NULL)
+rates = uc_get_rate (ds, vl);
+			if (rates == NULL)
+			{
+WARNING (csv plugin: 
+	uc_get_rate failed.);
 return (-1);
-		} /* switch (ds-ds[i].type) */
+			}
+			else
+			{
+pkg_values[i].gauge = htond (rates[i]);
+pkg_values_types[i] = DS_TYPE_GAUGE;
+			}
+		}
+		else if (ds-ds[i].type == DS_TYPE_COUNTER)
+		{
+			pkg_values[i].counter = htonll (vl-values[i].counter);
+		}
+		else if (ds-ds[i].type == DS_TYPE_DERIVE)
+		{
+			pkg_values[i].derive = htonll (vl-values[i].derive);
+		}
+		else if (ds-ds[i].type == DS_TYPE_ABSOLUTE)
+		{
+			pkg_values[i].absolute = htonll (vl-values[i].absolute);
+		}
+		else {
+			free (pkg_values_types);
+			free (pkg_values);
+			ERROR (network plugin: write_part_values: 
+	Unknown data source type: %i,
+	ds-ds[i].type);
+			return (-1);
+		}
 	} /* for (num_values) */
 
 	/*
@@ -687,6 +706,7 @@ static int write_part_values (char **ret
 
 	free (pkg_values_types);
 	free (pkg_values);
+	free (rates);
 
 	return (0);
 } /* int write_part_values */
@@ -3182,6 +3202,10 @@ static int network_config (oconfig_item_
   network_config_set_boolean (child, network_config_forward);
 else if (strcasecmp (ReportStats, 

[collectd] StoreRates option for Network plugin

2014-04-24 Thread Edson Marquezani Filho
Hello, everyone.

I'm new with Collectd but I'm finding it an amazing tool, very useful.

I'm trying to put up a simple setup here with it and Logstash, to send
metrics to an specific system we have in the company I work for.
Everything fine so far, except that I can't obtain rate values for
CPU. I'm using the network plugin to connect with Logstash, and it
does not have the StoreRates option that all write plugins seem to
have.

Is that feature in the plans for a near future? (I couldn't find much
about that on Internet, as if no one needed it like me.)

I know that I could get around it writing to a CSV file and reading it
from logstash but I really wouldn't like to, considering that Logstash
has even an specific output type for Collectd.

Thank you.

___
collectd mailing list
collectd@verplant.org
http://mailman.verplant.org/listinfo/collectd