X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fbattery.c;h=ebf41debdab783fdf2b676e2219a994f86e1959b;hb=1aa4295ba6875ceb02a7383237bc2485ffab9c1e;hp=c4c050d7ff1cd11cc70cd2d17323f90da747e542;hpb=b4cc5c7399adf79198aea816d44a42df25431d15;p=collectd.git diff --git a/src/battery.c b/src/battery.c index c4c050d7..ebf41deb 100644 --- a/src/battery.c +++ b/src/battery.c @@ -24,11 +24,10 @@ **/ #include "collectd.h" + #include "common.h" #include "plugin.h" -#include "utils_complain.h" - #if HAVE_MACH_MACH_TYPES_H # include #endif @@ -70,18 +69,18 @@ # define SYSFS_FACTOR 0.000001 #endif /* KERNEL_LINUX */ +int battery_read_statefs (void); /* defined in battery_statefs; used by StateFS backend */ + static _Bool report_percent = 0; static _Bool report_degraded = 0; +static _Bool query_statefs = 0; static void battery_submit2 (char const *plugin_instance, /* {{{ */ char const *type, char const *type_instance, gauge_t value) { - value_t values[1]; value_list_t vl = VALUE_LIST_INIT; - values[0].gauge = value; - - vl.values = values; + vl.values = &(value_t) { .gauge = value }; vl.values_len = 1; sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "battery", sizeof (vl.plugin)); @@ -143,7 +142,7 @@ static void submit_capacity (char const *plugin_instance, /* {{{ */ } /* }}} void submit_capacity */ #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H -static double dict_get_double (CFDictionaryRef dict, char *key_string) /* {{{ */ +static double dict_get_double (CFDictionaryRef dict, const char *key_string) /* {{{ */ { double val_double; long long val_int; @@ -203,7 +202,6 @@ static void get_via_io_power_sources (double *ret_charge, /* {{{ */ CFTypeRef ps_obj; double temp_double; - int i; ps_raw = IOPSCopyPowerSourcesInfo (); ps_array = IOPSCopyPowerSourcesList (ps_raw); @@ -211,7 +209,7 @@ static void get_via_io_power_sources (double *ret_charge, /* {{{ */ DEBUG ("ps_array_len == %i", ps_array_len); - for (i = 0; i < ps_array_len; i++) + for (int i = 0; i < ps_array_len; i++) { ps_obj = CFArrayGetValueAtIndex (ps_array, i); ps_dict = IOPSGetPowerSourceDescription (ps_raw, ps_obj); @@ -276,7 +274,6 @@ static void get_via_generic_iokit (double *ret_capacity_full, /* {{{ */ 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; @@ -311,7 +308,7 @@ static void get_via_generic_iokit (double *ret_capacity_full, /* {{{ */ } bat_info_arry_len = CFArrayGetCount (bat_info_arry); - for (bat_info_arry_pos = 0; + for (CFIndex bat_info_arry_pos = 0; bat_info_arry_pos < bat_info_arry_len; bat_info_arry_pos++) { @@ -341,7 +338,7 @@ static void get_via_generic_iokit (double *ret_capacity_full, /* {{{ */ *ret_voltage = temp_double / 1000.0; } } - + CFRelease (bat_root_dict); } @@ -358,10 +355,13 @@ static int battery_read (void) /* {{{ */ * IOPowerSources. IOKit, on the other hand, only reports the full * capacity. We use the two to calculate the current charged capacity. */ gauge_t charge_rel = NAN; /* Current charge in percent */ - gauge_t capacity_charged = NAN; /* Charged capacity */ + gauge_t capacity_charged; /* Charged capacity */ gauge_t capacity_full = NAN; /* Total capacity */ gauge_t capacity_design = NAN; /* Full design capacity */ + if (query_statefs) + return battery_read_statefs (); + #if HAVE_IOKIT_PS_IOPOWERSOURCES_H get_via_io_power_sources (&charge_rel, ¤t, &voltage); #endif @@ -389,47 +389,17 @@ static int sysfs_file_to_buffer(char const *dir, /* {{{ */ char const *basename, char *buffer, size_t buffer_size) { - int status; - FILE *fp; char filename[PATH_MAX]; + int status; 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; - - fp = fopen (filename, "r"); - if (fp == NULL) - { - status = errno; - if (status != ENOENT) - { - char errbuf[1024]; - WARNING ("battery plugin: fopen (%s) failed: %s", filename, - sstrerror (status, errbuf, sizeof (errbuf))); - } + status = (int) read_file_contents (filename, buffer, buffer_size); + if (status < 0) return status; - } - - if (fgets (buffer, buffer_size, fp) == NULL) - { - status = errno; - if (status != ENODEV) - { - char errbuf[1024]; - WARNING ("battery plugin: fgets (%s) failed: %s", filename, - sstrerror (status, errbuf, sizeof (errbuf))); - } - fclose (fp); - return status; - } strstripnewline (buffer); - - fclose (fp); return 0; } /* }}} int sysfs_file_to_buffer */ @@ -715,11 +685,10 @@ static int read_acpi (void) /* {{{ */ static int read_pmu (void) /* {{{ */ { - int i; - + int i = 0; /* 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++) + for (; i < 100; i++) { FILE *fh; @@ -782,6 +751,9 @@ static int battery_read (void) /* {{{ */ { int status; + if (query_statefs) + return battery_read_statefs (); + DEBUG ("battery plugin: Trying sysfs ..."); status = read_sysfs (); if (status == 0) @@ -804,9 +776,7 @@ static int battery_read (void) /* {{{ */ static int battery_config (oconfig_item_t *ci) { - int i; - - for (i = 0; i < ci->children_num; i++) + for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; @@ -814,6 +784,8 @@ static int battery_config (oconfig_item_t *ci) cf_util_get_boolean (child, &report_percent); else if (strcasecmp ("ReportDegraded", child->key) == 0) cf_util_get_boolean (child, &report_degraded); + else if (strcasecmp ("QueryStateFS", child->key) == 0) + cf_util_get_boolean (child, &query_statefs); else WARNING ("battery plugin: Ignoring unknown " "configuration option \"%s\".",