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, double value)
47 value_list_t vl = dt == TEMP ? vl_temp_template : vl_state_template;
53 vl.time = time (NULL);
54 sstrncpy (vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
56 plugin_dispatch_values (dt == TEMP ? "temperature" : "gauge", &vl);
59 static int read_file_contents (const char *filename, char *buf, int bufsize)
64 if ((fh = fopen (filename, "r")) == NULL)
67 n = fread(buf, 1, bufsize, fh);
73 static int thermal_sysfs_device_read (const char *name)
80 len = snprintf (filename, sizeof (filename), "%s/%s/temp", dirname_sysfs, name);
81 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
84 len = read_file_contents (filename, data, sizeof(data));
85 if (len > 1 && data[--len] == '\n') {
91 temp = strtod (data, &endptr) / 1000.0;
93 if (endptr == data + len && errno == 0) {
94 thermal_submit(name, TEMP, temp);
99 len = snprintf (filename, sizeof (filename), "%s/%s/cur_state", dirname_sysfs, name);
100 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
103 len = read_file_contents (filename, data, sizeof(data));
104 if (len > 1 && data[--len] == '\n') {
110 state = strtod (data, &endptr);
112 if (endptr == data + len && errno == 0) {
113 thermal_submit(name, COOLING_DEV, state);
121 static int thermal_procfs_device_read (const char *name)
123 const char str_temp[] = "temperature:";
129 * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
133 len = snprintf (filename, sizeof (filename), "%s/%s/temperature", dirname_procfs, name);
134 if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
137 len = read_file_contents (filename, data, sizeof(data));
138 if (len > sizeof(str_temp) && data[--len] == '\n' && !strncmp(data, str_temp, sizeof(str_temp)-1)) {
143 if (data[--len] == 'C') {
146 } else if (data[len] == 'F') {
149 } else if (data[len] == 'K') {
155 while (len > 0 && data[--len] == ' ')
159 while (len > 0 && data[--len] != ' ')
164 temp = (strtod (data + len, &endptr) + add) * celsius;
166 if (endptr != data + len && errno == 0) {
167 thermal_submit(name, TEMP, temp);
175 static const char *config_keys[] = {
181 static int thermal_config (const char *key, const char *value)
183 if (device_list == NULL)
184 device_list = ignorelist_create (1);
186 if (strcasecmp (key, "Device") == 0)
188 if (ignorelist_add (device_list, value))
190 ERROR ("thermal plugin: "
191 "Cannot add value to ignorelist.");
195 else if (strcasecmp (key, "IgnoreSelected") == 0)
197 ignorelist_set_invert (device_list, 1);
198 if ((strcasecmp (value, "True") == 0)
199 || (strcasecmp (value, "Yes") == 0)
200 || (strcasecmp (value, "On") == 0))
201 ignorelist_set_invert (device_list, 0);
203 else if (strcasecmp (key, "ForceUseProcfs") == 0)
206 if ((strcasecmp (value, "True") == 0)
207 || (strcasecmp (value, "Yes") == 0)
208 || (strcasecmp (value, "On") == 0))
219 static int walk_directory (const char *dir, int (*callback)(const char *dev))
225 if ((dh = opendir (dir)) == NULL)
228 ERROR ("Cannot open '%s': %s", dir,
229 sstrerror (errno, errbuf, sizeof (errbuf)));
233 while ((ent = readdir (dh)) != NULL)
235 if (ent->d_name[0] == '.')
239 DEBUG ("thermal plugin: Checking ignorelist for '%s'", ent->d_name);
240 if (ignorelist_match (device_list, ent->d_name))
244 if (!callback(ent->d_name))
253 static int thermal_sysfs_read (void)
255 return walk_directory (dirname_sysfs, thermal_sysfs_device_read);
258 static int thermal_procfs_read (void)
260 return walk_directory (dirname_procfs, thermal_procfs_device_read);
263 static int thermal_init (void)
267 if (!force_procfs && access (dirname_sysfs, R_OK | X_OK) == 0) {
268 ret = plugin_register_read ("thermal", thermal_sysfs_read);
269 } else if (access (dirname_procfs, R_OK | X_OK) == 0) {
270 ret = plugin_register_read ("thermal", thermal_procfs_read);
274 vl_temp_template.values_len = 1;
275 vl_temp_template.interval = interval_g;
276 sstrncpy (vl_temp_template.host, hostname_g,
277 sizeof(vl_temp_template.host));
278 sstrncpy (vl_temp_template.plugin, "thermal",
279 sizeof(vl_temp_template.plugin));
280 sstrncpy (vl_temp_template.type_instance, "temperature",
281 sizeof(vl_temp_template.type_instance));
283 vl_state_template = vl_temp_template;
284 sstrncpy (vl_state_template.type_instance, "cooling_state",
285 sizeof(vl_state_template.type_instance));
291 static int thermal_shutdown (void)
293 ignorelist_free (device_list);
298 void module_register (void)
300 plugin_register_config ("thermal", thermal_config,
301 config_keys, STATIC_ARRAY_SIZE(config_keys));
302 plugin_register_init ("thermal", thermal_init);
303 plugin_register_shutdown ("thermal", thermal_shutdown);