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 static const char *config_keys[] = {
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;
49 static void thermal_submit (const char *plugin_instance, enum dev_type dt,
52 value_list_t vl = VALUE_LIST_INIT;
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));
66 (dt == TEMP) ? "temperature" : "gauge",
69 plugin_dispatch_values (&vl);
72 static int thermal_sysfs_device_read (const char __attribute__((unused)) *dir,
73 const char *name, void __attribute__((unused)) *user_data)
80 if (device_list && ignorelist_match (device_list, name))
83 len = ssnprintf (filename, sizeof (filename),
84 "%s/%s/temp", dirname_sysfs, name);
85 if ((len < 0) || ((size_t) len >= sizeof (filename)))
88 len = (ssize_t) read_file_contents (filename, data, sizeof(data));
89 if (len > 1 && data[--len] == '\n') {
95 temp = strtod (data, &endptr) / 1000.0;
97 if (endptr == data + len && errno == 0) {
98 thermal_submit(name, TEMP, temp);
103 len = ssnprintf (filename, sizeof (filename),
104 "%s/%s/cur_state", dirname_sysfs, name);
105 if ((len < 0) || ((size_t) len >= sizeof (filename)))
108 len = (ssize_t) read_file_contents (filename, data, sizeof(data));
109 if (len > 1 && data[--len] == '\n') {
115 state = strtod (data, &endptr);
117 if (endptr == data + len && errno == 0) {
118 thermal_submit(name, COOLING_DEV, state);
123 return (success ? 0 : -1);
126 static int thermal_procfs_device_read (const char __attribute__((unused)) *dir,
127 const char *name, void __attribute__((unused)) *user_data)
129 const char str_temp[] = "temperature:";
134 if (device_list && ignorelist_match (device_list, name))
138 * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
142 len = ssnprintf (filename, sizeof (filename),
143 "%s/%s/temperature", dirname_procfs, name);
144 if ((len < 0) || ((size_t) len >= sizeof (filename)))
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))) {
155 if (data[--len] == 'C') {
158 } else if (data[len] == 'F') {
161 } else if (data[len] == 'K') {
167 while (len > 0 && data[--len] == ' ')
171 while (len > 0 && data[--len] != ' ')
176 temp = (strtod (data + len, &endptr) + add) * factor;
178 if (endptr != data + len && errno == 0) {
179 thermal_submit(name, TEMP, temp);
187 static int thermal_config (const char *key, const char *value)
189 if (device_list == NULL)
190 device_list = ignorelist_create (1);
192 if (strcasecmp (key, "Device") == 0)
194 if (ignorelist_add (device_list, value))
196 ERROR ("thermal plugin: "
197 "Cannot add value to ignorelist.");
201 else if (strcasecmp (key, "IgnoreSelected") == 0)
203 ignorelist_set_invert (device_list, 1);
205 ignorelist_set_invert (device_list, 0);
207 else if (strcasecmp (key, "ForceUseProcfs") == 0)
221 static int thermal_sysfs_read (void)
223 return walk_directory (dirname_sysfs, thermal_sysfs_device_read,
224 /* user_data = */ NULL, /* include hidden */ 0);
227 static int thermal_procfs_read (void)
229 return walk_directory (dirname_procfs, thermal_procfs_device_read,
230 /* user_data = */ NULL, /* include hidden */ 0);
233 static int thermal_init (void)
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);
246 static int thermal_shutdown (void)
248 ignorelist_free (device_list);
253 void module_register (void)
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);