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;
54 vl.time = time (NULL);
55 sstrncpy (vl.plugin, "thermal", sizeof(vl.plugin));
56 sstrncpy (vl.plugin_instance, plugin_instance,
57 sizeof(vl.plugin_instance));
58 sstrncpy (vl.type, (dt == TEMP) ? "temperature" : "gauge",
61 plugin_dispatch_values (&vl);
64 static int read_file_contents (const char *filename, char *buf, int bufsize)
69 if ((fh = fopen (filename, "r")) == NULL)
72 n = fread(buf, 1, bufsize, fh);
78 static int thermal_sysfs_device_read (const char *name)
85 if (device_list && ignorelist_match (device_list, name))
88 len = snprintf (filename, sizeof (filename), "%s/%s/temp", dirname_sysfs, name);
89 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
92 len = read_file_contents (filename, data, sizeof(data));
93 if (len > 1 && data[--len] == '\n') {
99 temp = strtod (data, &endptr) / 1000.0;
101 if (endptr == data + len && errno == 0) {
102 thermal_submit(name, TEMP, temp);
107 len = snprintf (filename, sizeof (filename), "%s/%s/cur_state", dirname_sysfs, name);
108 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
111 len = read_file_contents (filename, data, sizeof(data));
112 if (len > 1 && data[--len] == '\n') {
118 state = strtod (data, &endptr);
120 if (endptr == data + len && errno == 0) {
121 thermal_submit(name, COOLING_DEV, state);
129 static int thermal_procfs_device_read (const char *name)
131 const char str_temp[] = "temperature:";
136 if (device_list && ignorelist_match (device_list, name))
140 * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
144 len = snprintf (filename, sizeof (filename), "%s/%s/temperature", dirname_procfs, name);
145 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
148 len = read_file_contents (filename, data, sizeof(data));
149 if (len > sizeof(str_temp) && data[--len] == '\n' && !strncmp(data, str_temp, sizeof(str_temp)-1)) {
154 if (data[--len] == 'C') {
157 } else if (data[len] == 'F') {
160 } else if (data[len] == 'K') {
166 while (len > 0 && data[--len] == ' ')
170 while (len > 0 && data[--len] != ' ')
175 temp = (strtod (data + len, &endptr) + add) * celsius;
177 if (endptr != data + len && errno == 0) {
178 thermal_submit(name, TEMP, temp);
186 static const char *config_keys[] = {
192 static int thermal_config (const char *key, const char *value)
194 if (device_list == NULL)
195 device_list = ignorelist_create (1);
197 if (strcasecmp (key, "Device") == 0)
199 if (ignorelist_add (device_list, value))
201 ERROR ("thermal plugin: "
202 "Cannot add value to ignorelist.");
206 else if (strcasecmp (key, "IgnoreSelected") == 0)
208 ignorelist_set_invert (device_list, 1);
209 if ((strcasecmp (value, "True") == 0)
210 || (strcasecmp (value, "Yes") == 0)
211 || (strcasecmp (value, "On") == 0))
212 ignorelist_set_invert (device_list, 0);
214 else if (strcasecmp (key, "ForceUseProcfs") == 0)
217 if ((strcasecmp (value, "True") == 0)
218 || (strcasecmp (value, "Yes") == 0)
219 || (strcasecmp (value, "On") == 0))
230 static int thermal_sysfs_read (void)
232 return walk_directory (dirname_sysfs, thermal_sysfs_device_read);
235 static int thermal_procfs_read (void)
237 return walk_directory (dirname_procfs, thermal_procfs_device_read);
240 static int thermal_init (void)
244 if (!force_procfs && access (dirname_sysfs, R_OK | X_OK) == 0) {
245 ret = plugin_register_read ("thermal", thermal_sysfs_read);
246 } else if (access (dirname_procfs, R_OK | X_OK) == 0) {
247 ret = plugin_register_read ("thermal", thermal_procfs_read);
251 vl_temp_template.values_len = 1;
252 vl_temp_template.interval = interval_g;
253 sstrncpy (vl_temp_template.host, hostname_g,
254 sizeof(vl_temp_template.host));
255 sstrncpy (vl_temp_template.plugin, "thermal",
256 sizeof(vl_temp_template.plugin));
257 sstrncpy (vl_temp_template.type_instance, "temperature",
258 sizeof(vl_temp_template.type_instance));
260 vl_state_template = vl_temp_template;
261 sstrncpy (vl_state_template.type_instance, "cooling_state",
262 sizeof(vl_state_template.type_instance));
268 static int thermal_shutdown (void)
270 ignorelist_free (device_list);
275 void module_register (void)
277 plugin_register_config ("thermal", thermal_config,
278 config_keys, STATIC_ARRAY_SIZE(config_keys));
279 plugin_register_init ("thermal", thermal_init);
280 plugin_register_shutdown ("thermal", thermal_shutdown);