940b4b4c256b7451810ef4a4ecd8fb4037087bee
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 25, 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_debug.h"
38 #define MODULE_NAME "sensors"
40 #if defined(HAVE_SENSORS_SENSORS_H)
41 # include <sensors/sensors.h>
42 #else
43 # undef HAVE_LIBSENSORS
44 #endif
46 #if defined(HAVE_LIBSENSORS)
47 # define SENSORS_HAVE_READ 1
48 #else
49 # define SENSORS_HAVE_READ 0
50 #endif
52 #define BUFSIZE 512
54 static char *ds_def[] =
55 {
56 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
57 NULL
58 };
59 static int ds_num = 1;
61 /* old naming */
62 static char *filename_format = "sensors-%s.rrd";
63 /* end old naming */
65 /* new naming */
66 static char *sensor_filename_format = "lm_sensors-%s.rrd";
68 static char *sensor_voltage_ds_def[] =
69 {
70 "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":U:U",
71 NULL
72 };
73 static int sensor_voltage_ds_num = 1;
75 #define SENSOR_TYPE_UNKNOWN 0
76 #define SENSOR_TYPE_VOLTAGE 1
77 #define SENSOR_TYPE_FANSPEED 2
78 #define SENSOR_TYPE_TEMPERATURE 3
80 static char *sensor_type_prefix[] =
81 {
82 "/unknown",
83 "/voltage",
84 "/fanspeed",
85 "/temperature",
86 NULL
87 };
89 typedef struct sensors_labeltypes {
90 char *label;
91 int type;
92 } sensors_labeltypes;
94 /* finite list of known labels
95 * sorted reverse by the length for the same type
96 * because strncmp must match "temp1" before "temp"
97 */
98 static sensors_labeltypes known_features[] =
99 {
100 { "fan7", SENSOR_TYPE_FANSPEED },
101 { "fan6", SENSOR_TYPE_FANSPEED },
102 { "fan5", SENSOR_TYPE_FANSPEED },
103 { "fan4", SENSOR_TYPE_FANSPEED },
104 { "fan3", SENSOR_TYPE_FANSPEED },
105 { "fan2", SENSOR_TYPE_FANSPEED },
106 { "fan1", SENSOR_TYPE_FANSPEED },
107 { "in8", SENSOR_TYPE_VOLTAGE },
108 { "in7", SENSOR_TYPE_VOLTAGE },
109 { "in6", SENSOR_TYPE_VOLTAGE },
110 { "in5", SENSOR_TYPE_VOLTAGE },
111 { "in4", SENSOR_TYPE_VOLTAGE },
112 { "in3", SENSOR_TYPE_VOLTAGE },
113 { "in2", SENSOR_TYPE_VOLTAGE },
114 { "in0", SENSOR_TYPE_VOLTAGE },
115 { "remote_temp", SENSOR_TYPE_TEMPERATURE },
116 { "temp7", SENSOR_TYPE_TEMPERATURE },
117 { "temp6", SENSOR_TYPE_TEMPERATURE },
118 { "temp5", SENSOR_TYPE_TEMPERATURE },
119 { "temp4", SENSOR_TYPE_TEMPERATURE },
120 { "temp3", SENSOR_TYPE_TEMPERATURE },
121 { "temp2", SENSOR_TYPE_TEMPERATURE },
122 { "temp1", SENSOR_TYPE_TEMPERATURE },
123 { "temp", SENSOR_TYPE_TEMPERATURE },
124 { "Vccp2", SENSOR_TYPE_VOLTAGE },
125 { "Vccp1", SENSOR_TYPE_VOLTAGE },
126 { "vdd", SENSOR_TYPE_VOLTAGE },
127 { "vid4", SENSOR_TYPE_VOLTAGE },
128 { "vid3", SENSOR_TYPE_VOLTAGE },
129 { "vid2", SENSOR_TYPE_VOLTAGE },
130 { "vid1", SENSOR_TYPE_VOLTAGE },
131 { "vid", SENSOR_TYPE_VOLTAGE },
132 { "vin4", SENSOR_TYPE_VOLTAGE },
133 { "vin3", SENSOR_TYPE_VOLTAGE },
134 { "vin2", SENSOR_TYPE_VOLTAGE },
135 { "vin1", SENSOR_TYPE_VOLTAGE },
136 { "voltbatt", SENSOR_TYPE_VOLTAGE },
137 { "volt12", SENSOR_TYPE_VOLTAGE },
138 { "volt5", SENSOR_TYPE_VOLTAGE },
139 { "vrm", SENSOR_TYPE_VOLTAGE },
140 { "12V", SENSOR_TYPE_VOLTAGE },
141 { "2.5V", SENSOR_TYPE_VOLTAGE },
142 { "3.3V", SENSOR_TYPE_VOLTAGE },
143 { "5V", SENSOR_TYPE_VOLTAGE },
144 { 0, -1 }
145 };
146 /* end new naming */
148 static char *config_keys[] =
149 {
150 "Sensor",
151 "IgnoreSelected",
152 "ExtendedSensorNaming",
153 NULL
154 };
155 static int config_keys_num = 3;
157 static char **sensor_list = NULL;
158 static int sensor_list_num = 0;
159 /*
160 * sensor_list_action:
161 * 0 => default is to collect selected sensors
162 * 1 => ignore selected sensors
163 */
164 static int sensor_list_action = 0;
165 /*
166 * sensor_extended_naming:
167 * 0 => default is to create chip-feature
168 * 1 => use new naming scheme chip-bus-address/type-feature
169 */
170 static int sensor_extended_naming = 0;
172 #ifdef HAVE_LIBSENSORS
173 typedef struct featurelist
174 {
175 const sensors_chip_name *chip;
176 const sensors_feature_data *data;
177 int type;
178 struct featurelist *next;
179 } featurelist_t;
181 featurelist_t *first_feature = NULL;
182 #endif /* defined (HAVE_LIBSENSORS) */
184 static int sensors_config (char *key, char *value)
185 {
186 char **temp;
188 if (strcasecmp (key, "Sensor") == 0)
189 {
190 temp = (char **) realloc (sensor_list, (sensor_list_num + 1) * sizeof (char *));
191 if (temp == NULL)
192 {
193 syslog (LOG_EMERG, "Cannot allocate more memory.");
194 return (1);
195 }
196 sensor_list = temp;
198 if ((sensor_list[sensor_list_num] = strdup (value)) == NULL)
199 {
200 syslog (LOG_EMERG, "Cannot allocate memory.");
201 return (1);
202 }
203 sensor_list_num++;
204 }
205 else if (strcasecmp (key, "IgnoreSelected") == 0)
206 {
207 if ((strcasecmp (value, "True") == 0)
208 || (strcasecmp (value, "Yes") == 0)
209 || (strcasecmp (value, "On") == 0))
210 sensor_list_action = 1;
211 else
212 sensor_list_action = 0;
213 }
214 else if (strcasecmp (key, "ExtendedSensorNaming") == 0)
215 {
216 if ((strcasecmp (value, "True") == 0)
217 || (strcasecmp (value, "Yes") == 0)
218 || (strcasecmp (value, "On") == 0))
219 sensor_extended_naming = 1;
220 else
221 sensor_extended_naming = 0;
222 }
223 else
224 {
225 return (-1);
226 }
228 return (0);
229 }
231 /*
232 * Check if this feature should be ignored. This is called from
233 * both, `submit' and `write' to give client and server
234 * the ability to ignore certain stuff...
235 */
236 static int config_get_ignored (const char *inst)
237 {
238 int i;
240 /* If no ignored are given collect all features. */
241 if (sensor_list_num < 1)
242 return (0);
244 for (i = 0; i < sensor_list_num; i++)
245 if (strcasecmp (inst, sensor_list[i]) == 0)
246 return (sensor_list_action);
247 return (1 - sensor_list_action);
248 }
250 static void collectd_sensors_init (void)
251 {
252 #ifdef HAVE_LIBSENSORS
253 FILE *fh;
254 featurelist_t *last_feature = NULL;
255 featurelist_t *new_feature;
257 const sensors_chip_name *chip;
258 int chip_num;
260 const sensors_feature_data *data;
261 int data_num0, data_num1;
263 new_feature = first_feature;
264 while (new_feature != NULL)
265 {
266 last_feature = new_feature->next;
267 free (new_feature);
268 new_feature = last_feature;
269 }
271 #ifdef assert
272 assert (new_feature == NULL);
273 assert (last_feature == NULL);
274 #endif
276 if ((fh = fopen ("/etc/sensors.conf", "r")) == NULL)
277 return;
279 if (sensors_init (fh))
280 {
281 fclose (fh);
282 syslog (LOG_ERR, "sensors: Cannot initialize sensors. Data will not be collected.");
283 return;
284 }
286 fclose (fh);
288 chip_num = 0;
289 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
290 {
291 data = NULL;
292 data_num0 = data_num1 = 0;
294 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1)) != NULL)
295 {
296 /* "master features" only */
297 if (data->mapping != SENSORS_NO_MAPPING)
298 continue;
300 /* Only known features */
301 int i = 0;
302 while (known_features[i].type >= 0)
303 {
304 if(strncmp(data->name, known_features[i].label, strlen(known_features[i].label)) == 0)
305 {
306 /* skip ignored in sensors.conf */
307 if (sensors_get_ignored(*chip, data->number) == 0)
308 {
309 break;
310 }
312 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
313 {
314 perror ("malloc");
315 break;
316 }
318 DBG ("Adding feature: %s/%s/%i", chip->prefix, data->name, known_features[i].type);
319 new_feature->chip = chip;
320 new_feature->data = data;
321 new_feature->type = known_features[i].type;
322 new_feature->next = NULL;
324 if (first_feature == NULL)
325 {
326 first_feature = new_feature;
327 last_feature = new_feature;
328 }
329 else
330 {
331 last_feature->next = new_feature;
332 last_feature = new_feature;
333 }
335 /* stop searching known features at first found */
336 break;
337 }
338 i++;
339 }
340 }
341 }
343 if (first_feature == NULL)
344 sensors_cleanup ();
345 #endif /* defined(HAVE_LIBSENSORS) */
347 return;
348 }
350 static void sensors_write (char *host, char *inst, char *val)
351 {
352 char file[BUFSIZE];
353 int status;
354 char *typestart;
356 /* skip ignored in our config */
357 if (config_get_ignored (inst))
358 return;
360 /* extended sensor naming */
361 if(sensor_extended_naming)
362 status = snprintf (file, BUFSIZE, sensor_filename_format, inst);
363 else
364 status = snprintf (file, BUFSIZE, filename_format, inst);
365 if (status < 1)
366 return;
367 else if (status >= BUFSIZE)
368 return;
370 if(sensor_extended_naming)
371 {
372 typestart = strrchr(inst, '/');
373 if(typestart != NULL)
374 {
375 if(strncmp(typestart, sensor_type_prefix[SENSOR_TYPE_VOLTAGE], strlen(sensor_type_prefix[SENSOR_TYPE_VOLTAGE])) == 0)
376 rrd_update_file (host, file, val, sensor_voltage_ds_def, sensor_voltage_ds_num);
377 else
378 rrd_update_file (host, file, val, ds_def, ds_num);
379 }
380 else
381 return;
382 }
383 else
384 rrd_update_file (host, file, val, ds_def, ds_num);
385 }
387 #if SENSORS_HAVE_READ
388 static void sensors_submit (const char *feat_name, const char *chip_prefix, double value)
389 {
390 char buf[BUFSIZE];
391 char inst[BUFSIZE];
393 if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name) >= BUFSIZE)
394 return;
396 /* skip ignored in our config */
397 if (config_get_ignored (inst))
398 return;
400 if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime, value) >= BUFSIZE)
401 return;
403 DBG ("%s, %s", inst, buf);
404 plugin_submit (MODULE_NAME, inst, buf);
405 }
407 static void sensors_read (void)
408 {
409 featurelist_t *feature;
410 double value;
411 char chip_fullprefix[BUFSIZE];
413 for (feature = first_feature; feature != NULL; feature = feature->next)
414 {
415 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
416 continue;
418 if(sensor_extended_naming)
419 {
420 /* full chip name logic borrowed from lm_sensors */
421 if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
422 {
423 if (snprintf (chip_fullprefix, BUFSIZE, "%s-isa-%04x%s", feature->chip->prefix, feature->chip->addr, sensor_type_prefix[feature->type]) >= BUFSIZE)
424 continue;
425 }
426 else if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
427 {
428 if (snprintf (chip_fullprefix, BUFSIZE, "%s-%s-%04x%s", feature->chip->prefix, feature->chip->busname, feature->chip->addr, sensor_type_prefix[feature->type]) >= BUFSIZE)
429 continue;
430 }
431 else
432 {
433 if (snprintf (chip_fullprefix, BUFSIZE, "%s-i2c-%d-%02x%s", feature->chip->prefix, feature->chip->bus, feature->chip->addr, sensor_type_prefix[feature->type]) >= BUFSIZE)
434 continue;
435 }
436 sensors_submit (feature->data->name, (const char *)chip_fullprefix, value);
437 }
438 else
439 {
440 sensors_submit (feature->data->name, feature->chip->prefix, value);
441 }
442 }
443 }
444 #else
445 # define sensors_read NULL
446 #endif /* SENSORS_HAVE_READ */
448 void module_register (void)
449 {
450 plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read, sensors_write);
451 cf_register (MODULE_NAME, sensors_config, config_keys, config_keys_num);
452 }
454 #undef BUFSIZE
455 #undef MODULE_NAME