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) && (SENSORS_API_VERSION < 0x500)
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 (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
163
164 #else /* if SENSORS_API_VERSION >= 0x500 */
165 #error "This version of libsensors is not supported yet. Please report this " \
166         "as bug."
167 #endif
168
169 static featurelist_t *first_feature = NULL;
170 static ignorelist_t *sensor_list;
171
172 #if SENSORS_API_VERSION < 0x400
173 /* full chip name logic borrowed from lm_sensors */
174 static int sensors_snprintf_chip_name(char *buf, size_t buf_size,
175                                       const sensors_chip_name *chip) {
176   int status = -1;
177
178   if (chip->bus == SENSORS_CHIP_NAME_BUS_ISA) {
179     status = snprintf(buf, buf_size, "%s-isa-%04x", chip->prefix, chip->addr);
180   } else if (chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY) {
181     status = snprintf(buf, buf_size, "%s-%s-%04x", chip->prefix, chip->busname,
182                       chip->addr);
183   } else {
184     status = snprintf(buf, buf_size, "%s-i2c-%d-%02x", chip->prefix, chip->bus,
185                       chip->addr);
186   }
187
188   return status;
189 } /* int sensors_snprintf_chip_name */
190
191 static int sensors_feature_name_to_type(const char *name) {
192   /* Yes, this is slow, but it's only ever done during initialization, so
193    * it's a one time cost.. */
194   for (int i = 0; i < known_features_num; i++)
195     if (strcasecmp(known_features[i].label, name) == 0)
196       return known_features[i].type;
197
198   return SENSOR_TYPE_UNKNOWN;
199 } /* int sensors_feature_name_to_type */
200 #endif
201
202 static int sensors_config(const char *key, const char *value) {
203   if (sensor_list == NULL)
204     sensor_list = ignorelist_create(1);
205
206   /* TODO: This setting exists for compatibility with old versions of
207    * lm-sensors. Remove support for those ancient versions in the next
208    * major release. */
209   if (strcasecmp(key, "SensorConfigFile") == 0) {
210     char *tmp = strdup(value);
211     if (tmp != NULL) {
212       sfree(conffile);
213       conffile = tmp;
214     }
215   } else if (strcasecmp(key, "Sensor") == 0) {
216     if (ignorelist_add(sensor_list, value)) {
217       ERROR("sensors plugin: "
218             "Cannot add value to ignorelist.");
219       return 1;
220     }
221   } else if (strcasecmp(key, "IgnoreSelected") == 0) {
222     ignorelist_set_invert(sensor_list, 1);
223     if (IS_TRUE(value))
224       ignorelist_set_invert(sensor_list, 0);
225   }
226 #if (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
227   else if (strcasecmp(key, "UseLabels") == 0) {
228     use_labels = IS_TRUE(value) ? 1 : 0;
229   }
230 #endif
231   else {
232     return -1;
233   }
234
235   return 0;
236 }
237
238 static void sensors_free_features(void) {
239   featurelist_t *nextft;
240
241   if (first_feature == NULL)
242     return;
243
244   sensors_cleanup();
245
246   for (featurelist_t *thisft = first_feature; thisft != NULL; thisft = nextft) {
247     nextft = thisft->next;
248     sfree(thisft);
249   }
250   first_feature = NULL;
251 }
252
253 static int sensors_load_conf(void) {
254   static int call_once = 0;
255
256   FILE *fh = NULL;
257   featurelist_t *last_feature = NULL;
258
259   const sensors_chip_name *chip;
260   int chip_num;
261
262   int status;
263
264   if (call_once)
265     return 0;
266
267   call_once = 1;
268
269   if (conffile != NULL) {
270     fh = fopen(conffile, "r");
271     if (fh == NULL) {
272       char errbuf[1024];
273       ERROR("sensors plugin: fopen(%s) failed: %s", conffile,
274             sstrerror(errno, errbuf, sizeof(errbuf)));
275       return -1;
276     }
277   }
278
279   status = sensors_init(fh);
280   if (fh)
281     fclose(fh);
282
283   if (status != 0) {
284     ERROR("sensors plugin: Cannot initialize sensors. "
285           "Data will not be collected.");
286     return -1;
287   }
288
289 #if SENSORS_API_VERSION < 0x400
290   chip_num = 0;
291   while ((chip = sensors_get_detected_chips(&chip_num)) != NULL) {
292     int feature_num0 = 0;
293     int feature_num1 = 0;
294
295     while (42) {
296       const sensors_feature_data *feature;
297       int feature_type;
298       featurelist_t *fl;
299
300       feature = sensors_get_all_features(*chip, &feature_num0, &feature_num1);
301
302       /* Check if all features have been read. */
303       if (feature == NULL)
304         break;
305
306       /* "master features" only */
307       if (feature->mapping != SENSORS_NO_MAPPING) {
308         DEBUG("sensors plugin: sensors_load_conf: "
309               "Ignoring subfeature `%s', "
310               "because (feature->mapping "
311               "!= SENSORS_NO_MAPPING).",
312               feature->name);
313         continue;
314       }
315
316       /* skip ignored in sensors.conf */
317       if (sensors_get_ignored(*chip, feature->number) == 0) {
318         DEBUG("sensors plugin: sensors_load_conf: "
319               "Ignoring subfeature `%s', "
320               "because "
321               "`sensors_get_ignored' told "
322               "me so.",
323               feature->name);
324         continue;
325       }
326
327       feature_type = sensors_feature_name_to_type(feature->name);
328       if (feature_type == SENSOR_TYPE_UNKNOWN) {
329         DEBUG("sensors plugin: sensors_load_conf: "
330               "Ignoring subfeature `%s', "
331               "because its type is "
332               "unknown.",
333               feature->name);
334         continue;
335       }
336
337       fl = calloc(1, sizeof(*fl));
338       if (fl == NULL) {
339         ERROR("sensors plugin: calloc failed.");
340         continue;
341       }
342
343       fl->chip = chip;
344       fl->data = feature;
345       fl->type = feature_type;
346
347       if (first_feature == NULL)
348         first_feature = fl;
349       else
350         last_feature->next = fl;
351       last_feature = fl;
352     } /* while sensors_get_all_features */
353   }   /* while sensors_get_detected_chips */
354 /* #endif SENSORS_API_VERSION < 0x400 */
355
356 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
357   chip_num = 0;
358   while ((chip = sensors_get_detected_chips(NULL, &chip_num)) != NULL) {
359     const sensors_feature *feature;
360     int feature_num = 0;
361
362     while ((feature = sensors_get_features(chip, &feature_num)) != NULL) {
363       const sensors_subfeature *subfeature;
364       int subfeature_num = 0;
365
366       /* Only handle voltage, fanspeeds and temperatures */
367       if ((feature->type != SENSORS_FEATURE_IN) &&
368           (feature->type != SENSORS_FEATURE_FAN) &&
369           (feature->type != SENSORS_FEATURE_TEMP) &&
370 #if SENSORS_API_VERSION >= 0x402
371           (feature->type != SENSORS_FEATURE_CURR) &&
372 #endif
373           (feature->type != SENSORS_FEATURE_POWER)) {
374         DEBUG("sensors plugin: sensors_load_conf: "
375               "Ignoring feature `%s', "
376               "because its type is not "
377               "supported.",
378               feature->name);
379         continue;
380       }
381
382       while ((subfeature = sensors_get_all_subfeatures(
383                   chip, feature, &subfeature_num)) != NULL) {
384         featurelist_t *fl;
385
386         if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT) &&
387             (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT) &&
388             (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT) &&
389 #if SENSORS_API_VERSION >= 0x402
390             (subfeature->type != SENSORS_SUBFEATURE_CURR_INPUT) &&
391 #endif
392             (subfeature->type != SENSORS_SUBFEATURE_POWER_INPUT))
393           continue;
394
395         fl = calloc(1, sizeof(*fl));
396         if (fl == NULL) {
397           ERROR("sensors plugin: calloc failed.");
398           continue;
399         }
400
401         fl->chip = chip;
402         fl->feature = feature;
403         fl->subfeature = subfeature;
404
405         if (first_feature == NULL)
406           first_feature = fl;
407         else
408           last_feature->next = fl;
409         last_feature = fl;
410       } /* while (subfeature) */
411     }   /* while (feature) */
412   }     /* while (chip) */
413 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
414
415   if (first_feature == NULL) {
416     sensors_cleanup();
417     INFO("sensors plugin: lm_sensors reports no "
418          "features. Data will not be collected.");
419     return -1;
420   }
421
422   return 0;
423 } /* int sensors_load_conf */
424
425 static int sensors_shutdown(void) {
426   sensors_free_features();
427   ignorelist_free(sensor_list);
428
429   return 0;
430 } /* int sensors_shutdown */
431
432 static void sensors_submit(const char *plugin_instance, const char *type,
433                            const char *type_instance, double value) {
434   char match_key[1024];
435   int status;
436
437   value_list_t vl = VALUE_LIST_INIT;
438
439   status = snprintf(match_key, sizeof(match_key), "%s/%s-%s", plugin_instance,
440                     type, type_instance);
441   if (status < 1)
442     return;
443
444   if (sensor_list != NULL) {
445     DEBUG("sensors plugin: Checking ignorelist for `%s'", match_key);
446     if (ignorelist_match(sensor_list, match_key))
447       return;
448   }
449
450   vl.values = &(value_t){.gauge = value};
451   vl.values_len = 1;
452
453   sstrncpy(vl.plugin, "sensors", sizeof(vl.plugin));
454   sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
455   sstrncpy(vl.type, type, sizeof(vl.type));
456   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
457
458   plugin_dispatch_values(&vl);
459 } /* void sensors_submit */
460
461 static int sensors_read(void) {
462   if (sensors_load_conf() != 0)
463     return -1;
464
465 #if SENSORS_API_VERSION < 0x400
466   for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) {
467     double value;
468     int status;
469     char plugin_instance[DATA_MAX_NAME_LEN];
470     char type_instance[DATA_MAX_NAME_LEN];
471
472     status = sensors_get_feature(*fl->chip, fl->data->number, &value);
473     if (status < 0)
474       continue;
475
476     status = sensors_snprintf_chip_name(plugin_instance,
477                                         sizeof(plugin_instance), fl->chip);
478     if (status < 0)
479       continue;
480
481     sstrncpy(type_instance, fl->data->name, sizeof(type_instance));
482
483     sensors_submit(plugin_instance, sensor_type_name_map[fl->type],
484                    type_instance, value);
485   } /* for fl = first_feature .. NULL */
486 /* #endif SENSORS_API_VERSION < 0x400 */
487
488 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
489   for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) {
490     double value;
491     int status;
492     char plugin_instance[DATA_MAX_NAME_LEN];
493     char type_instance[DATA_MAX_NAME_LEN];
494     char *sensor_label;
495     const char *type;
496
497     status = sensors_get_value(fl->chip, fl->subfeature->number, &value);
498     if (status < 0)
499       continue;
500
501     status = sensors_snprintf_chip_name(plugin_instance,
502                                         sizeof(plugin_instance), fl->chip);
503     if (status < 0)
504       continue;
505
506     if (use_labels) {
507       sensor_label = sensors_get_label(fl->chip, fl->feature);
508       sstrncpy(type_instance, sensor_label, sizeof(type_instance));
509       free(sensor_label);
510     } else {
511       sstrncpy(type_instance, fl->feature->name, sizeof(type_instance));
512     }
513
514     if (fl->feature->type == SENSORS_FEATURE_IN)
515       type = "voltage";
516     else if (fl->feature->type == SENSORS_FEATURE_FAN)
517       type = "fanspeed";
518     else if (fl->feature->type == SENSORS_FEATURE_TEMP)
519       type = "temperature";
520     else if (fl->feature->type == SENSORS_FEATURE_POWER)
521       type = "power";
522 #if SENSORS_API_VERSION >= 0x402
523     else if (fl->feature->type == SENSORS_FEATURE_CURR)
524       type = "current";
525 #endif
526     else
527       continue;
528
529     sensors_submit(plugin_instance, type, type_instance, value);
530   } /* for fl = first_feature .. NULL */
531 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
532
533   return 0;
534 } /* int sensors_read */
535
536 void module_register(void) {
537   plugin_register_config("sensors", sensors_config, config_keys,
538                          config_keys_num);
539   plugin_register_read("sensors", sensors_read);
540   plugin_register_shutdown("sensors", sensors_shutdown);
541 } /* void module_register */