#include "collectd.h"
#include "common.h"
#include "plugin.h"
+#include "utils_debug.h"
#define MODULE_NAME "battery"
#define BUFSIZE 512
-#if defined(KERNEL_LINUX)
+#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_PS_IOPOWERSOURCES_H
+# include <IOKit/ps/IOPowerSources.h>
+#endif
+#if HAVE_IOKIT_PS_IOPSKEYS_H
+# include <IOKit/ps/IOPSKeys.h>
+#endif
+
+#if HAVE_IOKIT_PS_IOPOWERSOURCES_H || KERNEL_LINUX
# define BATTERY_HAVE_READ 1
#else
# define BATTERY_HAVE_READ 0
#endif
+#define INVALID_VALUE 47841.29
+
static char *battery_current_file = "battery-%s/current.rrd";
static char *battery_voltage_file = "battery-%s/voltage.rrd";
static char *battery_charge_file = "battery-%s/charge.rrd";
static char *ds_def_current[] =
{
- "DS:current:GAUGE:25:0:U",
+ "DS:current:GAUGE:"COLLECTD_HEARTBEAT":U:U",
NULL
};
static int ds_num_current = 1;
static char *ds_def_voltage[] =
{
- "DS:voltage:GAUGE:25:0:U",
+ "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":U:U",
NULL
};
static int ds_num_voltage = 1;
static char *ds_def_charge[] =
{
- "DS:charge:GAUGE:25:0:U",
+ "DS:charge:GAUGE:"COLLECTD_HEARTBEAT":0:U",
NULL
};
static int ds_num_charge = 1;
+#if HAVE_IOKIT_PS_IOPOWERSOURCES_H
+ /* No global variables */
+/* #endif HAVE_IOKIT_PS_IOPOWERSOURCES_H */
+
+#elif KERNEL_LINUX
static int battery_pmu_num = 0;
static char *battery_pmu_file = "/proc/pmu/battery_%i";
+#endif /* KERNEL_LINUX */
static void battery_init (void)
{
-#if BATTERY_HAVE_READ
+#if HAVE_IOKIT_PS_IOPOWERSOURCES_H
+ /* No init neccessary */
+/* #endif HAVE_IOKIT_PS_IOPOWERSOURCES_H */
+
+#elif KERNEL_LINUX
int len;
char filename[BUFSIZE];
if (access (filename, R_OK))
break;
}
-#endif
+#endif /* KERNEL_LINUX */
return;
}
}
#if BATTERY_HAVE_READ
-static void battery_submit (int batnum, double current, double voltage, double charge)
+static void battery_submit (char *inst, double current, double voltage, double charge)
{
int len;
char buffer[BUFSIZE];
- char batnum_str[BUFSIZE];
- len = snprintf (batnum_str, BUFSIZE, "%i", batnum);
- if ((len >= BUFSIZE) || (len < 0))
- return;
-
- if (current > 0.0)
+ if (current != INVALID_VALUE)
{
len = snprintf (buffer, BUFSIZE, "N:%.3f", current);
if ((len > 0) && (len < BUFSIZE))
- plugin_submit ("battery_current", batnum_str, buffer);
+ plugin_submit ("battery_current", inst, buffer);
+ }
+ else
+ {
+ plugin_submit ("battery_current", inst, "N:U");
}
- if (voltage > 0.0)
+ if (voltage != INVALID_VALUE)
{
len = snprintf (buffer, BUFSIZE, "N:%.3f", voltage);
if ((len > 0) && (len < BUFSIZE))
- plugin_submit ("battery_voltage", batnum_str, buffer);
+ plugin_submit ("battery_voltage", inst, buffer);
+ }
+ else
+ {
+ plugin_submit ("battery_voltage", inst, "N:U");
}
- if (charge > 0.0)
+ if (charge != INVALID_VALUE)
{
len = snprintf (buffer, BUFSIZE, "N:%.3f", charge);
if ((len > 0) && (len < BUFSIZE))
- plugin_submit ("battery_charge", batnum_str, buffer);
+ plugin_submit ("battery_charge", inst, buffer);
+ }
+ else
+ {
+ plugin_submit ("battery_charge", inst, "N:U");
}
}
static void battery_read (void)
{
-#ifdef KERNEL_LINUX
+#if HAVE_IOKIT_PS_IOPOWERSOURCES_H
+ CFTypeRef ps_raw;
+ CFArrayRef ps_array;
+ int ps_array_len;
+ CFDictionaryRef ps_dict;
+ CFTypeRef ps_obj;
+ CFTypeRef ps_value;
+
+ int i;
+
+ char name[128];
+ double charge = INVALID_VALUE;
+ double current = INVALID_VALUE;
+ double voltage = INVALID_VALUE;
+
+ ps_raw = IOPSCopyPowerSourcesInfo ();
+ ps_array = IOPSCopyPowerSourcesList (ps_raw);
+ ps_array_len = CFArrayGetCount (ps_array);
+
+ DBG ("ps_array_len == %i", ps_array_len);
+
+ for (i = 0; i < ps_array_len; i++)
+ {
+ ps_obj = CFArrayGetValueAtIndex (ps_array, i);
+ ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj);
+
+ if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ())
+ {
+ DBG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef");
+ continue;
+ }
+
+ if (ps_dict != NULL)
+ {
+ /* Get the current capacity/charge */
+ ps_value = NULL;
+ charge = INVALID_VALUE;
+ if (CFDictionaryGetValueIfPresent (ps_dict,
+ CFSTR (kIOPSCurrentCapacityKey),
+ &ps_value))
+ {
+ if (CFGetTypeID (ps_value) != CFNumberGetTypeID ())
+ CFNumberGetValue (ps_value,
+ kCFNumberDoubleType,
+ &charge);
+ else
+ DBG ("kIOPSCurrentCapacityKey: Not a CFNumber");
+
+ DBG ("charge = %f", charge);
+ }
+ else
+ DBG ("`%s' does not exist", kIOPSCurrentCapacityKey);
+
+ /* Get the current */
+ ps_value = NULL;
+ current = INVALID_VALUE;
+ if (CFDictionaryGetValueIfPresent (ps_dict,
+ CFSTR (kIOPSCurrentKey),
+ &ps_value))
+ {
+ if (CFGetTypeID (ps_value) != CFNumberGetTypeID ())
+ CFNumberGetValue (ps_value,
+ kCFNumberDoubleType,
+ ¤t);
+ else
+ DBG ("kIOPSCurrentKey: Not a CFNumber");
+ DBG ("current = %f", current);
+ }
+ else
+ DBG ("`%s' does not exist", kIOPSCurrentKey);
+
+ /* Get the voltage */
+ ps_value = NULL;
+ voltage = INVALID_VALUE;
+ if (CFDictionaryGetValueIfPresent (ps_dict,
+ CFSTR (kIOPSVoltageKey),
+ &ps_value))
+ {
+ if (CFGetTypeID (ps_value) != CFNumberGetTypeID ())
+ CFNumberGetValue (ps_value,
+ kCFNumberDoubleType,
+ &voltage);
+ else
+ DBG ("kIOPSVoltageKey: Not a CFNumber");
+ DBG ("voltage = %f", voltage);
+ }
+ else
+ DBG ("`%s' does not exist", kIOPSVoltageKey);
+
+ /* Get the name of the device.. */
+ ps_value = NULL;
+ if (CFDictionaryGetValueIfPresent (ps_dict,
+ CFSTR (kIOPSNameKey),
+ &ps_value))
+ {
+ if (CFGetTypeID (ps_value) != CFStringGetTypeID ())
+ {
+ if (!CFStringGetCString (ps_value,
+ name, 128,
+ kCFStringEncodingASCII))
+ continue;
+ }
+ else
+ {
+ DBG ("kIOPSNameKey: Not a CFStringGetTypeID");
+ }
+ DBG ("Original string: `%s'", name);
+ }
+ else
+ {
+ strncpy (name, "unknown", 128);
+ }
+ name[127] = '\0';
+ for (i = 0; i < 128; i++)
+ {
+ if (name[i] == '\0')
+ break;
+ else if (isalnum (name[i]))
+ name[i] = (char) tolower (name[i]);
+ else
+ name[i] = '_';
+ }
+
+ battery_submit (name, current, voltage, charge);
+ }
+ }
+
+ CFRelease(ps_array);
+ CFRelease(ps_raw);
+/* #endif HAVE_IOKIT_PS_IOPOWERSOURCES_H */
+
+#elif KERNEL_LINUX
FILE *fh;
char buffer[BUFSIZE];
char filename[BUFSIZE];
for (i = 0; i < battery_pmu_num; i++)
{
- double current = 0.0;
- double voltage = 0.0;
- double charge = 0.0;
+ char batnum_str[BUFSIZE];
+ double current = INVALID_VALUE;
+ double voltage = INVALID_VALUE;
+ double charge = INVALID_VALUE;
+ double *valptr = NULL;
len = snprintf (filename, BUFSIZE, battery_pmu_file, i);
+ if ((len >= BUFSIZE) || (len < 0))
+ continue;
+ len = snprintf (batnum_str, BUFSIZE, "%i", i);
if ((len >= BUFSIZE) || (len < 0))
continue;
continue;
if (strcmp ("current", fields[0]) == 0)
- current = atof (fields[2]) / 1000;
+ valptr = ¤t;
else if (strcmp ("voltage", fields[0]) == 0)
- voltage = atof (fields[2]) / 1000;
+ valptr = &voltage;
else if (strcmp ("charge", fields[0]) == 0)
- charge = atof (fields[2]) / 1000;
+ valptr = &charge;
+ else
+ valptr = NULL;
+
+ 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;
+ }
}
- if ((current != 0.0) || (voltage != 0.0) || (charge != 0.0))
- battery_submit (i, current, voltage, charge);
+ if ((current != INVALID_VALUE)
+ || (voltage != INVALID_VALUE)
+ || (charge != INVALID_VALUE))
+ battery_submit (batnum_str, current, voltage, charge);
fclose (fh);
fh = NULL;
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
- */
- syslog (LOG_DEBUG, "Found directory `/proc/acpi/battery'");
+ double current = INVALID_VALUE;
+ double voltage = INVALID_VALUE;
+ double charge = INVALID_VALUE;
+ double *valptr = NULL;
+ int charging = 0;
+
+ struct dirent *ent;
+ DIR *dh;
+
+ if ((dh = opendir ("/proc/acpi/battery")) == NULL)
+ {
+ syslog (LOG_ERR, "Cannot open `/proc/acpi/battery': %s", strerror (errno));
+ return;
+ }
+
+ while ((ent = readdir (dh)) != NULL)
+ {
+ if (ent->d_name[0] == '.')
+ continue;
+
+ len = snprintf (filename, BUFSIZE, "/proc/acpi/battery/%s/state", ent->d_name);
+ if ((len >= BUFSIZE) || (len < 0))
+ continue;
+
+ if ((fh = fopen (filename, "r")) == NULL)
+ {
+ syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
+ 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, BUFSIZE, fh) != NULL)
+ {
+ numfields = strsplit (buffer, fields, 8);
+
+ if (numfields < 3)
+ continue;
+
+ if ((strcmp (fields[0], "present") == 0)
+ && (strcmp (fields[1], "rate:") == 0))
+ valptr = ¤t;
+ 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;
+ }
+ }
+
+ if ((current != INVALID_VALUE) && (charging == 0))
+ current *= -1;
+
+ if ((current != INVALID_VALUE)
+ || (voltage != INVALID_VALUE)
+ || (charge != INVALID_VALUE))
+ battery_submit (ent->d_name, current, voltage, charge);
+
+ fclose (fh);
+ }
+
+ closedir (dh);
}
#endif /* KERNEL_LINUX */
}