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