From: Henrique de Moraes Holschuh Date: Sat, 16 Oct 2010 01:21:57 +0000 (-0300) Subject: sensors plugin: Fix initialization code. X-Git-Tag: collectd-5.1.0~27^2~3 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=475906f5a5769b176aa7c1ad2ca06b58da95583c;p=collectd.git sensors plugin: Fix initialization code. Sensors often require scaling. Also, you often have to disable them, or name them. The libsensors config file provides all these facilities. Without it, the sensor plugin is mostly useless except for some values that are often already properly scaled by the kernel (some temperatures, some fan tachometers). In particular, it is utterly useless for voltage monitoring outside of the 3.3V rails. The plugin should either evaluate the libsensors config files at startup, or it should provide facilities to give proper names and first-order polinomial scaling+offset. Change-Id: I97312bee5ca33fefe846a8cdc292818d46819574 Signed-off-by: Florian Forster --- diff --git a/src/sensors.c b/src/sensors.c index 8391346b..96a54fca 100644 --- a/src/sensors.c +++ b/src/sensors.c @@ -27,6 +27,10 @@ * - honor sensors.conf's ignored * - config Sensor option * - config IgnoreSelected option + * + * Henrique de Moraes Holschuh + * - use default libsensors config file on API 0x400 + * - config SensorConfigFile option **/ #include "collectd.h" @@ -132,7 +136,8 @@ static int known_features_num = STATIC_ARRAY_SIZE (known_features); static const char *config_keys[] = { "Sensor", - "IgnoreSelected" + "IgnoreSelected", + "SensorConfigFile" }; static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); @@ -148,6 +153,7 @@ typedef struct featurelist # ifndef SENSORS_CONF_PATH # define SENSORS_CONF_PATH "/etc/sensors.conf" # endif +static const char *conffile = SENSORS_CONF_PATH; /* #endif SENSORS_API_VERSION < 0x400 */ #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) @@ -159,9 +165,7 @@ typedef struct featurelist struct featurelist *next; } featurelist_t; -# ifndef SENSORS_CONF_PATH -# define SENSORS_CONF_PATH "/etc/sensors3.conf" -# endif +static const char *conffile = NULL; /* #endif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */ #else /* if SENSORS_API_VERSION >= 0x500 */ @@ -169,10 +173,8 @@ typedef struct featurelist "as bug." #endif -static const char *conffile = SENSORS_CONF_PATH; featurelist_t *first_feature = NULL; static ignorelist_t *sensor_list; -static time_t sensors_config_mtime = 0; #if SENSORS_API_VERSION < 0x400 /* full chip name logic borrowed from lm_sensors */ @@ -225,7 +227,14 @@ static int sensors_config (const char *key, const char *value) if (sensor_list == NULL) sensor_list = ignorelist_create (1); - if (strcasecmp (key, "Sensor") == 0) + if (strcasecmp (key, "SensorConfigFile") == 0) + { + /* we will leak memory here if SensorConfigFile is + used more than once, maybe we can just drop support + for broken, extremely ancient libsensors? */ + conffile = strdup (value); + } + else if (strcasecmp (key, "Sensor") == 0) { if (ignorelist_add (sensor_list, value)) { @@ -268,47 +277,37 @@ void sensors_free_features (void) static int sensors_load_conf (void) { - FILE *fh; + static int call_once = 0; + + FILE *fh = NULL; featurelist_t *last_feature = NULL; const sensors_chip_name *chip; int chip_num; - struct stat statbuf; int status; - - status = stat (conffile, &statbuf); - if (status != 0) - { - char errbuf[1024]; - ERROR ("sensors plugin: stat (%s) failed: %s", conffile, - sstrerror (errno, errbuf, sizeof (errbuf))); - sensors_config_mtime = 0; - } - if ((sensors_config_mtime != 0) - && (sensors_config_mtime == statbuf.st_mtime)) - return (0); + if (call_once) + return 0; - if (sensors_config_mtime != 0) - { - NOTICE ("sensors plugin: Reloading config from %s", - conffile); - sensors_free_features (); - sensors_config_mtime = 0; - } + call_once = 1; - fh = fopen (conffile, "r"); - if (fh == NULL) + if (conffile) { - char errbuf[1024]; - ERROR ("sensors plugin: fopen(%s) failed: %s", conffile, - sstrerror (errno, errbuf, sizeof (errbuf))); - return (-1); + fh = fopen (conffile, "r"); + if (fh == NULL) + { + char errbuf[1024]; + ERROR ("sensors plugin: fopen(%s) failed: %s", conffile, + sstrerror (errno, errbuf, sizeof (errbuf))); + return (-1); + } } status = sensors_init (fh); - fclose (fh); + if (fh) + fclose (fh); + if (status != 0) { ERROR ("sensors plugin: Cannot initialize sensors. " @@ -316,8 +315,6 @@ static int sensors_load_conf (void) return (-1); } - sensors_config_mtime = statbuf.st_mtime; - #if SENSORS_API_VERSION < 0x400 chip_num = 0; while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)