sensors plugin: Improved the data sources, changed the license and updated the copyri...
[collectd.git] / src / sensors.c
1 /**
2  * collectd - src/sensors.c
3  * Copyright (C) 2005-2007  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; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  *   
21  *   Lubos Stanek <lubek at users.sourceforge.net> Wed Oct 27, 2006
22  *   - config ExtendedSensorNaming option
23  *   - precise sensor feature selection (chip-bus-address/type-feature)
24  *     with ExtendedSensorNaming
25  *   - more sensor features (finite list)
26  *   - honor sensors.conf's ignored
27  *   - config Sensor option
28  *   - config IgnoreSelected option
29  **/
30
31 #include "collectd.h"
32 #include "common.h"
33 #include "plugin.h"
34 #include "configfile.h"
35 #include "utils_ignorelist.h"
36 #include "utils_debug.h"
37
38 #if defined(HAVE_SENSORS_SENSORS_H)
39 # include <sensors/sensors.h>
40 #else
41 # undef HAVE_LIBSENSORS
42 #endif
43
44 #if defined(HAVE_LIBSENSORS)
45 # define SENSORS_HAVE_READ 1
46 #else
47 # define SENSORS_HAVE_READ 0
48 #endif
49
50 static data_source_t data_source_fanspeed[1] =
51 {
52         {"value", DS_TYPE_GAUGE, 0, NAN}
53 };
54
55 static data_set_t fanspeed_ds =
56 {
57         "fanspeed", 1, data_source_fanspeed
58 };
59
60 static data_source_t data_source_temperature[1] =
61 {
62         {"value", DS_TYPE_GAUGE, -273.15, NAN}
63 };
64
65 static data_set_t temperature_ds =
66 {
67         "temperature", 1, data_source_temperature
68 };
69
70 static data_source_t data_source_voltage[1] =
71 {
72         {"voltage", DS_TYPE_GAUGE, NAN, NAN}
73 };
74
75 static data_set_t voltage_ds =
76 {
77         "voltage", 1, data_source_voltage
78 };
79
80 #if SENSORS_HAVE_READ
81 #define SENSOR_TYPE_VOLTAGE     0
82 #define SENSOR_TYPE_FANSPEED    1
83 #define SENSOR_TYPE_TEMPERATURE 2
84 #define SENSOR_TYPE_UNKNOWN     3
85
86 static char *sensor_to_type[] =
87 {
88         "voltage",
89         "fanspeed",
90         "temperature",
91         NULL
92 };
93
94 struct sensors_labeltypes_s
95 {
96         char *label;
97         int type;
98 };
99 typedef struct sensors_labeltypes_s sensors_labeltypes_t;
100
101 /*
102  * finite list of known labels extracted from lm_sensors
103  */
104 static sensors_labeltypes_t 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         { (char *) 0, SENSOR_TYPE_UNKNOWN }
159 };
160 /* end new naming */
161
162 static const char *config_keys[] =
163 {
164         "Sensor",
165         "IgnoreSelected",
166         NULL
167 };
168 static int config_keys_num = 2;
169
170 static ignorelist_t *sensor_list;
171
172 #ifndef SENSORS_CONF_PATH
173 # define SENSORS_CONF_PATH "/etc/sensors.conf"
174 #endif
175
176 static const char *conffile = SENSORS_CONF_PATH;
177 /* SENSORS_CONF_PATH */
178
179 /*
180  * remember stat of the loaded config
181  */
182 static time_t sensors_config_mtime = 0;
183
184 typedef struct featurelist
185 {
186         const sensors_chip_name    *chip;
187         const sensors_feature_data *data;
188         int                         type;
189         struct featurelist         *next;
190 } featurelist_t;
191
192 featurelist_t *first_feature = NULL;
193
194 static int sensors_config (const char *key, const char *value)
195 {
196         if (sensor_list == NULL)
197                 sensor_list = ignorelist_create (1);
198
199         if (strcasecmp (key, "Sensor") == 0)
200         {
201                 if (ignorelist_add (sensor_list, value))
202                 {
203                         syslog (LOG_ERR, "sensors plugin: "
204                                         "Cannot add value to ignorelist.");
205                         return (1);
206                 }
207         }
208         else if (strcasecmp (key, "IgnoreSelected") == 0)
209         {
210                 ignorelist_set_invert (sensor_list, 1);
211                 if ((strcasecmp (value, "True") == 0)
212                                 || (strcasecmp (value, "Yes") == 0)
213                                 || (strcasecmp (value, "On") == 0))
214                         ignorelist_set_invert (sensor_list, 0);
215         }
216         else
217         {
218                 return (-1);
219         }
220
221         return (0);
222 }
223
224 void sensors_free_features (void)
225 {
226         featurelist_t *thisft;
227         featurelist_t *nextft;
228
229         if (first_feature == NULL)
230                 return;
231
232         sensors_cleanup ();
233
234         for (thisft = first_feature; thisft != NULL; thisft = nextft)
235         {
236                 nextft = thisft->next;
237                 sfree (thisft);
238         }
239         first_feature = NULL;
240 }
241
242 static void sensors_load_conf (void)
243 {
244         FILE *fh;
245         featurelist_t *last_feature = NULL;
246         featurelist_t *new_feature = NULL;
247         
248         const sensors_chip_name *chip;
249         int chip_num;
250
251         const sensors_feature_data *data;
252         int data_num0, data_num1;
253
254         struct stat statbuf;
255         int status;
256         
257         status = stat (conffile, &statbuf);
258         if (status != 0)
259         {
260                 syslog (LOG_ERR, "sensors plugin: stat (%s) failed: %s",
261                                 conffile, strerror (errno));
262                 sensors_config_mtime = 0;
263         }
264
265         if ((sensors_config_mtime != 0)
266                         && (sensors_config_mtime == statbuf.st_mtime))
267                 return;
268
269         if (sensors_config_mtime != 0)
270         {
271                 syslog (LOG_NOTICE, "sensors plugin: Reloading config from %s",
272                                 conffile);
273                 sensors_free_features ();
274                 sensors_config_mtime = 0;
275         }
276
277         fh = fopen (conffile, "r");
278         if (fh == NULL)
279         {
280                 syslog (LOG_ERR, "sensors plugin: fopen(%s) failed: %s",
281                                 conffile, strerror(errno));
282                 return;
283         }
284
285         status = sensors_init (fh);
286         fclose (fh);
287         if (status != 0)
288         {
289                 syslog (LOG_ERR, "sensors plugin: Cannot initialize sensors. "
290                                 "Data will not be collected.");
291                 return;
292         }
293
294         sensors_config_mtime = statbuf.st_mtime;
295
296         chip_num = 0;
297         while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
298         {
299                 data = NULL;
300                 data_num0 = data_num1 = 0;
301
302                 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1))
303                                 != NULL)
304                 {
305                         int i;
306
307                         /* "master features" only */
308                         if (data->mapping != SENSORS_NO_MAPPING)
309                                 continue;
310
311                         /* Only known features */
312                         for (i = 0; known_features[i].type >= 0; i++)
313                         {
314                                 if (strcmp (data->name, known_features[i].label) != 0)
315                                         continue;
316
317                                 /* skip ignored in sensors.conf */
318                                 if (sensors_get_ignored (*chip, data->number) == 0)
319                                         break;
320
321                                 DBG ("Adding feature: %s-%s-%s",
322                                                 chip->prefix,
323                                                 sensor_type_prefix[known_features[i].type],
324                                                 data->name);
325
326                                 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
327                                 {
328                                         DBG ("malloc: %s", strerror (errno));
329                                         syslog (LOG_ERR, "sensors plugin:  malloc: %s",
330                                                         strerror (errno));
331                                         break;
332                                 }
333
334                                 new_feature->chip = chip;
335                                 new_feature->data = data;
336                                 new_feature->type = known_features[i].type;
337                                 new_feature->next = NULL;
338
339                                 if (first_feature == NULL)
340                                 {
341                                         first_feature = new_feature;
342                                         last_feature  = new_feature;
343                                 }
344                                 else
345                                 {
346                                         last_feature->next = new_feature;
347                                         last_feature = new_feature;
348                                 }
349
350                                 /* stop searching known features at first found */
351                                 break;
352                         } /* for i */
353                 } /* while sensors_get_all_features */
354         } /* while sensors_get_detected_chips */
355
356         if (first_feature == NULL)
357         {
358                 sensors_cleanup ();
359                 syslog (LOG_INFO, "sensors plugin: lm_sensors reports no "
360                                 "features. Data will not be collected.");
361         }
362 } /* void sensors_load_conf */
363
364 static int sensors_shutdown (void)
365 {
366         sensors_free_features ();
367         ignorelist_free (sensor_list);
368
369         return (0);
370 } /* int sensors_shutdown */
371
372 static void sensors_submit (const char *plugin_instance,
373                 const char *type, const char *type_instance,
374                 double val)
375 {
376         value_t values[1];
377         value_list_t vl = VALUE_LIST_INIT;
378
379         if (ignorelist_match (sensor_list, type_instance))
380                 return;
381
382         values[0].gauge = val;
383
384         vl.values = values;
385         vl.values_len = 1;
386         vl.time = time (NULL);
387         strcpy (vl.host, hostname);
388         strcpy (vl.plugin, "sensors");
389         strcpy (vl.plugin_instance, plugin_instance);
390         strcpy (vl.type_instance, type_instance);
391
392         plugin_dispatch_values (type, &vl);
393 } /* void sensors_submit */
394
395 static int sensors_read (void)
396 {
397         featurelist_t *feature;
398         double value;
399         char chip_fullprefix[512];
400
401         char plugin_instance[DATA_MAX_NAME_LEN];
402         char type_instance[DATA_MAX_NAME_LEN];
403
404         sensors_load_conf ();
405
406         for (feature = first_feature; feature != NULL; feature = feature->next)
407         {
408                 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
409                         continue;
410
411                 /* full chip name logic borrowed from lm_sensors */
412                 if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
413                 {
414                         if (snprintf (plugin_instance, DATA_MAX_NAME_LEN, "%s-isa-%04x",
415                                                 feature->chip->prefix,
416                                                 feature->chip->addr)
417                                         >= 512)
418                                 continue;
419                 }
420                 else if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
421                 {
422                         if (snprintf (plugin_instance, 512, "%s-%s-%04x",
423                                                 feature->chip->prefix,
424                                                 feature->chip->busname,
425                                                 feature->chip->addr)
426                                         >= 512)
427                                 continue;
428                 }
429                 else
430                 {
431                         if (snprintf (plugin_instance, 512, "%s-i2c-%d-%02x",
432                                                 feature->chip->prefix,
433                                                 feature->chip->bus,
434                                                 feature->chip->addr)
435                                         >= 512)
436                                 continue;
437                 }
438
439                 strncpy (type_instance, feature->data->name, DATA_MAX_NAME_LEN);
440
441                 sensors_submit (plugin_instance,
442                                 sensor_to_type[feature->type]
443                                 type_instance,
444                                 value);
445         } /* for feature = first_feature .. NULL */
446 } /* int sensors_read */
447 #endif /* SENSORS_HAVE_READ */
448
449 void module_register (void)
450 {
451         plugin_register_data_set (&fanspeed_ds);
452         plugin_register_data_set (&temperature_ds);
453         plugin_register_data_set (&voltage_ds);
454
455 #if SENSORS_HAVE_READ
456         plugin_register_config ("sensors", sensors_config,
457                         config_keys, config_keys_num);
458         plugin_register_read ("sensors", sensors_read);
459         plugin_register_shutdown ("sensors", sensors_shutdown);
460 #endif
461 } /* void module_register */