Merge branch 'collectd-5.5' into collectd-5.6
[collectd.git] / src / apple_sensors.c
1 /**
2  * collectd - src/apple_sensors.c
3  * Copyright (C) 2006,2007  Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 #if HAVE_CTYPE_H
33 #  include <ctype.h>
34 #endif
35
36 #if HAVE_MACH_MACH_TYPES_H
37 #  include <mach/mach_types.h>
38 #endif
39 #if HAVE_MACH_MACH_INIT_H
40 #  include <mach/mach_init.h>
41 #endif
42 #if HAVE_MACH_MACH_ERROR_H
43 #  include <mach/mach_error.h>
44 #endif
45 #if HAVE_MACH_MACH_PORT_H
46 #  include <mach/mach_port.h>
47 #endif
48 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
49 #  include <CoreFoundation/CoreFoundation.h>
50 #endif
51 #if HAVE_IOKIT_IOKITLIB_H
52 #  include <IOKit/IOKitLib.h>
53 #endif
54 #if HAVE_IOKIT_IOTYPES_H
55 #  include <IOKit/IOTypes.h>
56 #endif
57
58 static mach_port_t io_master_port = MACH_PORT_NULL;
59
60 static int as_init (void)
61 {
62         kern_return_t status;
63
64         if (io_master_port != MACH_PORT_NULL)
65         {
66                 mach_port_deallocate (mach_task_self (),
67                                 io_master_port);
68                 io_master_port = MACH_PORT_NULL;
69         }
70
71         status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
72         if (status != kIOReturnSuccess)
73         {
74                 ERROR ("IOMasterPort failed: %s",
75                                 mach_error_string (status));
76                 io_master_port = MACH_PORT_NULL;
77                 return (-1);
78         }
79
80         return (0);
81 }
82
83 static void as_submit (const char *type, const char *type_instance,
84                 double val)
85 {
86         value_t values[1];
87         value_list_t vl = VALUE_LIST_INIT;
88
89         DEBUG ("type = %s; type_instance = %s; val = %f;",
90                         type, type_instance, val);
91
92         values[0].gauge = val;
93
94         vl.values = values;
95         vl.values_len = 1;
96         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
97         sstrncpy (vl.plugin, "apple_sensors", sizeof (vl.plugin));
98         sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
99         sstrncpy (vl.type, type, sizeof (vl.type));
100         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
101
102         plugin_dispatch_values (&vl);
103 }
104
105 static int as_read (void)
106 {
107         kern_return_t   status;
108         io_iterator_t   iterator;
109         io_object_t     io_obj;
110         CFMutableDictionaryRef prop_dict;
111         CFTypeRef       property;
112
113         char   type[128];
114         char   inst[128];
115         int    value_int;
116         double value_double;
117         if (!io_master_port || (io_master_port == MACH_PORT_NULL))
118                 return (-1);
119
120         status = IOServiceGetMatchingServices (io_master_port,
121                         IOServiceNameMatching("IOHWSensor"),
122                         &iterator);
123         if (status != kIOReturnSuccess)
124         {
125                 ERROR ("IOServiceGetMatchingServices failed: %s",
126                                 mach_error_string (status));
127                 return (-1);
128         }
129
130         while ((io_obj = IOIteratorNext (iterator)))
131         {
132                 prop_dict = NULL;
133                 status = IORegistryEntryCreateCFProperties (io_obj,
134                                 &prop_dict,
135                                 kCFAllocatorDefault,
136                                 kNilOptions);
137                 if (status != kIOReturnSuccess)
138                 {
139                         DEBUG ("IORegistryEntryCreateCFProperties failed: %s",
140                                         mach_error_string (status));
141                         continue;
142                 }
143
144                 /* Copy the sensor type. */
145                 property = NULL;
146                 if (!CFDictionaryGetValueIfPresent (prop_dict,
147                                         CFSTR ("type"),
148                                         &property))
149                         continue;
150                 if (CFGetTypeID (property) != CFStringGetTypeID ())
151                         continue;
152                 if (!CFStringGetCString (property,
153                                         type, sizeof (type),
154                                         kCFStringEncodingASCII))
155                         continue;
156                 type[sizeof (type) - 1] = '\0';
157
158                 /* Copy the sensor location. This will be used as `instance'. */
159                 property = NULL;
160                 if (!CFDictionaryGetValueIfPresent (prop_dict,
161                                         CFSTR ("location"),
162                                         &property))
163                         continue;
164                 if (CFGetTypeID (property) != CFStringGetTypeID ())
165                         continue;
166                 if (!CFStringGetCString (property,
167                                         inst, sizeof (inst),
168                                         kCFStringEncodingASCII))
169                         continue;
170                 inst[sizeof (inst) - 1] = '\0';
171                 for (int i = 0; i < 128; i++)
172                 {
173                         if (inst[i] == '\0')
174                                 break;
175                         else if (isalnum (inst[i]))
176                                 inst[i] = (char) tolower (inst[i]);
177                         else
178                                 inst[i] = '_';
179                 }
180
181                 /* Get the actual value. Some computation, based on the `type'
182                  * is neccessary. */
183                 property = NULL;
184                 if (!CFDictionaryGetValueIfPresent (prop_dict,
185                                         CFSTR ("current-value"),
186                                         &property))
187                         continue;
188                 if (CFGetTypeID (property) != CFNumberGetTypeID ())
189                         continue;
190                 if (!CFNumberGetValue (property,
191                                         kCFNumberIntType,
192                                         &value_int))
193                         continue;
194
195                 /* Found e.g. in the 1.5GHz PowerBooks */
196                 if (strcmp (type, "temperature") == 0)
197                 {
198                         value_double = ((double) value_int) / 65536.0;
199                         sstrncpy (type, "temperature", sizeof (type));
200                 }
201                 else if (strcmp (type, "temp") == 0)
202                 {
203                         value_double = ((double) value_int) / 10.0;
204                         sstrncpy (type, "temperature", sizeof (type));
205                 }
206                 else if (strcmp (type, "fanspeed") == 0)
207                 {
208                         value_double = ((double) value_int) / 65536.0;
209                         sstrncpy (type, "fanspeed", sizeof (type));
210                 }
211                 else if (strcmp (type, "voltage") == 0)
212                 {
213                         /* Leave this to the battery plugin. */
214                         continue;
215                 }
216                 else if (strcmp (type, "adc") == 0)
217                 {
218                         value_double = ((double) value_int) / 10.0;
219                         sstrncpy (type, "fanspeed", sizeof (type));
220                 }
221                 else
222                 {
223                         DEBUG ("apple_sensors: Read unknown sensor type: %s",
224                                         type);
225                         value_double = (double) value_int;
226                 }
227
228                 as_submit (type, inst, value_double);
229
230                 CFRelease (prop_dict);
231                 IOObjectRelease (io_obj);
232         } /* while (iterator) */
233
234         IOObjectRelease (iterator);
235
236         return (0);
237 } /* int as_read */
238
239 void module_register (void)
240 {
241         plugin_register_init ("apple_sensors", as_init);
242         plugin_register_read ("apple_sensors", as_read);
243 } /* void module_register */