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