Merge branch 'collectd-5.7' into collectd-5.8
[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_MACH_MACH_TYPES_H
33 #include <mach/mach_types.h>
34 #endif
35 #if HAVE_MACH_MACH_INIT_H
36 #include <mach/mach_init.h>
37 #endif
38 #if HAVE_MACH_MACH_ERROR_H
39 #include <mach/mach_error.h>
40 #endif
41 #if HAVE_MACH_MACH_PORT_H
42 #include <mach/mach_port.h>
43 #endif
44 #if HAVE_COREFOUNDATION_COREFOUNDATION_H
45 #include <CoreFoundation/CoreFoundation.h>
46 #endif
47 #if HAVE_IOKIT_IOKITLIB_H
48 #include <IOKit/IOKitLib.h>
49 #endif
50 #if HAVE_IOKIT_IOTYPES_H
51 #include <IOKit/IOTypes.h>
52 #endif
53
54 static mach_port_t io_master_port = MACH_PORT_NULL;
55
56 static int as_init(void) {
57   kern_return_t status;
58
59   if (io_master_port != MACH_PORT_NULL) {
60     mach_port_deallocate(mach_task_self(), io_master_port);
61     io_master_port = MACH_PORT_NULL;
62   }
63
64   status = IOMasterPort(MACH_PORT_NULL, &io_master_port);
65   if (status != kIOReturnSuccess) {
66     ERROR("IOMasterPort failed: %s", mach_error_string(status));
67     io_master_port = MACH_PORT_NULL;
68     return -1;
69   }
70
71   return 0;
72 }
73
74 static void as_submit(const char *type, const char *type_instance, double val) {
75   value_list_t vl = VALUE_LIST_INIT;
76
77   vl.values = &(value_t){.gauge = val};
78   vl.values_len = 1;
79   sstrncpy(vl.plugin, "apple_sensors", sizeof(vl.plugin));
80   sstrncpy(vl.type, type, sizeof(vl.type));
81   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
82
83   plugin_dispatch_values(&vl);
84 }
85
86 static int as_read(void) {
87   kern_return_t status;
88   io_iterator_t iterator;
89   io_object_t io_obj;
90   CFMutableDictionaryRef prop_dict;
91   CFTypeRef property;
92
93   char type[128];
94   char inst[128];
95   int value_int;
96   double value_double;
97   if (!io_master_port || (io_master_port == MACH_PORT_NULL))
98     return -1;
99
100   status = IOServiceGetMatchingServices(
101       io_master_port, IOServiceNameMatching("IOHWSensor"), &iterator);
102   if (status != kIOReturnSuccess) {
103     ERROR("IOServiceGetMatchingServices failed: %s", mach_error_string(status));
104     return -1;
105   }
106
107   while ((io_obj = IOIteratorNext(iterator))) {
108     prop_dict = NULL;
109     status = IORegistryEntryCreateCFProperties(
110         io_obj, &prop_dict, kCFAllocatorDefault, kNilOptions);
111     if (status != kIOReturnSuccess) {
112       DEBUG("IORegistryEntryCreateCFProperties failed: %s",
113             mach_error_string(status));
114       continue;
115     }
116
117     /* Copy the sensor type. */
118     property = NULL;
119     if (!CFDictionaryGetValueIfPresent(prop_dict, CFSTR("type"), &property))
120       continue;
121     if (CFGetTypeID(property) != CFStringGetTypeID())
122       continue;
123     if (!CFStringGetCString(property, type, sizeof(type),
124                             kCFStringEncodingASCII))
125       continue;
126     type[sizeof(type) - 1] = '\0';
127
128     /* Copy the sensor location. This will be used as `instance'. */
129     property = NULL;
130     if (!CFDictionaryGetValueIfPresent(prop_dict, CFSTR("location"), &property))
131       continue;
132     if (CFGetTypeID(property) != CFStringGetTypeID())
133       continue;
134     if (!CFStringGetCString(property, inst, sizeof(inst),
135                             kCFStringEncodingASCII))
136       continue;
137     inst[sizeof(inst) - 1] = '\0';
138     for (int i = 0; i < 128; i++) {
139       if (inst[i] == '\0')
140         break;
141       else if (isalnum(inst[i]))
142         inst[i] = (char)tolower(inst[i]);
143       else
144         inst[i] = '_';
145     }
146
147     /* Get the actual value. Some computation, based on the `type'
148      * is neccessary. */
149     property = NULL;
150     if (!CFDictionaryGetValueIfPresent(prop_dict, CFSTR("current-value"),
151                                        &property))
152       continue;
153     if (CFGetTypeID(property) != CFNumberGetTypeID())
154       continue;
155     if (!CFNumberGetValue(property, kCFNumberIntType, &value_int))
156       continue;
157
158     /* Found e.g. in the 1.5GHz PowerBooks */
159     if (strcmp(type, "temperature") == 0) {
160       value_double = ((double)value_int) / 65536.0;
161       sstrncpy(type, "temperature", sizeof(type));
162     } else if (strcmp(type, "temp") == 0) {
163       value_double = ((double)value_int) / 10.0;
164       sstrncpy(type, "temperature", sizeof(type));
165     } else if (strcmp(type, "fanspeed") == 0) {
166       value_double = ((double)value_int) / 65536.0;
167       sstrncpy(type, "fanspeed", sizeof(type));
168     } else if (strcmp(type, "voltage") == 0) {
169       /* Leave this to the battery plugin. */
170       continue;
171     } else if (strcmp(type, "adc") == 0) {
172       value_double = ((double)value_int) / 10.0;
173       sstrncpy(type, "fanspeed", sizeof(type));
174     } else {
175       DEBUG("apple_sensors: Read unknown sensor type: %s", type);
176       value_double = (double)value_int;
177     }
178
179     as_submit(type, inst, value_double);
180
181     CFRelease(prop_dict);
182     IOObjectRelease(io_obj);
183   } /* while (iterator) */
184
185   IOObjectRelease(iterator);
186
187   return 0;
188 } /* int as_read */
189
190 void module_register(void) {
191   plugin_register_init("apple_sensors", as_init);
192   plugin_register_read("apple_sensors", as_read);
193 } /* void module_register */