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>
35 #if HAVE_MACH_MACH_TYPES_H
36 # include <mach/mach_types.h>
38 #if HAVE_MACH_MACH_INIT_H
39 # include <mach/mach_init.h>
41 #if HAVE_MACH_MACH_ERROR_H
42 # include <mach/mach_error.h>
44 #if HAVE_MACH_MACH_PORT_H
45 # include <mach/mach_port.h>
47 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
48 # include <CoreFoundation/CoreFoundation.h>
50 #if HAVE_IOKIT_IOKITLIB_H
51 # include <IOKit/IOKitLib.h>
53 #if HAVE_IOKIT_IOTYPES_H
54 # include <IOKit/IOTypes.h>
57 static mach_port_t io_master_port = MACH_PORT_NULL;
59 static int as_init (void)
63 if (io_master_port != MACH_PORT_NULL)
65 mach_port_deallocate (mach_task_self (),
67 io_master_port = MACH_PORT_NULL;
70 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
71 if (status != kIOReturnSuccess)
73 ERROR ("IOMasterPort failed: %s",
74 mach_error_string (status));
75 io_master_port = MACH_PORT_NULL;
82 static void as_submit (const char *type, const char *type_instance,
86 value_list_t vl = VALUE_LIST_INIT;
88 DEBUG ("type = %s; type_instance = %s; val = %f;",
89 type, type_instance, val);
91 values[0].gauge = val;
95 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
96 sstrncpy (vl.plugin, "apple_sensors", sizeof (vl.plugin));
97 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
98 sstrncpy (vl.type, type, sizeof (vl.type));
99 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
101 plugin_dispatch_values (&vl);
104 static int as_read (void)
106 kern_return_t status;
107 io_iterator_t iterator;
109 CFMutableDictionaryRef prop_dict;
118 if (!io_master_port || (io_master_port == MACH_PORT_NULL))
121 status = IOServiceGetMatchingServices (io_master_port,
122 IOServiceNameMatching("IOHWSensor"),
124 if (status != kIOReturnSuccess)
126 ERROR ("IOServiceGetMatchingServices failed: %s",
127 mach_error_string (status));
131 while ((io_obj = IOIteratorNext (iterator)))
134 status = IORegistryEntryCreateCFProperties (io_obj,
138 if (status != kIOReturnSuccess)
140 DEBUG ("IORegistryEntryCreateCFProperties failed: %s",
141 mach_error_string (status));
145 /* Copy the sensor type. */
147 if (!CFDictionaryGetValueIfPresent (prop_dict,
151 if (CFGetTypeID (property) != CFStringGetTypeID ())
153 if (!CFStringGetCString (property,
155 kCFStringEncodingASCII))
157 type[sizeof (type) - 1] = '\0';
159 /* Copy the sensor location. This will be used as `instance'. */
161 if (!CFDictionaryGetValueIfPresent (prop_dict,
165 if (CFGetTypeID (property) != CFStringGetTypeID ())
167 if (!CFStringGetCString (property,
169 kCFStringEncodingASCII))
171 inst[sizeof (inst) - 1] = '\0';
172 for (i = 0; i < 128; i++)
176 else if (isalnum (inst[i]))
177 inst[i] = (char) tolower (inst[i]);
182 /* Get the actual value. Some computation, based on the `type'
185 if (!CFDictionaryGetValueIfPresent (prop_dict,
186 CFSTR ("current-value"),
189 if (CFGetTypeID (property) != CFNumberGetTypeID ())
191 if (!CFNumberGetValue (property,
196 /* Found e.g. in the 1.5GHz PowerBooks */
197 if (strcmp (type, "temperature") == 0)
199 value_double = ((double) value_int) / 65536.0;
200 sstrncpy (type, "temperature", sizeof (type));
202 else if (strcmp (type, "temp") == 0)
204 value_double = ((double) value_int) / 10.0;
205 sstrncpy (type, "temperature", sizeof (type));
207 else if (strcmp (type, "fanspeed") == 0)
209 value_double = ((double) value_int) / 65536.0;
210 sstrncpy (type, "fanspeed", sizeof (type));
212 else if (strcmp (type, "voltage") == 0)
214 /* Leave this to the battery plugin. */
217 else if (strcmp (type, "adc") == 0)
219 value_double = ((double) value_int) / 10.0;
220 sstrncpy (type, "fanspeed", sizeof (type));
224 DEBUG ("apple_sensors: Read unknown sensor type: %s",
226 value_double = (double) value_int;
229 as_submit (type, inst, value_double);
231 CFRelease (prop_dict);
232 IOObjectRelease (io_obj);
233 } /* while (iterator) */
235 IOObjectRelease (iterator);
240 void module_register (void)
242 plugin_register_init ("apple_sensors", as_init);
243 plugin_register_read ("apple_sensors", as_read);
244 } /* void module_register */