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>
27 #define MODULE_NAME "sensors"
29 #if defined(HAVE_SENSORS_SENSORS_H)
30 # include <sensors/sensors.h>
32 # undef HAVE_LIBSENSORS
35 #if defined(HAVE_LIBSENSORS)
36 # define SENSORS_HAVE_READ 1
38 # define SENSORS_HAVE_READ 0
41 static char *filename_format = "sensors-%s.rrd";
43 static char *ds_def[] =
45 "DS:value:GAUGE:25:U:U",
48 static int ds_num = 1;
50 #ifdef HAVE_LIBSENSORS
51 typedef struct featurelist
53 const sensors_chip_name *chip;
54 const sensors_feature_data *data;
55 struct featurelist *next;
58 featurelist_t *first_feature = NULL;
59 #endif /* defined (HAVE_LIBSENSORS) */
61 static void collectd_sensors_init (void)
63 #ifdef HAVE_LIBSENSORS
65 featurelist_t *last_feature = NULL;
66 featurelist_t *new_feature;
68 const sensors_chip_name *chip;
71 const sensors_feature_data *data;
72 int data_num0, data_num1;
74 new_feature = first_feature;
75 while (new_feature != NULL)
77 last_feature = new_feature->next;
79 new_feature = last_feature;
83 assert (new_feature == NULL);
84 assert (last_feature == NULL);
87 if ((fh = fopen ("/etc/sensors.conf", "r")) == NULL)
90 if (sensors_init (fh))
93 syslog (LOG_ERR, "sensors: Cannot initialize sensors. Data will not be collected.");
100 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
103 data_num0 = data_num1 = 0;
105 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1)) != NULL)
107 /* "master features" only */
108 if (data->mapping != SENSORS_NO_MAPPING)
111 /* Only temperature for now.. */
112 if (strncmp (data->name, "temp", 4)
113 && strncmp (data->name, "fan", 3))
116 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
123 syslog (LOG_INFO, "sensors: Adding feature: %s/%s", chip->prefix, data->name);
126 new_feature->chip = chip;
127 new_feature->data = data;
128 new_feature->next = NULL;
130 if (first_feature == NULL)
132 first_feature = new_feature;
133 last_feature = new_feature;
137 last_feature->next = new_feature;
138 last_feature = new_feature;
143 if (first_feature == NULL)
145 #endif /* defined(HAVE_LIBSENSORS) */
151 static void sensors_write (char *host, char *inst, char *val)
156 status = snprintf (file, BUFSIZE, filename_format, inst);
159 else if (status >= BUFSIZE)
162 rrd_update_file (host, file, val, ds_def, ds_num);
165 static void sensors_submit (const char *feat_name, const char *chip_prefix, double value)
170 if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime, value) >= BUFSIZE)
173 if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name) >= BUFSIZE)
176 plugin_submit (MODULE_NAME, inst, buf);
180 #if SENSORS_HAVE_READ
181 static void sensors_read (void)
183 featurelist_t *feature;
186 for (feature = first_feature; feature != NULL; feature = feature->next)
188 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
191 sensors_submit (feature->data->name, feature->chip->prefix, value);
195 # define sensors_read NULL
196 #endif /* SENSORS_HAVE_READ */
198 void module_register (void)
200 plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read, sensors_write);