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 #define SENSOR_TYPE_VOLTAGE 0
42 #define SENSOR_TYPE_FANSPEED 1
43 #define SENSOR_TYPE_TEMPERATURE 2
44 #define SENSOR_TYPE_UNKNOWN 3
46 static char *sensor_to_type[] =
54 struct sensors_labeltypes_s
59 typedef struct sensors_labeltypes_s sensors_labeltypes_t;
62 * finite list of known labels extracted from lm_sensors
64 static sensors_labeltypes_t known_features[] =
66 { "fan1", SENSOR_TYPE_FANSPEED },
67 { "fan2", SENSOR_TYPE_FANSPEED },
68 { "fan3", SENSOR_TYPE_FANSPEED },
69 { "fan4", SENSOR_TYPE_FANSPEED },
70 { "fan5", SENSOR_TYPE_FANSPEED },
71 { "fan6", SENSOR_TYPE_FANSPEED },
72 { "fan7", SENSOR_TYPE_FANSPEED },
73 { "AIN2", SENSOR_TYPE_VOLTAGE },
74 { "AIN1", SENSOR_TYPE_VOLTAGE },
75 { "in10", SENSOR_TYPE_VOLTAGE },
76 { "in9", SENSOR_TYPE_VOLTAGE },
77 { "in8", SENSOR_TYPE_VOLTAGE },
78 { "in7", SENSOR_TYPE_VOLTAGE },
79 { "in6", SENSOR_TYPE_VOLTAGE },
80 { "in5", SENSOR_TYPE_VOLTAGE },
81 { "in4", SENSOR_TYPE_VOLTAGE },
82 { "in3", SENSOR_TYPE_VOLTAGE },
83 { "in2", SENSOR_TYPE_VOLTAGE },
84 { "in0", SENSOR_TYPE_VOLTAGE },
85 { "CPU_Temp", SENSOR_TYPE_TEMPERATURE },
86 { "remote_temp", SENSOR_TYPE_TEMPERATURE },
87 { "temp1", SENSOR_TYPE_TEMPERATURE },
88 { "temp2", SENSOR_TYPE_TEMPERATURE },
89 { "temp3", SENSOR_TYPE_TEMPERATURE },
90 { "temp4", SENSOR_TYPE_TEMPERATURE },
91 { "temp5", SENSOR_TYPE_TEMPERATURE },
92 { "temp6", SENSOR_TYPE_TEMPERATURE },
93 { "temp7", SENSOR_TYPE_TEMPERATURE },
94 { "temp", SENSOR_TYPE_TEMPERATURE },
95 { "Vccp2", SENSOR_TYPE_VOLTAGE },
96 { "Vccp1", SENSOR_TYPE_VOLTAGE },
97 { "vdd", SENSOR_TYPE_VOLTAGE },
98 { "vid5", SENSOR_TYPE_VOLTAGE },
99 { "vid4", SENSOR_TYPE_VOLTAGE },
100 { "vid3", SENSOR_TYPE_VOLTAGE },
101 { "vid2", SENSOR_TYPE_VOLTAGE },
102 { "vid1", SENSOR_TYPE_VOLTAGE },
103 { "vid", SENSOR_TYPE_VOLTAGE },
104 { "vin4", SENSOR_TYPE_VOLTAGE },
105 { "vin3", SENSOR_TYPE_VOLTAGE },
106 { "vin2", SENSOR_TYPE_VOLTAGE },
107 { "vin1", SENSOR_TYPE_VOLTAGE },
108 { "voltbatt", SENSOR_TYPE_VOLTAGE },
109 { "volt12", SENSOR_TYPE_VOLTAGE },
110 { "volt5", SENSOR_TYPE_VOLTAGE },
111 { "vrm", SENSOR_TYPE_VOLTAGE },
112 { "5.0V", SENSOR_TYPE_VOLTAGE },
113 { "5V", SENSOR_TYPE_VOLTAGE },
114 { "3.3V", SENSOR_TYPE_VOLTAGE },
115 { "2.5V", SENSOR_TYPE_VOLTAGE },
116 { "2.0V", SENSOR_TYPE_VOLTAGE },
117 { "12V", SENSOR_TYPE_VOLTAGE }
119 static int known_features_num = STATIC_ARRAY_SIZE (known_features);
122 static const char *config_keys[] =
128 static int config_keys_num = 2;
130 static ignorelist_t *sensor_list;
132 #ifndef SENSORS_CONF_PATH
133 # define SENSORS_CONF_PATH "/etc/sensors.conf"
136 static const char *conffile = SENSORS_CONF_PATH;
137 /* SENSORS_CONF_PATH */
140 * remember stat of the loaded config
142 static time_t sensors_config_mtime = 0;
144 typedef struct featurelist
146 const sensors_chip_name *chip;
147 const sensors_feature_data *data;
149 struct featurelist *next;
152 featurelist_t *first_feature = NULL;
154 static int sensors_config (const char *key, const char *value)
156 if (sensor_list == NULL)
157 sensor_list = ignorelist_create (1);
159 if (strcasecmp (key, "Sensor") == 0)
161 if (ignorelist_add (sensor_list, value))
163 ERROR ("sensors plugin: "
164 "Cannot add value to ignorelist.");
168 else if (strcasecmp (key, "IgnoreSelected") == 0)
170 ignorelist_set_invert (sensor_list, 1);
171 if ((strcasecmp (value, "True") == 0)
172 || (strcasecmp (value, "Yes") == 0)
173 || (strcasecmp (value, "On") == 0))
174 ignorelist_set_invert (sensor_list, 0);
184 void sensors_free_features (void)
186 featurelist_t *thisft;
187 featurelist_t *nextft;
189 if (first_feature == NULL)
194 for (thisft = first_feature; thisft != NULL; thisft = nextft)
196 nextft = thisft->next;
199 first_feature = NULL;
202 static int sensors_load_conf (void)
205 featurelist_t *last_feature = NULL;
206 featurelist_t *new_feature = NULL;
208 const sensors_chip_name *chip;
211 const sensors_feature_data *data;
212 int data_num0, data_num1;
217 status = stat (conffile, &statbuf);
221 ERROR ("sensors plugin: stat (%s) failed: %s", conffile,
222 sstrerror (errno, errbuf, sizeof (errbuf)));
223 sensors_config_mtime = 0;
226 if ((sensors_config_mtime != 0)
227 && (sensors_config_mtime == statbuf.st_mtime))
230 if (sensors_config_mtime != 0)
232 NOTICE ("sensors plugin: Reloading config from %s",
234 sensors_free_features ();
235 sensors_config_mtime = 0;
238 fh = fopen (conffile, "r");
242 ERROR ("sensors plugin: fopen(%s) failed: %s", conffile,
243 sstrerror (errno, errbuf, sizeof (errbuf)));
247 status = sensors_init (fh);
251 ERROR ("sensors plugin: Cannot initialize sensors. "
252 "Data will not be collected.");
256 sensors_config_mtime = statbuf.st_mtime;
259 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
262 data_num0 = data_num1 = 0;
264 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1))
269 /* "master features" only */
270 if (data->mapping != SENSORS_NO_MAPPING)
273 /* Only known features */
274 for (i = 0; i < known_features_num; i++)
276 if (strcmp (data->name, known_features[i].label) != 0)
279 /* skip ignored in sensors.conf */
280 if (sensors_get_ignored (*chip, data->number) == 0)
283 DEBUG ("Adding feature: %s-%s-%s",
285 sensor_to_type[known_features[i].type],
288 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
291 ERROR ("sensors plugin: malloc: %s",
292 sstrerror (errno, errbuf, sizeof (errbuf)));
296 new_feature->chip = chip;
297 new_feature->data = data;
298 new_feature->type = known_features[i].type;
299 new_feature->next = NULL;
301 if (first_feature == NULL)
303 first_feature = new_feature;
304 last_feature = new_feature;
308 last_feature->next = new_feature;
309 last_feature = new_feature;
312 /* stop searching known features at first found */
315 } /* while sensors_get_all_features */
316 } /* while sensors_get_detected_chips */
318 if (first_feature == NULL)
321 INFO ("sensors plugin: lm_sensors reports no "
322 "features. Data will not be collected.");
327 } /* int sensors_load_conf */
329 static int sensors_shutdown (void)
331 sensors_free_features ();
332 ignorelist_free (sensor_list);
335 } /* int sensors_shutdown */
337 static void sensors_submit (const char *plugin_instance,
338 const char *type, const char *type_instance,
342 value_list_t vl = VALUE_LIST_INIT;
344 if ((sensor_list != NULL)
345 && (ignorelist_match (sensor_list, type_instance)))
348 values[0].gauge = val;
352 vl.time = time (NULL);
353 strcpy (vl.host, hostname_g);
354 strcpy (vl.plugin, "sensors");
355 strcpy (vl.plugin_instance, plugin_instance);
356 strcpy (vl.type_instance, type_instance);
358 plugin_dispatch_values (type, &vl);
359 } /* void sensors_submit */
361 static int sensors_read (void)
363 featurelist_t *feature;
366 char plugin_instance[DATA_MAX_NAME_LEN];
367 char type_instance[DATA_MAX_NAME_LEN];
369 if (sensors_load_conf () != 0)
372 for (feature = first_feature; feature != NULL; feature = feature->next)
374 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
377 /* full chip name logic borrowed from lm_sensors */
378 if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
380 if (snprintf (plugin_instance, DATA_MAX_NAME_LEN, "%s-isa-%04x",
381 feature->chip->prefix,
386 else if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
388 if (snprintf (plugin_instance, 512, "%s-%s-%04x",
389 feature->chip->prefix,
390 feature->chip->busname,
397 if (snprintf (plugin_instance, 512, "%s-i2c-%d-%02x",
398 feature->chip->prefix,
405 strncpy (type_instance, feature->data->name, DATA_MAX_NAME_LEN);
407 sensors_submit (plugin_instance,
408 sensor_to_type[feature->type],
411 } /* for feature = first_feature .. NULL */
414 } /* int sensors_read */
416 void module_register (void)
418 plugin_register_config ("sensors", sensors_config,
419 config_keys, config_keys_num);
420 plugin_register_read ("sensors", sensors_read);
421 plugin_register_shutdown ("sensors", sensors_shutdown);
422 } /* void module_register */