2 * collectd - src/sensors.c
3 * Copyright (C) 2005-2007 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
21 * Lubos Stanek <lubek at users.sourceforge.net> Wed Oct 27, 2006
22 * - config ExtendedSensorNaming option
23 * - precise sensor feature selection (chip-bus-address/type-feature)
24 * with ExtendedSensorNaming
25 * - more sensor features (finite list)
26 * - honor sensors.conf's ignored
27 * - config Sensor option
28 * - config IgnoreSelected option
34 #include "configfile.h"
35 #include "utils_ignorelist.h"
37 #if defined(HAVE_SENSORS_SENSORS_H)
38 # include <sensors/sensors.h>
41 #if !defined(SENSORS_API_VERSION)
42 # define SENSORS_API_VERSION 0x000
45 #define SENSOR_TYPE_VOLTAGE 0
46 #define SENSOR_TYPE_FANSPEED 1
47 #define SENSOR_TYPE_TEMPERATURE 2
48 #define SENSOR_TYPE_UNKNOWN 3
50 #if SENSORS_API_VERSION < 0x400
51 static char *sensor_to_type[] =
59 struct sensors_labeltypes_s
64 typedef struct sensors_labeltypes_s sensors_labeltypes_t;
67 * finite list of known labels extracted from lm_sensors
69 static sensors_labeltypes_t known_features[] =
71 { "fan1", SENSOR_TYPE_FANSPEED },
72 { "fan2", SENSOR_TYPE_FANSPEED },
73 { "fan3", SENSOR_TYPE_FANSPEED },
74 { "fan4", SENSOR_TYPE_FANSPEED },
75 { "fan5", SENSOR_TYPE_FANSPEED },
76 { "fan6", SENSOR_TYPE_FANSPEED },
77 { "fan7", SENSOR_TYPE_FANSPEED },
78 { "AIN2", SENSOR_TYPE_VOLTAGE },
79 { "AIN1", SENSOR_TYPE_VOLTAGE },
80 { "in10", SENSOR_TYPE_VOLTAGE },
81 { "in9", SENSOR_TYPE_VOLTAGE },
82 { "in8", SENSOR_TYPE_VOLTAGE },
83 { "in7", SENSOR_TYPE_VOLTAGE },
84 { "in6", SENSOR_TYPE_VOLTAGE },
85 { "in5", SENSOR_TYPE_VOLTAGE },
86 { "in4", SENSOR_TYPE_VOLTAGE },
87 { "in3", SENSOR_TYPE_VOLTAGE },
88 { "in2", SENSOR_TYPE_VOLTAGE },
89 { "in0", SENSOR_TYPE_VOLTAGE },
90 { "CPU_Temp", SENSOR_TYPE_TEMPERATURE },
91 { "remote_temp", SENSOR_TYPE_TEMPERATURE },
92 { "temp1", SENSOR_TYPE_TEMPERATURE },
93 { "temp2", SENSOR_TYPE_TEMPERATURE },
94 { "temp3", SENSOR_TYPE_TEMPERATURE },
95 { "temp4", SENSOR_TYPE_TEMPERATURE },
96 { "temp5", SENSOR_TYPE_TEMPERATURE },
97 { "temp6", SENSOR_TYPE_TEMPERATURE },
98 { "temp7", SENSOR_TYPE_TEMPERATURE },
99 { "temp", SENSOR_TYPE_TEMPERATURE },
100 { "Vccp2", SENSOR_TYPE_VOLTAGE },
101 { "Vccp1", SENSOR_TYPE_VOLTAGE },
102 { "vdd", SENSOR_TYPE_VOLTAGE },
103 { "vid5", SENSOR_TYPE_VOLTAGE },
104 { "vid4", SENSOR_TYPE_VOLTAGE },
105 { "vid3", SENSOR_TYPE_VOLTAGE },
106 { "vid2", SENSOR_TYPE_VOLTAGE },
107 { "vid1", SENSOR_TYPE_VOLTAGE },
108 { "vid", SENSOR_TYPE_VOLTAGE },
109 { "vin4", SENSOR_TYPE_VOLTAGE },
110 { "vin3", SENSOR_TYPE_VOLTAGE },
111 { "vin2", SENSOR_TYPE_VOLTAGE },
112 { "vin1", SENSOR_TYPE_VOLTAGE },
113 { "voltbatt", SENSOR_TYPE_VOLTAGE },
114 { "volt12", SENSOR_TYPE_VOLTAGE },
115 { "volt5", SENSOR_TYPE_VOLTAGE },
116 { "vrm", SENSOR_TYPE_VOLTAGE },
117 { "5.0V", SENSOR_TYPE_VOLTAGE },
118 { "5V", SENSOR_TYPE_VOLTAGE },
119 { "3.3V", SENSOR_TYPE_VOLTAGE },
120 { "2.5V", SENSOR_TYPE_VOLTAGE },
121 { "2.0V", SENSOR_TYPE_VOLTAGE },
122 { "12V", SENSOR_TYPE_VOLTAGE }
124 static int known_features_num = STATIC_ARRAY_SIZE (known_features);
126 #endif /* SENSORS_API_VERSION < 0x400 */
128 static const char *config_keys[] =
134 static int config_keys_num = 2;
136 #if SENSORS_API_VERSION < 0x400
137 typedef struct featurelist
139 const sensors_chip_name *chip;
140 const sensors_feature_data *data;
142 struct featurelist *next;
145 # ifndef SENSORS_CONF_PATH
146 # define SENSORS_CONF_PATH "/etc/sensors.conf"
148 /* #endif SENSORS_API_VERSION < 0x400 */
150 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
151 typedef struct featurelist
153 const sensors_chip_name *chip;
154 const sensors_feature *feature;
155 const sensors_subfeature *subfeature;
156 struct featurelist *next;
159 # ifndef SENSORS_CONF_PATH
160 # define SENSORS_CONF_PATH "/etc/sensors3.conf"
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 const char *conffile = SENSORS_CONF_PATH;
170 featurelist_t *first_feature = NULL;
171 static ignorelist_t *sensor_list;
172 static time_t sensors_config_mtime = 0;
174 static int sensors_config (const char *key, const char *value)
176 if (sensor_list == NULL)
177 sensor_list = ignorelist_create (1);
179 if (strcasecmp (key, "Sensor") == 0)
181 if (ignorelist_add (sensor_list, value))
183 ERROR ("sensors plugin: "
184 "Cannot add value to ignorelist.");
188 else if (strcasecmp (key, "IgnoreSelected") == 0)
190 ignorelist_set_invert (sensor_list, 1);
191 if ((strcasecmp (value, "True") == 0)
192 || (strcasecmp (value, "Yes") == 0)
193 || (strcasecmp (value, "On") == 0))
194 ignorelist_set_invert (sensor_list, 0);
204 void sensors_free_features (void)
206 featurelist_t *thisft;
207 featurelist_t *nextft;
209 if (first_feature == NULL)
214 for (thisft = first_feature; thisft != NULL; thisft = nextft)
216 nextft = thisft->next;
219 first_feature = NULL;
222 static int sensors_load_conf (void)
225 featurelist_t *last_feature = NULL;
227 const sensors_chip_name *chip;
233 status = stat (conffile, &statbuf);
237 ERROR ("sensors plugin: stat (%s) failed: %s", conffile,
238 sstrerror (errno, errbuf, sizeof (errbuf)));
239 sensors_config_mtime = 0;
242 if ((sensors_config_mtime != 0)
243 && (sensors_config_mtime == statbuf.st_mtime))
246 if (sensors_config_mtime != 0)
248 NOTICE ("sensors plugin: Reloading config from %s",
250 sensors_free_features ();
251 sensors_config_mtime = 0;
254 fh = fopen (conffile, "r");
258 ERROR ("sensors plugin: fopen(%s) failed: %s", conffile,
259 sstrerror (errno, errbuf, sizeof (errbuf)));
263 status = sensors_init (fh);
267 ERROR ("sensors plugin: Cannot initialize sensors. "
268 "Data will not be collected.");
272 sensors_config_mtime = statbuf.st_mtime;
274 #if SENSORS_API_VERSION < 0x400
276 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
278 int feature_num0 = 0;
279 int feature_num1 = 0;
283 const sensors_feature_data *feature;
286 feature = sensors_get_all_features (*chip,
287 &feature_num0, &feature_num1);
289 /* Check if all features have been read. */
293 /* "master features" only */
294 if (feature->mapping != SENSORS_NO_MAPPING)
297 /* Only known features */
298 for (i = 0; i < known_features_num; i++)
302 if (strcasecmp (feature->name,
303 known_features[i].label)
307 /* skip ignored in sensors.conf */
308 if (sensors_get_ignored (*chip, feature->number) == 0)
311 DEBUG ("Adding feature: %s-%s-%s",
313 sensor_to_type[known_features[i].type],
316 fl = (featurelist_t *) malloc (sizeof (featurelist_t));
319 ERROR ("sensors plugin: malloc failed.");
322 memset (fl, '\0', sizeof (featurelist_t));
326 fl->type = known_features[i].type;
328 if (first_feature == NULL)
331 last_feature->next = fl;
334 /* stop searching known features at first found */
337 } /* while sensors_get_all_features */
338 } /* while sensors_get_detected_chips */
339 /* #endif SENSORS_API_VERSION < 0x400 */
341 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
343 DEBUG ("sensors plugin: Calling sensors_get_detected_chips (NULL, %p)..", (void *) &chip_num);
344 while ((chip = sensors_get_detected_chips (NULL, &chip_num)) != NULL)
346 const sensors_feature *feature;
349 DEBUG ("sensors plugin: Handling chip with prefix `%s'",
350 (chip->prefix == NULL)
354 while ((feature = sensors_get_features (chip, &feature_num)) != NULL)
356 const sensors_subfeature *subfeature;
357 int subfeature_num = 0;
359 /* Only handle voltage, fanspeeds and temperatures */
360 if ((feature->type != SENSORS_FEATURE_IN)
361 && (feature->type != SENSORS_FEATURE_FAN)
362 && (feature->type != SENSORS_FEATURE_TEMP))
365 while ((subfeature = sensors_get_all_subfeatures (chip,
366 feature, &subfeature_num)) != NULL)
370 if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT)
371 && (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT)
372 && (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT))
375 fl = (featurelist_t *) malloc (sizeof (featurelist_t));
378 ERROR ("sensors plugin: malloc failed.");
381 memset (fl, '\0', sizeof (featurelist_t));
384 fl->feature = feature;
385 fl->subfeature = subfeature;
387 if (first_feature == NULL)
390 last_feature->next = fl;
392 } /* while (subfeature) */
393 } /* while (feature) */
395 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
397 if (first_feature == NULL)
400 INFO ("sensors plugin: lm_sensors reports no "
401 "features. Data will not be collected.");
406 } /* int sensors_load_conf */
408 static int sensors_shutdown (void)
410 sensors_free_features ();
411 ignorelist_free (sensor_list);
414 } /* int sensors_shutdown */
416 static void sensors_submit (const char *plugin_instance,
417 const char *type, const char *type_instance,
420 char match_key[1024];
424 value_list_t vl = VALUE_LIST_INIT;
426 status = snprintf (match_key, sizeof (match_key), "%s/%s-%s",
427 plugin_instance, type, type_instance);
428 if ((status < 1) || (status >= sizeof (match_key)))
430 match_key[sizeof (match_key) - 1] = '\0';
432 if (sensor_list != NULL)
434 DEBUG ("sensors plugin: Checking ignorelist for `%s'", match_key);
435 if (ignorelist_match (sensor_list, match_key))
439 values[0].gauge = val;
443 vl.time = time (NULL);
444 strcpy (vl.host, hostname_g);
445 strcpy (vl.plugin, "sensors");
446 strcpy (vl.plugin_instance, plugin_instance);
447 strcpy (vl.type_instance, type_instance);
449 plugin_dispatch_values (type, &vl);
450 } /* void sensors_submit */
452 static int sensors_read (void)
456 if (sensors_load_conf () != 0)
459 #if SENSORS_API_VERSION < 0x400
460 for (fl = first_feature; fl != NULL; fl = fl->next)
464 char plugin_instance[DATA_MAX_NAME_LEN];
465 char type_instance[DATA_MAX_NAME_LEN];
467 status = sensors_get_feature (*fl->chip,
468 fl->data->number, &value);
472 /* full chip name logic borrowed from lm_sensors */
473 if (fl->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
475 snprintf (plugin_instance, DATA_MAX_NAME_LEN,
480 else if (fl->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
482 snprintf (plugin_instance, 512, "%s-%s-%04x",
489 snprintf (plugin_instance, 512, "%s-i2c-%d-%02x",
494 plugin_instance[sizeof (plugin_instance) - 1] = '\0';
496 strncpy (type_instance, fl->data->name, sizeof (type_instance));
497 type_instance[sizeof (type_instance) - 1] = '\0';
499 sensors_submit (plugin_instance,
500 sensor_to_type[fl->type],
503 } /* for fl = first_feature .. NULL */
504 /* #endif SENSORS_API_VERSION < 0x400 */
506 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
507 for (fl = first_feature; fl != NULL; fl = fl->next)
511 char plugin_instance[DATA_MAX_NAME_LEN];
512 char type_instance[DATA_MAX_NAME_LEN];
515 status = sensors_get_value (fl->chip,
516 fl->subfeature->number, &value);
520 status = sensors_snprintf_chip_name (plugin_instance,
521 sizeof (plugin_instance), fl->chip);
524 plugin_instance[sizeof (plugin_instance) - 1] = '\0';
526 strncpy (type_instance, fl->feature->name,
527 sizeof (type_instance));
528 type_instance[sizeof (type_instance) - 1] = '\0';
530 if (fl->feature->type == SENSORS_FEATURE_IN)
532 else if (fl->feature->type
533 == SENSORS_FEATURE_FAN)
535 else if (fl->feature->type
536 == SENSORS_FEATURE_TEMP)
541 sensors_submit (plugin_instance, type, type_instance, value);
542 } /* for fl = first_feature .. NULL */
543 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
546 } /* int sensors_read */
548 void module_register (void)
550 plugin_register_config ("sensors", sensors_config,
551 config_keys, config_keys_num);
552 plugin_register_read ("sensors", sensors_read);
553 plugin_register_shutdown ("sensors", sensors_shutdown);
554 } /* void module_register */