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"
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_COREFOUNDATION_COREFOUNDATION_H
40 # include <CoreFoundation/CoreFoundation.h>
42 #if HAVE_IOKIT_IOKITLIB_H
43 # include <IOKit/IOKitLib.h>
45 #if HAVE_IOKIT_IOTYPES_H
46 # include <IOKit/IOTypes.h>
49 #if HAVE_IOKIT_IOKITLIB_H
50 # define IOKIT_HAVE_READ 1
52 # define IOKIT_HAVE_READ 0
56 static mach_port_t io_master_port;
59 static char *temperature_file = "temperature-%s.rrd";
61 static char *ds_def[] =
63 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
66 static int ds_num = 1;
68 static void iokit_init (void)
73 /* FIXME: de-allocate port if it's defined */
75 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
76 if (status != kIOReturnSuccess)
78 syslog (LOG_ERR, "IOMasterPort failed: %s",
79 mach_error_string (status));
80 io_master_port = MACH_PORT_NULL;
88 static void temperature_write (char *host, char *inst, char *val)
90 rrd_update_file (host, temperature_file, val, ds_def, ds_num);
94 static void iokit_submit (char *type, char *inst, int value)
98 if (snprintf (buf, 1024, "%u:%i", (unsigned int) curtime,
102 plugin_submit (type, inst, buf);
105 static void iokit_read (void)
107 kern_return_t status;
108 io_iterator_t iterator;
110 CFMutableDictionaryRef prop_dict;
117 if (!io_master_port || (io_master_port == MACH_PORT_NULL))
120 status = IOServiceGetMatchingServices (io_master_port,
121 IOServiceNameMatching("IOHWSensor"),
123 if (status != kIOReturnSuccess)
125 syslog (LOG_ERR, "IOServiceGetMatchingServices failed: %s",
126 mach_error_string (status));
130 while ((io_obj = IOIteratorNext (iterator)))
133 status = IORegistryEntryCreateCFProperties (io_obj,
137 if (status != kIOReturnSuccess)
139 DBG ("IORegistryEntryCreateCFProperties failed: %s",
140 mach_error_string (status));
144 /* Copy the sensor type. */
146 if (!CFDictionaryGetValueIfPresent (prop_dict,
150 if (CFGetTypeID (property) != CFStringGetTypeID ())
152 if (!CFStringGetCString (property,
154 kCFStringEncodingASCII))
158 /* Copy the sensor location. This will be used as `instance'. */
160 if (!CFDictionaryGetValueIfPresent (prop_dict,
164 if (CFGetTypeID (property) != CFStringGetTypeID ())
166 if (!CFStringGetCString (property,
168 kCFStringEncodingASCII))
172 /* Get the actual value. Some computation, based on the `type'
175 if (!CFDictionaryGetValueIfPresent (prop_dict,
176 CFSTR ("current-value"),
179 if (CFGetTypeID (property) != CFNumberGetTypeID ())
181 if (!CFNumberGetValue (property,
187 DBG ("type = %s, inst = %s, value = %i",
189 iokit_submit (type, inst, value);
191 CFRelease (prop_dict);
192 IOObjectRelease (io_obj);
193 } /* while (iterator) */
195 IOObjectRelease (iterator);
198 # define iokit_read NULL
199 #endif /* IOKIT_HAVE_READ */
201 void module_register (void)
203 DBG ("IOKIT_HAVE_READ = %i", IOKIT_HAVE_READ);
205 plugin_register (MODULE_NAME, iokit_init, iokit_read, NULL);
206 plugin_register ("iokit-temperature", NULL, NULL, temperature_write);