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 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
92 sstrncpy (vl.plugin, "apple_sensors", sizeof (vl.plugin));
93 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
94 sstrncpy (vl.type, type, sizeof (vl.type));
95 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
97 plugin_dispatch_values (&vl);
100 static int as_read (void)
102 kern_return_t status;
103 io_iterator_t iterator;
105 CFMutableDictionaryRef prop_dict;
114 if (!io_master_port || (io_master_port == MACH_PORT_NULL))
117 status = IOServiceGetMatchingServices (io_master_port,
118 IOServiceNameMatching("IOHWSensor"),
120 if (status != kIOReturnSuccess)
122 ERROR ("IOServiceGetMatchingServices failed: %s",
123 mach_error_string (status));
127 while ((io_obj = IOIteratorNext (iterator)))
130 status = IORegistryEntryCreateCFProperties (io_obj,
134 if (status != kIOReturnSuccess)
136 DEBUG ("IORegistryEntryCreateCFProperties failed: %s",
137 mach_error_string (status));
141 /* Copy the sensor type. */
143 if (!CFDictionaryGetValueIfPresent (prop_dict,
147 if (CFGetTypeID (property) != CFStringGetTypeID ())
149 if (!CFStringGetCString (property,
151 kCFStringEncodingASCII))
153 type[sizeof (type) - 1] = '\0';
155 /* Copy the sensor location. This will be used as `instance'. */
157 if (!CFDictionaryGetValueIfPresent (prop_dict,
161 if (CFGetTypeID (property) != CFStringGetTypeID ())
163 if (!CFStringGetCString (property,
165 kCFStringEncodingASCII))
167 inst[sizeof (inst) - 1] = '\0';
168 for (i = 0; i < 128; i++)
172 else if (isalnum (inst[i]))
173 inst[i] = (char) tolower (inst[i]);
178 /* Get the actual value. Some computation, based on the `type'
181 if (!CFDictionaryGetValueIfPresent (prop_dict,
182 CFSTR ("current-value"),
185 if (CFGetTypeID (property) != CFNumberGetTypeID ())
187 if (!CFNumberGetValue (property,
192 /* Found e.g. in the 1.5GHz PowerBooks */
193 if (strcmp (type, "temperature") == 0)
195 value_double = ((double) value_int) / 65536.0;
196 sstrncpy (type, "temperature", sizeof (type));
198 else if (strcmp (type, "temp") == 0)
200 value_double = ((double) value_int) / 10.0;
201 sstrncpy (type, "temperature", sizeof (type));
203 else if (strcmp (type, "fanspeed") == 0)
205 value_double = ((double) value_int) / 65536.0;
206 sstrncpy (type, "fanspeed", sizeof (type));
208 else if (strcmp (type, "voltage") == 0)
210 /* Leave this to the battery plugin. */
213 else if (strcmp (type, "adc") == 0)
215 value_double = ((double) value_int) / 10.0;
216 sstrncpy (type, "fanspeed", sizeof (type));
220 DEBUG ("apple_sensors: Read unknown sensor type: %s",
222 value_double = (double) value_int;
225 as_submit (type, inst, value_double);
227 CFRelease (prop_dict);
228 IOObjectRelease (io_obj);
229 } /* while (iterator) */
231 IOObjectRelease (iterator);
236 void module_register (void)
238 plugin_register_init ("apple_sensors", as_init);
239 plugin_register_read ("apple_sensors", as_read);
240 } /* void module_register */