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 len = snprintf (filename, sizeof (filename), "%s/%s/temp", dirname_sysfs, name);
86 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
89 len = read_file_contents (filename, data, sizeof(data));
90 if (len > 1 && data[--len] == '\n') {
96 temp = strtod (data, &endptr) / 1000.0;
98 if (endptr == data + len && errno == 0) {
99 thermal_submit(name, TEMP, temp);
104 len = snprintf (filename, sizeof (filename), "%s/%s/cur_state", dirname_sysfs, name);
105 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
108 len = 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);
126 static int thermal_procfs_device_read (const char *name)
128 const char str_temp[] = "temperature:";
134 * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
138 len = snprintf (filename, sizeof (filename), "%s/%s/temperature", dirname_procfs, name);
139 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
142 len = read_file_contents (filename, data, sizeof(data));
143 if (len > sizeof(str_temp) && data[--len] == '\n' && !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);
203 if ((strcasecmp (value, "True") == 0)
204 || (strcasecmp (value, "Yes") == 0)
205 || (strcasecmp (value, "On") == 0))
206 ignorelist_set_invert (device_list, 0);
208 else if (strcasecmp (key, "ForceUseProcfs") == 0)
211 if ((strcasecmp (value, "True") == 0)
212 || (strcasecmp (value, "Yes") == 0)
213 || (strcasecmp (value, "On") == 0))
224 static int walk_directory (const char *dir, int (*callback)(const char *dev))
230 if ((dh = opendir (dir)) == NULL)
233 ERROR ("Cannot open '%s': %s", dir,
234 sstrerror (errno, errbuf, sizeof (errbuf)));
238 while ((ent = readdir (dh)) != NULL)
240 if (ent->d_name[0] == '.')
244 DEBUG ("thermal plugin: Checking ignorelist for '%s'", ent->d_name);
245 if (ignorelist_match (device_list, ent->d_name))
249 if (!callback(ent->d_name))
258 static int thermal_sysfs_read (void)
260 return walk_directory (dirname_sysfs, thermal_sysfs_device_read);
263 static int thermal_procfs_read (void)
265 return walk_directory (dirname_procfs, thermal_procfs_device_read);
268 static int thermal_init (void)
272 if (!force_procfs && access (dirname_sysfs, R_OK | X_OK) == 0) {
273 ret = plugin_register_read ("thermal", thermal_sysfs_read);
274 } else if (access (dirname_procfs, R_OK | X_OK) == 0) {
275 ret = plugin_register_read ("thermal", thermal_procfs_read);
279 vl_temp_template.values_len = 1;
280 vl_temp_template.interval = interval_g;
281 sstrncpy (vl_temp_template.host, hostname_g,
282 sizeof(vl_temp_template.host));
283 sstrncpy (vl_temp_template.plugin, "thermal",
284 sizeof(vl_temp_template.plugin));
285 sstrncpy (vl_temp_template.type_instance, "temperature",
286 sizeof(vl_temp_template.type_instance));
288 vl_state_template = vl_temp_template;
289 sstrncpy (vl_state_template.type_instance, "cooling_state",
290 sizeof(vl_state_template.type_instance));
296 static int thermal_shutdown (void)
298 ignorelist_free (device_list);
303 void module_register (void)
305 plugin_register_config ("thermal", thermal_config,
306 config_keys, STATIC_ARRAY_SIZE(config_keys));
307 plugin_register_init ("thermal", thermal_init);
308 plugin_register_shutdown ("thermal", thermal_shutdown);