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