2 * collectd - src/sensors.c
3 * Copyright (C) 2005 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>
26 #define MODULE_NAME "sensors"
28 #include <sensors/sensors.h>
33 typedef struct featurelist
35 const sensors_chip_name *chip;
36 const sensors_feature_data *data;
37 struct featurelist *next;
40 featurelist_t *first_feature = NULL;
42 static char *filename_format = "sensors-%s.rrd";
44 static char *ds_def[] =
46 "DS:value:GAUGE:25:U:U",
49 static int ds_num = 1;
51 extern time_t curtime;
53 void collectd_sensors_init (void)
56 featurelist_t *last_feature = NULL;
57 featurelist_t *new_feature;
59 const sensors_chip_name *chip;
62 const sensors_feature_data *data;
63 int data_num0, data_num1;
65 new_feature = first_feature;
66 while (new_feature != NULL)
68 last_feature = new_feature->next;
70 new_feature = last_feature;
74 assert (new_feature == NULL);
75 assert (last_feature == NULL);
78 if ((fh = fopen ("/etc/sensors.conf", "r")) == NULL)
81 if (sensors_init (fh))
84 syslog (LOG_ERR, "sensors: Cannot initialize sensors. Data will not be collected.");
91 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
94 data_num0 = data_num1 = 0;
96 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1)) != NULL)
98 /* "master features" only */
99 if (data->mapping != SENSORS_NO_MAPPING)
102 /* Only temperature for now.. */
103 if (strncmp (data->name, "temp", 4)
104 && strncmp (data->name, "fan", 3))
107 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
114 syslog (LOG_INFO, "sensors: Adding feature: %s/%s", chip->prefix, data->name);
117 new_feature->chip = chip;
118 new_feature->data = data;
119 new_feature->next = NULL;
121 if (first_feature == NULL)
123 first_feature = new_feature;
124 last_feature = new_feature;
128 last_feature->next = new_feature;
129 last_feature = new_feature;
134 if (first_feature == NULL)
138 void sensors_write (char *host, char *inst, char *val)
143 status = snprintf (file, 512, filename_format, inst);
146 else if (status >= 512)
149 rrd_update_file (host, file, val, ds_def, ds_num);
153 void sensors_submit (const char *feat_name, const char *chip_prefix, double value)
158 if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime, value) >= BUFSIZE)
161 if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name) >= BUFSIZE)
164 plugin_submit (MODULE_NAME, inst, buf);
168 void sensors_read (void)
170 featurelist_t *feature;
173 for (feature = first_feature; feature != NULL; feature = feature->next)
175 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
178 sensors_submit (feature->data->name, feature->chip->prefix, value);
182 void module_register (void)
184 plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read,
189 #endif /* COLLECT_SENSORS */