[collectd] [PATCH] cpu plugin: Add AggregateCPU option.

Pierre-Yves Kerembellec pierre-yves.kerembellec at dailymotion.com
Wed Apr 7 12:30:06 CEST 2010


---
 src/cpu.c |   23 +++++++++++++++++++++--
 1 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/src/cpu.c b/src/cpu.c
index 7aa6361..3947f54 100644
--- a/src/cpu.c
+++ b/src/cpu.c
@@ -145,6 +145,23 @@ static int numcpu;
 static int pnumcpu;
 #endif /* HAVE_PERFSTAT */
 
+static const char *config_keys[] =
+{
+	"AggregateCPU"
+};
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
+static int aggregate_cpu = 0;
+
+static int cpu_config (const char *key, const char *value)
+{
+	if (! strcasecmp ("AggregateCPU", key))
+	{
+		if (IS_TRUE(value))
+			aggregate_cpu = 1;
+	}
+	return 0;
+}
+
 static int init (void)
 {
 #if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE
@@ -252,7 +269,8 @@ static void submit (int cpu_num, const char *type_instance, counter_t value)
 	vl.values_len = 1;
 	sstrncpy (vl.host, hostname_g, sizeof (vl.host));
 	sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
-	ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
+	if (! aggregate_cpu)
+		ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
 			"%i", cpu_num);
 	sstrncpy (vl.type, "cpu", sizeof (vl.type));
 	sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
@@ -372,7 +390,7 @@ static int cpu_read (void)
 	{
 		if (strncmp (buf, "cpu", 3))
 			continue;
-		if ((buf[3] < '0') || (buf[3] > '9'))
+		if ((! aggregate_cpu && (buf[3] < '0' || buf[3] > '9')) || (aggregate_cpu && buf[3] != ' '))
 			continue;
 
 		numfields = strsplit (buf, fields, 9);
@@ -603,6 +621,7 @@ static int cpu_read (void)
 
 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 */
-- 
1.6.3.3




More information about the collectd mailing list