Various plugins: Use the IS_TRUE and IS_FALSE macros everywhere.
[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 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_ignorelist.h"
27
28 #if !KERNEL_LINUX
29 # error "This module is for Linux only."
30 #endif
31
32 const char *const dirname_sysfs = "/sys/class/thermal";
33 const char *const dirname_procfs = "/proc/acpi/thermal_zone";
34
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;
39
40 enum dev_type {
41         TEMP = 0,
42         COOLING_DEV
43 };
44
45 static void thermal_submit (const char *plugin_instance, enum dev_type dt,
46                 gauge_t value)
47 {
48         value_list_t vl = (dt == TEMP) ? vl_temp_template : vl_state_template;
49         value_t vt;
50
51         vt.gauge = value;
52
53         vl.values = &vt;
54         sstrncpy (vl.plugin, "thermal", sizeof(vl.plugin));
55         sstrncpy (vl.plugin_instance, plugin_instance,
56                         sizeof(vl.plugin_instance));
57         sstrncpy (vl.type, (dt == TEMP) ? "temperature" : "gauge",
58                         sizeof (vl.type));
59
60         plugin_dispatch_values (&vl);
61 }
62
63 static int thermal_sysfs_device_read (const char __attribute__((unused)) *dir,
64                 const char *name, void __attribute__((unused)) *user_data)
65 {
66         char filename[256];
67         char data[1024];
68         int len;
69         int ok = 0;
70
71         if (device_list && ignorelist_match (device_list, name))
72                 return -1;
73
74         len = snprintf (filename, sizeof (filename),
75                         "%s/%s/temp", dirname_sysfs, name);
76         if ((len < 0) || ((size_t) len >= sizeof (filename)))
77                 return -1;
78
79         len = read_file_contents (filename, data, sizeof(data));
80         if (len > 1 && data[--len] == '\n') {
81                 char *endptr = NULL;
82                 double temp;
83
84                 data[len] = 0;
85                 errno = 0;
86                 temp = strtod (data, &endptr) / 1000.0;
87
88                 if (endptr == data + len && errno == 0) {
89                         thermal_submit(name, TEMP, temp);
90                         ++ok;
91                 }
92         }
93
94         len = snprintf (filename, sizeof (filename),
95                         "%s/%s/cur_state", dirname_sysfs, name);
96         if ((len < 0) || ((size_t) len >= sizeof (filename)))
97                 return -1;
98
99         len = read_file_contents (filename, data, sizeof(data));
100         if (len > 1 && data[--len] == '\n') {
101                 char *endptr = NULL;
102                 double state;
103
104                 data[len] = 0;
105                 errno = 0;
106                 state = strtod (data, &endptr);
107
108                 if (endptr == data + len && errno == 0) {
109                         thermal_submit(name, COOLING_DEV, state);
110                         ++ok;
111                 }
112         }
113
114         return ok ? 0 : -1;
115 }
116
117 static int thermal_procfs_device_read (const char __attribute__((unused)) *dir,
118                 const char *name, void __attribute__((unused)) *user_data)
119 {
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 = snprintf (filename, sizeof (filename),
134                         "%s/%s/temperature", dirname_procfs, name);
135         if ((len < 0) || ((size_t) len >= sizeof (filename)))
136                 return -1;
137
138         len = read_file_contents (filename, data, sizeof(data));
139         if ((len > 0) && ((size_t) len > sizeof(str_temp))
140                         && (data[--len] == '\n')
141                         && (! strncmp(data, str_temp, sizeof(str_temp)-1))) {
142                 char *endptr = NULL;
143                 double temp;
144                 double celsius, add;
145                 
146                 if (data[--len] == 'C') {
147                         add = 0;
148                         celsius = 1;
149                 } else if (data[len] == 'F') {
150                         add = -32;
151                         celsius = 5/9;
152                 } else if (data[len] == 'K') {
153                         add = -273.15;
154                         celsius = 1;
155                 } else
156                         return -1;
157
158                 while (len > 0 && data[--len] == ' ')
159                         ;
160                 data[len + 1] = 0;
161
162                 while (len > 0 && data[--len] != ' ')
163                         ;
164                 ++len;
165
166                 errno = 0;
167                 temp = (strtod (data + len, &endptr) + add) * celsius;
168
169                 if (endptr != data + len && errno == 0) {
170                         thermal_submit(name, TEMP, temp);
171                         return 0;
172                 }
173         }
174
175         return -1;
176 }
177
178 static const char *config_keys[] = {
179         "Device",
180         "IgnoreSelected",
181         "ForceUseProcfs"
182 };
183
184 static int thermal_config (const char *key, const char *value)
185 {
186         if (device_list == NULL)
187                 device_list = ignorelist_create (1);
188
189         if (strcasecmp (key, "Device") == 0)
190         {
191                 if (ignorelist_add (device_list, value))
192                 {
193                         ERROR ("thermal plugin: "
194                                         "Cannot add value to ignorelist.");
195                         return 1;
196                 }
197         }
198         else if (strcasecmp (key, "IgnoreSelected") == 0)
199         {
200                 ignorelist_set_invert (device_list, 1);
201                 if (IS_TRUE (value))
202                         ignorelist_set_invert (device_list, 0);
203         }
204         else if (strcasecmp (key, "ForceUseProcfs") == 0)
205         {
206                 force_procfs = 0;
207                 if (IS_TRUE (value))
208                         force_procfs = 1;
209         }
210         else
211         {
212                 return -1;
213         }
214
215         return 0;
216 }
217
218 static int thermal_sysfs_read (void)
219 {
220         return walk_directory (dirname_sysfs, thermal_sysfs_device_read,
221                         /* user_data = */ NULL);
222 }
223
224 static int thermal_procfs_read (void)
225 {
226         return walk_directory (dirname_procfs, thermal_procfs_device_read,
227                         /* user_data = */ NULL);
228 }
229
230 static int thermal_init (void)
231 {
232         int ret = -1;
233
234         if (!force_procfs && access (dirname_sysfs, R_OK | X_OK) == 0) {
235                 ret = plugin_register_read ("thermal", thermal_sysfs_read);
236         } else if (access (dirname_procfs, R_OK | X_OK) == 0) {
237                 ret = plugin_register_read ("thermal", thermal_procfs_read);
238         }
239
240         if (!ret) {
241                 vl_temp_template.values_len = 1;
242                 vl_temp_template.interval = interval_g;
243                 sstrncpy (vl_temp_template.host, hostname_g,
244                         sizeof(vl_temp_template.host));
245                 sstrncpy (vl_temp_template.plugin, "thermal",
246                         sizeof(vl_temp_template.plugin));
247                 sstrncpy (vl_temp_template.type_instance, "temperature",
248                         sizeof(vl_temp_template.type_instance));
249
250                 vl_state_template = vl_temp_template;
251                 sstrncpy (vl_state_template.type_instance, "cooling_state",
252                         sizeof(vl_state_template.type_instance));
253         }
254
255         return ret;
256 }
257
258 static int thermal_shutdown (void)
259 {
260         ignorelist_free (device_list);
261
262         return 0;
263 }
264
265 void module_register (void)
266 {
267         plugin_register_config ("thermal", thermal_config,
268                         config_keys, STATIC_ARRAY_SIZE(config_keys));
269         plugin_register_init ("thermal", thermal_init);
270         plugin_register_shutdown ("thermal", thermal_shutdown);
271 }
272