2 * collectd - src/sensors.c
3 * Copyright (C) 2005-2008 Florian octo Forster
4 * Copyright (C) 2006 Luboš Staněk
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at collectd.org>
22 * Lubos Stanek <lubek at users.sourceforge.net> Wed Oct 27, 2006
23 * - config ExtendedSensorNaming option
24 * - precise sensor feature selection (chip-bus-address/type-feature)
25 * with ExtendedSensorNaming
26 * - more sensor features (finite list)
27 * - honor sensors.conf's ignored
28 * - config Sensor option
29 * - config IgnoreSelected option
31 * Henrique de Moraes Holschuh <hmh at debian.org>
32 * - use default libsensors config file on API 0x400
33 * - config SensorConfigFile option
40 #include "utils_ignorelist.h"
42 #if defined(HAVE_SENSORS_SENSORS_H)
43 #include <sensors/sensors.h>
46 #if !defined(SENSORS_API_VERSION)
47 #define SENSORS_API_VERSION 0x000
51 * The sensors library prior to version 3.0 (internal version 0x400) didn't
52 * report the type of values, only a name. The following lists are there to
53 * convert from the names to the type. They are not used with the new
56 #if SENSORS_API_VERSION < 0x400
57 static char *sensor_type_name_map[] = {
58 #define SENSOR_TYPE_VOLTAGE 0
60 #define SENSOR_TYPE_FANSPEED 1
62 #define SENSOR_TYPE_TEMPERATURE 2
64 #define SENSOR_TYPE_POWER 3
66 #define SENSOR_TYPE_UNKNOWN 4
69 struct sensors_labeltypes_s {
73 typedef struct sensors_labeltypes_s sensors_labeltypes_t;
75 /* finite list of known labels extracted from lm_sensors */
76 static sensors_labeltypes_t known_features[] = {
77 {"fan1", SENSOR_TYPE_FANSPEED},
78 {"fan2", SENSOR_TYPE_FANSPEED},
79 {"fan3", SENSOR_TYPE_FANSPEED},
80 {"fan4", SENSOR_TYPE_FANSPEED},
81 {"fan5", SENSOR_TYPE_FANSPEED},
82 {"fan6", SENSOR_TYPE_FANSPEED},
83 {"fan7", SENSOR_TYPE_FANSPEED},
84 {"AIN2", SENSOR_TYPE_VOLTAGE},
85 {"AIN1", SENSOR_TYPE_VOLTAGE},
86 {"in10", SENSOR_TYPE_VOLTAGE},
87 {"in9", SENSOR_TYPE_VOLTAGE},
88 {"in8", SENSOR_TYPE_VOLTAGE},
89 {"in7", SENSOR_TYPE_VOLTAGE},
90 {"in6", SENSOR_TYPE_VOLTAGE},
91 {"in5", SENSOR_TYPE_VOLTAGE},
92 {"in4", SENSOR_TYPE_VOLTAGE},
93 {"in3", SENSOR_TYPE_VOLTAGE},
94 {"in2", SENSOR_TYPE_VOLTAGE},
95 {"in0", SENSOR_TYPE_VOLTAGE},
96 {"CPU_Temp", SENSOR_TYPE_TEMPERATURE},
97 {"remote_temp", SENSOR_TYPE_TEMPERATURE},
98 {"temp1", SENSOR_TYPE_TEMPERATURE},
99 {"temp2", SENSOR_TYPE_TEMPERATURE},
100 {"temp3", SENSOR_TYPE_TEMPERATURE},
101 {"temp4", SENSOR_TYPE_TEMPERATURE},
102 {"temp5", SENSOR_TYPE_TEMPERATURE},
103 {"temp6", SENSOR_TYPE_TEMPERATURE},
104 {"temp7", SENSOR_TYPE_TEMPERATURE},
105 {"temp", SENSOR_TYPE_TEMPERATURE},
106 {"Vccp2", SENSOR_TYPE_VOLTAGE},
107 {"Vccp1", SENSOR_TYPE_VOLTAGE},
108 {"vdd", SENSOR_TYPE_VOLTAGE},
109 {"vid5", SENSOR_TYPE_VOLTAGE},
110 {"vid4", SENSOR_TYPE_VOLTAGE},
111 {"vid3", SENSOR_TYPE_VOLTAGE},
112 {"vid2", SENSOR_TYPE_VOLTAGE},
113 {"vid1", SENSOR_TYPE_VOLTAGE},
114 {"vid", SENSOR_TYPE_VOLTAGE},
115 {"vin4", SENSOR_TYPE_VOLTAGE},
116 {"vin3", SENSOR_TYPE_VOLTAGE},
117 {"vin2", SENSOR_TYPE_VOLTAGE},
118 {"vin1", SENSOR_TYPE_VOLTAGE},
119 {"voltbatt", SENSOR_TYPE_VOLTAGE},
120 {"volt12", SENSOR_TYPE_VOLTAGE},
121 {"volt5", SENSOR_TYPE_VOLTAGE},
122 {"vrm", SENSOR_TYPE_VOLTAGE},
123 {"5.0V", SENSOR_TYPE_VOLTAGE},
124 {"5V", SENSOR_TYPE_VOLTAGE},
125 {"3.3V", SENSOR_TYPE_VOLTAGE},
126 {"2.5V", SENSOR_TYPE_VOLTAGE},
127 {"2.0V", SENSOR_TYPE_VOLTAGE},
128 {"12V", SENSOR_TYPE_VOLTAGE},
129 {"power1", SENSOR_TYPE_POWER}};
130 static int known_features_num = STATIC_ARRAY_SIZE(known_features);
132 #endif /* SENSORS_API_VERSION < 0x400 */
134 static const char *config_keys[] = {"Sensor", "IgnoreSelected",
135 "SensorConfigFile", "UseLabels"};
136 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
138 #if SENSORS_API_VERSION < 0x400
139 typedef struct featurelist {
140 const sensors_chip_name *chip;
141 const sensors_feature_data *data;
143 struct featurelist *next;
146 #ifndef SENSORS_CONF_PATH
147 #define SENSORS_CONF_PATH "/etc/sensors.conf"
149 static char *conffile = SENSORS_CONF_PATH;
150 /* #endif SENSORS_API_VERSION < 0x400 */
152 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
153 typedef struct featurelist {
154 const sensors_chip_name *chip;
155 const sensors_feature *feature;
156 const sensors_subfeature *subfeature;
157 struct featurelist *next;
160 static char *conffile;
161 static bool use_labels;
162 /* #endif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
164 #else /* if SENSORS_API_VERSION >= 0x500 */
165 #error "This version of libsensors is not supported yet. Please report this " \
169 static featurelist_t *first_feature;
170 static ignorelist_t *sensor_list;
172 #if SENSORS_API_VERSION < 0x400
173 /* full chip name logic borrowed from lm_sensors */
174 static int sensors_snprintf_chip_name(char *buf, size_t buf_size,
175 const sensors_chip_name *chip) {
178 if (chip->bus == SENSORS_CHIP_NAME_BUS_ISA) {
179 status = snprintf(buf, buf_size, "%s-isa-%04x", chip->prefix, chip->addr);
180 } else if (chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY) {
181 status = snprintf(buf, buf_size, "%s-%s-%04x", chip->prefix, chip->busname,
184 status = snprintf(buf, buf_size, "%s-i2c-%d-%02x", chip->prefix, chip->bus,
189 } /* int sensors_snprintf_chip_name */
191 static int sensors_feature_name_to_type(const char *name) {
192 /* Yes, this is slow, but it's only ever done during initialization, so
193 * it's a one time cost.. */
194 for (int i = 0; i < known_features_num; i++)
195 if (strcasecmp(known_features[i].label, name) == 0)
196 return known_features[i].type;
198 return SENSOR_TYPE_UNKNOWN;
199 } /* int sensors_feature_name_to_type */
202 static int sensors_config(const char *key, const char *value) {
203 if (sensor_list == NULL)
204 sensor_list = ignorelist_create(1);
206 /* TODO: This setting exists for compatibility with old versions of
207 * lm-sensors. Remove support for those ancient versions in the next
209 if (strcasecmp(key, "SensorConfigFile") == 0) {
210 char *tmp = strdup(value);
215 } else if (strcasecmp(key, "Sensor") == 0) {
216 if (ignorelist_add(sensor_list, value)) {
217 ERROR("sensors plugin: "
218 "Cannot add value to ignorelist.");
221 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
222 ignorelist_set_invert(sensor_list, 1);
224 ignorelist_set_invert(sensor_list, 0);
226 #if (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
227 else if (strcasecmp(key, "UseLabels") == 0) {
228 use_labels = IS_TRUE(value);
238 static void sensors_free_features(void) {
239 featurelist_t *nextft;
241 if (first_feature == NULL)
246 for (featurelist_t *thisft = first_feature; thisft != NULL; thisft = nextft) {
247 nextft = thisft->next;
250 first_feature = NULL;
253 static int sensors_load_conf(void) {
254 static int call_once;
257 featurelist_t *last_feature = NULL;
259 const sensors_chip_name *chip;
269 if (conffile != NULL) {
270 fh = fopen(conffile, "r");
272 ERROR("sensors plugin: fopen(%s) failed: %s", conffile, STRERRNO);
277 status = sensors_init(fh);
282 ERROR("sensors plugin: Cannot initialize sensors. "
283 "Data will not be collected.");
287 #if SENSORS_API_VERSION < 0x400
289 while ((chip = sensors_get_detected_chips(&chip_num)) != NULL) {
290 int feature_num0 = 0;
291 int feature_num1 = 0;
294 const sensors_feature_data *feature;
298 feature = sensors_get_all_features(*chip, &feature_num0, &feature_num1);
300 /* Check if all features have been read. */
304 /* "master features" only */
305 if (feature->mapping != SENSORS_NO_MAPPING) {
306 DEBUG("sensors plugin: sensors_load_conf: "
307 "Ignoring subfeature `%s', "
308 "because (feature->mapping "
309 "!= SENSORS_NO_MAPPING).",
314 /* skip ignored in sensors.conf */
315 if (sensors_get_ignored(*chip, feature->number) == 0) {
316 DEBUG("sensors plugin: sensors_load_conf: "
317 "Ignoring subfeature `%s', "
319 "`sensors_get_ignored' told "
325 feature_type = sensors_feature_name_to_type(feature->name);
326 if (feature_type == SENSOR_TYPE_UNKNOWN) {
327 DEBUG("sensors plugin: sensors_load_conf: "
328 "Ignoring subfeature `%s', "
329 "because its type is "
335 fl = calloc(1, sizeof(*fl));
337 ERROR("sensors plugin: calloc failed.");
343 fl->type = feature_type;
345 if (first_feature == NULL)
348 last_feature->next = fl;
350 } /* while sensors_get_all_features */
351 } /* while sensors_get_detected_chips */
352 /* #endif SENSORS_API_VERSION < 0x400 */
354 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
356 while ((chip = sensors_get_detected_chips(NULL, &chip_num)) != NULL) {
357 const sensors_feature *feature;
360 while ((feature = sensors_get_features(chip, &feature_num)) != NULL) {
361 const sensors_subfeature *subfeature;
362 int subfeature_num = 0;
364 /* Only handle voltage, fanspeeds and temperatures */
365 if ((feature->type != SENSORS_FEATURE_IN) &&
366 (feature->type != SENSORS_FEATURE_FAN) &&
367 (feature->type != SENSORS_FEATURE_TEMP) &&
368 #if SENSORS_API_VERSION >= 0x402
369 (feature->type != SENSORS_FEATURE_CURR) &&
371 #if SENSORS_API_VERSION >= 0x431
372 (feature->type != SENSORS_FEATURE_HUMIDITY) &&
374 (feature->type != SENSORS_FEATURE_POWER)) {
375 DEBUG("sensors plugin: sensors_load_conf: "
376 "Ignoring feature `%s', "
377 "because its type is not "
383 while ((subfeature = sensors_get_all_subfeatures(
384 chip, feature, &subfeature_num)) != NULL) {
387 if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT) &&
388 (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT) &&
389 (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT) &&
390 #if SENSORS_API_VERSION >= 0x402
391 (subfeature->type != SENSORS_SUBFEATURE_CURR_INPUT) &&
393 #if SENSORS_API_VERSION >= 0x431
394 (subfeature->type != SENSORS_SUBFEATURE_HUMIDITY_INPUT) &&
396 (subfeature->type != SENSORS_SUBFEATURE_POWER_INPUT))
399 fl = calloc(1, sizeof(*fl));
401 ERROR("sensors plugin: calloc failed.");
406 fl->feature = feature;
407 fl->subfeature = subfeature;
409 if (first_feature == NULL)
412 last_feature->next = fl;
414 } /* while (subfeature) */
415 } /* while (feature) */
417 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
419 if (first_feature == NULL) {
421 INFO("sensors plugin: lm_sensors reports no "
422 "features. Data will not be collected.");
427 } /* int sensors_load_conf */
429 static int sensors_shutdown(void) {
430 sensors_free_features();
431 ignorelist_free(sensor_list);
434 } /* int sensors_shutdown */
436 static void sensors_submit(const char *plugin_instance, const char *type,
437 const char *type_instance, double value) {
438 char match_key[1024];
441 value_list_t vl = VALUE_LIST_INIT;
443 status = snprintf(match_key, sizeof(match_key), "%s/%s-%s", plugin_instance,
444 type, type_instance);
448 if (sensor_list != NULL) {
449 DEBUG("sensors plugin: Checking ignorelist for `%s'", match_key);
450 if (ignorelist_match(sensor_list, match_key))
454 vl.values = &(value_t){.gauge = value};
457 sstrncpy(vl.plugin, "sensors", sizeof(vl.plugin));
458 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
459 sstrncpy(vl.type, type, sizeof(vl.type));
460 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
462 plugin_dispatch_values(&vl);
463 } /* void sensors_submit */
465 static int sensors_read(void) {
466 if (sensors_load_conf() != 0)
469 #if SENSORS_API_VERSION < 0x400
470 for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) {
473 char plugin_instance[DATA_MAX_NAME_LEN];
474 char type_instance[DATA_MAX_NAME_LEN];
476 status = sensors_get_feature(*fl->chip, fl->data->number, &value);
480 status = sensors_snprintf_chip_name(plugin_instance,
481 sizeof(plugin_instance), fl->chip);
485 sstrncpy(type_instance, fl->data->name, sizeof(type_instance));
487 sensors_submit(plugin_instance, sensor_type_name_map[fl->type],
488 type_instance, value);
489 } /* for fl = first_feature .. NULL */
490 /* #endif SENSORS_API_VERSION < 0x400 */
492 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
493 for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) {
496 char plugin_instance[DATA_MAX_NAME_LEN];
497 char type_instance[DATA_MAX_NAME_LEN];
501 status = sensors_get_value(fl->chip, fl->subfeature->number, &value);
505 status = sensors_snprintf_chip_name(plugin_instance,
506 sizeof(plugin_instance), fl->chip);
511 sensor_label = sensors_get_label(fl->chip, fl->feature);
512 sstrncpy(type_instance, sensor_label, sizeof(type_instance));
515 sstrncpy(type_instance, fl->feature->name, sizeof(type_instance));
518 if (fl->feature->type == SENSORS_FEATURE_IN)
520 else if (fl->feature->type == SENSORS_FEATURE_FAN)
522 else if (fl->feature->type == SENSORS_FEATURE_TEMP)
523 type = "temperature";
524 else if (fl->feature->type == SENSORS_FEATURE_POWER)
526 #if SENSORS_API_VERSION >= 0x402
527 else if (fl->feature->type == SENSORS_FEATURE_CURR)
530 #if SENSORS_API_VERSION >= 0x431
531 else if (fl->feature->type == SENSORS_FEATURE_HUMIDITY)
537 sensors_submit(plugin_instance, type, type_instance, value);
538 } /* for fl = first_feature .. NULL */
539 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
542 } /* int sensors_read */
544 void module_register(void) {
545 plugin_register_config("sensors", sensors_config, config_keys,
547 plugin_register_read("sensors", sensors_read);
548 plugin_register_shutdown("sensors", sensors_shutdown);
549 } /* void module_register */