utils_cmd_flush.c: Terminate all lines printed to the socket with a newline.
[collectd.git] / src / sensors.c
1 /**
2  * collectd - src/sensors.c
3  * Copyright (C) 2005-2007  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 = snprintf (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                 snprintf (buf, buf_size, "%s-%s-%04x",
193                                 chip->prefix,
194                                 chip->busname,
195                                 chip->addr);
196         }
197         else
198         {
199                 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                                 continue;
345
346                         /* skip ignored in sensors.conf */
347                         if (sensors_get_ignored (*chip, feature->number) == 0)
348                                 break;
349
350                         feature_type = sensors_feature_name_to_type (
351                                         feature->name);
352                         if (feature_type == SENSOR_TYPE_UNKNOWN)
353                                 continue;
354
355                         fl = (featurelist_t *) malloc (sizeof (featurelist_t));
356                         if (fl == NULL)
357                         {
358                                 ERROR ("sensors plugin: malloc failed.");
359                                 continue;
360                         }
361                         memset (fl, '\0', sizeof (featurelist_t));
362
363                         fl->chip = chip;
364                         fl->data = feature;
365                         fl->type = feature_type;
366
367                         if (first_feature == NULL)
368                                 first_feature = fl;
369                         else
370                                 last_feature->next = fl;
371                         last_feature = fl;
372                 } /* while sensors_get_all_features */
373         } /* while sensors_get_detected_chips */
374 /* #endif SENSORS_API_VERSION < 0x400 */
375
376 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
377         chip_num = 0;
378         while ((chip = sensors_get_detected_chips (NULL, &chip_num)) != NULL)
379         {
380                 const sensors_feature *feature;
381                 int feature_num = 0;
382
383                 while ((feature = sensors_get_features (chip, &feature_num)) != NULL)
384                 {
385                         const sensors_subfeature *subfeature;
386                         int subfeature_num = 0;
387
388                         /* Only handle voltage, fanspeeds and temperatures */
389                         if ((feature->type != SENSORS_FEATURE_IN)
390                                         && (feature->type != SENSORS_FEATURE_FAN)
391                                         && (feature->type != SENSORS_FEATURE_TEMP))
392                                 continue;
393
394                         while ((subfeature = sensors_get_all_subfeatures (chip,
395                                                         feature, &subfeature_num)) != NULL)
396                         {
397                                 featurelist_t *fl;
398
399                                 if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT)
400                                                 && (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT)
401                                                 && (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT))
402                                         continue;
403
404                                 fl = (featurelist_t *) malloc (sizeof (featurelist_t));
405                                 if (fl == NULL)
406                                 {
407                                         ERROR ("sensors plugin: malloc failed.");
408                                         continue;
409                                 }
410                                 memset (fl, '\0', sizeof (featurelist_t));
411
412                                 fl->chip = chip;
413                                 fl->feature = feature;
414                                 fl->subfeature = subfeature;
415
416                                 if (first_feature == NULL)
417                                         first_feature = fl;
418                                 else
419                                         last_feature->next = fl;
420                                 last_feature  = fl;
421                         } /* while (subfeature) */
422                 } /* while (feature) */
423         } /* while (chip) */
424 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
425
426         if (first_feature == NULL)
427         {
428                 sensors_cleanup ();
429                 INFO ("sensors plugin: lm_sensors reports no "
430                                 "features. Data will not be collected.");
431                 return (-1);
432         }
433
434         return (0);
435 } /* int sensors_load_conf */
436
437 static int sensors_shutdown (void)
438 {
439         sensors_free_features ();
440         ignorelist_free (sensor_list);
441
442         return (0);
443 } /* int sensors_shutdown */
444
445 static void sensors_submit (const char *plugin_instance,
446                 const char *type, const char *type_instance,
447                 double val)
448 {
449         char match_key[1024];
450         int status;
451
452         value_t values[1];
453         value_list_t vl = VALUE_LIST_INIT;
454
455         status = snprintf (match_key, sizeof (match_key), "%s/%s-%s",
456                         plugin_instance, type, type_instance);
457         if (status < 1)
458                 return;
459         match_key[sizeof (match_key) - 1] = '\0';
460
461         if (sensor_list != NULL)
462         {
463                 DEBUG ("sensors plugin: Checking ignorelist for `%s'", match_key);
464                 if (ignorelist_match (sensor_list, match_key))
465                         return;
466         }
467
468         values[0].gauge = val;
469
470         vl.values = values;
471         vl.values_len = 1;
472         vl.time = time (NULL);
473
474         strncpy (vl.host, hostname_g, sizeof (vl.host));
475         vl.host[sizeof (vl.host) - 1] = '\0';
476         strncpy (vl.plugin, "sensors", sizeof (vl.plugin));
477         vl.plugin[sizeof (vl.plugin) - 1] = '\0';
478         strncpy (vl.plugin_instance, plugin_instance,
479                         sizeof (vl.plugin_instance));
480         vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
481         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
482         vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
483
484         plugin_dispatch_values (type, &vl);
485 } /* void sensors_submit */
486
487 static int sensors_read (void)
488 {
489         featurelist_t *fl;
490
491         if (sensors_load_conf () != 0)
492                 return (-1);
493
494 #if SENSORS_API_VERSION < 0x400
495         for (fl = first_feature; fl != NULL; fl = fl->next)
496         {
497                 double value;
498                 int status;
499                 char plugin_instance[DATA_MAX_NAME_LEN];
500                 char type_instance[DATA_MAX_NAME_LEN];
501
502                 status = sensors_get_feature (*fl->chip,
503                                 fl->data->number, &value);
504                 if (status < 0)
505                         continue;
506
507                 status = sensors_snprintf_chip_name (plugin_instance,
508                                 sizeof (plugin_instance), fl->chip);
509                 if (status < 0)
510                         continue;
511                 plugin_instance[sizeof (plugin_instance) - 1] = '\0';
512
513                 strncpy (type_instance, fl->data->name,
514                                 sizeof (type_instance));
515                 type_instance[sizeof (type_instance) - 1] = '\0';
516
517                 sensors_submit (plugin_instance,
518                                 sensor_type_name_map[fl->type],
519                                 type_instance,
520                                 value);
521         } /* for fl = first_feature .. NULL */
522 /* #endif SENSORS_API_VERSION < 0x400 */
523
524 #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
525         for (fl = first_feature; fl != NULL; fl = fl->next)
526         {
527                 double value;
528                 int status;
529                 char plugin_instance[DATA_MAX_NAME_LEN];
530                 char type_instance[DATA_MAX_NAME_LEN];
531                 const char *type;
532
533                 status = sensors_get_value (fl->chip,
534                                 fl->subfeature->number, &value);
535                 if (status < 0)
536                         continue;
537
538                 status = sensors_snprintf_chip_name (plugin_instance,
539                                 sizeof (plugin_instance), fl->chip);
540                 if (status < 0)
541                         continue;
542                 plugin_instance[sizeof (plugin_instance) - 1] = '\0';
543
544                 strncpy (type_instance, fl->feature->name,
545                                 sizeof (type_instance));
546                 type_instance[sizeof (type_instance) - 1] = '\0';
547
548                 if (fl->feature->type == SENSORS_FEATURE_IN)
549                         type = "voltage";
550                 else if (fl->feature->type
551                                 == SENSORS_FEATURE_FAN)
552                         type = "fanspeed";
553                 else if (fl->feature->type
554                                 == SENSORS_FEATURE_TEMP)
555                         type = "input";
556                 else
557                         continue;
558
559                 sensors_submit (plugin_instance, type, type_instance, value);
560         } /* for fl = first_feature .. NULL */
561 #endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
562
563         return (0);
564 } /* int sensors_read */
565
566 void module_register (void)
567 {
568         plugin_register_config ("sensors", sensors_config,
569                         config_keys, config_keys_num);
570         plugin_register_read ("sensors", sensors_read);
571         plugin_register_shutdown ("sensors", sensors_shutdown);
572 } /* void module_register */