270ba035cfdfbbdf350fc00da36fa0b0eca5bfec
1 /**
2 * collectd - src/sensors.c
3 * Copyright (C) 2005 Florian octo Forster
4 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 **/
23 #include "sensors.h"
25 #if COLLECT_SENSORS
26 #define MODULE_NAME "sensors"
28 #include <sensors/sensors.h>
30 #include "plugin.h"
31 #include "common.h"
33 typedef struct featurelist
34 {
35 const sensors_chip_name *chip;
36 const sensors_feature_data *data;
37 struct featurelist *next;
38 } featurelist_t;
40 featurelist_t *first_feature = NULL;
42 static char *filename_format = "sensors-%s.rrd";
44 static char *ds_def[] =
45 {
46 "DS:value:GAUGE:25:U:U",
47 NULL
48 };
49 static int ds_num = 1;
51 void collectd_sensors_init (void)
52 {
53 FILE *fh;
54 featurelist_t *last_feature = NULL;
55 featurelist_t *new_feature;
57 const sensors_chip_name *chip;
58 int chip_num;
60 const sensors_feature_data *data;
61 int data_num0, data_num1;
63 new_feature = first_feature;
64 while (new_feature != NULL)
65 {
66 last_feature = new_feature->next;
67 free (new_feature);
68 new_feature = last_feature;
69 }
71 #ifdef assert
72 assert (new_feature == NULL);
73 assert (last_feature == NULL);
74 #endif
76 if ((fh = fopen ("/etc/sensors.conf", "r")) == NULL)
77 return;
79 if (sensors_init (fh))
80 {
81 fclose (fh);
82 syslog (LOG_ERR, "sensors: Cannot initialize sensors. Data will not be collected.");
83 return;
84 }
86 fclose (fh);
88 chip_num = 0;
89 while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
90 {
91 data = NULL;
92 data_num0 = data_num1 = 0;
94 while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1)) != NULL)
95 {
96 /* "master features" only */
97 if (data->mapping != SENSORS_NO_MAPPING)
98 continue;
100 /* Only temperature for now.. */
101 if (strncmp (data->name, "temp", 4)
102 && strncmp (data->name, "fan", 3))
103 continue;
105 if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
106 {
107 perror ("malloc");
108 continue;
109 }
111 /*
112 syslog (LOG_INFO, "sensors: Adding feature: %s/%s", chip->prefix, data->name);
113 */
115 new_feature->chip = chip;
116 new_feature->data = data;
117 new_feature->next = NULL;
119 if (first_feature == NULL)
120 {
121 first_feature = new_feature;
122 last_feature = new_feature;
123 }
124 else
125 {
126 last_feature->next = new_feature;
127 last_feature = new_feature;
128 }
129 }
130 }
132 if (first_feature == NULL)
133 sensors_cleanup ();
134 }
136 void sensors_write (char *host, char *inst, char *val)
137 {
138 char file[512];
139 int status;
141 status = snprintf (file, 512, filename_format, inst);
142 if (status < 1)
143 return;
144 else if (status >= 512)
145 return;
147 rrd_update_file (host, file, val, ds_def, ds_num);
148 }
150 #define BUFSIZE 512
151 void sensors_submit (const char *feat_name, const char *chip_prefix, double value)
152 {
153 char buf[BUFSIZE];
154 char inst[BUFSIZE];
156 if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime, value) >= BUFSIZE)
157 return;
159 if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name) >= BUFSIZE)
160 return;
162 plugin_submit (MODULE_NAME, inst, buf);
163 }
164 #undef BUFSIZE
166 void sensors_read (void)
167 {
168 featurelist_t *feature;
169 double value;
171 for (feature = first_feature; feature != NULL; feature = feature->next)
172 {
173 if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
174 continue;
176 sensors_submit (feature->data->name, feature->chip->prefix, value);
177 }
178 }
180 void module_register (void)
181 {
182 plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read,
183 sensors_write);
184 }
186 #undef MODULE_NAME
187 #endif /* COLLECT_SENSORS */