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[] =
59 # define SENSOR_TYPE_VOLTAGE 0
61 # define SENSOR_TYPE_FANSPEED 1
63 # define SENSOR_TYPE_TEMPERATURE 2
65 # define SENSOR_TYPE_POWER 3
67 # define SENSOR_TYPE_UNKNOWN 4
71 struct sensors_labeltypes_s
76 typedef struct sensors_labeltypes_s sensors_labeltypes_t;
78 /* finite list of known labels extracted from lm_sensors */
79 static sensors_labeltypes_t known_features[] =
81 { "fan1", SENSOR_TYPE_FANSPEED },
82 { "fan2", SENSOR_TYPE_FANSPEED },
83 { "fan3", SENSOR_TYPE_FANSPEED },
84 { "fan4", SENSOR_TYPE_FANSPEED },
85 { "fan5", SENSOR_TYPE_FANSPEED },
86 { "fan6", SENSOR_TYPE_FANSPEED },
87 { "fan7", SENSOR_TYPE_FANSPEED },
88 { "AIN2", SENSOR_TYPE_VOLTAGE },
89 { "AIN1", SENSOR_TYPE_VOLTAGE },
90 { "in10", SENSOR_TYPE_VOLTAGE },
91 { "in9", SENSOR_TYPE_VOLTAGE },
92 { "in8", SENSOR_TYPE_VOLTAGE },
93 { "in7", SENSOR_TYPE_VOLTAGE },
94 { "in6", SENSOR_TYPE_VOLTAGE },
95 { "in5", SENSOR_TYPE_VOLTAGE },
96 { "in4", SENSOR_TYPE_VOLTAGE },
97 { "in3", SENSOR_TYPE_VOLTAGE },
98 { "in2", SENSOR_TYPE_VOLTAGE },
99 { "in0", SENSOR_TYPE_VOLTAGE },
100 { "CPU_Temp", SENSOR_TYPE_TEMPERATURE },
101 { "remote_temp", SENSOR_TYPE_TEMPERATURE },
102 { "temp1", SENSOR_TYPE_TEMPERATURE },
103 { "temp2", SENSOR_TYPE_TEMPERATURE },
104 { "temp3", SENSOR_TYPE_TEMPERATURE },
105 { "temp4", SENSOR_TYPE_TEMPERATURE },
106 { "temp5", SENSOR_TYPE_TEMPERATURE },
107 { "temp6", SENSOR_TYPE_TEMPERATURE },
108 { "temp7", SENSOR_TYPE_TEMPERATURE },
109 { "temp", SENSOR_TYPE_TEMPERATURE },
110 { "Vccp2", SENSOR_TYPE_VOLTAGE },
111 { "Vccp1", SENSOR_TYPE_VOLTAGE },
112 { "vdd", SENSOR_TYPE_VOLTAGE },
113 { "vid5", SENSOR_TYPE_VOLTAGE },
114 { "vid4", SENSOR_TYPE_VOLTAGE },
115 { "vid3", SENSOR_TYPE_VOLTAGE },
116 { "vid2", SENSOR_TYPE_VOLTAGE },
117 { "vid1", SENSOR_TYPE_VOLTAGE },
118 { "vid", SENSOR_TYPE_VOLTAGE },
119 { "vin4", SENSOR_TYPE_VOLTAGE },
120 { "vin3", SENSOR_TYPE_VOLTAGE },
121 { "vin2", SENSOR_TYPE_VOLTAGE },
122 { "vin1", SENSOR_TYPE_VOLTAGE },
123 { "voltbatt", SENSOR_TYPE_VOLTAGE },
124 { "volt12", SENSOR_TYPE_VOLTAGE },
125 { "volt5", SENSOR_TYPE_VOLTAGE },
126 { "vrm", SENSOR_TYPE_VOLTAGE },
127 { "5.0V", SENSOR_TYPE_VOLTAGE },
128 { "5V", SENSOR_TYPE_VOLTAGE },
129 { "3.3V", SENSOR_TYPE_VOLTAGE },
130 { "2.5V", SENSOR_TYPE_VOLTAGE },
131 { "2.0V", SENSOR_TYPE_VOLTAGE },
132 { "12V", SENSOR_TYPE_VOLTAGE },
133 { "power1", SENSOR_TYPE_POWER }
135 static int known_features_num = STATIC_ARRAY_SIZE (known_features);
137 #endif /* SENSORS_API_VERSION < 0x400 */
139 static const char *config_keys[] =
146 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
148 #if SENSORS_API_VERSION < 0x400
149 typedef struct featurelist
151 const sensors_chip_name *chip;
152 const sensors_feature_data *data;
154 struct featurelist *next;
157 # ifndef SENSORS_CONF_PATH
158 # define SENSORS_CONF_PATH "/etc/sensors.conf"
160 static char *conffile = SENSORS_CONF_PATH;
161 /* #endif SENSORS_API_VERSION < 0x400 */
163 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
164 typedef struct featurelist
166 const sensors_chip_name *chip;
167 const sensors_feature *feature;
168 const sensors_subfeature *subfeature;
169 struct featurelist *next;
172 static char *conffile = NULL;
173 static _Bool use_labels = 0;
174 /* #endif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
176 #else /* if SENSORS_API_VERSION >= 0x500 */
177 # error "This version of libsensors is not supported yet. Please report this " \
181 static featurelist_t *first_feature = NULL;
182 static ignorelist_t *sensor_list;
184 #if SENSORS_API_VERSION < 0x400
185 /* full chip name logic borrowed from lm_sensors */
186 static int sensors_snprintf_chip_name (char *buf, size_t buf_size,
187 const sensors_chip_name *chip)
191 if (chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
193 status = ssnprintf (buf, buf_size,
198 else if (chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
200 status = snprintf (buf, buf_size, "%s-%s-%04x",
207 status = snprintf (buf, buf_size, "%s-i2c-%d-%02x",
214 } /* int sensors_snprintf_chip_name */
216 static int sensors_feature_name_to_type (const char *name)
218 /* Yes, this is slow, but it's only ever done during initialization, so
219 * it's a one time cost.. */
220 for (int i = 0; i < known_features_num; i++)
221 if (strcasecmp (known_features[i].label, name) == 0)
222 return (known_features[i].type);
224 return (SENSOR_TYPE_UNKNOWN);
225 } /* int sensors_feature_name_to_type */
228 static int sensors_config (const char *key, const char *value)
230 if (sensor_list == NULL)
231 sensor_list = ignorelist_create (1);
233 /* TODO: This setting exists for compatibility with old versions of
234 * lm-sensors. Remove support for those ancient versions in the next
236 if (strcasecmp (key, "SensorConfigFile") == 0)
238 char *tmp = strdup (value);
245 else if (strcasecmp (key, "Sensor") == 0)
247 if (ignorelist_add (sensor_list, value))
249 ERROR ("sensors plugin: "
250 "Cannot add value to ignorelist.");
254 else if (strcasecmp (key, "IgnoreSelected") == 0)
256 ignorelist_set_invert (sensor_list, 1);
258 ignorelist_set_invert (sensor_list, 0);
260 #if (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
261 else if (strcasecmp (key, "UseLabels") == 0)
263 use_labels = IS_TRUE (value) ? 1 : 0;
274 static void sensors_free_features (void)
276 featurelist_t *nextft;
278 if (first_feature == NULL)
283 for (featurelist_t *thisft = first_feature; thisft != NULL; thisft = nextft)
285 nextft = thisft->next;
288 first_feature = NULL;
291 static int sensors_load_conf (void)
293 static int call_once = 0;
296 featurelist_t *last_feature = NULL;
298 const sensors_chip_name *chip;
308 if (conffile != NULL)
310 fh = fopen (conffile, "r");
314 ERROR ("sensors plugin: fopen(%s) failed: %s", conffile,
315 sstrerror (errno, errbuf, sizeof (errbuf)));
320 status = sensors_init (fh);
326 ERROR ("sensors plugin: Cannot initialize sensors. "
327 "Data will not be collected.");
331 #if SENSORS_API_VERSION < 0x400
333 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
335 int feature_num0 = 0;
336 int feature_num1 = 0;
340 const sensors_feature_data *feature;
344 feature = sensors_get_all_features (*chip,
345 &feature_num0, &feature_num1);
347 /* Check if all features have been read. */
351 /* "master features" only */
352 if (feature->mapping != SENSORS_NO_MAPPING)
354 DEBUG ("sensors plugin: sensors_load_conf: "
355 "Ignoring subfeature `%s', "
356 "because (feature->mapping "
357 "!= SENSORS_NO_MAPPING).",
362 /* skip ignored in sensors.conf */
363 if (sensors_get_ignored (*chip, feature->number) == 0)
365 DEBUG ("sensors plugin: sensors_load_conf: "
366 "Ignoring subfeature `%s', "
368 "`sensors_get_ignored' told "
374 feature_type = sensors_feature_name_to_type (
376 if (feature_type == SENSOR_TYPE_UNKNOWN)
378 DEBUG ("sensors plugin: sensors_load_conf: "
379 "Ignoring subfeature `%s', "
380 "because its type is "
386 fl = calloc (1, sizeof (*fl));
389 ERROR ("sensors plugin: calloc failed.");
395 fl->type = feature_type;
397 if (first_feature == NULL)
400 last_feature->next = fl;
402 } /* while sensors_get_all_features */
403 } /* while sensors_get_detected_chips */
404 /* #endif SENSORS_API_VERSION < 0x400 */
406 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
408 while ((chip = sensors_get_detected_chips (NULL, &chip_num)) != NULL)
410 const sensors_feature *feature;
413 while ((feature = sensors_get_features (chip, &feature_num)) != NULL)
415 const sensors_subfeature *subfeature;
416 int subfeature_num = 0;
418 /* Only handle voltage, fanspeeds and temperatures */
419 if ((feature->type != SENSORS_FEATURE_IN)
420 && (feature->type != SENSORS_FEATURE_FAN)
421 && (feature->type != SENSORS_FEATURE_TEMP)
422 && (feature->type != SENSORS_FEATURE_POWER))
424 DEBUG ("sensors plugin: sensors_load_conf: "
425 "Ignoring feature `%s', "
426 "because its type is not "
427 "supported.", feature->name);
431 while ((subfeature = sensors_get_all_subfeatures (chip,
432 feature, &subfeature_num)) != NULL)
436 if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT)
437 && (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT)
438 && (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT)
439 && (subfeature->type != SENSORS_SUBFEATURE_POWER_INPUT))
442 fl = calloc (1, sizeof (*fl));
445 ERROR ("sensors plugin: calloc failed.");
450 fl->feature = feature;
451 fl->subfeature = subfeature;
453 if (first_feature == NULL)
456 last_feature->next = fl;
458 } /* while (subfeature) */
459 } /* while (feature) */
461 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
463 if (first_feature == NULL)
466 INFO ("sensors plugin: lm_sensors reports no "
467 "features. Data will not be collected.");
472 } /* int sensors_load_conf */
474 static int sensors_shutdown (void)
476 sensors_free_features ();
477 ignorelist_free (sensor_list);
480 } /* int sensors_shutdown */
482 static void sensors_submit (const char *plugin_instance,
483 const char *type, const char *type_instance,
486 char match_key[1024];
489 value_list_t vl = VALUE_LIST_INIT;
491 status = ssnprintf (match_key, sizeof (match_key), "%s/%s-%s",
492 plugin_instance, type, type_instance);
496 if (sensor_list != NULL)
498 DEBUG ("sensors plugin: Checking ignorelist for `%s'", match_key);
499 if (ignorelist_match (sensor_list, match_key))
503 vl.values = &(value_t) { .gauge = value };
506 sstrncpy (vl.plugin, "sensors", sizeof (vl.plugin));
507 sstrncpy (vl.plugin_instance, plugin_instance,
508 sizeof (vl.plugin_instance));
509 sstrncpy (vl.type, type, sizeof (vl.type));
510 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
512 plugin_dispatch_values (&vl);
513 } /* void sensors_submit */
515 static int sensors_read (void)
517 if (sensors_load_conf () != 0)
520 #if SENSORS_API_VERSION < 0x400
521 for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next)
525 char plugin_instance[DATA_MAX_NAME_LEN];
526 char type_instance[DATA_MAX_NAME_LEN];
528 status = sensors_get_feature (*fl->chip,
529 fl->data->number, &value);
533 status = sensors_snprintf_chip_name (plugin_instance,
534 sizeof (plugin_instance), fl->chip);
538 sstrncpy (type_instance, fl->data->name,
539 sizeof (type_instance));
541 sensors_submit (plugin_instance,
542 sensor_type_name_map[fl->type],
545 } /* for fl = first_feature .. NULL */
546 /* #endif SENSORS_API_VERSION < 0x400 */
548 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
549 for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next)
553 char plugin_instance[DATA_MAX_NAME_LEN];
554 char type_instance[DATA_MAX_NAME_LEN];
558 status = sensors_get_value (fl->chip,
559 fl->subfeature->number, &value);
563 status = sensors_snprintf_chip_name (plugin_instance,
564 sizeof (plugin_instance), fl->chip);
570 sensor_label = sensors_get_label (fl->chip, fl->feature);
571 sstrncpy (type_instance, sensor_label, sizeof (type_instance));
576 sstrncpy (type_instance, fl->feature->name,
577 sizeof (type_instance));
580 if (fl->feature->type == SENSORS_FEATURE_IN)
582 else if (fl->feature->type
583 == SENSORS_FEATURE_FAN)
585 else if (fl->feature->type
586 == SENSORS_FEATURE_TEMP)
587 type = "temperature";
588 else if (fl->feature->type
589 == SENSORS_FEATURE_POWER)
594 sensors_submit (plugin_instance, type, type_instance, value);
595 } /* for fl = first_feature .. NULL */
596 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
599 } /* int sensors_read */
601 void module_register (void)
603 plugin_register_config ("sensors", sensors_config,
604 config_keys, config_keys_num);
605 plugin_register_read ("sensors", sensors_read);
606 plugin_register_shutdown ("sensors", sensors_shutdown);
607 } /* void module_register */