2 * collectd - src/apple_sensors.c
3 * Copyright (C) 2006,2007 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
30 #if HAVE_MACH_MACH_TYPES_H
31 # include <mach/mach_types.h>
33 #if HAVE_MACH_MACH_INIT_H
34 # include <mach/mach_init.h>
36 #if HAVE_MACH_MACH_ERROR_H
37 # include <mach/mach_error.h>
39 #if HAVE_MACH_MACH_PORT_H
40 # include <mach/mach_port.h>
42 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
43 # include <CoreFoundation/CoreFoundation.h>
45 #if HAVE_IOKIT_IOKITLIB_H
46 # include <IOKit/IOKitLib.h>
48 #if HAVE_IOKIT_IOTYPES_H
49 # include <IOKit/IOTypes.h>
52 static mach_port_t io_master_port = MACH_PORT_NULL;
54 static int as_init (void)
58 if (io_master_port != MACH_PORT_NULL)
60 mach_port_deallocate (mach_task_self (),
62 io_master_port = MACH_PORT_NULL;
65 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
66 if (status != kIOReturnSuccess)
68 ERROR ("IOMasterPort failed: %s",
69 mach_error_string (status));
70 io_master_port = MACH_PORT_NULL;
77 static void as_submit (const char *type, const char *type_instance,
81 value_list_t vl = VALUE_LIST_INIT;
83 DEBUG ("type = %s; type_instance = %s; val = %f;",
84 type, type_instance, val);
86 values[0].gauge = val;
90 vl.time = time (NULL);
91 strcpy (vl.host, hostname_g);
92 strcpy (vl.plugin, "apple_sensors");
93 strcpy (vl.plugin_instance, "");
94 strcpy (vl.type_instance, type_instance);
96 plugin_dispatch_values (type, &vl);
99 static int as_read (void)
101 kern_return_t status;
102 io_iterator_t iterator;
104 CFMutableDictionaryRef prop_dict;
113 if (!io_master_port || (io_master_port == MACH_PORT_NULL))
116 status = IOServiceGetMatchingServices (io_master_port,
117 IOServiceNameMatching("IOHWSensor"),
119 if (status != kIOReturnSuccess)
121 ERROR ("IOServiceGetMatchingServices failed: %s",
122 mach_error_string (status));
126 while ((io_obj = IOIteratorNext (iterator)))
129 status = IORegistryEntryCreateCFProperties (io_obj,
133 if (status != kIOReturnSuccess)
135 DEBUG ("IORegistryEntryCreateCFProperties failed: %s",
136 mach_error_string (status));
140 /* Copy the sensor type. */
142 if (!CFDictionaryGetValueIfPresent (prop_dict,
146 if (CFGetTypeID (property) != CFStringGetTypeID ())
148 if (!CFStringGetCString (property,
150 kCFStringEncodingASCII))
154 /* Copy the sensor location. This will be used as `instance'. */
156 if (!CFDictionaryGetValueIfPresent (prop_dict,
160 if (CFGetTypeID (property) != CFStringGetTypeID ())
162 if (!CFStringGetCString (property,
164 kCFStringEncodingASCII))
167 for (i = 0; i < 128; i++)
171 else if (isalnum (inst[i]))
172 inst[i] = (char) tolower (inst[i]);
177 /* Get the actual value. Some computation, based on the `type'
180 if (!CFDictionaryGetValueIfPresent (prop_dict,
181 CFSTR ("current-value"),
184 if (CFGetTypeID (property) != CFNumberGetTypeID ())
186 if (!CFNumberGetValue (property,
191 /* Found e.g. in the 1.5GHz PowerBooks */
192 if (strcmp (type, "temperature") == 0)
194 value_double = ((double) value_int) / 65536.0;
195 strcpy (type, "temperature");
197 else if (strcmp (type, "temp") == 0)
199 value_double = ((double) value_int) / 10.0;
200 strcpy (type, "temperature");
202 else if (strcmp (type, "fanspeed") == 0)
204 value_double = ((double) value_int) / 65536.0;
205 strcpy (type, "fanspeed");
207 else if (strcmp (type, "voltage") == 0)
209 /* Leave this to the battery plugin. */
212 else if (strcmp (type, "adc") == 0)
214 value_double = ((double) value_int) / 10.0;
215 strcpy (type, "fanspeed");
219 DEBUG ("apple_sensors: Read unknown sensor type: %s",
221 value_double = (double) value_int;
224 as_submit (type, inst, value_double);
226 CFRelease (prop_dict);
227 IOObjectRelease (io_obj);
228 } /* while (iterator) */
230 IOObjectRelease (iterator);
235 void module_register (void)
237 plugin_register_init ("apple_sensors", as_init);
238 plugin_register_read ("apple_sensors", as_read);
239 } /* void module_register */