Merge branch 'collectd-3.11'
[collectd.git] / src / apple_sensors.c
1 /**
2  * collectd - src/apple_sensors.c
3  * Copyright (C) 2006,2007  Florian octo Forster
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  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #if HAVE_CTYPE_H
27 #  include <ctype.h>
28 #endif
29
30 #if HAVE_MACH_MACH_TYPES_H
31 #  include <mach/mach_types.h>
32 #endif
33 #if HAVE_MACH_MACH_INIT_H
34 #  include <mach/mach_init.h>
35 #endif
36 #if HAVE_MACH_MACH_ERROR_H
37 #  include <mach/mach_error.h>
38 #endif
39 #if HAVE_MACH_MACH_PORT_H
40 #  include <mach/mach_port.h>
41 #endif
42 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
43 #  include <CoreFoundation/CoreFoundation.h>
44 #endif
45 #if HAVE_IOKIT_IOKITLIB_H
46 #  include <IOKit/IOKitLib.h>
47 #endif
48 #if HAVE_IOKIT_IOTYPES_H
49 #  include <IOKit/IOTypes.h>
50 #endif
51
52 #if HAVE_IOKIT_IOKITLIB_H
53 # define IOKIT_HAVE_READ 1
54 #else
55 # define IOKIT_HAVE_READ 0
56 #endif
57
58 #if HAVE_IOKIT_IOKITLIB_H
59 static mach_port_t io_master_port = MACH_PORT_NULL;
60 #endif
61
62 #if IOKIT_HAVE_READ
63 static int as_init (void)
64 {
65         kern_return_t status;
66         
67         if (io_master_port != MACH_PORT_NULL)
68         {
69                 mach_port_deallocate (mach_task_self (),
70                                 io_master_port);
71                 io_master_port = MACH_PORT_NULL;
72         }
73
74         status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
75         if (status != kIOReturnSuccess)
76         {
77                 ERROR ("IOMasterPort failed: %s",
78                                 mach_error_string (status));
79                 io_master_port = MACH_PORT_NULL;
80                 return (-1);
81         }
82
83         return (0);
84 }
85
86 static void as_submit (const char *type, const char *type_instance,
87                 double val)
88 {
89         value_t values[1];
90         value_list_t vl = VALUE_LIST_INIT;
91
92         DEBUG ("type = %s; type_instance = %s; val = %f;",
93                         type, type_instance, val);
94
95         values[0].gauge = val;
96
97         vl.values = values;
98         vl.values_len = 1;
99         vl.time = time (NULL);
100         strcpy (vl.host, hostname_g);
101         strcpy (vl.plugin, "apple_sensors");
102         strcpy (vl.plugin_instance, "");
103         strcpy (vl.type_instance, type_instance);
104
105         plugin_dispatch_values (type, &vl);
106 }
107
108 static int as_read (void)
109 {
110         kern_return_t   status;
111         io_iterator_t   iterator;
112         io_object_t     io_obj;
113         CFMutableDictionaryRef prop_dict;
114         CFTypeRef       property;
115
116         char   type[128];
117         char   inst[128];
118         int    value_int;
119         double value_double;
120         int    i;
121
122         if (!io_master_port || (io_master_port == MACH_PORT_NULL))
123                 return (-1);
124
125         status = IOServiceGetMatchingServices (io_master_port,
126                         IOServiceNameMatching("IOHWSensor"),
127                         &iterator);
128         if (status != kIOReturnSuccess)
129         {
130                 ERROR ("IOServiceGetMatchingServices failed: %s",
131                                 mach_error_string (status));
132                 return (-1);
133         }
134
135         while ((io_obj = IOIteratorNext (iterator)))
136         {
137                 prop_dict = NULL;
138                 status = IORegistryEntryCreateCFProperties (io_obj,
139                                 &prop_dict,
140                                 kCFAllocatorDefault,
141                                 kNilOptions);
142                 if (status != kIOReturnSuccess)
143                 {
144                         DEBUG ("IORegistryEntryCreateCFProperties failed: %s",
145                                         mach_error_string (status));
146                         continue;
147                 }
148
149                 /* Copy the sensor type. */
150                 property = NULL;
151                 if (!CFDictionaryGetValueIfPresent (prop_dict,
152                                         CFSTR ("type"),
153                                         &property))
154                         continue;
155                 if (CFGetTypeID (property) != CFStringGetTypeID ())
156                         continue;
157                 if (!CFStringGetCString (property,
158                                         type, 128,
159                                         kCFStringEncodingASCII))
160                         continue;
161                 type[127] = '\0';
162
163                 /* Copy the sensor location. This will be used as `instance'. */
164                 property = NULL;
165                 if (!CFDictionaryGetValueIfPresent (prop_dict,
166                                         CFSTR ("location"),
167                                         &property))
168                         continue;
169                 if (CFGetTypeID (property) != CFStringGetTypeID ())
170                         continue;
171                 if (!CFStringGetCString (property,
172                                         inst, 128,
173                                         kCFStringEncodingASCII))
174                         continue;
175                 inst[127] = '\0';
176                 for (i = 0; i < 128; i++)
177                 {
178                         if (inst[i] == '\0')
179                                 break;
180                         else if (isalnum (inst[i]))
181                                 inst[i] = (char) tolower (inst[i]);
182                         else
183                                 inst[i] = '_';
184                 }
185
186                 /* Get the actual value. Some computation, based on the `type'
187                  * is neccessary. */
188                 property = NULL;
189                 if (!CFDictionaryGetValueIfPresent (prop_dict,
190                                         CFSTR ("current-value"),
191                                         &property))
192                         continue;
193                 if (CFGetTypeID (property) != CFNumberGetTypeID ())
194                         continue;
195                 if (!CFNumberGetValue (property,
196                                         kCFNumberIntType,
197                                         &value_int))
198                         continue;
199
200                 /* Found e.g. in the 1.5GHz PowerBooks */
201                 if (strcmp (type, "temperature") == 0)
202                 {
203                         value_double = ((double) value_int) / 65536.0;
204                         strcpy (type, "temperature");
205                 }
206                 else if (strcmp (type, "temp") == 0)
207                 {
208                         value_double = ((double) value_int) / 10.0;
209                         strcpy (type, "temperature");
210                 }
211                 else if (strcmp (type, "fanspeed") == 0)
212                 {
213                         value_double = ((double) value_int) / 65536.0;
214                         strcpy (type, "fanspeed");
215                 }
216                 else if (strcmp (type, "voltage") == 0)
217                 {
218                         /* Leave this to the battery plugin. */
219                         continue;
220                 }
221                 else if (strcmp (type, "adc") == 0)
222                 {
223                         value_double = ((double) value_int) / 10.0;
224                         strcpy (type, "fanspeed");
225                 }
226                 else
227                 {
228                         DEBUG ("apple_sensors: Read unknown sensor type: %s",
229                                         type);
230                         value_double = (double) value_int;
231                 }
232
233                 as_submit (type, inst, value_double);
234
235                 CFRelease (prop_dict);
236                 IOObjectRelease (io_obj);
237         } /* while (iterator) */
238
239         IOObjectRelease (iterator);
240
241         return (0);
242 } /* int as_read */
243 #endif /* IOKIT_HAVE_READ */
244
245 void module_register (void)
246 {
247 #if IOKIT_HAVE_READ
248         plugin_register_init ("apple_sensors", as_init);
249         plugin_register_read ("apple_sensors", as_read);
250 #endif /* IOKIT_HAVE_READ */
251 } /* void module_register */