Code

Replace all syslog-calls with one of the new logging-macros.
[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 #if HAVE_IOKIT_IOKITLIB_H
53 # define IOKIT_HAVE_READ 1
54 #else
55 # define IOKIT_HAVE_READ 0
56 #endif
58 #if HAVE_IOKIT_IOKITLIB_H
59 static mach_port_t io_master_port = MACH_PORT_NULL;
60 #endif
62 static data_source_t data_source_fanspeed[1] =
63 {
64         {"value", DS_TYPE_GAUGE, 0, NAN}
65 };
67 static data_set_t fanspeed_ds =
68 {
69         "fanspeed", 1, data_source_fanspeed
70 };
72 static data_source_t data_source_temperature[1] =
73 {
74         {"value", DS_TYPE_GAUGE, -273.15, NAN}
75 };
77 static data_set_t temperature_ds =
78 {
79         "temperature", 1, data_source_temperature
80 };
82 #if IOKIT_HAVE_READ
83 static int as_init (void)
84 {
85         kern_return_t status;
86         
87         if (io_master_port != MACH_PORT_NULL)
88         {
89                 mach_port_deallocate (mach_task_self (),
90                                 io_master_port);
91                 io_master_port = MACH_PORT_NULL;
92         }
94         status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
95         if (status != kIOReturnSuccess)
96         {
97                 ERROR ("IOMasterPort failed: %s",
98                                 mach_error_string (status));
99                 io_master_port = MACH_PORT_NULL;
100                 return (-1);
101         }
103         return (0);
106 static void as_submit (const char *type, const char *type_instance,
107                 double val)
109         value_t values[1];
110         value_list_t vl = VALUE_LIST_INIT;
112         DEBUG ("type = %s; type_instance = %s; val = %f;",
113                         type, type_instance, val);
115         values[0].gauge = val;
117         vl.values = values;
118         vl.values_len = 1;
119         vl.time = time (NULL);
120         strcpy (vl.host, hostname_g);
121         strcpy (vl.plugin, "apple_sensors");
122         strcpy (vl.plugin_instance, "");
123         strcpy (vl.type_instance, type_instance);
125         plugin_dispatch_values (type, &vl);
128 static int as_read (void)
130         kern_return_t   status;
131         io_iterator_t   iterator;
132         io_object_t     io_obj;
133         CFMutableDictionaryRef prop_dict;
134         CFTypeRef       property;
136         char   type[128];
137         char   inst[128];
138         int    value_int;
139         double value_double;
140         int    i;
142         if (!io_master_port || (io_master_port == MACH_PORT_NULL))
143                 return (-1);
145         status = IOServiceGetMatchingServices (io_master_port,
146                         IOServiceNameMatching("IOHWSensor"),
147                         &iterator);
148         if (status != kIOReturnSuccess)
149         {
150                 ERROR ("IOServiceGetMatchingServices failed: %s",
151                                 mach_error_string (status));
152                 return (-1);
153         }
155         while ((io_obj = IOIteratorNext (iterator)))
156         {
157                 prop_dict = NULL;
158                 status = IORegistryEntryCreateCFProperties (io_obj,
159                                 &prop_dict,
160                                 kCFAllocatorDefault,
161                                 kNilOptions);
162                 if (status != kIOReturnSuccess)
163                 {
164                         DEBUG ("IORegistryEntryCreateCFProperties failed: %s",
165                                         mach_error_string (status));
166                         continue;
167                 }
169                 /* Copy the sensor type. */
170                 property = NULL;
171                 if (!CFDictionaryGetValueIfPresent (prop_dict,
172                                         CFSTR ("type"),
173                                         &property))
174                         continue;
175                 if (CFGetTypeID (property) != CFStringGetTypeID ())
176                         continue;
177                 if (!CFStringGetCString (property,
178                                         type, 128,
179                                         kCFStringEncodingASCII))
180                         continue;
181                 type[127] = '\0';
183                 /* Copy the sensor location. This will be used as `instance'. */
184                 property = NULL;
185                 if (!CFDictionaryGetValueIfPresent (prop_dict,
186                                         CFSTR ("location"),
187                                         &property))
188                         continue;
189                 if (CFGetTypeID (property) != CFStringGetTypeID ())
190                         continue;
191                 if (!CFStringGetCString (property,
192                                         inst, 128,
193                                         kCFStringEncodingASCII))
194                         continue;
195                 inst[127] = '\0';
196                 for (i = 0; i < 128; i++)
197                 {
198                         if (inst[i] == '\0')
199                                 break;
200                         else if (isalnum (inst[i]))
201                                 inst[i] = (char) tolower (inst[i]);
202                         else
203                                 inst[i] = '_';
204                 }
206                 /* Get the actual value. Some computation, based on the `type'
207                  * is neccessary. */
208                 property = NULL;
209                 if (!CFDictionaryGetValueIfPresent (prop_dict,
210                                         CFSTR ("current-value"),
211                                         &property))
212                         continue;
213                 if (CFGetTypeID (property) != CFNumberGetTypeID ())
214                         continue;
215                 if (!CFNumberGetValue (property,
216                                         kCFNumberIntType,
217                                         &value_int))
218                         continue;
220                 /* Found e.g. in the 1.5GHz PowerBooks */
221                 if (strcmp (type, "temperature") == 0)
222                 {
223                         value_double = ((double) value_int) / 65536.0;
224                         strcpy (type, "temperature");
225                 }
226                 else if (strcmp (type, "temp") == 0)
227                 {
228                         value_double = ((double) value_int) / 10.0;
229                         strcpy (type, "temperature");
230                 }
231                 else if (strcmp (type, "fanspeed") == 0)
232                 {
233                         value_double = ((double) value_int) / 65536.0;
234                         strcpy (type, "fanspeed");
235                 }
236                 else if (strcmp (type, "voltage") == 0)
237                 {
238                         /* Leave this to the battery plugin. */
239                         continue;
240                 }
241                 else if (strcmp (type, "adc") == 0)
242                 {
243                         value_double = ((double) value_int) / 10.0;
244                         strcpy (type, "fanspeed");
245                 }
246                 else
247                 {
248                         DEBUG ("apple_sensors: Read unknown sensor type: %s",
249                                         type);
250                         value_double = (double) value_int;
251                 }
253                 as_submit (type, inst, value_double);
255                 CFRelease (prop_dict);
256                 IOObjectRelease (io_obj);
257         } /* while (iterator) */
259         IOObjectRelease (iterator);
261         return (0);
262 } /* int as_read */
263 #endif /* IOKIT_HAVE_READ */
265 void module_register (void)
267         plugin_register_data_set (&fanspeed_ds);
268         plugin_register_data_set (&temperature_ds);
270 #if IOKIT_HAVE_READ
271         plugin_register_init ("apple_sensors", as_init);
272         plugin_register_read ("apple_sensors", as_read);
273 #endif /* IOKIT_HAVE_READ */