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
46 * The sensors library prior to version 3.0 (internal version 0x400) didn't
47 * report the type of values, only a name. The following lists are there to
48 * convert from the names to the type. They are not used with the new
51 #if SENSORS_API_VERSION < 0x400
52 static char *sensor_type_name_map[] =
54 # define SENSOR_TYPE_VOLTAGE 0
56 # define SENSOR_TYPE_FANSPEED 1
58 # define SENSOR_TYPE_TEMPERATURE 2
60 # define SENSOR_TYPE_UNKNOWN 3
64 struct sensors_labeltypes_s
69 typedef struct sensors_labeltypes_s sensors_labeltypes_t;
71 /* finite list of known labels extracted from lm_sensors */
72 static sensors_labeltypes_t known_features[] =
74 { "fan1", SENSOR_TYPE_FANSPEED },
75 { "fan2", SENSOR_TYPE_FANSPEED },
76 { "fan3", SENSOR_TYPE_FANSPEED },
77 { "fan4", SENSOR_TYPE_FANSPEED },
78 { "fan5", SENSOR_TYPE_FANSPEED },
79 { "fan6", SENSOR_TYPE_FANSPEED },
80 { "fan7", SENSOR_TYPE_FANSPEED },
81 { "AIN2", SENSOR_TYPE_VOLTAGE },
82 { "AIN1", SENSOR_TYPE_VOLTAGE },
83 { "in10", SENSOR_TYPE_VOLTAGE },
84 { "in9", SENSOR_TYPE_VOLTAGE },
85 { "in8", SENSOR_TYPE_VOLTAGE },
86 { "in7", SENSOR_TYPE_VOLTAGE },
87 { "in6", SENSOR_TYPE_VOLTAGE },
88 { "in5", SENSOR_TYPE_VOLTAGE },
89 { "in4", SENSOR_TYPE_VOLTAGE },
90 { "in3", SENSOR_TYPE_VOLTAGE },
91 { "in2", SENSOR_TYPE_VOLTAGE },
92 { "in0", SENSOR_TYPE_VOLTAGE },
93 { "CPU_Temp", SENSOR_TYPE_TEMPERATURE },
94 { "remote_temp", SENSOR_TYPE_TEMPERATURE },
95 { "temp1", SENSOR_TYPE_TEMPERATURE },
96 { "temp2", SENSOR_TYPE_TEMPERATURE },
97 { "temp3", SENSOR_TYPE_TEMPERATURE },
98 { "temp4", SENSOR_TYPE_TEMPERATURE },
99 { "temp5", SENSOR_TYPE_TEMPERATURE },
100 { "temp6", SENSOR_TYPE_TEMPERATURE },
101 { "temp7", SENSOR_TYPE_TEMPERATURE },
102 { "temp", SENSOR_TYPE_TEMPERATURE },
103 { "Vccp2", SENSOR_TYPE_VOLTAGE },
104 { "Vccp1", SENSOR_TYPE_VOLTAGE },
105 { "vdd", SENSOR_TYPE_VOLTAGE },
106 { "vid5", SENSOR_TYPE_VOLTAGE },
107 { "vid4", SENSOR_TYPE_VOLTAGE },
108 { "vid3", SENSOR_TYPE_VOLTAGE },
109 { "vid2", SENSOR_TYPE_VOLTAGE },
110 { "vid1", SENSOR_TYPE_VOLTAGE },
111 { "vid", SENSOR_TYPE_VOLTAGE },
112 { "vin4", SENSOR_TYPE_VOLTAGE },
113 { "vin3", SENSOR_TYPE_VOLTAGE },
114 { "vin2", SENSOR_TYPE_VOLTAGE },
115 { "vin1", SENSOR_TYPE_VOLTAGE },
116 { "voltbatt", SENSOR_TYPE_VOLTAGE },
117 { "volt12", SENSOR_TYPE_VOLTAGE },
118 { "volt5", SENSOR_TYPE_VOLTAGE },
119 { "vrm", SENSOR_TYPE_VOLTAGE },
120 { "5.0V", SENSOR_TYPE_VOLTAGE },
121 { "5V", SENSOR_TYPE_VOLTAGE },
122 { "3.3V", SENSOR_TYPE_VOLTAGE },
123 { "2.5V", SENSOR_TYPE_VOLTAGE },
124 { "2.0V", SENSOR_TYPE_VOLTAGE },
125 { "12V", SENSOR_TYPE_VOLTAGE }
127 static int known_features_num = STATIC_ARRAY_SIZE (known_features);
129 #endif /* SENSORS_API_VERSION < 0x400 */
131 static const char *config_keys[] =
136 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
138 #if SENSORS_API_VERSION < 0x400
139 typedef struct featurelist
141 const sensors_chip_name *chip;
142 const sensors_feature_data *data;
144 struct featurelist *next;
147 # ifndef SENSORS_CONF_PATH
148 # define SENSORS_CONF_PATH "/etc/sensors.conf"
150 /* #endif SENSORS_API_VERSION < 0x400 */
152 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
153 typedef struct featurelist
155 const sensors_chip_name *chip;
156 const sensors_feature *feature;
157 const sensors_subfeature *subfeature;
158 struct featurelist *next;
161 # ifndef SENSORS_CONF_PATH
162 # define SENSORS_CONF_PATH "/etc/sensors3.conf"
164 /* #endif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
166 #else /* if SENSORS_API_VERSION >= 0x500 */
167 # error "This version of libsensors is not supported yet. Please report this " \
171 static const char *conffile = SENSORS_CONF_PATH;
172 featurelist_t *first_feature = NULL;
173 static ignorelist_t *sensor_list;
174 static time_t sensors_config_mtime = 0;
176 #if SENSORS_API_VERSION < 0x400
177 /* full chip name logic borrowed from lm_sensors */
178 static int sensors_snprintf_chip_name (char *buf, size_t buf_size,
179 const sensors_chip_name *chip)
183 if (chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
185 status = snprintf (buf, buf_size,
190 else if (chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
192 snprintf (buf, buf_size, "%s-%s-%04x",
199 snprintf (buf, buf_size, "%s-i2c-%d-%02x",
206 } /* int sensors_snprintf_chip_name */
208 static int sensors_feature_name_to_type (const char *name)
212 /* Yes, this is slow, but it's only ever done during initialization, so
213 * it's a one time cost.. */
214 for (i = 0; i < known_features_num; i++)
215 if (strcasecmp (known_features[i].label, name) == 0)
216 return (known_features[i].type);
218 return (SENSOR_TYPE_UNKNOWN);
219 } /* int sensors_feature_name_to_type */
222 static int sensors_config (const char *key, const char *value)
224 if (sensor_list == NULL)
225 sensor_list = ignorelist_create (1);
227 if (strcasecmp (key, "Sensor") == 0)
229 if (ignorelist_add (sensor_list, value))
231 ERROR ("sensors plugin: "
232 "Cannot add value to ignorelist.");
236 else if (strcasecmp (key, "IgnoreSelected") == 0)
238 ignorelist_set_invert (sensor_list, 1);
239 if ((strcasecmp (value, "True") == 0)
240 || (strcasecmp (value, "Yes") == 0)
241 || (strcasecmp (value, "On") == 0))
242 ignorelist_set_invert (sensor_list, 0);
252 void sensors_free_features (void)
254 featurelist_t *thisft;
255 featurelist_t *nextft;
257 if (first_feature == NULL)
262 for (thisft = first_feature; thisft != NULL; thisft = nextft)
264 nextft = thisft->next;
267 first_feature = NULL;
270 static int sensors_load_conf (void)
273 featurelist_t *last_feature = NULL;
275 const sensors_chip_name *chip;
281 status = stat (conffile, &statbuf);
285 ERROR ("sensors plugin: stat (%s) failed: %s", conffile,
286 sstrerror (errno, errbuf, sizeof (errbuf)));
287 sensors_config_mtime = 0;
290 if ((sensors_config_mtime != 0)
291 && (sensors_config_mtime == statbuf.st_mtime))
294 if (sensors_config_mtime != 0)
296 NOTICE ("sensors plugin: Reloading config from %s",
298 sensors_free_features ();
299 sensors_config_mtime = 0;
302 fh = fopen (conffile, "r");
306 ERROR ("sensors plugin: fopen(%s) failed: %s", conffile,
307 sstrerror (errno, errbuf, sizeof (errbuf)));
311 status = sensors_init (fh);
315 ERROR ("sensors plugin: Cannot initialize sensors. "
316 "Data will not be collected.");
320 sensors_config_mtime = statbuf.st_mtime;
322 #if SENSORS_API_VERSION < 0x400
324 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
326 int feature_num0 = 0;
327 int feature_num1 = 0;
331 const sensors_feature_data *feature;
335 feature = sensors_get_all_features (*chip,
336 &feature_num0, &feature_num1);
338 /* Check if all features have been read. */
342 /* "master features" only */
343 if (feature->mapping != SENSORS_NO_MAPPING)
346 /* skip ignored in sensors.conf */
347 if (sensors_get_ignored (*chip, feature->number) == 0)
350 feature_type = sensors_feature_name_to_type (
352 if (feature_type == SENSOR_TYPE_UNKNOWN)
355 fl = (featurelist_t *) malloc (sizeof (featurelist_t));
358 ERROR ("sensors plugin: malloc failed.");
361 memset (fl, '\0', sizeof (featurelist_t));
365 fl->type = feature_type;
367 if (first_feature == NULL)
370 last_feature->next = fl;
372 } /* while sensors_get_all_features */
373 } /* while sensors_get_detected_chips */
374 /* #endif SENSORS_API_VERSION < 0x400 */
376 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
378 while ((chip = sensors_get_detected_chips (NULL, &chip_num)) != NULL)
380 const sensors_feature *feature;
383 while ((feature = sensors_get_features (chip, &feature_num)) != NULL)
385 const sensors_subfeature *subfeature;
386 int subfeature_num = 0;
388 /* Only handle voltage, fanspeeds and temperatures */
389 if ((feature->type != SENSORS_FEATURE_IN)
390 && (feature->type != SENSORS_FEATURE_FAN)
391 && (feature->type != SENSORS_FEATURE_TEMP))
394 while ((subfeature = sensors_get_all_subfeatures (chip,
395 feature, &subfeature_num)) != NULL)
399 if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT)
400 && (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT)
401 && (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT))
404 fl = (featurelist_t *) malloc (sizeof (featurelist_t));
407 ERROR ("sensors plugin: malloc failed.");
410 memset (fl, '\0', sizeof (featurelist_t));
413 fl->feature = feature;
414 fl->subfeature = subfeature;
416 if (first_feature == NULL)
419 last_feature->next = fl;
421 } /* while (subfeature) */
422 } /* while (feature) */
424 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
426 if (first_feature == NULL)
429 INFO ("sensors plugin: lm_sensors reports no "
430 "features. Data will not be collected.");
435 } /* int sensors_load_conf */
437 static int sensors_shutdown (void)
439 sensors_free_features ();
440 ignorelist_free (sensor_list);
443 } /* int sensors_shutdown */
445 static void sensors_submit (const char *plugin_instance,
446 const char *type, const char *type_instance,
449 char match_key[1024];
453 value_list_t vl = VALUE_LIST_INIT;
455 status = snprintf (match_key, sizeof (match_key), "%s/%s-%s",
456 plugin_instance, type, type_instance);
459 match_key[sizeof (match_key) - 1] = '\0';
461 if (sensor_list != NULL)
463 DEBUG ("sensors plugin: Checking ignorelist for `%s'", match_key);
464 if (ignorelist_match (sensor_list, match_key))
468 values[0].gauge = val;
472 vl.time = time (NULL);
474 strncpy (vl.host, hostname_g, sizeof (vl.host));
475 vl.host[sizeof (vl.host) - 1] = '\0';
476 strncpy (vl.plugin, "sensors", sizeof (vl.plugin));
477 vl.plugin[sizeof (vl.plugin) - 1] = '\0';
478 strncpy (vl.plugin_instance, plugin_instance,
479 sizeof (vl.plugin_instance));
480 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
481 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
482 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
484 plugin_dispatch_values (type, &vl);
485 } /* void sensors_submit */
487 static int sensors_read (void)
491 if (sensors_load_conf () != 0)
494 #if SENSORS_API_VERSION < 0x400
495 for (fl = first_feature; fl != NULL; fl = fl->next)
499 char plugin_instance[DATA_MAX_NAME_LEN];
500 char type_instance[DATA_MAX_NAME_LEN];
502 status = sensors_get_feature (*fl->chip,
503 fl->data->number, &value);
507 status = sensors_snprintf_chip_name (plugin_instance,
508 sizeof (plugin_instance), fl->chip);
511 plugin_instance[sizeof (plugin_instance) - 1] = '\0';
513 strncpy (type_instance, fl->data->name,
514 sizeof (type_instance));
515 type_instance[sizeof (type_instance) - 1] = '\0';
517 sensors_submit (plugin_instance,
518 sensor_type_name_map[fl->type],
521 } /* for fl = first_feature .. NULL */
522 /* #endif SENSORS_API_VERSION < 0x400 */
524 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
525 for (fl = first_feature; fl != NULL; fl = fl->next)
529 char plugin_instance[DATA_MAX_NAME_LEN];
530 char type_instance[DATA_MAX_NAME_LEN];
533 status = sensors_get_value (fl->chip,
534 fl->subfeature->number, &value);
538 status = sensors_snprintf_chip_name (plugin_instance,
539 sizeof (plugin_instance), fl->chip);
542 plugin_instance[sizeof (plugin_instance) - 1] = '\0';
544 strncpy (type_instance, fl->feature->name,
545 sizeof (type_instance));
546 type_instance[sizeof (type_instance) - 1] = '\0';
548 if (fl->feature->type == SENSORS_FEATURE_IN)
550 else if (fl->feature->type
551 == SENSORS_FEATURE_FAN)
553 else if (fl->feature->type
554 == SENSORS_FEATURE_TEMP)
559 sensors_submit (plugin_instance, type, type_instance, value);
560 } /* for fl = first_feature .. NULL */
561 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
564 } /* int sensors_read */
566 void module_register (void)
568 plugin_register_config ("sensors", sensors_config,
569 config_keys, config_keys_num);
570 plugin_register_read ("sensors", sensors_read);
571 plugin_register_shutdown ("sensors", sensors_shutdown);
572 } /* void module_register */