Merge branch 'collectd-4.4' into collectd-4.5
[collectd.git] / src / sensors.c
1 /**
2  * collectd - src/sensors.c
3  * Copyright (C) 2005-2008  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 #endif
40
41 #if !defined(SENSORS_API_VERSION)
42 # define SENSORS_API_VERSION 0x000
43 #endif
44
45 /*
46  * The sensors library prior to version 3.0 (internal version 0x400) didn't
47  * report the type of values, only a name. The following lists are there to
48  * convert from the names to the type. They are not used with the new
49  * interface.
50  */
51 #if SENSORS_API_VERSION < 0x400
52 static char *sensor_type_name_map[] =
53 {
54 # define SENSOR_TYPE_VOLTAGE     0
55         "voltage",
56 # define SENSOR_TYPE_FANSPEED    1
57         "fanspeed",
58 # define SENSOR_TYPE_TEMPERATURE 2
59         "temperature",
60 # define SENSOR_TYPE_UNKNOWN     3
61         NULL
62 };
63
64 struct sensors_labeltypes_s
65 {
66         char *label;
67         int type;
68 };
69 typedef struct sensors_labeltypes_s sensors_labeltypes_t;
70
71 /* finite list of known labels extracted from lm_sensors */
72 static sensors_labeltypes_t known_features[] = 
73 {
74         { "fan1", SENSOR_TYPE_FANSPEED },
75         { "fan2", SENSOR_TYPE_FANSPEED },
76         { "fan3", SENSOR_TYPE_FANSPEED },
77         { "fan4", SENSOR_TYPE_FANSPEED },
78         { "fan5", SENSOR_TYPE_FANSPEED },
79         { "fan6", SENSOR_TYPE_FANSPEED },
80         { "fan7", SENSOR_TYPE_FANSPEED },
81         { "AIN2", SENSOR_TYPE_VOLTAGE },
82         { "AIN1", SENSOR_TYPE_VOLTAGE },
83         { "in10", SENSOR_TYPE_VOLTAGE },
84         { "in9", SENSOR_TYPE_VOLTAGE },
85         { "in8", SENSOR_TYPE_VOLTAGE },
86         { "in7", SENSOR_TYPE_VOLTAGE },
87         { "in6", SENSOR_TYPE_VOLTAGE },
88         { "in5", SENSOR_TYPE_VOLTAGE },
89         { "in4", SENSOR_TYPE_VOLTAGE },
90         { "in3", SENSOR_TYPE_VOLTAGE },
91         { "in2", SENSOR_TYPE_VOLTAGE },
92         { "in0", SENSOR_TYPE_VOLTAGE },
93         { "CPU_Temp", SENSOR_TYPE_TEMPERATURE },
94         { "remote_temp", SENSOR_TYPE_TEMPERATURE },
95         { "temp1", SENSOR_TYPE_TEMPERATURE },
96         { "temp2", SENSOR_TYPE_TEMPERATURE },
97         { "temp3", SENSOR_TYPE_TEMPERATURE },
98         { "temp4", SENSOR_TYPE_TEMPERATURE },
99         { "temp5", SENSOR_TYPE_TEMPERATURE },
100         { "temp6", SENSOR_TYPE_TEMPERATURE },
101         { "temp7", SENSOR_TYPE_TEMPERATURE },
102         { "temp", SENSOR_TYPE_TEMPERATURE },
103         { "Vccp2", SENSOR_TYPE_VOLTAGE },
104         { "Vccp1", SENSOR_TYPE_VOLTAGE },
105         { "vdd", SENSOR_TYPE_VOLTAGE },
106         { "vid5", SENSOR_TYPE_VOLTAGE },
107         { "vid4", SENSOR_TYPE_VOLTAGE },
108         { "vid3", SENSOR_TYPE_VOLTAGE },
109         { "vid2", SENSOR_TYPE_VOLTAGE },
110         { "vid1", SENSOR_TYPE_VOLTAGE },
111         { "vid", SENSOR_TYPE_VOLTAGE },
112         { "vin4", SENSOR_TYPE_VOLTAGE },
113         { "vin3", SENSOR_TYPE_VOLTAGE },
114         { "vin2", SENSOR_TYPE_VOLTAGE },
115         { "vin1", SENSOR_TYPE_VOLTAGE },
116         { "voltbatt", SENSOR_TYPE_VOLTAGE },
117         { "volt12", SENSOR_TYPE_VOLTAGE },
118         { "volt5", SENSOR_TYPE_VOLTAGE },
119         { "vrm", SENSOR_TYPE_VOLTAGE },
120         { "5.0V", SENSOR_TYPE_VOLTAGE },
121         { "5V", SENSOR_TYPE_VOLTAGE },
122         { "3.3V", SENSOR_TYPE_VOLTAGE },
123         { "2.5V", SENSOR_TYPE_VOLTAGE },
124         { "2.0V", SENSOR_TYPE_VOLTAGE },
125         { "12V", SENSOR_TYPE_VOLTAGE }
126 };
127 static int known_features_num = STATIC_ARRAY_SIZE (known_features);
128 /* end new naming */
129 #endif /* SENSORS_API_VERSION < 0x400 */
130
131 static const char *config_keys[] =
132 {
133         "Sensor",
134         "IgnoreSelected"
135 };
136 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
137
138 #if SENSORS_API_VERSION < 0x400
139 typedef struct featurelist
140 {
141         const sensors_chip_name    *chip;
142         const sensors_feature_data *data;
143         int                         type;
144         struct featurelist         *next;
145 } featurelist_t;
146
147 # ifndef SENSORS_CONF_PATH
148 #  define SENSORS_CONF_PATH "/etc/sensors.conf"
149 # endif
150 /* #endif SENSORS_API_VERSION < 0x400 */
151
152 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
153 typedef struct featurelist
154 {
155         const sensors_chip_name    *chip;
156         const sensors_feature      *feature;
157         const sensors_subfeature   *subfeature;
158         struct featurelist         *next;
159 } featurelist_t;
160
161 # ifndef SENSORS_CONF_PATH
162 #  define SENSORS_CONF_PATH "/etc/sensors3.conf"
163 # endif
164 /* #endif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
165
166 #else /* if SENSORS_API_VERSION >= 0x500 */
167 # error "This version of libsensors is not supported yet. Please report this " \
168         "as bug."
169 #endif
170
171 static const char *conffile = SENSORS_CONF_PATH;
172 featurelist_t *first_feature = NULL;
173 static ignorelist_t *sensor_list;
174 static time_t sensors_config_mtime = 0;
175
176 #if SENSORS_API_VERSION < 0x400
177 /* full chip name logic borrowed from lm_sensors */
178 static int sensors_snprintf_chip_name (char *buf, size_t buf_size,
179                 const sensors_chip_name *chip)
180 {
181         int status = -1;
182
183         if (chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
184         {
185                 status = ssnprintf (buf, buf_size,
186                                 "%s-isa-%04x",
187                                 chip->prefix,
188                                 chip->addr);
189         }
190         else if (chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
191         {
192                 status = snprintf (buf, buf_size, "%s-%s-%04x",
193                                 chip->prefix,
194                                 chip->busname,
195                                 chip->addr);
196         }
197         else
198         {
199                 status = snprintf (buf, buf_size, "%s-i2c-%d-%02x",
200                                 chip->prefix,
201                                 chip->bus,
202                                 chip->addr);
203         }
204
205         return (status);
206 } /* int sensors_snprintf_chip_name */
207
208 static int sensors_feature_name_to_type (const char *name)
209 {
210         int i;
211
212         /* Yes, this is slow, but it's only ever done during initialization, so
213          * it's a one time cost.. */
214         for (i = 0; i < known_features_num; i++)
215                 if (strcasecmp (known_features[i].label, name) == 0)
216                         return (known_features[i].type);
217
218         return (SENSOR_TYPE_UNKNOWN);
219 } /* int sensors_feature_name_to_type */
220 #endif
221
222 static int sensors_config (const char *key, const char *value)
223 {
224         if (sensor_list == NULL)
225                 sensor_list = ignorelist_create (1);
226
227         if (strcasecmp (key, "Sensor") == 0)
228         {
229                 if (ignorelist_add (sensor_list, value))
230                 {
231                         ERROR ("sensors plugin: "
232                                         "Cannot add value to ignorelist.");
233                         return (1);
234                 }
235         }
236         else if (strcasecmp (key, "IgnoreSelected") == 0)
237         {
238                 ignorelist_set_invert (sensor_list, 1);
239                 if ((strcasecmp (value, "True") == 0)
240                                 || (strcasecmp (value, "Yes") == 0)
241                                 || (strcasecmp (value, "On") == 0))
242                         ignorelist_set_invert (sensor_list, 0);
243         }
244         else
245         {
246                 return (-1);
247         }
248
249         return (0);
250 }
251
252 void sensors_free_features (void)
253 {
254         featurelist_t *thisft;
255         featurelist_t *nextft;
256
257         if (first_feature == NULL)
258                 return;
259
260         sensors_cleanup ();
261
262         for (thisft = first_feature; thisft != NULL; thisft = nextft)
263         {
264                 nextft = thisft->next;
265                 sfree (thisft);
266         }
267         first_feature = NULL;
268 }
269
270 static int sensors_load_conf (void)
271 {
272         FILE *fh;
273         featurelist_t *last_feature = NULL;
274         
275         const sensors_chip_name *chip;
276         int chip_num;
277
278         struct stat statbuf;
279         int status;
280         
281         status = stat (conffile, &statbuf);
282         if (status != 0)
283         {
284                 char errbuf[1024];
285                 ERROR ("sensors plugin: stat (%s) failed: %s", conffile,
286                                 sstrerror (errno, errbuf, sizeof (errbuf)));
287                 sensors_config_mtime = 0;
288         }
289
290         if ((sensors_config_mtime != 0)
291                         && (sensors_config_mtime == statbuf.st_mtime))
292                 return (0);
293
294         if (sensors_config_mtime != 0)
295         {
296                 NOTICE ("sensors plugin: Reloading config from %s",
297                                 conffile);
298                 sensors_free_features ();
299                 sensors_config_mtime = 0;
300         }
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         status = sensors_init (fh);
312         fclose (fh);
313         if (status != 0)
314         {
315                 ERROR ("sensors plugin: Cannot initialize sensors. "
316                                 "Data will not be collected.");
317                 return (-1);
318         }
319
320         sensors_config_mtime = statbuf.st_mtime;
321
322 #if SENSORS_API_VERSION < 0x400
323         chip_num = 0;
324         while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
325         {
326                 int feature_num0 = 0;
327                 int feature_num1 = 0;
328
329                 while (42)
330                 {
331                         const sensors_feature_data *feature;
332                         int feature_type;
333                         featurelist_t *fl;
334
335                         feature = sensors_get_all_features (*chip,
336                                         &feature_num0, &feature_num1);
337
338                         /* Check if all features have been read. */
339                         if (feature == NULL)
340                                 break;
341
342                         /* "master features" only */
343                         if (feature->mapping != SENSORS_NO_MAPPING)
344                         {
345                                 DEBUG ("sensors plugin: sensors_load_conf: "
346                                                 "Ignoring subfeature `%s', "
347                                                 "because (feature->mapping "
348                                                 "!= SENSORS_NO_MAPPING).",
349                                                 feature->name);
350                                 continue;
351                         }
352
353                         /* skip ignored in sensors.conf */
354                         if (sensors_get_ignored (*chip, feature->number) == 0)
355                         {
356                                 DEBUG ("sensors plugin: sensors_load_conf: "
357                                                 "Ignoring subfeature `%s', "
358                                                 "because "
359                                                 "`sensors_get_ignored' told "
360                                                 "me so.",
361                                                 feature->name);
362                                 continue;
363                         }
364
365                         feature_type = sensors_feature_name_to_type (
366                                         feature->name);
367                         if (feature_type == SENSOR_TYPE_UNKNOWN)
368                         {
369                                 DEBUG ("sensors plugin: sensors_load_conf: "
370                                                 "Ignoring subfeature `%s', "
371                                                 "because its type is "
372                                                 "unknown.",
373                                                 feature->name);
374                                 continue;
375                         }
376
377                         fl = (featurelist_t *) malloc (sizeof (featurelist_t));
378                         if (fl == NULL)
379                         {
380                                 ERROR ("sensors plugin: malloc failed.");
381                                 continue;
382                         }
383                         memset (fl, '\0', sizeof (featurelist_t));
384
385                         fl->chip = chip;
386                         fl->data = feature;
387                         fl->type = feature_type;
388
389                         if (first_feature == NULL)
390                                 first_feature = fl;
391                         else
392                                 last_feature->next = fl;
393                         last_feature = fl;
394                 } /* while sensors_get_all_features */
395         } /* while sensors_get_detected_chips */
396 /* #endif SENSORS_API_VERSION < 0x400 */
397
398 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
399         chip_num = 0;
400         while ((chip = sensors_get_detected_chips (NULL, &chip_num)) != NULL)
401         {
402                 const sensors_feature *feature;
403                 int feature_num = 0;
404
405                 while ((feature = sensors_get_features (chip, &feature_num)) != NULL)
406                 {
407                         const sensors_subfeature *subfeature;
408                         int subfeature_num = 0;
409
410                         /* Only handle voltage, fanspeeds and temperatures */
411                         if ((feature->type != SENSORS_FEATURE_IN)
412                                         && (feature->type != SENSORS_FEATURE_FAN)
413                                         && (feature->type != SENSORS_FEATURE_TEMP))
414                         {
415                                 DEBUG ("sensors plugin: sensors_load_conf: "
416                                                 "Ignoring feature `%s', "
417                                                 "because its type is not "
418                                                 "supported.", feature->name);
419                                 continue;
420                         }
421
422                         while ((subfeature = sensors_get_all_subfeatures (chip,
423                                                         feature, &subfeature_num)) != NULL)
424                         {
425                                 featurelist_t *fl;
426
427                                 if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT)
428                                                 && (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT)
429                                                 && (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT))
430                                         continue;
431
432                                 fl = (featurelist_t *) malloc (sizeof (featurelist_t));
433                                 if (fl == NULL)
434                                 {
435                                         ERROR ("sensors plugin: malloc failed.");
436                                         continue;
437                                 }
438                                 memset (fl, '\0', sizeof (featurelist_t));
439
440                                 fl->chip = chip;
441                                 fl->feature = feature;
442                                 fl->subfeature = subfeature;
443
444                                 if (first_feature == NULL)
445                                         first_feature = fl;
446                                 else
447                                         last_feature->next = fl;
448                                 last_feature  = fl;
449                         } /* while (subfeature) */
450                 } /* while (feature) */
451         } /* while (chip) */
452 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
453
454         if (first_feature == NULL)
455         {
456                 sensors_cleanup ();
457                 INFO ("sensors plugin: lm_sensors reports no "
458                                 "features. Data will not be collected.");
459                 return (-1);
460         }
461
462         return (0);
463 } /* int sensors_load_conf */
464
465 static int sensors_shutdown (void)
466 {
467         sensors_free_features ();
468         ignorelist_free (sensor_list);
469
470         return (0);
471 } /* int sensors_shutdown */
472
473 static void sensors_submit (const char *plugin_instance,
474                 const char *type, const char *type_instance,
475                 double val)
476 {
477         char match_key[1024];
478         int status;
479
480         value_t values[1];
481         value_list_t vl = VALUE_LIST_INIT;
482
483         status = ssnprintf (match_key, sizeof (match_key), "%s/%s-%s",
484                         plugin_instance, type, type_instance);
485         if (status < 1)
486                 return;
487
488         if (sensor_list != NULL)
489         {
490                 DEBUG ("sensors plugin: Checking ignorelist for `%s'", match_key);
491                 if (ignorelist_match (sensor_list, match_key))
492                         return;
493         }
494
495         values[0].gauge = val;
496
497         vl.values = values;
498         vl.values_len = 1;
499         vl.time = time (NULL);
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 */