209482e09f88e37a536641bda22d71e2d71810b4
[collectd.git] / src / sensors.c
1 /**
2  * collectd - src/sensors.c
3  * Copyright (C) 2005-2008  Florian octo Forster
4  * Copyright (C) 2006       Luboš Staněk
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
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  *   Henrique de Moraes Holschuh <hmh at debian.org>
32  *   - use default libsensors config file on API 0x400
33  *   - config SensorConfigFile option
34  **/
35
36 #include "collectd.h"
37 #include "common.h"
38 #include "plugin.h"
39 #include "configfile.h"
40 #include "utils_ignorelist.h"
41
42 #if defined(HAVE_SENSORS_SENSORS_H)
43 # include <sensors/sensors.h>
44 #endif
45
46 #if !defined(SENSORS_API_VERSION)
47 # define SENSORS_API_VERSION 0x000
48 #endif
49
50 /*
51  * The sensors library prior to version 3.0 (internal version 0x400) didn't
52  * report the type of values, only a name. The following lists are there to
53  * convert from the names to the type. They are not used with the new
54  * interface.
55  */
56 #if SENSORS_API_VERSION < 0x400
57 static char *sensor_type_name_map[] =
58 {
59 # define SENSOR_TYPE_VOLTAGE     0
60         "voltage",
61 # define SENSOR_TYPE_FANSPEED    1
62         "fanspeed",
63 # define SENSOR_TYPE_TEMPERATURE 2
64         "temperature",
65 # define SENSOR_TYPE_UNKNOWN     3
66         NULL
67 };
68
69 struct sensors_labeltypes_s
70 {
71         char *label;
72         int type;
73 };
74 typedef struct sensors_labeltypes_s sensors_labeltypes_t;
75
76 /* finite list of known labels extracted from lm_sensors */
77 static sensors_labeltypes_t known_features[] = 
78 {
79         { "fan1", SENSOR_TYPE_FANSPEED },
80         { "fan2", SENSOR_TYPE_FANSPEED },
81         { "fan3", SENSOR_TYPE_FANSPEED },
82         { "fan4", SENSOR_TYPE_FANSPEED },
83         { "fan5", SENSOR_TYPE_FANSPEED },
84         { "fan6", SENSOR_TYPE_FANSPEED },
85         { "fan7", SENSOR_TYPE_FANSPEED },
86         { "AIN2", SENSOR_TYPE_VOLTAGE },
87         { "AIN1", SENSOR_TYPE_VOLTAGE },
88         { "in10", SENSOR_TYPE_VOLTAGE },
89         { "in9", SENSOR_TYPE_VOLTAGE },
90         { "in8", SENSOR_TYPE_VOLTAGE },
91         { "in7", SENSOR_TYPE_VOLTAGE },
92         { "in6", SENSOR_TYPE_VOLTAGE },
93         { "in5", SENSOR_TYPE_VOLTAGE },
94         { "in4", SENSOR_TYPE_VOLTAGE },
95         { "in3", SENSOR_TYPE_VOLTAGE },
96         { "in2", SENSOR_TYPE_VOLTAGE },
97         { "in0", SENSOR_TYPE_VOLTAGE },
98         { "CPU_Temp", SENSOR_TYPE_TEMPERATURE },
99         { "remote_temp", SENSOR_TYPE_TEMPERATURE },
100         { "temp1", SENSOR_TYPE_TEMPERATURE },
101         { "temp2", SENSOR_TYPE_TEMPERATURE },
102         { "temp3", SENSOR_TYPE_TEMPERATURE },
103         { "temp4", SENSOR_TYPE_TEMPERATURE },
104         { "temp5", SENSOR_TYPE_TEMPERATURE },
105         { "temp6", SENSOR_TYPE_TEMPERATURE },
106         { "temp7", SENSOR_TYPE_TEMPERATURE },
107         { "temp", SENSOR_TYPE_TEMPERATURE },
108         { "Vccp2", SENSOR_TYPE_VOLTAGE },
109         { "Vccp1", SENSOR_TYPE_VOLTAGE },
110         { "vdd", SENSOR_TYPE_VOLTAGE },
111         { "vid5", SENSOR_TYPE_VOLTAGE },
112         { "vid4", SENSOR_TYPE_VOLTAGE },
113         { "vid3", SENSOR_TYPE_VOLTAGE },
114         { "vid2", SENSOR_TYPE_VOLTAGE },
115         { "vid1", SENSOR_TYPE_VOLTAGE },
116         { "vid", SENSOR_TYPE_VOLTAGE },
117         { "vin4", SENSOR_TYPE_VOLTAGE },
118         { "vin3", SENSOR_TYPE_VOLTAGE },
119         { "vin2", SENSOR_TYPE_VOLTAGE },
120         { "vin1", SENSOR_TYPE_VOLTAGE },
121         { "voltbatt", SENSOR_TYPE_VOLTAGE },
122         { "volt12", SENSOR_TYPE_VOLTAGE },
123         { "volt5", SENSOR_TYPE_VOLTAGE },
124         { "vrm", SENSOR_TYPE_VOLTAGE },
125         { "5.0V", SENSOR_TYPE_VOLTAGE },
126         { "5V", SENSOR_TYPE_VOLTAGE },
127         { "3.3V", SENSOR_TYPE_VOLTAGE },
128         { "2.5V", SENSOR_TYPE_VOLTAGE },
129         { "2.0V", SENSOR_TYPE_VOLTAGE },
130         { "12V", SENSOR_TYPE_VOLTAGE }
131 };
132 static int known_features_num = STATIC_ARRAY_SIZE (known_features);
133 /* end new naming */
134 #endif /* SENSORS_API_VERSION < 0x400 */
135
136 static const char *config_keys[] =
137 {
138         "Sensor",
139         "IgnoreSelected",
140         "SensorConfigFile"
141 };
142 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
143
144 #if SENSORS_API_VERSION < 0x400
145 typedef struct featurelist
146 {
147         const sensors_chip_name    *chip;
148         const sensors_feature_data *data;
149         int                         type;
150         struct featurelist         *next;
151 } featurelist_t;
152
153 # ifndef SENSORS_CONF_PATH
154 #  define SENSORS_CONF_PATH "/etc/sensors.conf"
155 # endif
156 static char *conffile = SENSORS_CONF_PATH;
157 /* #endif SENSORS_API_VERSION < 0x400 */
158
159 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
160 typedef struct featurelist
161 {
162         const sensors_chip_name    *chip;
163         const sensors_feature      *feature;
164         const sensors_subfeature   *subfeature;
165         struct featurelist         *next;
166 } featurelist_t;
167
168 static char *conffile = NULL;
169 /* #endif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
170
171 #else /* if SENSORS_API_VERSION >= 0x500 */
172 # error "This version of libsensors is not supported yet. Please report this " \
173         "as bug."
174 #endif
175
176 featurelist_t *first_feature = NULL;
177 static ignorelist_t *sensor_list;
178
179 #if SENSORS_API_VERSION < 0x400
180 /* full chip name logic borrowed from lm_sensors */
181 static int sensors_snprintf_chip_name (char *buf, size_t buf_size,
182                 const sensors_chip_name *chip)
183 {
184         int status = -1;
185
186         if (chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
187         {
188                 status = ssnprintf (buf, buf_size,
189                                 "%s-isa-%04x",
190                                 chip->prefix,
191                                 chip->addr);
192         }
193         else if (chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
194         {
195                 status = snprintf (buf, buf_size, "%s-%s-%04x",
196                                 chip->prefix,
197                                 chip->busname,
198                                 chip->addr);
199         }
200         else
201         {
202                 status = snprintf (buf, buf_size, "%s-i2c-%d-%02x",
203                                 chip->prefix,
204                                 chip->bus,
205                                 chip->addr);
206         }
207
208         return (status);
209 } /* int sensors_snprintf_chip_name */
210
211 static int sensors_feature_name_to_type (const char *name)
212 {
213         int i;
214
215         /* Yes, this is slow, but it's only ever done during initialization, so
216          * it's a one time cost.. */
217         for (i = 0; i < known_features_num; i++)
218                 if (strcasecmp (known_features[i].label, name) == 0)
219                         return (known_features[i].type);
220
221         return (SENSOR_TYPE_UNKNOWN);
222 } /* int sensors_feature_name_to_type */
223 #endif
224
225 static int sensors_config (const char *key, const char *value)
226 {
227         if (sensor_list == NULL)
228                 sensor_list = ignorelist_create (1);
229
230         /* TODO: This setting exists for compatibility with old versions of
231          * lm-sensors. Remove support for those ancient versions in the next
232          * major release. */
233         if (strcasecmp (key, "SensorConfigFile") == 0)
234         {
235                 char *tmp = strdup (value);
236                 if (tmp != NULL)
237                 {
238                         sfree (conffile);
239                         conffile = tmp;
240                 }
241         }
242         else if (strcasecmp (key, "Sensor") == 0)
243         {
244                 if (ignorelist_add (sensor_list, value))
245                 {
246                         ERROR ("sensors plugin: "
247                                         "Cannot add value to ignorelist.");
248                         return (1);
249                 }
250         }
251         else if (strcasecmp (key, "IgnoreSelected") == 0)
252         {
253                 ignorelist_set_invert (sensor_list, 1);
254                 if (IS_TRUE (value))
255                         ignorelist_set_invert (sensor_list, 0);
256         }
257         else
258         {
259                 return (-1);
260         }
261
262         return (0);
263 }
264
265 void sensors_free_features (void)
266 {
267         featurelist_t *thisft;
268         featurelist_t *nextft;
269
270         if (first_feature == NULL)
271                 return;
272
273         sensors_cleanup ();
274
275         for (thisft = first_feature; thisft != NULL; thisft = nextft)
276         {
277                 nextft = thisft->next;
278                 sfree (thisft);
279         }
280         first_feature = NULL;
281 }
282
283 static int sensors_load_conf (void)
284 {
285         static int call_once = 0;
286
287         FILE *fh = NULL;
288         featurelist_t *last_feature = NULL;
289         
290         const sensors_chip_name *chip;
291         int chip_num;
292
293         int status;
294
295         if (call_once)
296                 return 0;
297
298         call_once = 1;
299
300         if (conffile != NULL)
301         {
302                 fh = fopen (conffile, "r");
303                 if (fh == NULL)
304                 {
305                         char errbuf[1024];
306                         ERROR ("sensors plugin: fopen(%s) failed: %s", conffile,
307                                         sstrerror (errno, errbuf, sizeof (errbuf)));
308                         return (-1);
309                 }
310         }
311
312         status = sensors_init (fh);
313         if (fh)
314                 fclose (fh);
315
316         if (status != 0)
317         {
318                 ERROR ("sensors plugin: Cannot initialize sensors. "
319                                 "Data will not be collected.");
320                 return (-1);
321         }
322
323 #if SENSORS_API_VERSION < 0x400
324         chip_num = 0;
325         while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
326         {
327                 int feature_num0 = 0;
328                 int feature_num1 = 0;
329
330                 while (42)
331                 {
332                         const sensors_feature_data *feature;
333                         int feature_type;
334                         featurelist_t *fl;
335
336                         feature = sensors_get_all_features (*chip,
337                                         &feature_num0, &feature_num1);
338
339                         /* Check if all features have been read. */
340                         if (feature == NULL)
341                                 break;
342
343                         /* "master features" only */
344                         if (feature->mapping != SENSORS_NO_MAPPING)
345                         {
346                                 DEBUG ("sensors plugin: sensors_load_conf: "
347                                                 "Ignoring subfeature `%s', "
348                                                 "because (feature->mapping "
349                                                 "!= SENSORS_NO_MAPPING).",
350                                                 feature->name);
351                                 continue;
352                         }
353
354                         /* skip ignored in sensors.conf */
355                         if (sensors_get_ignored (*chip, feature->number) == 0)
356                         {
357                                 DEBUG ("sensors plugin: sensors_load_conf: "
358                                                 "Ignoring subfeature `%s', "
359                                                 "because "
360                                                 "`sensors_get_ignored' told "
361                                                 "me so.",
362                                                 feature->name);
363                                 continue;
364                         }
365
366                         feature_type = sensors_feature_name_to_type (
367                                         feature->name);
368                         if (feature_type == SENSOR_TYPE_UNKNOWN)
369                         {
370                                 DEBUG ("sensors plugin: sensors_load_conf: "
371                                                 "Ignoring subfeature `%s', "
372                                                 "because its type is "
373                                                 "unknown.",
374                                                 feature->name);
375                                 continue;
376                         }
377
378                         fl = (featurelist_t *) malloc (sizeof (featurelist_t));
379                         if (fl == NULL)
380                         {
381                                 ERROR ("sensors plugin: malloc failed.");
382                                 continue;
383                         }
384                         memset (fl, '\0', sizeof (featurelist_t));
385
386                         fl->chip = chip;
387                         fl->data = feature;
388                         fl->type = feature_type;
389
390                         if (first_feature == NULL)
391                                 first_feature = fl;
392                         else
393                                 last_feature->next = fl;
394                         last_feature = fl;
395                 } /* while sensors_get_all_features */
396         } /* while sensors_get_detected_chips */
397 /* #endif SENSORS_API_VERSION < 0x400 */
398
399 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
400         chip_num = 0;
401         while ((chip = sensors_get_detected_chips (NULL, &chip_num)) != NULL)
402         {
403                 const sensors_feature *feature;
404                 int feature_num = 0;
405
406                 while ((feature = sensors_get_features (chip, &feature_num)) != NULL)
407                 {
408                         const sensors_subfeature *subfeature;
409                         int subfeature_num = 0;
410
411                         /* Only handle voltage, fanspeeds and temperatures */
412                         if ((feature->type != SENSORS_FEATURE_IN)
413                                         && (feature->type != SENSORS_FEATURE_FAN)
414                                         && (feature->type != SENSORS_FEATURE_TEMP))
415                         {
416                                 DEBUG ("sensors plugin: sensors_load_conf: "
417                                                 "Ignoring feature `%s', "
418                                                 "because its type is not "
419                                                 "supported.", feature->name);
420                                 continue;
421                         }
422
423                         while ((subfeature = sensors_get_all_subfeatures (chip,
424                                                         feature, &subfeature_num)) != NULL)
425                         {
426                                 featurelist_t *fl;
427
428                                 if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT)
429                                                 && (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT)
430                                                 && (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT))
431                                         continue;
432
433                                 fl = (featurelist_t *) malloc (sizeof (featurelist_t));
434                                 if (fl == NULL)
435                                 {
436                                         ERROR ("sensors plugin: malloc failed.");
437                                         continue;
438                                 }
439                                 memset (fl, '\0', sizeof (featurelist_t));
440
441                                 fl->chip = chip;
442                                 fl->feature = feature;
443                                 fl->subfeature = subfeature;
444
445                                 if (first_feature == NULL)
446                                         first_feature = fl;
447                                 else
448                                         last_feature->next = fl;
449                                 last_feature  = fl;
450                         } /* while (subfeature) */
451                 } /* while (feature) */
452         } /* while (chip) */
453 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
454
455         if (first_feature == NULL)
456         {
457                 sensors_cleanup ();
458                 INFO ("sensors plugin: lm_sensors reports no "
459                                 "features. Data will not be collected.");
460                 return (-1);
461         }
462
463         return (0);
464 } /* int sensors_load_conf */
465
466 static int sensors_shutdown (void)
467 {
468         sensors_free_features ();
469         ignorelist_free (sensor_list);
470
471         return (0);
472 } /* int sensors_shutdown */
473
474 static void sensors_submit (const char *plugin_instance,
475                 const char *type, const char *type_instance,
476                 double val)
477 {
478         char match_key[1024];
479         int status;
480
481         value_t values[1];
482         value_list_t vl = VALUE_LIST_INIT;
483
484         status = ssnprintf (match_key, sizeof (match_key), "%s/%s-%s",
485                         plugin_instance, type, type_instance);
486         if (status < 1)
487                 return;
488
489         if (sensor_list != NULL)
490         {
491                 DEBUG ("sensors plugin: Checking ignorelist for `%s'", match_key);
492                 if (ignorelist_match (sensor_list, match_key))
493                         return;
494         }
495
496         values[0].gauge = val;
497
498         vl.values = values;
499         vl.values_len = 1;
500
501         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
502         sstrncpy (vl.plugin, "sensors", sizeof (vl.plugin));
503         sstrncpy (vl.plugin_instance, plugin_instance,
504                         sizeof (vl.plugin_instance));
505         sstrncpy (vl.type, type, sizeof (vl.type));
506         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
507
508         plugin_dispatch_values (&vl);
509 } /* void sensors_submit */
510
511 static int sensors_read (void)
512 {
513         featurelist_t *fl;
514
515         if (sensors_load_conf () != 0)
516                 return (-1);
517
518 #if SENSORS_API_VERSION < 0x400
519         for (fl = first_feature; fl != NULL; fl = fl->next)
520         {
521                 double value;
522                 int status;
523                 char plugin_instance[DATA_MAX_NAME_LEN];
524                 char type_instance[DATA_MAX_NAME_LEN];
525
526                 status = sensors_get_feature (*fl->chip,
527                                 fl->data->number, &value);
528                 if (status < 0)
529                         continue;
530
531                 status = sensors_snprintf_chip_name (plugin_instance,
532                                 sizeof (plugin_instance), fl->chip);
533                 if (status < 0)
534                         continue;
535
536                 sstrncpy (type_instance, fl->data->name,
537                                 sizeof (type_instance));
538
539                 sensors_submit (plugin_instance,
540                                 sensor_type_name_map[fl->type],
541                                 type_instance,
542                                 value);
543         } /* for fl = first_feature .. NULL */
544 /* #endif SENSORS_API_VERSION < 0x400 */
545
546 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
547         for (fl = first_feature; fl != NULL; fl = fl->next)
548         {
549                 double value;
550                 int status;
551                 char plugin_instance[DATA_MAX_NAME_LEN];
552                 char type_instance[DATA_MAX_NAME_LEN];
553                 const char *type;
554
555                 status = sensors_get_value (fl->chip,
556                                 fl->subfeature->number, &value);
557                 if (status < 0)
558                         continue;
559
560                 status = sensors_snprintf_chip_name (plugin_instance,
561                                 sizeof (plugin_instance), fl->chip);
562                 if (status < 0)
563                         continue;
564
565                 sstrncpy (type_instance, fl->feature->name,
566                                 sizeof (type_instance));
567
568                 if (fl->feature->type == SENSORS_FEATURE_IN)
569                         type = "voltage";
570                 else if (fl->feature->type
571                                 == SENSORS_FEATURE_FAN)
572                         type = "fanspeed";
573                 else if (fl->feature->type
574                                 == SENSORS_FEATURE_TEMP)
575                         type = "temperature";
576                 else
577                         continue;
578
579                 sensors_submit (plugin_instance, type, type_instance, value);
580         } /* for fl = first_feature .. NULL */
581 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
582
583         return (0);
584 } /* int sensors_read */
585
586 void module_register (void)
587 {
588         plugin_register_config ("sensors", sensors_config,
589                         config_keys, config_keys_num);
590         plugin_register_read ("sensors", sensors_read);
591         plugin_register_shutdown ("sensors", sensors_shutdown);
592 } /* void module_register */