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 void collectd_sensors_init (void)
54 featurelist_t *last_feature = NULL;
55 featurelist_t *new_feature;
57 const sensors_chip_name *chip;
60 const sensors_feature_data *data;
61 int data_num0, data_num1;
63 new_feature = first_feature;
64 while (new_feature != NULL)
66 last_feature = new_feature->next;
68 new_feature = last_feature;
72 assert (new_feature == NULL);
73 assert (last_feature == NULL);
76 if ((fh = fopen ("/etc/sensors.conf", "r")) == NULL)
79 if (sensors_init (fh))
82 syslog (LOG_ERR, "sensors: Cannot initialize sensors. Data will not be collected.");
89 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
92 data_num0 = data_num1 = 0;
94 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1)) != NULL)
96 /* "master features" only */
97 if (data->mapping != SENSORS_NO_MAPPING)
100 /* Only temperature for now.. */
101 if (strncmp (data->name, "temp", 4)
102 && strncmp (data->name, "fan", 3))
105 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
112 syslog (LOG_INFO, "sensors: Adding feature: %s/%s", chip->prefix, data->name);
115 new_feature->chip = chip;
116 new_feature->data = data;
117 new_feature->next = NULL;
119 if (first_feature == NULL)
121 first_feature = new_feature;
122 last_feature = new_feature;
126 last_feature->next = new_feature;
127 last_feature = new_feature;
132 if (first_feature == NULL)
136 void sensors_write (char *host, char *inst, char *val)
141 status = snprintf (file, 512, filename_format, inst);
144 else if (status >= 512)
147 rrd_update_file (host, file, val, ds_def, ds_num);
151 void sensors_submit (const char *feat_name, const char *chip_prefix, double value)
156 if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime, value) >= BUFSIZE)
159 if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name) >= BUFSIZE)
162 plugin_submit (MODULE_NAME, inst, buf);
166 void sensors_read (void)
168 featurelist_t *feature;
171 for (feature = first_feature; feature != NULL; feature = feature->next)
173 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
176 sensors_submit (feature->data->name, feature->chip->prefix, value);
180 void module_register (void)
182 plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read,
187 #endif /* COLLECT_SENSORS */