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