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