7c05b7a90f21bf731a8e8672e1e6b7685a1dbfa8
[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 #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 */
187
188 /*
189  * remember stat of the loaded config
190  */
191 static struct stat sensors_conf_stat;
192 static int sensors_conf_loaded = 0;
193
194 typedef struct featurelist
195 {
196         const sensors_chip_name    *chip;
197         const sensors_feature_data *data;
198         int                         type;
199         struct featurelist         *next;
200 } featurelist_t;
201
202 featurelist_t *first_feature = NULL;
203 #endif /* if SENSORS_HAVE_READ */
204
205 static int sensors_config (char *key, char *value)
206 {
207         if (sensor_list == NULL)
208                 sensor_list = ignorelist_create (1);
209
210         if (strcasecmp (key, "Sensor") == 0)
211         {
212                 if (ignorelist_add (sensor_list, value))
213                 {
214                         syslog (LOG_EMERG, MODULE_NAME": Cannot add value to ignorelist.");
215                         return (1);
216                 }
217         }
218         else if (strcasecmp (key, "IgnoreSelected") == 0)
219         {
220                 ignorelist_set_invert (sensor_list, 1);
221                 if ((strcasecmp (value, "True") == 0)
222                                 || (strcasecmp (value, "Yes") == 0)
223                                 || (strcasecmp (value, "On") == 0))
224                         ignorelist_set_invert (sensor_list, 0);
225         }
226         else if (strcasecmp (key, "ExtendedSensorNaming") == 0)
227         {
228                 if ((strcasecmp (value, "True") == 0)
229                                 || (strcasecmp (value, "Yes") == 0)
230                                 || (strcasecmp (value, "On") == 0))
231                         sensor_extended_naming = 1;
232                 else
233                         sensor_extended_naming = 0;
234         }
235         else
236         {
237                 return (-1);
238         }
239
240         return (0);
241 }
242
243 #if SENSORS_HAVE_READ
244 void sensors_free_features (void)
245 {
246         featurelist_t *thisft;
247         featurelist_t *nextft;
248
249         if (sensors_conf_loaded)
250         {
251                 sensors_cleanup ();
252                 sensors_conf_loaded = 0;
253         }
254
255         for (thisft = first_feature; thisft != NULL; thisft = nextft)
256         {
257                 nextft = thisft->next;
258                 sfree (thisft);
259         }
260         first_feature = NULL;
261 }
262
263 static void sensors_load_conf (int firsttime)
264 {
265         FILE *fh;
266         featurelist_t *last_feature = NULL;
267         featurelist_t *new_feature;
268         
269         const sensors_chip_name *chip;
270         int chip_num;
271
272         const sensors_feature_data *data;
273         int data_num0, data_num1;
274         
275         sensors_free_features ();
276         new_feature = first_feature;
277
278 #ifdef assert
279         assert (new_feature == NULL);
280         assert (last_feature == NULL);
281 #endif
282
283         if ((fh = fopen (conffile, "r")) == NULL)
284         {
285                 syslog (LOG_ERR, MODULE_NAME": cannot open %s: %s. "
286                                 "Data will not be collected.", conffile, strerror(errno));
287                 return;
288         }
289
290         if (sensors_init (fh))
291         {
292                 fclose (fh);
293                 syslog (LOG_ERR, MODULE_NAME": Cannot initialize sensors. "
294                                 "Data will not be collected.");
295                 return;
296         }
297
298         /* remember file status to detect changes */
299         if (fstat (fileno (fh), &sensors_conf_stat) == -1)
300         {
301                 fclose (fh);
302                 syslog (LOG_ERR, MODULE_NAME": cannot fstat %s: %s "
303                                 "Data will not be collected.", conffile, strerror(errno));
304                 return;
305         }
306
307         fclose (fh);
308
309         chip_num = 0;
310         while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
311         {
312                 data = NULL;
313                 data_num0 = data_num1 = 0;
314
315                 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1))
316                                 != NULL)
317                 {
318                         int i;
319
320                         /* "master features" only */
321                         if (data->mapping != SENSORS_NO_MAPPING)
322                                 continue;
323
324                         /* Only known features */
325                         for (i = 0; known_features[i].type >= 0; i++)
326                         {
327                                 if (strcmp (data->name, known_features[i].label) != 0)
328                                         continue;
329
330                                 /* skip ignored in sensors.conf */
331                                 if (sensors_get_ignored (*chip, data->number) == 0)
332                                         break;
333
334                                 DBG ("Adding feature: %s-%s-%s",
335                                                 chip->prefix,
336                                                 sensor_type_prefix[known_features[i].type],
337                                                 data->name);
338
339                                 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
340                                 {
341                                         DBG ("malloc: %s", strerror (errno));
342                                         syslog (LOG_ERR, MODULE_NAME":  malloc: %s",
343                                                         strerror (errno));
344                                         break;
345                                 }
346
347                                 new_feature->chip = chip;
348                                 new_feature->data = data;
349                                 new_feature->type = known_features[i].type;
350                                 new_feature->next = NULL;
351
352                                 if (first_feature == NULL)
353                                 {
354                                         first_feature = new_feature;
355                                         last_feature  = new_feature;
356                                 }
357                                 else
358                                 {
359                                         last_feature->next = new_feature;
360                                         last_feature = new_feature;
361                                 }
362
363                                 /* stop searching known features at first found */
364                                 break;
365                         } /* for i */
366                 } /* while sensors_get_all_features */
367         } /* while sensors_get_detected_chips */
368
369         if (!firsttime)
370                 syslog (LOG_INFO, MODULE_NAME": lm_sensors' configuration reloaded.");
371
372         if (first_feature == NULL)
373         {
374                 sensors_cleanup ();
375                 sensors_conf_loaded = 0;
376                 syslog (LOG_INFO, MODULE_NAME": lm_sensors reports no features. "
377                         "Data will not be collected.");
378         }
379         else
380                 sensors_conf_loaded = 1;
381 }
382 #endif /* if SENSORS_HAVE_READ */
383
384 static void collectd_sensors_init (void)
385 {
386 #if SENSORS_HAVE_READ
387         sensors_load_conf (1);
388 #endif /* if SENSORS_HAVE_READ */
389 }
390
391 static void sensors_shutdown (void)
392 {
393 #if SENSORS_HAVE_READ
394         sensors_free_features ();
395 #endif /* if SENSORS_HAVE_READ */
396
397         ignorelist_free (sensor_list);
398 }
399
400 static void sensors_voltage_write (char *host, char *inst, char *val)
401 {
402         char file[BUFSIZE];
403         int status;
404
405         /* skip ignored in our config */
406         if (ignorelist_match (sensor_list, inst))
407                 return;
408
409         /* extended sensor naming */
410         if(sensor_extended_naming)
411                 status = snprintf (file, BUFSIZE, extended_filename_format, inst);
412         else
413                 status = snprintf (file, BUFSIZE, old_filename_format, inst);
414
415         if ((status < 1) || (status >= BUFSIZE))
416                 return;
417
418         rrd_update_file (host, file, val, sensor_voltage_ds_def, sensor_voltage_ds_num);
419 }
420
421 static void sensors_write (char *host, char *inst, char *val)
422 {
423         char file[BUFSIZE];
424         int status;
425
426         /* skip ignored in our config */
427         if (ignorelist_match (sensor_list, inst))
428                 return;
429
430         /* extended sensor naming */
431         if (sensor_extended_naming)
432                 status = snprintf (file, BUFSIZE, extended_filename_format, inst);
433         else
434                 status = snprintf (file, BUFSIZE, old_filename_format, inst);
435
436         if ((status < 1) || (status >= BUFSIZE))
437                 return;
438
439         rrd_update_file (host, file, val, ds_def, ds_num);
440 }
441
442 #if SENSORS_HAVE_READ
443 static void sensors_submit (const char *feat_name,
444                 const char *chip_prefix, double value, int type)
445 {
446         char buf[BUFSIZE];
447         char inst[BUFSIZE];
448
449         if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name)
450                         >= BUFSIZE)
451                 return;
452
453         /* skip ignored in our config */
454         if (ignorelist_match (sensor_list, inst))
455                 return;
456
457         if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime,
458                                 value) >= BUFSIZE)
459                 return;
460
461         if (type == SENSOR_TYPE_VOLTAGE)
462         {
463                 DBG ("%s: %s/%s, %s", MODULE_NAME_VOLTAGE,
464                                 sensor_type_prefix[type], inst, buf);
465                 plugin_submit (MODULE_NAME_VOLTAGE, inst, buf);
466         }
467         else
468         {
469                 DBG ("%s: %s/%s, %s", MODULE_NAME,
470                                 sensor_type_prefix[type], inst, buf);
471                 plugin_submit (MODULE_NAME, inst, buf);
472         }
473 }
474
475 static void sensors_read (void)
476 {
477         featurelist_t *feature;
478         double value;
479         char chip_fullprefix[BUFSIZE];
480         struct stat changedbuf;
481
482         /* check sensors.conf for changes */
483         if (sensors_conf_loaded)
484         {
485                 if (stat (conffile, &changedbuf) == -1)
486                 {
487                         syslog (LOG_ERR, MODULE_NAME": cannot stat %s: %s",
488                                         conffile, strerror(errno));
489                         /*
490                          * sensors.conf does not exist,
491                          * throw away previous conf
492                          */
493                         sensors_load_conf (0);
494                 }
495                 else
496                 {
497                         if ((changedbuf.st_size != sensors_conf_stat.st_size) ||
498                                         (changedbuf.st_mtime != sensors_conf_stat.st_mtime) ||
499                                         (changedbuf.st_ino != sensors_conf_stat.st_ino))
500                         {
501                                 sensors_load_conf (0);
502                         }
503                 }
504         }
505
506         for (feature = first_feature; feature != NULL; feature = feature->next)
507         {
508                 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
509                         continue;
510
511                 if (sensor_extended_naming)
512                 {
513                         /* full chip name logic borrowed from lm_sensors */
514                         if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
515                         {
516                                 if (snprintf (chip_fullprefix, BUFSIZE, "%s-isa-%04x/%s",
517                                                         feature->chip->prefix,
518                                                         feature->chip->addr,
519                                                         sensor_type_prefix[feature->type])
520                                                 >= BUFSIZE)
521                                         continue;
522                         }
523                         else if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
524                         {
525                                 if (snprintf (chip_fullprefix, BUFSIZE, "%s-%s-%04x/%s",
526                                                         feature->chip->prefix,
527                                                         feature->chip->busname,
528                                                         feature->chip->addr,
529                                                         sensor_type_prefix[feature->type])
530                                                 >= BUFSIZE)
531                                         continue;
532                         }
533                         else
534                         {
535                                 if (snprintf (chip_fullprefix, BUFSIZE, "%s-i2c-%d-%02x/%s",
536                                                         feature->chip->prefix,
537                                                         feature->chip->bus,
538                                                         feature->chip->addr,
539                                                         sensor_type_prefix[feature->type])
540                                                 >= BUFSIZE)
541                                         continue;
542                         }
543
544                         sensors_submit (feature->data->name,
545                                         chip_fullprefix,
546                                         value, feature->type);
547                 }
548                 else
549                 {
550                         sensors_submit (feature->data->name,
551                                         feature->chip->prefix,
552                                         value, feature->type);
553                 }
554         }
555 }
556 #else
557 # define sensors_read NULL
558 #endif /* SENSORS_HAVE_READ */
559
560 void module_register (void)
561 {
562         plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read, sensors_write);
563         plugin_register (MODULE_NAME_VOLTAGE, NULL, NULL, sensors_voltage_write);
564         plugin_register_shutdown (MODULE_NAME, sensors_shutdown);
565         cf_register (MODULE_NAME, sensors_config, config_keys, config_keys_num);
566 }
567
568 #undef BUFSIZE
569 #undef MODULE_NAME