src/thermal.c: Changed file encoding from ISO-8859-2 to UTF-8.
[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, double value)
46 {
47         value_list_t vl = dt == TEMP ? vl_temp_template : vl_state_template;
48         value_t vt;
49
50         vt.gauge = value;
51
52         vl.values = &vt;
53         vl.time = time (NULL);
54         sstrncpy (vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
55
56         plugin_dispatch_values (dt == TEMP ? "temperature" : "gauge", &vl);
57 }
58
59 static int read_file_contents (const char *filename, char *buf, int bufsize)
60 {
61         FILE *fh;
62         int n;
63
64         if ((fh = fopen (filename, "r")) == NULL)
65                 return -1;
66
67         n = fread(buf, 1, bufsize, fh);
68         fclose(fh);
69
70         return n;
71 }
72
73 static int thermal_sysfs_device_read (const char *name)
74 {
75         char filename[256];
76         char data[1024];
77         int len;
78         int ok = 0;
79
80         len = snprintf (filename, sizeof (filename), "%s/%s/temp", dirname_sysfs, name);
81         if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
82                 return -1;
83
84         len = read_file_contents (filename, data, sizeof(data));
85         if (len > 1 && data[--len] == '\n') {
86                 char *endptr = NULL;
87                 double temp;
88
89                 data[len] = 0;
90                 errno = 0;
91                 temp = strtod (data, &endptr) / 1000.0;
92
93                 if (endptr == data + len && errno == 0) {
94                         thermal_submit(name, TEMP, temp);
95                         ++ok;
96                 }
97         }
98
99         len = snprintf (filename, sizeof (filename), "%s/%s/cur_state", dirname_sysfs, name);
100         if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
101                 return -1;
102
103         len = read_file_contents (filename, data, sizeof(data));
104         if (len > 1 && data[--len] == '\n') {
105                 char *endptr = NULL;
106                 double state;
107
108                 data[len] = 0;
109                 errno = 0;
110                 state = strtod (data, &endptr);
111
112                 if (endptr == data + len && errno == 0) {
113                         thermal_submit(name, COOLING_DEV, state);
114                         ++ok;
115                 }
116         }
117
118         return ok ? 0 : -1;
119 }
120
121 static int thermal_procfs_device_read (const char *name)
122 {
123         const char str_temp[] = "temperature:";
124         char filename[256];
125         char data[1024];
126         int len;
127
128         /**
129          * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
130          * temperature:             55 C
131          */
132         
133         len = snprintf (filename, sizeof (filename), "%s/%s/temperature", dirname_procfs, name);
134         if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
135                 return -1;
136
137         len = read_file_contents (filename, data, sizeof(data));
138         if (len > sizeof(str_temp) && data[--len] == '\n' && !strncmp(data, str_temp, sizeof(str_temp)-1)) {
139                 char *endptr = NULL;
140                 double temp;
141                 double celsius, add;
142                 
143                 if (data[--len] == 'C') {
144                         add = 0;
145                         celsius = 1;
146                 } else if (data[len] == 'F') {
147                         add = -32;
148                         celsius = 5/9;
149                 } else if (data[len] == 'K') {
150                         add = -273.15;
151                         celsius = 1;
152                 } else
153                         return -1;
154
155                 while (len > 0 && data[--len] == ' ')
156                         ;
157                 data[len + 1] = 0;
158
159                 while (len > 0 && data[--len] != ' ')
160                         ;
161                 ++len;
162
163                 errno = 0;
164                 temp = (strtod (data + len, &endptr) + add) * celsius;
165
166                 if (endptr != data + len && errno == 0) {
167                         thermal_submit(name, TEMP, temp);
168                         return 0;
169                 }
170         }
171
172         return -1;
173 }
174
175 static const char *config_keys[] = {
176         "Device",
177         "IgnoreSelected",
178         "ForceUseProcfs"
179 };
180
181 static int thermal_config (const char *key, const char *value)
182 {
183         if (device_list == NULL)
184                 device_list = ignorelist_create (1);
185
186         if (strcasecmp (key, "Device") == 0)
187         {
188                 if (ignorelist_add (device_list, value))
189                 {
190                         ERROR ("thermal plugin: "
191                                         "Cannot add value to ignorelist.");
192                         return 1;
193                 }
194         }
195         else if (strcasecmp (key, "IgnoreSelected") == 0)
196         {
197                 ignorelist_set_invert (device_list, 1);
198                 if ((strcasecmp (value, "True") == 0)
199                                 || (strcasecmp (value, "Yes") == 0)
200                                 || (strcasecmp (value, "On") == 0))
201                         ignorelist_set_invert (device_list, 0);
202         }
203         else if (strcasecmp (key, "ForceUseProcfs") == 0)
204         {
205                 force_procfs = 0;
206                 if ((strcasecmp (value, "True") == 0)
207                                 || (strcasecmp (value, "Yes") == 0)
208                                 || (strcasecmp (value, "On") == 0))
209                         force_procfs = 1;
210         }
211         else
212         {
213                 return -1;
214         }
215
216         return 0;
217 }
218
219 static int walk_directory (const char *dir, int (*callback)(const char *dev))
220 {
221         struct dirent *ent;
222         DIR *dh;
223         int ok = 0;
224
225         if ((dh = opendir (dir)) == NULL)
226         {
227                 char errbuf[1024];
228                 ERROR ("Cannot open '%s': %s", dir,
229                                 sstrerror (errno, errbuf, sizeof (errbuf)));
230                 return -1;
231         }
232
233         while ((ent = readdir (dh)) != NULL)
234         {
235                 if (ent->d_name[0] == '.')
236                         continue;
237
238                 if (device_list) {
239                         DEBUG ("thermal plugin: Checking ignorelist for '%s'", ent->d_name);
240                         if (ignorelist_match (device_list, ent->d_name))
241                                 continue;
242                 }
243
244                 if (!callback(ent->d_name))
245                         ++ok;
246         }
247
248         closedir (dh);
249
250         return ok ? 0 : -1;
251 }
252
253 static int thermal_sysfs_read (void)
254 {
255         return walk_directory (dirname_sysfs, thermal_sysfs_device_read);
256 }
257
258 static int thermal_procfs_read (void)
259 {
260         return walk_directory (dirname_procfs, thermal_procfs_device_read);
261 }
262
263 static int thermal_init (void)
264 {
265         int ret = -1;
266
267         if (!force_procfs && access (dirname_sysfs, R_OK | X_OK) == 0) {
268                 ret = plugin_register_read ("thermal", thermal_sysfs_read);
269         } else if (access (dirname_procfs, R_OK | X_OK) == 0) {
270                 ret = plugin_register_read ("thermal", thermal_procfs_read);
271         }
272
273         if (!ret) {
274                 vl_temp_template.values_len = 1;
275                 vl_temp_template.interval = interval_g;
276                 sstrncpy (vl_temp_template.host, hostname_g,
277                         sizeof(vl_temp_template.host));
278                 sstrncpy (vl_temp_template.plugin, "thermal",
279                         sizeof(vl_temp_template.plugin));
280                 sstrncpy (vl_temp_template.type_instance, "temperature",
281                         sizeof(vl_temp_template.type_instance));
282
283                 vl_state_template = vl_temp_template;
284                 sstrncpy (vl_state_template.type_instance, "cooling_state",
285                         sizeof(vl_state_template.type_instance));
286         }
287
288         return ret;
289 }
290
291 static int thermal_shutdown (void)
292 {
293         ignorelist_free (device_list);
294
295         return 0;
296 }
297
298 void module_register (void)
299 {
300         plugin_register_config ("thermal", thermal_config,
301                         config_keys, STATIC_ARRAY_SIZE(config_keys));
302         plugin_register_init ("thermal", thermal_init);
303         plugin_register_shutdown ("thermal", thermal_shutdown);
304 }
305