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>
25 #include "utils_debug.h"
31 #if HAVE_MACH_MACH_TYPES_H
32 # include <mach/mach_types.h>
34 #if HAVE_MACH_MACH_INIT_H
35 # include <mach/mach_init.h>
37 #if HAVE_MACH_MACH_ERROR_H
38 # include <mach/mach_error.h>
40 #if HAVE_MACH_MACH_PORT_H
41 # include <mach/mach_port.h>
43 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
44 # include <CoreFoundation/CoreFoundation.h>
46 #if HAVE_IOKIT_IOKITLIB_H
47 # include <IOKit/IOKitLib.h>
49 #if HAVE_IOKIT_IOTYPES_H
50 # include <IOKit/IOTypes.h>
53 #if HAVE_IOKIT_IOKITLIB_H
54 # define IOKIT_HAVE_READ 1
56 # define IOKIT_HAVE_READ 0
59 #if HAVE_IOKIT_IOKITLIB_H
60 static mach_port_t io_master_port = MACH_PORT_NULL;
63 static data_source_t data_source_fanspeed[1] =
65 {"value", DS_TYPE_GAUGE, 0, NAN}
68 static data_set_t fanspeed_ds =
70 "fanspeed", 1, data_source_fanspeed
73 static data_source_t data_source_temperature[1] =
75 {"value", DS_TYPE_GAUGE, -273.15, NAN}
78 static data_set_t temperature_ds =
80 "temperature", 1, data_source_temperature
84 static int as_init (void)
88 if (io_master_port != MACH_PORT_NULL)
90 mach_port_deallocate (mach_task_self (),
92 io_master_port = MACH_PORT_NULL;
95 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
96 if (status != kIOReturnSuccess)
98 syslog (LOG_ERR, "IOMasterPort failed: %s",
99 mach_error_string (status));
100 io_master_port = MACH_PORT_NULL;
107 static void as_submit (const char *type, const char *type_instance,
111 value_list_t vl = VALUE_LIST_INIT;
113 DBG ("type = %s; type_instance = %s; val = %f;",
114 type, type_instance, val);
116 values[0].gauge = val;
120 vl.time = time (NULL);
121 strcpy (vl.host, hostname);
122 strcpy (vl.plugin, "apple_sensors");
123 strcpy (vl.plugin_instance, "");
124 strcpy (vl.type_instance, type_instance);
126 plugin_dispatch_values (type, &vl);
129 static int as_read (void)
131 kern_return_t status;
132 io_iterator_t iterator;
134 CFMutableDictionaryRef prop_dict;
143 if (!io_master_port || (io_master_port == MACH_PORT_NULL))
146 status = IOServiceGetMatchingServices (io_master_port,
147 IOServiceNameMatching("IOHWSensor"),
149 if (status != kIOReturnSuccess)
151 syslog (LOG_ERR, "IOServiceGetMatchingServices failed: %s",
152 mach_error_string (status));
156 while ((io_obj = IOIteratorNext (iterator)))
159 status = IORegistryEntryCreateCFProperties (io_obj,
163 if (status != kIOReturnSuccess)
165 DBG ("IORegistryEntryCreateCFProperties failed: %s",
166 mach_error_string (status));
170 /* Copy the sensor type. */
172 if (!CFDictionaryGetValueIfPresent (prop_dict,
176 if (CFGetTypeID (property) != CFStringGetTypeID ())
178 if (!CFStringGetCString (property,
180 kCFStringEncodingASCII))
184 /* Copy the sensor location. This will be used as `instance'. */
186 if (!CFDictionaryGetValueIfPresent (prop_dict,
190 if (CFGetTypeID (property) != CFStringGetTypeID ())
192 if (!CFStringGetCString (property,
194 kCFStringEncodingASCII))
197 for (i = 0; i < 128; i++)
201 else if (isalnum (inst[i]))
202 inst[i] = (char) tolower (inst[i]);
207 /* Get the actual value. Some computation, based on the `type'
210 if (!CFDictionaryGetValueIfPresent (prop_dict,
211 CFSTR ("current-value"),
214 if (CFGetTypeID (property) != CFNumberGetTypeID ())
216 if (!CFNumberGetValue (property,
221 /* Found e.g. in the 1.5GHz PowerBooks */
222 if (strcmp (type, "temperature") == 0)
224 value_double = ((double) value_int) / 65536.0;
225 strcpy (type, "temperature");
227 else if (strcmp (type, "temp") == 0)
229 value_double = ((double) value_int) / 10.0;
230 strcpy (type, "temperature");
232 else if (strcmp (type, "fanspeed") == 0)
234 value_double = ((double) value_int) / 65536.0;
235 strcpy (type, "fanspeed");
237 else if (strcmp (type, "voltage") == 0)
239 /* Leave this to the battery plugin. */
242 else if (strcmp (type, "adc") == 0)
244 value_double = ((double) value_int) / 10.0;
245 strcpy (type, "fanspeed");
249 DBG ("apple_sensors: Read unknown sensor type: %s",
251 value_double = (double) value_int;
254 as_submit (type, inst, value_double);
256 CFRelease (prop_dict);
257 IOObjectRelease (io_obj);
258 } /* while (iterator) */
260 IOObjectRelease (iterator);
264 #endif /* IOKIT_HAVE_READ */
266 void module_register (void)
268 plugin_register_data_set (&fanspeed_ds);
269 plugin_register_data_set (&temperature_ds);
272 plugin_register_init ("apple_sensors", as_init);
273 plugin_register_read ("apple_sensors", as_read);
274 #endif /* IOKIT_HAVE_READ */