14d8abd1eb1fd2eee36a111a4f8ac2ff0ce4762f
1 /**
2 * collectd - src/sensors.c
3 * Copyright (C) 2005,2006 Florian octo Forster
4 *
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; either version 2 of the License, or (at your
8 * option) any later version.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 *
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
30 **/
34 #include "collectd.h"
35 #include "common.h"
36 #include "plugin.h"
37 #include "configfile.h"
38 #include "utils_ignorelist.h"
39 #include "utils_debug.h"
41 /*
42 * This weird macro cascade forces the glibc to define `NAN'. I don't know
43 * another way to solve this, so more intelligent solutions are welcome. -octo
44 */
45 #ifndef __USE_ISOC99
46 # define DISABLE__USE_ISOC99 1
47 # define __USE_ISOC99 1
48 #endif
49 #include <math.h>
50 #ifdef DISABLE__USE_ISOC99
51 # undef DISABLE__USE_ISOC99
52 # undef __USE_ISOC99
53 #endif
55 #if defined(HAVE_SENSORS_SENSORS_H)
56 # include <sensors/sensors.h>
57 #else
58 # undef HAVE_LIBSENSORS
59 #endif
61 #if defined(HAVE_LIBSENSORS)
62 # define SENSORS_HAVE_READ 1
63 #else
64 # define SENSORS_HAVE_READ 0
65 #endif
67 static data_source_t data_source[1] =
68 {
69 {"value", DS_TYPE_GAUGE, NAN, NAN}
70 };
71 static data_set_t fanspeed_ds =
72 {
73 "fanspeed", 1, data_source
74 };
76 static data_set_t temperature_ds =
77 {
78 "temperature", 1, data_source
79 };
81 static data_set_t voltage_ds =
82 {
83 "voltage", 1, data_source
84 };
86 #if SENSORS_HAVE_READ
87 #define SENSOR_TYPE_VOLTAGE 0
88 #define SENSOR_TYPE_FANSPEED 1
89 #define SENSOR_TYPE_TEMPERATURE 2
90 #define SENSOR_TYPE_UNKNOWN 3
92 static char *sensor_to_type[] =
93 {
94 "voltage",
95 "fanspeed",
96 "temperature",
97 NULL
98 };
100 struct sensors_labeltypes_s
101 {
102 char *label;
103 int type;
104 };
105 typedef struct sensors_labeltypes_s sensors_labeltypes_t;
107 /*
108 * finite list of known labels extracted from lm_sensors
109 */
110 static sensors_labeltypes_t known_features[] =
111 {
112 { "fan1", SENSOR_TYPE_FANSPEED },
113 { "fan2", SENSOR_TYPE_FANSPEED },
114 { "fan3", SENSOR_TYPE_FANSPEED },
115 { "fan4", SENSOR_TYPE_FANSPEED },
116 { "fan5", SENSOR_TYPE_FANSPEED },
117 { "fan6", SENSOR_TYPE_FANSPEED },
118 { "fan7", SENSOR_TYPE_FANSPEED },
119 { "AIN2", SENSOR_TYPE_VOLTAGE },
120 { "AIN1", SENSOR_TYPE_VOLTAGE },
121 { "in10", SENSOR_TYPE_VOLTAGE },
122 { "in9", SENSOR_TYPE_VOLTAGE },
123 { "in8", SENSOR_TYPE_VOLTAGE },
124 { "in7", SENSOR_TYPE_VOLTAGE },
125 { "in6", SENSOR_TYPE_VOLTAGE },
126 { "in5", SENSOR_TYPE_VOLTAGE },
127 { "in4", SENSOR_TYPE_VOLTAGE },
128 { "in3", SENSOR_TYPE_VOLTAGE },
129 { "in2", SENSOR_TYPE_VOLTAGE },
130 { "in0", SENSOR_TYPE_VOLTAGE },
131 { "CPU_Temp", SENSOR_TYPE_TEMPERATURE },
132 { "remote_temp", SENSOR_TYPE_TEMPERATURE },
133 { "temp1", SENSOR_TYPE_TEMPERATURE },
134 { "temp2", SENSOR_TYPE_TEMPERATURE },
135 { "temp3", SENSOR_TYPE_TEMPERATURE },
136 { "temp4", SENSOR_TYPE_TEMPERATURE },
137 { "temp5", SENSOR_TYPE_TEMPERATURE },
138 { "temp6", SENSOR_TYPE_TEMPERATURE },
139 { "temp7", SENSOR_TYPE_TEMPERATURE },
140 { "temp", SENSOR_TYPE_TEMPERATURE },
141 { "Vccp2", SENSOR_TYPE_VOLTAGE },
142 { "Vccp1", SENSOR_TYPE_VOLTAGE },
143 { "vdd", SENSOR_TYPE_VOLTAGE },
144 { "vid5", SENSOR_TYPE_VOLTAGE },
145 { "vid4", SENSOR_TYPE_VOLTAGE },
146 { "vid3", SENSOR_TYPE_VOLTAGE },
147 { "vid2", SENSOR_TYPE_VOLTAGE },
148 { "vid1", SENSOR_TYPE_VOLTAGE },
149 { "vid", SENSOR_TYPE_VOLTAGE },
150 { "vin4", SENSOR_TYPE_VOLTAGE },
151 { "vin3", SENSOR_TYPE_VOLTAGE },
152 { "vin2", SENSOR_TYPE_VOLTAGE },
153 { "vin1", SENSOR_TYPE_VOLTAGE },
154 { "voltbatt", SENSOR_TYPE_VOLTAGE },
155 { "volt12", SENSOR_TYPE_VOLTAGE },
156 { "volt5", SENSOR_TYPE_VOLTAGE },
157 { "vrm", SENSOR_TYPE_VOLTAGE },
158 { "5.0V", SENSOR_TYPE_VOLTAGE },
159 { "5V", SENSOR_TYPE_VOLTAGE },
160 { "3.3V", SENSOR_TYPE_VOLTAGE },
161 { "2.5V", SENSOR_TYPE_VOLTAGE },
162 { "2.0V", SENSOR_TYPE_VOLTAGE },
163 { "12V", SENSOR_TYPE_VOLTAGE },
164 { (char *) 0, SENSOR_TYPE_UNKNOWN }
165 };
166 /* end new naming */
168 static const char *config_keys[] =
169 {
170 "Sensor",
171 "IgnoreSelected",
172 NULL
173 };
174 static int config_keys_num = 2;
176 static ignorelist_t *sensor_list;
178 #ifndef SENSORS_CONF_PATH
179 # define SENSORS_CONF_PATH "/etc/sensors.conf"
180 #endif
182 static const char *conffile = SENSORS_CONF_PATH;
183 /* SENSORS_CONF_PATH */
185 /*
186 * remember stat of the loaded config
187 */
188 static time_t sensors_config_mtime = 0;
190 typedef struct featurelist
191 {
192 const sensors_chip_name *chip;
193 const sensors_feature_data *data;
194 int type;
195 struct featurelist *next;
196 } featurelist_t;
198 featurelist_t *first_feature = NULL;
200 static int sensors_config (const char *key, const char *value)
201 {
202 if (sensor_list == NULL)
203 sensor_list = ignorelist_create (1);
205 if (strcasecmp (key, "Sensor") == 0)
206 {
207 if (ignorelist_add (sensor_list, value))
208 {
209 syslog (LOG_ERR, "sensors plugin: "
210 "Cannot add value to ignorelist.");
211 return (1);
212 }
213 }
214 else if (strcasecmp (key, "IgnoreSelected") == 0)
215 {
216 ignorelist_set_invert (sensor_list, 1);
217 if ((strcasecmp (value, "True") == 0)
218 || (strcasecmp (value, "Yes") == 0)
219 || (strcasecmp (value, "On") == 0))
220 ignorelist_set_invert (sensor_list, 0);
221 }
222 else
223 {
224 return (-1);
225 }
227 return (0);
228 }
230 void sensors_free_features (void)
231 {
232 featurelist_t *thisft;
233 featurelist_t *nextft;
235 if (first_feature == NULL)
236 return;
238 sensors_cleanup ();
240 for (thisft = first_feature; thisft != NULL; thisft = nextft)
241 {
242 nextft = thisft->next;
243 sfree (thisft);
244 }
245 first_feature = NULL;
246 }
248 static void sensors_load_conf (void)
249 {
250 FILE *fh;
251 featurelist_t *last_feature = NULL;
252 featurelist_t *new_feature = NULL;
254 const sensors_chip_name *chip;
255 int chip_num;
257 const sensors_feature_data *data;
258 int data_num0, data_num1;
260 struct stat statbuf;
261 int status;
263 status = stat (conffile, &statbuf);
264 if (status != 0)
265 {
266 syslog (LOG_ERR, "sensors plugin: stat (%s) failed: %s",
267 conffile, strerror (errno));
268 sensors_config_mtime = 0;
269 }
271 if ((sensors_config_mtime != 0)
272 && (sensors_config_mtime == statbuf.st_mtime))
273 return;
275 if (sensors_config_mtime != 0)
276 {
277 syslog (LOG_NOTICE, "sensors plugin: Reloading config from %s",
278 conffile);
279 sensors_free_features ();
280 sensors_config_mtime = 0;
281 }
283 fh = fopen (conffile, "r");
284 if (fh == NULL)
285 {
286 syslog (LOG_ERR, "sensors plugin: fopen(%s) failed: %s",
287 conffile, strerror(errno));
288 return;
289 }
291 status = sensors_init (fh);
292 fclose (fh);
293 if (status != 0)
294 {
295 syslog (LOG_ERR, "sensors plugin: Cannot initialize sensors. "
296 "Data will not be collected.");
297 return;
298 }
300 sensors_config_mtime = statbuf.st_mtime;
302 chip_num = 0;
303 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
304 {
305 data = NULL;
306 data_num0 = data_num1 = 0;
308 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1))
309 != NULL)
310 {
311 int i;
313 /* "master features" only */
314 if (data->mapping != SENSORS_NO_MAPPING)
315 continue;
317 /* Only known features */
318 for (i = 0; known_features[i].type >= 0; i++)
319 {
320 if (strcmp (data->name, known_features[i].label) != 0)
321 continue;
323 /* skip ignored in sensors.conf */
324 if (sensors_get_ignored (*chip, data->number) == 0)
325 break;
327 DBG ("Adding feature: %s-%s-%s",
328 chip->prefix,
329 sensor_type_prefix[known_features[i].type],
330 data->name);
332 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
333 {
334 DBG ("malloc: %s", strerror (errno));
335 syslog (LOG_ERR, "sensors plugin: malloc: %s",
336 strerror (errno));
337 break;
338 }
340 new_feature->chip = chip;
341 new_feature->data = data;
342 new_feature->type = known_features[i].type;
343 new_feature->next = NULL;
345 if (first_feature == NULL)
346 {
347 first_feature = new_feature;
348 last_feature = new_feature;
349 }
350 else
351 {
352 last_feature->next = new_feature;
353 last_feature = new_feature;
354 }
356 /* stop searching known features at first found */
357 break;
358 } /* for i */
359 } /* while sensors_get_all_features */
360 } /* while sensors_get_detected_chips */
362 if (first_feature == NULL)
363 {
364 sensors_cleanup ();
365 syslog (LOG_INFO, "sensors plugin: lm_sensors reports no "
366 "features. Data will not be collected.");
367 }
368 } /* void sensors_load_conf */
370 static int sensors_shutdown (void)
371 {
372 sensors_free_features ();
373 ignorelist_free (sensor_list);
375 return (0);
376 } /* int sensors_shutdown */
378 static void sensors_submit (const char *plugin_instance,
379 const char *type, const char *type_instance,
380 double val)
381 {
382 value_t values[1];
383 value_list_t vl = VALUE_LIST_INIT;
385 if (ignorelist_match (sensor_list, type_instance))
386 return;
388 values[0].gauge = val;
390 vl.values = values;
391 vl.values_len = 1;
392 vl.time = time (NULL);
393 strcpy (vl.host, hostname);
394 strcpy (vl.plugin, "sensors");
395 strcpy (vl.plugin_instance, plugin_instance);
396 strcpy (vl.type_instance, type_instance);
398 plugin_dispatch_values (type, &vl);
399 } /* void sensors_submit */
401 static int sensors_read (void)
402 {
403 featurelist_t *feature;
404 double value;
405 char chip_fullprefix[512];
407 char plugin_instance[DATA_MAX_NAME_LEN];
408 char type_instance[DATA_MAX_NAME_LEN];
410 sensors_load_conf ();
412 for (feature = first_feature; feature != NULL; feature = feature->next)
413 {
414 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
415 continue;
417 /* full chip name logic borrowed from lm_sensors */
418 if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
419 {
420 if (snprintf (plugin_instance, DATA_MAX_NAME_LEN, "%s-isa-%04x",
421 feature->chip->prefix,
422 feature->chip->addr)
423 >= 512)
424 continue;
425 }
426 else if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
427 {
428 if (snprintf (plugin_instance, 512, "%s-%s-%04x",
429 feature->chip->prefix,
430 feature->chip->busname,
431 feature->chip->addr)
432 >= 512)
433 continue;
434 }
435 else
436 {
437 if (snprintf (plugin_instance, 512, "%s-i2c-%d-%02x",
438 feature->chip->prefix,
439 feature->chip->bus,
440 feature->chip->addr)
441 >= 512)
442 continue;
443 }
445 strncpy (type_instance, feature->data->name, DATA_MAX_NAME_LEN);
447 sensors_submit (plugin_instance,
448 sensor_to_type[feature->type]
449 type_instance,
450 value);
451 } /* for feature = first_feature .. NULL */
452 } /* int sensors_read */
453 #endif /* SENSORS_HAVE_READ */
455 void module_register (void)
456 {
457 plugin_register_data_set (&fanspeed_ds);
458 plugin_register_data_set (&temperature_ds);
459 plugin_register_data_set (&voltage_ds);
461 #if SENSORS_HAVE_READ
462 plugin_register_config ("sensors", sensors_config,
463 config_keys, config_keys_num);
464 plugin_register_read ("sensors", sensors_read);
465 plugin_register_shutdown ("sensors", sensors_shutdown);
466 #endif
467 } /* void module_register */