X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fapple_sensors.c;h=f78c3da2025413e576d6d6e51ac0ef077d94e576;hp=17c822ff3c91ca9f44a52188ac2d9ecb5faebd18;hb=1159cb5d383c55a80a0db100b8f7aadcf44740a5;hpb=41288c6a9ed050b41ad47184aa1b53668c3588cc diff --git a/src/apple_sensors.c b/src/apple_sensors.c index 17c822ff..f78c3da2 100644 --- a/src/apple_sensors.c +++ b/src/apple_sensors.c @@ -29,207 +29,165 @@ #include "common.h" #include "plugin.h" -#if HAVE_CTYPE_H -# include -#endif - #if HAVE_MACH_MACH_TYPES_H -# include +#include #endif #if HAVE_MACH_MACH_INIT_H -# include +#include #endif #if HAVE_MACH_MACH_ERROR_H -# include +#include #endif #if HAVE_MACH_MACH_PORT_H -# include +#include #endif #if HAVE_COREFOUNDATION_COREFOUNDATION_H -# include +#include #endif #if HAVE_IOKIT_IOKITLIB_H -# include +#include #endif #if HAVE_IOKIT_IOTYPES_H -# include +#include #endif static mach_port_t io_master_port = MACH_PORT_NULL; -static int as_init (void) -{ - kern_return_t status; - - if (io_master_port != MACH_PORT_NULL) - { - mach_port_deallocate (mach_task_self (), - io_master_port); - io_master_port = MACH_PORT_NULL; - } - - status = IOMasterPort (MACH_PORT_NULL, &io_master_port); - if (status != kIOReturnSuccess) - { - ERROR ("IOMasterPort failed: %s", - mach_error_string (status)); - io_master_port = MACH_PORT_NULL; - return (-1); - } - - return (0); +static int as_init(void) { + kern_return_t status; + + if (io_master_port != MACH_PORT_NULL) { + mach_port_deallocate(mach_task_self(), io_master_port); + io_master_port = MACH_PORT_NULL; + } + + status = IOMasterPort(MACH_PORT_NULL, &io_master_port); + if (status != kIOReturnSuccess) { + ERROR("IOMasterPort failed: %s", mach_error_string(status)); + io_master_port = MACH_PORT_NULL; + return -1; + } + + return 0; } -static void as_submit (const char *type, const char *type_instance, - double val) -{ - value_list_t vl = VALUE_LIST_INIT; +static void as_submit(const char *type, const char *type_instance, double val) { + value_list_t vl = VALUE_LIST_INIT; - vl.values = &(value_t) { .gauge = val }; - vl.values_len = 1; - sstrncpy (vl.plugin, "apple_sensors", sizeof (vl.plugin)); - sstrncpy (vl.type, type, sizeof (vl.type)); - sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); + vl.values = &(value_t){.gauge = val}; + vl.values_len = 1; + sstrncpy(vl.plugin, "apple_sensors", sizeof(vl.plugin)); + sstrncpy(vl.type, type, sizeof(vl.type)); + sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance)); - plugin_dispatch_values (&vl); + plugin_dispatch_values(&vl); } -static int as_read (void) -{ - kern_return_t status; - io_iterator_t iterator; - io_object_t io_obj; - CFMutableDictionaryRef prop_dict; - CFTypeRef property; - - char type[128]; - char inst[128]; - int value_int; - double value_double; - if (!io_master_port || (io_master_port == MACH_PORT_NULL)) - return (-1); - - status = IOServiceGetMatchingServices (io_master_port, - IOServiceNameMatching("IOHWSensor"), - &iterator); - if (status != kIOReturnSuccess) - { - ERROR ("IOServiceGetMatchingServices failed: %s", - mach_error_string (status)); - return (-1); - } - - while ((io_obj = IOIteratorNext (iterator))) - { - prop_dict = NULL; - status = IORegistryEntryCreateCFProperties (io_obj, - &prop_dict, - kCFAllocatorDefault, - kNilOptions); - if (status != kIOReturnSuccess) - { - DEBUG ("IORegistryEntryCreateCFProperties failed: %s", - mach_error_string (status)); - continue; - } - - /* Copy the sensor type. */ - property = NULL; - if (!CFDictionaryGetValueIfPresent (prop_dict, - CFSTR ("type"), - &property)) - continue; - if (CFGetTypeID (property) != CFStringGetTypeID ()) - continue; - if (!CFStringGetCString (property, - type, sizeof (type), - kCFStringEncodingASCII)) - continue; - type[sizeof (type) - 1] = '\0'; - - /* Copy the sensor location. This will be used as `instance'. */ - property = NULL; - if (!CFDictionaryGetValueIfPresent (prop_dict, - CFSTR ("location"), - &property)) - continue; - if (CFGetTypeID (property) != CFStringGetTypeID ()) - continue; - if (!CFStringGetCString (property, - inst, sizeof (inst), - kCFStringEncodingASCII)) - continue; - inst[sizeof (inst) - 1] = '\0'; - for (int i = 0; i < 128; i++) - { - if (inst[i] == '\0') - break; - else if (isalnum (inst[i])) - inst[i] = (char) tolower (inst[i]); - else - inst[i] = '_'; - } - - /* Get the actual value. Some computation, based on the `type' - * is neccessary. */ - property = NULL; - if (!CFDictionaryGetValueIfPresent (prop_dict, - CFSTR ("current-value"), - &property)) - continue; - if (CFGetTypeID (property) != CFNumberGetTypeID ()) - continue; - if (!CFNumberGetValue (property, - kCFNumberIntType, - &value_int)) - continue; - - /* Found e.g. in the 1.5GHz PowerBooks */ - if (strcmp (type, "temperature") == 0) - { - value_double = ((double) value_int) / 65536.0; - sstrncpy (type, "temperature", sizeof (type)); - } - else if (strcmp (type, "temp") == 0) - { - value_double = ((double) value_int) / 10.0; - sstrncpy (type, "temperature", sizeof (type)); - } - else if (strcmp (type, "fanspeed") == 0) - { - value_double = ((double) value_int) / 65536.0; - sstrncpy (type, "fanspeed", sizeof (type)); - } - else if (strcmp (type, "voltage") == 0) - { - /* Leave this to the battery plugin. */ - continue; - } - else if (strcmp (type, "adc") == 0) - { - value_double = ((double) value_int) / 10.0; - sstrncpy (type, "fanspeed", sizeof (type)); - } - else - { - DEBUG ("apple_sensors: Read unknown sensor type: %s", - type); - value_double = (double) value_int; - } - - as_submit (type, inst, value_double); - - CFRelease (prop_dict); - IOObjectRelease (io_obj); - } /* while (iterator) */ - - IOObjectRelease (iterator); - - return (0); +static int as_read(void) { + kern_return_t status; + io_iterator_t iterator; + io_object_t io_obj; + CFMutableDictionaryRef prop_dict; + CFTypeRef property; + + char type[128]; + char inst[128]; + int value_int; + double value_double; + if (!io_master_port || (io_master_port == MACH_PORT_NULL)) + return -1; + + status = IOServiceGetMatchingServices( + io_master_port, IOServiceNameMatching("IOHWSensor"), &iterator); + if (status != kIOReturnSuccess) { + ERROR("IOServiceGetMatchingServices failed: %s", mach_error_string(status)); + return -1; + } + + while ((io_obj = IOIteratorNext(iterator))) { + prop_dict = NULL; + status = IORegistryEntryCreateCFProperties( + io_obj, &prop_dict, kCFAllocatorDefault, kNilOptions); + if (status != kIOReturnSuccess) { + DEBUG("IORegistryEntryCreateCFProperties failed: %s", + mach_error_string(status)); + continue; + } + + /* Copy the sensor type. */ + property = NULL; + if (!CFDictionaryGetValueIfPresent(prop_dict, CFSTR("type"), &property)) + continue; + if (CFGetTypeID(property) != CFStringGetTypeID()) + continue; + if (!CFStringGetCString(property, type, sizeof(type), + kCFStringEncodingASCII)) + continue; + type[sizeof(type) - 1] = '\0'; + + /* Copy the sensor location. This will be used as `instance'. */ + property = NULL; + if (!CFDictionaryGetValueIfPresent(prop_dict, CFSTR("location"), &property)) + continue; + if (CFGetTypeID(property) != CFStringGetTypeID()) + continue; + if (!CFStringGetCString(property, inst, sizeof(inst), + kCFStringEncodingASCII)) + continue; + inst[sizeof(inst) - 1] = '\0'; + for (int i = 0; i < 128; i++) { + if (inst[i] == '\0') + break; + else if (isalnum(inst[i])) + inst[i] = (char)tolower(inst[i]); + else + inst[i] = '_'; + } + + /* Get the actual value. Some computation, based on the `type' + * is neccessary. */ + property = NULL; + if (!CFDictionaryGetValueIfPresent(prop_dict, CFSTR("current-value"), + &property)) + continue; + if (CFGetTypeID(property) != CFNumberGetTypeID()) + continue; + if (!CFNumberGetValue(property, kCFNumberIntType, &value_int)) + continue; + + /* Found e.g. in the 1.5GHz PowerBooks */ + if (strcmp(type, "temperature") == 0) { + value_double = ((double)value_int) / 65536.0; + sstrncpy(type, "temperature", sizeof(type)); + } else if (strcmp(type, "temp") == 0) { + value_double = ((double)value_int) / 10.0; + sstrncpy(type, "temperature", sizeof(type)); + } else if (strcmp(type, "fanspeed") == 0) { + value_double = ((double)value_int) / 65536.0; + sstrncpy(type, "fanspeed", sizeof(type)); + } else if (strcmp(type, "voltage") == 0) { + /* Leave this to the battery plugin. */ + continue; + } else if (strcmp(type, "adc") == 0) { + value_double = ((double)value_int) / 10.0; + sstrncpy(type, "fanspeed", sizeof(type)); + } else { + DEBUG("apple_sensors: Read unknown sensor type: %s", type); + value_double = (double)value_int; + } + + as_submit(type, inst, value_double); + + CFRelease(prop_dict); + IOObjectRelease(io_obj); + } /* while (iterator) */ + + IOObjectRelease(iterator); + + return 0; } /* int as_read */ -void module_register (void) -{ - plugin_register_init ("apple_sensors", as_init); - plugin_register_read ("apple_sensors", as_read); +void module_register(void) { + plugin_register_init("apple_sensors", as_init); + plugin_register_read("apple_sensors", as_read); } /* void module_register */