sensors: Removed checks for upper limit of SENSORS_API_VERSION
[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;
161 static bool use_labels;
162 #endif
163
164 static featurelist_t *first_feature;
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);
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;
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       ERROR("sensors plugin: fopen(%s) failed: %s", conffile, STRERRNO);
268       return -1;
269     }
270   }
271
272   status = sensors_init(fh);
273   if (fh)
274     fclose(fh);
275
276   if (status != 0) {
277     ERROR("sensors plugin: Cannot initialize sensors. "
278           "Data will not be collected.");
279     return -1;
280   }
281
282 #if SENSORS_API_VERSION < 0x400
283   chip_num = 0;
284   while ((chip = sensors_get_detected_chips(&chip_num)) != NULL) {
285     int feature_num0 = 0;
286     int feature_num1 = 0;
287
288     while (42) {
289       const sensors_feature_data *feature;
290       int feature_type;
291       featurelist_t *fl;
292
293       feature = sensors_get_all_features(*chip, &feature_num0, &feature_num1);
294
295       /* Check if all features have been read. */
296       if (feature == NULL)
297         break;
298
299       /* "master features" only */
300       if (feature->mapping != SENSORS_NO_MAPPING) {
301         DEBUG("sensors plugin: sensors_load_conf: "
302               "Ignoring subfeature `%s', "
303               "because (feature->mapping "
304               "!= SENSORS_NO_MAPPING).",
305               feature->name);
306         continue;
307       }
308
309       /* skip ignored in sensors.conf */
310       if (sensors_get_ignored(*chip, feature->number) == 0) {
311         DEBUG("sensors plugin: sensors_load_conf: "
312               "Ignoring subfeature `%s', "
313               "because "
314               "`sensors_get_ignored' told "
315               "me so.",
316               feature->name);
317         continue;
318       }
319
320       feature_type = sensors_feature_name_to_type(feature->name);
321       if (feature_type == SENSOR_TYPE_UNKNOWN) {
322         DEBUG("sensors plugin: sensors_load_conf: "
323               "Ignoring subfeature `%s', "
324               "because its type is "
325               "unknown.",
326               feature->name);
327         continue;
328       }
329
330       fl = calloc(1, sizeof(*fl));
331       if (fl == NULL) {
332         ERROR("sensors plugin: calloc failed.");
333         continue;
334       }
335
336       fl->chip = chip;
337       fl->data = feature;
338       fl->type = feature_type;
339
340       if (first_feature == NULL)
341         first_feature = fl;
342       else
343         last_feature->next = fl;
344       last_feature = fl;
345     } /* while sensors_get_all_features */
346   }   /* while sensors_get_detected_chips */
347 /* #endif SENSORS_API_VERSION < 0x400 */
348
349 #elif (SENSORS_API_VERSION >= 0x400)
350   chip_num = 0;
351   while ((chip = sensors_get_detected_chips(NULL, &chip_num)) != NULL) {
352     const sensors_feature *feature;
353     int feature_num = 0;
354
355     while ((feature = sensors_get_features(chip, &feature_num)) != NULL) {
356       const sensors_subfeature *subfeature;
357       int subfeature_num = 0;
358
359       /* Only handle voltage, fanspeeds and temperatures */
360       if ((feature->type != SENSORS_FEATURE_IN) &&
361           (feature->type != SENSORS_FEATURE_FAN) &&
362           (feature->type != SENSORS_FEATURE_TEMP) &&
363 #if SENSORS_API_VERSION >= 0x402
364           (feature->type != SENSORS_FEATURE_CURR) &&
365 #endif
366 #if SENSORS_API_VERSION >= 0x431
367           (feature->type != SENSORS_FEATURE_HUMIDITY) &&
368 #endif
369           (feature->type != SENSORS_FEATURE_POWER)) {
370         DEBUG("sensors plugin: sensors_load_conf: "
371               "Ignoring feature `%s', "
372               "because its type is not "
373               "supported.",
374               feature->name);
375         continue;
376       }
377
378       while ((subfeature = sensors_get_all_subfeatures(
379                   chip, feature, &subfeature_num)) != NULL) {
380         featurelist_t *fl;
381
382         if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT) &&
383             (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT) &&
384             (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT) &&
385 #if SENSORS_API_VERSION >= 0x402
386             (subfeature->type != SENSORS_SUBFEATURE_CURR_INPUT) &&
387 #endif
388 #if SENSORS_API_VERSION >= 0x431
389             (subfeature->type != SENSORS_SUBFEATURE_HUMIDITY_INPUT) &&
390 #endif
391             (subfeature->type != SENSORS_SUBFEATURE_POWER_INPUT))
392           continue;
393
394         fl = calloc(1, sizeof(*fl));
395         if (fl == NULL) {
396           ERROR("sensors plugin: calloc failed.");
397           continue;
398         }
399
400         fl->chip = chip;
401         fl->feature = feature;
402         fl->subfeature = subfeature;
403
404         if (first_feature == NULL)
405           first_feature = fl;
406         else
407           last_feature->next = fl;
408         last_feature = fl;
409       } /* while (subfeature) */
410     }   /* while (feature) */
411   }     /* while (chip) */
412 #endif /* (SENSORS_API_VERSION >= 0x400) */
413
414   if (first_feature == NULL) {
415     sensors_cleanup();
416     INFO("sensors plugin: lm_sensors reports no "
417          "features. Data will not be collected.");
418     return -1;
419   }
420
421   return 0;
422 } /* int sensors_load_conf */
423
424 static int sensors_shutdown(void) {
425   sensors_free_features();
426   ignorelist_free(sensor_list);
427
428   return 0;
429 } /* int sensors_shutdown */
430
431 static void sensors_submit(const char *plugin_instance, const char *type,
432                            const char *type_instance, double value) {
433   char match_key[1024];
434   int status;
435
436   value_list_t vl = VALUE_LIST_INIT;
437
438   status = snprintf(match_key, sizeof(match_key), "%s/%s-%s", plugin_instance,
439                     type, type_instance);
440   if (status < 1)
441     return;
442
443   if (sensor_list != NULL) {
444     DEBUG("sensors plugin: Checking ignorelist for `%s'", match_key);
445     if (ignorelist_match(sensor_list, match_key))
446       return;
447   }
448
449   vl.values = &(value_t){.gauge = value};
450   vl.values_len = 1;
451
452   sstrncpy(vl.plugin, "sensors", sizeof(vl.plugin));
453   sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
454   sstrncpy(vl.type, type, sizeof(vl.type));
455   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
456
457   plugin_dispatch_values(&vl);
458 } /* void sensors_submit */
459
460 static int sensors_read(void) {
461   if (sensors_load_conf() != 0)
462     return -1;
463
464 #if SENSORS_API_VERSION < 0x400
465   for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) {
466     double value;
467     int status;
468     char plugin_instance[DATA_MAX_NAME_LEN];
469     char type_instance[DATA_MAX_NAME_LEN];
470
471     status = sensors_get_feature(*fl->chip, fl->data->number, &value);
472     if (status < 0)
473       continue;
474
475     status = sensors_snprintf_chip_name(plugin_instance,
476                                         sizeof(plugin_instance), fl->chip);
477     if (status < 0)
478       continue;
479
480     sstrncpy(type_instance, fl->data->name, sizeof(type_instance));
481
482     sensors_submit(plugin_instance, sensor_type_name_map[fl->type],
483                    type_instance, value);
484   } /* for fl = first_feature .. NULL */
485 /* #endif SENSORS_API_VERSION < 0x400 */
486
487 #elif (SENSORS_API_VERSION >= 0x400)
488   for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) {
489     double value;
490     int status;
491     char plugin_instance[DATA_MAX_NAME_LEN];
492     char type_instance[DATA_MAX_NAME_LEN];
493     char *sensor_label;
494     const char *type;
495
496     status = sensors_get_value(fl->chip, fl->subfeature->number, &value);
497     if (status < 0)
498       continue;
499
500     status = sensors_snprintf_chip_name(plugin_instance,
501                                         sizeof(plugin_instance), fl->chip);
502     if (status < 0)
503       continue;
504
505     if (use_labels) {
506       sensor_label = sensors_get_label(fl->chip, fl->feature);
507       sstrncpy(type_instance, sensor_label, sizeof(type_instance));
508       free(sensor_label);
509     } else {
510       sstrncpy(type_instance, fl->feature->name, sizeof(type_instance));
511     }
512
513     if (fl->feature->type == SENSORS_FEATURE_IN)
514       type = "voltage";
515     else if (fl->feature->type == SENSORS_FEATURE_FAN)
516       type = "fanspeed";
517     else if (fl->feature->type == SENSORS_FEATURE_TEMP)
518       type = "temperature";
519     else if (fl->feature->type == SENSORS_FEATURE_POWER)
520       type = "power";
521 #if SENSORS_API_VERSION >= 0x402
522     else if (fl->feature->type == SENSORS_FEATURE_CURR)
523       type = "current";
524 #endif
525 #if SENSORS_API_VERSION >= 0x431
526     else if (fl->feature->type == SENSORS_FEATURE_HUMIDITY)
527       type = "humidity";
528 #endif
529     else
530       continue;
531
532     sensors_submit(plugin_instance, type, type_instance, value);
533   } /* for fl = first_feature .. NULL */
534 #endif /* (SENSORS_API_VERSION >= 0x400) */
535
536   return 0;
537 } /* int sensors_read */
538
539 void module_register(void) {
540   plugin_register_config("sensors", sensors_config, config_keys,
541                          config_keys_num);
542   plugin_register_read("sensors", sensors_read);
543   plugin_register_shutdown("sensors", sensors_shutdown);
544 } /* void module_register */