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