X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fbattery.c;h=952c56c071f854e0c3fbbc3d12ba7866f2c420bc;hb=d4b364625cc80550a29f392e7a8cc4fdd3aebf00;hp=a1c5e28b3be81a7e49d80b8e94a548f30963d495;hpb=84b0b421d6b4a4997601370e750527ab8066a125;p=collectd.git diff --git a/src/battery.c b/src/battery.c index a1c5e28b..952c56c0 100644 --- a/src/battery.c +++ b/src/battery.c @@ -1,11 +1,10 @@ /** * collectd - src/battery.c - * Copyright (C) 2006 Florian octo Forster + * Copyright (C) 2006,2007 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. + * Free Software Foundation; only version 2 of the License is applicable. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -24,124 +23,330 @@ #include "common.h" #include "plugin.h" -#define MODULE_NAME "battery" -#define BUFSIZE 512 +#if HAVE_MACH_MACH_TYPES_H +# include +#endif +#if HAVE_MACH_MACH_INIT_H +# include +#endif +#if HAVE_MACH_MACH_ERROR_H +# include +#endif +#if HAVE_COREFOUNDATION_COREFOUNDATION_H +# include +#endif +#if HAVE_IOKIT_IOKITLIB_H +# include +#endif +#if HAVE_IOKIT_IOTYPES_H +# include +#endif +#if HAVE_IOKIT_PS_IOPOWERSOURCES_H +# include +#endif +#if HAVE_IOKIT_PS_IOPSKEYS_H +# include +#endif -#if defined(KERNEL_LINUX) +#if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H || KERNEL_LINUX # define BATTERY_HAVE_READ 1 #else # define BATTERY_HAVE_READ 0 #endif -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"; +#define INVALID_VALUE 47841.29 -static char *ds_def_current[] = -{ - "DS:current:GAUGE:25:0:U", - NULL -}; -static int ds_num_current = 1; - -static char *ds_def_voltage[] = -{ - "DS:voltage:GAUGE:25:0:U", - NULL -}; -static int ds_num_voltage = 1; - -static char *ds_def_charge[] = -{ - "DS:charge:GAUGE:25:0:U", - NULL -}; -static int ds_num_charge = 1; +#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"; +#endif /* KERNEL_LINUX */ -static void battery_init (void) +static int battery_init (void) { -#if BATTERY_HAVE_READ +#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[BUFSIZE]; + char filename[128]; for (battery_pmu_num = 0; ; battery_pmu_num++) { - len = snprintf (filename, BUFSIZE, battery_pmu_file, battery_pmu_num); + len = snprintf (filename, sizeof (filename), battery_pmu_file, battery_pmu_num); - if ((len >= BUFSIZE) || (len < 0)) + if ((len >= sizeof (filename)) || (len < 0)) break; if (access (filename, R_OK)) break; } -#endif +#endif /* KERNEL_LINUX */ - return; + return (0); } -static void battery_current_write (char *host, char *inst, char *val) +static void battery_submit (const char *plugin_instance, const char *type, double value) { - rrd_update_file (host, battery_current_file, val, - ds_def_current, ds_num_current); -} + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; -static void battery_voltage_write (char *host, char *inst, char *val) -{ - rrd_update_file (host, battery_voltage_file, val, - ds_def_voltage, ds_num_voltage); -} + values[0].gauge = value; -static void battery_charge_write (char *host, char *inst, char *val) + vl.values = values; + vl.values_len = 1; + vl.time = time (NULL); + strcpy (vl.host, hostname_g); + strcpy (vl.plugin, "battery"); + strcpy (vl.plugin_instance, plugin_instance); + + plugin_dispatch_values (type, &vl); +} /* void battery_submit */ + +#if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H +double dict_get_double (CFDictionaryRef dict, char *key_string) { - rrd_update_file (host, battery_charge_file, val, - ds_def_charge, ds_num_charge); + double val_double; + long long val_int; + CFNumberRef val_obj; + CFStringRef key_obj; + + key_obj = CFStringCreateWithCString (kCFAllocatorDefault, key_string, + kCFStringEncodingASCII); + if (key_obj == NULL) + { + DEBUG ("CFStringCreateWithCString (%s) failed.\n", key_string); + return (INVALID_VALUE); + } + + if ((val_obj = CFDictionaryGetValue (dict, key_obj)) == NULL) + { + DEBUG ("CFDictionaryGetValue (%s) failed.", key_string); + CFRelease (key_obj); + return (INVALID_VALUE); + } + CFRelease (key_obj); + + if (CFGetTypeID (val_obj) == CFNumberGetTypeID ()) + { + if (CFNumberIsFloatType (val_obj)) + { + CFNumberGetValue (val_obj, + kCFNumberDoubleType, + &val_double); + } + else + { + CFNumberGetValue (val_obj, + kCFNumberLongLongType, + &val_int); + val_double = val_int; + } + } + else + { + 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 */ -#if BATTERY_HAVE_READ -static void battery_submit (int batnum, double current, double voltage, double charge) +#if HAVE_IOKIT_PS_IOPOWERSOURCES_H +static void get_via_io_power_sources (double *ret_charge, + double *ret_current, + double *ret_voltage) { - int len; - char buffer[BUFSIZE]; - char batnum_str[BUFSIZE]; + CFTypeRef ps_raw; + CFArrayRef ps_array; + int ps_array_len; + CFDictionaryRef ps_dict; + CFTypeRef ps_obj; - len = snprintf (batnum_str, BUFSIZE, "%i", batnum); - if ((len >= BUFSIZE) || (len < 0)) - return; + double temp_double; + int i; - if (current > 0.0) + ps_raw = IOPSCopyPowerSourcesInfo (); + ps_array = IOPSCopyPowerSourcesList (ps_raw); + ps_array_len = CFArrayGetCount (ps_array); + + DEBUG ("ps_array_len == %i", ps_array_len); + + for (i = 0; i < ps_array_len; i++) { - len = snprintf (buffer, BUFSIZE, "N:%.3f", current); + ps_obj = CFArrayGetValueAtIndex (ps_array, i); + ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj); + + if (ps_dict == NULL) + { + DEBUG ("IOPSGetPowerSourceDescription failed."); + continue; + } + + if (CFGetTypeID (ps_dict) != CFDictionaryGetTypeID ()) + { + DEBUG ("IOPSGetPowerSourceDescription did not return a CFDictionaryRef"); + continue; + } + + /* FIXME: Check if this is really an internal battery */ + + if (*ret_charge == INVALID_VALUE) + { + /* This is the charge in percent. */ + temp_double = dict_get_double (ps_dict, + kIOPSCurrentCapacityKey); + if ((temp_double != INVALID_VALUE) + && (temp_double >= 0.0) + && (temp_double <= 100.0)) + *ret_charge = temp_double; + } - if ((len > 0) && (len < BUFSIZE)) - plugin_submit ("battery_current", batnum_str, buffer); + if (*ret_current == INVALID_VALUE) + { + temp_double = dict_get_double (ps_dict, + kIOPSCurrentKey); + if (temp_double != INVALID_VALUE) + *ret_current = temp_double / 1000.0; + } + + if (*ret_voltage == INVALID_VALUE) + { + temp_double = dict_get_double (ps_dict, + kIOPSVoltageKey); + if (temp_double != INVALID_VALUE) + *ret_voltage = temp_double / 1000.0; + } } - if (voltage > 0.0) - { - len = snprintf (buffer, BUFSIZE, "N:%.3f", voltage); + CFRelease(ps_array); + CFRelease(ps_raw); +} +#endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */ - if ((len > 0) && (len < BUFSIZE)) - plugin_submit ("battery_voltage", batnum_str, buffer); +#if HAVE_IOKIT_IOKITLIB_H +static void get_via_generic_iokit (double *ret_charge, + double *ret_current, + double *ret_voltage) +{ + kern_return_t status; + io_iterator_t iterator; + io_object_t io_obj; + + CFDictionaryRef bat_root_dict; + CFArrayRef bat_info_arry; + CFIndex bat_info_arry_len; + CFIndex bat_info_arry_pos; + CFDictionaryRef bat_info_dict; + + double temp_double; + + status = IOServiceGetMatchingServices (kIOMasterPortDefault, + IOServiceNameMatching ("battery"), + &iterator); + if (status != kIOReturnSuccess) + { + DEBUG ("IOServiceGetMatchingServices failed."); + return; } - if (charge > 0.0) + while ((io_obj = IOIteratorNext (iterator))) { - len = snprintf (buffer, BUFSIZE, "N:%.3f", charge); + status = IORegistryEntryCreateCFProperties (io_obj, + (CFMutableDictionaryRef *) &bat_root_dict, + kCFAllocatorDefault, + kNilOptions); + if (status != kIOReturnSuccess) + { + DEBUG ("IORegistryEntryCreateCFProperties failed."); + continue; + } - if ((len > 0) && (len < BUFSIZE)) - plugin_submit ("battery_charge", batnum_str, buffer); + bat_info_arry = (CFArrayRef) CFDictionaryGetValue (bat_root_dict, + CFSTR ("IOBatteryInfo")); + if (bat_info_arry == NULL) + { + CFRelease (bat_root_dict); + continue; + } + 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_dict = (CFDictionaryRef) CFArrayGetValueAtIndex (bat_info_arry, bat_info_arry_pos); + + if (*ret_charge == INVALID_VALUE) + { + temp_double = dict_get_double (bat_info_dict, + "Capacity"); + if (temp_double != INVALID_VALUE) + *ret_charge = temp_double / 1000.0; + } + + if (*ret_current == INVALID_VALUE) + { + temp_double = dict_get_double (bat_info_dict, + "Current"); + if (temp_double != INVALID_VALUE) + *ret_current = temp_double / 1000.0; + } + + if (*ret_voltage == INVALID_VALUE) + { + temp_double = dict_get_double (bat_info_dict, + "Voltage"); + if (temp_double != INVALID_VALUE) + *ret_voltage = temp_double / 1000.0; + } + } + + CFRelease (bat_root_dict); } + + IOObjectRelease (iterator); } +#endif /* HAVE_IOKIT_IOKITLIB_H */ -static void battery_read (void) +static int battery_read (void) { -#ifdef KERNEL_LINUX +#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 */ + + double charge_rel = INVALID_VALUE; /* Current charge in percent */ + double charge_abs = INVALID_VALUE; /* Total capacity */ + +#if HAVE_IOKIT_PS_IOPOWERSOURCES_H + get_via_io_power_sources (&charge_rel, ¤t, &voltage); +#endif +#if HAVE_IOKIT_IOKITLIB_H + get_via_generic_iokit (&charge_abs, ¤t, &voltage); +#endif + + if ((charge_rel != INVALID_VALUE) && (charge_abs != INVALID_VALUE)) + charge = charge_abs * charge_rel / 100.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); +/* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */ + +#elif KERNEL_LINUX FILE *fh; - char buffer[BUFSIZE]; - char filename[BUFSIZE]; + char buffer[1024]; + char filename[256]; char *fields[8]; int numfields; @@ -151,19 +356,24 @@ static void battery_read (void) for (i = 0; i < battery_pmu_num; i++) { - double current = 0.0; - double voltage = 0.0; - double charge = 0.0; - - len = snprintf (filename, BUFSIZE, battery_pmu_file, i); + 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; - if ((len >= BUFSIZE) || (len < 0)) + len = snprintf (batnum_str, sizeof (batnum_str), "%i", i); + if ((len >= sizeof (batnum_str)) || (len < 0)) continue; if ((fh = fopen (filename, "r")) == NULL) continue; - while (fgets (buffer, BUFSIZE, fh) != NULL) + while (fgets (buffer, sizeof (buffer), fh) != NULL) { numfields = strsplit (buffer, fields, 8); @@ -171,32 +381,154 @@ static void battery_read (void) 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; - if ((current != 0.0) || (voltage != 0.0) || (charge != 0.0)) - battery_submit (i, current, voltage, charge); + endptr = NULL; + errno = 0; + + *valptr = strtod (fields[2], &endptr) / 1000.0; + + if ((fields[2] == endptr) || (errno != 0)) + *valptr = INVALID_VALUE; + } + } fclose (fh); fh = NULL; + + 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 (access ("/proc/acpi/battery", R_OK | X_OK) == 0) + { + 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) + { + char errbuf[1024]; + ERROR ("Cannot open `/proc/acpi/battery': %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (-1); + } + + while ((ent = readdir (dh)) != NULL) + { + if (ent->d_name[0] == '.') + continue; + + len = snprintf (filename, sizeof (filename), + "/proc/acpi/battery/%s/state", + ent->d_name); + if ((len >= sizeof (filename)) || (len < 0)) + continue; + + if ((fh = fopen (filename, "r")) == NULL) + { + char errbuf[1024]; + ERROR ("Cannot open `%s': %s", filename, + sstrerror (errno, errbuf, + sizeof (errbuf))); + 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 = ¤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; + } + } /* 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); + } + + closedir (dh); } #endif /* KERNEL_LINUX */ + + return (0); } -#else -# define battery_read NULL #endif /* BATTERY_HAVE_READ */ void module_register (void) { - plugin_register (MODULE_NAME, battery_init, battery_read, NULL); - plugin_register ("battery_current", NULL, NULL, battery_current_write); - plugin_register ("battery_voltage", NULL, NULL, battery_voltage_write); - plugin_register ("battery_charge", NULL, NULL, battery_charge_write); -} - -#undef BUFSIZE -#undef MODULE_NAME +#if BATTERY_HAVE_READ + plugin_register_init ("battery", battery_init); + plugin_register_read ("battery", battery_read); +#endif /* BATTERY_HAVE_READ */ +} /* void module_register */