Code

sensors plugin: Fix initialization code.
[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  **/
36 #include "collectd.h"
37 #include "common.h"
38 #include "plugin.h"
39 #include "configfile.h"
40 #include "utils_ignorelist.h"
42 #if defined(HAVE_SENSORS_SENSORS_H)
43 # include <sensors/sensors.h>
44 #endif
46 #if !defined(SENSORS_API_VERSION)
47 # define SENSORS_API_VERSION 0x000
48 #endif
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 };
69 struct sensors_labeltypes_s
70 {
71         char *label;
72         int type;
73 };
74 typedef struct sensors_labeltypes_s sensors_labeltypes_t;
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 */
136 static const char *config_keys[] =
138         "Sensor",
139         "IgnoreSelected",
140         "SensorConfigFile"
141 };
142 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
144 #if SENSORS_API_VERSION < 0x400
145 typedef struct featurelist
147         const sensors_chip_name    *chip;
148         const sensors_feature_data *data;
149         int                         type;
150         struct featurelist         *next;
151 } featurelist_t;
153 # ifndef SENSORS_CONF_PATH
154 #  define SENSORS_CONF_PATH "/etc/sensors.conf"
155 # endif
156 static const char *conffile = SENSORS_CONF_PATH;
157 /* #endif SENSORS_API_VERSION < 0x400 */
159 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
160 typedef struct featurelist
162         const sensors_chip_name    *chip;
163         const sensors_feature      *feature;
164         const sensors_subfeature   *subfeature;
165         struct featurelist         *next;
166 } featurelist_t;
168 static const char *conffile = NULL;
169 /* #endif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
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
176 featurelist_t *first_feature = NULL;
177 static ignorelist_t *sensor_list;
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)
184         int status = -1;
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         }
208         return (status);
209 } /* int sensors_snprintf_chip_name */
211 static int sensors_feature_name_to_type (const char *name)
213         int i;
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);
221         return (SENSOR_TYPE_UNKNOWN);
222 } /* int sensors_feature_name_to_type */
223 #endif
225 static int sensors_config (const char *key, const char *value)
227         if (sensor_list == NULL)
228                 sensor_list = ignorelist_create (1);
230         if (strcasecmp (key, "SensorConfigFile") == 0)
231         {
232                 /* we will leak memory here if SensorConfigFile is
233                    used more than once, maybe we can just drop support
234                    for broken, extremely ancient libsensors? */
235                 conffile = strdup (value);
236         }
237         else if (strcasecmp (key, "Sensor") == 0)
238         {
239                 if (ignorelist_add (sensor_list, value))
240                 {
241                         ERROR ("sensors plugin: "
242                                         "Cannot add value to ignorelist.");
243                         return (1);
244                 }
245         }
246         else if (strcasecmp (key, "IgnoreSelected") == 0)
247         {
248                 ignorelist_set_invert (sensor_list, 1);
249                 if (IS_TRUE (value))
250                         ignorelist_set_invert (sensor_list, 0);
251         }
252         else
253         {
254                 return (-1);
255         }
257         return (0);
260 void sensors_free_features (void)
262         featurelist_t *thisft;
263         featurelist_t *nextft;
265         if (first_feature == NULL)
266                 return;
268         sensors_cleanup ();
270         for (thisft = first_feature; thisft != NULL; thisft = nextft)
271         {
272                 nextft = thisft->next;
273                 sfree (thisft);
274         }
275         first_feature = NULL;
278 static int sensors_load_conf (void)
280         static int call_once = 0;
282         FILE *fh = NULL;
283         featurelist_t *last_feature = NULL;
284         
285         const sensors_chip_name *chip;
286         int chip_num;
288         int status;
290         if (call_once)
291                 return 0;
293         call_once = 1;
295         if (conffile)
296         {
297                 fh = fopen (conffile, "r");
298                 if (fh == NULL)
299                 {
300                         char errbuf[1024];
301                         ERROR ("sensors plugin: fopen(%s) failed: %s", conffile,
302                                         sstrerror (errno, errbuf, sizeof (errbuf)));
303                         return (-1);
304                 }
305         }
307         status = sensors_init (fh);
308         if (fh)
309                 fclose (fh);
311         if (status != 0)
312         {
313                 ERROR ("sensors plugin: Cannot initialize sensors. "
314                                 "Data will not be collected.");
315                 return (-1);
316         }
318 #if SENSORS_API_VERSION < 0x400
319         chip_num = 0;
320         while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
321         {
322                 int feature_num0 = 0;
323                 int feature_num1 = 0;
325                 while (42)
326                 {
327                         const sensors_feature_data *feature;
328                         int feature_type;
329                         featurelist_t *fl;
331                         feature = sensors_get_all_features (*chip,
332                                         &feature_num0, &feature_num1);
334                         /* Check if all features have been read. */
335                         if (feature == NULL)
336                                 break;
338                         /* "master features" only */
339                         if (feature->mapping != SENSORS_NO_MAPPING)
340                         {
341                                 DEBUG ("sensors plugin: sensors_load_conf: "
342                                                 "Ignoring subfeature `%s', "
343                                                 "because (feature->mapping "
344                                                 "!= SENSORS_NO_MAPPING).",
345                                                 feature->name);
346                                 continue;
347                         }
349                         /* skip ignored in sensors.conf */
350                         if (sensors_get_ignored (*chip, feature->number) == 0)
351                         {
352                                 DEBUG ("sensors plugin: sensors_load_conf: "
353                                                 "Ignoring subfeature `%s', "
354                                                 "because "
355                                                 "`sensors_get_ignored' told "
356                                                 "me so.",
357                                                 feature->name);
358                                 continue;
359                         }
361                         feature_type = sensors_feature_name_to_type (
362                                         feature->name);
363                         if (feature_type == SENSOR_TYPE_UNKNOWN)
364                         {
365                                 DEBUG ("sensors plugin: sensors_load_conf: "
366                                                 "Ignoring subfeature `%s', "
367                                                 "because its type is "
368                                                 "unknown.",
369                                                 feature->name);
370                                 continue;
371                         }
373                         fl = (featurelist_t *) malloc (sizeof (featurelist_t));
374                         if (fl == NULL)
375                         {
376                                 ERROR ("sensors plugin: malloc failed.");
377                                 continue;
378                         }
379                         memset (fl, '\0', sizeof (featurelist_t));
381                         fl->chip = chip;
382                         fl->data = feature;
383                         fl->type = feature_type;
385                         if (first_feature == NULL)
386                                 first_feature = fl;
387                         else
388                                 last_feature->next = fl;
389                         last_feature = fl;
390                 } /* while sensors_get_all_features */
391         } /* while sensors_get_detected_chips */
392 /* #endif SENSORS_API_VERSION < 0x400 */
394 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
395         chip_num = 0;
396         while ((chip = sensors_get_detected_chips (NULL, &chip_num)) != NULL)
397         {
398                 const sensors_feature *feature;
399                 int feature_num = 0;
401                 while ((feature = sensors_get_features (chip, &feature_num)) != NULL)
402                 {
403                         const sensors_subfeature *subfeature;
404                         int subfeature_num = 0;
406                         /* Only handle voltage, fanspeeds and temperatures */
407                         if ((feature->type != SENSORS_FEATURE_IN)
408                                         && (feature->type != SENSORS_FEATURE_FAN)
409                                         && (feature->type != SENSORS_FEATURE_TEMP))
410                         {
411                                 DEBUG ("sensors plugin: sensors_load_conf: "
412                                                 "Ignoring feature `%s', "
413                                                 "because its type is not "
414                                                 "supported.", feature->name);
415                                 continue;
416                         }
418                         while ((subfeature = sensors_get_all_subfeatures (chip,
419                                                         feature, &subfeature_num)) != NULL)
420                         {
421                                 featurelist_t *fl;
423                                 if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT)
424                                                 && (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT)
425                                                 && (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT))
426                                         continue;
428                                 fl = (featurelist_t *) malloc (sizeof (featurelist_t));
429                                 if (fl == NULL)
430                                 {
431                                         ERROR ("sensors plugin: malloc failed.");
432                                         continue;
433                                 }
434                                 memset (fl, '\0', sizeof (featurelist_t));
436                                 fl->chip = chip;
437                                 fl->feature = feature;
438                                 fl->subfeature = subfeature;
440                                 if (first_feature == NULL)
441                                         first_feature = fl;
442                                 else
443                                         last_feature->next = fl;
444                                 last_feature  = fl;
445                         } /* while (subfeature) */
446                 } /* while (feature) */
447         } /* while (chip) */
448 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
450         if (first_feature == NULL)
451         {
452                 sensors_cleanup ();
453                 INFO ("sensors plugin: lm_sensors reports no "
454                                 "features. Data will not be collected.");
455                 return (-1);
456         }
458         return (0);
459 } /* int sensors_load_conf */
461 static int sensors_shutdown (void)
463         sensors_free_features ();
464         ignorelist_free (sensor_list);
466         return (0);
467 } /* int sensors_shutdown */
469 static void sensors_submit (const char *plugin_instance,
470                 const char *type, const char *type_instance,
471                 double val)
473         char match_key[1024];
474         int status;
476         value_t values[1];
477         value_list_t vl = VALUE_LIST_INIT;
479         status = ssnprintf (match_key, sizeof (match_key), "%s/%s-%s",
480                         plugin_instance, type, type_instance);
481         if (status < 1)
482                 return;
484         if (sensor_list != NULL)
485         {
486                 DEBUG ("sensors plugin: Checking ignorelist for `%s'", match_key);
487                 if (ignorelist_match (sensor_list, match_key))
488                         return;
489         }
491         values[0].gauge = val;
493         vl.values = values;
494         vl.values_len = 1;
496         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
497         sstrncpy (vl.plugin, "sensors", sizeof (vl.plugin));
498         sstrncpy (vl.plugin_instance, plugin_instance,
499                         sizeof (vl.plugin_instance));
500         sstrncpy (vl.type, type, sizeof (vl.type));
501         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
503         plugin_dispatch_values (&vl);
504 } /* void sensors_submit */
506 static int sensors_read (void)
508         featurelist_t *fl;
510         if (sensors_load_conf () != 0)
511                 return (-1);
513 #if SENSORS_API_VERSION < 0x400
514         for (fl = first_feature; fl != NULL; fl = fl->next)
515         {
516                 double value;
517                 int status;
518                 char plugin_instance[DATA_MAX_NAME_LEN];
519                 char type_instance[DATA_MAX_NAME_LEN];
521                 status = sensors_get_feature (*fl->chip,
522                                 fl->data->number, &value);
523                 if (status < 0)
524                         continue;
526                 status = sensors_snprintf_chip_name (plugin_instance,
527                                 sizeof (plugin_instance), fl->chip);
528                 if (status < 0)
529                         continue;
531                 sstrncpy (type_instance, fl->data->name,
532                                 sizeof (type_instance));
534                 sensors_submit (plugin_instance,
535                                 sensor_type_name_map[fl->type],
536                                 type_instance,
537                                 value);
538         } /* for fl = first_feature .. NULL */
539 /* #endif SENSORS_API_VERSION < 0x400 */
541 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
542         for (fl = first_feature; fl != NULL; fl = fl->next)
543         {
544                 double value;
545                 int status;
546                 char plugin_instance[DATA_MAX_NAME_LEN];
547                 char type_instance[DATA_MAX_NAME_LEN];
548                 const char *type;
550                 status = sensors_get_value (fl->chip,
551                                 fl->subfeature->number, &value);
552                 if (status < 0)
553                         continue;
555                 status = sensors_snprintf_chip_name (plugin_instance,
556                                 sizeof (plugin_instance), fl->chip);
557                 if (status < 0)
558                         continue;
560                 sstrncpy (type_instance, fl->feature->name,
561                                 sizeof (type_instance));
563                 if (fl->feature->type == SENSORS_FEATURE_IN)
564                         type = "voltage";
565                 else if (fl->feature->type
566                                 == SENSORS_FEATURE_FAN)
567                         type = "fanspeed";
568                 else if (fl->feature->type
569                                 == SENSORS_FEATURE_TEMP)
570                         type = "temperature";
571                 else
572                         continue;
574                 sensors_submit (plugin_instance, type, type_instance, value);
575         } /* for fl = first_feature .. NULL */
576 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
578         return (0);
579 } /* int sensors_read */
581 void module_register (void)
583         plugin_register_config ("sensors", sensors_config,
584                         config_keys, config_keys_num);
585         plugin_register_read ("sensors", sensors_read);
586         plugin_register_shutdown ("sensors", sensors_shutdown);
587 } /* void module_register */