Code

Bump version to 5.0.5; Update ChangeLog.
[collectd.git] / src / apple_sensors.c
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;
57         
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         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
91         sstrncpy (vl.plugin, "apple_sensors", sizeof (vl.plugin));
92         sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
93         sstrncpy (vl.type, type, sizeof (vl.type));
94         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
96         plugin_dispatch_values (&vl);
97 }
99 static int as_read (void)
101         kern_return_t   status;
102         io_iterator_t   iterator;
103         io_object_t     io_obj;
104         CFMutableDictionaryRef prop_dict;
105         CFTypeRef       property;
107         char   type[128];
108         char   inst[128];
109         int    value_int;
110         double value_double;
111         int    i;
113         if (!io_master_port || (io_master_port == MACH_PORT_NULL))
114                 return (-1);
116         status = IOServiceGetMatchingServices (io_master_port,
117                         IOServiceNameMatching("IOHWSensor"),
118                         &iterator);
119         if (status != kIOReturnSuccess)
120         {
121                 ERROR ("IOServiceGetMatchingServices failed: %s",
122                                 mach_error_string (status));
123                 return (-1);
124         }
126         while ((io_obj = IOIteratorNext (iterator)))
127         {
128                 prop_dict = NULL;
129                 status = IORegistryEntryCreateCFProperties (io_obj,
130                                 &prop_dict,
131                                 kCFAllocatorDefault,
132                                 kNilOptions);
133                 if (status != kIOReturnSuccess)
134                 {
135                         DEBUG ("IORegistryEntryCreateCFProperties failed: %s",
136                                         mach_error_string (status));
137                         continue;
138                 }
140                 /* Copy the sensor type. */
141                 property = NULL;
142                 if (!CFDictionaryGetValueIfPresent (prop_dict,
143                                         CFSTR ("type"),
144                                         &property))
145                         continue;
146                 if (CFGetTypeID (property) != CFStringGetTypeID ())
147                         continue;
148                 if (!CFStringGetCString (property,
149                                         type, sizeof (type),
150                                         kCFStringEncodingASCII))
151                         continue;
152                 type[sizeof (type) - 1] = '\0';
154                 /* Copy the sensor location. This will be used as `instance'. */
155                 property = NULL;
156                 if (!CFDictionaryGetValueIfPresent (prop_dict,
157                                         CFSTR ("location"),
158                                         &property))
159                         continue;
160                 if (CFGetTypeID (property) != CFStringGetTypeID ())
161                         continue;
162                 if (!CFStringGetCString (property,
163                                         inst, sizeof (inst),
164                                         kCFStringEncodingASCII))
165                         continue;
166                 inst[sizeof (inst) - 1] = '\0';
167                 for (i = 0; i < 128; i++)
168                 {
169                         if (inst[i] == '\0')
170                                 break;
171                         else if (isalnum (inst[i]))
172                                 inst[i] = (char) tolower (inst[i]);
173                         else
174                                 inst[i] = '_';
175                 }
177                 /* Get the actual value. Some computation, based on the `type'
178                  * is neccessary. */
179                 property = NULL;
180                 if (!CFDictionaryGetValueIfPresent (prop_dict,
181                                         CFSTR ("current-value"),
182                                         &property))
183                         continue;
184                 if (CFGetTypeID (property) != CFNumberGetTypeID ())
185                         continue;
186                 if (!CFNumberGetValue (property,
187                                         kCFNumberIntType,
188                                         &value_int))
189                         continue;
191                 /* Found e.g. in the 1.5GHz PowerBooks */
192                 if (strcmp (type, "temperature") == 0)
193                 {
194                         value_double = ((double) value_int) / 65536.0;
195                         sstrncpy (type, "temperature", sizeof (type));
196                 }
197                 else if (strcmp (type, "temp") == 0)
198                 {
199                         value_double = ((double) value_int) / 10.0;
200                         sstrncpy (type, "temperature", sizeof (type));
201                 }
202                 else if (strcmp (type, "fanspeed") == 0)
203                 {
204                         value_double = ((double) value_int) / 65536.0;
205                         sstrncpy (type, "fanspeed", sizeof (type));
206                 }
207                 else if (strcmp (type, "voltage") == 0)
208                 {
209                         /* Leave this to the battery plugin. */
210                         continue;
211                 }
212                 else if (strcmp (type, "adc") == 0)
213                 {
214                         value_double = ((double) value_int) / 10.0;
215                         sstrncpy (type, "fanspeed", sizeof (type));
216                 }
217                 else
218                 {
219                         DEBUG ("apple_sensors: Read unknown sensor type: %s",
220                                         type);
221                         value_double = (double) value_int;
222                 }
224                 as_submit (type, inst, value_double);
226                 CFRelease (prop_dict);
227                 IOObjectRelease (io_obj);
228         } /* while (iterator) */
230         IOObjectRelease (iterator);
232         return (0);
233 } /* int as_read */
235 void module_register (void)
237         plugin_register_init ("apple_sensors", as_init);
238         plugin_register_read ("apple_sensors", as_read);
239 } /* void module_register */