battery plugin: Only report "current" when actually supplied by the battery.
[collectd.git] / src / battery.c
index e52c2c8..87f2043 100644 (file)
@@ -1,6 +1,8 @@
 /**
  * collectd - src/battery.c
- * Copyright (C) 2006,2007  Florian octo Forster
+ * Copyright (C) 2006-2014  Florian octo Forster
+ * Copyright (C) 2008       Michał Mirosław
+ * Copyright (C) 2014       Andy Parkins
  *
  * 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
  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  *
  * Authors:
- *   Florian octo Forster <octo at verplant.org>
+ *   Florian octo Forster <octo at collectd.org>
+ *   Michał Mirosław <mirq-linux at rere.qmqm.pl>
+ *   Andy Parkins <andyp at fussylogic.co.uk>
  **/
 
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
-#include "utils_debug.h"
+
+#include "utils_complain.h"
 
 #if HAVE_MACH_MACH_TYPES_H
 #  include <mach/mach_types.h>
 #  include <IOKit/ps/IOPSKeys.h>
 #endif
 
-#if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H || KERNEL_LINUX
-# define BATTERY_HAVE_READ 1
-#else
-# define BATTERY_HAVE_READ 0
+#if !HAVE_IOKIT_IOKITLIB_H && !HAVE_IOKIT_PS_IOPOWERSOURCES_H && !KERNEL_LINUX
+# error "No applicable input method."
 #endif
 
 #define INVALID_VALUE 47841.29
 
-static data_source_t data_source_charge[1] =
-{
-       {"value", DS_TYPE_GAUGE, 0, NAN}
-};
-
-static data_set_t charge_ds =
-{
-       "charge", 1, data_source_charge
-};
-
-static data_source_t data_source_current[1] =
-{
-       {"value", DS_TYPE_GAUGE, NAN, NAN}
-};
-
-static data_set_t current_ds =
-{
-       "current", 1, data_source_current
-};
-
-static data_source_t data_source_voltage[1] =
-{
-       {"value", DS_TYPE_GAUGE, NAN, NAN}
-};
-
-static data_set_t voltage_ds =
-{
-       "voltage", 1, data_source_voltage
-};
-
-#if BATTERY_HAVE_READ
 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
        /* No global variables */
 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
 
 #elif KERNEL_LINUX
-static int   battery_pmu_num = 0;
-static char *battery_pmu_file = "/proc/pmu/battery_%i";
+# define PROC_PMU_PATH_FORMAT "/proc/pmu/battery_%i"
+# define PROC_ACPI_PATH "/proc/acpi/battery"
+# define SYSFS_PATH "/sys/class/power_supply"
 #endif /* KERNEL_LINUX */
 
-static int battery_init (void)
-{
-#if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
-       /* No init neccessary */
-/* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
-
-#elif KERNEL_LINUX
-       int len;
-       char filename[128];
-
-       for (battery_pmu_num = 0; ; battery_pmu_num++)
-       {
-               len = snprintf (filename, sizeof (filename), battery_pmu_file, battery_pmu_num);
-
-               if ((len >= sizeof (filename)) || (len < 0))
-                       break;
-
-               if (access (filename, R_OK))
-                       break;
-       }
-#endif /* KERNEL_LINUX */
-
-       return (0);
-}
-
-static void battery_submit (const char *plugin_instance, const char *type, double value)
+static void battery_submit (char const *plugin_instance, /* {{{ */
+               const char *type, gauge_t value)
 {
        value_t values[1];
        value_list_t vl = VALUE_LIST_INIT;
@@ -131,16 +80,16 @@ static void battery_submit (const char *plugin_instance, const char *type, doubl
 
        vl.values = values;
        vl.values_len = 1;
-       vl.time = time (NULL);
-       strcpy (vl.host, hostname);
-       strcpy (vl.plugin, "battery");
-       strcpy (vl.plugin_instance, plugin_instance);
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "battery", sizeof (vl.plugin));
+       sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
 
-       plugin_dispatch_values (type, &vl);
-} /* void battery_submit */
+       plugin_dispatch_values (&vl);
+} /* }}} void battery_submit */
 
 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
