1 /**
2 * collectd - src/apple_sensors.c
3 * Copyright (C) 2006,2007 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; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #if HAVE_CTYPE_H
27 # include <ctype.h>
28 #endif
30 #if HAVE_MACH_MACH_TYPES_H
31 # include <mach/mach_types.h>
32 #endif
33 #if HAVE_MACH_MACH_INIT_H
34 # include <mach/mach_init.h>
35 #endif
36 #if HAVE_MACH_MACH_ERROR_H
37 # include <mach/mach_error.h>
38 #endif
39 #if HAVE_MACH_MACH_PORT_H
40 # include <mach/mach_port.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
52 static mach_port_t io_master_port = MACH_PORT_NULL;
54 static int as_init (void)
55 {
56 kern_return_t status;
58 if (io_master_port != MACH_PORT_NULL)
59 {
60 mach_port_deallocate (mach_task_self (),
61 io_master_port);
62 io_master_port = MACH_PORT_NULL;
63 }
65 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
66 if (status != kIOReturnSuccess)
67 {
68 ERROR ("IOMasterPort failed: %s",
69 mach_error_string (status));
70 io_master_port = MACH_PORT_NULL;
71 return (-1);
72 }
74 return (0);
75 }
77 static void as_submit (const char *type, const char *type_instance,
78 double val)
79 {
80 value_t values[1];
81 value_list_t vl = VALUE_LIST_INIT;
83 DEBUG ("type = %s; type_instance = %s; val = %f;",
84 type, type_instance, val);
86 values[0].gauge = val;
88 vl.values = values;
89 vl.values_len = 1;
90 vl.time = time (NULL);
91 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
92 sstrncpy (vl.plugin, "apple_sensors", sizeof (vl.plugin));
93 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
94 sstrncpy (vl.type, type, sizeof (vl.type));
95 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
97 plugin_dispatch_values (&vl);
98 }
100 static int as_read (void)
101 {
102 kern_return_t status;
103 io_iterator_t iterator;
104 io_object_t io_obj;
105 CFMutableDictionaryRef prop_dict;
106 CFTypeRef property;
108 char type[128];
109 char inst[128];
110 int value_int;
111 double value_double;
112 int i;
114 if (!io_master_port || (io_master_port == MACH_PORT_NULL))
115 return (-1);
117 status = IOServiceGetMatchingServices (io_master_port,
118 IOServiceNameMatching("IOHWSensor"),
119 &iterator);
120 if (status != kIOReturnSuccess)
121 {
122 ERROR ("IOServiceGetMatchingServices failed: %s",
123 mach_error_string (status));
124 return (-1);
125 }
127 while ((io_obj = IOIteratorNext (iterator)))
128 {
129 prop_dict = NULL;
130 status = IORegistryEntryCreateCFProperties (io_obj,
131 &prop_dict,
132 kCFAllocatorDefault,
133 kNilOptions);
134 if (status != kIOReturnSuccess)
135 {
136 DEBUG ("IORegistryEntryCreateCFProperties failed: %s",
137 mach_error_string (status));
138 continue;
139 }
141 /* Copy the sensor type. */
142 property = NULL;
143 if (!CFDictionaryGetValueIfPresent (prop_dict,
144 CFSTR ("type"),
145 &property))
146 continue;
147 if (CFGetTypeID (property) != CFStringGetTypeID ())
148 continue;
149 if (!CFStringGetCString (property,
150 type, sizeof (type),
151 kCFStringEncodingASCII))
152 continue;
153 type[sizeof (type) - 1] = '\0';
155 /* Copy the sensor location. This will be used as `instance'. */
156 property = NULL;
157 if (!CFDictionaryGetValueIfPresent (prop_dict,
158 CFSTR ("location"),
159 &property))
160 continue;
161 if (CFGetTypeID (property) != CFStringGetTypeID ())
162 continue;
163 if (!CFStringGetCString (property,
164 inst, sizeof (inst),
165 kCFStringEncodingASCII))
166 continue;
167 inst[sizeof (inst) - 1] = '\0';
168 for (i = 0; i < 128; i++)
169 {
170 if (inst[i] == '\0')
171 break;
172 else if (isalnum (inst[i]))
173 inst[i] = (char) tolower (inst[i]);
174 else
175 inst[i] = '_';
176 }
178 /* Get the actual value. Some computation, based on the `type'
179 * is neccessary. */
180 property = NULL;
181 if (!CFDictionaryGetValueIfPresent (prop_dict,
182 CFSTR ("current-value"),
183 &property))
184 continue;
185 if (CFGetTypeID (property) != CFNumberGetTypeID ())
186 continue;
187 if (!CFNumberGetValue (property,
188 kCFNumberIntType,
189 &value_int))
190 continue;
192 /* Found e.g. in the 1.5GHz PowerBooks */
193 if (strcmp (type, "temperature") == 0)
194 {
195 value_double = ((double) value_int) / 65536.0;
196 sstrncpy (type, "temperature", sizeof (type));
197 }
198 else if (strcmp (type, "temp") == 0)
199 {
200 value_double = ((double) value_int) / 10.0;
201 sstrncpy (type, "temperature", sizeof (type));
202 }
203 else if (strcmp (type, "fanspeed") == 0)
204 {
205 value_double = ((double) value_int) / 65536.0;
206 sstrncpy (type, "fanspeed", sizeof (type));
207 }
208 else if (strcmp (type, "voltage") == 0)
209 {
210 /* Leave this to the battery plugin. */
211 continue;
212 }
213 else if (strcmp (type, "adc") == 0)
214 {
215 value_double = ((double) value_int) / 10.0;
216 sstrncpy (type, "fanspeed", sizeof (type));
217 }
218 else
219 {
220 DEBUG ("apple_sensors: Read unknown sensor type: %s",
221 type);
222 value_double = (double) value_int;
223 }
225 as_submit (type, inst, value_double);
227 CFRelease (prop_dict);
228 IOObjectRelease (io_obj);
229 } /* while (iterator) */
231 IOObjectRelease (iterator);
233 return (0);
234 } /* int as_read */
236 void module_register (void)
237 {
238 plugin_register_init ("apple_sensors", as_init);
239 plugin_register_read ("apple_sensors", as_read);
240 } /* void module_register */