2 * collectd - src/iokit.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 "iokit"
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 = "temperature-%s.rrd";
64 static char *ds_def[] =
66 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
69 static int ds_num = 1;
71 static void iokit_init (void)
76 /* FIXME: de-allocate port if it's defined */
78 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
79 if (status != kIOReturnSuccess)
81 syslog (LOG_ERR, "IOMasterPort failed: %s",
82 mach_error_string (status));
83 io_master_port = MACH_PORT_NULL;
91 static void temperature_write (char *host, char *inst, char *val)
93 rrd_update_file (host, temperature_file, val, ds_def, ds_num);
97 static void iokit_submit (char *type, char *inst, double value)
101 if (snprintf (buf, 1024, "%u:%f", (unsigned int) curtime,
105 plugin_submit (type, inst, buf);
108 static void iokit_read (void)
110 kern_return_t status;
111 io_iterator_t iterator;
113 CFMutableDictionaryRef prop_dict;
122 if (!io_master_port || (io_master_port == MACH_PORT_NULL))
125 status = IOServiceGetMatchingServices (io_master_port,
126 IOServiceNameMatching("IOHWSensor"),
128 if (status != kIOReturnSuccess)
130 syslog (LOG_ERR, "IOServiceGetMatchingServices failed: %s",
131 mach_error_string (status));
135 while ((io_obj = IOIteratorNext (iterator)))
138 status = IORegistryEntryCreateCFProperties (io_obj,
142 if (status != kIOReturnSuccess)
144 DBG ("IORegistryEntryCreateCFProperties failed: %s",
145 mach_error_string (status));
149 /* Copy the sensor type. */
151 if (!CFDictionaryGetValueIfPresent (prop_dict,
155 if (CFGetTypeID (property) != CFStringGetTypeID ())
157 if (!CFStringGetCString (property,
159 kCFStringEncodingASCII))
163 /* Copy the sensor location. This will be used as `instance'. */
165 if (!CFDictionaryGetValueIfPresent (prop_dict,
169 if (CFGetTypeID (property) != CFStringGetTypeID ())
171 if (!CFStringGetCString (property,
173 kCFStringEncodingASCII))
176 for (i = 0; i < 128; i++)
180 else if (isalnum (inst[i]))
181 inst[i] = (char) tolower (inst[i]);
186 /* Get the actual value. Some computation, based on the `type'
189 if (!CFDictionaryGetValueIfPresent (prop_dict,
190 CFSTR ("current-value"),
193 if (CFGetTypeID (property) != CFNumberGetTypeID ())
195 if (!CFNumberGetValue (property,
200 if ((strcmp (type, "temperature") == 0)
201 || (strcmp (type, "fanspeed") == 0)
202 || (strcmp (type, "voltage") == 0))
204 value_double = ((double) value_int) / 65536.0;
208 value_double = (double) value_int;
212 DBG ("type = %s, inst = %s, value_int = %i, value_double = %f",
213 type, inst, value_int, value_double);
214 iokit_submit (type, inst, value_double);
216 CFRelease (prop_dict);
217 IOObjectRelease (io_obj);
218 } /* while (iterator) */
220 IOObjectRelease (iterator);
223 # define iokit_read NULL
224 #endif /* IOKIT_HAVE_READ */
226 void module_register (void)
228 DBG ("IOKIT_HAVE_READ = %i", IOKIT_HAVE_READ);
230 plugin_register (MODULE_NAME, iokit_init, iokit_read, NULL);
231 plugin_register ("iokit-temperature", NULL, NULL, temperature_write);