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 "plugin.h"
39 #include "utils/common/common.h"
40 #include "utils/ignorelist/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 #if SENSORS_API_VERSION < 0x400
55 typedef struct featurelist {
56 const sensors_chip_name *chip;
57 const sensors_feature_data *data;
58 int type;
59 struct featurelist *next;
60 } featurelist_t;
61
62 #ifndef SENSORS_CONF_PATH
63 #define SENSORS_CONF_PATH "/etc/sensors.conf"
64 #endif
65 static char *conffile = SENSORS_CONF_PATH;
66 /* #endif SENSORS_API_VERSION < 0x400 */
67
68 #elif (SENSORS_API_VERSION >= 0x400)
69 typedef struct featurelist {
70 const sensors_chip_name *chip;
71 const sensors_feature *feature;
72 const sensors_subfeature *subfeature;
73 struct featurelist *next;
74 } featurelist_t;
75
76 static char *conffile;
77 static bool use_labels;
78 #endif
79
80 static featurelist_t *first_feature;
81 static ignorelist_t *sensor_list;
82
sensors_config(const char * key,const char * value)83 static int sensors_config(const char *key, const char *value) {
84 if (sensor_list == NULL)
85 sensor_list = ignorelist_create(1);
86
87 /* TODO: This setting exists for compatibility with old versions of
88 * lm-sensors. Remove support for those ancient versions in the next
89 * major release. */
90 if (strcasecmp(key, "SensorConfigFile") == 0) {
91 char *tmp = strdup(value);
92 if (tmp != NULL) {
93 sfree(conffile);
94 conffile = tmp;
95 }
96 } else if (strcasecmp(key, "Sensor") == 0) {
97 if (ignorelist_add(sensor_list, value)) {
98 ERROR("sensors plugin: "
99 "Cannot add value to ignorelist.");
100 return 1;
101 }
102 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
103 ignorelist_set_invert(sensor_list, 1);
104 if (IS_TRUE(value))
105 ignorelist_set_invert(sensor_list, 0);
106 }
107 #if (SENSORS_API_VERSION >= 0x400)
108 else if (strcasecmp(key, "UseLabels") == 0) {
109 use_labels = IS_TRUE(value);
110 }
111 #endif
112 else {
113 return -1;
114 }
115
116 return 0;
117 }
118
sensors_free_features(void)119 static void sensors_free_features(void) {
120 featurelist_t *nextft;
121
122 if (first_feature == NULL)
123 return;
124
125 sensors_cleanup();
126
127 for (featurelist_t *thisft = first_feature; thisft != NULL; thisft = nextft) {
128 nextft = thisft->next;
129 sfree(thisft);
130 }
131 first_feature = NULL;
132 }
133
sensors_load_conf(void)134 static int sensors_load_conf(void) {
135 static int call_once;
136
137 FILE *fh = NULL;
138 featurelist_t *last_feature = NULL;
139
140 const sensors_chip_name *chip;
141 int chip_num;
142
143 int status;
144
145 if (call_once)
146 return 0;
147
148 call_once = 1;
149
150 if (conffile != NULL) {
151 fh = fopen(conffile, "r");
152 if (fh == NULL) {
153 ERROR("sensors plugin: fopen(%s) failed: %s", conffile, STRERRNO);
154 return -1;
155 }
156 }
157
158 status = sensors_init(fh);
159 if (fh)
160 fclose(fh);
161
162 if (status != 0) {
163 ERROR("sensors plugin: Cannot initialize sensors. "
164 "Data will not be collected.");
165 return -1;
166 }
167
168 #if SENSORS_API_VERSION < 0x400
169 chip_num = 0;
170 while ((chip = sensors_get_detected_chips(&chip_num)) != NULL) {
171 int feature_num0 = 0;
172 int feature_num1 = 0;
173
174 while (42) {
175 const sensors_feature_data *feature;
176 int feature_type;
177 featurelist_t *fl;
178
179 feature = sensors_get_all_features(*chip, &feature_num0, &feature_num1);
180
181 /* Check if all features have been read. */
182 if (feature == NULL)
183 break;
184
185 /* "master features" only */
186 if (feature->mapping != SENSORS_NO_MAPPING) {
187 DEBUG("sensors plugin: sensors_load_conf: "
188 "Ignoring subfeature `%s', "
189 "because (feature->mapping "
190 "!= SENSORS_NO_MAPPING).",
191 feature->name);
192 continue;
193 }
194
195 /* skip ignored in sensors.conf */
196 if (sensors_get_ignored(*chip, feature->number) == 0) {
197 DEBUG("sensors plugin: sensors_load_conf: "
198 "Ignoring subfeature `%s', "
199 "because "
200 "`sensors_get_ignored' told "
201 "me so.",
202 feature->name);
203 continue;
204 }
205
206 feature_type = sensors_feature_name_to_type(feature->name);
207 if (feature_type == SENSOR_TYPE_UNKNOWN) {
208 DEBUG("sensors plugin: sensors_load_conf: "
209 "Ignoring subfeature `%s', "
210 "because its type is "
211 "unknown.",
212 feature->name);
213 continue;
214 }
215
216 fl = calloc(1, sizeof(*fl));
217 if (fl == NULL) {
218 ERROR("sensors plugin: calloc failed.");
219 continue;
220 }
221
222 fl->chip = chip;
223 fl->data = feature;
224 fl->type = feature_type;
225
226 if (first_feature == NULL)
227 first_feature = fl;
228 else
229 last_feature->next = fl;
230 last_feature = fl;
231 } /* while sensors_get_all_features */
232 } /* while sensors_get_detected_chips */
233 /* #endif SENSORS_API_VERSION < 0x400 */
234
235 #elif (SENSORS_API_VERSION >= 0x400)
236 chip_num = 0;
237 while ((chip = sensors_get_detected_chips(NULL, &chip_num)) != NULL) {
238 const sensors_feature *feature;
239 int feature_num = 0;
240
241 while ((feature = sensors_get_features(chip, &feature_num)) != NULL) {
242 const sensors_subfeature *subfeature;
243 int subfeature_num = 0;
244
245 /* Only handle voltage, fanspeeds and temperatures */
246 if ((feature->type != SENSORS_FEATURE_IN) &&
247 (feature->type != SENSORS_FEATURE_FAN) &&
248 (feature->type != SENSORS_FEATURE_TEMP) &&
249 #if SENSORS_API_VERSION >= 0x402
250 (feature->type != SENSORS_FEATURE_CURR) &&
251 #endif
252 #if SENSORS_API_VERSION >= 0x431
253 (feature->type != SENSORS_FEATURE_HUMIDITY) &&
254 #endif
255 (feature->type != SENSORS_FEATURE_POWER)) {
256 DEBUG("sensors plugin: sensors_load_conf: "
257 "Ignoring feature `%s', "
258 "because its type is not "
259 "supported.",
260 feature->name);
261 continue;
262 }
263
264 while ((subfeature = sensors_get_all_subfeatures(
265 chip, feature, &subfeature_num)) != NULL) {
266 featurelist_t *fl;
267
268 if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT) &&
269 (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT) &&
270 (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT) &&
271 #if SENSORS_API_VERSION >= 0x402
272 (subfeature->type != SENSORS_SUBFEATURE_CURR_INPUT) &&
273 #endif
274 #if SENSORS_API_VERSION >= 0x431
275 (subfeature->type != SENSORS_SUBFEATURE_HUMIDITY_INPUT) &&
276 #endif
277 (subfeature->type != SENSORS_SUBFEATURE_POWER_INPUT))
278 continue;
279
280 fl = calloc(1, sizeof(*fl));
281 if (fl == NULL) {
282 ERROR("sensors plugin: calloc failed.");
283 continue;
284 }
285
286 fl->chip = chip;
287 fl->feature = feature;
288 fl->subfeature = subfeature;
289
290 if (first_feature == NULL)
291 first_feature = fl;
292 else
293 last_feature->next = fl;
294 last_feature = fl;
295 } /* while (subfeature) */
296 } /* while (feature) */
297 } /* while (chip) */
298 #endif /* (SENSORS_API_VERSION >= 0x400) */
299
300 if (first_feature == NULL) {
301 sensors_cleanup();
302 INFO("sensors plugin: lm_sensors reports no "
303 "features. Data will not be collected.");
304 return -1;
305 }
306
307 return 0;
308 } /* int sensors_load_conf */
309
sensors_shutdown(void)310 static int sensors_shutdown(void) {
311 sensors_free_features();
312 ignorelist_free(sensor_list);
313
314 return 0;
315 } /* int sensors_shutdown */
316
sensors_submit(const char * plugin_instance,const char * type,const char * type_instance,double value)317 static void sensors_submit(const char *plugin_instance, const char *type,
318 const char *type_instance, double value) {
319 char match_key[1024];
320 int status;
321
322 value_list_t vl = VALUE_LIST_INIT;
323
324 status = snprintf(match_key, sizeof(match_key), "%s/%s-%s", plugin_instance,
325 type, type_instance);
326 if (status < 1)
327 return;
328
329 if (sensor_list != NULL) {
330 DEBUG("sensors plugin: Checking ignorelist for `%s'", match_key);
331 if (ignorelist_match(sensor_list, match_key))
332 return;
333 }
334
335 vl.values = &(value_t){.gauge = value};
336 vl.values_len = 1;
337
338 sstrncpy(vl.plugin, "sensors", sizeof(vl.plugin));
339 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
340 sstrncpy(vl.type, type, sizeof(vl.type));
341 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
342
343 plugin_dispatch_values(&vl);
344 } /* void sensors_submit */
345
sensors_read(void)346 static int sensors_read(void) {
347 if (sensors_load_conf() != 0)
348 return -1;
349
350 #if SENSORS_API_VERSION < 0x400
351 for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) {
352 double value;
353 int status;
354 char plugin_instance[DATA_MAX_NAME_LEN];
355 char type_instance[DATA_MAX_NAME_LEN];
356
357 status = sensors_get_feature(*fl->chip, fl->data->number, &value);
358 if (status < 0)
359 continue;
360
361 status = sensors_snprintf_chip_name(plugin_instance,
362 sizeof(plugin_instance), fl->chip);
363 if (status < 0)
364 continue;
365
366 sstrncpy(type_instance, fl->data->name, sizeof(type_instance));
367
368 sensors_submit(plugin_instance, sensor_type_name_map[fl->type],
369 type_instance, value);
370 } /* for fl = first_feature .. NULL */
371 /* #endif SENSORS_API_VERSION < 0x400 */
372
373 #elif (SENSORS_API_VERSION >= 0x400)
374 for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next) {
375 double value;
376 int status;
377 char plugin_instance[DATA_MAX_NAME_LEN];
378 char type_instance[DATA_MAX_NAME_LEN];
379 char *sensor_label;
380 const char *type;
381
382 status = sensors_get_value(fl->chip, fl->subfeature->number, &value);
383 if (status < 0)
384 continue;
385
386 status = sensors_snprintf_chip_name(plugin_instance,
387 sizeof(plugin_instance), fl->chip);
388 if (status < 0)
389 continue;
390
391 if (use_labels) {
392 sensor_label = sensors_get_label(fl->chip, fl->feature);
393 sstrncpy(type_instance, sensor_label, sizeof(type_instance));
394 free(sensor_label);
395 } else {
396 sstrncpy(type_instance, fl->feature->name, sizeof(type_instance));
397 }
398
399 if (fl->feature->type == SENSORS_FEATURE_IN)
400 type = "voltage";
401 else if (fl->feature->type == SENSORS_FEATURE_FAN)
402 type = "fanspeed";
403 else if (fl->feature->type == SENSORS_FEATURE_TEMP)
404 type = "temperature";
405 else if (fl->feature->type == SENSORS_FEATURE_POWER)
406 type = "power";
407 #if SENSORS_API_VERSION >= 0x402
408 else if (fl->feature->type == SENSORS_FEATURE_CURR)
409 type = "current";
410 #endif
411 #if SENSORS_API_VERSION >= 0x431
412 else if (fl->feature->type == SENSORS_FEATURE_HUMIDITY)
413 type = "humidity";
414 #endif
415 else
416 continue;
417
418 sensors_submit(plugin_instance, type, type_instance, value);
419 } /* for fl = first_feature .. NULL */
420 #endif /* (SENSORS_API_VERSION >= 0x400) */
421
422 return 0;
423 } /* int sensors_read */
424
module_register(void)425 void module_register(void) {
426 plugin_register_config("sensors", sensors_config, config_keys,
427 config_keys_num);
428 plugin_register_read("sensors", sensors_read);
429 plugin_register_shutdown("sensors", sensors_shutdown);
430 } /* void module_register */
431