X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fsensors.c;h=4f3d0d6032b33b6d3e13d11a3223bbf326efd33b;hb=5523a080010dcb7a61dc0dccc3969a2a048e52f9;hp=37d335c9da71f2e7f6fc71dbc491734214e07960;hpb=c1219a1c9db2e8400e2ee94b87f86ccd441485d5;p=collectd.git diff --git a/src/sensors.c b/src/sensors.c index 37d335c9..4f3d0d60 100644 --- a/src/sensors.c +++ b/src/sensors.c @@ -17,8 +17,8 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: - * Florian octo Forster - * + * Florian octo Forster + * * Lubos Stanek Wed Oct 27, 2006 * - config ExtendedSensorNaming option * - precise sensor feature selection (chip-bus-address/type-feature) @@ -34,9 +34,9 @@ **/ #include "collectd.h" + #include "common.h" #include "plugin.h" -#include "configfile.h" #include "utils_ignorelist.h" #if defined(HAVE_SENSORS_SENSORS_H) @@ -62,7 +62,9 @@ static char *sensor_type_name_map[] = "fanspeed", # define SENSOR_TYPE_TEMPERATURE 2 "temperature", -# define SENSOR_TYPE_UNKNOWN 3 +# define SENSOR_TYPE_POWER 3 + "power", +# define SENSOR_TYPE_UNKNOWN 4 NULL }; @@ -74,7 +76,7 @@ struct sensors_labeltypes_s typedef struct sensors_labeltypes_s sensors_labeltypes_t; /* finite list of known labels extracted from lm_sensors */ -static sensors_labeltypes_t known_features[] = +static sensors_labeltypes_t known_features[] = { { "fan1", SENSOR_TYPE_FANSPEED }, { "fan2", SENSOR_TYPE_FANSPEED }, @@ -127,7 +129,8 @@ static sensors_labeltypes_t known_features[] = { "3.3V", SENSOR_TYPE_VOLTAGE }, { "2.5V", SENSOR_TYPE_VOLTAGE }, { "2.0V", SENSOR_TYPE_VOLTAGE }, - { "12V", SENSOR_TYPE_VOLTAGE } + { "12V", SENSOR_TYPE_VOLTAGE }, + { "power1", SENSOR_TYPE_POWER } }; static int known_features_num = STATIC_ARRAY_SIZE (known_features); /* end new naming */ @@ -137,7 +140,8 @@ static const char *config_keys[] = { "Sensor", "IgnoreSelected", - "SensorConfigFile" + "SensorConfigFile", + "UseLabels" }; static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); @@ -166,6 +170,7 @@ typedef struct featurelist } featurelist_t; static char *conffile = NULL; +static _Bool use_labels = 0; /* #endif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */ #else /* if SENSORS_API_VERSION >= 0x500 */ @@ -173,7 +178,7 @@ static char *conffile = NULL; "as bug." #endif -featurelist_t *first_feature = NULL; +static featurelist_t *first_feature = NULL; static ignorelist_t *sensor_list; #if SENSORS_API_VERSION < 0x400 @@ -210,11 +215,9 @@ static int sensors_snprintf_chip_name (char *buf, size_t buf_size, static int sensors_feature_name_to_type (const char *name) { - int i; - /* Yes, this is slow, but it's only ever done during initialization, so * it's a one time cost.. */ - for (i = 0; i < known_features_num; i++) + for (int i = 0; i < known_features_num; i++) if (strcasecmp (known_features[i].label, name) == 0) return (known_features[i].type); @@ -254,6 +257,12 @@ static int sensors_config (const char *key, const char *value) if (IS_TRUE (value)) ignorelist_set_invert (sensor_list, 0); } +#if (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) + else if (strcasecmp (key, "UseLabels") == 0) + { + use_labels = IS_TRUE (value) ? 1 : 0; + } +#endif else { return (-1); @@ -264,7 +273,6 @@ static int sensors_config (const char *key, const char *value) static void sensors_free_features (void) { - featurelist_t *thisft; featurelist_t *nextft; if (first_feature == NULL) @@ -272,7 +280,7 @@ static void sensors_free_features (void) sensors_cleanup (); - for (thisft = first_feature; thisft != NULL; thisft = nextft) + for (featurelist_t *thisft = first_feature; thisft != NULL; thisft = nextft) { nextft = thisft->next; sfree (thisft); @@ -286,7 +294,7 @@ static int sensors_load_conf (void) FILE *fh = NULL; featurelist_t *last_feature = NULL; - + const sensors_chip_name *chip; int chip_num; @@ -375,13 +383,12 @@ static int sensors_load_conf (void) continue; } - fl = (featurelist_t *) malloc (sizeof (featurelist_t)); + fl = calloc (1, sizeof (*fl)); if (fl == NULL) { - ERROR ("sensors plugin: malloc failed."); + ERROR ("sensors plugin: calloc failed."); continue; } - memset (fl, '\0', sizeof (featurelist_t)); fl->chip = chip; fl->data = feature; @@ -411,7 +418,8 @@ static int sensors_load_conf (void) /* Only handle voltage, fanspeeds and temperatures */ if ((feature->type != SENSORS_FEATURE_IN) && (feature->type != SENSORS_FEATURE_FAN) - && (feature->type != SENSORS_FEATURE_TEMP)) + && (feature->type != SENSORS_FEATURE_TEMP) + && (feature->type != SENSORS_FEATURE_POWER)) { DEBUG ("sensors plugin: sensors_load_conf: " "Ignoring feature `%s', " @@ -427,16 +435,16 @@ static int sensors_load_conf (void) if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT) && (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT) - && (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT)) + && (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT) + && (subfeature->type != SENSORS_SUBFEATURE_POWER_INPUT)) continue; - fl = (featurelist_t *) malloc (sizeof (featurelist_t)); + fl = calloc (1, sizeof (*fl)); if (fl == NULL) { - ERROR ("sensors plugin: malloc failed."); + ERROR ("sensors plugin: calloc failed."); continue; } - memset (fl, '\0', sizeof (featurelist_t)); fl->chip = chip; fl->feature = feature; @@ -510,13 +518,11 @@ static void sensors_submit (const char *plugin_instance, static int sensors_read (void) { - featurelist_t *fl; - if (sensors_load_conf () != 0) return (-1); #if SENSORS_API_VERSION < 0x400 - for (fl = first_feature; fl != NULL; fl = fl->next) + for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) { double value; int status; @@ -544,12 +550,13 @@ static int sensors_read (void) /* #endif SENSORS_API_VERSION < 0x400 */ #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) - for (fl = first_feature; fl != NULL; fl = fl->next) + for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) { double value; int status; char plugin_instance[DATA_MAX_NAME_LEN]; char type_instance[DATA_MAX_NAME_LEN]; + char *sensor_label; const char *type; status = sensors_get_value (fl->chip, @@ -562,8 +569,17 @@ static int sensors_read (void) if (status < 0) continue; - sstrncpy (type_instance, fl->feature->name, - sizeof (type_instance)); + if (use_labels) + { + sensor_label = sensors_get_label (fl->chip, fl->feature); + sstrncpy (type_instance, sensor_label, sizeof (type_instance)); + free (sensor_label); + } + else + { + sstrncpy (type_instance, fl->feature->name, + sizeof (type_instance)); + } if (fl->feature->type == SENSORS_FEATURE_IN) type = "voltage"; @@ -573,6 +589,9 @@ static int sensors_read (void) else if (fl->feature->type == SENSORS_FEATURE_TEMP) type = "temperature"; + else if (fl->feature->type + == SENSORS_FEATURE_POWER) + type = "power"; else continue;