Code

Merge branch 'collectd-3.11' into collectd-4.0
[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  **/
31 #include "collectd.h"
32 #include "common.h"
33 #include "plugin.h"
34 #include "configfile.h"
35 #include "utils_ignorelist.h"
37 #if defined(HAVE_SENSORS_SENSORS_H)
38 # include <sensors/sensors.h>
39 #else
40 # undef HAVE_LIBSENSORS
41 #endif
43 #if defined(HAVE_LIBSENSORS)
44 # define SENSORS_HAVE_READ 1
45 #else
46 # define SENSORS_HAVE_READ 0
47 #endif
49 #if SENSORS_HAVE_READ
50 #define SENSOR_TYPE_VOLTAGE     0
51 #define SENSOR_TYPE_FANSPEED    1
52 #define SENSOR_TYPE_TEMPERATURE 2
53 #define SENSOR_TYPE_UNKNOWN     3
55 static char *sensor_to_type[] =
56 {
57         "voltage",
58         "fanspeed",
59         "temperature",
60         NULL
61 };
63 struct sensors_labeltypes_s
64 {
65         char *label;
66         int type;
67 };
68 typedef struct sensors_labeltypes_s sensors_labeltypes_t;
70 /*
71  * finite list of known labels extracted from lm_sensors
72  */
73 static sensors_labeltypes_t known_features[] = 
74 {
75         { "fan1", SENSOR_TYPE_FANSPEED },
76         { "fan2", SENSOR_TYPE_FANSPEED },
77         { "fan3", SENSOR_TYPE_FANSPEED },
78         { "fan4", SENSOR_TYPE_FANSPEED },
79         { "fan5", SENSOR_TYPE_FANSPEED },
80         { "fan6", SENSOR_TYPE_FANSPEED },
81         { "fan7", SENSOR_TYPE_FANSPEED },
82         { "AIN2", SENSOR_TYPE_VOLTAGE },
83         { "AIN1", SENSOR_TYPE_VOLTAGE },
84         { "in10", SENSOR_TYPE_VOLTAGE },
85         { "in9", SENSOR_TYPE_VOLTAGE },
86         { "in8", SENSOR_TYPE_VOLTAGE },
87         { "in7", SENSOR_TYPE_VOLTAGE },
88         { "in6", SENSOR_TYPE_VOLTAGE },
89         { "in5", SENSOR_TYPE_VOLTAGE },
90         { "in4", SENSOR_TYPE_VOLTAGE },
91         { "in3", SENSOR_TYPE_VOLTAGE },
92         { "in2", SENSOR_TYPE_VOLTAGE },
93         { "in0", SENSOR_TYPE_VOLTAGE },
94         { "CPU_Temp", SENSOR_TYPE_TEMPERATURE },
95         { "remote_temp", SENSOR_TYPE_TEMPERATURE },
96         { "temp1", SENSOR_TYPE_TEMPERATURE },
97         { "temp2", SENSOR_TYPE_TEMPERATURE },
98         { "temp3", SENSOR_TYPE_TEMPERATURE },
99         { "temp4", SENSOR_TYPE_TEMPERATURE },
100         { "temp5", SENSOR_TYPE_TEMPERATURE },
101         { "temp6", SENSOR_TYPE_TEMPERATURE },
102         { "temp7", SENSOR_TYPE_TEMPERATURE },
103         { "temp", SENSOR_TYPE_TEMPERATURE },
104         { "Vccp2", SENSOR_TYPE_VOLTAGE },
105         { "Vccp1", SENSOR_TYPE_VOLTAGE },
106         { "vdd", SENSOR_TYPE_VOLTAGE },
107         { "vid5", SENSOR_TYPE_VOLTAGE },
108         { "vid4", SENSOR_TYPE_VOLTAGE },
109         { "vid3", SENSOR_TYPE_VOLTAGE },
110         { "vid2", SENSOR_TYPE_VOLTAGE },
111         { "vid1", SENSOR_TYPE_VOLTAGE },
112         { "vid", SENSOR_TYPE_VOLTAGE },
113         { "vin4", SENSOR_TYPE_VOLTAGE },
114         { "vin3", SENSOR_TYPE_VOLTAGE },
115         { "vin2", SENSOR_TYPE_VOLTAGE },
116         { "vin1", SENSOR_TYPE_VOLTAGE },
117         { "voltbatt", SENSOR_TYPE_VOLTAGE },
118         { "volt12", SENSOR_TYPE_VOLTAGE },
119         { "volt5", SENSOR_TYPE_VOLTAGE },
120         { "vrm", SENSOR_TYPE_VOLTAGE },
121         { "5.0V", SENSOR_TYPE_VOLTAGE },
122         { "5V", SENSOR_TYPE_VOLTAGE },
123         { "3.3V", SENSOR_TYPE_VOLTAGE },
124         { "2.5V", SENSOR_TYPE_VOLTAGE },
125         { "2.0V", SENSOR_TYPE_VOLTAGE },
126         { "12V", SENSOR_TYPE_VOLTAGE }
127 };
128 static int known_features_num = STATIC_ARRAY_SIZE (known_features);
129 /* end new naming */
131 static const char *config_keys[] =
133         "Sensor",
134         "IgnoreSelected",
135         NULL
136 };
137 static int config_keys_num = 2;
139 static ignorelist_t *sensor_list;
141 #ifndef SENSORS_CONF_PATH
142 # define SENSORS_CONF_PATH "/etc/sensors.conf"
143 #endif
145 static const char *conffile = SENSORS_CONF_PATH;
146 /* SENSORS_CONF_PATH */
148 /*
149  * remember stat of the loaded config
150  */
151 static time_t sensors_config_mtime = 0;
153 typedef struct featurelist
155         const sensors_chip_name    *chip;
156         const sensors_feature_data *data;
157         int                         type;
158         struct featurelist         *next;
159 } featurelist_t;
161 featurelist_t *first_feature = NULL;
163 static int sensors_config (const char *key, const char *value)
165         if (sensor_list == NULL)
166                 sensor_list = ignorelist_create (1);
168         if (strcasecmp (key, "Sensor") == 0)
169         {
170                 if (ignorelist_add (sensor_list, value))
171                 {
172                         ERROR ("sensors plugin: "
173                                         "Cannot add value to ignorelist.");
174                         return (1);
175                 }
176         }
177         else if (strcasecmp (key, "IgnoreSelected") == 0)
178         {
179                 ignorelist_set_invert (sensor_list, 1);
180                 if ((strcasecmp (value, "True") == 0)
181                                 || (strcasecmp (value, "Yes") == 0)
182                                 || (strcasecmp (value, "On") == 0))
183                         ignorelist_set_invert (sensor_list, 0);
184         }
185         else
186         {
187                 return (-1);
188         }
190         return (0);
193 void sensors_free_features (void)
195         featurelist_t *thisft;
196         featurelist_t *nextft;
198         if (first_feature == NULL)
199                 return;
201         sensors_cleanup ();
203         for (thisft = first_feature; thisft != NULL; thisft = nextft)
204         {
205                 nextft = thisft->next;
206                 sfree (thisft);
207         }
208         first_feature = NULL;
211 static int sensors_load_conf (void)
213         FILE *fh;
214         featurelist_t *last_feature = NULL;
215         featurelist_t *new_feature = NULL;
216         
217         const sensors_chip_name *chip;
218         int chip_num;
220         const sensors_feature_data *data;
221         int data_num0, data_num1;
223         struct stat statbuf;
224         int status;
225         
226         status = stat (conffile, &statbuf);
227         if (status != 0)
228         {
229                 char errbuf[1024];
230                 ERROR ("sensors plugin: stat (%s) failed: %s", conffile,
231                                 sstrerror (errno, errbuf, sizeof (errbuf)));
232                 sensors_config_mtime = 0;
233         }
235         if ((sensors_config_mtime != 0)
236                         && (sensors_config_mtime == statbuf.st_mtime))
237                 return (0);
239         if (sensors_config_mtime != 0)
240         {
241                 NOTICE ("sensors plugin: Reloading config from %s",
242                                 conffile);
243                 sensors_free_features ();
244                 sensors_config_mtime = 0;
245         }
247         fh = fopen (conffile, "r");
248         if (fh == NULL)
249         {
250                 char errbuf[1024];
251                 ERROR ("sensors plugin: fopen(%s) failed: %s", conffile,
252                                 sstrerror (errno, errbuf, sizeof (errbuf)));
253                 return (-1);
254         }
256         status = sensors_init (fh);
257         fclose (fh);
258         if (status != 0)
259         {
260                 ERROR ("sensors plugin: Cannot initialize sensors. "
261                                 "Data will not be collected.");
262                 return (-1);
263         }
265         sensors_config_mtime = statbuf.st_mtime;
267         chip_num = 0;
268         while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
269         {
270                 data = NULL;
271                 data_num0 = data_num1 = 0;
273                 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1))
274                                 != NULL)
275                 {
276                         int i;
278                         /* "master features" only */
279                         if (data->mapping != SENSORS_NO_MAPPING)
280                                 continue;
282                         /* Only known features */
283                         for (i = 0; i < known_features_num; i++)
284                         {
285                                 if (strcmp (data->name, known_features[i].label) != 0)
286                                         continue;
288                                 /* skip ignored in sensors.conf */
289                                 if (sensors_get_ignored (*chip, data->number) == 0)
290                                         break;
292                                 DEBUG ("Adding feature: %s-%s-%s",
293                                                 chip->prefix,
294                                                 sensor_to_type[known_features[i].type],
295                                                 data->name);
297                                 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
298                                 {
299                                         char errbuf[1024];
300                                         ERROR ("sensors plugin: malloc: %s",
301                                                         sstrerror (errno, errbuf, sizeof (errbuf)));
302                                         break;
303                                 }
305                                 new_feature->chip = chip;
306                                 new_feature->data = data;
307                                 new_feature->type = known_features[i].type;
308                                 new_feature->next = NULL;
310                                 if (first_feature == NULL)
311                                 {
312                                         first_feature = new_feature;
313                                         last_feature  = new_feature;
314                                 }
315                                 else
316                                 {
317                                         last_feature->next = new_feature;
318                                         last_feature = new_feature;
319                                 }
321                                 /* stop searching known features at first found */
322                                 break;
323                         } /* for i */
324                 } /* while sensors_get_all_features */
325         } /* while sensors_get_detected_chips */
327         if (first_feature == NULL)
328         {
329                 sensors_cleanup ();
330                 INFO ("sensors plugin: lm_sensors reports no "
331                                 "features. Data will not be collected.");
332                 return (-1);
333         }
335         return (0);
336 } /* int sensors_load_conf */
338 static int sensors_shutdown (void)
340         sensors_free_features ();
341         ignorelist_free (sensor_list);
343         return (0);
344 } /* int sensors_shutdown */
346 static void sensors_submit (const char *plugin_instance,
347                 const char *type, const char *type_instance,
348                 double val)
350         char match_key[1024];
351         int status;
353         value_t values[1];
354         value_list_t vl = VALUE_LIST_INIT;
356         status = snprintf (match_key, sizeof (match_key), "%s/%s-%s",
357                         plugin_instance, type, type_instance);
358         if ((status < 1) || (status >= sizeof (match_key)))
359                 return;
360         match_key[sizeof (match_key) - 1] = '\0';
362         if (sensor_list != NULL)
363         {
364                 DEBUG ("sensors plugin: Checking ignorelist for `%s'", match_key);
365                 if (ignorelist_match (sensor_list, match_key))
366                         return;
367         }
369         values[0].gauge = val;
371         vl.values = values;
372         vl.values_len = 1;
373         vl.time = time (NULL);
374         strcpy (vl.host, hostname_g);
375         strcpy (vl.plugin, "sensors");
376         strcpy (vl.plugin_instance, plugin_instance);
377         strcpy (vl.type_instance, type_instance);
379         plugin_dispatch_values (type, &vl);
380 } /* void sensors_submit */
382 static int sensors_read (void)
384         featurelist_t *feature;
385         double value;
387         char plugin_instance[DATA_MAX_NAME_LEN];
388         char type_instance[DATA_MAX_NAME_LEN];
390         if (sensors_load_conf () != 0)
391                 return (-1);
393         for (feature = first_feature; feature != NULL; feature = feature->next)
394         {
395                 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
396                         continue;
398                 /* full chip name logic borrowed from lm_sensors */
399                 if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
400                 {
401                         if (snprintf (plugin_instance, DATA_MAX_NAME_LEN, "%s-isa-%04x",
402                                                 feature->chip->prefix,
403                                                 feature->chip->addr)
404                                         >= 512)
405                                 continue;
406                 }
407                 else if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
408                 {
409                         if (snprintf (plugin_instance, 512, "%s-%s-%04x",
410                                                 feature->chip->prefix,
411                                                 feature->chip->busname,
412                                                 feature->chip->addr)
413                                         >= 512)
414                                 continue;
415                 }
416                 else
417                 {
418                         if (snprintf (plugin_instance, 512, "%s-i2c-%d-%02x",
419                                                 feature->chip->prefix,
420                                                 feature->chip->bus,
421                                                 feature->chip->addr)
422                                         >= 512)
423                                 continue;
424                 }
426                 strncpy (type_instance, feature->data->name, DATA_MAX_NAME_LEN);
428                 sensors_submit (plugin_instance,
429                                 sensor_to_type[feature->type],
430                                 type_instance,
431                                 value);
432         } /* for feature = first_feature .. NULL */
434         return (0);
435 } /* int sensors_read */
436 #endif /* SENSORS_HAVE_READ */
438 void module_register (void)
440 #if SENSORS_HAVE_READ
441         plugin_register_config ("sensors", sensors_config,
442                         config_keys, config_keys_num);
443         plugin_register_read ("sensors", sensors_read);
444         plugin_register_shutdown ("sensors", sensors_shutdown);
445 #endif
446 } /* void module_register */