Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / thermal.c
1 /**
2  * collectd - src/thermal.c
3  * Copyright (C) 2008  Michał Mirosław
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Michał Mirosław <mirq-linux at rere.qmqm.pl>
20  **/
21
22 #include "collectd.h"
23
24 #include "common.h"
25 #include "plugin.h"
26 #include "utils_ignorelist.h"
27
28 #if !KERNEL_LINUX
29 #error "This module is for Linux only."
30 #endif
31
32 static const char *config_keys[] = {"Device", "IgnoreSelected",
33                                     "ForceUseProcfs"};
34
35 static const char *const dirname_sysfs = "/sys/class/thermal";
36 static const char *const dirname_procfs = "/proc/acpi/thermal_zone";
37
38 static _Bool force_procfs = 0;
39 static ignorelist_t *device_list;
40
41 enum dev_type { TEMP = 0, COOLING_DEV };
42
43 static void thermal_submit(const char *plugin_instance, enum dev_type dt,
44                            value_t value) {
45   value_list_t vl = VALUE_LIST_INIT;
46
47   vl.values = &value;
48   vl.values_len = 1;
49
50   sstrncpy(vl.plugin, "thermal", sizeof(vl.plugin));
51   if (plugin_instance != NULL)
52     sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
53   sstrncpy(vl.type, (dt == TEMP) ? "temperature" : "gauge", sizeof(vl.type));
54
55   plugin_dispatch_values(&vl);
56 }
57
58 static int thermal_sysfs_device_read(const char __attribute__((unused)) * dir,
59                                      const char *name,
60                                      void __attribute__((unused)) * user_data) {
61   char filename[PATH_MAX];
62   _Bool success = 0;
63   value_t value;
64
65   if (device_list && ignorelist_match(device_list, name))
66     return -1;
67
68   snprintf(filename, sizeof(filename), "%s/%s/temp", dirname_sysfs, name);
69   if (parse_value_file(filename, &value, DS_TYPE_GAUGE) == 0) {
70     value.gauge /= 1000.0;
71     thermal_submit(name, TEMP, value);
72     success = 1;
73   }
74
75   snprintf(filename, sizeof(filename), "%s/%s/cur_state", dirname_sysfs, name);
76   if (parse_value_file(filename, &value, DS_TYPE_GAUGE) == 0) {
77     thermal_submit(name, COOLING_DEV, value);
78     success = 1;
79   }
80
81   return success ? 0 : -1;
82 }
83
84 static int thermal_procfs_device_read(const char __attribute__((unused)) * dir,
85                                       const char *name,
86                                       void __attribute__((unused)) *
87                                           user_data) {
88   const char str_temp[] = "temperature:";
89   char filename[256];
90   char data[1024];
91   int len;
92
93   if (device_list && ignorelist_match(device_list, name))
94     return -1;
95
96   /**
97    * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
98    * temperature:             55 C
99    */
100
101   len = snprintf(filename, sizeof(filename), "%s/%s/temperature",
102                  dirname_procfs, name);
103   if ((len < 0) || ((size_t)len >= sizeof(filename)))
104     return -1;
105
106   len = (ssize_t)read_file_contents(filename, data, sizeof(data));
107   if ((len > 0) && ((size_t)len > sizeof(str_temp)) && (data[--len] == '\n') &&
108       (!strncmp(data, str_temp, sizeof(str_temp) - 1))) {
109     char *endptr = NULL;
110     double temp;
111     double factor, add;
112
113     if (data[--len] == 'C') {
114       add = 0;
115       factor = 1.0;
116     } else if (data[len] == 'F') {
117       add = -32;
118       factor = 5.0 / 9.0;
119     } else if (data[len] == 'K') {
120       add = -273.15;
121       factor = 1.0;
122     } else
123       return -1;
124
125     while (len > 0 && data[--len] == ' ')
126       ;
127     data[len + 1] = 0;
128
129     while (len > 0 && data[--len] != ' ')
130       ;
131     ++len;
132
133     errno = 0;
134     temp = (strtod(data + len, &endptr) + add) * factor;
135
136     if (endptr != data + len && errno == 0) {
137       thermal_submit(name, TEMP, (value_t){.gauge = temp});
138       return 0;
139     }
140   }
141
142   return -1;
143 }
144
145 static int thermal_config(const char *key, const char *value) {
146   if (device_list == NULL)
147     device_list = ignorelist_create(1);
148
149   if (strcasecmp(key, "Device") == 0) {
150     if (ignorelist_add(device_list, value)) {
151       ERROR("thermal plugin: "
152             "Cannot add value to ignorelist.");
153       return 1;
154     }
155   } else if (strcasecmp(key, "IgnoreSelected") == 0) {
156     ignorelist_set_invert(device_list, 1);
157     if (IS_TRUE(value))
158       ignorelist_set_invert(device_list, 0);
159   } else if (strcasecmp(key, "ForceUseProcfs") == 0) {
160     force_procfs = 0;
161     if (IS_TRUE(value))
162       force_procfs = 1;
163   } else {
164     return -1;
165   }
166
167   return 0;
168 }
169
170 static int thermal_sysfs_read(void) {
171   return walk_directory(dirname_sysfs, thermal_sysfs_device_read, NULL, 0);
172 }
173
174 static int thermal_procfs_read(void) {
175   return walk_directory(dirname_procfs, thermal_procfs_device_read, NULL, 0);
176 }
177
178 static int thermal_init(void) {
179   int ret = -1;
180
181   if (!force_procfs && access(dirname_sysfs, R_OK | X_OK) == 0) {
182     ret = plugin_register_read("thermal", thermal_sysfs_read);
183   } else if (access(dirname_procfs, R_OK | X_OK) == 0) {
184     ret = plugin_register_read("thermal", thermal_procfs_read);
185   }
186
187   return ret;
188 }
189
190 static int thermal_shutdown(void) {
191   ignorelist_free(device_list);
192
193   return 0;
194 }
195
196 void module_register(void) {
197   plugin_register_config("thermal", thermal_config, config_keys,
198                          STATIC_ARRAY_SIZE(config_keys));
199   plugin_register_init("thermal", thermal_init);
200   plugin_register_shutdown("thermal", thermal_shutdown);
201 }