2 * collectd - src/sensors.c
3 * Copyright (C) 2005,2006 Florian octo Forster
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; either version 2 of the License, or (at your
8 * option) any later version.
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.
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
20 * Florian octo Forster <octo at verplant.org>
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
35 #include "configfile.h"
36 #include "utils_debug.h"
38 #define MODULE_NAME "sensors"
39 #define MODULE_NAME_VOLTAGE MODULE_NAME"_voltage"
41 #if defined(HAVE_SENSORS_SENSORS_H)
42 # include <sensors/sensors.h>
44 # undef HAVE_LIBSENSORS
47 #if defined(HAVE_LIBSENSORS)
48 # define SENSORS_HAVE_READ 1
50 # define SENSORS_HAVE_READ 0
55 /* temperature and fan sensors */
56 static char *ds_def[] =
58 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
61 static int ds_num = 1;
64 static char *sensor_voltage_ds_def[] =
66 "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":U:U",
69 static int sensor_voltage_ds_num = 1;
72 static char *old_filename_format = "sensors-%s.rrd";
75 /* new naming <chip-bus-address/type-feature */
76 static char *extended_filename_format = "lm_sensors-%s.rrd";
78 #define SENSOR_TYPE_UNKNOWN 0
79 #define SENSOR_TYPE_VOLTAGE 1
80 #define SENSOR_TYPE_FANSPEED 2
81 #define SENSOR_TYPE_TEMPERATURE 3
83 static char *sensor_type_prefix[] =
92 typedef struct sensors_labeltypes {
98 * finite list of known labels extracted from lm_sensors
100 static sensors_labeltypes known_features[] =
102 { "fan1", SENSOR_TYPE_FANSPEED },
103 { "fan2", SENSOR_TYPE_FANSPEED },
104 { "fan3", SENSOR_TYPE_FANSPEED },
105 { "fan4", SENSOR_TYPE_FANSPEED },
106 { "fan5", SENSOR_TYPE_FANSPEED },
107 { "fan6", SENSOR_TYPE_FANSPEED },
108 { "fan7", SENSOR_TYPE_FANSPEED },
109 { "AIN2", SENSOR_TYPE_VOLTAGE },
110 { "AIN1", SENSOR_TYPE_VOLTAGE },
111 { "in10", SENSOR_TYPE_VOLTAGE },
112 { "in9", SENSOR_TYPE_VOLTAGE },
113 { "in8", SENSOR_TYPE_VOLTAGE },
114 { "in7", SENSOR_TYPE_VOLTAGE },
115 { "in6", SENSOR_TYPE_VOLTAGE },
116 { "in5", SENSOR_TYPE_VOLTAGE },
117 { "in4", SENSOR_TYPE_VOLTAGE },
118 { "in3", SENSOR_TYPE_VOLTAGE },
119 { "in2", SENSOR_TYPE_VOLTAGE },
120 { "in0", SENSOR_TYPE_VOLTAGE },
121 { "CPU_Temp", SENSOR_TYPE_TEMPERATURE },
122 { "remote_temp", SENSOR_TYPE_TEMPERATURE },
123 { "temp1", SENSOR_TYPE_TEMPERATURE },
124 { "temp2", SENSOR_TYPE_TEMPERATURE },
125 { "temp3", SENSOR_TYPE_TEMPERATURE },
126 { "temp4", SENSOR_TYPE_TEMPERATURE },
127 { "temp5", SENSOR_TYPE_TEMPERATURE },
128 { "temp6", SENSOR_TYPE_TEMPERATURE },
129 { "temp7", SENSOR_TYPE_TEMPERATURE },
130 { "temp", SENSOR_TYPE_TEMPERATURE },
131 { "Vccp2", SENSOR_TYPE_VOLTAGE },
132 { "Vccp1", SENSOR_TYPE_VOLTAGE },
133 { "vdd", SENSOR_TYPE_VOLTAGE },
134 { "vid5", SENSOR_TYPE_VOLTAGE },
135 { "vid4", SENSOR_TYPE_VOLTAGE },
136 { "vid3", SENSOR_TYPE_VOLTAGE },
137 { "vid2", SENSOR_TYPE_VOLTAGE },
138 { "vid1", SENSOR_TYPE_VOLTAGE },
139 { "vid", SENSOR_TYPE_VOLTAGE },
140 { "vin4", SENSOR_TYPE_VOLTAGE },
141 { "vin3", SENSOR_TYPE_VOLTAGE },
142 { "vin2", SENSOR_TYPE_VOLTAGE },
143 { "vin1", SENSOR_TYPE_VOLTAGE },
144 { "voltbatt", SENSOR_TYPE_VOLTAGE },
145 { "volt12", SENSOR_TYPE_VOLTAGE },
146 { "volt5", SENSOR_TYPE_VOLTAGE },
147 { "vrm", SENSOR_TYPE_VOLTAGE },
148 { "5.0V", SENSOR_TYPE_VOLTAGE },
149 { "5V", SENSOR_TYPE_VOLTAGE },
150 { "3.3V", SENSOR_TYPE_VOLTAGE },
151 { "2.5V", SENSOR_TYPE_VOLTAGE },
152 { "2.0V", SENSOR_TYPE_VOLTAGE },
153 { "12V", SENSOR_TYPE_VOLTAGE },
158 static char *config_keys[] =
162 "ExtendedSensorNaming",
165 static int config_keys_num = 3;
167 static char **sensor_list = NULL;
168 static int sensor_list_num = 0;
170 * sensor_list_action:
171 * 0 => default is to collect selected sensors
172 * 1 => ignore selected sensors
174 static int sensor_list_action = 0;
176 * sensor_extended_naming:
177 * 0 => default is to create chip-feature
178 * 1 => use new naming scheme chip-bus-address/type-feature
180 static int sensor_extended_naming = 0;
182 #ifdef HAVE_LIBSENSORS
183 typedef struct featurelist
185 const sensors_chip_name *chip;
186 const sensors_feature_data *data;
188 struct featurelist *next;
191 featurelist_t *first_feature = NULL;
192 #endif /* defined (HAVE_LIBSENSORS) */
194 static int sensors_config (char *key, char *value)
198 if (strcasecmp (key, "Sensor") == 0)
200 temp = (char **) realloc (sensor_list, (sensor_list_num + 1) * sizeof (char *));
203 syslog (LOG_EMERG, "Cannot allocate more memory.");
208 if ((sensor_list[sensor_list_num] = strdup (value)) == NULL)
210 syslog (LOG_EMERG, "Cannot allocate memory.");
215 else if (strcasecmp (key, "IgnoreSelected") == 0)
217 if ((strcasecmp (value, "True") == 0)
218 || (strcasecmp (value, "Yes") == 0)
219 || (strcasecmp (value, "On") == 0))
220 sensor_list_action = 1;
222 sensor_list_action = 0;
224 else if (strcasecmp (key, "ExtendedSensorNaming") == 0)
226 if ((strcasecmp (value, "True") == 0)
227 || (strcasecmp (value, "Yes") == 0)
228 || (strcasecmp (value, "On") == 0))
229 sensor_extended_naming = 1;
231 sensor_extended_naming = 0;
242 * Check if this feature should be ignored. This is called from
243 * both, `submit' and `write' to give client and server
244 * the ability to ignore certain stuff...
246 static int config_get_ignored (const char *inst)
250 /* If no ignored are given collect all features. */
251 if (sensor_list_num < 1)
254 for (i = 0; i < sensor_list_num; i++)
255 if (strcasecmp (inst, sensor_list[i]) == 0)
256 return (sensor_list_action);
257 return (1 - sensor_list_action);
260 static void collectd_sensors_init (void)
262 #ifdef HAVE_LIBSENSORS
264 featurelist_t *last_feature = NULL;
265 featurelist_t *new_feature;
267 const sensors_chip_name *chip;
270 const sensors_feature_data *data;
271 int data_num0, data_num1;
273 new_feature = first_feature;
274 while (new_feature != NULL)
276 last_feature = new_feature->next;
278 new_feature = last_feature;
282 assert (new_feature == NULL);
283 assert (last_feature == NULL);
286 if ((fh = fopen ("/etc/sensors.conf", "r")) == NULL)
289 if (sensors_init (fh))
292 syslog (LOG_ERR, "sensors: Cannot initialize sensors. "
293 "Data will not be collected.");
300 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
303 data_num0 = data_num1 = 0;
305 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1))
310 /* "master features" only */
311 if (data->mapping != SENSORS_NO_MAPPING)
314 /* Only known features */
315 for (i = 0; known_features[i].type >= 0; i++)
317 if (strcmp (data->name, known_features[i].label) != 0)
320 /* skip ignored in sensors.conf */
321 if (sensors_get_ignored (*chip, data->number) == 0)
324 DBG ("Adding feature: %s-%s-%s",
326 sensor_type_prefix[known_features[i].type],
329 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
331 DBG ("sensors plugin: malloc: %s",
333 syslog (LOG_ERR, "sensors plugin: malloc: %s",
338 new_feature->chip = chip;
339 new_feature->data = data;
340 new_feature->type = known_features[i].type;
341 new_feature->next = NULL;
343 if (first_feature == NULL)
345 first_feature = new_feature;
346 last_feature = new_feature;
350 last_feature->next = new_feature;
351 last_feature = new_feature;
354 /* stop searching known features at first found */
357 } /* while sensors_get_all_features */
358 } /* while sensors_get_detected_chips */
360 if (first_feature == NULL)
362 #endif /* defined(HAVE_LIBSENSORS) */
367 static void sensors_voltage_write (char *host, char *inst, char *val)
372 /* skip ignored in our config */
373 if (config_get_ignored (inst))
376 /* extended sensor naming */
377 if(sensor_extended_naming)
378 status = snprintf (file, BUFSIZE, extended_filename_format, inst);
380 status = snprintf (file, BUFSIZE, old_filename_format, inst);
382 if ((status < 1) || (status >= BUFSIZE))
385 rrd_update_file (host, file, val, sensor_voltage_ds_def, sensor_voltage_ds_num);
388 static void sensors_write (char *host, char *inst, char *val)
393 /* skip ignored in our config */
394 if (config_get_ignored (inst))
397 /* extended sensor naming */
398 if (sensor_extended_naming)
399 status = snprintf (file, BUFSIZE, extended_filename_format, inst);
401 status = snprintf (file, BUFSIZE, old_filename_format, inst);
403 if ((status < 1) || (status >= BUFSIZE))
406 rrd_update_file (host, file, val, ds_def, ds_num);
409 #if SENSORS_HAVE_READ
410 static void sensors_submit (const char *feat_name,
411 const char *chip_prefix, double value, int type)
416 if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name)
420 /* skip ignored in our config */
421 if (config_get_ignored (inst))
424 if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime,
428 if (type == SENSOR_TYPE_VOLTAGE)
430 DBG ("%s: %s/%s, %s", MODULE_NAME_VOLTAGE,
431 sensor_type_prefix[type], inst, buf);
432 plugin_submit (MODULE_NAME_VOLTAGE, inst, buf);
436 DBG ("%s: %s/%s, %s", MODULE_NAME,
437 sensor_type_prefix[type], inst, buf);
438 plugin_submit (MODULE_NAME, inst, buf);
442 static void sensors_read (void)
444 featurelist_t *feature;
446 char chip_fullprefix[BUFSIZE];
448 for (feature = first_feature; feature != NULL; feature = feature->next)
450 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
453 if (sensor_extended_naming)
455 /* full chip name logic borrowed from lm_sensors */
456 if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
458 if (snprintf (chip_fullprefix, BUFSIZE, "%s-isa-%04x/%s",
459 feature->chip->prefix,
461 sensor_type_prefix[feature->type])
465 else if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
467 if (snprintf (chip_fullprefix, BUFSIZE, "%s-%s-%04x/%s",
468 feature->chip->prefix,
469 feature->chip->busname,
471 sensor_type_prefix[feature->type])
477 if (snprintf (chip_fullprefix, BUFSIZE, "%s-i2c-%d-%02x/%s",
478 feature->chip->prefix,
481 sensor_type_prefix[feature->type])
486 sensors_submit (feature->data->name,
488 value, feature->type);
492 sensors_submit (feature->data->name,
493 feature->chip->prefix,
494 value, feature->type);
499 # define sensors_read NULL
500 #endif /* SENSORS_HAVE_READ */
502 void module_register (void)
504 plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read, sensors_write);
505 plugin_register (MODULE_NAME_VOLTAGE, NULL, NULL, sensors_voltage_write);
506 cf_register (MODULE_NAME, sensors_config, config_keys, config_keys_num);