Auto-Merge pull request #3058 from cjmayo/debug_utf8
[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 static const char *config_keys[] = {"Sensor", "IgnoreSelected",
51                                     "SensorConfigFile", "UseLabels"};
52 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
53
54 typedef struct featurelist {
55   const sensors_chip_name *chip;
56   const sensors_feature *feature;
57   const sensors_subfeature *subfeature;
58   struct featurelist *next;
59 } featurelist_t;
60
61 static char *conffile;
62 static bool use_labels;
63
64 static featurelist_t *first_feature;
65 static ignorelist_t *sensor_list;
66
67 static int sensors_config(const char *key, const char *value) {
68   if (sensor_list == NULL)
69     sensor_list = ignorelist_create(1);
70
71   /* TODO: This setting exists for compatibility with old versions of
72    * lm-sensors. Remove support for those ancient versions in the next
73    * major release. */
74   if (strcasecmp(key, "SensorConfigFile") == 0) {
75     char *tmp = strdup(value);
76     if (tmp != NULL) {
77       sfree(conffile);
78       conffile = tmp;
79     }
80   } else if (strcasecmp(key, "Sensor") == 0) {
81     if (ignorelist_add(sensor_list, value)) {
82       ERROR("sensors plugin: "
83             "Cannot add value to ignorelist.");
84       return 1;
85     }
86   } else if (strcasecmp(key, "IgnoreSelected") == 0) {
87     ignorelist_set_invert(sensor_list, 1);
88     if (IS_TRUE(value))
89       ignorelist_set_invert(sensor_list, 0);
90   } else if (strcasecmp(key, "UseLabels") == 0) {
91     use_labels = IS_TRUE(value);
92   } else {
93     return -1;
94   }
95
96   return 0;
97 }
98
99 static void sensors_free_features(void) {
100   featurelist_t *nextft;
101
102   if (first_feature == NULL)
103     return;
104
105   sensors_cleanup();
106
107   for (featurelist_t *thisft = first_feature; thisft != NULL; thisft = nextft) {
108     nextft = thisft->next;
109     sfree(thisft);
110   }
111   first_feature = NULL;
112 }
113
114 static int sensors_load_conf(void) {
115   static int call_once;
116
117   FILE *fh = NULL;
118   featurelist_t *last_feature = NULL;
119
120   const sensors_chip_name *chip;
121   int chip_num;
122
123   int status;
124
125   if (call_once)
126     return 0;
127
128   call_once = 1;
129
130   if (conffile != NULL) {
131     fh = fopen(conffile, "r");
132     if (fh == NULL) {
133       ERROR("sensors plugin: fopen(%s) failed: %s", conffile, STRERRNO);
134       return -1;
135     }
136   }
137
138   status = sensors_init(fh);
139   if (fh)
140     fclose(fh);
141
142   if (status != 0) {
143     ERROR("sensors plugin: Cannot initialize sensors. "
144           "Data will not be collected.");
145     return -1;
146   }
147
148   chip_num = 0;
149   while ((chip = sensors_get_detected_chips(NULL, &chip_num)) != NULL) {
150     const sensors_feature *feature;
151     int feature_num = 0;
152
153     while ((feature = sensors_get_features(chip, &feature_num)) != NULL) {
154       const sensors_subfeature *subfeature;
155       int subfeature_num = 0;
156
157       /* Only handle voltage, fanspeeds and temperatures */
158       if ((feature->type != SENSORS_FEATURE_IN) &&
159           (feature->type != SENSORS_FEATURE_FAN) &&
160           (feature->type != SENSORS_FEATURE_TEMP) &&
161 #if SENSORS_API_VERSION >= 0x402
162           (feature->type != SENSORS_FEATURE_CURR) &&
163 #endif
164 #if SENSORS_API_VERSION >= 0x431
165           (feature->type != SENSORS_FEATURE_HUMIDITY) &&
166 #endif
167           (feature->type != SENSORS_FEATURE_POWER)) {
168         DEBUG("sensors plugin: sensors_load_conf: "
169               "Ignoring feature `%s', "
170               "because its type is not "
171               "supported.",
172               feature->name);
173         continue;
174       }
175
176       while ((subfeature = sensors_get_all_subfeatures(
177                   chip, feature, &subfeature_num)) != NULL) {
178         featurelist_t *fl;
179
180         if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT) &&
181             (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT) &&
182             (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT) &&
183 #if SENSORS_API_VERSION >= 0x402
184             (subfeature->type != SENSORS_SUBFEATURE_CURR_INPUT) &&
185 #endif
186 #if SENSORS_API_VERSION >= 0x431
187             (subfeature->type != SENSORS_SUBFEATURE_HUMIDITY_INPUT) &&
188 #endif
189             (subfeature->type != SENSORS_SUBFEATURE_POWER_INPUT))
190           continue;
191
192         fl = calloc(1, sizeof(*fl));
193         if (fl == NULL) {
194           ERROR("sensors plugin: calloc failed.");
195           continue;
196         }
197
198         fl->chip = chip;
199         fl->feature = feature;
200         fl->subfeature = subfeature;
201
202         if (first_feature == NULL)
203           first_feature = fl;
204         else
205           last_feature->next = fl;
206         last_feature = fl;
207       } /* while (subfeature) */
208     }   /* while (feature) */
209   }     /* while (chip) */
210
211   if (first_feature == NULL) {
212     sensors_cleanup();
213     INFO("sensors plugin: lm_sensors reports no "
214          "features. Data will not be collected.");
215     return -1;
216   }
217
218   return 0;
219 } /* int sensors_load_conf */
220
221 static int sensors_shutdown(void) {
222   sensors_free_features();
223   ignorelist_free(sensor_list);
224
225   return 0;
226 } /* int sensors_shutdown */
227
228 static void sensors_submit(const char *plugin_instance, const char *type,
229                            const char *type_instance, double value) {
230   char match_key[1024];
231   int status;
232
233   value_list_t vl = VALUE_LIST_INIT;
234
235   status = snprintf(match_key, sizeof(match_key), "%s/%s-%s", plugin_instance,
236                     type, type_instance);
237   if (status < 1)
238     return;
239
240   if (sensor_list != NULL) {
241     DEBUG("sensors plugin: Checking ignorelist for `%s'", match_key);
242     if (ignorelist_match(sensor_list, match_key))
243       return;
244   }
245
246   vl.values = &(value_t){.gauge = value};
247   vl.values_len = 1;
248
249   sstrncpy(vl.plugin, "sensors", sizeof(vl.plugin));
250   sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
251   sstrncpy(vl.type, type, sizeof(vl.type));
252   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
253
254   plugin_dispatch_values(&vl);
255 } /* void sensors_submit */
256
257 static int sensors_read(void) {
258   if (sensors_load_conf() != 0)
259     return -1;
260
261   for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) {
262     double value;
263     int status;
264     char plugin_instance[DATA_MAX_NAME_LEN];
265     char type_instance[DATA_MAX_NAME_LEN];
266     char *sensor_label;
267     const char *type;
268
269     status = sensors_get_value(fl->chip, fl->subfeature->number, &value);
270     if (status < 0)
271       continue;
272
273     status = sensors_snprintf_chip_name(plugin_instance,
274                                         sizeof(plugin_instance), fl->chip);
275     if (status < 0)
276       continue;
277
278     if (use_labels) {
279       sensor_label = sensors_get_label(fl->chip, fl->feature);
280       sstrncpy(type_instance, sensor_label, sizeof(type_instance));
281       free(sensor_label);
282     } else {
283       sstrncpy(type_instance, fl->feature->name, sizeof(type_instance));
284     }
285
286     if (fl->feature->type == SENSORS_FEATURE_IN)
287       type = "voltage";
288     else if (fl->feature->type == SENSORS_FEATURE_FAN)
289       type = "fanspeed";
290     else if (fl->feature->type == SENSORS_FEATURE_TEMP)
291       type = "temperature";
292     else if (fl->feature->type == SENSORS_FEATURE_POWER)
293       type = "power";
294 #if SENSORS_API_VERSION >= 0x402
295     else if (fl->feature->type == SENSORS_FEATURE_CURR)
296       type = "current";
297 #endif
298 #if SENSORS_API_VERSION >= 0x431
299     else if (fl->feature->type == SENSORS_FEATURE_HUMIDITY)
300       type = "humidity";
301 #endif
302     else
303       continue;
304
305     sensors_submit(plugin_instance, type, type_instance, value);
306   } /* for fl = first_feature .. NULL */
307
308   return 0;
309 } /* int sensors_read */
310
311 void module_register(void) {
312   plugin_register_config("sensors", sensors_config, config_keys,
313                          config_keys_num);
314   plugin_register_read("sensors", sensors_read);
315   plugin_register_shutdown("sensors", sensors_shutdown);
316 } /* void module_register */