Merged revision 304 (removal of all `extern time_t curtime' in all plugins) to the...
[collectd.git] / src / sensors.c
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  **/
22
23 #include "sensors.h"
24
25 #if COLLECT_SENSORS
26 #define MODULE_NAME "sensors"
27
28 #include <sensors/sensors.h>
29
30 #include "plugin.h"
31 #include "common.h"
32
33 typedef struct featurelist
34 {
35         const sensors_chip_name    *chip;
36         const sensors_feature_data *data;
37         struct featurelist         *next;
38 } featurelist_t;
39
40 featurelist_t *first_feature = NULL;
41
42 static char *filename_format = "sensors-%s.rrd";
43
44 static char *ds_def[] =
45 {
46         "DS:value:GAUGE:25:U:U",
47         NULL
48 };
49 static int ds_num = 1;
50
51 void collectd_sensors_init (void)
52 {
53         FILE *fh;
54         featurelist_t *last_feature = NULL;
55         featurelist_t *new_feature;
56         
57         const sensors_chip_name *chip;
58         int chip_num;
59
60         const sensors_feature_data *data;
61         int data_num0, data_num1;
62         
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         }
70
71 #ifdef assert
72         assert (new_feature == NULL);
73         assert (last_feature == NULL);
74 #endif
75
76         if ((fh = fopen ("/etc/sensors.conf", "r")) == NULL)
77                 return;
78
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         }
85
86         fclose (fh);
87
88         chip_num = 0;
89         while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
90         {
91                 data = NULL;
92                 data_num0 = data_num1 = 0;
93
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;
99
100                         /* Only temperature for now.. */
101                         if (strncmp (data->name, "temp", 4)
102                                         && strncmp (data->name, "fan", 3))
103                                 continue;
104
105                         if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
106                         {
107                                 perror ("malloc");
108                                 continue;
109                         }
110
111                         /*
112                         syslog (LOG_INFO, "sensors: Adding feature: %s/%s", chip->prefix, data->name);
113                         */
114
115                         new_feature->chip = chip;
116                         new_feature->data = data;
117                         new_feature->next = NULL;
118
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         }
131
132         if (first_feature == NULL)
133                 sensors_cleanup ();
134 }
135
136 void sensors_write (char *host, char *inst, char *val)
137 {
138         char file[512];
139         int status;
140
141         status = snprintf (file, 512, filename_format, inst);
142         if (status < 1)
143                 return;
144         else if (status >= 512)
145                 return;
146
147         rrd_update_file (host, file, val, ds_def, ds_num);
148 }
149
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];
155
156         if (snprintf (buf, BUFSIZE, "%u:%.3f", (unsigned int) curtime, value) >= BUFSIZE)
157                 return;
158
159         if (snprintf (inst, BUFSIZE, "%s-%s", chip_prefix, feat_name) >= BUFSIZE)
160                 return;
161
162         plugin_submit (MODULE_NAME, inst, buf);
163 }
164 #undef BUFSIZE
165
166 void sensors_read (void)
167 {
168         featurelist_t *feature;
169         double value;
170
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;
175
176                 sensors_submit (feature->data->name, feature->chip->prefix, value);
177         }
178 }
179
180 void module_register (void)
181 {
182         plugin_register (MODULE_NAME, collectd_sensors_init, sensors_read,
183                         sensors_write);
184 }
185
186 #undef MODULE_NAME
187 #endif /* COLLECT_SENSORS */