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 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_ignorelist.h"
28 #if !KERNEL_LINUX
29 # error "This module is for Linux only."
30 #endif
32 static const char *config_keys[] = {
33 "Device",
34 "IgnoreSelected",
35 "ForceUseProcfs"
36 };
38 const char *const dirname_sysfs = "/sys/class/thermal";
39 const char *const dirname_procfs = "/proc/acpi/thermal_zone";
41 static _Bool force_procfs = 0;
42 static ignorelist_t *device_list;
44 enum dev_type {
45 TEMP = 0,
46 COOLING_DEV
47 };
49 static void thermal_submit (const char *plugin_instance, enum dev_type dt,
50 gauge_t value)
51 {
52 value_list_t vl = VALUE_LIST_INIT;
53 value_t v;
55 v.gauge = value;
56 vl.values = &v;
58 vl.values_len = 1;
60 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
61 sstrncpy (vl.plugin, "thermal", sizeof(vl.plugin));
62 if (plugin_instance != NULL)
63 sstrncpy (vl.plugin_instance, plugin_instance,
64 sizeof (vl.plugin_instance));
65 sstrncpy (vl.type,
66 (dt == TEMP) ? "temperature" : "gauge",
67 sizeof (vl.type));
69 plugin_dispatch_values (&vl);
70 }
72 static int thermal_sysfs_device_read (const char __attribute__((unused)) *dir,
73 const char *name, void __attribute__((unused)) *user_data)
74 {
75 char filename[256];
76 char data[1024];
77 int len;
78 _Bool success = 0;
80 if (device_list && ignorelist_match (device_list, name))
81 return -1;
83 len = ssnprintf (filename, sizeof (filename),
84 "%s/%s/temp", dirname_sysfs, name);
85 if ((len < 0) || ((size_t) len >= sizeof (filename)))
86 return -1;
88 len = (ssize_t) read_file_contents (filename, data, sizeof(data));
89 if (len > 1 && data[--len] == '\n') {
90 char *endptr = NULL;
91 double temp;
93 data[len] = 0;
94 errno = 0;
95 temp = strtod (data, &endptr) / 1000.0;
97 if (endptr == data + len && errno == 0) {
98 thermal_submit(name, TEMP, temp);
99 success = 1;
100 }
101 }
103 len = ssnprintf (filename, sizeof (filename),
104 "%s/%s/cur_state", dirname_sysfs, name);
105 if ((len < 0) || ((size_t) len >= sizeof (filename)))
106 return -1;
108 len = (ssize_t) read_file_contents (filename, data, sizeof(data));
109 if (len > 1 && data[--len] == '\n') {
110 char *endptr = NULL;
111 double state;
113 data[len] = 0;
114 errno = 0;
115 state = strtod (data, &endptr);
117 if (endptr == data + len && errno == 0) {
118 thermal_submit(name, COOLING_DEV, state);
119 success = 1;
120 }
121 }
123 return (success ? 0 : -1);
124 }
126 static int thermal_procfs_device_read (const char __attribute__((unused)) *dir,
127 const char *name, void __attribute__((unused)) *user_data)
128 {
129 const char str_temp[] = "temperature:";
130 char filename[256];
131 char data[1024];
132 int len;
134 if (device_list && ignorelist_match (device_list, name))
135 return -1;
137 /**
138 * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
139 * temperature: 55 C
140 */
142 len = ssnprintf (filename, sizeof (filename),
143 "%s/%s/temperature", dirname_procfs, name);
144 if ((len < 0) || ((size_t) len >= sizeof (filename)))
145 return -1;
147 len = (ssize_t) read_file_contents (filename, data, sizeof(data));
148 if ((len > 0) && ((size_t) len > sizeof(str_temp))
149 && (data[--len] == '\n')
150 && (! strncmp(data, str_temp, sizeof(str_temp)-1))) {
151 char *endptr = NULL;
152 double temp;
153 double factor, add;
155 if (data[--len] == 'C') {
156 add = 0;
157 factor = 1.0;
158 } else if (data[len] == 'F') {
159 add = -32;
160 factor = 5.0/9.0;
161 } else if (data[len] == 'K') {
162 add = -273.15;
163 factor = 1.0;
164 } else
165 return -1;
167 while (len > 0 && data[--len] == ' ')
168 ;
169 data[len + 1] = 0;
171 while (len > 0 && data[--len] != ' ')
172 ;
173 ++len;
175 errno = 0;
176 temp = (strtod (data + len, &endptr) + add) * factor;
178 if (endptr != data + len && errno == 0) {
179 thermal_submit(name, TEMP, temp);
180 return 0;
181 }
182 }
184 return -1;
185 }
187 static int thermal_config (const char *key, const char *value)
188 {
189 if (device_list == NULL)
190 device_list = ignorelist_create (1);
192 if (strcasecmp (key, "Device") == 0)
193 {
194 if (ignorelist_add (device_list, value))
195 {
196 ERROR ("thermal plugin: "
197 "Cannot add value to ignorelist.");
198 return 1;
199 }
200 }
201 else if (strcasecmp (key, "IgnoreSelected") == 0)
202 {
203 ignorelist_set_invert (device_list, 1);
204 if (IS_TRUE (value))
205 ignorelist_set_invert (device_list, 0);
206 }
207 else if (strcasecmp (key, "ForceUseProcfs") == 0)
208 {
209 force_procfs = 0;
210 if (IS_TRUE (value))
211 force_procfs = 1;
212 }
213 else
214 {
215 return -1;
216 }
218 return 0;
219 }
221 static int thermal_sysfs_read (void)
222 {
223 return walk_directory (dirname_sysfs, thermal_sysfs_device_read,
224 /* user_data = */ NULL, /* include hidden */ 0);
225 }
227 static int thermal_procfs_read (void)
228 {
229 return walk_directory (dirname_procfs, thermal_procfs_device_read,
230 /* user_data = */ NULL, /* include hidden */ 0);
231 }
233 static int thermal_init (void)
234 {
235 int ret = -1;
237 if (!force_procfs && access (dirname_sysfs, R_OK | X_OK) == 0) {
238 ret = plugin_register_read ("thermal", thermal_sysfs_read);
239 } else if (access (dirname_procfs, R_OK | X_OK) == 0) {
240 ret = plugin_register_read ("thermal", thermal_procfs_read);
241 }
243 return ret;
244 }
246 static int thermal_shutdown (void)
247 {
248 ignorelist_free (device_list);
250 return 0;
251 }
253 void module_register (void)
254 {
255 plugin_register_config ("thermal", thermal_config,
256 config_keys, STATIC_ARRAY_SIZE(config_keys));
257 plugin_register_init ("thermal", thermal_init);
258 plugin_register_shutdown ("thermal", thermal_shutdown);
259 }