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
50 static const char *config_keys[] = {"Sensor", "IgnoreSelected",
51 "SensorConfigFile", "UseLabels"};
52 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
54 typedef struct featurelist {
55 const sensors_chip_name *chip;
56 const sensors_feature *feature;
57 const sensors_subfeature *subfeature;
58 struct featurelist *next;
61 static char *conffile;
62 static bool use_labels;
64 static featurelist_t *first_feature;
65 static ignorelist_t *sensor_list;
67 static int sensors_config(const char *key, const char *value) {
68 if (sensor_list == NULL)
69 sensor_list = ignorelist_create(1);
71 /* TODO: This setting exists for compatibility with old versions of
72 * lm-sensors. Remove support for those ancient versions in the next
74 if (strcasecmp(key, "SensorConfigFile") == 0) {
75 char *tmp = strdup(value);
80 } else if (strcasecmp(key, "Sensor") == 0) {
81 if (ignorelist_add(sensor_list, value)) {
82 ERROR("sensors plugin: "
83 "Cannot add value to ignorelist.");
86 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
87 ignorelist_set_invert(sensor_list, 1);
89 ignorelist_set_invert(sensor_list, 0);
90 } else if (strcasecmp(key, "UseLabels") == 0) {
91 use_labels = IS_TRUE(value);
99 static void sensors_free_features(void) {
100 featurelist_t *nextft;
102 if (first_feature == NULL)
107 for (featurelist_t *thisft = first_feature; thisft != NULL; thisft = nextft) {
108 nextft = thisft->next;
111 first_feature = NULL;
114 static int sensors_load_conf(void) {
115 static int call_once;
118 featurelist_t *last_feature = NULL;
120 const sensors_chip_name *chip;
130 if (conffile != NULL) {
131 fh = fopen(conffile, "r");
133 ERROR("sensors plugin: fopen(%s) failed: %s", conffile, STRERRNO);
138 status = sensors_init(fh);
143 ERROR("sensors plugin: Cannot initialize sensors. "
144 "Data will not be collected.");
149 while ((chip = sensors_get_detected_chips(NULL, &chip_num)) != NULL) {
150 const sensors_feature *feature;
153 while ((feature = sensors_get_features(chip, &feature_num)) != NULL) {
154 const sensors_subfeature *subfeature;
155 int subfeature_num = 0;
157 /* Only handle voltage, fanspeeds and temperatures */
158 if ((feature->type != SENSORS_FEATURE_IN) &&
159 (feature->type != SENSORS_FEATURE_FAN) &&
160 (feature->type != SENSORS_FEATURE_TEMP) &&
161 #if SENSORS_API_VERSION >= 0x402
162 (feature->type != SENSORS_FEATURE_CURR) &&
164 #if SENSORS_API_VERSION >= 0x431
165 (feature->type != SENSORS_FEATURE_HUMIDITY) &&
167 (feature->type != SENSORS_FEATURE_POWER)) {
168 DEBUG("sensors plugin: sensors_load_conf: "
169 "Ignoring feature `%s', "
170 "because its type is not "
176 while ((subfeature = sensors_get_all_subfeatures(
177 chip, feature, &subfeature_num)) != NULL) {
180 if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT) &&
181 (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT) &&
182 (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT) &&
183 #if SENSORS_API_VERSION >= 0x402
184 (subfeature->type != SENSORS_SUBFEATURE_CURR_INPUT) &&
186 #if SENSORS_API_VERSION >= 0x431
187 (subfeature->type != SENSORS_SUBFEATURE_HUMIDITY_INPUT) &&
189 (subfeature->type != SENSORS_SUBFEATURE_POWER_INPUT))
192 fl = calloc(1, sizeof(*fl));
194 ERROR("sensors plugin: calloc failed.");
199 fl->feature = feature;
200 fl->subfeature = subfeature;
202 if (first_feature == NULL)
205 last_feature->next = fl;
207 } /* while (subfeature) */
208 } /* while (feature) */
211 if (first_feature == NULL) {
213 INFO("sensors plugin: lm_sensors reports no "
214 "features. Data will not be collected.");
219 } /* int sensors_load_conf */
221 static int sensors_shutdown(void) {
222 sensors_free_features();
223 ignorelist_free(sensor_list);
226 } /* int sensors_shutdown */
228 static void sensors_submit(const char *plugin_instance, const char *type,
229 const char *type_instance, double value) {
230 char match_key[1024];
233 value_list_t vl = VALUE_LIST_INIT;
235 status = snprintf(match_key, sizeof(match_key), "%s/%s-%s", plugin_instance,
236 type, type_instance);
240 if (sensor_list != NULL) {
241 DEBUG("sensors plugin: Checking ignorelist for `%s'", match_key);
242 if (ignorelist_match(sensor_list, match_key))
246 vl.values = &(value_t){.gauge = value};
249 sstrncpy(vl.plugin, "sensors", sizeof(vl.plugin));
250 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
251 sstrncpy(vl.type, type, sizeof(vl.type));
252 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
254 plugin_dispatch_values(&vl);
255 } /* void sensors_submit */
257 static int sensors_read(void) {
258 if (sensors_load_conf() != 0)
261 for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) {
264 char plugin_instance[DATA_MAX_NAME_LEN];
265 char type_instance[DATA_MAX_NAME_LEN];
269 status = sensors_get_value(fl->chip, fl->subfeature->number, &value);
273 status = sensors_snprintf_chip_name(plugin_instance,
274 sizeof(plugin_instance), fl->chip);
279 sensor_label = sensors_get_label(fl->chip, fl->feature);
280 sstrncpy(type_instance, sensor_label, sizeof(type_instance));
283 sstrncpy(type_instance, fl->feature->name, sizeof(type_instance));
286 if (fl->feature->type == SENSORS_FEATURE_IN)
288 else if (fl->feature->type == SENSORS_FEATURE_FAN)
290 else if (fl->feature->type == SENSORS_FEATURE_TEMP)
291 type = "temperature";
292 else if (fl->feature->type == SENSORS_FEATURE_POWER)
294 #if SENSORS_API_VERSION >= 0x402
295 else if (fl->feature->type == SENSORS_FEATURE_CURR)
298 #if SENSORS_API_VERSION >= 0x431
299 else if (fl->feature->type == SENSORS_FEATURE_HUMIDITY)
305 sensors_submit(plugin_instance, type, type_instance, value);
306 } /* for fl = first_feature .. NULL */
309 } /* int sensors_read */
311 void module_register(void) {
312 plugin_register_config("sensors", sensors_config, config_keys,
314 plugin_register_read("sensors", sensors_read);
315 plugin_register_shutdown("sensors", sensors_shutdown);
316 } /* void module_register */