-double dict_get_double (CFDictionaryRef dict, char *key_string)
+static double dict_get_double (CFDictionaryRef dict, char *key_string) /* {{{ */
 {
        double      val_double;
        long long   val_int;
@@ -148,16 +97,16 @@ double dict_get_double (CFDictionaryRef dict, char *key_string)
        CFStringRef key_obj;
 
        key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key_string,
-                       kCFStringEncodingASCII);
+                       kCFStringEncodingASCII);
        if (key_obj == NULL)
        {
-               DBG ("CFStringCreateWithCString (%s) failed.\n", key_string);
+               DEBUG ("CFStringCreateWithCString (%s) failed.\n", key_string);
                return (INVALID_VALUE);
        }
 
        if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL)
        {
-               DBG ("CFDictionaryGetValue (%s) failed.", key_string);
+               DEBUG ("CFDictionaryGetValue (%s) failed.", key_string);
                CFRelease (key_obj);
                return (INVALID_VALUE);
        }
@@ -181,16 +130,15 @@ double dict_get_double (CFDictionaryRef dict, char *key_string)
        }
        else
        {
-               DBG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
+               DEBUG ("CFGetTypeID (val_obj) = %i", (int) CFGetTypeID (val_obj));
                return (INVALID_VALUE);
        }
 
        return (val_double);
-}
-#endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H */
+} /* }}} double dict_get_double */
 
