[collectd] 3.10.1 - proposed patch to extend sensors plugin

Luboš Staněk lubek at users.sourceforge.net
Sun Oct 22 15:34:14 CEST 2006


Hi all,
I reviewed the collectd functions and discovered, that the sensors
plugin requires several modifications.

Problems solved:
- precise sensor feature selection
There are boards that feature more than one sensor chip. It is possible
to select sensor exactly similar way the sensors application does it. It
is required for the "Ignore" config feature.
- more sensors
All sensors are gathered except controls (only temps and fans previously).
- honor sensors.conf's ignore
Do not gather data for ignored sensors.
- config Ignore option
The collectd daemon can be instructed to ignore other sensors.

Warning!
The collectd will create new differently named .rrds according to the
new naming scheme and start gather data there. The old .rrds will remain
in the data directory. If you want to continue with previous .rrds,
rename old files according to the new scheme.

Best regards,
Lubos


And here it is:
---------------- snip ---------------------
diff -pur collectd-3.10.1/README collectd-3.10.1-allsensors/README
--- collectd-3.10.1/README	2006-07-09 10:16:40.000000000 +0200
+++ collectd-3.10.1-allsensors/README	2006-10-22 15:04:15.000000000 +0200
@@ -65,7 +65,7 @@ Features
       (Number of running, sleeping, zombie, ... processes)

     - Sensors
-      (System temperatured and fan rotation speeds)
+      (System voltages, temperatures and fan rotation speeds)

     - Serial
       (RX and TX of serial interfaces)
diff -pur collectd-3.10.1/src/collectd.conf.in
collectd-3.10.1-allsensors/src/collectd.conf.in
--- collectd-3.10.1/src/collectd.conf.in	2006-07-19 18:58:41.000000000 +0200
+++ collectd-3.10.1-allsensors/src/collectd.conf.in	2006-10-22
15:04:15.000000000 +0200
@@ -72,6 +72,10 @@
 #	Host host.foo.bar
 #</Plugin>

+#<Plugin sensors>
+#	Ignore it8712-isa-0290-in8
+#</Plugin>
+
 #<Plugin traffic>
 #	Interface eth0
 #	IgnoreSelected false
diff -pur collectd-3.10.1/src/collectd.conf.5
collectd-3.10.1-allsensors/src/collectd.conf.5
--- collectd-3.10.1/src/collectd.conf.5	2006-07-19 22:14:09.000000000 +0200
+++ collectd-3.10.1-allsensors/src/collectd.conf.5	2006-10-22
15:04:15.000000000 +0200
@@ -317,6 +317,14 @@ multiple hosts.
 .IP "\fB\s-1TTL\s0\fR \fI0\-255\fR" 4
 .IX Item "TTL 0-255"
 Sets the Time-To-Live of generated \s-1ICMP\s0 packets.
+.ie n .Sh "Plugin ""sensors"""
+.el .Sh "Plugin \f(CWsensors\fP"
+.IX Subsection "Plugin sensors"
+.IP "\fBIgnore\fR \fIchip-bus-address-feature\fR" 4
+.IX Item "Ignore chip-bus-address-feature"
+Ignore the selected sensor feature. This sensor feature will not then
be collected.
+For example the option "\fBIgnore\fR \fIit8712-isa-0290-in8\fR" will
cause the collectd
+to ignore the voltage sensor in8 of it8712 on isa bus at address 0290.
 .ie n .Sh "Plugin ""traffic"""
 .el .Sh "Plugin \f(CWtraffic\fP"
 .IX Subsection "Plugin traffic"
diff -pur collectd-3.10.1/src/sensors.c
collectd-3.10.1-allsensors/src/sensors.c
--- collectd-3.10.1/src/sensors.c	2006-06-30 10:02:36.000000000 +0200
+++ collectd-3.10.1-allsensors/src/sensors.c	2006-10-22
15:04:15.000000000 +0200
@@ -18,11 +18,18 @@
  *
  * Authors:
  *   Florian octo Forster <octo at verplant.org>
+ *
+ *   Lubos Stanek <lubek at users.sourceforge.net> Sun Oct 22, 2006
+ *   - precise sensor feature selection (chip-bus-address-feature)
+ *   - more sensor features (all except controls)
+ *   - honor sensors.conf's ignored
+ *   - config ignore option
  **/

 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
+#include "configfile.h"

 #define MODULE_NAME "sensors"

@@ -42,6 +49,13 @@

 static char *filename_format = "sensors-%s.rrd";

+static char *config_keys[] =
+{
+	"Ignore",
+	NULL
+};
+static int config_keys_num = 1;
+
 static char *ds_def[] =
 {
 	"DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
@@ -49,6 +63,9 @@ static char *ds_def[] =
 };
 static int ds_num = 1;

