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