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
39 #include "utils/common/common.h"
40 #include "utils/ignorelist/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
50 static const char *config_keys[] = {"Sensor", "IgnoreSelected",
51 "SensorConfigFile", "UseLabels"};
52 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
54 #if SENSORS_API_VERSION < 0x400
55 typedef struct featurelist {
56 const sensors_chip_name *chip;
57 const sensors_feature_data *data;
59 struct featurelist *next;
62 #ifndef SENSORS_CONF_PATH
63 #define SENSORS_CONF_PATH "/etc/sensors.conf"
65 static char *conffile = SENSORS_CONF_PATH;
66 /* #endif SENSORS_API_VERSION < 0x400 */
68 #elif (SENSORS_API_VERSION >= 0x400)
69 typedef struct featurelist {
70 const sensors_chip_name *chip;
71 const sensors_feature *feature;
72 const sensors_subfeature *subfeature;
73 struct featurelist *next;
76 static char *conffile;
77 static bool use_labels;
80 static featurelist_t *first_feature;
81 static ignorelist_t *sensor_list;
83 static int sensors_config(const char *key, const char *value) {
84 if (sensor_list == NULL)
85 sensor_list = ignorelist_create(1);
87 /* TODO: This setting exists for compatibility with old versions of
88 * lm-sensors. Remove support for those ancient versions in the next
90 if (strcasecmp(key, "SensorConfigFile") == 0) {
91 char *tmp = strdup(value);
96 } else if (strcasecmp(key, "Sensor") == 0) {
97 if (ignorelist_add(sensor_list, value)) {
98 ERROR("sensors plugin: "
99 "Cannot add value to ignorelist.");
102 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
103 ignorelist_set_invert(sensor_list, 1);
105 ignorelist_set_invert(sensor_list, 0);
107 #if (SENSORS_API_VERSION >= 0x400)
108 else if (strcasecmp(key, "UseLabels") == 0) {
109 use_labels = IS_TRUE(value);
119 static void sensors_free_features(void) {
120 featurelist_t *nextft;
122 if (first_feature == NULL)
127 for (featurelist_t *thisft = first_feature; thisft != NULL; thisft = nextft) {
128 nextft = thisft->next;
131 first_feature = NULL;
134 static int sensors_load_conf(void) {
135 static int call_once;
138 featurelist_t *last_feature = NULL;
140 const sensors_chip_name *chip;
150 if (conffile != NULL) {
151 fh = fopen(conffile, "r");
153 ERROR("sensors plugin: fopen(%s) failed: %s", conffile, STRERRNO);
158 status = sensors_init(fh);
163 ERROR("sensors plugin: Cannot initialize sensors. "
164 "Data will not be collected.");
168 #if SENSORS_API_VERSION < 0x400
170 while ((chip = sensors_get_detected_chips(&chip_num)) != NULL) {
171 int feature_num0 = 0;
172 int feature_num1 = 0;
175 const sensors_feature_data *feature;
179 feature = sensors_get_all_features(*chip, &feature_num0, &feature_num1);
181 /* Check if all features have been read. */
185 /* "master features" only */
186 if (feature->mapping != SENSORS_NO_MAPPING) {
187 DEBUG("sensors plugin: sensors_load_conf: "
188 "Ignoring subfeature `%s', "
189 "because (feature->mapping "
190 "!= SENSORS_NO_MAPPING).",
195 /* skip ignored in sensors.conf */
196 if (sensors_get_ignored(*chip, feature->number) == 0) {
197 DEBUG("sensors plugin: sensors_load_conf: "
198 "Ignoring subfeature `%s', "
200 "`sensors_get_ignored' told "
206 feature_type = sensors_feature_name_to_type(feature->name);
207 if (feature_type == SENSOR_TYPE_UNKNOWN) {
208 DEBUG("sensors plugin: sensors_load_conf: "
209 "Ignoring subfeature `%s', "
210 "because its type is "
216 fl = calloc(1, sizeof(*fl));
218 ERROR("sensors plugin: calloc failed.");
224 fl->type = feature_type;
226 if (first_feature == NULL)
229 last_feature->next = fl;
231 } /* while sensors_get_all_features */
232 } /* while sensors_get_detected_chips */
233 /* #endif SENSORS_API_VERSION < 0x400 */
235 #elif (SENSORS_API_VERSION >= 0x400)
237 while ((chip = sensors_get_detected_chips(NULL, &chip_num)) != NULL) {
238 const sensors_feature *feature;
241 while ((feature = sensors_get_features(chip, &feature_num)) != NULL) {
242 const sensors_subfeature *subfeature;
243 int subfeature_num = 0;
245 /* Only handle voltage, fanspeeds and temperatures */
246 if ((feature->type != SENSORS_FEATURE_IN) &&
247 (feature->type != SENSORS_FEATURE_FAN) &&
248 (feature->type != SENSORS_FEATURE_TEMP) &&
249 #if SENSORS_API_VERSION >= 0x402
250 (feature->type != SENSORS_FEATURE_CURR) &&
252 #if SENSORS_API_VERSION >= 0x431
253 (feature->type != SENSORS_FEATURE_HUMIDITY) &&
255 (feature->type != SENSORS_FEATURE_POWER)) {
256 DEBUG("sensors plugin: sensors_load_conf: "
257 "Ignoring feature `%s', "
258 "because its type is not "
264 while ((subfeature = sensors_get_all_subfeatures(
265 chip, feature, &subfeature_num)) != NULL) {
268 if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT) &&
269 (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT) &&
270 (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT) &&
271 #if SENSORS_API_VERSION >= 0x402
272 (subfeature->type != SENSORS_SUBFEATURE_CURR_INPUT) &&
274 #if SENSORS_API_VERSION >= 0x431
275 (subfeature->type != SENSORS_SUBFEATURE_HUMIDITY_INPUT) &&
277 (subfeature->type != SENSORS_SUBFEATURE_POWER_INPUT))
280 fl = calloc(1, sizeof(*fl));
282 ERROR("sensors plugin: calloc failed.");
287 fl->feature = feature;
288 fl->subfeature = subfeature;
290 if (first_feature == NULL)
293 last_feature->next = fl;
295 } /* while (subfeature) */
296 } /* while (feature) */
298 #endif /* (SENSORS_API_VERSION >= 0x400) */
300 if (first_feature == NULL) {
302 INFO("sensors plugin: lm_sensors reports no "
303 "features. Data will not be collected.");
308 } /* int sensors_load_conf */
310 static int sensors_shutdown(void) {
311 sensors_free_features();
312 ignorelist_free(sensor_list);
315 } /* int sensors_shutdown */
317 static void sensors_submit(const char *plugin_instance, const char *type,
318 const char *type_instance, double value) {
319 char match_key[1024];
322 value_list_t vl = VALUE_LIST_INIT;
324 status = snprintf(match_key, sizeof(match_key), "%s/%s-%s", plugin_instance,
325 type, type_instance);
329 if (sensor_list != NULL) {
330 DEBUG("sensors plugin: Checking ignorelist for `%s'", match_key);
331 if (ignorelist_match(sensor_list, match_key))
335 vl.values = &(value_t){.gauge = value};
338 sstrncpy(vl.plugin, "sensors", sizeof(vl.plugin));
339 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
340 sstrncpy(vl.type, type, sizeof(vl.type));
341 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
343 plugin_dispatch_values(&vl);
344 } /* void sensors_submit */
346 static int sensors_read(void) {
347 if (sensors_load_conf() != 0)
350 #if SENSORS_API_VERSION < 0x400
351 for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) {
354 char plugin_instance[DATA_MAX_NAME_LEN];
355 char type_instance[DATA_MAX_NAME_LEN];
357 status = sensors_get_feature(*fl->chip, fl->data->number, &value);
361 status = sensors_snprintf_chip_name(plugin_instance,
362 sizeof(plugin_instance), fl->chip);
366 sstrncpy(type_instance, fl->data->name, sizeof(type_instance));
368 sensors_submit(plugin_instance, sensor_type_name_map[fl->type],
369 type_instance, value);
370 } /* for fl = first_feature .. NULL */
371 /* #endif SENSORS_API_VERSION < 0x400 */
373 #elif (SENSORS_API_VERSION >= 0x400)
374 for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) {
377 char plugin_instance[DATA_MAX_NAME_LEN];
378 char type_instance[DATA_MAX_NAME_LEN];
382 status = sensors_get_value(fl->chip, fl->subfeature->number, &value);
386 status = sensors_snprintf_chip_name(plugin_instance,
387 sizeof(plugin_instance), fl->chip);
392 sensor_label = sensors_get_label(fl->chip, fl->feature);
393 sstrncpy(type_instance, sensor_label, sizeof(type_instance));
396 sstrncpy(type_instance, fl->feature->name, sizeof(type_instance));
399 if (fl->feature->type == SENSORS_FEATURE_IN)
401 else if (fl->feature->type == SENSORS_FEATURE_FAN)
403 else if (fl->feature->type == SENSORS_FEATURE_TEMP)
404 type = "temperature";
405 else if (fl->feature->type == SENSORS_FEATURE_POWER)
407 #if SENSORS_API_VERSION >= 0x402
408 else if (fl->feature->type == SENSORS_FEATURE_CURR)
411 #if SENSORS_API_VERSION >= 0x431
412 else if (fl->feature->type == SENSORS_FEATURE_HUMIDITY)
418 sensors_submit(plugin_instance, type, type_instance, value);
419 } /* for fl = first_feature .. NULL */
420 #endif /* (SENSORS_API_VERSION >= 0x400) */
423 } /* int sensors_read */
425 void module_register(void) {
426 plugin_register_config("sensors", sensors_config, config_keys,
428 plugin_register_read("sensors", sensors_read);
429 plugin_register_shutdown("sensors", sensors_shutdown);
430 } /* void module_register */