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 const char *const dirname_sysfs = "/sys/class/thermal";
33 const char *const dirname_procfs = "/proc/acpi/thermal_zone";
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;
40 enum dev_type {
41 TEMP = 0,
42 COOLING_DEV
43 };
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;
51 vt.gauge = value;
53 vl.values = &vt;
54 vl.values_len = 1;
56 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
57 sstrncpy (vl.plugin, "thermal", sizeof(vl.plugin));
58 sstrncpy (vl.plugin_instance, plugin_instance,
59 sizeof(vl.plugin_instance));
60 sstrncpy (vl.type, (dt == TEMP) ? "temperature" : "gauge",
61 sizeof (vl.type));
63 plugin_dispatch_values (&vl);
64 }
66 static int thermal_sysfs_device_read (const char __attribute__((unused)) *dir,
67 const char *name, void __attribute__((unused)) *user_data)
68 {
69 char filename[256];
70 char data[1024];
71 int len;
72 int ok = 0;
74 if (device_list && ignorelist_match (device_list, name))
75 return -1;
77 len = snprintf (filename, sizeof (filename),
78 "%s/%s/temp", dirname_sysfs, name);
79 if ((len < 0) || ((size_t) len >= sizeof (filename)))
80 return -1;
82 len = read_file_contents (filename, data, sizeof(data));
83 if (len > 1 && data[--len] == '\n') {
84 char *endptr = NULL;
85 double temp;
87 data[len] = 0;
88 errno = 0;
89 temp = strtod (data, &endptr) / 1000.0;
91 if (endptr == data + len && errno == 0) {
92 thermal_submit(name, TEMP, temp);
93 ++ok;
94 }
95 }
97 len = snprintf (filename, sizeof (filename),
98 "%s/%s/cur_state", dirname_sysfs, name);
99 if ((len < 0) || ((size_t) len >= sizeof (filename)))
100 return -1;
102 len = read_file_contents (filename, data, sizeof(data));
103 if (len > 1 && data[--len] == '\n') {
104 char *endptr = NULL;
105 double state;
107 data[len] = 0;
108 errno = 0;
109 state = strtod (data, &endptr);
111 if (endptr == data + len && errno == 0) {
112 thermal_submit(name, COOLING_DEV, state);
113 ++ok;
114 }
115 }
117 return ok ? 0 : -1;
118 }
120 static int thermal_procfs_device_read (const char __attribute__((unused)) *dir,
121 const char *name, void __attribute__((unused)) *user_data)
122 {
123 const char str_temp[] = "temperature:";
124 char filename[256];
125 char data[1024];
126 int len;
128 if (device_list && ignorelist_match (device_list, name))
129 return -1;
131 /**
132 * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
133 * temperature: 55 C
134 */
136 len = snprintf (filename, sizeof (filename),
137 "%s/%s/temperature", dirname_procfs, name);
138 if ((len < 0) || ((size_t) len >= sizeof (filename)))
139 return -1;
141 len = read_file_contents (filename, data, sizeof(data));
142 if ((len > 0) && ((size_t) len > sizeof(str_temp))
143 && (data[--len] == '\n')
144 && (! strncmp(data, str_temp, sizeof(str_temp)-1))) {
145 char *endptr = NULL;
146 double temp;
147 double celsius, add;
149 if (data[--len] == 'C') {
150 add = 0;
151 celsius = 1;
152 } else if (data[len] == 'F') {
153 add = -32;
154 celsius = 5/9;
155 } else if (data[len] == 'K') {
156 add = -273.15;
157 celsius = 1;
158 } else
159 return -1;
161 while (len > 0 && data[--len] == ' ')
162 ;
163 data[len + 1] = 0;
165 while (len > 0 && data[--len] != ' ')
166 ;
167 ++len;
169 errno = 0;
170 temp = (strtod (data + len, &endptr) + add) * celsius;
172 if (endptr != data + len && errno == 0) {
173 thermal_submit(name, TEMP, temp);
174 return 0;
175 }
176 }
178 return -1;
179 }
181 static const char *config_keys[] = {
182 "Device",
183 "IgnoreSelected",
184 "ForceUseProcfs"
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 if (!ret) {
244 vl_temp_template.values_len = 1;
245 vl_temp_template.interval = interval_g;
246 sstrncpy (vl_temp_template.host, hostname_g,
247 sizeof(vl_temp_template.host));
248 sstrncpy (vl_temp_template.plugin, "thermal",
249 sizeof(vl_temp_template.plugin));
250 sstrncpy (vl_temp_template.type_instance, "temperature",
251 sizeof(vl_temp_template.type_instance));
253 vl_state_template = vl_temp_template;
254 sstrncpy (vl_state_template.type_instance, "cooling_state",
255 sizeof(vl_state_template.type_instance));
256 }
258 return ret;
259 }
261 static int thermal_shutdown (void)
262 {
263 ignorelist_free (device_list);
265 return 0;
266 }
268 void module_register (void)
269 {
270 plugin_register_config ("thermal", thermal_config,
271 config_keys, STATIC_ARRAY_SIZE(config_keys));
272 plugin_register_init ("thermal", thermal_init);
273 plugin_register_shutdown ("thermal", thermal_shutdown);
274 }