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 25, 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"
40 #if defined(HAVE_SENSORS_SENSORS_H)
41 # include <sensors/sensors.h>
43 # undef HAVE_LIBSENSORS
46 #if defined(HAVE_LIBSENSORS)
47 # define SENSORS_HAVE_READ 1
49 # define SENSORS_HAVE_READ 0
54 static char *ds_def[] =
56 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
59 static int ds_num = 1;
62 static char *filename_format = "sensors-%s.rrd";
66 static char *sensor_filename_format = "lm_sensors-%s.rrd";
68 static char *sensor_voltage_ds_def[] =
70 "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":U:U",
73 static int sensor_voltage_ds_num = 1;
75 #define SENSOR_TYPE_UNKNOWN 0
76 #define SENSOR_TYPE_VOLTAGE 1
77 #define SENSOR_TYPE_FANSPEED 2
78 #define SENSOR_TYPE_TEMPERATURE 3
80 static char *sensor_type_prefix[] =
89 typedef struct sensors_labeltypes {
94 /* finite list of known labels
95 * sorted reverse by the length for the same type
96 * because strncmp must match "temp1" before "temp"
98 static sensors_labeltypes known_features[] =
100 { "fan7", SENSOR_TYPE_FANSPEED },
101 { "fan6", SENSOR_TYPE_FANSPEED },
102 { "fan5", SENSOR_TYPE_FANSPEED },
103 { "fan4", SENSOR_TYPE_FANSPEED },
104 { "fan3", SENSOR_TYPE_FANSPEED },
105 { "fan2", SENSOR_TYPE_FANSPEED },
106 { "fan1", SENSOR_TYPE_FANSPEED },
107 { "in8", SENSOR_TYPE_VOLTAGE },
108 { "in7", SENSOR_TYPE_VOLTAGE },
109 { "in6", SENSOR_TYPE_VOLTAGE },
110 { "in5", SENSOR_TYPE_VOLTAGE },
111 { "in4", SENSOR_TYPE_VOLTAGE },
112 { "in3", SENSOR_TYPE_VOLTAGE },
113 { "in2", SENSOR_TYPE_VOLTAGE },
114 { "in0", SENSOR_TYPE_VOLTAGE },
115 { "remote_temp", SENSOR_TYPE_TEMPERATURE },
116 { "temp7", SENSOR_TYPE_TEMPERATURE },
117 { "temp6", SENSOR_TYPE_TEMPERATURE },
118 { "temp5", SENSOR_TYPE_TEMPERATURE },
119 { "temp4", SENSOR_TYPE_TEMPERATURE },
120 { "temp3", SENSOR_TYPE_TEMPERATURE },
121 { "temp2", SENSOR_TYPE_TEMPERATURE },
122 { "temp1", SENSOR_TYPE_TEMPERATURE },
123 { "temp", SENSOR_TYPE_TEMPERATURE },
124 { "Vccp2", SENSOR_TYPE_VOLTAGE },
125 { "Vccp1", SENSOR_TYPE_VOLTAGE },
126 { "vdd", SENSOR_TYPE_VOLTAGE },
127 { "vid4", SENSOR_TYPE_VOLTAGE },
128 { "vid3", SENSOR_TYPE_VOLTAGE },
129 { "vid2", SENSOR_TYPE_VOLTAGE },
130 { "vid1", SENSOR_TYPE_VOLTAGE },
131 { "vid", SENSOR_TYPE_VOLTAGE },
132 { "vin4", SENSOR_TYPE_VOLTAGE },
133 { "vin3", SENSOR_TYPE_VOLTAGE },
134 { "vin2", SENSOR_TYPE_VOLTAGE },
135 { "vin1", SENSOR_TYPE_VOLTAGE },
136 { "voltbatt", SENSOR_TYPE_VOLTAGE },
137 { "volt12", SENSOR_TYPE_VOLTAGE },
138 { "volt5", SENSOR_TYPE_VOLTAGE },
139 { "vrm", SENSOR_TYPE_VOLTAGE },
140 { "12V", SENSOR_TYPE_VOLTAGE },
141 { "2.5V", SENSOR_TYPE_VOLTAGE },
142 { "3.3V", SENSOR_TYPE_VOLTAGE },
143 { "5V", SENSOR_TYPE_VOLTAGE },
148 static char *config_keys[] =
152 "ExtendedSensorNaming",
155 static int config_keys_num = 3;
157 static char **sensor_list = NULL;
158 static int sensor_list_num = 0;
160 * sensor_list_action:
161 * 0 => default is to collect selected sensors
162 * 1 => ignore selected sensors
164 static int sensor_list_action = 0;
166 * sensor_extended_naming:
167 * 0 => default is to create chip-feature
168 * 1 => use new naming scheme chip-bus-address/type-feature
170 static int sensor_extended_naming = 0;
172 #ifdef HAVE_LIBSENSORS
173 typedef struct featurelist
175 const sensors_chip_name *chip;
176 const sensors_feature_data *data;
178 struct featurelist *next;
181 featurelist_t *first_feature = NULL;
182 #endif /* defined (HAVE_LIBSENSORS) */
184 static int sensors_config (char *key, char *value)
188 if (strcasecmp (key, "Sensor") == 0)
190 temp = (char **) realloc (sensor_list, (sensor_list_num + 1) * sizeof (char *));
193 syslog (LOG_EMERG, "Cannot allocate more memory.");
198 if ((sensor_list[sensor_list_num] = strdup (value)) == NULL)
200 syslog (LOG_EMERG, "Cannot allocate memory.");
205 else if (strcasecmp (key, "IgnoreSelected") == 0)
207 if ((strcasecmp (value, "True") == 0)
208 || (strcasecmp (value, "Yes") == 0)
209 || (strcasecmp (value, "On") == 0))
210 sensor_list_action = 1;
212 sensor_list_action = 0;
214 else if (strcasecmp (key, "ExtendedSensorNaming") == 0)
216 if ((strcasecmp (value, "True") == 0)
217 || (strcasecmp (value, "Yes") == 0)
218 || (strcasecmp (value, "On") == 0))
219 sensor_extended_naming = 1;
221 sensor_extended_naming = 0;
232 * Check if this feature should be ignored. This is called from
233 * both, `submit' and `write' to give client and server
234 * the ability to ignore certain stuff...
236 static int config_get_ignored (const char *inst)
240 /* If no ignored are given collect all features. */
241 if (sensor_list_num < 1)
244 for (i = 0; i < sensor_list_num; i++)
245 if (strcasecmp (inst, sensor_list[i]) == 0)
246 return (sensor_list_action);
247 return (1 - sensor_list_action);
250 static void collectd_sensors_init (void)
252 #ifdef HAVE_LIBSENSORS
254 featurelist_t *last_feature = NULL;
255 featurelist_t *new_feature;
257 const sensors_chip_name *chip;
260 const sensors_feature_data *data;
261 int data_num0, data_num1;
263 new_feature = first_feature;
264 while (new_feature != NULL)
266 last_feature = new_feature->next;
268 new_feature = last_feature;
272 assert (new_feature == NULL);
273 assert (last_feature == NULL);
276 if ((fh = fopen ("/etc/sensors.conf", "r")) == NULL)
279 if (sensors_init (fh))
282 syslog (LOG_ERR, "sensors: Cannot initialize sensors. Data will not be collected.");
289 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
292 data_num0 = data_num1 = 0;
294 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1)) != NULL)
296 /* "master features" only */
297 if (data->mapping != SENSORS_NO_MAPPING)
300 /* Only known features */
302 while (known_features[i].type >= 0)
304 if(strncmp(data->name, known_features[i].label, strlen(known_features[i].label)) == 0)
306 /* skip ignored in sensors.conf */
307 if (sensors_get_ignored(*chip, data->number) == 0)
312 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
318 DBG ("Adding feature: %s/%s/%i", chip->prefix, data->name, known_features[i].type);
319 new_feature->chip = chip;
320 new_feature->data = data;
321 new_feature->type = known_features[i].type;
322 new_feature->next = NULL;
324 if (first_feature == NULL)
326 first_feature = new_feature;
327 last_feature = new_feature;
331 last_feature->next = new_feature;
332 last_feature = new_feature;
335 /* stop searching known features at first found */
343 if (first_feature == NULL)
345 #endif /* defined(HAVE_LIBSENSORS) */
350 static void sensors_write (char *host, char *inst, char *val)
356 /* skip ignored in our config */
357 if (config_get_ignored (inst))
360 /* extended sensor naming */
361 if(sensor_extended_naming)
362 status = snprintf (file, BUFSIZE, sensor_filename_format, inst);
364 status = snprintf (file, BUFSIZE, filename_format, inst);
367 else if (status >= BUFSIZE)
370 if(sensor_extended_naming)
372 typestart = strrchr(inst, '/');
373 if(typestart != NULL)
375 if(strncmp(typestart, sensor_type_prefix[SENSOR_TYPE_VOLTAGE], strlen(sensor_type_prefix[SENSOR_TYPE_VOLTAGE])) == 0)
376 rrd_update_file (host, file, val, sensor_voltage_ds_def, sensor_voltage_ds_num);
378 rrd_update_file (host, file, val, ds_def, ds_num);
384 rrd_update_file (host, file, val, ds_def, ds_num);
387 #if SENSORS_HAVE_READ
388 static void sensors_submit (const char *feat_name, const char *chip_prefix, double value)
393 if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name) >= BUFSIZE)
396 /* skip ignored in our config */
397 if (config_get_ignored (inst))
400 if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime, value) >= BUFSIZE)
403 DBG ("%s, %s", inst, buf);
404 plugin_submit (MODULE_NAME, inst, buf);
407 static void sensors_read (void)
409 featurelist_t *feature;
411 char chip_fullprefix[BUFSIZE];
413 for (feature = first_feature; feature != NULL; feature = feature->next)
415 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
418 if(sensor_extended_naming)
420 /* full chip name logic borrowed from lm_sensors */
421 if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
423 if (snprintf (chip_fullprefix, BUFSIZE, "%s-isa-%04x%s", feature->chip->prefix, feature->chip->addr, sensor_type_prefix[feature->type]) >= BUFSIZE)
426 else if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
428 if (snprintf (chip_fullprefix, BUFSIZE, "%s-%s-%04x%s", feature->chip->prefix, feature->chip->busname, feature->chip->addr, sensor_type_prefix[feature->type]) >= BUFSIZE)
433 if (snprintf (chip_fullprefix, BUFSIZE, "%s-i2c-%d-%02x%s", feature->chip->prefix, feature->chip->bus, feature->chip->addr, sensor_type_prefix[feature->type]) >= BUFSIZE)
436 sensors_submit (feature->data->name, (const char *)chip_fullprefix, value);
440 sensors_submit (feature->data->name, feature->chip->prefix, value);
445 # define sensors_read NULL
446 #endif /* SENSORS_HAVE_READ */
448 void module_register (void)
450 plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read, sensors_write);
451 cf_register (MODULE_NAME, sensors_config, config_keys, config_keys_num);