+static char **ignore_list = NULL;
+static int    ignore_list_num = 0;
+
 #ifdef HAVE_LIBSENSORS
 typedef struct featurelist
 {
@@ -60,6 +77,54 @@ typedef struct featurelist
 featurelist_t *first_feature = NULL;
 #endif /* defined (HAVE_LIBSENSORS) */

+static int sensors_config (char *key, char *value)
+{
+	char **temp;
+
+	if (strcasecmp (key, "Ignore") == 0)
+	{
+		temp = (char **) realloc (ignore_list, (ignore_list_num + 1) * sizeof
(char *));
+		if (temp == NULL)
+		{
+			syslog (LOG_EMERG, "Cannot allocate more memory.");
+			return (1);
+		}
+		ignore_list = temp;
+
+		if ((ignore_list[ignore_list_num] = strdup (value)) == NULL)
+		{
+			syslog (LOG_EMERG, "Cannot allocate memory.");
+			return (1);
+		}
+		ignore_list_num++;
+	}
+	else
+	{
+		return (-1);
+	}
+
+	return (0);
+}
+
+/*
+ * Check if this feature should be ignored. This is called from
+ * both, `submit' and `write' to give client and server the ability to
+ * ignore certain stuff...
+ */
+static int config_get_ignored (const char *fullname)
+{
+	int i;
+
+	/* If no ignored are given collect all features. */
+	if (ignore_list_num < 1)
+		return (0);
+
+	for (i = 0; i < ignore_list_num; i++)
+		if (strcasecmp (fullname, ignore_list[i]) == 0)
+			return (1);
+	return (0);
+}
+
 static void collectd_sensors_init (void)
 {
 #ifdef HAVE_LIBSENSORS
@@ -110,10 +175,18 @@ static void collectd_sensors_init (void)
 			if (data->mapping != SENSORS_NO_MAPPING)
 				continue;

-			/* Only temperature for now.. */
-			if (strncmp (data->name, "temp", 4)
-					&& strncmp (data->name, "fan", 3))
-				continue;
+			/* Do not gather control features */
+			/* e.g.: alarms, sensor types, pwms, ... */
+			if (!strncmp (data->name, "alarms", 6))
+			    continue;
+			if (!strncmp (data->name, "sensor", 6))
+			    continue;
+			if (!strncmp (data->name, "pwm", 3))
+			    continue;
+
+			/* skip ignored in sensors.conf */
+			if (sensors_get_ignored(*chip, data->number) == 0)
+			    continue;

 			if ((new_feature = (featurelist_t *) malloc (sizeof
(featurelist_t))) == NULL)
 			{
@@ -154,6 +227,10 @@ static void sensors_write (char *host, c
 	char file[BUFSIZE];
 	int status;

+	/* skip ignored in our config */
+	if (config_get_ignored (inst))
+	    return;
+
 	status = snprintf (file, BUFSIZE, filename_format, inst);
 	if (status < 1)
 		return;
@@ -169,10 +246,14 @@ static void sensors_submit (const char *
 	char buf[BUFSIZE];
 	char inst[BUFSIZE];

-	if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime, value)
>= BUFSIZE)
+	if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name) >= BUFSIZE)
 		return;

-	if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name) >= BUFSIZE)
+	/* skip ignored in our config */
+	if (config_get_ignored (inst))
+	    return;
+
+	if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime, value)
>= BUFSIZE)
 		return;

 	plugin_submit (MODULE_NAME, inst, buf);
@@ -182,13 +263,31 @@ static void sensors_read (void)
 {
 	featurelist_t *feature;
 	double value;
+	char chip_fullprefix[BUFSIZE];

 	for (feature = first_feature; feature != NULL; feature = feature->next)
 	{
 		if (sensors_get_feature (*feature->chip, feature->data->number,
&value) < 0)
 			continue;

-		sensors_submit (feature->data->name, feature->chip->prefix, value);
+		/* full chip name logic borrowed from lm_sensors */
+		if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
+		{
+		    if (snprintf (chip_fullprefix, BUFSIZE, "%s-isa-%04x",
feature->chip->prefix, feature->chip->addr) >= BUFSIZE)
+			continue;
+		}
+		else if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
+		{
+		    if (snprintf (chip_fullprefix, BUFSIZE, "%s-%s-%04x",
feature->chip->prefix, feature->chip->busname, feature->chip->addr) >=
BUFSIZE)
+			continue;
+		}
+		else
+		{
+		    if (snprintf (chip_fullprefix, BUFSIZE, "%s-i2c-%d-%02x",
feature->chip->prefix, feature->chip->bus, feature->chip->addr) >= BUFSIZE)
+			continue;
+		}
+
+		sensors_submit (feature->data->name, (const char *)chip_fullprefix,
value);
 	}
 }
 #else
@@ -198,6 +297,7 @@ static void sensors_read (void)
 void module_register (void)
 {
 	plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read,
sensors_write);
+	cf_register (MODULE_NAME, sensors_config, config_keys, config_keys_num);
 }

 #undef BUFSIZE





More information about the collectd mailing list