8a533f0fa676b986355a9c547abf8b6d8791c203
[collectd.git] / src / iokit.c
1 /**
2  * collectd - src/iokit.c
3  * Copyright (C) 2006  Florian octo Forster
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors:
20  *   Florian octo Forster <octo at verplant.org>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "utils_debug.h"
27
28 #define MODULE_NAME "iokit"
29
30 #if HAVE_CTYPE_H
31 #  include <ctype.h>
32 #endif
33 #if HAVE_MACH_MACH_TYPES_H
34 #  include <mach/mach_types.h>
35 #endif
36 #if HAVE_MACH_MACH_INIT_H
37 #  include <mach/mach_init.h>
38 #endif
39 #if HAVE_MACH_MACH_ERROR_H
40 #  include <mach/mach_error.h>
41 #endif
42 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
43 #  include <CoreFoundation/CoreFoundation.h>
44 #endif
45 #if HAVE_IOKIT_IOKITLIB_H
46 #  include <IOKit/IOKitLib.h>
47 #endif
48 #if HAVE_IOKIT_IOTYPES_H
49 #  include <IOKit/IOTypes.h>
50 #endif
51
52 #if HAVE_IOKIT_IOKITLIB_H
53 # define IOKIT_HAVE_READ 1
54 #else
55 # define IOKIT_HAVE_READ 0
56 #endif
57
58 #if IOKIT_HAVE_READ
59 static mach_port_t io_master_port;
60 #endif
61
62 static char *temperature_file = "temperature-%s.rrd";
63
64 static char *ds_def[] =
65 {
66         "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
67         NULL
68 };
69 static int ds_num = 1;
70
71 static void iokit_init (void)
72 {
73 #if IOKIT_HAVE_READ
74         kern_return_t status;
75         
76         /* FIXME: de-allocate port if it's defined */
77
78         status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
79         if (status != kIOReturnSuccess)
80         {
81                 syslog (LOG_ERR, "IOMasterPort failed: %s",
82                                 mach_error_string (status));
83                 io_master_port = MACH_PORT_NULL;
84                 return;
85         }
86 #endif
87
88         return;
89 }
90
91 static void temperature_write (char *host, char *inst, char *val)
92 {
93         rrd_update_file (host, temperature_file, val, ds_def, ds_num);
94 }
95
96 #if IOKIT_HAVE_READ
97 static void iokit_submit (char *type, char *inst, double value)
98 {
99         char buf[128];
100
101         if (snprintf (buf, 1024, "%u:%f", (unsigned int) curtime,
102                                 value) >= 128)
103                 return;
104
105         plugin_submit (type, inst, buf);
106 }
107
108 static void iokit_read (void)
109 {
110         kern_return_t   status;
111         io_iterator_t   iterator;
112         io_object_t     io_obj;
113         CFMutableDictionaryRef prop_dict;
114         CFTypeRef       property;
115
116         char   type[128];
117         char   inst[128];
118         int    value_int;
119         double value_double;
120         int    i;
121
122         if (!io_master_port || (io_master_port == MACH_PORT_NULL))
123                 return;
124
125         status = IOServiceGetMatchingServices (io_master_port,
126                         IOServiceNameMatching("IOHWSensor"),
127                         &iterator);
128         if (status != kIOReturnSuccess)
129         {
130                 syslog (LOG_ERR, "IOServiceGetMatchingServices failed: %s",
131                                 mach_error_string (status));
132                 return;
133         }
134
135         while ((io_obj = IOIteratorNext (iterator)))
136         {
137                 prop_dict = NULL;
138                 status = IORegistryEntryCreateCFProperties (io_obj,
139                                 &prop_dict,
140                                 kCFAllocatorDefault,
141                                 kNilOptions);
142                 if (status != kIOReturnSuccess)
143                 {
144                         DBG ("IORegistryEntryCreateCFProperties failed: %s",
145                                         mach_error_string (status));
146                         continue;
147                 }
148
149                 /* Copy the sensor type. */
150                 property = NULL;
151                 if (!CFDictionaryGetValueIfPresent (prop_dict,
152                                         CFSTR ("type"),
153                                         &property))
154                         continue;
155                 if (CFGetTypeID (property) != CFStringGetTypeID ())
156                         continue;
157                 if (!CFStringGetCString (property,
158                                         type, 128,
159                                         kCFStringEncodingASCII))
160                         continue;
161                 type[127] = '\0';
162
163                 /* Copy the sensor location. This will be used as `instance'. */
164                 property = NULL;
165                 if (!CFDictionaryGetValueIfPresent (prop_dict,
166                                         CFSTR ("location"),
167                                         &property))
168                         continue;
169                 if (CFGetTypeID (property) != CFStringGetTypeID ())
170                         continue;
171                 if (!CFStringGetCString (property,
172                                         inst, 128,
173                                         kCFStringEncodingASCII))
174                         continue;
175                 inst[127] = '\0';
176                 for (i = 0; i < 128; i++)
177                 {
178                         if (inst[i] == '\0')
179                                 break;
180                         else if (isalnum (inst[i]))
181                                 inst[i] = (char) tolower (inst[i]);
182                         else
183                                 inst[i] = '_';
184                 }
185
186                 /* Get the actual value. Some computation, based on the `type'
187                  * is neccessary. */
188                 property = NULL;
189                 if (!CFDictionaryGetValueIfPresent (prop_dict,
190                                         CFSTR ("current-value"),
191                                         &property))
192                         continue;
193                 if (CFGetTypeID (property) != CFNumberGetTypeID ())
194                         continue;
195                 if (!CFNumberGetValue (property,
196                                         kCFNumberIntType,
197                                         &value_int))
198                         continue;
199
200                 if ((strcmp (type, "temperature") == 0)
201                                 || (strcmp (type, "fanspeed") == 0)
202                                 || (strcmp (type, "voltage") == 0))
203                 {
204                         value_double = ((double) value_int) / 65536.0;
205                 }
206                 else
207                 {
208                         value_double = (double) value_int;
209                 }
210
211                 /* Do stuff */
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);
215
216                 CFRelease (prop_dict);
217                 IOObjectRelease (io_obj);
218         } /* while (iterator) */
219
220         IOObjectRelease (iterator);
221 }
222 #else
223 # define iokit_read NULL
224 #endif /* IOKIT_HAVE_READ */
225
226 void module_register (void)
227 {
228         DBG ("IOKIT_HAVE_READ = %i", IOKIT_HAVE_READ);
229
230         plugin_register (MODULE_NAME, iokit_init, iokit_read, NULL);
231         plugin_register ("iokit-temperature", NULL, NULL, temperature_write);
232 }
233
234 #undef MODULE_NAME