2 * collectd - src/apple_sensors.c
3 * Copyright (C) 2006,2007 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
36 #if HAVE_MACH_MACH_TYPES_H
37 # include <mach/mach_types.h>
39 #if HAVE_MACH_MACH_INIT_H
40 # include <mach/mach_init.h>
42 #if HAVE_MACH_MACH_ERROR_H
43 # include <mach/mach_error.h>
45 #if HAVE_MACH_MACH_PORT_H
46 # include <mach/mach_port.h>
48 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
49 # include <CoreFoundation/CoreFoundation.h>
51 #if HAVE_IOKIT_IOKITLIB_H
52 # include <IOKit/IOKitLib.h>
54 #if HAVE_IOKIT_IOTYPES_H
55 # include <IOKit/IOTypes.h>
58 static mach_port_t io_master_port = MACH_PORT_NULL;
60 static int as_init (void)
64 if (io_master_port != MACH_PORT_NULL)
66 mach_port_deallocate (mach_task_self (),
68 io_master_port = MACH_PORT_NULL;
71 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
72 if (status != kIOReturnSuccess)
74 ERROR ("IOMasterPort failed: %s",
75 mach_error_string (status));
76 io_master_port = MACH_PORT_NULL;
83 static void as_submit (const char *type, const char *type_instance,
87 value_list_t vl = VALUE_LIST_INIT;
89 DEBUG ("type = %s; type_instance = %s; val = %f;",
90 type, type_instance, val);
92 values[0].gauge = val;
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));
102 plugin_dispatch_values (&vl);
105 static int as_read (void)
107 kern_return_t status;
108 io_iterator_t iterator;
110 CFMutableDictionaryRef prop_dict;
117 if (!io_master_port || (io_master_port == MACH_PORT_NULL))
120 status = IOServiceGetMatchingServices (io_master_port,
121 IOServiceNameMatching("IOHWSensor"),
123 if (status != kIOReturnSuccess)
125 ERROR ("IOServiceGetMatchingServices failed: %s",
126 mach_error_string (status));
130 while ((io_obj = IOIteratorNext (iterator)))
133 status = IORegistryEntryCreateCFProperties (io_obj,
137 if (status != kIOReturnSuccess)
139 DEBUG ("IORegistryEntryCreateCFProperties failed: %s",
140 mach_error_string (status));
144 /* Copy the sensor type. */
146 if (!CFDictionaryGetValueIfPresent (prop_dict,
150 if (CFGetTypeID (property) != CFStringGetTypeID ())
152 if (!CFStringGetCString (property,
154 kCFStringEncodingASCII))
156 type[sizeof (type) - 1] = '\0';
158 /* Copy the sensor location. This will be used as `instance'. */
160 if (!CFDictionaryGetValueIfPresent (prop_dict,
164 if (CFGetTypeID (property) != CFStringGetTypeID ())
166 if (!CFStringGetCString (property,
168 kCFStringEncodingASCII))
170 inst[sizeof (inst) - 1] = '\0';
171 for (int i = 0; i < 128; i++)
175 else if (isalnum (inst[i]))
176 inst[i] = (char) tolower (inst[i]);
181 /* Get the actual value. Some computation, based on the `type'
184 if (!CFDictionaryGetValueIfPresent (prop_dict,
185 CFSTR ("current-value"),
188 if (CFGetTypeID (property) != CFNumberGetTypeID ())
190 if (!CFNumberGetValue (property,
195 /* Found e.g. in the 1.5GHz PowerBooks */
196 if (strcmp (type, "temperature") == 0)
198 value_double = ((double) value_int) / 65536.0;
199 sstrncpy (type, "temperature", sizeof (type));
201 else if (strcmp (type, "temp") == 0)
203 value_double = ((double) value_int) / 10.0;
204 sstrncpy (type, "temperature", sizeof (type));
206 else if (strcmp (type, "fanspeed") == 0)
208 value_double = ((double) value_int) / 65536.0;
209 sstrncpy (type, "fanspeed", sizeof (type));
211 else if (strcmp (type, "voltage") == 0)
213 /* Leave this to the battery plugin. */
216 else if (strcmp (type, "adc") == 0)
218 value_double = ((double) value_int) / 10.0;
219 sstrncpy (type, "fanspeed", sizeof (type));
223 DEBUG ("apple_sensors: Read unknown sensor type: %s",
225 value_double = (double) value_int;
228 as_submit (type, inst, value_double);
230 CFRelease (prop_dict);
231 IOObjectRelease (io_obj);
232 } /* while (iterator) */
234 IOObjectRelease (iterator);
239 void module_register (void)
241 plugin_register_init ("apple_sensors", as_init);
242 plugin_register_read ("apple_sensors", as_read);
243 } /* void module_register */