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 *filename_format = "sensors-%s.rrd";
75 /* new naming <chip-bus-address/type-feature */
76 static char *sensor_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
99 * sorted reverse by the length for the same type
100 * because strncmp must match "temp1" before "temp"
102 static sensors_labeltypes known_features[] =
104 { "fan7", SENSOR_TYPE_FANSPEED },
105 { "fan6", SENSOR_TYPE_FANSPEED },
106 { "fan5", SENSOR_TYPE_FANSPEED },
107 { "fan4", SENSOR_TYPE_FANSPEED },
108 { "fan3", SENSOR_TYPE_FANSPEED },
109 { "fan2", SENSOR_TYPE_FANSPEED },
110 { "fan1", SENSOR_TYPE_FANSPEED },
111 { "AIN2", SENSOR_TYPE_VOLTAGE },
112 { "AIN1", SENSOR_TYPE_VOLTAGE },
113 { "in10", SENSOR_TYPE_VOLTAGE },
114 { "in9", SENSOR_TYPE_VOLTAGE },
115 { "in8", SENSOR_TYPE_VOLTAGE },
116 { "in7", SENSOR_TYPE_VOLTAGE },
117 { "in6", SENSOR_TYPE_VOLTAGE },
118 { "in5", SENSOR_TYPE_VOLTAGE },
119 { "in4", SENSOR_TYPE_VOLTAGE },
120 { "in3", SENSOR_TYPE_VOLTAGE },
121 { "in2", SENSOR_TYPE_VOLTAGE },
122 { "in0", SENSOR_TYPE_VOLTAGE },
123 { "CPU_Temp", SENSOR_TYPE_TEMPERATURE },
124 { "remote_temp", SENSOR_TYPE_TEMPERATURE },
125 { "temp7", SENSOR_TYPE_TEMPERATURE },
126 { "temp6", SENSOR_TYPE_TEMPERATURE },
127 { "temp5", SENSOR_TYPE_TEMPERATURE },
128 { "temp4", SENSOR_TYPE_TEMPERATURE },
129 { "temp3", SENSOR_TYPE_TEMPERATURE },
130 { "temp2", SENSOR_TYPE_TEMPERATURE },
131 { "temp1", SENSOR_TYPE_TEMPERATURE },
132 { "temp", SENSOR_TYPE_TEMPERATURE },
133 { "Vccp2", SENSOR_TYPE_VOLTAGE },
134 { "Vccp1", SENSOR_TYPE_VOLTAGE },
135 { "vdd", SENSOR_TYPE_VOLTAGE },
136 { "vid5", SENSOR_TYPE_VOLTAGE },
137 { "vid4", SENSOR_TYPE_VOLTAGE },
138 { "vid3", SENSOR_TYPE_VOLTAGE },
139 { "vid2", SENSOR_TYPE_VOLTAGE },
140 { "vid1", SENSOR_TYPE_VOLTAGE },
141 { "vid", SENSOR_TYPE_VOLTAGE },
142 { "vin4", SENSOR_TYPE_VOLTAGE },
143 { "vin3", SENSOR_TYPE_VOLTAGE },
144 { "vin2", SENSOR_TYPE_VOLTAGE },
145 { "vin1", SENSOR_TYPE_VOLTAGE },
146 { "voltbatt", SENSOR_TYPE_VOLTAGE },
147 { "volt12", SENSOR_TYPE_VOLTAGE },
148 { "volt5", SENSOR_TYPE_VOLTAGE },
149 { "vrm", SENSOR_TYPE_VOLTAGE },
150 { "5.0V", SENSOR_TYPE_VOLTAGE },
151 { "5V", SENSOR_TYPE_VOLTAGE },
152 { "3.3V", SENSOR_TYPE_VOLTAGE },
153 { "2.5V", SENSOR_TYPE_VOLTAGE },
154 { "2.0V", SENSOR_TYPE_VOLTAGE },
155 { "12V", SENSOR_TYPE_VOLTAGE },
160 static char *config_keys[] =
164 "ExtendedSensorNaming",
167 static int config_keys_num = 3;
169 static char **sensor_list = NULL;
170 static int sensor_list_num = 0;
172 * sensor_list_action:
173 * 0 => default is to collect selected sensors
174 * 1 => ignore selected sensors
176 static int sensor_list_action = 0;
178 * sensor_extended_naming:
179 * 0 => default is to create chip-feature
180 * 1 => use new naming scheme chip-bus-address/type-feature
182 static int sensor_extended_naming = 0;
184 #ifdef HAVE_LIBSENSORS
185 typedef struct featurelist
187 const sensors_chip_name *chip;
188 const sensors_feature_data *data;
190 struct featurelist *next;
193 featurelist_t *first_feature = NULL;
194 #endif /* defined (HAVE_LIBSENSORS) */
196 static int sensors_config (char *key, char *value)
200 if (strcasecmp (key, "Sensor") == 0)
202 temp = (char **) realloc (sensor_list, (sensor_list_num + 1) * sizeof (char *));
205 syslog (LOG_EMERG, "Cannot allocate more memory.");
210 if ((sensor_list[sensor_list_num] = strdup (value)) == NULL)
212 syslog (LOG_EMERG, "Cannot allocate memory.");
217 else if (strcasecmp (key, "IgnoreSelected") == 0)
219 if ((strcasecmp (value, "True") == 0)
220 || (strcasecmp (value, "Yes") == 0)
221 || (strcasecmp (value, "On") == 0))
222 sensor_list_action = 1;
224 sensor_list_action = 0;
226 else if (strcasecmp (key, "ExtendedSensorNaming") == 0)
228 if ((strcasecmp (value, "True") == 0)
229 || (strcasecmp (value, "Yes") == 0)
230 || (strcasecmp (value, "On") == 0))
231 sensor_extended_naming = 1;
233 sensor_extended_naming = 0;
244 * Check if this feature should be ignored. This is called from
245 * both, `submit' and `write' to give client and server
246 * the ability to ignore certain stuff...
248 static int config_get_ignored (const char *inst)
252 /* If no ignored are given collect all features. */
253 if (sensor_list_num < 1)
256 for (i = 0; i < sensor_list_num; i++)
257 if (strcasecmp (inst, sensor_list[i]) == 0)
258 return (sensor_list_action);
259 return (1 - sensor_list_action);
262 static void collectd_sensors_init (void)
264 #ifdef HAVE_LIBSENSORS
266 featurelist_t *last_feature = NULL;
267 featurelist_t *new_feature;
269 const sensors_chip_name *chip;
272 const sensors_feature_data *data;
273 int data_num0, data_num1;
275 new_feature = first_feature;
276 while (new_feature != NULL)
278 last_feature = new_feature->next;
280 new_feature = last_feature;
284 assert (new_feature == NULL);
285 assert (last_feature == NULL);
288 if ((fh = fopen ("/etc/sensors.conf", "r")) == NULL)
291 if (sensors_init (fh))
294 syslog (LOG_ERR, "sensors: Cannot initialize sensors. Data will not be collected.");
301 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
304 data_num0 = data_num1 = 0;
306 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1)) != NULL)
308 /* "master features" only */
309 if (data->mapping != SENSORS_NO_MAPPING)
312 /* Only known features */
314 while (known_features[i].type >= 0)
316 if(strncmp(data->name, known_features[i].label, strlen(known_features[i].label)) == 0)
318 /* skip ignored in sensors.conf */
319 if (sensors_get_ignored(*chip, data->number) == 0)
324 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
330 DBG ("Adding feature: %s-%s-%s", chip->prefix, sensor_type_prefix[known_features[i].type], data->name);
331 new_feature->chip = chip;
332 new_feature->data = data;
333 new_feature->type = known_features[i].type;
334 new_feature->next = NULL;
336 if (first_feature == NULL)
338 first_feature = new_feature;
339 last_feature = new_feature;
343 last_feature->next = new_feature;
344 last_feature = new_feature;
347 /* stop searching known features at first found */
355 if (first_feature == NULL)
357 #endif /* defined(HAVE_LIBSENSORS) */
362 static void sensors_voltage_write (char *host, char *inst, char *val)
367 /* skip ignored in our config */
368 if (config_get_ignored (inst))
371 /* extended sensor naming */
372 if(sensor_extended_naming)
373 status = snprintf (file, BUFSIZE, sensor_filename_format, inst);
375 status = snprintf (file, BUFSIZE, filename_format, inst);
379 else if (status >= BUFSIZE)
382 rrd_update_file (host, file, val, sensor_voltage_ds_def, sensor_voltage_ds_num);
385 static void sensors_write (char *host, char *inst, char *val)
390 /* skip ignored in our config */
391 if (config_get_ignored (inst))
394 /* extended sensor naming */
395 if(sensor_extended_naming)
396 status = snprintf (file, BUFSIZE, sensor_filename_format, inst);
398 status = snprintf (file, BUFSIZE, filename_format, inst);
402 else if (status >= BUFSIZE)
405 rrd_update_file (host, file, val, ds_def, ds_num);
408 #if SENSORS_HAVE_READ
409 static void sensors_submit (const char *feat_name, const char *chip_prefix, double value, int type)
414 if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name) >= BUFSIZE)
417 /* skip ignored in our config */
418 if (config_get_ignored (inst))
421 if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime, value) >= BUFSIZE)
424 if (type == SENSOR_TYPE_VOLTAGE)
426 DBG ("%s: %s/%s, %s", MODULE_NAME_VOLTAGE, sensor_type_prefix[type], inst, buf);
427 plugin_submit (MODULE_NAME_VOLTAGE, inst, buf);
431 DBG ("%s: %s/%s, %s", MODULE_NAME, sensor_type_prefix[type], inst, buf);
432 plugin_submit (MODULE_NAME, inst, buf);
436 static void sensors_read (void)
438 featurelist_t *feature;
440 char chip_fullprefix[BUFSIZE];
442 for (feature = first_feature; feature != NULL; feature = feature->next)
444 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
447 if(sensor_extended_naming)
449 /* full chip name logic borrowed from lm_sensors */
450 if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
452 if (snprintf (chip_fullprefix, BUFSIZE, "%s-isa-%04x/%s",
453 feature->chip->prefix,
455 sensor_type_prefix[feature->type])
459 else if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
461 if (snprintf (chip_fullprefix, BUFSIZE, "%s-%s-%04x/%s",
462 feature->chip->prefix,
463 feature->chip->busname,
465 sensor_type_prefix[feature->type])
471 if (snprintf (chip_fullprefix, BUFSIZE, "%s-i2c-%d-%02x/%s",
472 feature->chip->prefix,
475 sensor_type_prefix[feature->type])
480 sensors_submit (feature->data->name, (const char *)chip_fullprefix, value, feature->type);
484 sensors_submit (feature->data->name, feature->chip->prefix, value, feature->type);
489 # define sensors_read NULL
490 #endif /* SENSORS_HAVE_READ */
492 void module_register (void)
494 plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read, sensors_write);
495 plugin_register (MODULE_NAME_VOLTAGE, NULL, NULL, sensors_voltage_write);
496 cf_register (MODULE_NAME, sensors_config, config_keys, config_keys_num);