6ba8154c2dc0a14bfabdba7710bd7fadfb0282bc
1 /**
2 * collectd - src/sensors.c
3 * Copyright (C) 2005,2006 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
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 verplant.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 **/
32 #include "collectd.h"
33 #include "common.h"
34 #include "plugin.h"
35 #include "configfile.h"
36 #include "utils_ignorelist.h"
37 #include "utils_debug.h"
39 #define MODULE_NAME "sensors"
40 #define MODULE_NAME_VOLTAGE MODULE_NAME"_voltage"
42 #if defined(HAVE_SENSORS_SENSORS_H)
43 # include <sensors/sensors.h>
44 #else
45 # undef HAVE_LIBSENSORS
46 #endif
48 #if defined(HAVE_LIBSENSORS)
49 # define SENSORS_HAVE_READ 1
50 #else
51 # define SENSORS_HAVE_READ 0
52 #endif
54 #define BUFSIZE 512
56 /* temperature and fan sensors */
57 static char *ds_def[] =
58 {
59 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
60 NULL
61 };
62 static int ds_num = 1;
64 /* voltage sensors */
65 static char *sensor_voltage_ds_def[] =
66 {
67 "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":U:U",
68 NULL
69 };
70 static int sensor_voltage_ds_num = 1;
72 /* old naming */
73 static char *old_filename_format = "sensors-%s.rrd";
74 /* end old naming */
76 /* new naming <chip-bus-address/type-feature */
77 static char *extended_filename_format = "lm_sensors-%s.rrd";
79 #define SENSOR_TYPE_UNKNOWN 0
80 #define SENSOR_TYPE_VOLTAGE 1
81 #define SENSOR_TYPE_FANSPEED 2
82 #define SENSOR_TYPE_TEMPERATURE 3
84 #if SENSORS_HAVE_READ
85 static char *sensor_type_prefix[] =
86 {
87 "unknown",
88 "voltage",
89 "fanspeed",
90 "temperature",
91 NULL
92 };
93 #endif
95 typedef struct sensors_labeltypes {
96 char *label;
97 int type;
98 } sensors_labeltypes;
100 /*
101 * finite list of known labels extracted from lm_sensors
102 */
103 #if SENSORS_HAVE_READ
104 static sensors_labeltypes known_features[] =
105 {
106 { "fan1", SENSOR_TYPE_FANSPEED },
107 { "fan2", SENSOR_TYPE_FANSPEED },
108 { "fan3", SENSOR_TYPE_FANSPEED },
109 { "fan4", SENSOR_TYPE_FANSPEED },
110 { "fan5", SENSOR_TYPE_FANSPEED },
111 { "fan6", SENSOR_TYPE_FANSPEED },
112 { "fan7", SENSOR_TYPE_FANSPEED },
113 { "AIN2", SENSOR_TYPE_VOLTAGE },
114 { "AIN1", SENSOR_TYPE_VOLTAGE },
115 { "in10", SENSOR_TYPE_VOLTAGE },
116 { "in9", SENSOR_TYPE_VOLTAGE },
117 { "in8", SENSOR_TYPE_VOLTAGE },
118 { "in7", SENSOR_TYPE_VOLTAGE },
119 { "in6", SENSOR_TYPE_VOLTAGE },
120 { "in5", SENSOR_TYPE_VOLTAGE },
121 { "in4", SENSOR_TYPE_VOLTAGE },
122 { "in3", SENSOR_TYPE_VOLTAGE },
123 { "in2", SENSOR_TYPE_VOLTAGE },
124 { "in0", SENSOR_TYPE_VOLTAGE },
125 { "CPU_Temp", SENSOR_TYPE_TEMPERATURE },
126 { "remote_temp", SENSOR_TYPE_TEMPERATURE },
127 { "temp1", SENSOR_TYPE_TEMPERATURE },
128 { "temp2", SENSOR_TYPE_TEMPERATURE },
129 { "temp3", SENSOR_TYPE_TEMPERATURE },
130 { "temp4", SENSOR_TYPE_TEMPERATURE },
131 { "temp5", SENSOR_TYPE_TEMPERATURE },
132 { "temp6", SENSOR_TYPE_TEMPERATURE },
133 { "temp7", SENSOR_TYPE_TEMPERATURE },
134 { "temp", SENSOR_TYPE_TEMPERATURE },
135 { "Vccp2", SENSOR_TYPE_VOLTAGE },
136 { "Vccp1", SENSOR_TYPE_VOLTAGE },
137 { "vdd", SENSOR_TYPE_VOLTAGE },
138 { "vid5", SENSOR_TYPE_VOLTAGE },
139 { "vid4", SENSOR_TYPE_VOLTAGE },
140 { "vid3", SENSOR_TYPE_VOLTAGE },
141 { "vid2", SENSOR_TYPE_VOLTAGE },
142 { "vid1", SENSOR_TYPE_VOLTAGE },
143 { "vid", SENSOR_TYPE_VOLTAGE },
144 { "vin4", SENSOR_TYPE_VOLTAGE },
145 { "vin3", SENSOR_TYPE_VOLTAGE },
146 { "vin2", SENSOR_TYPE_VOLTAGE },
147 { "vin1", SENSOR_TYPE_VOLTAGE },
148 { "voltbatt", SENSOR_TYPE_VOLTAGE },
149 { "volt12", SENSOR_TYPE_VOLTAGE },
150 { "volt5", SENSOR_TYPE_VOLTAGE },
151 { "vrm", SENSOR_TYPE_VOLTAGE },
152 { "5.0V", SENSOR_TYPE_VOLTAGE },
153 { "5V", SENSOR_TYPE_VOLTAGE },
154 { "3.3V", SENSOR_TYPE_VOLTAGE },
155 { "2.5V", SENSOR_TYPE_VOLTAGE },
156 { "2.0V", SENSOR_TYPE_VOLTAGE },
157 { "12V", SENSOR_TYPE_VOLTAGE },
158 { 0, -1 }
159 };
160 #endif
161 /* end new naming */
163 static char *config_keys[] =
164 {
165 "Sensor",
166 "IgnoreSelected",
167 "ExtendedSensorNaming",
168 NULL
169 };
170 static int config_keys_num = 3;
172 static ignorelist_t *sensor_list;
174 /*
175 * sensor_extended_naming:
176 * 0 => default is to create chip-feature
177 * 1 => use new naming scheme chip-bus-address/type-feature
178 */
179 static int sensor_extended_naming = 0;
181 #if SENSORS_HAVE_READ
182 #ifndef SENSORS_CONF_PATH
183 # define SENSORS_CONF_PATH "/etc/sensors.conf"
184 #endif
185 static const char *conffile = SENSORS_CONF_PATH;
186 /* SENSORS_CONF_PATH */
188 typedef struct featurelist
189 {
190 const sensors_chip_name *chip;
191 const sensors_feature_data *data;
192 int type;
193 struct featurelist *next;
194 } featurelist_t;
196 featurelist_t *first_feature = NULL;
197 #endif /* if SENSORS_HAVE_READ */
199 static int sensors_config (char *key, char *value)
200 {
201 if (sensor_list == NULL)
202 sensor_list = ignorelist_create (1);
204 if (strcasecmp (key, "Sensor") == 0)
205 {
206 if (ignorelist_add (sensor_list, value))
207 {
208 syslog (LOG_EMERG, MODULE_NAME": Cannot add value to ignorelist.");
209 return (1);
210 }
211 }
212 else if (strcasecmp (key, "IgnoreSelected") == 0)
213 {
214 ignorelist_set_invert (sensor_list, 1);
215 if ((strcasecmp (value, "True") == 0)
216 || (strcasecmp (value, "Yes") == 0)
217 || (strcasecmp (value, "On") == 0))
218 ignorelist_set_invert (sensor_list, 0);
219 }
220 else if (strcasecmp (key, "ExtendedSensorNaming") == 0)
221 {
222 if ((strcasecmp (value, "True") == 0)
223 || (strcasecmp (value, "Yes") == 0)
224 || (strcasecmp (value, "On") == 0))
225 sensor_extended_naming = 1;
226 else
227 sensor_extended_naming = 0;
228 }
229 else
230 {
231 return (-1);
232 }
234 return (0);
235 }
237 static void collectd_sensors_init (void)
238 {
239 #if SENSORS_HAVE_READ
240 FILE *fh;
241 featurelist_t *last_feature = NULL;
242 featurelist_t *new_feature;
244 const sensors_chip_name *chip;
245 int chip_num;
247 const sensors_feature_data *data;
248 int data_num0, data_num1;
250 new_feature = first_feature;
251 while (new_feature != NULL)
252 {
253 last_feature = new_feature->next;
254 free (new_feature);
255 new_feature = last_feature;
256 }
258 #ifdef assert
259 assert (new_feature == NULL);
260 assert (last_feature == NULL);
261 #endif
263 if ((fh = fopen (conffile, "r")) == NULL)
264 return;
266 if (sensors_init (fh))
267 {
268 fclose (fh);
269 syslog (LOG_ERR, MODULE_NAME": Cannot initialize sensors. "
270 "Data will not be collected.");
271 return;
272 }
274 fclose (fh);
276 chip_num = 0;
277 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
278 {
279 data = NULL;
280 data_num0 = data_num1 = 0;
282 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1))
283 != NULL)
284 {
285 int i;
287 /* "master features" only */
288 if (data->mapping != SENSORS_NO_MAPPING)
289 continue;
291 /* Only known features */
292 for (i = 0; known_features[i].type >= 0; i++)
293 {
294 if (strcmp (data->name, known_features[i].label) != 0)
295 continue;
297 /* skip ignored in sensors.conf */
298 if (sensors_get_ignored (*chip, data->number) == 0)
299 break;
301 DBG ("Adding feature: %s-%s-%s",
302 chip->prefix,
303 sensor_type_prefix[known_features[i].type],
304 data->name);
306 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
307 {
308 DBG ("malloc: %s", strerror (errno));
309 syslog (LOG_ERR, MODULE_NAME": malloc: %s",
310 strerror (errno));
311 break;
312 }
314 new_feature->chip = chip;
315 new_feature->data = data;
316 new_feature->type = known_features[i].type;
317 new_feature->next = NULL;
319 if (first_feature == NULL)
320 {
321 first_feature = new_feature;
322 last_feature = new_feature;
323 }
324 else
325 {
326 last_feature->next = new_feature;
327 last_feature = new_feature;
328 }
330 /* stop searching known features at first found */
331 break;
332 } /* for i */
333 } /* while sensors_get_all_features */
334 } /* while sensors_get_detected_chips */
336 if (first_feature == NULL)
337 sensors_cleanup ();
338 #endif /* if SENSORS_HAVE_READ */
340 return;
341 }
343 static void sensors_shutdown (void)
344 {
345 #if SENSORS_HAVE_READ
346 featurelist_t *thisft = first_feature;
347 featurelist_t *nextft;
349 while (thisft != NULL)
350 {
351 nextft = thisft->next;
352 sfree (thisft);
353 thisft = nextft;
354 }
356 sensors_cleanup ();
357 #endif /* if SENSORS_HAVE_READ */
359 ignorelist_free (sensor_list);
360 }
362 static void sensors_voltage_write (char *host, char *inst, char *val)
363 {
364 char file[BUFSIZE];
365 int status;
367 /* skip ignored in our config */
368 if (ignorelist_match (sensor_list, inst))
369 return;
371 /* extended sensor naming */
372 if(sensor_extended_naming)
373 status = snprintf (file, BUFSIZE, extended_filename_format, inst);
374 else
375 status = snprintf (file, BUFSIZE, old_filename_format, inst);
377 if ((status < 1) || (status >= BUFSIZE))
378 return;
380 rrd_update_file (host, file, val, sensor_voltage_ds_def, sensor_voltage_ds_num);
381 }
383 static void sensors_write (char *host, char *inst, char *val)
384 {
385 char file[BUFSIZE];
386 int status;
388 /* skip ignored in our config */
389 if (ignorelist_match (sensor_list, inst))
390 return;
392 /* extended sensor naming */
393 if (sensor_extended_naming)
394 status = snprintf (file, BUFSIZE, extended_filename_format, inst);
395 else
396 status = snprintf (file, BUFSIZE, old_filename_format, inst);
398 if ((status < 1) || (status >= BUFSIZE))
399 return;
401 rrd_update_file (host, file, val, ds_def, ds_num);
402 }
404 #if SENSORS_HAVE_READ
405 static void sensors_submit (const char *feat_name,
406 const char *chip_prefix, double value, int type)
407 {
408 char buf[BUFSIZE];
409 char inst[BUFSIZE];
411 if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name)
412 >= BUFSIZE)
413 return;
415 /* skip ignored in our config */
416 if (ignorelist_match (sensor_list, inst))
417 return;
419 if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime,
420 value) >= BUFSIZE)
421 return;
423 if (type == SENSOR_TYPE_VOLTAGE)
424 {
425 DBG ("%s: %s/%s, %s", MODULE_NAME_VOLTAGE,
426 sensor_type_prefix[type], inst, buf);
427 plugin_submit (MODULE_NAME_VOLTAGE, inst, buf);
428 }
429 else
430 {
431 DBG ("%s: %s/%s, %s", MODULE_NAME,
432 sensor_type_prefix[type], inst, buf);
433 plugin_submit (MODULE_NAME, inst, buf);
434 }
435 }
437 static void sensors_read (void)
438 {
439 featurelist_t *feature;
440 double value;
441 char chip_fullprefix[BUFSIZE];
443 for (feature = first_feature; feature != NULL; feature = feature->next)
444 {
445 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
446 continue;
448 if (sensor_extended_naming)
449 {
450 /* full chip name logic borrowed from lm_sensors */
451 if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
452 {
453 if (snprintf (chip_fullprefix, BUFSIZE, "%s-isa-%04x/%s",
454 feature->chip->prefix,
455 feature->chip->addr,
456 sensor_type_prefix[feature->type])
457 >= BUFSIZE)
458 continue;
459 }
460 else if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
461 {
462 if (snprintf (chip_fullprefix, BUFSIZE, "%s-%s-%04x/%s",
463 feature->chip->prefix,
464 feature->chip->busname,
465 feature->chip->addr,
466 sensor_type_prefix[feature->type])
467 >= BUFSIZE)
468 continue;
469 }
470 else
471 {
472 if (snprintf (chip_fullprefix, BUFSIZE, "%s-i2c-%d-%02x/%s",
473 feature->chip->prefix,
474 feature->chip->bus,
475 feature->chip->addr,
476 sensor_type_prefix[feature->type])
477 >= BUFSIZE)
478 continue;
479 }
481 sensors_submit (feature->data->name,
482 chip_fullprefix,
483 value, feature->type);
484 }
485 else
486 {
487 sensors_submit (feature->data->name,
488 feature->chip->prefix,
489 value, feature->type);
490 }
491 }
492 }
493 #else
494 # define sensors_read NULL
495 #endif /* SENSORS_HAVE_READ */
497 void module_register (void)
498 {
499 plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read, sensors_write);
500 plugin_register (MODULE_NAME_VOLTAGE, NULL, NULL, sensors_voltage_write);
501 plugin_register_shutdown (MODULE_NAME, sensors_shutdown);
502 cf_register (MODULE_NAME, sensors_config, config_keys, config_keys_num);
503 }
505 #undef BUFSIZE
506 #undef MODULE_NAME