1 /**
2 * collectd - src/sensors.c
3 * Copyright (C) 2005-2008 Florian octo Forster
4 * Copyright (C) 2006 Luboš Staněk
5 *
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.
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 collectd.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 *
31 * Henrique de Moraes Holschuh <hmh at debian.org>
32 * - use default libsensors config file on API 0x400
33 * - config SensorConfigFile option
34 **/
36 #include "collectd.h"
37 #include "common.h"
38 #include "plugin.h"
39 #include "configfile.h"
40 #include "utils_ignorelist.h"
42 #if defined(HAVE_SENSORS_SENSORS_H)
43 # include <sensors/sensors.h>
44 #endif
46 #if !defined(SENSORS_API_VERSION)
47 # define SENSORS_API_VERSION 0x000
48 #endif
50 /*
51 * The sensors library prior to version 3.0 (internal version 0x400) didn't
52 * report the type of values, only a name. The following lists are there to
53 * convert from the names to the type. They are not used with the new
54 * interface.
55 */
56 #if SENSORS_API_VERSION < 0x400
57 static char *sensor_type_name_map[] =
58 {
59 # define SENSOR_TYPE_VOLTAGE 0
60 "voltage",
61 # define SENSOR_TYPE_FANSPEED 1
62 "fanspeed",
63 # define SENSOR_TYPE_TEMPERATURE 2
64 "temperature",
65 # define SENSOR_TYPE_POWER 3
66 "power",
67 # define SENSOR_TYPE_UNKNOWN 4
68 NULL
69 };
71 struct sensors_labeltypes_s
72 {
73 char *label;
74 int type;
75 };
76 typedef struct sensors_labeltypes_s sensors_labeltypes_t;
78 /* finite list of known labels extracted from lm_sensors */
79 static sensors_labeltypes_t known_features[] =
80 {
81 { "fan1", SENSOR_TYPE_FANSPEED },
82 { "fan2", SENSOR_TYPE_FANSPEED },
83 { "fan3", SENSOR_TYPE_FANSPEED },
84 { "fan4", SENSOR_TYPE_FANSPEED },
85 { "fan5", SENSOR_TYPE_FANSPEED },
86 { "fan6", SENSOR_TYPE_FANSPEED },
87 { "fan7", SENSOR_TYPE_FANSPEED },
88 { "AIN2", SENSOR_TYPE_VOLTAGE },
89 { "AIN1", SENSOR_TYPE_VOLTAGE },
90 { "in10", SENSOR_TYPE_VOLTAGE },
91 { "in9", SENSOR_TYPE_VOLTAGE },
92 { "in8", SENSOR_TYPE_VOLTAGE },
93 { "in7", SENSOR_TYPE_VOLTAGE },
94 { "in6", SENSOR_TYPE_VOLTAGE },
95 { "in5", SENSOR_TYPE_VOLTAGE },
96 { "in4", SENSOR_TYPE_VOLTAGE },
97 { "in3", SENSOR_TYPE_VOLTAGE },
98 { "in2", SENSOR_TYPE_VOLTAGE },
99 { "in0", SENSOR_TYPE_VOLTAGE },
100 { "CPU_Temp", SENSOR_TYPE_TEMPERATURE },
101 { "remote_temp", SENSOR_TYPE_TEMPERATURE },
102 { "temp1", SENSOR_TYPE_TEMPERATURE },
103 { "temp2", SENSOR_TYPE_TEMPERATURE },
104 { "temp3", SENSOR_TYPE_TEMPERATURE },
105 { "temp4", SENSOR_TYPE_TEMPERATURE },
106 { "temp5", SENSOR_TYPE_TEMPERATURE },
107 { "temp6", SENSOR_TYPE_TEMPERATURE },
108 { "temp7", SENSOR_TYPE_TEMPERATURE },
109 { "temp", SENSOR_TYPE_TEMPERATURE },
110 { "Vccp2", SENSOR_TYPE_VOLTAGE },
111 { "Vccp1", SENSOR_TYPE_VOLTAGE },
112 { "vdd", SENSOR_TYPE_VOLTAGE },
113 { "vid5", SENSOR_TYPE_VOLTAGE },
114 { "vid4", SENSOR_TYPE_VOLTAGE },
115 { "vid3", SENSOR_TYPE_VOLTAGE },
116 { "vid2", SENSOR_TYPE_VOLTAGE },
117 { "vid1", SENSOR_TYPE_VOLTAGE },
118 { "vid", SENSOR_TYPE_VOLTAGE },
119 { "vin4", SENSOR_TYPE_VOLTAGE },
120 { "vin3", SENSOR_TYPE_VOLTAGE },
121 { "vin2", SENSOR_TYPE_VOLTAGE },
122 { "vin1", SENSOR_TYPE_VOLTAGE },
123 { "voltbatt", SENSOR_TYPE_VOLTAGE },
124 { "volt12", SENSOR_TYPE_VOLTAGE },
125 { "volt5", SENSOR_TYPE_VOLTAGE },
126 { "vrm", SENSOR_TYPE_VOLTAGE },
127 { "5.0V", SENSOR_TYPE_VOLTAGE },
128 { "5V", SENSOR_TYPE_VOLTAGE },
129 { "3.3V", SENSOR_TYPE_VOLTAGE },
130 { "2.5V", SENSOR_TYPE_VOLTAGE },
131 { "2.0V", SENSOR_TYPE_VOLTAGE },
132 { "12V", SENSOR_TYPE_VOLTAGE },
133 { "power1", SENSOR_TYPE_POWER }
134 };
135 static int known_features_num = STATIC_ARRAY_SIZE (known_features);
136 /* end new naming */
137 #endif /* SENSORS_API_VERSION < 0x400 */
139 static const char *config_keys[] =
140 {
141 "Sensor",
142 "IgnoreSelected",
143 "SensorConfigFile",
144 "UseLabels"
145 };
146 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
148 #if SENSORS_API_VERSION < 0x400
149 typedef struct featurelist
150 {
151 const sensors_chip_name *chip;
152 const sensors_feature_data *data;
153 int type;
154 struct featurelist *next;
155 } featurelist_t;
157 # ifndef SENSORS_CONF_PATH
158 # define SENSORS_CONF_PATH "/etc/sensors.conf"
159 # endif
160 static char *conffile = SENSORS_CONF_PATH;
161 /* #endif SENSORS_API_VERSION < 0x400 */
163 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
164 typedef struct featurelist
165 {
166 const sensors_chip_name *chip;
167 const sensors_feature *feature;
168 const sensors_subfeature *subfeature;
169 struct featurelist *next;
170 } featurelist_t;
172 static char *conffile = NULL;
173 static _Bool use_labels = 0;
174 /* #endif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
176 #else /* if SENSORS_API_VERSION >= 0x500 */
177 # error "This version of libsensors is not supported yet. Please report this " \
178 "as bug."
179 #endif
181 featurelist_t *first_feature = NULL;
182 static ignorelist_t *sensor_list;
184 #if SENSORS_API_VERSION < 0x400
185 /* full chip name logic borrowed from lm_sensors */
186 static int sensors_snprintf_chip_name (char *buf, size_t buf_size,
187 const sensors_chip_name *chip)
188 {
189 int status = -1;
191 if (chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
192 {
193 status = ssnprintf (buf, buf_size,
194 "%s-isa-%04x",
195 chip->prefix,
196 chip->addr);
197 }
198 else if (chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
199 {
200 status = snprintf (buf, buf_size, "%s-%s-%04x",
201 chip->prefix,
202 chip->busname,
203 chip->addr);
204 }
205 else
206 {
207 status = snprintf (buf, buf_size, "%s-i2c-%d-%02x",
208 chip->prefix,
209 chip->bus,
210 chip->addr);
211 }
213 return (status);
214 } /* int sensors_snprintf_chip_name */
216 static int sensors_feature_name_to_type (const char *name)
217 {
218 int i;
220 /* Yes, this is slow, but it's only ever done during initialization, so
221 * it's a one time cost.. */
222 for (i = 0; i < known_features_num; i++)
223 if (strcasecmp (known_features[i].label, name) == 0)
224 return (known_features[i].type);
226 return (SENSOR_TYPE_UNKNOWN);
227 } /* int sensors_feature_name_to_type */
228 #endif
230 static int sensors_config (const char *key, const char *value)
231 {
232 if (sensor_list == NULL)
233 sensor_list = ignorelist_create (1);
235 /* TODO: This setting exists for compatibility with old versions of
236 * lm-sensors. Remove support for those ancient versions in the next
237 * major release. */
238 if (strcasecmp (key, "SensorConfigFile") == 0)
239 {
240 char *tmp = strdup (value);
241 if (tmp != NULL)
242 {
243 sfree (conffile);
244 conffile = tmp;
245 }
246 }
247 else if (strcasecmp (key, "Sensor") == 0)
248 {
249 if (ignorelist_add (sensor_list, value))
250 {
251 ERROR ("sensors plugin: "
252 "Cannot add value to ignorelist.");
253 return (1);
254 }
255 }
256 else if (strcasecmp (key, "IgnoreSelected") == 0)
257 {
258 ignorelist_set_invert (sensor_list, 1);
259 if (IS_TRUE (value))
260 ignorelist_set_invert (sensor_list, 0);
261 }
262 #if (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
263 else if (strcasecmp (key, "UseLabels") == 0)
264 {
265 use_labels = IS_TRUE (value) ? 1 : 0;
266 }
267 #endif
268 else
269 {
270 return (-1);
271 }
273 return (0);
274 }
276 void sensors_free_features (void)
277 {
278 featurelist_t *thisft;
279 featurelist_t *nextft;
281 if (first_feature == NULL)
282 return;
284 sensors_cleanup ();
286 for (thisft = first_feature; thisft != NULL; thisft = nextft)
287 {
288 nextft = thisft->next;
289 sfree (thisft);
290 }
291 first_feature = NULL;
292 }
294 static int sensors_load_conf (void)
295 {
296 static int call_once = 0;
298 FILE *fh = NULL;
299 featurelist_t *last_feature = NULL;
301 const sensors_chip_name *chip;
302 int chip_num;
304 int status;
306 if (call_once)
307 return 0;
309 call_once = 1;
311 if (conffile != NULL)
312 {
313 fh = fopen (conffile, "r");
314 if (fh == NULL)
315 {
316 char errbuf[1024];
317 ERROR ("sensors plugin: fopen(%s) failed: %s", conffile,
318 sstrerror (errno, errbuf, sizeof (errbuf)));
319 return (-1);
320 }
321 }
323 status = sensors_init (fh);
324 if (fh)
325 fclose (fh);
327 if (status != 0)
328 {
329 ERROR ("sensors plugin: Cannot initialize sensors. "
330 "Data will not be collected.");
331 return (-1);
332 }
334 #if SENSORS_API_VERSION < 0x400
335 chip_num = 0;
336 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
337 {
338 int feature_num0 = 0;
339 int feature_num1 = 0;
341 while (42)
342 {
343 const sensors_feature_data *feature;
344 int feature_type;
345 featurelist_t *fl;
347 feature = sensors_get_all_features (*chip,
348 &feature_num0, &feature_num1);
350 /* Check if all features have been read. */
351 if (feature == NULL)
352 break;
354 /* "master features" only */
355 if (feature->mapping != SENSORS_NO_MAPPING)
356 {
357 DEBUG ("sensors plugin: sensors_load_conf: "
358 "Ignoring subfeature `%s', "
359 "because (feature->mapping "
360 "!= SENSORS_NO_MAPPING).",
361 feature->name);
362 continue;
363 }
365 /* skip ignored in sensors.conf */
366 if (sensors_get_ignored (*chip, feature->number) == 0)
367 {
368 DEBUG ("sensors plugin: sensors_load_conf: "
369 "Ignoring subfeature `%s', "
370 "because "
371 "`sensors_get_ignored' told "
372 "me so.",
373 feature->name);
374 continue;
375 }
377 feature_type = sensors_feature_name_to_type (
378 feature->name);
379 if (feature_type == SENSOR_TYPE_UNKNOWN)
380 {
381 DEBUG ("sensors plugin: sensors_load_conf: "
382 "Ignoring subfeature `%s', "
383 "because its type is "
384 "unknown.",
385 feature->name);
386 continue;
387 }
389 fl = (featurelist_t *) malloc (sizeof (featurelist_t));
390 if (fl == NULL)
391 {
392 ERROR ("sensors plugin: malloc failed.");
393 continue;
394 }
395 memset (fl, '\0', sizeof (featurelist_t));
397 fl->chip = chip;
398 fl->data = feature;
399 fl->type = feature_type;
401 if (first_feature == NULL)
402 first_feature = fl;
403 else
404 last_feature->next = fl;
405 last_feature = fl;
406 } /* while sensors_get_all_features */
407 } /* while sensors_get_detected_chips */
408 /* #endif SENSORS_API_VERSION < 0x400 */
410 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
411 chip_num = 0;
412 while ((chip = sensors_get_detected_chips (NULL, &chip_num)) != NULL)
413 {
414 const sensors_feature *feature;
415 int feature_num = 0;
417 while ((feature = sensors_get_features (chip, &feature_num)) != NULL)
418 {
419 const sensors_subfeature *subfeature;
420 int subfeature_num = 0;
422 /* Only handle voltage, fanspeeds and temperatures */
423 if ((feature->type != SENSORS_FEATURE_IN)
424 && (feature->type != SENSORS_FEATURE_FAN)
425 && (feature->type != SENSORS_FEATURE_TEMP)
426 && (feature->type != SENSORS_FEATURE_POWER))
427 {
428 DEBUG ("sensors plugin: sensors_load_conf: "
429 "Ignoring feature `%s', "
430 "because its type is not "
431 "supported.", feature->name);
432 continue;
433 }
435 while ((subfeature = sensors_get_all_subfeatures (chip,
436 feature, &subfeature_num)) != NULL)
437 {
438 featurelist_t *fl;
440 if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT)
441 && (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT)
442 && (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT)
443 && (subfeature->type != SENSORS_SUBFEATURE_POWER_INPUT))
444 continue;
446 fl = (featurelist_t *) malloc (sizeof (featurelist_t));
447 if (fl == NULL)
448 {
449 ERROR ("sensors plugin: malloc failed.");
450 continue;
451 }
452 memset (fl, '\0', sizeof (featurelist_t));
454 fl->chip = chip;
455 fl->feature = feature;
456 fl->subfeature = subfeature;
458 if (first_feature == NULL)
459 first_feature = fl;
460 else
461 last_feature->next = fl;
462 last_feature = fl;
463 } /* while (subfeature) */
464 } /* while (feature) */
465 } /* while (chip) */
466 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
468 if (first_feature == NULL)
469 {
470 sensors_cleanup ();
471 INFO ("sensors plugin: lm_sensors reports no "
472 "features. Data will not be collected.");
473 return (-1);
474 }
476 return (0);
477 } /* int sensors_load_conf */
479 static int sensors_shutdown (void)
480 {
481 sensors_free_features ();
482 ignorelist_free (sensor_list);
484 return (0);
485 } /* int sensors_shutdown */
487 static void sensors_submit (const char *plugin_instance,
488 const char *type, const char *type_instance,
489 double val)
490 {
491 char match_key[1024];
492 int status;
494 value_t values[1];
495 value_list_t vl = VALUE_LIST_INIT;
497 status = ssnprintf (match_key, sizeof (match_key), "%s/%s-%s",
498 plugin_instance, type, type_instance);
499 if (status < 1)
500 return;
502 if (sensor_list != NULL)
503 {
504 DEBUG ("sensors plugin: Checking ignorelist for `%s'", match_key);
505 if (ignorelist_match (sensor_list, match_key))
506 return;
507 }
509 values[0].gauge = val;
511 vl.values = values;
512 vl.values_len = 1;
514 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
515 sstrncpy (vl.plugin, "sensors", sizeof (vl.plugin));
516 sstrncpy (vl.plugin_instance, plugin_instance,
517 sizeof (vl.plugin_instance));
518 sstrncpy (vl.type, type, sizeof (vl.type));
519 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
521 plugin_dispatch_values (&vl);
522 } /* void sensors_submit */
524 static int sensors_read (void)
525 {
526 featurelist_t *fl;
528 if (sensors_load_conf () != 0)
529 return (-1);
531 #if SENSORS_API_VERSION < 0x400
532 for (fl = first_feature; fl != NULL; fl = fl->next)
533 {
534 double value;
535 int status;
536 char plugin_instance[DATA_MAX_NAME_LEN];
537 char type_instance[DATA_MAX_NAME_LEN];
539 status = sensors_get_feature (*fl->chip,
540 fl->data->number, &value);
541 if (status < 0)
542 continue;
544 status = sensors_snprintf_chip_name (plugin_instance,
545 sizeof (plugin_instance), fl->chip);
546 if (status < 0)
547 continue;
549 sstrncpy (type_instance, fl->data->name,
550 sizeof (type_instance));
552 sensors_submit (plugin_instance,
553 sensor_type_name_map[fl->type],
554 type_instance,
555 value);
556 } /* for fl = first_feature .. NULL */
557 /* #endif SENSORS_API_VERSION < 0x400 */
559 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
560 for (fl = first_feature; fl != NULL; fl = fl->next)
561 {
562 double value;
563 int status;
564 char plugin_instance[DATA_MAX_NAME_LEN];
565 char type_instance[DATA_MAX_NAME_LEN];
566 char *sensor_label;
567 const char *type;
569 status = sensors_get_value (fl->chip,
570 fl->subfeature->number, &value);
571 if (status < 0)
572 continue;
574 status = sensors_snprintf_chip_name (plugin_instance,
575 sizeof (plugin_instance), fl->chip);
576 if (status < 0)
577 continue;
579 if (use_labels)
580 {
581 sensor_label = sensors_get_label (fl->chip, fl->feature);
582 sstrncpy (type_instance, sensor_label, sizeof (type_instance));
583 free (sensor_label);
584 }
585 else
586 {
587 sstrncpy (type_instance, fl->feature->name,
588 sizeof (type_instance));
589 }
591 if (fl->feature->type == SENSORS_FEATURE_IN)
592 type = "voltage";
593 else if (fl->feature->type
594 == SENSORS_FEATURE_FAN)
595 type = "fanspeed";
596 else if (fl->feature->type
597 == SENSORS_FEATURE_TEMP)
598 type = "temperature";
599 else if (fl->feature->type
600 == SENSORS_FEATURE_POWER)
601 type = "power";
602 else
603 continue;
605 sensors_submit (plugin_instance, type, type_instance, value);
606 } /* for fl = first_feature .. NULL */
607 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
609 return (0);
610 } /* int sensors_read */
612 void module_register (void)
613 {
614 plugin_register_config ("sensors", sensors_config,
615 config_keys, config_keys_num);
616 plugin_register_read ("sensors", sensors_read);
617 plugin_register_shutdown ("sensors", sensors_shutdown);
618 } /* void module_register */