Tree wide: Reformat with clang-format.
[collectd.git] / src / apple_sensors.c
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  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 #if HAVE_CTYPE_H
33 #include <ctype.h>
34 #endif
35
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
57
58 static mach_port_t io_master_port = MACH_PORT_NULL;
59
60 static int as_init(void) {
61   kern_return_t status;
62
63   if (io_master_port != MACH_PORT_NULL) {
64     mach_port_deallocate(mach_task_self(), io_master_port);
65     io_master_port = MACH_PORT_NULL;
66   }
67
68   status = IOMasterPort(MACH_PORT_NULL, &io_master_port);
69   if (status != kIOReturnSuccess) {
70     ERROR("IOMasterPort failed: %s", mach_error_string(status));
71     io_master_port = MACH_PORT_NULL;
72     return (-1);
73   }
74
75   return (0);
76 }
77
78 static void as_submit(const char *type, const char *type_instance, double val) {
79   value_t values[1];
80   value_list_t vl = VALUE_LIST_INIT;
81
82   DEBUG("type = %s; type_instance = %s; val = %f;", type, type_instance, val);
83
84   values[0].gauge = val;
85
86   vl.values = values;
87   vl.values_len = 1;
88   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
89   sstrncpy(vl.plugin, "apple_sensors", sizeof(vl.plugin));
90   sstrncpy(vl.plugin_instance, "", sizeof(vl.plugin_instance));
91   sstrncpy(vl.type, type, sizeof(vl.type));
92   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
93
94   plugin_dispatch_values(&vl);
95 }
96
97 static int as_read(void) {
98   kern_return_t status;
99   io_iterator_t iterator;
100   io_object_t io_obj;
101   CFMutableDictionaryRef prop_dict;
102   CFTypeRef property;
103
104   char type[128];
105   char inst[128];
106   int value_int;
107   double value_double;
108   if (!io_master_port || (io_master_port == MACH_PORT_NULL))
109     return (-1);
110
111   status = IOServiceGetMatchingServices(
112       io_master_port, IOServiceNameMatching("IOHWSensor"), &iterator);
113   if (status != kIOReturnSuccess) {
114     ERROR("IOServiceGetMatchingServices failed: %s", mach_error_string(status));
115     return (-1);
116   }
117
118   while ((io_obj = IOIteratorNext(iterator))) {
119     prop_dict = NULL;
120     status = IORegistryEntryCreateCFProperties(
121         io_obj, &prop_dict, kCFAllocatorDefault, kNilOptions);
122     if (status != kIOReturnSuccess) {
123       DEBUG("IORegistryEntryCreateCFProperties failed: %s",
124             mach_error_string(status));
125       continue;
126     }
127
128     /* Copy the sensor type. */
129     property = NULL;
130     if (!CFDictionaryGetValueIfPresent(prop_dict, CFSTR("type"), &property))
131       continue;
132     if (CFGetTypeID(property) != CFStringGetTypeID())
133       continue;
134     if (!CFStringGetCString(property, type, sizeof(type),
135                             kCFStringEncodingASCII))
136       continue;
137     type[sizeof(type) - 1] = '\0';
138
139     /* Copy the sensor location. This will be used as `instance'. */
140     property = NULL;
141     if (!CFDictionaryGetValueIfPresent(prop_dict, CFSTR("location"), &property))
142       continue;
143     if (CFGetTypeID(property) != CFStringGetTypeID())
144       continue;
145     if (!CFStringGetCString(property, inst, sizeof(inst),
146                             kCFStringEncodingASCII))
147       continue;
148     inst[sizeof(inst) - 1] = '\0';
149     for (int i = 0; i < 128; i++) {
150       if (inst[i] == '\0')
151         break;
152       else if (isalnum(inst[i]))
153         inst[i] = (char)tolower(inst[i]);
154       else
155         inst[i] = '_';
156     }
157
158     /* Get the actual value. Some computation, based on the `type'
159      * is neccessary. */
160     property = NULL;
161     if (!CFDictionaryGetValueIfPresent(prop_dict, CFSTR("current-value"),
162                                        &property))
163       continue;
164     if (CFGetTypeID(property) != CFNumberGetTypeID())
165       continue;
166     if (!CFNumberGetValue(property, kCFNumberIntType, &value_int))
167       continue;
168
169     /* Found e.g. in the 1.5GHz PowerBooks */
170     if (strcmp(type, "temperature") == 0) {
171       value_double = ((double)value_int) / 65536.0;
172       sstrncpy(type, "temperature", sizeof(type));
173     } else if (strcmp(type, "temp") == 0) {
174       value_double = ((double)value_int) / 10.0;
175       sstrncpy(type, "temperature", sizeof(type));
176     } else if (strcmp(type, "fanspeed") == 0) {
177       value_double = ((double)value_int) / 65536.0;
178       sstrncpy(type, "fanspeed", sizeof(type));
179     } else if (strcmp(type, "voltage") == 0) {
180       /* Leave this to the battery plugin. */
181       continue;
182     } else if (strcmp(type, "adc") == 0) {
183       value_double = ((double)value_int) / 10.0;
184       sstrncpy(type, "fanspeed", sizeof(type));
185     } else {
186       DEBUG("apple_sensors: Read unknown sensor type: %s", type);
187       value_double = (double)value_int;
188     }
189
190     as_submit(type, inst, value_double);
191
192     CFRelease(prop_dict);
193     IOObjectRelease(io_obj);
194   } /* while (iterator) */
195
196   IOObjectRelease(iterator);
197
198   return (0);
199 } /* int as_read */
200
201 void module_register(void) {
202   plugin_register_init("apple_sensors", as_init);
203   plugin_register_read("apple_sensors", as_read);
204 } /* void module_register */