sensors plugin: declare variables only if SENSORS_HAVE_READ
[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 "utils_ignorelist.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 #if SENSORS_HAVE_READ
85 static char *sensor_type_prefix[] =
86 {
87         "unknown",
88         "voltage",
89         "fanspeed",
90         "temperature",
91         NULL
92 };
93 #endif
94
95 typedef struct sensors_labeltypes {
96         char *label;
97         int type;
98 } sensors_labeltypes;
99
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 */
162
163 static char *config_keys[] =
164 {
165         "Sensor",
166         "IgnoreSelected",
167         "ExtendedSensorNaming",
168         NULL
169 };
170 static int config_keys_num = 3;
171
172 static ignorelist_t *sensor_list;
173
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;
180
181 #if SENSORS_HAVE_READ
182 typedef struct featurelist
183 {
184         const sensors_chip_name    *chip;
185         const sensors_feature_data *data;
186         int                         type;
187         struct featurelist         *next;
188 } featurelist_t;
189
190 featurelist_t *first_feature = NULL;
191 #endif /* if SENSORS_HAVE_READ */
192
193 static int sensors_config (char *key, char *value)
194 {
195         if (sensor_list == NULL)
196                 sensor_list = ignorelist_create (1);
197
198         if (strcasecmp (key, "Sensor") == 0)
199         {
200                 if (ignorelist_add (sensor_list, value))
201                 {
202                         syslog (LOG_EMERG, "Cannot add value to ignorelist.");
203                         return (1);
204                 }
205         }
206         else if (strcasecmp (key, "IgnoreSelected") == 0)
207         {
208                 ignorelist_set_invert (sensor_list, 1);
209                 if ((strcasecmp (value, "True") == 0)
210                                 || (strcasecmp (value, "Yes") == 0)
211                                 || (strcasecmp (value, "On") == 0))
212                         ignorelist_set_invert (sensor_list, 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         }
227
228         return (0);
229 }
230
231 static void collectd_sensors_init (void)
232 {
233 #if SENSORS_HAVE_READ
234         FILE *fh;
235         featurelist_t *last_feature = NULL;
236         featurelist_t *new_feature;
237         
238         const sensors_chip_name *chip;
239         int chip_num;
240
241         const sensors_feature_data *data;
242         int data_num0, data_num1;
243         
244         new_feature = first_feature;
245         while (new_feature != NULL)
246         {
247                 last_feature = new_feature->next;
248                 free (new_feature);
249                 new_feature = last_feature;
250         }
251
252 #ifdef assert
253         assert (new_feature == NULL);
254         assert (last_feature == NULL);
255 #endif
256
257         if ((fh = fopen ("/etc/sensors.conf", "r")) == NULL)
258                 return;
259
260         if (sensors_init (fh))
261         {
262                 fclose (fh);
263                 syslog (LOG_ERR, "sensors: Cannot initialize sensors. "
264                                 "Data will not be collected.");
265                 return;
266         }
267
268         fclose (fh);
269
270         chip_num = 0;
271         while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
272         {
273                 data = NULL;
274                 data_num0 = data_num1 = 0;
275
276                 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1))
277                                 != NULL)
278                 {
279                         int i;
280
281                         /* "master features" only */
282                         if (data->mapping != SENSORS_NO_MAPPING)
283                                 continue;
284
285                         /* Only known features */
286                         for (i = 0; known_features[i].type >= 0; i++)
287                         {
288                                 if (strcmp (data->name, known_features[i].label) != 0)
289                                         continue;
290
291                                 /* skip ignored in sensors.conf */
292                                 if (sensors_get_ignored (*chip, data->number) == 0)
293                                         break;
294
295                                 DBG ("Adding feature: %s-%s-%s",
296                                                 chip->prefix,
297                                                 sensor_type_prefix[known_features[i].type],
298                                                 data->name);
299
300                                 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
301                                 {
302                                         DBG ("sensors plugin: malloc: %s",
303                                                         strerror (errno));
304                                         syslog (LOG_ERR, "sensors plugin: malloc: %s",
305                                                         strerror (errno));
306                                         break;
307                                 }
308
309                                 new_feature->chip = chip;
310                                 new_feature->data = data;
311                                 new_feature->type = known_features[i].type;
312                                 new_feature->next = NULL;
313
314                                 if (first_feature == NULL)
315                                 {
316                                         first_feature = new_feature;
317                                         last_feature  = new_feature;
318                                 }
319                                 else
320                                 {
321                                         last_feature->next = new_feature;
322                                         last_feature = new_feature;
323                                 }
324
325                                 /* stop searching known features at first found */
326                                 break;
327                         } /* for i */
328                 } /* while sensors_get_all_features */
329         } /* while sensors_get_detected_chips */
330
331         if (first_feature == NULL)
332                 sensors_cleanup ();
333 #endif /* if SENSORS_HAVE_READ */
334
335         return;
336 }
337
338 static void sensors_shutdown (void)
339 {
340 #if SENSORS_HAVE_READ
341         featurelist_t *thisft = first_feature;
342         featurelist_t *nextft;
343
344         while (thisft != NULL)
345         {
346                 nextft = thisft->next;
347                 sfree (thisft);
348                 thisft = nextft;
349         }
350
351         sensors_cleanup ();
352 #endif /* if SENSORS_HAVE_READ */
353
354         ignorelist_free (sensor_list);
355 }
356
357 static void sensors_voltage_write (char *host, char *inst, char *val)
358 {
359         char file[BUFSIZE];
360         int status;
361
362         /* skip ignored in our config */
363         if (ignorelist_match (sensor_list, inst))
364                 return;
365
366         /* extended sensor naming */
367         if(sensor_extended_naming)
368                 status = snprintf (file, BUFSIZE, extended_filename_format, inst);
369         else
370                 status = snprintf (file, BUFSIZE, old_filename_format, inst);
371
372         if ((status < 1) || (status >= BUFSIZE))
373                 return;
374
375         rrd_update_file (host, file, val, sensor_voltage_ds_def, sensor_voltage_ds_num);
376 }
377
378 static void sensors_write (char *host, char *inst, char *val)
379 {
380         char file[BUFSIZE];
381         int status;
382
383         /* skip ignored in our config */
384         if (ignorelist_match (sensor_list, inst))
385                 return;
386
387         /* extended sensor naming */
388         if (sensor_extended_naming)
389                 status = snprintf (file, BUFSIZE, extended_filename_format, inst);
390         else
391                 status = snprintf (file, BUFSIZE, old_filename_format, inst);
392
393         if ((status < 1) || (status >= BUFSIZE))
394                 return;
395
396         rrd_update_file (host, file, val, ds_def, ds_num);
397 }
398
399 #if SENSORS_HAVE_READ
400 static void sensors_submit (const char *feat_name,
401                 const char *chip_prefix, double value, int type)
402 {
403         char buf[BUFSIZE];
404         char inst[BUFSIZE];
405
406         if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name)
407                         >= BUFSIZE)
408                 return;
409
410         /* skip ignored in our config */
411         if (ignorelist_match (sensor_list, inst))
412                 return;
413
414         if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime,
415                                 value) >= BUFSIZE)
416                 return;
417
418         if (type == SENSOR_TYPE_VOLTAGE)
419         {
420                 DBG ("%s: %s/%s, %s", MODULE_NAME_VOLTAGE,
421                                 sensor_type_prefix[type], inst, buf);
422                 plugin_submit (MODULE_NAME_VOLTAGE, inst, buf);
423         }
424         else
425         {
426                 DBG ("%s: %s/%s, %s", MODULE_NAME,
427                                 sensor_type_prefix[type], inst, buf);
428                 plugin_submit (MODULE_NAME, inst, buf);
429         }
430 }
431
432 static void sensors_read (void)
433 {
434         featurelist_t *feature;
435         double value;
436         char chip_fullprefix[BUFSIZE];
437
438         for (feature = first_feature; feature != NULL; feature = feature->next)
439         {
440                 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
441                         continue;
442
443                 if (sensor_extended_naming)
444                 {
445                         /* full chip name logic borrowed from lm_sensors */
446                         if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
447                         {
448                                 if (snprintf (chip_fullprefix, BUFSIZE, "%s-isa-%04x/%s",
449                                                         feature->chip->prefix,
450                                                         feature->chip->addr,
451                                                         sensor_type_prefix[feature->type])
452                                                 >= BUFSIZE)
453                                         continue;
454                         }
455                         else if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
456                         {
457                                 if (snprintf (chip_fullprefix, BUFSIZE, "%s-%s-%04x/%s",
458                                                         feature->chip->prefix,
459                                                         feature->chip->busname,
460                                                         feature->chip->addr,
461                                                         sensor_type_prefix[feature->type])
462                                                 >= BUFSIZE)
463                                         continue;
464                         }
465                         else
466                         {
467                                 if (snprintf (chip_fullprefix, BUFSIZE, "%s-i2c-%d-%02x/%s",
468                                                         feature->chip->prefix,
469                                                         feature->chip->bus,
470                                                         feature->chip->addr,
471                                                         sensor_type_prefix[feature->type])
472                                                 >= BUFSIZE)
473                                         continue;
474                         }
475
476                         sensors_submit (feature->data->name,
477                                         chip_fullprefix,
478                                         value, feature->type);
479                 }
480                 else
481                 {
482                         sensors_submit (feature->data->name,
483                                         feature->chip->prefix,
484                                         value, feature->type);
485                 }
486         }
487 }
488 #else
489 # define sensors_read NULL
490 #endif /* SENSORS_HAVE_READ */
491
492 void module_register (void)
493 {
494         plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read, sensors_write);
495         plugin_register (MODULE_NAME_VOLTAGE, NULL, NULL, sensors_voltage_write);
496         plugin_register_shutdown (MODULE_NAME, sensors_shutdown);
497         cf_register (MODULE_NAME, sensors_config, config_keys, config_keys_num);
498 }
499
500 #undef BUFSIZE
501 #undef MODULE_NAME