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
43 static char *filename_format = "sensors-%s.rrd";
45 static char *ds_def[] =
47 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
50 static int ds_num = 1;
52 #ifdef HAVE_LIBSENSORS
53 typedef struct featurelist
55 const sensors_chip_name *chip;
56 const sensors_feature_data *data;
57 struct featurelist *next;
60 featurelist_t *first_feature = NULL;
61 #endif /* defined (HAVE_LIBSENSORS) */
63 static void collectd_sensors_init (void)
65 #ifdef HAVE_LIBSENSORS
67 featurelist_t *last_feature = NULL;
68 featurelist_t *new_feature;
70 const sensors_chip_name *chip;
73 const sensors_feature_data *data;
74 int data_num0, data_num1;
76 new_feature = first_feature;
77 while (new_feature != NULL)
79 last_feature = new_feature->next;
81 new_feature = last_feature;
85 assert (new_feature == NULL);
86 assert (last_feature == NULL);
89 if ((fh = fopen ("/etc/sensors.conf", "r")) == NULL)
92 if (sensors_init (fh))
95 syslog (LOG_ERR, "sensors: Cannot initialize sensors. Data will not be collected.");
102 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
105 data_num0 = data_num1 = 0;
107 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1)) != NULL)
109 /* "master features" only */
110 if (data->mapping != SENSORS_NO_MAPPING)
113 /* Only temperature for now.. */
114 if (strncmp (data->name, "temp", 4)
115 && strncmp (data->name, "fan", 3))
118 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
125 syslog (LOG_INFO, "sensors: Adding feature: %s/%s", chip->prefix, data->name);
128 new_feature->chip = chip;
129 new_feature->data = data;
130 new_feature->next = NULL;
132 if (first_feature == NULL)
134 first_feature = new_feature;
135 last_feature = new_feature;
139 last_feature->next = new_feature;
140 last_feature = new_feature;
145 if (first_feature == NULL)
147 #endif /* defined(HAVE_LIBSENSORS) */
152 static void sensors_write (char *host, char *inst, char *val)
157 status = snprintf (file, BUFSIZE, filename_format, inst);
160 else if (status >= BUFSIZE)
163 rrd_update_file (host, file, val, ds_def, ds_num);
166 #if SENSORS_HAVE_READ
167 static void sensors_submit (const char *feat_name, const char *chip_prefix, double value)
172 if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime, value) >= BUFSIZE)
175 if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name) >= BUFSIZE)
178 plugin_submit (MODULE_NAME, inst, buf);
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);