Improve and generalize the ignorelist functionality.
[collectd.git] / src / sensors.c
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  **/
31
32 #include "collectd.h"
33 #include "common.h"
34 #include "plugin.h"
35 #include "configfile.h"
36 #include "config_list.h"
37 #include "utils_debug.h"
38
39 #define MODULE_NAME "sensors"
40 #define MODULE_NAME_VOLTAGE MODULE_NAME"_voltage"
41
42 #if defined(HAVE_SENSORS_SENSORS_H)
43 # include <sensors/sensors.h>
44 #else
45 # undef HAVE_LIBSENSORS
46 #endif
47
48 #if defined(HAVE_LIBSENSORS)
49 # define SENSORS_HAVE_READ 1
50 #else
51 # define SENSORS_HAVE_READ 0
52 #endif
53
54 #define BUFSIZE 512
55
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;
63
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;
71
72 /* old naming */
73 static char *old_filename_format = "sensors-%s.rrd";
74 /* end old naming */
75
76 /* new naming <chip-bus-address/type-feature */
77 static char *extended_filename_format = "lm_sensors-%s.rrd";
78
79 #define SENSOR_TYPE_UNKNOWN 0
80 #define SENSOR_TYPE_VOLTAGE 1
81 #define SENSOR_TYPE_FANSPEED 2
82 #define SENSOR_TYPE_TEMPERATURE 3
83
84 static char *sensor_type_prefix[] =
85 {
86         "unknown",
87         "voltage",
88         "fanspeed",
89         "temperature",
90         NULL
91 };
92
93 typedef struct sensors_labeltypes {
94         char *label;
95         int type;
96 } sensors_labeltypes;
97
98 /*
99  * finite list of known labels extracted from lm_sensors
100  */
101 static sensors_labeltypes known_features[] = 
102 {
103         { "fan1", SENSOR_TYPE_FANSPEED },
104         { "fan2", SENSOR_TYPE_FANSPEED },
105         { "fan3", SENSOR_TYPE_FANSPEED },
106         { "fan4", SENSOR_TYPE_FANSPEED },
107         { "fan5", SENSOR_TYPE_FANSPEED },
108         { "fan6", SENSOR_TYPE_FANSPEED },
109         { "fan7", SENSOR_TYPE_FANSPEED },
110         { "AIN2", SENSOR_TYPE_VOLTAGE },
111         { "AIN1", SENSOR_TYPE_VOLTAGE },
112         { "in10", SENSOR_TYPE_VOLTAGE },
113         { "in9", SENSOR_TYPE_VOLTAGE },
114         { "in8", SENSOR_TYPE_VOLTAGE },
115         { "in7", SENSOR_TYPE_VOLTAGE },
116         { "in6", SENSOR_TYPE_VOLTAGE },
117         { "in5", SENSOR_TYPE_VOLTAGE },
118         { "in4", SENSOR_TYPE_VOLTAGE },
119         { "in3", SENSOR_TYPE_VOLTAGE },
120         { "in2", SENSOR_TYPE_VOLTAGE },
121         { "in0", SENSOR_TYPE_VOLTAGE },
122         { "CPU_Temp", SENSOR_TYPE_TEMPERATURE },
123         { "remote_temp", SENSOR_TYPE_TEMPERATURE },
124         { "temp1", SENSOR_TYPE_TEMPERATURE },
125         { "temp2", SENSOR_TYPE_TEMPERATURE },
126         { "temp3", SENSOR_TYPE_TEMPERATURE },
127         { "temp4", SENSOR_TYPE_TEMPERATURE },
128         { "temp5", SENSOR_TYPE_TEMPERATURE },
129         { "temp6", SENSOR_TYPE_TEMPERATURE },
130         { "temp7", SENSOR_TYPE_TEMPERATURE },
131         { "temp", SENSOR_TYPE_TEMPERATURE },
132         { "Vccp2", SENSOR_TYPE_VOLTAGE },
133         { "Vccp1", SENSOR_TYPE_VOLTAGE },
134         { "vdd", SENSOR_TYPE_VOLTAGE },
135         { "vid5", SENSOR_TYPE_VOLTAGE },
136         { "vid4", SENSOR_TYPE_VOLTAGE },
137         { "vid3", SENSOR_TYPE_VOLTAGE },
138         { "vid2", SENSOR_TYPE_VOLTAGE },
139         { "vid1", SENSOR_TYPE_VOLTAGE },
140         { "vid", SENSOR_TYPE_VOLTAGE },
141         { "vin4", SENSOR_TYPE_VOLTAGE },
142         { "vin3", SENSOR_TYPE_VOLTAGE },
143         { "vin2", SENSOR_TYPE_VOLTAGE },
144         { "vin1", SENSOR_TYPE_VOLTAGE },
145         { "voltbatt", SENSOR_TYPE_VOLTAGE },
146         { "volt12", SENSOR_TYPE_VOLTAGE },
147         { "volt5", SENSOR_TYPE_VOLTAGE },
148         { "vrm", SENSOR_TYPE_VOLTAGE },
149         { "5.0V", SENSOR_TYPE_VOLTAGE },
150         { "5V", SENSOR_TYPE_VOLTAGE },
151         { "3.3V", SENSOR_TYPE_VOLTAGE },
152         { "2.5V", SENSOR_TYPE_VOLTAGE },
153         { "2.0V", SENSOR_TYPE_VOLTAGE },
154         { "12V", SENSOR_TYPE_VOLTAGE },
155         { 0, -1 }
156 };
157 /* end new naming */
158
159 static char *config_keys[] =
160 {
161         "Sensor",
162         "IgnoreSelected",
163         "ExtendedSensorNaming",
164         NULL
165 };
166 static int config_keys_num = 3;
167
168 static configlist_t *sensor_list;
169
170 /* 
171  * sensor_extended_naming:
172  * 0 => default is to create chip-feature
173  * 1 => use new naming scheme chip-bus-address/type-feature
174  */
175 static int sensor_extended_naming = 0;
176
177 #ifdef HAVE_LIBSENSORS
178 typedef struct featurelist
179 {
180         const sensors_chip_name    *chip;
181         const sensors_feature_data *data;
182         int                         type;
183         struct featurelist         *next;
184 } featurelist_t;
185
186 featurelist_t *first_feature = NULL;
187 #endif /* defined (HAVE_LIBSENSORS) */
188
189 static int sensors_config (char *key, char *value)
190 {
191         if (sensor_list == NULL)
192                 sensor_list = configlist_init();
193
194         if (strcasecmp (key, "Sensor") == 0)
195         {
196                 if (!configlist_add (sensor_list, value))
197                 {
198                         syslog (LOG_EMERG, "Cannot add value.");
199                         return (1);
200                 }
201         }
202         else if (strcasecmp (key, "IgnoreSelected") == 0)
203         {
204                 if ((strcasecmp (value, "True") == 0)
205                                 || (strcasecmp (value, "Yes") == 0)
206                                 || (strcasecmp (value, "On") == 0))
207                         configlist_ignore (sensor_list, 1);
208         }
209         else if (strcasecmp (key, "ExtendedSensorNaming") == 0)
210         {
211                 if ((strcasecmp (value, "True") == 0)
212                                 || (strcasecmp (value, "Yes") == 0)
213                                 || (strcasecmp (value, "On") == 0))
214                         sensor_extended_naming = 1;
215                 else
216                         sensor_extended_naming = 0;
217         }
218         else
219         {
220                 return (-1);
221         }
222
223         return (0);
224 }
225
226 static void collectd_sensors_init (void)
227 {
228 #ifdef HAVE_LIBSENSORS
229         FILE *fh;
230         featurelist_t *last_feature = NULL;
231         featurelist_t *new_feature;
232         
233         const sensors_chip_name *chip;
234         int chip_num;
235
236         const sensors_feature_data *data;
237         int data_num0, data_num1;
238         
239         new_feature = first_feature;
240         while (new_feature != NULL)
241         {
242                 last_feature = new_feature->next;
243                 free (new_feature);
244                 new_feature = last_feature;
245         }
246
247 #ifdef assert
248         assert (new_feature == NULL);
249         assert (last_feature == NULL);
250 #endif
251
252         if ((fh = fopen ("/etc/sensors.conf", "r")) == NULL)
253                 return;
254
255         if (sensors_init (fh))
256         {
257                 fclose (fh);
258                 syslog (LOG_ERR, "sensors: Cannot initialize sensors. "
259                                 "Data will not be collected.");
260                 return;
261         }
262
263         fclose (fh);
264
265         chip_num = 0;
266         while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
267         {
268                 data = NULL;
269                 data_num0 = data_num1 = 0;
270
271                 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1))
272                                 != NULL)
273                 {
274                         int i;
275
276                         /* "master features" only */
277                         if (data->mapping != SENSORS_NO_MAPPING)
278                                 continue;
279
280                         /* Only known features */
281                         for (i = 0; known_features[i].type >= 0; i++)
282                         {
283                                 if (strcmp (data->name, known_features[i].label) != 0)
284                                         continue;
285
286                                 /* skip ignored in sensors.conf */
287                                 if (sensors_get_ignored (*chip, data->number) == 0)
288                                         break;
289
290                                 DBG ("Adding feature: %s-%s-%s",
291                                                 chip->prefix,
292                                                 sensor_type_prefix[known_features[i].type],
293                                                 data->name);
294
295                                 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
296                                 {
297                                         DBG ("sensors plugin: malloc: %s",
298                                                         strerror (errno));
299                                         syslog (LOG_ERR, "sensors plugin: malloc: %s",
300                                                         strerror (errno));
301                                         break;
302                                 }
303
304                                 new_feature->chip = chip;
305                                 new_feature->data = data;
306                                 new_feature->type = known_features[i].type;
307                                 new_feature->next = NULL;
308
309                                 if (first_feature == NULL)
310                                 {
311                                         first_feature = new_feature;
312                                         last_feature  = new_feature;
313                                 }
314                                 else
315                                 {
316                                         last_feature->next = new_feature;
317                                         last_feature = new_feature;
318                                 }
319
320                                 /* stop searching known features at first found */
321                                 break;
322                         } /* for i */
323                 } /* while sensors_get_all_features */
324         } /* while sensors_get_detected_chips */
325
326         if (first_feature == NULL)
327                 sensors_cleanup ();
328 #endif /* defined(HAVE_LIBSENSORS) */
329
330         return;
331 }
332
333 static void sensors_voltage_write (char *host, char *inst, char *val)
334 {
335         char file[BUFSIZE];
336         int status;
337
338         /* skip ignored in our config */
339         if (configlist_ignored (sensor_list, inst))
340                 return;
341
342         /* extended sensor naming */
343         if(sensor_extended_naming)
344                 status = snprintf (file, BUFSIZE, extended_filename_format, inst);
345         else
346                 status = snprintf (file, BUFSIZE, old_filename_format, inst);
347
348         if ((status < 1) || (status >= BUFSIZE))
349                 return;
350
351         rrd_update_file (host, file, val, sensor_voltage_ds_def, sensor_voltage_ds_num);
352 }
353
354 static void sensors_write (char *host, char *inst, char *val)
355 {
356         char file[BUFSIZE];
357         int status;
358
359         /* skip ignored in our config */
360         if (configlist_ignored (sensor_list, inst))
361                 return;
362
363         /* extended sensor naming */
364         if (sensor_extended_naming)
365                 status = snprintf (file, BUFSIZE, extended_filename_format, inst);
366         else
367                 status = snprintf (file, BUFSIZE, old_filename_format, inst);
368
369         if ((status < 1) || (status >= BUFSIZE))
370                 return;
371
372         rrd_update_file (host, file, val, ds_def, ds_num);
373 }
374
375 #if SENSORS_HAVE_READ
376 static void sensors_submit (const char *feat_name,
377                 const char *chip_prefix, double value, int type)
378 {
379         char buf[BUFSIZE];
380         char inst[BUFSIZE];
381
382         if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name)
383                         >= BUFSIZE)
384                 return;
385
386         /* skip ignored in our config */
387         if (configlist_ignored (sensor_list, inst))
388                 return;
389
390         if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime,
391                                 value) >= BUFSIZE)
392                 return;
393
394         if (type == SENSOR_TYPE_VOLTAGE)
395         {
396                 DBG ("%s: %s/%s, %s", MODULE_NAME_VOLTAGE,
397                                 sensor_type_prefix[type], inst, buf);
398                 plugin_submit (MODULE_NAME_VOLTAGE, inst, buf);
399         }
400         else
401         {
402                 DBG ("%s: %s/%s, %s", MODULE_NAME,
403                                 sensor_type_prefix[type], inst, buf);
404                 plugin_submit (MODULE_NAME, inst, buf);
405         }
406 }
407
408 static void sensors_read (void)
409 {
410         featurelist_t *feature;
411         double value;
412         char chip_fullprefix[BUFSIZE];
413
414         for (feature = first_feature; feature != NULL; feature = feature->next)
415         {
416                 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
417                         continue;
418
419                 if (sensor_extended_naming)
420                 {
421                         /* full chip name logic borrowed from lm_sensors */
422                         if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
423                         {
424                                 if (snprintf (chip_fullprefix, BUFSIZE, "%s-isa-%04x/%s",
425                                                         feature->chip->prefix,
426                                                         feature->chip->addr,
427                                                         sensor_type_prefix[feature->type])
428                                                 >= BUFSIZE)
429                                         continue;
430                         }
431                         else if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
432                         {
433                                 if (snprintf (chip_fullprefix, BUFSIZE, "%s-%s-%04x/%s",
434                                                         feature->chip->prefix,
435                                                         feature->chip->busname,
436                                                         feature->chip->addr,
437                                                         sensor_type_prefix[feature->type])
438                                                 >= BUFSIZE)
439                                         continue;
440                         }
441                         else
442                         {
443                                 if (snprintf (chip_fullprefix, BUFSIZE, "%s-i2c-%d-%02x/%s",
444                                                         feature->chip->prefix,
445                                                         feature->chip->bus,
446                                                         feature->chip->addr,
447                                                         sensor_type_prefix[feature->type])
448                                                 >= BUFSIZE)
449                                         continue;
450                         }
451
452                         sensors_submit (feature->data->name,
453                                         chip_fullprefix,
454                                         value, feature->type);
455                 }
456                 else
457                 {
458                         sensors_submit (feature->data->name,
459                                         feature->chip->prefix,
460                                         value, feature->type);
461                 }
462         }
463 }
464 #else
465 # define sensors_read NULL
466 #endif /* SENSORS_HAVE_READ */
467
468 void module_register (void)
469 {
470         plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read, sensors_write);
471         plugin_register (MODULE_NAME_VOLTAGE, NULL, NULL, sensors_voltage_write);
472         cf_register (MODULE_NAME, sensors_config, config_keys, config_keys_num);
473 }
474
475 #undef BUFSIZE
476 #undef MODULE_NAME