sigrok: use pkg-config to find library
[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         int    i;
118
119         if (!io_master_port || (io_master_port == MACH_PORT_NULL))
120                 return (-1);
121
122         status = IOServiceGetMatchingServices (io_master_port,
123                         IOServiceNameMatching("IOHWSensor"),
124                         &iterator);
125         if (status != kIOReturnSuccess)
126         {
127                 ERROR ("IOServiceGetMatchingServices failed: %s",
128                                 mach_error_string (status));
129                 return (-1);
130         }
131
132         while ((io_obj = IOIteratorNext (iterator)))
133         {
134                 prop_dict = NULL;
135                 status = IORegistryEntryCreateCFProperties (io_obj,
136                                 &prop_dict,
137                                 kCFAllocatorDefault,
138                                 kNilOptions);
139                 if (status != kIOReturnSuccess)
140                 {
141                         DEBUG ("IORegistryEntryCreateCFProperties failed: %s",
142                                         mach_error_string (status));
143                         continue;
144                 }
145
146                 /* Copy the sensor type. */
147                 property = NULL;
148                 if (!CFDictionaryGetValueIfPresent (prop_dict,
149                                         CFSTR ("type"),
150                                         &property))
151                         continue;
152                 if (CFGetTypeID (property) != CFStringGetTypeID ())
153                         continue;
154                 if (!CFStringGetCString (property,
155                                         type, sizeof (type),
156                                         kCFStringEncodingASCII))
157                         continue;
158                 type[sizeof (type) - 1] = '\0';
159
160                 /* Copy the sensor location. This will be used as `instance'. */
161                 property = NULL;
162                 if (!CFDictionaryGetValueIfPresent (prop_dict,
163                                         CFSTR ("location"),
164                                         &property))
165                         continue;
166                 if (CFGetTypeID (property) != CFStringGetTypeID ())
167                         continue;
168                 if (!CFStringGetCString (property,
169                                         inst, sizeof (inst),
170                                         kCFStringEncodingASCII))
171                         continue;
172                 inst[sizeof (inst) - 1] = '\0';
173                 for (i = 0; i < 128; i++)
174                 {
175                         if (inst[i] == '\0')
176                                 break;
177                         else if (isalnum (inst[i]))
178                                 inst[i] = (char) tolower (inst[i]);
179                         else
180                                 inst[i] = '_';
181                 }
182
183                 /* Get the actual value. Some computation, based on the `type'
184                  * is neccessary. */
185                 property = NULL;
186                 if (!CFDictionaryGetValueIfPresent (prop_dict,
187                                         CFSTR ("current-value"),
188                                         &property))
189                         continue;
190                 if (CFGetTypeID (property) != CFNumberGetTypeID ())
191                         continue;
192                 if (!CFNumberGetValue (property,
193                                         kCFNumberIntType,
194                                         &value_int))
195                         continue;
196
197                 /* Found e.g. in the 1.5GHz PowerBooks */
198                 if (strcmp (type, "temperature") == 0)
199                 {
200                         value_double = ((double) value_int) / 65536.0;
201                         sstrncpy (type, "temperature", sizeof (type));
202                 }
203                 else if (strcmp (type, "temp") == 0)
204                 {
205                         value_double = ((double) value_int) / 10.0;
206                         sstrncpy (type, "temperature", sizeof (type));
207                 }
208                 else if (strcmp (type, "fanspeed") == 0)
209                 {
210                         value_double = ((double) value_int) / 65536.0;
211                         sstrncpy (type, "fanspeed", sizeof (type));
212                 }
213                 else if (strcmp (type, "voltage") == 0)
214                 {
215                         /* Leave this to the battery plugin. */
216                         continue;
217                 }
218                 else if (strcmp (type, "adc") == 0)
219                 {
220                         value_double = ((double) value_int) / 10.0;
221                         sstrncpy (type, "fanspeed", sizeof (type));
222                 }
223                 else
224                 {
225                         DEBUG ("apple_sensors: Read unknown sensor type: %s",
226                                         type);
227                         value_double = (double) value_int;
228                 }
229
230                 as_submit (type, inst, value_double);
231
232                 CFRelease (prop_dict);
233                 IOObjectRelease (io_obj);
234         } /* while (iterator) */
235
236         IOObjectRelease (iterator);
237
238         return (0);
239 } /* int as_read */
240
241 void module_register (void)
242 {
243         plugin_register_init ("apple_sensors", as_init);
244         plugin_register_read ("apple_sensors", as_read);
245 } /* void module_register */