Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / sensors.c
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  **/
35
36 #include "collectd.h"
37
38 #include "common.h"
39 #include "plugin.h"
40 #include "utils_ignorelist.h"
41
42 #if defined(HAVE_SENSORS_SENSORS_H)
43 #include <sensors/sensors.h>
44 #endif
45
46 #if !defined(SENSORS_API_VERSION)
47 #define SENSORS_API_VERSION 0x000
48 #endif
49
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 #define SENSOR_TYPE_VOLTAGE 0
59     "voltage",
60 #define SENSOR_TYPE_FANSPEED 1
61     "fanspeed",
62 #define SENSOR_TYPE_TEMPERATURE 2
63     "temperature",
64 #define SENSOR_TYPE_POWER 3
65     "power",
66 #define SENSOR_TYPE_UNKNOWN 4
67     NULL};
68
69 struct sensors_labeltypes_s {
70   char *label;
71   int type;
72 };
73 typedef struct sensors_labeltypes_s sensors_labeltypes_t;
74
75 /* finite list of known labels extracted from lm_sensors */
76 static sensors_labeltypes_t known_features[] = {
77     {"fan1", SENSOR_TYPE_FANSPEED},
78     {"fan2", SENSOR_TYPE_FANSPEED},
79     {"fan3", SENSOR_TYPE_FANSPEED},
80     {"fan4", SENSOR_TYPE_FANSPEED},
81     {"fan5", SENSOR_TYPE_FANSPEED},
82     {"fan6", SENSOR_TYPE_FANSPEED},
83     {"fan7", SENSOR_TYPE_FANSPEED},
84     {"AIN2", SENSOR_TYPE_VOLTAGE},
85     {"AIN1", SENSOR_TYPE_VOLTAGE},
86     {"in10", SENSOR_TYPE_VOLTAGE},
87     {"in9", SENSOR_TYPE_VOLTAGE},
88     {"in8", SENSOR_TYPE_VOLTAGE},
89     {"in7", SENSOR_TYPE_VOLTAGE},
90     {"in6", SENSOR_TYPE_VOLTAGE},
91     {"in5", SENSOR_TYPE_VOLTAGE},
92     {"in4", SENSOR_TYPE_VOLTAGE},
93     {"in3", SENSOR_TYPE_VOLTAGE},
94     {"in2", SENSOR_TYPE_VOLTAGE},
95     {"in0", SENSOR_TYPE_VOLTAGE},
96     {"CPU_Temp", SENSOR_TYPE_TEMPERATURE},
97     {"remote_temp", SENSOR_TYPE_TEMPERATURE},
98     {"temp1", SENSOR_TYPE_TEMPERATURE},
99     {"temp2", SENSOR_TYPE_TEMPERATURE},
100     {"temp3", SENSOR_TYPE_TEMPERATURE},
101     {"temp4", SENSOR_TYPE_TEMPERATURE},
102     {"temp5", SENSOR_TYPE_TEMPERATURE},
103     {"temp6", SENSOR_TYPE_TEMPERATURE},
104     {"temp7", SENSOR_TYPE_TEMPERATURE},
105     {"temp", SENSOR_TYPE_TEMPERATURE},
106     {"Vccp2", SENSOR_TYPE_VOLTAGE},
107     {"Vccp1", SENSOR_TYPE_VOLTAGE},
108     {"vdd", SENSOR_TYPE_VOLTAGE},
109     {"vid5", SENSOR_TYPE_VOLTAGE},
110     {"vid4", SENSOR_TYPE_VOLTAGE},
111     {"vid3", SENSOR_TYPE_VOLTAGE},
112     {"vid2", SENSOR_TYPE_VOLTAGE},
113     {"vid1", SENSOR_TYPE_VOLTAGE},
114     {"vid", SENSOR_TYPE_VOLTAGE},
115     {"vin4", SENSOR_TYPE_VOLTAGE},
116     {"vin3", SENSOR_TYPE_VOLTAGE},
117     {"vin2", SENSOR_TYPE_VOLTAGE},
118     {"vin1", SENSOR_TYPE_VOLTAGE},
119     {"voltbatt", SENSOR_TYPE_VOLTAGE},
120     {"volt12", SENSOR_TYPE_VOLTAGE},
121     {"volt5", SENSOR_TYPE_VOLTAGE},
122     {"vrm", SENSOR_TYPE_VOLTAGE},
123     {"5.0V", SENSOR_TYPE_VOLTAGE},
124     {"5V", SENSOR_TYPE_VOLTAGE},
125     {"3.3V", SENSOR_TYPE_VOLTAGE},
126     {"2.5V", SENSOR_TYPE_VOLTAGE},
127     {"2.0V", SENSOR_TYPE_VOLTAGE},
128     {"12V", SENSOR_TYPE_VOLTAGE},
129     {"power1", SENSOR_TYPE_POWER}};
130 static int known_features_num = STATIC_ARRAY_SIZE(known_features);
131 /* end new naming */
132 #endif /* SENSORS_API_VERSION < 0x400 */
133
134 static const char *config_keys[] = {"Sensor", "IgnoreSelected",
135                                     "SensorConfigFile", "UseLabels"};
136 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
137
138 #if SENSORS_API_VERSION < 0x400
139 typedef struct featurelist {
140   const sensors_chip_name *chip;
141   const sensors_feature_data *data;
142   int type;
143   struct featurelist *next;
144 } featurelist_t;
145
146 #ifndef SENSORS_CONF_PATH
147 #define SENSORS_CONF_PATH "/etc/sensors.conf"
148 #endif
149 static char *conffile = SENSORS_CONF_PATH;
150 /* #endif SENSORS_API_VERSION < 0x400 */
151
152 #elif (SENSORS_API_VERSION >= 0x400)
153 typedef struct featurelist {
154   const sensors_chip_name *chip;
155   const sensors_feature *feature;
156   const sensors_subfeature *subfeature;
157   struct featurelist *next;
158 } featurelist_t;
159
160 static char *conffile = NULL;
161 static _Bool use_labels = 0;
162 #endif
163
164 static featurelist_t *first_feature = NULL;
165 static ignorelist_t *sensor_list;
166
167 #if SENSORS_API_VERSION < 0x400
168 /* full chip name logic borrowed from lm_sensors */
169 static int sensors_snprintf_chip_name(char *buf, size_t buf_size,
170                                       const sensors_chip_name *chip) {
171   int status = -1;
172
173   if (chip->bus == SENSORS_CHIP_NAME_BUS_ISA) {
174     status = snprintf(buf, buf_size, "%s-isa-%04x", chip->prefix, chip->addr);
175   } else if (chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY) {
176     status = snprintf(buf, buf_size, "%s-%s-%04x", chip->prefix, chip->busname,
177                       chip->addr);
178   } else {
179     status = snprintf(buf, buf_size, "%s-i2c-%d-%02x", chip->prefix, chip->bus,
180                       chip->addr);
181   }
182
183   return status;
184 } /* int sensors_snprintf_chip_name */
185
186 static int sensors_feature_name_to_type(const char *name) {
187   /* Yes, this is slow, but it's only ever done during initialization, so
188    * it's a one time cost.. */
189   for (int i = 0; i < known_features_num; i++)
190     if (strcasecmp(known_features[i].label, name) == 0)
191       return known_features[i].type;
192
193   return SENSOR_TYPE_UNKNOWN;
194 } /* int sensors_feature_name_to_type */
195 #endif
196
197 static int sensors_config(const char *key, const char *value) {
198   if (sensor_list == NULL)
199     sensor_list = ignorelist_create(1);
200
201   /* TODO: This setting exists for compatibility with old versions of
202    * lm-sensors. Remove support for those ancient versions in the next
203    * major release. */
204   if (strcasecmp(key, "SensorConfigFile") == 0) {
205     char *tmp = strdup(value);
206     if (tmp != NULL) {
207       sfree(conffile);
208       conffile = tmp;
209     }
210   } else if (strcasecmp(key, "Sensor") == 0) {
211     if (ignorelist_add(sensor_list, value)) {
212       ERROR("sensors plugin: "
213             "Cannot add value to ignorelist.");
214       return 1;
215     }
216   } else if (strcasecmp(key, "IgnoreSelected") == 0) {
217     ignorelist_set_invert(sensor_list, 1);
218     if (IS_TRUE(value))
219       ignorelist_set_invert(sensor_list, 0);
220   }
221 #if (SENSORS_API_VERSION >= 0x400)
222   else if (strcasecmp(key, "UseLabels") == 0) {
223     use_labels = IS_TRUE(value) ? 1 : 0;
224   }
225 #endif
226   else {
227     return -1;
228   }
229
230   return 0;
231 }
232
233 static void sensors_free_features(void) {
234   featurelist_t *nextft;
235
236   if (first_feature == NULL)
237     return;
238
239   sensors_cleanup();
240
241   for (featurelist_t *thisft = first_feature; thisft != NULL; thisft = nextft) {
242     nextft = thisft->next;
243     sfree(thisft);
244   }
245   first_feature = NULL;
246 }
247
248 static int sensors_load_conf(void) {
249   static int call_once = 0;
250
251   FILE *fh = NULL;
252   featurelist_t *last_feature = NULL;
253
254   const sensors_chip_name *chip;
255   int chip_num;
256
257   int status;
258
259   if (call_once)
260     return 0;
261
262   call_once = 1;
263
264   if (conffile != NULL) {
265     fh = fopen(conffile, "r");
266     if (fh == NULL) {
267       char errbuf[1024];
268       ERROR("sensors plugin: fopen(%s) failed: %s", conffile,
269             sstrerror(errno, errbuf, sizeof(errbuf)));
270       return -1;
271     }
272   }
273
274   status = sensors_init(fh);
275   if (fh)
276     fclose(fh);
277
278   if (status != 0) {
279     ERROR("sensors plugin: Cannot initialize sensors. "
280           "Data will not be collected.");
281     return -1;
282   }
283
284 #if SENSORS_API_VERSION < 0x400
285   chip_num = 0;
286   while ((chip = sensors_get_detected_chips(&chip_num)) != NULL) {
287     int feature_num0 = 0;
288     int feature_num1 = 0;
289
290     while (42) {
291       const sensors_feature_data *feature;
292       int feature_type;
293       featurelist_t *fl;
294
295       feature = sensors_get_all_features(*chip, &feature_num0, &feature_num1);
296
297       /* Check if all features have been read. */
298       if (feature == NULL)
299         break;
300
301       /* "master features" only */
302       if (feature->mapping != SENSORS_NO_MAPPING) {
303         DEBUG("sensors plugin: sensors_load_conf: "
304               "Ignoring subfeature `%s', "
305               "because (feature->mapping "
306               "!= SENSORS_NO_MAPPING).",
307               feature->name);
308         continue;
309       }
310
311       /* skip ignored in sensors.conf */
312       if (sensors_get_ignored(*chip, feature->number) == 0) {
313         DEBUG("sensors plugin: sensors_load_conf: "
314               "Ignoring subfeature `%s', "
315               "because "
316               "`sensors_get_ignored' told "
317               "me so.",
318               feature->name);
319         continue;
320       }
321
322       feature_type = sensors_feature_name_to_type(feature->name);
323       if (feature_type == SENSOR_TYPE_UNKNOWN) {
324         DEBUG("sensors plugin: sensors_load_conf: "
325               "Ignoring subfeature `%s', "
326               "because its type is "
327               "unknown.",
328               feature->name);
329         continue;
330       }
331
332       fl = calloc(1, sizeof(*fl));
333       if (fl == NULL) {
334         ERROR("sensors plugin: calloc failed.");
335         continue;
336       }
337
338       fl->chip = chip;
339       fl->data = feature;
340       fl->type = feature_type;
341
342       if (first_feature == NULL)
343         first_feature = fl;
344       else
345         last_feature->next = fl;
346       last_feature = fl;
347     } /* while sensors_get_all_features */
348   }   /* while sensors_get_detected_chips */
349 /* #endif SENSORS_API_VERSION < 0x400 */
350
351 #elif (SENSORS_API_VERSION >= 0x400)
352   chip_num = 0;
353   while ((chip = sensors_get_detected_chips(NULL, &chip_num)) != NULL) {
354     const sensors_feature *feature;
355     int feature_num = 0;
356
357     while ((feature = sensors_get_features(chip, &feature_num)) != NULL) {
358       const sensors_subfeature *subfeature;
359       int subfeature_num = 0;
360
361       /* Only handle voltage, fanspeeds and temperatures */
362       if ((feature->type != SENSORS_FEATURE_IN) &&
363           (feature->type != SENSORS_FEATURE_FAN) &&
364           (feature->type != SENSORS_FEATURE_TEMP) &&
365 #if SENSORS_API_VERSION >= 0x402
366           (feature->type != SENSORS_FEATURE_CURR) &&
367 #endif
368           (feature->type != SENSORS_FEATURE_POWER)) {
369         DEBUG("sensors plugin: sensors_load_conf: "
370               "Ignoring feature `%s', "
371               "because its type is not "
372               "supported.",
373               feature->name);
374         continue;
375       }
376
377       while ((subfeature = sensors_get_all_subfeatures(
378                   chip, feature, &subfeature_num)) != NULL) {
379         featurelist_t *fl;
380
381         if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT) &&
382             (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT) &&
383             (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT) &&
384 #if SENSORS_API_VERSION >= 0x402
385             (subfeature->type != SENSORS_SUBFEATURE_CURR_INPUT) &&
386 #endif
387             (subfeature->type != SENSORS_SUBFEATURE_POWER_INPUT))
388           continue;
389
390         fl = calloc(1, sizeof(*fl));
391         if (fl == NULL) {
392           ERROR("sensors plugin: calloc failed.");
393           continue;
394         }
395
396         fl->chip = chip;
397         fl->feature = feature;
398         fl->subfeature = subfeature;
399
400         if (first_feature == NULL)
401           first_feature = fl;
402         else
403           last_feature->next = fl;
404         last_feature = fl;
405       } /* while (subfeature) */
406     }   /* while (feature) */
407   }     /* while (chip) */
408 #endif /* (SENSORS_API_VERSION >= 0x400) */
409
410   if (first_feature == NULL) {
411     sensors_cleanup();
412     INFO("sensors plugin: lm_sensors reports no "
413          "features. Data will not be collected.");
414     return -1;
415   }
416
417   return 0;
418 } /* int sensors_load_conf */
419
420 static int sensors_shutdown(void) {
421   sensors_free_features();
422   ignorelist_free(sensor_list);
423
424   return 0;
425 } /* int sensors_shutdown */
426
427 static void sensors_submit(const char *plugin_instance, const char *type,
428                            const char *type_instance, double value) {
429   char match_key[1024];
430   int status;
431
432   value_list_t vl = VALUE_LIST_INIT;
433
434   status = snprintf(match_key, sizeof(match_key), "%s/%s-%s", plugin_instance,
435                     type, type_instance);
436   if (status < 1)
437     return;
438
439   if (sensor_list != NULL) {
440     DEBUG("sensors plugin: Checking ignorelist for `%s'", match_key);
441     if (ignorelist_match(sensor_list, match_key))
442       return;
443   }
444
445   vl.values = &(value_t){.gauge = value};
446   vl.values_len = 1;
447
448   sstrncpy(vl.plugin, "sensors", sizeof(vl.plugin));
449   sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
450   sstrncpy(vl.type, type, sizeof(vl.type));
451   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
452
453   plugin_dispatch_values(&vl);
454 } /* void sensors_submit */
455
456 static int sensors_read(void) {
457   if (sensors_load_conf() != 0)
458     return -1;
459
460 #if SENSORS_API_VERSION < 0x400
461   for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) {
462     double value;
463     int status;
464     char plugin_instance[DATA_MAX_NAME_LEN];
465     char type_instance[DATA_MAX_NAME_LEN];
466
467     status = sensors_get_feature(*fl->chip, fl->data->number, &value);
468     if (status < 0)
469       continue;
470
471     status = sensors_snprintf_chip_name(plugin_instance,
472                                         sizeof(plugin_instance), fl->chip);
473     if (status < 0)
474       continue;
475
476     sstrncpy(type_instance, fl->data->name, sizeof(type_instance));
477
478     sensors_submit(plugin_instance, sensor_type_name_map[fl->type],
479                    type_instance, value);
480   } /* for fl = first_feature .. NULL */
481 /* #endif SENSORS_API_VERSION < 0x400 */
482
483 #elif (SENSORS_API_VERSION >= 0x400)
484   for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) {
485     double value;
486     int status;
487     char plugin_instance[DATA_MAX_NAME_LEN];
488     char type_instance[DATA_MAX_NAME_LEN];
489     char *sensor_label;
490     const char *type;
491
492     status = sensors_get_value(fl->chip, fl->subfeature->number, &value);
493     if (status < 0)
494       continue;
495
496     status = sensors_snprintf_chip_name(plugin_instance,
497                                         sizeof(plugin_instance), fl->chip);
498     if (status < 0)
499       continue;
500
501     if (use_labels) {
502       sensor_label = sensors_get_label(fl->chip, fl->feature);
503       sstrncpy(type_instance, sensor_label, sizeof(type_instance));
504       free(sensor_label);
505     } else {
506       sstrncpy(type_instance, fl->feature->name, sizeof(type_instance));
507     }
508
509     if (fl->feature->type == SENSORS_FEATURE_IN)
510       type = "voltage";
511     else if (fl->feature->type == SENSORS_FEATURE_FAN)
512       type = "fanspeed";
513     else if (fl->feature->type == SENSORS_FEATURE_TEMP)
514       type = "temperature";
515     else if (fl->feature->type == SENSORS_FEATURE_POWER)
516       type = "power";
517 #if SENSORS_API_VERSION >= 0x402
518     else if (fl->feature->type == SENSORS_FEATURE_CURR)
519       type = "current";
520 #endif
521     else
522       continue;
523
524     sensors_submit(plugin_instance, type, type_instance, value);
525   } /* for fl = first_feature .. NULL */
526 #endif /* (SENSORS_API_VERSION >= 0x400) */
527
528   return 0;
529 } /* int sensors_read */
530
531 void module_register(void) {
532   plugin_register_config("sensors", sensors_config, config_keys,
533                          config_keys_num);
534   plugin_register_read("sensors", sensors_read);
535   plugin_register_shutdown("sensors", sensors_shutdown);
536 } /* void module_register */