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