-#if HAVE_IOKIT_PS_IOPOWERSOURCES_H
-static void get_via_io_power_sources (double *ret_charge,
+# if HAVE_IOKIT_PS_IOPOWERSOURCES_H
+static void get_via_io_power_sources (double *ret_charge, /* {{{ */
                double *ret_current,
                double *ret_voltage)
 {
@@ -207,7 +155,7 @@ static void get_via_io_power_sources (double *ret_charge,
        ps_array     = IOPSCopyPowerSourcesList (ps_raw);
        ps_array_len = CFArrayGetCount (ps_array);
 
-       DBG ("ps_array_len == %i", ps_array_len);
+       DEBUG ("ps_array_len == %i", ps_array_len);
 
        for (i = 0; i < ps_array_len; i++)
        {
@@ -216,13 +164,13 @@ static void get_via_io_power_sources (double *ret_charge,
 
                if (ps_dict == NULL)
                {
-                       DBG ("IOPSGetPowerSourceDescription failed.");
+                       DEBUG ("IOPSGetPowerSourceDescription failed.");
                        continue;
                }
 
                if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
                {
-                       DBG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
+                       DEBUG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
                        continue;
                }
 
@@ -258,11 +206,11 @@ static void get_via_io_power_sources (double *ret_charge,
 
        CFRelease(ps_array);
        CFRelease(ps_raw);
-}
-#endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */
+} /* }}} void get_via_io_power_sources */
+# endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */
 
-#if HAVE_IOKIT_IOKITLIB_H
-static void get_via_generic_iokit (double *ret_charge,
+# if HAVE_IOKIT_IOKITLIB_H
+static void get_via_generic_iokit (double *ret_charge, /* {{{ */
                double *ret_current,
                double *ret_voltage)
 {
@@ -283,7 +231,7 @@ static void get_via_generic_iokit (double *ret_charge,
                        &iterator);
        if (status != kIOReturnSuccess)
        {
-               DBG ("IOServiceGetMatchingServices failed.");
+               DEBUG ("IOServiceGetMatchingServices failed.");
                return;
        }
 
@@ -295,7 +243,7 @@ static void get_via_generic_iokit (double *ret_charge,
                                kNilOptions);
                if (status != kIOReturnSuccess)
                {
-                       DBG ("IORegistryEntryCreateCFProperties failed.");
+                       DEBUG ("IORegistryEntryCreateCFProperties failed.");
                        continue;
                }
 
@@ -309,8 +257,8 @@ static void get_via_generic_iokit (double *ret_charge,
                bat_info_arry_len = CFArrayGetCount (bat_info_arry);
 
                for (bat_info_arry_pos = 0;
-                               bat_info_arry_pos < bat_info_arry_len;
-                               bat_info_arry_pos++)
+                               bat_info_arry_pos < bat_info_arry_len;
+                               bat_info_arry_pos++)
                {
                        bat_info_dict = (CFDictionaryRef) CFArrayGetValueAtIndex (bat_info_arry, bat_info_arry_pos);
 
@@ -343,12 +291,11 @@ static void get_via_generic_iokit (double *ret_charge,
        }
 
        IOObjectRelease (iterator);
-}
-#endif /* HAVE_IOKIT_IOKITLIB_H */
+} /* }}} void get_via_generic_iokit */
+# endif /* HAVE_IOKIT_IOKITLIB_H */
 
-static int battery_read (void)
+static int battery_read (void) /* {{{ */
 {
-#if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
        double charge  = INVALID_VALUE; /* Current charge in Ah */
        double current = INVALID_VALUE; /* Current in A */
        double voltage = INVALID_VALUE; /* Voltage in V */
@@ -372,193 +319,344 @@ static int battery_read (void)
                battery_submit ("0", "current", current);
        if (voltage != INVALID_VALUE)
                battery_submit ("0", "voltage", voltage);
+} /* }}} int battery_read */
 /* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */
 
 #elif KERNEL_LINUX
-       FILE *fh;
-       char buffer[1024];
-       char filename[256];
-       
-       char *fields[8];
-       int numfields;
+/* Reads a file which contains only a number (and optionally a trailing
+ * newline) and parses that number. */
+static int sysfs_file_to_buffer(char const *dir, /* {{{ */
+               char const *power_supply,
+               char const *basename,
+               char *buffer, size_t buffer_size)
+{
+       int status;
+       FILE *fp;
+       char filename[PATH_MAX];
 
-       int i;
-       int len;
+       ssnprintf (filename, sizeof (filename), "%s/%s/%s",
+                       dir, power_supply, basename);
+
+       /* No file isn't the end of the world -- not every system will be
+        * reporting the same set of statistics */
+       if (access (filename, R_OK) != 0)
+               return ENOENT;
 
-       for (i = 0; i < battery_pmu_num; i++)
+       fp = fopen (filename, "r");
+       if (fp == NULL)
        {
-               char    batnum_str[256];
-               double  current = INVALID_VALUE;
-               double  voltage = INVALID_VALUE;
-               double  charge  = INVALID_VALUE;
-               double *valptr = NULL;
-
-               len = snprintf (filename, sizeof (filename), battery_pmu_file, i);
-               if ((len >= sizeof (filename)) || (len < 0))
-                       continue;
+               status = errno;
+               if (status != ENOENT)
+               {
+                       char errbuf[1024];
+                       WARNING ("battery plugin: fopen (%s) failed: %s", filename,
+                                       sstrerror (status, errbuf, sizeof (errbuf)));
+               }
+               return status;
+       }
 
-               len = snprintf (batnum_str, sizeof (batnum_str), "%i", i);
-               if ((len >= sizeof (batnum_str)) || (len < 0))
-                       continue;
+       if (fgets (buffer, buffer_size, fp) == NULL)
+       {
+               char errbuf[1024];
+               status = errno;
+               WARNING ("battery plugin: fgets failed: %s",
+                               sstrerror (status, errbuf, sizeof (errbuf)));
+               fclose (fp);
+               return status;
+       }
 
-               if ((fh = fopen (filename, "r")) == NULL)
-                       continue;
+       strstripnewline (buffer);
 
-               while (fgets (buffer, sizeof (buffer), fh) != NULL)
-               {
-                       numfields = strsplit (buffer, fields, 8);
+       fclose (fp);
+       return 0;
+} /* }}} int sysfs_file_to_buffer */
 
-                       if (numfields < 3)
-                               continue;
+/* Reads a file which contains only a number (and optionally a trailing
+ * newline) and parses that number. */
+static int sysfs_file_to_gauge(char const *dir, /* {{{ */
+               char const *power_supply,
+               char const *basename, gauge_t *ret_value)
+{
+       int status;
+       char buffer[32] = "";
 
-                       if (strcmp ("current", fields[0]) == 0)
-                               valptr = &current;
-                       else if (strcmp ("voltage", fields[0]) == 0)
-                               valptr = &voltage;
-                       else if (strcmp ("charge", fields[0]) == 0)
-                               valptr = &charge;
-                       else
-                               valptr = NULL;
+       status = sysfs_file_to_buffer (dir, power_supply, basename, buffer, sizeof (buffer));
+       if (status != 0)
+               return (status);
 
-                       if (valptr != NULL)
-                       {
-                               char *endptr;
+       return (strtogauge (buffer, ret_value));
+} /* }}} sysfs_file_to_gauge */
+
+static int read_sysfs_callback (char const *dir, /* {{{ */
+               char const *power_supply,
+               void *user_data)
+{
+       int *battery_index = user_data;
+
+       char const *plugin_instance;
+       char buffer[32];
+       gauge_t v = NAN;
+       _Bool discharging = 0;
+       int status;
+
+       /* Ignore non-battery directories, such as AC power. */
+       status = sysfs_file_to_buffer (dir, power_supply, "type", buffer, sizeof (buffer));
+       if (status != 0)
+               return (0);
+       if (strcasecmp ("Battery", buffer) != 0)
+               return (0);
+
+       (void) sysfs_file_to_buffer (dir, power_supply, "status", buffer, sizeof (buffer));
+       if (strcasecmp ("Discharging", buffer) == 0)
+               discharging = 1;
+
+       /* FIXME: This is a dirty hack for backwards compatibility: The battery
+        * plugin, for a very long time, has had the plugin_instance
+        * hard-coded to "0". So, to keep backwards compatibility, we'll use
+        * "0" for the first battery we find and the power_supply name for all
+        * following. This should be reverted in a future major version. */
+       plugin_instance = (*battery_index == 0) ? "0" : power_supply;
+       (*battery_index)++;
+
+       if (sysfs_file_to_gauge (dir, power_supply, "energy_now", &v) == 0)
+               battery_submit (plugin_instance, "charge", v / 1000000.0);
+       if (sysfs_file_to_gauge (dir, power_supply, "power_now", &v) == 0)
+       {
+               if (discharging)
+                       battery_submit (plugin_instance, "power", v / -1000000.0);
+               else
+                       battery_submit (plugin_instance, "power", v / 1000000.0);
+       }
+       if (sysfs_file_to_gauge (dir, power_supply, "voltage_now", &v) == 0)
+               battery_submit (plugin_instance, "voltage", v / 1000000.0);
+#if 0
+       if (sysfs_file_to_gauge (dir, power_supply, "energy_full_design", &v) == 0)
+               battery_submit (plugin_instance, "charge", v / 1000000.0);
+       if (sysfs_file_to_gauge (dir, power_supply, "energy_full", &v) == 0)
+               battery_submit (plugin_instance, "charge", v / 1000000.0);
+       if (sysfs_file_to_gauge (dir, power_supply, "voltage_min_design", &v) == 0)
+               battery_submit (plugin_instance, "voltage", v / 1000000.0);
+#endif
 
-                               endptr = NULL;
-                               errno  = 0;
+       return (0);
+} /* }}} int read_sysfs_callback */
 
-                               *valptr = strtod (fields[2], &endptr) / 1000.0;
+static int read_sysfs (void) /* {{{ */
+{
+       int status;
+       int battery_counter = 0;
 
-                               if ((fields[2] == endptr) || (errno != 0))
-                                       *valptr = INVALID_VALUE;
-                       }
-               }
+       if (access (SYSFS_PATH, R_OK) != 0)
+               return (ENOENT);
 
-               fclose (fh);
-               fh = NULL;
+       status = walk_directory (SYSFS_PATH, read_sysfs_callback,
+                       /* user_data = */ &battery_counter,
+                       /* include hidden */ 0);
+       return (status);
+} /* }}} int read_sysfs */
+
+static int read_acpi_callback (char const *dir, /* {{{ */
+               char const *power_supply,
+               void *user_data)
+{
+       int *battery_index = user_data;
+
+       gauge_t power = NAN;
+       gauge_t voltage = NAN;
+       gauge_t charge  = NAN;
+       _Bool charging = 0;
+       _Bool is_current = 0;
 
-               if (charge != INVALID_VALUE)
-                       battery_submit ("0", "charge", charge);
-               if (current != INVALID_VALUE)
-                       battery_submit ("0", "current", current);
-               if (voltage != INVALID_VALUE)
-                       battery_submit ("0", "voltage", voltage);
+       char const *plugin_instance;
+       char filename[PATH_MAX];
+       char buffer[1024];
+
+       FILE *fh;
+
+       ssnprintf (filename, sizeof (filename), "%s/%s/state", dir, power_supply);
+       fh = fopen (filename, "r");
+       if ((fh = fopen (filename, "r")) == NULL)
+       {
+               if ((errno == EAGAIN) || (errno == EINTR) || (errno == ENOENT))
+                       return (0);
+               else
+                       return (errno);
        }
 
-       if (access ("/proc/acpi/battery", R_OK | X_OK) == 0)
+       /*
+        * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
+        * [11:00] <@tokkee> present:                 yes
+        * [11:00] <@tokkee> capacity state:          ok
+        * [11:00] <@tokkee> charging state:          charging
+        * [11:00] <@tokkee> present rate:            1724 mA
+        * [11:00] <@tokkee> remaining capacity:      4136 mAh
+        * [11:00] <@tokkee> present voltage:         12428 mV
+        */
+       while (fgets (buffer, sizeof (buffer), fh) != NULL)
        {
-               double  current = INVALID_VALUE;
-               double  voltage = INVALID_VALUE;
-               double  charge  = INVALID_VALUE;
-               double *valptr = NULL;
-               int charging = 0;
+               char *fields[8];
+               int numfields;
 
-               struct dirent *ent;
-               DIR *dh;
+               numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
+               if (numfields < 3)
+                       continue;
 
-               if ((dh = opendir ("/proc/acpi/battery")) == NULL)
+               if ((strcmp (fields[0], "charging") == 0)
+                               && (strcmp (fields[1], "state:") == 0))
                {
-                       syslog (LOG_ERR, "Cannot open `/proc/acpi/battery': %s", strerror (errno));
-                       return (-1);
+                       if (strcmp (fields[2], "charging") == 0)
+                               charging = 1;
+                       else
+                               charging = 0;
+                       continue;
                }
 
-               while ((ent = readdir (dh)) != NULL)
+               /* The unit of "present rate" depends on the battery. Modern
+                * batteries export power (watts), older batteries (used to)
+                * export current (amperes). We check the fourth column and try
+                * to find old batteries this way. */
+               if ((strcmp (fields[0], "present") == 0)
+                               && (strcmp (fields[1], "rate:") == 0))
                {
-                       if (ent->d_name[0] == '.')
-                               continue;
+                       strtogauge (fields[2], &power);
+
+                       if ((numfields >= 4) && (strcmp ("mA", fields[3]) == 0))
+                               is_current = 1;
+               }
+               else if ((strcmp (fields[0], "remaining") == 0)
+                               && (strcmp (fields[1], "capacity:") == 0))
+                       strtogauge (fields[2], &charge);
+               else if ((strcmp (fields[0], "present") == 0)
+                               && (strcmp (fields[1], "voltage:") == 0))
+                       strtogauge (fields[2], &voltage);
+       } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
+
+       fclose (fh);
+
+       if (!charging)
+               power *= -1.0;
+
+       /* FIXME: This is a dirty hack for backwards compatibility: The battery
+        * plugin, for a very long time, has had the plugin_instance
+        * hard-coded to "0". So, to keep backwards compatibility, we'll use
+        * "0" for the first battery we find and the power_supply name for all
+        * following. This should be reverted in a future major version. */
+       plugin_instance = (*battery_index == 0) ? "0" : power_supply;
+       (*battery_index)++;
+
+       battery_submit (plugin_instance, "charge", charge / 1000.0);
+       battery_submit (plugin_instance,
+                       is_current ? "current" : "power",
+                       power / 1000.0);
+       battery_submit (plugin_instance, "voltage", voltage / 1000.0);
+
+       return 0;
+} /* }}} int read_acpi_callback */
+
+static int read_acpi (void) /* {{{ */
+{
+       int status;
+       int battery_counter = 0;
+
+       if (access (PROC_ACPI_PATH, R_OK) != 0)
+               return (ENOENT);
+
+       status = walk_directory (PROC_ACPI_PATH, read_acpi_callback,
+                       /* user_data = */ &battery_counter,
+                       /* include hidden */ 0);
+       return (status);
+} /* }}} int read_acpi */
+
+static int read_pmu (void) /* {{{ */
+{
+       int i;
+
+       /* The upper limit here is just a safeguard. If there is a system with
+        * more than 100 batteries, this can easily be increased. */
+       for (i = 0; i < 100; i++)
+       {
+               FILE *fh;
+
+               char buffer[1024];
+               char filename[PATH_MAX];
+               char plugin_instance[DATA_MAX_NAME_LEN];
+
+               gauge_t current = NAN;
+               gauge_t voltage = NAN;
+               gauge_t charge  = NAN;
+
+               ssnprintf (filename, sizeof (filename), PROC_PMU_PATH_FORMAT, i);
+               if (access (filename, R_OK) != 0)
+                       break;
+
+               ssnprintf (plugin_instance, sizeof (plugin_instance), "%i", i);
 
-                       len = snprintf (filename, sizeof (filename),
-                                       "/proc/acpi/battery/%s/state",
-                                       ent->d_name);
-                       if ((len >= sizeof (filename)) || (len < 0))
+               fh = fopen (filename, "r");
+               if (fh == NULL)
+               {
+                       if (errno == ENOENT)
+                               break;
+                       else if ((errno == EAGAIN) || (errno == EINTR))
                                continue;
+                       else
+                               return (errno);
+               }
 
-                       if ((fh = fopen (filename, "r")) == NULL)
-                       {
-                               syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
+               while (fgets (buffer, sizeof (buffer), fh) != NULL)
+               {
+                       char *fields[8];
+                       int numfields;
+
+                       numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
+                       if (numfields < 3)
                                continue;
-                       }
 
-                       /*
-                        * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
-                        * [11:00] <@tokkee> present:                 yes
-                        * [11:00] <@tokkee> capacity state:          ok
-                        * [11:00] <@tokkee> charging state:          charging
-                        * [11:00] <@tokkee> present rate:            1724 mA
-                        * [11:00] <@tokkee> remaining capacity:      4136 mAh
-                        * [11:00] <@tokkee> present voltage:         12428 mV
-                        */
-                       while (fgets (buffer, sizeof (buffer), fh) != NULL)
-                       {
-                               numfields = strsplit (buffer, fields, 8);
-
-                               if (numfields < 3)
-                                       continue;
-
-                               if ((strcmp (fields[0], "present") == 0)
-                                               && (strcmp (fields[1], "rate:") == 0))
-                                       valptr = &current;
-                               else if ((strcmp (fields[0], "remaining") == 0)
-                                               && (strcmp (fields[1], "capacity:") == 0))
-                                       valptr = &charge;
-                               else if ((strcmp (fields[0], "present") == 0)
-                                               && (strcmp (fields[1], "voltage:") == 0))
-                                       valptr = &voltage;
-                               else
-                                       valptr = NULL;
-
-                               if ((strcmp (fields[0], "charging") == 0)
-                                               && (strcmp (fields[1], "state:") == 0))
-                               {
-                                       if (strcmp (fields[2], "charging") == 0)
-                                               charging = 1;
-                                       else
-                                               charging = 0;
-                               }
-
-                               if (valptr != NULL)
-                               {
-                                       char *endptr;
-
-                                       endptr = NULL;
-                                       errno  = 0;
-
-                                       *valptr = strtod (fields[2], &endptr) / 1000.0;
-
-                                       if ((fields[2] == endptr) || (errno != 0))
-                                               *valptr = INVALID_VALUE;
-                               }
-                       } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
-
-                       fclose (fh);
-
-                       if ((current != INVALID_VALUE) && (charging == 0))
-                                       current *= -1;
-
-                       if (charge != INVALID_VALUE)
-                               battery_submit ("0", "charge", charge);
-                       if (current != INVALID_VALUE)
-                               battery_submit ("0", "current", current);
-                       if (voltage != INVALID_VALUE)
-                               battery_submit ("0", "voltage", voltage);
+                       if (strcmp ("current", fields[0]) == 0)
+                               strtogauge (fields[2], &current);
+                       else if (strcmp ("voltage", fields[0]) == 0)
+                               strtogauge (fields[2], &voltage);
+                       else if (strcmp ("charge", fields[0]) == 0)
+                               strtogauge (fields[2], &charge);
                }
 
-               closedir (dh);
+               fclose (fh);
+               fh = NULL;
+
+               battery_submit (plugin_instance, "charge", charge / 1000.0);
+               battery_submit (plugin_instance, "current", current / 1000.0);
+               battery_submit (plugin_instance, "voltage", voltage / 1000.0);
        }
-#endif /* KERNEL_LINUX */
 
+       if (i == 0)
+               return (ENOENT);
        return (0);
-}
-#endif /* BATTERY_HAVE_READ */
+} /* }}} int read_pmu */
 
-void module_register (void)
+static int battery_read (void) /* {{{ */
 {
-       plugin_register_data_set (&charge_ds);
-       plugin_register_data_set (&current_ds);
-       plugin_register_data_set (&voltage_ds);
+       int status;
+
+       DEBUG ("battery plugin: Trying sysfs ...");
+       status = read_sysfs ();
+       if (status == 0)
+               return (0);
+
+       DEBUG ("battery plugin: Trying acpi ...");
+       status = read_acpi ();
+       if (status == 0)
+               return (0);
+
+       DEBUG ("battery plugin: Trying pmu ...");
+       status = read_pmu ();
+       if (status == 0)
+               return (0);
+
+       ERROR ("battery plugin: Add available input methods failed.");
+       return (-1);
+} /* }}} int battery_read */
+#endif /* KERNEL_LINUX */
 
-#if BATTERY_HAVE_READ
-       plugin_register_init ("battery", battery_init);
+void module_register (void)
+{
        plugin_register_read ("battery", battery_read);
-#endif /* BATTERY_HAVE_READ */
-}
+} /* void module_register */