[collectd] Patch: Support custom CPU interval

Lee Hardy lee at leeh.co.uk
Sat Mar 8 21:42:15 CET 2014


Hi folks,

A project I'm doing ideally needs to run with a different interval for CPU
vs general checks, so I've written a patch to allow the 'Interval'
configuration option within the <Plugin "cpu"> configuration section.  This
will then collect the cpu-n details at the custom interval.

I'm not sure registering the read function at the top of the init()
function is ok, but couldn't see any obvious alternative other than adding
a new plugin postconfig callback.

Could someone give it a check for me please and consider whether it could
be merged?


I'm also considering how to do this for the aggregated cpu stats, in
particular I'm thinking about whether there's any issue in allowing the cpu
plugin to do aggregation itself.  It knows the stats and number of cpus, so
I'm guessing it could easily sum the percentages and divide by ncpus to get
the stat.  The aggregation plugin could still be used if people wanted more
complex cpu aggregations, but it seems like it would be useful to provide
the aggregation as standard, it would also allow the overall percentages to
run on the same interval.

I'd be happy to code that up and could put it behind a configuration option
if the maintainers preferred.

Any comments, please let me know.


Cheers,
Lee H
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.verplant.org/pipermail/collectd/attachments/20140308/8f12fc5f/attachment.html>
-------------- next part --------------
--- collectd-5.4.1/src/cpu.c.bak	2014-03-08 18:20:00.762005340 +0000
+++ collectd-5.4.1/src/cpu.c	2014-03-08 19:37:16.748003894 +0000
@@ -28,6 +28,7 @@
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
+#include "utils_time.h"
 
 #ifdef HAVE_MACH_KERN_RETURN_H
 # include <mach/kern_return.h>
@@ -145,8 +146,33 @@
 static int pnumcpu;
 #endif /* HAVE_PERFSTAT */
 
+static int cpu_read (user_data_t *);
+
+static cdtime_t cpu_interval;
+
+static const char *config_keys[] =
+{
+  "Interval"
+};
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
+
 static int init (void)
 {
+	struct timespec ts_interval;
+	struct timespec *ts_interval_ptr = NULL;
+
+	if(cpu_interval)
+	{
+		CDTIME_T_TO_TIMESPEC(cpu_interval, &ts_interval);
+		ts_interval_ptr = &ts_interval;
+	}
+
+	plugin_register_complex_read (/* group = */ NULL,
+			/* name      = */ "cpu",
+			/* callback  = */ cpu_read,
+			/* interval  = */ ts_interval_ptr,
+			/* user data = */ NULL);
+
 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
 	kern_return_t status;
 
@@ -241,6 +267,32 @@
 	return (0);
 } /* int init */
 
+static int cpu_config (const char *key, const char *value) /* {{{ */
+{
+	if (strcasecmp (key, "Interval") == 0)
+	{
+		double tmp;
+
+		tmp = atof (value);
+		if (tmp > 0.0)
+		{
+			cpu_interval = DOUBLE_TO_CDTIME_T (tmp);
+		}
+		else
+			WARNING ("cpu plugin: Ignoring invalid interval %g (%s)",
+					tmp, value);
+	}
+  	else
+	{
+		return (-1);
+	}
+
+
+	return (0);
+} /* }}} int cpu_config */
+
+
+
 static void submit (int cpu_num, const char *type_instance, derive_t value)
 {
 	value_t values[1];
@@ -260,7 +312,7 @@
 	plugin_dispatch_values (&vl);
 }
 
-static int cpu_read (void)
+static int cpu_read (user_data_t *ud)
 {
 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
 	int cpu;
@@ -601,6 +653,8 @@
 
 void module_register (void)
 {
+	plugin_register_config ("cpu", cpu_config,
+		config_keys, config_keys_num);
+
 	plugin_register_init ("cpu", init);
-	plugin_register_read ("cpu", cpu_read);
 } /* void module_register */


More information about the collectd mailing list