2 * collectd - src/thermal.c
3 * Copyright (C) 2008 Michał Mirosław
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.
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.
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
19 * Michał Mirosław <mirq-linux at rere.qmqm.pl>
25 #include "configfile.h"
26 #include "utils_ignorelist.h"
29 # error "This module is for Linux only."
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;
45 static void thermal_submit (const char *plugin_instance, enum dev_type dt,
48 value_list_t vl = (dt == TEMP) ? vl_temp_template : vl_state_template;
56 sstrncpy (vl.plugin, "thermal", sizeof(vl.plugin));
57 sstrncpy (vl.plugin_instance, plugin_instance,
58 sizeof(vl.plugin_instance));
59 sstrncpy (vl.type, (dt == TEMP) ? "temperature" : "gauge",
62 plugin_dispatch_values (&vl);
65 static int thermal_sysfs_device_read (const char __attribute__((unused)) *dir,
66 const char *name, void __attribute__((unused)) *user_data)
73 if (device_list && ignorelist_match (device_list, name))
76 len = snprintf (filename, sizeof (filename),
77 "%s/%s/temp", dirname_sysfs, name);
78 if ((len < 0) || ((size_t) len >= sizeof (filename)))
81 len = read_file_contents (filename, data, sizeof(data));
82 if (len > 1 && data[--len] == '\n') {
88 temp = strtod (data, &endptr) / 1000.0;
90 if (endptr == data + len && errno == 0) {
91 thermal_submit(name, TEMP, temp);
96 len = snprintf (filename, sizeof (filename),
97 "%s/%s/cur_state", dirname_sysfs, name);
98 if ((len < 0) || ((size_t) len >= sizeof (filename)))
101 len = read_file_contents (filename, data, sizeof(data));
102 if (len > 1 && data[--len] == '\n') {
108 state = strtod (data, &endptr);
110 if (endptr == data + len && errno == 0) {
111 thermal_submit(name, COOLING_DEV, state);
119 static int thermal_procfs_device_read (const char __attribute__((unused)) *dir,
120 const char *name, void __attribute__((unused)) *user_data)
122 const char str_temp[] = "temperature:";
127 if (device_list && ignorelist_match (device_list, name))
131 * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
135 len = snprintf (filename, sizeof (filename),
136 "%s/%s/temperature", dirname_procfs, name);
137 if ((len < 0) || ((size_t) len >= sizeof (filename)))
140 len = read_file_contents (filename, data, sizeof(data));
141 if ((len > 0) && ((size_t) len > sizeof(str_temp))
142 && (data[--len] == '\n')
143 && (! strncmp(data, str_temp, sizeof(str_temp)-1))) {
148 if (data[--len] == 'C') {
151 } else if (data[len] == 'F') {
154 } else if (data[len] == 'K') {
160 while (len > 0 && data[--len] == ' ')
164 while (len > 0 && data[--len] != ' ')
169 temp = (strtod (data + len, &endptr) + add) * celsius;
171 if (endptr != data + len && errno == 0) {
172 thermal_submit(name, TEMP, temp);
180 static const char *config_keys[] = {
186 static int thermal_config (const char *key, const char *value)
188 if (device_list == NULL)
189 device_list = ignorelist_create (1);
191 if (strcasecmp (key, "Device") == 0)
193 if (ignorelist_add (device_list, value))
195 ERROR ("thermal plugin: "
196 "Cannot add value to ignorelist.");
200 else if (strcasecmp (key, "IgnoreSelected") == 0)
202 ignorelist_set_invert (device_list, 1);
204 ignorelist_set_invert (device_list, 0);
206 else if (strcasecmp (key, "ForceUseProcfs") == 0)
220 static int thermal_sysfs_read (void)
222 return walk_directory (dirname_sysfs, thermal_sysfs_device_read,
223 /* user_data = */ NULL, /* include hidden */ 0);
226 static int thermal_procfs_read (void)
228 return walk_directory (dirname_procfs, thermal_procfs_device_read,
229 /* user_data = */ NULL, /* include hidden */ 0);
232 static int thermal_init (void)
236 if (!force_procfs && access (dirname_sysfs, R_OK | X_OK) == 0) {
237 ret = plugin_register_read ("thermal", thermal_sysfs_read);
238 } else if (access (dirname_procfs, R_OK | X_OK) == 0) {
239 ret = plugin_register_read ("thermal", thermal_procfs_read);
243 vl_temp_template.values_len = 1;
244 vl_temp_template.interval = interval_g;
245 sstrncpy (vl_temp_template.host, hostname_g,
246 sizeof(vl_temp_template.host));
247 sstrncpy (vl_temp_template.plugin, "thermal",
248 sizeof(vl_temp_template.plugin));
249 sstrncpy (vl_temp_template.type_instance, "temperature",
250 sizeof(vl_temp_template.type_instance));
252 vl_state_template = vl_temp_template;
253 sstrncpy (vl_state_template.type_instance, "cooling_state",
254 sizeof(vl_state_template.type_instance));
260 static int thermal_shutdown (void)
262 ignorelist_free (device_list);
267 void module_register (void)
269 plugin_register_config ("thermal", thermal_config,
270 config_keys, STATIC_ARRAY_SIZE(config_keys));
271 plugin_register_init ("thermal", thermal_init);
272 plugin_register_shutdown ("thermal", thermal_shutdown);