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";
65 static char *ds_def[] =
67 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
70 static int ds_num = 1;
72 static void as_init (void)
77 /* FIXME: de-allocate port if it's defined */
79 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
80 if (status != kIOReturnSuccess)
82 syslog (LOG_ERR, "IOMasterPort failed: %s",
83 mach_error_string (status));
84 io_master_port = MACH_PORT_NULL;
92 static void as_write (char *host, char *inst, char *val, const char *template)
97 status = snprintf (filename, 256, template, inst);
98 if ((status < 1) || (status >= 256))
101 rrd_update_file (host, filename, val, ds_def, ds_num);
104 static void temperature_write (char *host, char *inst, char *val)
106 as_write (host, inst, val, temperature_file);
109 static void fanspeed_write (char *host, char *inst, char *val)
111 as_write (host, inst, val, fanspeed_file);
115 static void as_submit (char *type, char *inst, double value)
119 if (snprintf (buf, 1024, "%u:%f", (unsigned int) curtime,
123 plugin_submit (type, inst, buf);
126 static void as_read (void)
128 kern_return_t status;
129 io_iterator_t iterator;
131 CFMutableDictionaryRef prop_dict;
140 if (!io_master_port || (io_master_port == MACH_PORT_NULL))
143 status = IOServiceGetMatchingServices (io_master_port,
144 IOServiceNameMatching("IOHWSensor"),
146 if (status != kIOReturnSuccess)
148 syslog (LOG_ERR, "IOServiceGetMatchingServices failed: %s",
149 mach_error_string (status));
153 while ((io_obj = IOIteratorNext (iterator)))
156 status = IORegistryEntryCreateCFProperties (io_obj,
160 if (status != kIOReturnSuccess)
162 DBG ("IORegistryEntryCreateCFProperties failed: %s",
163 mach_error_string (status));
167 /* Copy the sensor type. */
169 if (!CFDictionaryGetValueIfPresent (prop_dict,
173 if (CFGetTypeID (property) != CFStringGetTypeID ())
175 if (!CFStringGetCString (property,
177 kCFStringEncodingASCII))
181 /* Copy the sensor location. This will be used as `instance'. */
183 if (!CFDictionaryGetValueIfPresent (prop_dict,
187 if (CFGetTypeID (property) != CFStringGetTypeID ())
189 if (!CFStringGetCString (property,
191 kCFStringEncodingASCII))
194 for (i = 0; i < 128; i++)
198 else if (isalnum (inst[i]))
199 inst[i] = (char) tolower (inst[i]);
204 /* Get the actual value. Some computation, based on the `type'
207 if (!CFDictionaryGetValueIfPresent (prop_dict,
208 CFSTR ("current-value"),
211 if (CFGetTypeID (property) != CFNumberGetTypeID ())
213 if (!CFNumberGetValue (property,
218 if (strcmp (type, "temperature") == 0)
220 value_double = ((double) value_int) / 65536.0;
221 strncpy (type, "apple_temperature", 128);
223 else if (strcmp (type, "fanspeed") == 0)
225 value_double = ((double) value_int) / 65536.0;
226 strncpy (type, "apple_fanspeed", 128);
228 else if (strcmp (type, "voltage") == 0)
230 /* Leave this to the battery plugin. */
235 DBG ("apple_sensors: Read unknown sensor type: %s",
237 value_double = (double) value_int;
240 as_submit (type, inst, value_double);
242 CFRelease (prop_dict);
243 IOObjectRelease (io_obj);
244 } /* while (iterator) */
246 IOObjectRelease (iterator);
249 # define as_read NULL
250 #endif /* IOKIT_HAVE_READ */
252 void module_register (void)
254 plugin_register (MODULE_NAME, as_init, as_read, NULL);
255 plugin_register ("apple_temperature", NULL, NULL, temperature_write);
256 plugin_register ("apple_fanspeed", NULL, NULL, fanspeed_write);