17c822ff3c91ca9f44a52188ac2d9ecb5faebd18
1 /**
2 * collectd - src/apple_sensors.c
3 * Copyright (C) 2006,2007 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
32 #if HAVE_CTYPE_H
33 # include <ctype.h>
34 #endif
36 #if HAVE_MACH_MACH_TYPES_H
37 # include <mach/mach_types.h>
38 #endif
39 #if HAVE_MACH_MACH_INIT_H
40 # include <mach/mach_init.h>
41 #endif
42 #if HAVE_MACH_MACH_ERROR_H
43 # include <mach/mach_error.h>
44 #endif
45 #if HAVE_MACH_MACH_PORT_H
46 # include <mach/mach_port.h>
47 #endif
48 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
49 # include <CoreFoundation/CoreFoundation.h>
50 #endif
51 #if HAVE_IOKIT_IOKITLIB_H
52 # include <IOKit/IOKitLib.h>
53 #endif
54 #if HAVE_IOKIT_IOTYPES_H
55 # include <IOKit/IOTypes.h>
56 #endif
58 static mach_port_t io_master_port = MACH_PORT_NULL;
60 static int as_init (void)
61 {
62 kern_return_t status;
64 if (io_master_port != MACH_PORT_NULL)
65 {
66 mach_port_deallocate (mach_task_self (),
67 io_master_port);
68 io_master_port = MACH_PORT_NULL;
69 }
71 status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
72 if (status != kIOReturnSuccess)
73 {
74 ERROR ("IOMasterPort failed: %s",
75 mach_error_string (status));
76 io_master_port = MACH_PORT_NULL;
77 return (-1);
78 }
80 return (0);
81 }
83 static void as_submit (const char *type, const char *type_instance,
84 double val)
85 {
86 value_list_t vl = VALUE_LIST_INIT;
88 vl.values = &(value_t) { .gauge = val };
89 vl.values_len = 1;
90 sstrncpy (vl.plugin, "apple_sensors", sizeof (vl.plugin));
91 sstrncpy (vl.type, type, sizeof (vl.type));
92 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
94 plugin_dispatch_values (&vl);
95 }
97 static int as_read (void)
98 {
99 kern_return_t status;
100 io_iterator_t iterator;
101 io_object_t io_obj;
102 CFMutableDictionaryRef prop_dict;
103 CFTypeRef property;
105 char type[128];
106 char inst[128];
107 int value_int;
108 double value_double;
109 if (!io_master_port || (io_master_port == MACH_PORT_NULL))
110 return (-1);
112 status = IOServiceGetMatchingServices (io_master_port,
113 IOServiceNameMatching("IOHWSensor"),
114 &iterator);
115 if (status != kIOReturnSuccess)
116 {
117 ERROR ("IOServiceGetMatchingServices failed: %s",
118 mach_error_string (status));
119 return (-1);
120 }
122 while ((io_obj = IOIteratorNext (iterator)))
123 {
124 prop_dict = NULL;
125 status = IORegistryEntryCreateCFProperties (io_obj,
126 &prop_dict,
127 kCFAllocatorDefault,
128 kNilOptions);
129 if (status != kIOReturnSuccess)
130 {
131 DEBUG ("IORegistryEntryCreateCFProperties failed: %s",
132 mach_error_string (status));
133 continue;
134 }
136 /* Copy the sensor type. */
137 property = NULL;
138 if (!CFDictionaryGetValueIfPresent (prop_dict,
139 CFSTR ("type"),
140 &property))
141 continue;
142 if (CFGetTypeID (property) != CFStringGetTypeID ())
143 continue;
144 if (!CFStringGetCString (property,
145 type, sizeof (type),
146 kCFStringEncodingASCII))
147 continue;
148 type[sizeof (type) - 1] = '\0';
150 /* Copy the sensor location. This will be used as `instance'. */
151 property = NULL;
152 if (!CFDictionaryGetValueIfPresent (prop_dict,
153 CFSTR ("location"),
154 &property))
155 continue;
156 if (CFGetTypeID (property) != CFStringGetTypeID ())
157 continue;
158 if (!CFStringGetCString (property,
159 inst, sizeof (inst),
160 kCFStringEncodingASCII))
161 continue;
162 inst[sizeof (inst) - 1] = '\0';
163 for (int i = 0; i < 128; i++)
164 {
165 if (inst[i] == '\0')
166 break;
167 else if (isalnum (inst[i]))
168 inst[i] = (char) tolower (inst[i]);
169 else
170 inst[i] = '_';
171 }
173 /* Get the actual value. Some computation, based on the `type'
174 * is neccessary. */
175 property = NULL;
176 if (!CFDictionaryGetValueIfPresent (prop_dict,
177 CFSTR ("current-value"),
178 &property))
179 continue;
180 if (CFGetTypeID (property) != CFNumberGetTypeID ())
181 continue;
182 if (!CFNumberGetValue (property,
183 kCFNumberIntType,
184 &value_int))
185 continue;
187 /* Found e.g. in the 1.5GHz PowerBooks */
188 if (strcmp (type, "temperature") == 0)
189 {
190 value_double = ((double) value_int) / 65536.0;
191 sstrncpy (type, "temperature", sizeof (type));
192 }
193 else if (strcmp (type, "temp") == 0)
194 {
195 value_double = ((double) value_int) / 10.0;
196 sstrncpy (type, "temperature", sizeof (type));
197 }
198 else if (strcmp (type, "fanspeed") == 0)
199 {
200 value_double = ((double) value_int) / 65536.0;
201 sstrncpy (type, "fanspeed", sizeof (type));
202 }
203 else if (strcmp (type, "voltage") == 0)
204 {
205 /* Leave this to the battery plugin. */
206 continue;
207 }
208 else if (strcmp (type, "adc") == 0)
209 {
210 value_double = ((double) value_int) / 10.0;
211 sstrncpy (type, "fanspeed", sizeof (type));
212 }
213 else
214 {
215 DEBUG ("apple_sensors: Read unknown sensor type: %s",
216 type);
217 value_double = (double) value_int;
218 }
220 as_submit (type, inst, value_double);
222 CFRelease (prop_dict);
223 IOObjectRelease (io_obj);
224 } /* while (iterator) */
226 IOObjectRelease (iterator);
228 return (0);
229 } /* int as_read */
231 void module_register (void)
232 {
233 plugin_register_init ("apple_sensors", as_init);
234 plugin_register_read ("apple_sensors", as_read);
235 } /* void module_register */