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 "configfile.h"
41 #include "utils_ignorelist.h"
43 #if defined(HAVE_SENSORS_SENSORS_H)
44 # include <sensors/sensors.h>
47 #if !defined(SENSORS_API_VERSION)
48 # define SENSORS_API_VERSION 0x000
52 * The sensors library prior to version 3.0 (internal version 0x400) didn't
53 * report the type of values, only a name. The following lists are there to
54 * convert from the names to the type. They are not used with the new
57 #if SENSORS_API_VERSION < 0x400
58 static char *sensor_type_name_map[] =
60 # define SENSOR_TYPE_VOLTAGE 0
62 # define SENSOR_TYPE_FANSPEED 1
64 # define SENSOR_TYPE_TEMPERATURE 2
66 # define SENSOR_TYPE_POWER 3
68 # define SENSOR_TYPE_UNKNOWN 4
72 struct sensors_labeltypes_s
77 typedef struct sensors_labeltypes_s sensors_labeltypes_t;
79 /* finite list of known labels extracted from lm_sensors */
80 static sensors_labeltypes_t known_features[] =
82 { "fan1", SENSOR_TYPE_FANSPEED },
83 { "fan2", SENSOR_TYPE_FANSPEED },
84 { "fan3", SENSOR_TYPE_FANSPEED },
85 { "fan4", SENSOR_TYPE_FANSPEED },
86 { "fan5", SENSOR_TYPE_FANSPEED },
87 { "fan6", SENSOR_TYPE_FANSPEED },
88 { "fan7", SENSOR_TYPE_FANSPEED },
89 { "AIN2", SENSOR_TYPE_VOLTAGE },
90 { "AIN1", SENSOR_TYPE_VOLTAGE },
91 { "in10", SENSOR_TYPE_VOLTAGE },
92 { "in9", SENSOR_TYPE_VOLTAGE },
93 { "in8", SENSOR_TYPE_VOLTAGE },
94 { "in7", SENSOR_TYPE_VOLTAGE },
95 { "in6", SENSOR_TYPE_VOLTAGE },
96 { "in5", SENSOR_TYPE_VOLTAGE },
97 { "in4", SENSOR_TYPE_VOLTAGE },
98 { "in3", SENSOR_TYPE_VOLTAGE },
99 { "in2", SENSOR_TYPE_VOLTAGE },
100 { "in0", SENSOR_TYPE_VOLTAGE },
101 { "CPU_Temp", SENSOR_TYPE_TEMPERATURE },
102 { "remote_temp", SENSOR_TYPE_TEMPERATURE },
103 { "temp1", SENSOR_TYPE_TEMPERATURE },
104 { "temp2", SENSOR_TYPE_TEMPERATURE },
105 { "temp3", SENSOR_TYPE_TEMPERATURE },
106 { "temp4", SENSOR_TYPE_TEMPERATURE },
107 { "temp5", SENSOR_TYPE_TEMPERATURE },
108 { "temp6", SENSOR_TYPE_TEMPERATURE },
109 { "temp7", SENSOR_TYPE_TEMPERATURE },
110 { "temp", SENSOR_TYPE_TEMPERATURE },
111 { "Vccp2", SENSOR_TYPE_VOLTAGE },
112 { "Vccp1", SENSOR_TYPE_VOLTAGE },
113 { "vdd", SENSOR_TYPE_VOLTAGE },
114 { "vid5", SENSOR_TYPE_VOLTAGE },
115 { "vid4", SENSOR_TYPE_VOLTAGE },
116 { "vid3", SENSOR_TYPE_VOLTAGE },
117 { "vid2", SENSOR_TYPE_VOLTAGE },
118 { "vid1", SENSOR_TYPE_VOLTAGE },
119 { "vid", SENSOR_TYPE_VOLTAGE },
120 { "vin4", SENSOR_TYPE_VOLTAGE },
121 { "vin3", SENSOR_TYPE_VOLTAGE },
122 { "vin2", SENSOR_TYPE_VOLTAGE },
123 { "vin1", SENSOR_TYPE_VOLTAGE },
124 { "voltbatt", SENSOR_TYPE_VOLTAGE },
125 { "volt12", SENSOR_TYPE_VOLTAGE },
126 { "volt5", SENSOR_TYPE_VOLTAGE },
127 { "vrm", SENSOR_TYPE_VOLTAGE },
128 { "5.0V", SENSOR_TYPE_VOLTAGE },
129 { "5V", SENSOR_TYPE_VOLTAGE },
130 { "3.3V", SENSOR_TYPE_VOLTAGE },
131 { "2.5V", SENSOR_TYPE_VOLTAGE },
132 { "2.0V", SENSOR_TYPE_VOLTAGE },
133 { "12V", SENSOR_TYPE_VOLTAGE },
134 { "power1", SENSOR_TYPE_POWER }
136 static int known_features_num = STATIC_ARRAY_SIZE (known_features);
138 #endif /* SENSORS_API_VERSION < 0x400 */
140 static const char *config_keys[] =
147 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
149 #if SENSORS_API_VERSION < 0x400
150 typedef struct featurelist
152 const sensors_chip_name *chip;
153 const sensors_feature_data *data;
155 struct featurelist *next;
158 # ifndef SENSORS_CONF_PATH
159 # define SENSORS_CONF_PATH "/etc/sensors.conf"
161 static char *conffile = SENSORS_CONF_PATH;
162 /* #endif SENSORS_API_VERSION < 0x400 */
164 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
165 typedef struct featurelist
167 const sensors_chip_name *chip;
168 const sensors_feature *feature;
169 const sensors_subfeature *subfeature;
170 struct featurelist *next;
173 static char *conffile = NULL;
174 static _Bool use_labels = 0;
175 /* #endif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
177 #else /* if SENSORS_API_VERSION >= 0x500 */
178 # error "This version of libsensors is not supported yet. Please report this " \
182 static featurelist_t *first_feature = NULL;
183 static ignorelist_t *sensor_list;
185 #if SENSORS_API_VERSION < 0x400
186 /* full chip name logic borrowed from lm_sensors */
187 static int sensors_snprintf_chip_name (char *buf, size_t buf_size,
188 const sensors_chip_name *chip)
192 if (chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
194 status = ssnprintf (buf, buf_size,
199 else if (chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
201 status = snprintf (buf, buf_size, "%s-%s-%04x",
208 status = snprintf (buf, buf_size, "%s-i2c-%d-%02x",
215 } /* int sensors_snprintf_chip_name */
217 static int sensors_feature_name_to_type (const char *name)
219 /* Yes, this is slow, but it's only ever done during initialization, so
220 * it's a one time cost.. */
221 for (int i = 0; i < known_features_num; i++)
222 if (strcasecmp (known_features[i].label, name) == 0)
223 return (known_features[i].type);
225 return (SENSOR_TYPE_UNKNOWN);
226 } /* int sensors_feature_name_to_type */
229 static int sensors_config (const char *key, const char *value)
231 if (sensor_list == NULL)
232 sensor_list = ignorelist_create (1);
234 /* TODO: This setting exists for compatibility with old versions of
235 * lm-sensors. Remove support for those ancient versions in the next
237 if (strcasecmp (key, "SensorConfigFile") == 0)
239 char *tmp = strdup (value);
246 else if (strcasecmp (key, "Sensor") == 0)
248 if (ignorelist_add (sensor_list, value))
250 ERROR ("sensors plugin: "
251 "Cannot add value to ignorelist.");
255 else if (strcasecmp (key, "IgnoreSelected") == 0)
257 ignorelist_set_invert (sensor_list, 1);
259 ignorelist_set_invert (sensor_list, 0);
261 #if (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
262 else if (strcasecmp (key, "UseLabels") == 0)
264 use_labels = IS_TRUE (value) ? 1 : 0;
275 static void sensors_free_features (void)
277 featurelist_t *nextft;
279 if (first_feature == NULL)
284 for (featurelist_t *thisft = first_feature; thisft != NULL; thisft = nextft)
286 nextft = thisft->next;
289 first_feature = NULL;
292 static int sensors_load_conf (void)
294 static int call_once = 0;
297 featurelist_t *last_feature = NULL;
299 const sensors_chip_name *chip;
309 if (conffile != NULL)
311 fh = fopen (conffile, "r");
315 ERROR ("sensors plugin: fopen(%s) failed: %s", conffile,
316 sstrerror (errno, errbuf, sizeof (errbuf)));
321 status = sensors_init (fh);
327 ERROR ("sensors plugin: Cannot initialize sensors. "
328 "Data will not be collected.");
332 #if SENSORS_API_VERSION < 0x400
334 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
336 int feature_num0 = 0;
337 int feature_num1 = 0;
341 const sensors_feature_data *feature;
345 feature = sensors_get_all_features (*chip,
346 &feature_num0, &feature_num1);
348 /* Check if all features have been read. */
352 /* "master features" only */
353 if (feature->mapping != SENSORS_NO_MAPPING)
355 DEBUG ("sensors plugin: sensors_load_conf: "
356 "Ignoring subfeature `%s', "
357 "because (feature->mapping "
358 "!= SENSORS_NO_MAPPING).",
363 /* skip ignored in sensors.conf */
364 if (sensors_get_ignored (*chip, feature->number) == 0)
366 DEBUG ("sensors plugin: sensors_load_conf: "
367 "Ignoring subfeature `%s', "
369 "`sensors_get_ignored' told "
375 feature_type = sensors_feature_name_to_type (
377 if (feature_type == SENSOR_TYPE_UNKNOWN)
379 DEBUG ("sensors plugin: sensors_load_conf: "
380 "Ignoring subfeature `%s', "
381 "because its type is "
387 fl = calloc (1, sizeof (*fl));
390 ERROR ("sensors plugin: calloc failed.");
396 fl->type = feature_type;
398 if (first_feature == NULL)
401 last_feature->next = fl;
403 } /* while sensors_get_all_features */
404 } /* while sensors_get_detected_chips */
405 /* #endif SENSORS_API_VERSION < 0x400 */
407 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
409 while ((chip = sensors_get_detected_chips (NULL, &chip_num)) != NULL)
411 const sensors_feature *feature;
414 while ((feature = sensors_get_features (chip, &feature_num)) != NULL)
416 const sensors_subfeature *subfeature;
417 int subfeature_num = 0;
419 /* Only handle voltage, fanspeeds and temperatures */
420 if ((feature->type != SENSORS_FEATURE_IN)
421 && (feature->type != SENSORS_FEATURE_FAN)
422 && (feature->type != SENSORS_FEATURE_TEMP)
423 && (feature->type != SENSORS_FEATURE_POWER))
425 DEBUG ("sensors plugin: sensors_load_conf: "
426 "Ignoring feature `%s', "
427 "because its type is not "
428 "supported.", feature->name);
432 while ((subfeature = sensors_get_all_subfeatures (chip,
433 feature, &subfeature_num)) != NULL)
437 if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT)
438 && (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT)
439 && (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT)
440 && (subfeature->type != SENSORS_SUBFEATURE_POWER_INPUT))
443 fl = calloc (1, sizeof (*fl));
446 ERROR ("sensors plugin: calloc failed.");
451 fl->feature = feature;
452 fl->subfeature = subfeature;
454 if (first_feature == NULL)
457 last_feature->next = fl;
459 } /* while (subfeature) */
460 } /* while (feature) */
462 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
464 if (first_feature == NULL)
467 INFO ("sensors plugin: lm_sensors reports no "
468 "features. Data will not be collected.");
473 } /* int sensors_load_conf */
475 static int sensors_shutdown (void)
477 sensors_free_features ();
478 ignorelist_free (sensor_list);
481 } /* int sensors_shutdown */
483 static void sensors_submit (const char *plugin_instance,
484 const char *type, const char *type_instance,
487 char match_key[1024];
491 value_list_t vl = VALUE_LIST_INIT;
493 status = ssnprintf (match_key, sizeof (match_key), "%s/%s-%s",
494 plugin_instance, type, type_instance);
498 if (sensor_list != NULL)
500 DEBUG ("sensors plugin: Checking ignorelist for `%s'", match_key);
501 if (ignorelist_match (sensor_list, match_key))
505 values[0].gauge = val;
510 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
511 sstrncpy (vl.plugin, "sensors", sizeof (vl.plugin));
512 sstrncpy (vl.plugin_instance, plugin_instance,
513 sizeof (vl.plugin_instance));
514 sstrncpy (vl.type, type, sizeof (vl.type));
515 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
517 plugin_dispatch_values (&vl);
518 } /* void sensors_submit */
520 static int sensors_read (void)
522 if (sensors_load_conf () != 0)
525 #if SENSORS_API_VERSION < 0x400
526 for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next)
530 char plugin_instance[DATA_MAX_NAME_LEN];
531 char type_instance[DATA_MAX_NAME_LEN];
533 status = sensors_get_feature (*fl->chip,
534 fl->data->number, &value);
538 status = sensors_snprintf_chip_name (plugin_instance,
539 sizeof (plugin_instance), fl->chip);
543 sstrncpy (type_instance, fl->data->name,
544 sizeof (type_instance));
546 sensors_submit (plugin_instance,
547 sensor_type_name_map[fl->type],
550 } /* for fl = first_feature .. NULL */
551 /* #endif SENSORS_API_VERSION < 0x400 */
553 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
554 for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next)
558 char plugin_instance[DATA_MAX_NAME_LEN];
559 char type_instance[DATA_MAX_NAME_LEN];
563 status = sensors_get_value (fl->chip,
564 fl->subfeature->number, &value);
568 status = sensors_snprintf_chip_name (plugin_instance,
569 sizeof (plugin_instance), fl->chip);
575 sensor_label = sensors_get_label (fl->chip, fl->feature);
576 sstrncpy (type_instance, sensor_label, sizeof (type_instance));
581 sstrncpy (type_instance, fl->feature->name,
582 sizeof (type_instance));
585 if (fl->feature->type == SENSORS_FEATURE_IN)
587 else if (fl->feature->type
588 == SENSORS_FEATURE_FAN)
590 else if (fl->feature->type
591 == SENSORS_FEATURE_TEMP)
592 type = "temperature";
593 else if (fl->feature->type
594 == SENSORS_FEATURE_POWER)
599 sensors_submit (plugin_instance, type, type_instance, value);
600 } /* for fl = first_feature .. NULL */
601 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
604 } /* int sensors_read */
606 void module_register (void)
608 plugin_register_config ("sensors", sensors_config,
609 config_keys, config_keys_num);
610 plugin_register_read ("sensors", sensors_read);
611 plugin_register_shutdown ("sensors", sensors_shutdown);
612 } /* void module_register */