Merge pull request #2341 from rpv-tomsk/issue-2328
[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                            gauge_t value) {
45   value_list_t vl = VALUE_LIST_INIT;
46   value_t v;
47
48   v.gauge = value;
49   vl.values = &v;
50
51   vl.values_len = 1;
52
53   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
54   sstrncpy(vl.plugin, "thermal", sizeof(vl.plugin));
55   if (plugin_instance != NULL)
56     sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
57   sstrncpy(vl.type, (dt == TEMP) ? "temperature" : "gauge", sizeof(vl.type));
58
59   plugin_dispatch_values(&vl);
60 }
61
62 static int thermal_sysfs_device_read(const char __attribute__((unused)) * dir,
63                                      const char *name,
64                                      void __attribute__((unused)) * user_data) {
65   char filename[256];
66   char data[1024];
67   int len;
68   _Bool success = 0;
69
70   if (device_list && ignorelist_match(device_list, name))
71     return -1;
72
73   len =
74       ssnprintf(filename, sizeof(filename), "%s/%s/temp", dirname_sysfs, name);
75   if ((len < 0) || ((size_t)len >= sizeof(filename)))
76     return -1;
77
78   len = (ssize_t)read_file_contents(filename, data, sizeof(data));
79   if (len > 1 && data[--len] == '\n') {
80     char *endptr = NULL;
81     double temp;
82
83     data[len] = 0;
84     errno = 0;
85     temp = strtod(data, &endptr) / 1000.0;
86
87     if (endptr == data + len && errno == 0) {
88       thermal_submit(name, TEMP, temp);
89       success = 1;
90     }
91   }
92
93   len = ssnprintf(filename, sizeof(filename), "%s/%s/cur_state", dirname_sysfs,
94                   name);
95   if ((len < 0) || ((size_t)len >= sizeof(filename)))
96     return -1;
97
98   len = (ssize_t)read_file_contents(filename, data, sizeof(data));
99   if (len > 1 && data[--len] == '\n') {
100     char *endptr = NULL;
101     double state;
102
103     data[len] = 0;
104     errno = 0;
105     state = strtod(data, &endptr);
106
107     if (endptr == data + len && errno == 0) {
108       thermal_submit(name, COOLING_DEV, state);
109       success = 1;
110     }
111   }
112
113   return (success ? 0 : -1);
114 }
115
116 static int thermal_procfs_device_read(const char __attribute__((unused)) * dir,
117                                       const char *name,
118                                       void __attribute__((unused)) *
119                                           user_data) {
120   const char str_temp[] = "temperature:";
121   char filename[256];
122   char data[1024];
123   int len;
124
125   if (device_list && ignorelist_match(device_list, name))
126     return -1;
127
128   /**
129    * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
130    * temperature:             55 C
131    */
132
133   len = ssnprintf(filename, sizeof(filename), "%s/%s/temperature",
134                   dirname_procfs, name);
135   if ((len < 0) || ((size_t)len >= sizeof(filename)))
136     return -1;
137
138   len = (ssize_t)read_file_contents(filename, data, sizeof(data));
139   if ((len > 0) && ((size_t)len > sizeof(str_temp)) && (data[--len] == '\n') &&
140       (!strncmp(data, str_temp, sizeof(str_temp) - 1))) {
141     char *endptr = NULL;
142     double temp;
143     double factor, add;
144
145     if (data[--len] == 'C') {
146       add = 0;
147       factor = 1.0;
148     } else if (data[len] == 'F') {
149       add = -32;
150       factor = 5.0 / 9.0;
151     } else if (data[len] == 'K') {
152       add = -273.15;
153       factor = 1.0;
154     } else
155       return -1;
156
157     while (len > 0 && data[--len] == ' ')
158       ;
159     data[len + 1] = 0;
160
161     while (len > 0 && data[--len] != ' ')
162       ;
163     ++len;
164
165     errno = 0;
166     temp = (strtod(data + len, &endptr) + add) * factor;
167
168     if (endptr != data + len && errno == 0) {
169       thermal_submit(name, TEMP, temp);
170       return 0;
171     }
172   }
173
174   return -1;
175 }
176
177 static int thermal_config(const char *key, const char *value) {
178   if (device_list == NULL)
179     device_list = ignorelist_create(1);
180
181   if (strcasecmp(key, "Device") == 0) {
182     if (ignorelist_add(device_list, value)) {
183       ERROR("thermal plugin: "
184             "Cannot add value to ignorelist.");
185       return 1;
186     }
187   } else if (strcasecmp(key, "IgnoreSelected") == 0) {
188     ignorelist_set_invert(device_list, 1);
189     if (IS_TRUE(value))
190       ignorelist_set_invert(device_list, 0);
191   } else if (strcasecmp(key, "ForceUseProcfs") == 0) {
192     force_procfs = 0;
193     if (IS_TRUE(value))
194       force_procfs = 1;
195   } else {
196     return -1;
197   }
198
199   return 0;
200 }
201
202 static int thermal_sysfs_read(void) {
203   return walk_directory(dirname_sysfs, thermal_sysfs_device_read,
204                         /* user_data = */ NULL, /* include hidden */ 0);
205 }
206
207 static int thermal_procfs_read(void) {
208   return walk_directory(dirname_procfs, thermal_procfs_device_read,
209                         /* user_data = */ NULL, /* include hidden */ 0);
210 }
211
212 static int thermal_init(void) {
213   int ret = -1;
214
215   if (!force_procfs && access(dirname_sysfs, R_OK | X_OK) == 0) {
216     ret = plugin_register_read("thermal", thermal_sysfs_read);
217   } else if (access(dirname_procfs, R_OK | X_OK) == 0) {
218     ret = plugin_register_read("thermal", thermal_procfs_read);
219   }
220
221   return ret;
222 }
223
224 static int thermal_shutdown(void) {
225   ignorelist_free(device_list);
226
227   return 0;
228 }
229
230 void module_register(void) {
231   plugin_register_config("thermal", thermal_config, config_keys,
232                          STATIC_ARRAY_SIZE(config_keys));
233   plugin_register_init("thermal", thermal_init);
234   plugin_register_shutdown("thermal", thermal_shutdown);
235 }