notify_email: Add charset in Content-Type header (seems required by some picky spam...
[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 static const char *config_keys[] = {
33         "Device",
34         "IgnoreSelected",
35         "ForceUseProcfs"
36 };
37
38 const char *const dirname_sysfs = "/sys/class/thermal";
39 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                 gauge_t value)
51 {
52         value_list_t vl = VALUE_LIST_INIT;
53         value_t v;
54
55         v.gauge = value;
56         vl.values = &v;
57
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[256];
73         char data[1024];
74         int len;
75         _Bool success = 0;
76
77         if (device_list && ignorelist_match (device_list, name))
78                 return -1;
79
80         len = snprintf (filename, sizeof (filename),
81                         "%s/%s/temp", dirname_sysfs, name);
82         if ((len < 0) || ((size_t) len >= sizeof (filename)))
83                 return -1;
84
85         len = read_file_contents (filename, data, sizeof(data));
86         if (len > 1 && data[--len] == '\n') {
87                 char *endptr = NULL;
88                 double temp;
89
90                 data[len] = 0;
91                 errno = 0;
92                 temp = strtod (data, &endptr) / 1000.0;
93
94                 if (endptr == data + len && errno == 0) {
95                         thermal_submit(name, TEMP, temp);
96                         success = 1;
97                 }
98         }
99
100         len = snprintf (filename, sizeof (filename),
101                         "%s/%s/cur_state", dirname_sysfs, name);
102         if ((len < 0) || ((size_t) len >= sizeof (filename)))
103                 return -1;
104
105         len = read_file_contents (filename, data, sizeof(data));
106         if (len > 1 && data[--len] == '\n') {
107                 char *endptr = NULL;
108                 double state;
109
110                 data[len] = 0;
111                 errno = 0;
112                 state = strtod (data, &endptr);
113
114                 if (endptr == data + len && errno == 0) {
115                         thermal_submit(name, COOLING_DEV, state);
116                         success = 1;
117                 }
118         }
119
120         return (success ? 0 : -1);
121 }
122
123 static int thermal_procfs_device_read (const char __attribute__((unused)) *dir,
124                 const char *name, void __attribute__((unused)) *user_data)
125 {
126         const char str_temp[] = "temperature:";
127         char filename[256];
128         char data[1024];
129         int len;
130
131         if (device_list && ignorelist_match (device_list, name))
132                 return -1;
133
134         /**
135          * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
136          * temperature:             55 C
137          */
138         
139         len = snprintf (filename, sizeof (filename),
140                         "%s/%s/temperature", dirname_procfs, name);
141         if ((len < 0) || ((size_t) len >= sizeof (filename)))
142                 return -1;
143
144         len = read_file_contents (filename, data, sizeof(data));
145         if ((len > 0) && ((size_t) len > sizeof(str_temp))
146                         && (data[--len] == '\n')
147                         && (! strncmp(data, str_temp, sizeof(str_temp)-1))) {
148                 char *endptr = NULL;
149                 double temp;
150                 double factor, add;
151                 
152                 if (data[--len] == 'C') {
153                         add = 0;
154                         factor = 1.0;
155                 } else if (data[len] == 'F') {
156                         add = -32;
157                         factor = 5.0/9.0;
158                 } else if (data[len] == 'K') {
159                         add = -273.15;
160                         factor = 1.0;
161                 } else
162                         return -1;
163
164                 while (len > 0 && data[--len] == ' ')
165                         ;
166                 data[len + 1] = 0;
167
168                 while (len > 0 && data[--len] != ' ')
169                         ;
170                 ++len;
171
172                 errno = 0;
173                 temp = (strtod (data + len, &endptr) + add) * factor;
174
175                 if (endptr != data + len && errno == 0) {
176                         thermal_submit(name, TEMP, temp);
177                         return 0;
178                 }
179         }
180
181         return -1;
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, /* include hidden */ 0);
222 }
223
224 static int thermal_procfs_read (void)
225 {
226         return walk_directory (dirname_procfs, thermal_procfs_device_read,
227                         /* user_data = */ NULL, /* include hidden */ 0);
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         return ret;
241 }
242
243 static int thermal_shutdown (void)
244 {
245         ignorelist_free (device_list);
246
247         return 0;
248 }
249
250 void module_register (void)
251 {
252         plugin_register_config ("thermal", thermal_config,
253                         config_keys, STATIC_ARRAY_SIZE(config_keys));
254         plugin_register_init ("thermal", thermal_init);
255         plugin_register_shutdown ("thermal", thermal_shutdown);
256 }
257