thermal plugin: Updated the submit function to the new infrastructure.
[collectd.git] / src / thermal.c
1 /**
2  * collectd - src/thermal.c
3  * Copyright (C) 2008  Micha³ Miros³aw
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; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Micha³ Miros³aw <mirq-linux at rere.qmqm.pl>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_ignorelist.h"
27
28 #if !KERNEL_LINUX
29 # error "This module is for Linux only."
30 #endif
31
32 const char *const dirname_sysfs = "/sys/class/thermal";
33 const char *const dirname_procfs = "/proc/acpi/thermal_zone";
34
35 static char force_procfs = 0;
36 static ignorelist_t *device_list;
37 static value_list_t vl_temp_template = VALUE_LIST_STATIC;
38 static value_list_t vl_state_template = VALUE_LIST_STATIC;
39
40 enum dev_type {
41         TEMP = 0,
42         COOLING_DEV
43 };
44
45 static void thermal_submit (const char *plugin_instance, enum dev_type dt,
46                 gauge_t value)
47 {
48         value_list_t vl = (dt == TEMP) ? vl_temp_template : vl_state_template;
49         value_t vt;
50
51         vt.gauge = value;
52
53         vl.values = &vt;
54         vl.time = time (NULL);
55         sstrncpy (vl.plugin, "thermal", sizeof(vl.plugin));
56         sstrncpy (vl.plugin_instance, plugin_instance,
57                         sizeof(vl.plugin_instance));
58         sstrncpy (vl.type, (dt == TEMP) ? "temperature" : "gauge",
59                         sizeof (vl.type));
60
61         plugin_dispatch_values (&vl);
62 }
63
64 static int read_file_contents (const char *filename, char *buf, int bufsize)
65 {
66         FILE *fh;
67         int n;
68
69         if ((fh = fopen (filename, "r")) == NULL)
70                 return -1;
71
72         n = fread(buf, 1, bufsize, fh);
73         fclose(fh);
74
75         return n;
76 }
77
78 static int thermal_sysfs_device_read (const char *name)
79 {
80         char filename[256];
81         char data[1024];
82         int len;
83         int ok = 0;
84
85         len = snprintf (filename, sizeof (filename), "%s/%s/temp", dirname_sysfs, name);
86         if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
87                 return -1;
88
89         len = read_file_contents (filename, data, sizeof(data));
90         if (len > 1 && data[--len] == '\n') {
91                 char *endptr = NULL;
92                 double temp;
93
94                 data[len] = 0;
95                 errno = 0;
96                 temp = strtod (data, &endptr) / 1000.0;
97
98                 if (endptr == data + len && errno == 0) {
99                         thermal_submit(name, TEMP, temp);
100                         ++ok;
101                 }
102         }
103
104         len = snprintf (filename, sizeof (filename), "%s/%s/cur_state", dirname_sysfs, name);
105         if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
106                 return -1;
107
108         len = read_file_contents (filename, data, sizeof(data));
109         if (len > 1 && data[--len] == '\n') {
110                 char *endptr = NULL;
111                 double state;
112
113                 data[len] = 0;
114                 errno = 0;
115                 state = strtod (data, &endptr);
116
117                 if (endptr == data + len && errno == 0) {
118                         thermal_submit(name, COOLING_DEV, state);
119                         ++ok;
120                 }
121         }
122
123         return ok ? 0 : -1;
124 }
125
126 static int thermal_procfs_device_read (const char *name)
127 {
128         const char str_temp[] = "temperature:";
129         char filename[256];
130         char data[1024];
131         int len;
132
133         /**
134          * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
135          * temperature:             55 C
136          */
137         
138         len = snprintf (filename, sizeof (filename), "%s/%s/temperature", dirname_procfs, name);
139         if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
140                 return -1;
141
142         len = read_file_contents (filename, data, sizeof(data));
143         if (len > sizeof(str_temp) && data[--len] == '\n' && !strncmp(data, str_temp, sizeof(str_temp)-1)) {
144                 char *endptr = NULL;
145                 double temp;
146                 double celsius, add;
147                 
148                 if (data[--len] == 'C') {
149                         add = 0;
150                         celsius = 1;
151                 } else if (data[len] == 'F') {
152                         add = -32;
153                         celsius = 5/9;
154                 } else if (data[len] == 'K') {
155                         add = -273.15;
156                         celsius = 1;
157                 } else
158                         return -1;
159
160                 while (len > 0 && data[--len] == ' ')
161                         ;
162                 data[len + 1] = 0;
163
164                 while (len > 0 && data[--len] != ' ')
165                         ;
166                 ++len;
167
168                 errno = 0;
169                 temp = (strtod (data + len, &endptr) + add) * celsius;
170
171                 if (endptr != data + len && errno == 0) {
172                         thermal_submit(name, TEMP, temp);
173                         return 0;
174                 }
175         }
176
177         return -1;
178 }
179
180 static const char *config_keys[] = {
181         "Device",
182         "IgnoreSelected",
183         "ForceUseProcfs"
184 };
185
186 static int thermal_config (const char *key, const char *value)
187 {
188         if (device_list == NULL)
189                 device_list = ignorelist_create (1);
190
191         if (strcasecmp (key, "Device") == 0)
192         {
193                 if (ignorelist_add (device_list, value))
194                 {
195                         ERROR ("thermal plugin: "
196                                         "Cannot add value to ignorelist.");
197                         return 1;
198                 }
199         }
200         else if (strcasecmp (key, "IgnoreSelected") == 0)
201         {
202                 ignorelist_set_invert (device_list, 1);
203                 if ((strcasecmp (value, "True") == 0)
204                                 || (strcasecmp (value, "Yes") == 0)
205                                 || (strcasecmp (value, "On") == 0))
206                         ignorelist_set_invert (device_list, 0);
207         }
208         else if (strcasecmp (key, "ForceUseProcfs") == 0)
209         {
210                 force_procfs = 0;
211                 if ((strcasecmp (value, "True") == 0)
212                                 || (strcasecmp (value, "Yes") == 0)
213                                 || (strcasecmp (value, "On") == 0))
214                         force_procfs = 1;
215         }
216         else
217         {
218                 return -1;
219         }
220
221         return 0;
222 }
223
224 static int walk_directory (const char *dir, int (*callback)(const char *dev))
225 {
226         struct dirent *ent;
227         DIR *dh;
228         int ok = 0;
229
230         if ((dh = opendir (dir)) == NULL)
231         {
232                 char errbuf[1024];
233                 ERROR ("Cannot open '%s': %s", dir,
234                                 sstrerror (errno, errbuf, sizeof (errbuf)));
235                 return -1;
236         }
237
238         while ((ent = readdir (dh)) != NULL)
239         {
240                 if (ent->d_name[0] == '.')
241                         continue;
242
243                 if (device_list) {
244                         DEBUG ("thermal plugin: Checking ignorelist for '%s'", ent->d_name);
245                         if (ignorelist_match (device_list, ent->d_name))
246                                 continue;
247                 }
248
249                 if (!callback(ent->d_name))
250                         ++ok;
251         }
252
253         closedir (dh);
254
255         return ok ? 0 : -1;
256 }
257
258 static int thermal_sysfs_read (void)
259 {
260         return walk_directory (dirname_sysfs, thermal_sysfs_device_read);
261 }
262
263 static int thermal_procfs_read (void)
264 {
265         return walk_directory (dirname_procfs, thermal_procfs_device_read);
266 }
267
268 static int thermal_init (void)
269 {
270         int ret = -1;
271
272         if (!force_procfs && access (dirname_sysfs, R_OK | X_OK) == 0) {
273                 ret = plugin_register_read ("thermal", thermal_sysfs_read);
274         } else if (access (dirname_procfs, R_OK | X_OK) == 0) {
275                 ret = plugin_register_read ("thermal", thermal_procfs_read);
276         }
277
278         if (!ret) {
279                 vl_temp_template.values_len = 1;
280                 vl_temp_template.interval = interval_g;
281                 sstrncpy (vl_temp_template.host, hostname_g,
282                         sizeof(vl_temp_template.host));
283                 sstrncpy (vl_temp_template.plugin, "thermal",
284                         sizeof(vl_temp_template.plugin));
285                 sstrncpy (vl_temp_template.type_instance, "temperature",
286                         sizeof(vl_temp_template.type_instance));
287
288                 vl_state_template = vl_temp_template;
289                 sstrncpy (vl_state_template.type_instance, "cooling_state",
290                         sizeof(vl_state_template.type_instance));
291         }
292
293         return ret;
294 }
295
296 static int thermal_shutdown (void)
297 {
298         ignorelist_free (device_list);
299
300         return 0;
301 }
302
303 void module_register (void)
304 {
305         plugin_register_config ("thermal", thermal_config,
306                         config_keys, STATIC_ARRAY_SIZE(config_keys));
307         plugin_register_init ("thermal", thermal_init);
308         plugin_register_shutdown ("thermal", thermal_shutdown);
309 }
310