2 * collectd - src/apple_sensors.c
3 * Copyright (C) 2006 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; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
26 #include "utils_debug.h"
28 #define MODULE_NAME "apple_sensors"
33 #if HAVE_MACH_MACH_TYPES_H
34 # include <mach/mach_types.h>
36 #if HAVE_MACH_MACH_INIT_H
37 # include <mach/mach_init.h>
39 #if HAVE_MACH_MACH_ERROR_H
40 # include <mach/mach_error.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
59 static mach_port_t io_master_port;
62 static char *temperature_file = "apple_sensors/temperature-%s.rrd";
63 static char *fanspeed_file = "apple_sensors/fanspeed-%s.rrd";
64 static char *voltage_file = "apple_sensors/temperature-%s.rrd";
66 static char *ds_def[] =
68 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
71 static int ds_num = 1;
73 static void as_init (void)
78 /* FIXME: de-allocate port if it's defined */
80 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
81 if (status != kIOReturnSuccess)
83 syslog (LOG_ERR, "IOMasterPort failed: %s",
84 mach_error_string (status));
85 io_master_port = MACH_PORT_NULL;
93 static void as_write (char *host, char *inst, char *val, const char *template)
98 status = snprintf (filename, 256, template, inst);
99 if ((status < 1) || (status >= 256))
102 rrd_update_file (host, filename, val, ds_def, ds_num);
105 static void temperature_write (char *host, char *inst, char *val)
107 as_write (host, inst, val, temperature_file);
110 static void fanspeed_write (char *host, char *inst, char *val)
112 as_write (host, inst, val, fanspeed_file);
115 static void voltage_write (char *host, char *inst, char *val)
117 as_write (host, inst, val, voltage_file);
121 static void as_submit (char *type, char *inst, double value)
125 if (snprintf (buf, 1024, "%u:%f", (unsigned int) curtime,
129 plugin_submit (type, inst, buf);
132 static void as_read (void)
134 kern_return_t status;
135 io_iterator_t iterator;
137 CFMutableDictionaryRef prop_dict;
146 if (!io_master_port || (io_master_port == MACH_PORT_NULL))
149 status = IOServiceGetMatchingServices (io_master_port,
150 IOServiceNameMatching("IOHWSensor"),
152 if (status != kIOReturnSuccess)
154 syslog (LOG_ERR, "IOServiceGetMatchingServices failed: %s",
155 mach_error_string (status));
159 while ((io_obj = IOIteratorNext (iterator)))
162 status = IORegistryEntryCreateCFProperties (io_obj,
166 if (status != kIOReturnSuccess)
168 DBG ("IORegistryEntryCreateCFProperties failed: %s",
169 mach_error_string (status));
173 /* Copy the sensor type. */
175 if (!CFDictionaryGetValueIfPresent (prop_dict,
179 if (CFGetTypeID (property) != CFStringGetTypeID ())
181 if (!CFStringGetCString (property,
183 kCFStringEncodingASCII))
187 /* Copy the sensor location. This will be used as `instance'. */
189 if (!CFDictionaryGetValueIfPresent (prop_dict,
193 if (CFGetTypeID (property) != CFStringGetTypeID ())
195 if (!CFStringGetCString (property,
197 kCFStringEncodingASCII))
200 for (i = 0; i < 128; i++)
204 else if (isalnum (inst[i]))
205 inst[i] = (char) tolower (inst[i]);
210 /* Get the actual value. Some computation, based on the `type'
213 if (!CFDictionaryGetValueIfPresent (prop_dict,
214 CFSTR ("current-value"),
217 if (CFGetTypeID (property) != CFNumberGetTypeID ())
219 if (!CFNumberGetValue (property,
224 if (strcmp (type, "temperature") == 0)
226 value_double = ((double) value_int) / 65536.0;
227 strncpy (type, "apple_temperature", 128);
229 else if (strcmp (type, "fanspeed") == 0)
231 value_double = ((double) value_int) / 65536.0;
232 strncpy (type, "apple_fanspeed", 128);
234 else if (strcmp (type, "voltage") == 0)
236 value_double = ((double) value_int) / 65536.0;
237 strncpy (type, "apple_voltage", 128);
241 DBG ("apple_sensors: Read unknown sensor type: %s",
243 value_double = (double) value_int;
246 as_submit (type, inst, value_double);
248 CFRelease (prop_dict);
249 IOObjectRelease (io_obj);
250 } /* while (iterator) */
252 IOObjectRelease (iterator);
255 # define as_read NULL
256 #endif /* IOKIT_HAVE_READ */
258 void module_register (void)
260 plugin_register (MODULE_NAME, as_init, as_read, NULL);
261 plugin_register ("apple_temperature", NULL, NULL, temperature_write);
262 plugin_register ("apple_fanspeed", NULL, NULL, fanspeed_write);
263 plugin_register ("apple_voltage", NULL, NULL, voltage_write);