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 #if HAVE_IOKIT_IOKITLIB_H
53 # define IOKIT_HAVE_READ 1
55 # define IOKIT_HAVE_READ 0
58 #if HAVE_IOKIT_IOKITLIB_H
59 static mach_port_t io_master_port = MACH_PORT_NULL;
62 static data_source_t data_source_fanspeed[1] =
64 {"value", DS_TYPE_GAUGE, 0, NAN}
67 static data_set_t fanspeed_ds =
69 "fanspeed", 1, data_source_fanspeed
72 static data_source_t data_source_temperature[1] =
74 {"value", DS_TYPE_GAUGE, -273.15, NAN}
77 static data_set_t temperature_ds =
79 "temperature", 1, data_source_temperature
83 static int as_init (void)
87 if (io_master_port != MACH_PORT_NULL)
89 mach_port_deallocate (mach_task_self (),
91 io_master_port = MACH_PORT_NULL;
94 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
95 if (status != kIOReturnSuccess)
97 ERROR ("IOMasterPort failed: %s",
98 mach_error_string (status));
99 io_master_port = MACH_PORT_NULL;
106 static void as_submit (const char *type, const char *type_instance,
110 value_list_t vl = VALUE_LIST_INIT;
112 DEBUG ("type = %s; type_instance = %s; val = %f;",
113 type, type_instance, val);
115 values[0].gauge = val;
119 vl.time = time (NULL);
120 strcpy (vl.host, hostname_g);
121 strcpy (vl.plugin, "apple_sensors");
122 strcpy (vl.plugin_instance, "");
123 strcpy (vl.type_instance, type_instance);
125 plugin_dispatch_values (type, &vl);
128 static int as_read (void)
130 kern_return_t status;
131 io_iterator_t iterator;
133 CFMutableDictionaryRef prop_dict;
142 if (!io_master_port || (io_master_port == MACH_PORT_NULL))
145 status = IOServiceGetMatchingServices (io_master_port,
146 IOServiceNameMatching("IOHWSensor"),
148 if (status != kIOReturnSuccess)
150 ERROR ("IOServiceGetMatchingServices failed: %s",
151 mach_error_string (status));
155 while ((io_obj = IOIteratorNext (iterator)))
158 status = IORegistryEntryCreateCFProperties (io_obj,
162 if (status != kIOReturnSuccess)
164 DEBUG ("IORegistryEntryCreateCFProperties failed: %s",
165 mach_error_string (status));
169 /* Copy the sensor type. */
171 if (!CFDictionaryGetValueIfPresent (prop_dict,
175 if (CFGetTypeID (property) != CFStringGetTypeID ())
177 if (!CFStringGetCString (property,
179 kCFStringEncodingASCII))
183 /* Copy the sensor location. This will be used as `instance'. */
185 if (!CFDictionaryGetValueIfPresent (prop_dict,
189 if (CFGetTypeID (property) != CFStringGetTypeID ())
191 if (!CFStringGetCString (property,
193 kCFStringEncodingASCII))
196 for (i = 0; i < 128; i++)
200 else if (isalnum (inst[i]))
201 inst[i] = (char) tolower (inst[i]);
206 /* Get the actual value. Some computation, based on the `type'
209 if (!CFDictionaryGetValueIfPresent (prop_dict,
210 CFSTR ("current-value"),
213 if (CFGetTypeID (property) != CFNumberGetTypeID ())
215 if (!CFNumberGetValue (property,
220 /* Found e.g. in the 1.5GHz PowerBooks */
221 if (strcmp (type, "temperature") == 0)
223 value_double = ((double) value_int) / 65536.0;
224 strcpy (type, "temperature");
226 else if (strcmp (type, "temp") == 0)
228 value_double = ((double) value_int) / 10.0;
229 strcpy (type, "temperature");
231 else if (strcmp (type, "fanspeed") == 0)
233 value_double = ((double) value_int) / 65536.0;
234 strcpy (type, "fanspeed");
236 else if (strcmp (type, "voltage") == 0)
238 /* Leave this to the battery plugin. */
241 else if (strcmp (type, "adc") == 0)
243 value_double = ((double) value_int) / 10.0;
244 strcpy (type, "fanspeed");
248 DEBUG ("apple_sensors: Read unknown sensor type: %s",
250 value_double = (double) value_int;
253 as_submit (type, inst, value_double);
255 CFRelease (prop_dict);
256 IOObjectRelease (io_obj);
257 } /* while (iterator) */
259 IOObjectRelease (iterator);
263 #endif /* IOKIT_HAVE_READ */
265 void module_register (modreg_e load)
267 if (load & MR_DATASETS)
269 plugin_register_data_set (&fanspeed_ds);
270 plugin_register_data_set (&temperature_ds);
276 plugin_register_init ("apple_sensors", as_init);
277 plugin_register_read ("apple_sensors", as_read);
279 #endif /* IOKIT_HAVE_READ */
280 } /* void module_register */