Renamed `iokit' to `apple_sensors'
authorocto <octo>
Tue, 11 Apr 2006 19:03:13 +0000 (19:03 +0000)
committerocto <octo>
Tue, 11 Apr 2006 19:03:13 +0000 (19:03 +0000)
configure.in
src/Makefile.am
src/apple_sensors.c [new file with mode: 0644]
src/iokit.c [deleted file]

index 5b62025..8fd2b48 100644 (file)
@@ -157,7 +157,7 @@ AC_CHECK_HEADERS(mach/kern_return.h)
 # For hddtemp module
 AC_CHECK_HEADERS(linux/major.h)
 
-# For the iokit module
+# For the apple_sensors module
 AC_CHECK_HEADERS(CoreFoundation/CoreFoundation.h)
 AC_CHECK_HEADERS(IOKit/IOKitLib.h)
 AC_CHECK_HEADERS(IOKit/IOTypes.h)
@@ -789,6 +789,7 @@ AC_COLLECTD([daemon],    [disable], [feature], [daemon mode])
 m4_divert_once([HELP_ENABLE], [
 collectd modules:])
 AC_COLLECTD([apache],    [disable], [module], [Apache httpd statistics])
+AC_COLLECTD([apple_sensors], [disable], [module], [Apple's hardware sensors])
 AC_COLLECTD([battery],   [disable], [module], [battery statistics])
 AC_COLLECTD([cpu],       [disable], [module], [cpu usage statistics])
 AC_COLLECTD([cpufreq],   [disable], [module], [system cpu frequency statistics])
@@ -796,7 +797,6 @@ AC_COLLECTD([disk],      [disable], [module], [disk/partition statistics])
 AC_COLLECTD([df],        [disable], [module], [df statistics])
 AC_COLLECTD([quota],     [enable],  [module], [quota statistics (experimental)])
 AC_COLLECTD([hddtemp],   [disable], [module], [hdd temperature statistics])
-AC_COLLECTD([iokit],     [disable], [module], [Apple's iokit hardware sensors])
 AC_COLLECTD([load],      [disable], [module], [system load statistics])
 AC_COLLECTD([memory],    [disable], [module], [memory statistics])
 AC_COLLECTD([mysql],     [disable], [module], [mysql statistics])
index c13e950..dc47b88 100644 (file)
@@ -55,6 +55,17 @@ collectd_LDADD += "-dlopen" apache.la
 collectd_DEPENDENCIES += apache.la
 endif
 
+if BUILD_MODULE_APPLE_SENSORS
+pkglib_LTLIBRARIES += apple_sensors.la
+apple_sensors_la_SOURCES = apple_sensors.c
+apple_sensors_la_LDFLAGS = -module -avoid-version
+if BUILD_WITH_LIBIOKIT
+apple_sensors_la_LDFLAGS += -lIOKit
+endif
+collectd_LDADD += "-dlopen" apple_sensors.la
+collectd_DEPENDENCIES += apple_sensors.la
+endif
+
 if BUILD_MODULE_BATTERY
 pkglib_LTLIBRARIES += battery.la
 battery_la_SOURCES = battery.c
@@ -114,17 +125,6 @@ collectd_LDADD += "-dlopen" hddtemp.la
 collectd_DEPENDENCIES += hddtemp.la
 endif
 
-if BUILD_MODULE_IOKIT
-pkglib_LTLIBRARIES += iokit.la
-iokit_la_SOURCES = iokit.c
-iokit_la_LDFLAGS = -module -avoid-version
-if BUILD_WITH_LIBIOKIT
-iokit_la_LDFLAGS += -lIOKit
-endif
-collectd_LDADD += "-dlopen" iokit.la
-collectd_DEPENDENCIES += iokit.la
-endif
-
 if BUILD_MODULE_LOAD
 pkglib_LTLIBRARIES += load.la
 load_la_SOURCES = load.c
diff --git a/src/apple_sensors.c b/src/apple_sensors.c
new file mode 100644 (file)
index 0000000..04a97cc
--- /dev/null
@@ -0,0 +1,266 @@
+/**
+ * collectd - src/apple_sensors.c
+ * Copyright (C) 2006  Florian octo Forster
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ *
+ * Authors:
+ *   Florian octo Forster <octo at verplant.org>
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+#include "utils_debug.h"
+
+#define MODULE_NAME "apple_sensors"
+
+#if HAVE_CTYPE_H
+#  include <ctype.h>
+#endif
+#if HAVE_MACH_MACH_TYPES_H
+#  include <mach/mach_types.h>
+#endif
+#if HAVE_MACH_MACH_INIT_H
+#  include <mach/mach_init.h>
+#endif
+#if HAVE_MACH_MACH_ERROR_H
+#  include <mach/mach_error.h>
+#endif
+#if HAVE_COREFOUNDATION_COREFOUNDATION_H
+#  include <CoreFoundation/CoreFoundation.h>
+#endif
+#if HAVE_IOKIT_IOKITLIB_H
+#  include <IOKit/IOKitLib.h>
+#endif
+#if HAVE_IOKIT_IOTYPES_H
+#  include <IOKit/IOTypes.h>
+#endif
+
+#if HAVE_IOKIT_IOKITLIB_H
+# define IOKIT_HAVE_READ 1
+#else
+# define IOKIT_HAVE_READ 0
+#endif
+
+#if IOKIT_HAVE_READ
+static mach_port_t io_master_port;
+#endif
+
+static char *temperature_file = "apple_sensors/temperature-%s.rrd";
+static char *fanspeed_file    = "apple_sensors/fanspeed-%s.rrd";
+static char *voltage_file     = "apple_sensors/temperature-%s.rrd";
+
+static char *ds_def[] =
+{
+       "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
+       NULL
+};
+static int ds_num = 1;
+
+static void as_init (void)
+{
+#if IOKIT_HAVE_READ
+       kern_return_t status;
+       
+       /* FIXME: de-allocate port if it's defined */
+
+       status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
+       if (status != kIOReturnSuccess)
+       {
+               syslog (LOG_ERR, "IOMasterPort failed: %s",
+                               mach_error_string (status));
+               io_master_port = MACH_PORT_NULL;
+               return;
+       }
+#endif
+
+       return;
+}
+
+static void as_write (char *host, char *inst, char *val, const char *template)
+{
+       char filename[256];
+       int  status;
+
+       status = snprintf (filename, 256, template, inst);
+       if ((status < 1) || (status >= 256))
+               return;
+
+       rrd_update_file (host, filename, val, ds_def, ds_num);
+}
+
+static void temperature_write (char *host, char *inst, char *val)
+{
+       as_write (host, inst, val, temperature_file);
+}
+
+static void fanspeed_write (char *host, char *inst, char *val)
+{
+       as_write (host, inst, val, fanspeed_file);
+}
+
+static void voltage_write (char *host, char *inst, char *val)
+{
+       as_write (host, inst, val, voltage_file);
+}
+
+#if IOKIT_HAVE_READ
+static void as_submit (char *type, char *inst, double value)
+{
+       char buf[128];
+
+       if (snprintf (buf, 1024, "%u:%f", (unsigned int) curtime,
+                               value) >= 128)
+               return;
+
+       plugin_submit (type, inst, buf);
+}
+
+static void 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;
+       int    i;
+
+       if (!io_master_port || (io_master_port == MACH_PORT_NULL))
+               return;
+
+       status = IOServiceGetMatchingServices (io_master_port,
+                       IOServiceNameMatching("IOHWSensor"),
+                       &iterator);
+       if (status != kIOReturnSuccess)
+               {
+               syslog (LOG_ERR, "IOServiceGetMatchingServices failed: %s",
+                               mach_error_string (status));
+               return;
+       }
+
+       while ((io_obj = IOIteratorNext (iterator)))
+       {
+               prop_dict = NULL;
+               status = IORegistryEntryCreateCFProperties (io_obj,
+                               &prop_dict,
+                               kCFAllocatorDefault,
+                               kNilOptions);
+               if (status != kIOReturnSuccess)
+               {
+                       DBG ("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, 128,
+                                       kCFStringEncodingASCII))
+                       continue;
+               type[127] = '\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, 128,
+                                       kCFStringEncodingASCII))
+                       continue;
+               inst[127] = '\0';
+               for (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;
+
+               if (strcmp (type, "temperature") == 0)
+               {
+                       value_double = ((double) value_int) / 65536.0;
+                       strncpy (type, "apple_temperature", 128);
+               }
+               else if (strcmp (type, "fanspeed") == 0)
+               {
+                       value_double = ((double) value_int) / 65536.0;
+                       strncpy (type, "apple_fanspeed", 128);
+               }
+               else if (strcmp (type, "voltage") == 0)
+               {
+                       value_double = ((double) value_int) / 65536.0;
+                       strncpy (type, "apple_voltage", 128);
+               }
+               else
+               {
+                       DBG ("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);
+}
+#else
+# define as_read NULL
+#endif /* IOKIT_HAVE_READ */
+
+void module_register (void)
+{
+       plugin_register (MODULE_NAME, as_init, as_read, NULL);
+       plugin_register ("apple_temperature", NULL, NULL, temperature_write);
+       plugin_register ("apple_fanspeed",    NULL, NULL, fanspeed_write);
+       plugin_register ("apple_voltage",     NULL, NULL, voltage_write);
+}
+
+#undef MODULE_NAME
diff --git a/src/iokit.c b/src/iokit.c
deleted file mode 100644 (file)
index 8a533f0..0000000
+++ /dev/null
@@ -1,234 +0,0 @@
-/**
- * collectd - src/iokit.c
- * Copyright (C) 2006  Florian octo Forster
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
- *
- * Authors:
- *   Florian octo Forster <octo at verplant.org>
- **/
-
-#include "collectd.h"
-#include "common.h"
-#include "plugin.h"
-#include "utils_debug.h"
-
-#define MODULE_NAME "iokit"
-
-#if HAVE_CTYPE_H
-#  include <ctype.h>
-#endif
-#if HAVE_MACH_MACH_TYPES_H
-#  include <mach/mach_types.h>
-#endif
-#if HAVE_MACH_MACH_INIT_H
-#  include <mach/mach_init.h>
-#endif
-#if HAVE_MACH_MACH_ERROR_H
-#  include <mach/mach_error.h>
-#endif
-#if HAVE_COREFOUNDATION_COREFOUNDATION_H
-#  include <CoreFoundation/CoreFoundation.h>
-#endif
-#if HAVE_IOKIT_IOKITLIB_H
-#  include <IOKit/IOKitLib.h>
-#endif
-#if HAVE_IOKIT_IOTYPES_H
-#  include <IOKit/IOTypes.h>
-#endif
-
-#if HAVE_IOKIT_IOKITLIB_H
-# define IOKIT_HAVE_READ 1
-#else
-# define IOKIT_HAVE_READ 0
-#endif
-
-#if IOKIT_HAVE_READ
-static mach_port_t io_master_port;
-#endif
-
-static char *temperature_file = "temperature-%s.rrd";
-
-static char *ds_def[] =
-{
-       "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
-       NULL
-};
-static int ds_num = 1;
-
-static void iokit_init (void)
-{
-#if IOKIT_HAVE_READ
-       kern_return_t status;
-       
-       /* FIXME: de-allocate port if it's defined */
-
-       status = IOMasterPort (MACH_PORT_NULL, &io_master_port);
-       if (status != kIOReturnSuccess)
-       {
-               syslog (LOG_ERR, "IOMasterPort failed: %s",
-                               mach_error_string (status));
-               io_master_port = MACH_PORT_NULL;
-               return;
-       }
-#endif
-
-       return;
-}
-
-static void temperature_write (char *host, char *inst, char *val)
-{
-       rrd_update_file (host, temperature_file, val, ds_def, ds_num);
-}
-
-#if IOKIT_HAVE_READ
-static void iokit_submit (char *type, char *inst, double value)
-{
-       char buf[128];
-
-       if (snprintf (buf, 1024, "%u:%f", (unsigned int) curtime,
-                               value) >= 128)
-               return;
-
-       plugin_submit (type, inst, buf);
-}
-
-static void iokit_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;
-       int    i;
-
-       if (!io_master_port || (io_master_port == MACH_PORT_NULL))
-               return;
-
-       status = IOServiceGetMatchingServices (io_master_port,
-                       IOServiceNameMatching("IOHWSensor"),
-                       &iterator);
-       if (status != kIOReturnSuccess)
-               {
-               syslog (LOG_ERR, "IOServiceGetMatchingServices failed: %s",
-                               mach_error_string (status));
-               return;
-       }
-
-       while ((io_obj = IOIteratorNext (iterator)))
-       {
-               prop_dict = NULL;
-               status = IORegistryEntryCreateCFProperties (io_obj,
-                               &prop_dict,
-                               kCFAllocatorDefault,
-                               kNilOptions);
-               if (status != kIOReturnSuccess)
-               {
-                       DBG ("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, 128,
-                                       kCFStringEncodingASCII))
-                       continue;
-               type[127] = '\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, 128,
-                                       kCFStringEncodingASCII))
-                       continue;
-               inst[127] = '\0';
-               for (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;
-
-               if ((strcmp (type, "temperature") == 0)
-                               || (strcmp (type, "fanspeed") == 0)
-                               || (strcmp (type, "voltage") == 0))
-               {
-                       value_double = ((double) value_int) / 65536.0;
-               }
-               else
-               {
-                       value_double = (double) value_int;
-               }
-
-               /* Do stuff */
-               DBG ("type = %s, inst = %s, value_int = %i, value_double = %f",
-                               type, inst, value_int, value_double);
-               iokit_submit (type, inst, value_double);
-
-               CFRelease (prop_dict);
-               IOObjectRelease (io_obj);
-       } /* while (iterator) */
-
-       IOObjectRelease (iterator);
-}
-#else
-# define iokit_read NULL
-#endif /* IOKIT_HAVE_READ */
-
-void module_register (void)
-{
-       DBG ("IOKIT_HAVE_READ = %i", IOKIT_HAVE_READ);
-
-       plugin_register (MODULE_NAME, iokit_init, iokit_read, NULL);
-       plugin_register ("iokit-temperature", NULL, NULL, temperature_write);
-}
-
-#undef MODULE_NAME