| 1 |
/** |
| 2 |
* collectd - src/battery.c |
| 3 |
* Copyright (C) 2006-2014 Florian octo Forster |
| 4 |
* Copyright (C) 2008 Michał Mirosław |
| 5 |
* Copyright (C) 2014 Andy Parkins |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify it |
| 8 |
* under the terms of the GNU General Public License as published by the |
| 9 |
* Free Software Foundation; only version 2 of the License is applicable. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License along |
| 17 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 18 |
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
* Authors: |
| 21 |
* Florian octo Forster <octo at collectd.org> |
| 22 |
* Michał Mirosław <mirq-linux at rere.qmqm.pl> |
| 23 |
* Andy Parkins <andyp at fussylogic.co.uk> |
| 24 |
**/ |
| 25 |
|
| 26 |
#include "collectd.h" |
| 27 |
|
| 28 |
#include "plugin.h" |
| 29 |
#include "utils/common/common.h" |
| 30 |
|
| 31 |
#if HAVE_MACH_MACH_TYPES_H |
| 32 |
#include <mach/mach_types.h> |
| 33 |
#endif |
| 34 |
#if HAVE_MACH_MACH_INIT_H |
| 35 |
#include <mach/mach_init.h> |
| 36 |
#endif |
| 37 |
#if HAVE_MACH_MACH_ERROR_H |
| 38 |
#include <mach/mach_error.h> |
| 39 |
#endif |
| 40 |
#if HAVE_COREFOUNDATION_COREFOUNDATION_H |
| 41 |
#include <CoreFoundation/CoreFoundation.h> |
| 42 |
#endif |
| 43 |
#if HAVE_IOKIT_IOKITLIB_H |
| 44 |
#include <IOKit/IOKitLib.h> |
| 45 |
#endif |
| 46 |
#if HAVE_IOKIT_IOTYPES_H |
| 47 |
#include <IOKit/IOTypes.h> |
| 48 |
#endif |
| 49 |
#if HAVE_IOKIT_PS_IOPOWERSOURCES_H |
| 50 |
#include <IOKit/ps/IOPowerSources.h> |
| 51 |
#endif |
| 52 |
#if HAVE_IOKIT_PS_IOPSKEYS_H |
| 53 |
#include <IOKit/ps/IOPSKeys.h> |
| 54 |
#endif |
| 55 |
|
| 56 |
#if !HAVE_IOKIT_IOKITLIB_H && !HAVE_IOKIT_PS_IOPOWERSOURCES_H && !KERNEL_LINUX |
| 57 |
#error "No applicable input method." |
| 58 |
#endif |
| 59 |
|
| 60 |
#if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H |
| 61 |
/* No global variables */ |
| 62 |
/* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */ |
| 63 |
|
| 64 |
#elif KERNEL_LINUX |
| 65 |
#define PROC_PMU_PATH_FORMAT "/proc/pmu/battery_%i" |
| 66 |
#define PROC_ACPI_PATH "/proc/acpi/battery" |
| 67 |
#define PROC_ACPI_FACTOR 0.001 |
| 68 |
#define SYSFS_PATH "/sys/class/power_supply" |
| 69 |
#define SYSFS_FACTOR 0.000001 |
| 70 |
#endif /* KERNEL_LINUX */ |
| 71 |
|
| 72 |
int battery_read_statefs( |
| 73 |
void); /* defined in battery_statefs; used by StateFS backend */ |
| 74 |
|
| 75 |
static bool report_percent; |
| 76 |
static bool report_degraded; |
| 77 |
static bool query_statefs; |
| 78 |
|
| 79 |
static void battery_submit2(char const *plugin_instance, /* {{{ */ |
| 80 |
char const *type, char const *type_instance, |
| 81 |
gauge_t value) { |
| 82 |
value_list_t vl = VALUE_LIST_INIT; |
| 83 |
|
| 84 |
vl.values = &(value_t){.gauge = value}; |
| 85 |
vl.values_len = 1; |
| 86 |
sstrncpy(vl.plugin, "battery", sizeof(vl.plugin)); |
| 87 |
sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance)); |
| 88 |
sstrncpy(vl.type, type, sizeof(vl.type)); |
| 89 |
if (type_instance != NULL) |
| 90 |
sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance)); |
| 91 |
|
| 92 |
plugin_dispatch_values(&vl); |
| 93 |
} /* }}} void battery_submit2 */ |
| 94 |
|
| 95 |
static void battery_submit(char const *plugin_instance, /* {{{ */ |
| 96 |
char const *type, gauge_t value) { |
| 97 |
battery_submit2(plugin_instance, type, NULL, value); |
| 98 |
} /* }}} void battery_submit */ |
| 99 |
|
| 100 |
static void submit_capacity(char const *plugin_instance, /* {{{ */ |
| 101 |
gauge_t capacity_charged, gauge_t capacity_full, |
| 102 |
gauge_t capacity_design) { |
| 103 |
if (report_percent && (capacity_charged > capacity_full)) |
| 104 |
return; |
| 105 |
if (report_degraded && (capacity_full > capacity_design)) |
| 106 |
return; |
| 107 |
|
| 108 |
if (report_percent) { |
| 109 |
gauge_t capacity_max; |
| 110 |
|
| 111 |
if (report_degraded) |
| 112 |
capacity_max = capacity_design; |
| 113 |
else |
| 114 |
capacity_max = capacity_full; |
| 115 |
|
| 116 |
battery_submit2(plugin_instance, "percent", "charged", |
| 117 |
100.0 * capacity_charged / capacity_max); |
| 118 |
battery_submit2(plugin_instance, "percent", "discharged", |
| 119 |
100.0 * (capacity_full - capacity_charged) / capacity_max); |
| 120 |
|
| 121 |
if (report_degraded) |
| 122 |
battery_submit2(plugin_instance, "percent", "degraded", |
| 123 |
100.0 * (capacity_design - capacity_full) / capacity_max); |
| 124 |
} else if (report_degraded) /* && !report_percent */ |
| 125 |
{ |
| 126 |
battery_submit2(plugin_instance, "capacity", "charged", capacity_charged); |
| 127 |
battery_submit2(plugin_instance, "capacity", "discharged", |
| 128 |
(capacity_full - capacity_charged)); |
| 129 |
battery_submit2(plugin_instance, "capacity", "degraded", |
| 130 |
(capacity_design - capacity_full)); |
| 131 |
} else /* !report_percent && !report_degraded */ |
| 132 |
{ |
| 133 |
battery_submit(plugin_instance, "capacity", capacity_charged); |
| 134 |
} |
| 135 |
} /* }}} void submit_capacity */ |
| 136 |
|
| 137 |
#if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H |
| 138 |
static double dict_get_double(CFDictionaryRef dict, |
| 139 |
const char *key_string) /* {{{ */ |
| 140 |
{ |
| 141 |
double val_double; |
| 142 |
long long val_int; |
| 143 |
CFNumberRef val_obj; |
| 144 |
CFStringRef key_obj; |
| 145 |
|
| 146 |
key_obj = CFStringCreateWithCString(kCFAllocatorDefault, key_string, |
| 147 |
kCFStringEncodingASCII); |
| 148 |
if (key_obj == NULL) { |
| 149 |
DEBUG("CFStringCreateWithCString (%s) failed.\n", key_string); |
| 150 |
return NAN; |
| 151 |
} |
| 152 |
|
| 153 |
if ((val_obj = CFDictionaryGetValue(dict, key_obj)) == NULL) { |
| 154 |
DEBUG("CFDictionaryGetValue (%s) failed.", key_string); |
| 155 |
CFRelease(key_obj); |
| 156 |
return NAN; |
| 157 |
} |
| 158 |
CFRelease(key_obj); |
| 159 |
|
| 160 |
if (CFGetTypeID(val_obj) == CFNumberGetTypeID()) { |
| 161 |
if (CFNumberIsFloatType(val_obj)) { |
| 162 |
CFNumberGetValue(val_obj, kCFNumberDoubleType, &val_double); |
| 163 |
} else { |
| 164 |
CFNumberGetValue(val_obj, kCFNumberLongLongType, &val_int); |
| 165 |
val_double = val_int; |
| 166 |
} |
| 167 |
} else { |
| 168 |
DEBUG("CFGetTypeID (val_obj) = %i", (int)CFGetTypeID(val_obj)); |
| 169 |
return NAN; |
| 170 |
} |
| 171 |
|
| 172 |
return val_double; |
| 173 |
} /* }}} double dict_get_double */ |
| 174 |
|
| 175 |
#if HAVE_IOKIT_PS_IOPOWERSOURCES_H |
| 176 |
static void get_via_io_power_sources(double *ret_charge, /* {{{ */ |
| 177 |
double *ret_current, double *ret_voltage) { |
| 178 |
CFTypeRef ps_raw; |
| 179 |
CFArrayRef ps_array; |
| 180 |
int ps_array_len; |
| 181 |
CFDictionaryRef ps_dict; |
| 182 |
CFTypeRef ps_obj; |
| 183 |
|
| 184 |
double temp_double; |
| 185 |
|
| 186 |
ps_raw = IOPSCopyPowerSourcesInfo(); |
| 187 |
ps_array = IOPSCopyPowerSourcesList(ps_raw); |
| 188 |
ps_array_len = CFArrayGetCount(ps_array); |
| 189 |
|
| 190 |
DEBUG("ps_array_len == %i", ps_array_len); |
| 191 |
|
| 192 |
for (int i = 0; i < ps_array_len; i++) { |
| 193 |
ps_obj = CFArrayGetValueAtIndex(ps_array, i); |
| 194 |
ps_dict = IOPSGetPowerSourceDescription(ps_raw, ps_obj); |
| 195 |
|
| 196 |
if (ps_dict == NULL) { |
| 197 |
DEBUG("IOPSGetPowerSourceDescription failed."); |
| 198 |
continue; |
| 199 |
} |
| 200 |
|
| 201 |
if (CFGetTypeID(ps_dict) != CFDictionaryGetTypeID()) { |
| 202 |
DEBUG("IOPSGetPowerSourceDescription did not return a CFDictionaryRef"); |
| 203 |
continue; |
| 204 |
} |
| 205 |
|
| 206 |
/* FIXME: Check if this is really an internal battery */ |
| 207 |
|
| 208 |
if (isnan(*ret_charge)) { |
| 209 |
/* This is the charge in percent. */ |
| 210 |
temp_double = dict_get_double(ps_dict, kIOPSCurrentCapacityKey); |
| 211 |
if (!isnan((temp_double)) && (temp_double >= 0.0) && |
| 212 |
(temp_double <= 100.0)) |
| 213 |
*ret_charge = temp_double; |
| 214 |
} |
| 215 |
|
| 216 |
if (isnan(*ret_current)) { |
| 217 |
temp_double = dict_get_double(ps_dict, kIOPSCurrentKey); |
| 218 |
if (!isnan(temp_double)) |
| 219 |
*ret_current = temp_double / 1000.0; |
| 220 |
} |
| 221 |
|
| 222 |
if (isnan(*ret_voltage)) { |
| 223 |
temp_double = dict_get_double(ps_dict, kIOPSVoltageKey); |
| 224 |
if (!isnan(temp_double)) |
| 225 |
*ret_voltage = temp_double / 1000.0; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
CFRelease(ps_array); |
| 230 |
CFRelease(ps_raw); |
| 231 |
} /* }}} void get_via_io_power_sources */ |
| 232 |
#endif /* HAVE_IOKIT_PS_IOPOWERSOURCES_H */ |
| 233 |
|
| 234 |
#if HAVE_IOKIT_IOKITLIB_H |
| 235 |
static void get_via_generic_iokit(double *ret_capacity_full, /* {{{ */ |
| 236 |
double *ret_capacity_design, |
| 237 |
double *ret_current, double *ret_voltage) { |
| 238 |
kern_return_t status; |
| 239 |
io_iterator_t iterator; |
| 240 |
io_object_t io_obj; |
| 241 |
|
| 242 |
CFDictionaryRef bat_root_dict; |
| 243 |
CFArrayRef bat_info_arry; |
| 244 |
CFIndex bat_info_arry_len; |
| 245 |
CFDictionaryRef bat_info_dict; |
| 246 |
|
| 247 |
double temp_double; |
| 248 |
|
| 249 |
status = IOServiceGetMatchingServices( |
| 250 |
kIOMasterPortDefault, IOServiceNameMatching("battery"), &iterator); |
| 251 |
if (status != kIOReturnSuccess) { |
| 252 |
DEBUG("IOServiceGetMatchingServices failed."); |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
while ((io_obj = IOIteratorNext(iterator))) { |
| 257 |
status = IORegistryEntryCreateCFProperties( |
| 258 |
io_obj, (CFMutableDictionaryRef *)&bat_root_dict, kCFAllocatorDefault, |
| 259 |
kNilOptions); |
| 260 |
if (status != kIOReturnSuccess) { |
| 261 |
DEBUG("IORegistryEntryCreateCFProperties failed."); |
| 262 |
continue; |
| 263 |
} |
| 264 |
|
| 265 |
bat_info_arry = |
| 266 |
(CFArrayRef)CFDictionaryGetValue(bat_root_dict, CFSTR("IOBatteryInfo")); |
| 267 |
if (bat_info_arry == NULL) { |
| 268 |
CFRelease(bat_root_dict); |
| 269 |
continue; |
| 270 |
} |
| 271 |
bat_info_arry_len = CFArrayGetCount(bat_info_arry); |
| 272 |
|
| 273 |
for (CFIndex bat_info_arry_pos = 0; bat_info_arry_pos < bat_info_arry_len; |
| 274 |
bat_info_arry_pos++) { |
| 275 |
bat_info_dict = (CFDictionaryRef)CFArrayGetValueAtIndex( |
| 276 |
bat_info_arry, bat_info_arry_pos); |
| 277 |
|
| 278 |
if (isnan(*ret_capacity_full)) { |
| 279 |
temp_double = dict_get_double(bat_info_dict, "Capacity"); |
| 280 |
*ret_capacity_full = temp_double / 1000.0; |
| 281 |
} |
| 282 |
|
| 283 |
if (isnan(*ret_capacity_design)) { |
| 284 |
temp_double = dict_get_double(bat_info_dict, "AbsoluteMaxCapacity"); |
| 285 |
*ret_capacity_design = temp_double / 1000.0; |
| 286 |
} |
| 287 |
|
| 288 |
if (isnan(*ret_current)) { |
| 289 |
temp_double = dict_get_double(bat_info_dict, "Current"); |
| 290 |
*ret_current = temp_double / 1000.0; |
| 291 |
} |
| 292 |
|
| 293 |
if (isnan(*ret_voltage)) { |
| 294 |
temp_double = dict_get_double(bat_info_dict, "Voltage"); |
| 295 |
*ret_voltage = temp_double / 1000.0; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
CFRelease(bat_root_dict); |
| 300 |
} |
| 301 |
|
| 302 |
IOObjectRelease(iterator); |
| 303 |
} /* }}} void get_via_generic_iokit */ |
| 304 |
#endif /* HAVE_IOKIT_IOKITLIB_H */ |
| 305 |
|
| 306 |
static int battery_read(void) /* {{{ */ |
| 307 |
{ |
| 308 |
gauge_t current = NAN; /* Current in A */ |
| 309 |
gauge_t voltage = NAN; /* Voltage in V */ |
| 310 |
|
| 311 |
/* We only get the charged capacity as a percentage from |
| 312 |
* IOPowerSources. IOKit, on the other hand, only reports the full |
| 313 |
* capacity. We use the two to calculate the current charged capacity. */ |
| 314 |
gauge_t charge_rel = NAN; /* Current charge in percent */ |
| 315 |
gauge_t capacity_charged; /* Charged capacity */ |
| 316 |
gauge_t capacity_full = NAN; /* Total capacity */ |
| 317 |
gauge_t capacity_design = NAN; /* Full design capacity */ |
| 318 |
|
| 319 |
if (query_statefs) |
| 320 |
return battery_read_statefs(); |
| 321 |
|
| 322 |
#if HAVE_IOKIT_PS_IOPOWERSOURCES_H |
| 323 |
get_via_io_power_sources(&charge_rel, ¤t, &voltage); |
| 324 |
#endif |
| 325 |
#if HAVE_IOKIT_IOKITLIB_H |
| 326 |
get_via_generic_iokit(&capacity_full, &capacity_design, ¤t, &voltage); |
| 327 |
#endif |
| 328 |
|
| 329 |
capacity_charged = charge_rel * capacity_full / 100.0; |
| 330 |
submit_capacity("0", capacity_charged, capacity_full, capacity_design); |
| 331 |
|
| 332 |
if (!isnan(current)) |
| 333 |
battery_submit("0", "current", current); |
| 334 |
if (!isnan(voltage)) |
| 335 |
battery_submit("0", "voltage", voltage); |
| 336 |
|
| 337 |
return 0; |
| 338 |
} /* }}} int battery_read */ |
| 339 |
/* #endif HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H */ |
| 340 |
|
| 341 |
#elif KERNEL_LINUX |
| 342 |
/* Reads a file which contains only a number (and optionally a trailing |
| 343 |
* newline) and parses that number. */ |
| 344 |
static int sysfs_file_to_buffer(char const *dir, /* {{{ */ |
| 345 |
char const *power_supply, char const *basename, |
| 346 |
char *buffer, size_t buffer_size) { |
| 347 |
char filename[PATH_MAX]; |
| 348 |
int status; |
| 349 |
|
| 350 |
snprintf(filename, sizeof(filename), "%s/%s/%s", dir, power_supply, basename); |
| 351 |
|
| 352 |
status = (int)read_text_file_contents(filename, buffer, buffer_size); |
| 353 |
if (status < 0) |
| 354 |
return status; |
| 355 |
|
| 356 |
strstripnewline(buffer); |
| 357 |
return 0; |
| 358 |
} /* }}} int sysfs_file_to_buffer */ |
| 359 |
|
| 360 |
/* Reads a file which contains only a number (and optionally a trailing |
| 361 |
* newline) and parses that number. */ |
| 362 |
static int sysfs_file_to_gauge(char const *dir, /* {{{ */ |
| 363 |
char const *power_supply, char const *basename, |
| 364 |
gauge_t *ret_value) { |
| 365 |
int status; |
| 366 |
char buffer[32]; |
| 367 |
|
| 368 |
status = |
| 369 |
sysfs_file_to_buffer(dir, power_supply, basename, buffer, sizeof(buffer)); |
| 370 |
if (status != 0) |
| 371 |
return status; |
| 372 |
|
| 373 |
return strtogauge(buffer, ret_value); |
| 374 |
} /* }}} sysfs_file_to_gauge */ |
| 375 |
|
| 376 |
static int read_sysfs_capacity(char const *dir, /* {{{ */ |
| 377 |
char const *power_supply, |
| 378 |
char const *plugin_instance) { |
| 379 |
gauge_t capacity_charged = NAN; |
| 380 |
gauge_t capacity_full = NAN; |
| 381 |
gauge_t capacity_design = NAN; |
| 382 |
int status; |
| 383 |
|
| 384 |
status = |
| 385 |
sysfs_file_to_gauge(dir, power_supply, "energy_now", &capacity_charged); |
| 386 |
if (status != 0) |
| 387 |
return status; |
| 388 |
|
| 389 |
status = |
| 390 |
sysfs_file_to_gauge(dir, power_supply, "energy_full", &capacity_full); |
| 391 |
if (status != 0) |
| 392 |
return status; |
| 393 |
|
| 394 |
status = sysfs_file_to_gauge(dir, power_supply, "energy_full_design", |
| 395 |
&capacity_design); |
| 396 |
if (status != 0) |
| 397 |
return status; |
| 398 |
|
| 399 |
submit_capacity(plugin_instance, capacity_charged * SYSFS_FACTOR, |
| 400 |
capacity_full * SYSFS_FACTOR, capacity_design * SYSFS_FACTOR); |
| 401 |
return 0; |
| 402 |
} /* }}} int read_sysfs_capacity */ |
| 403 |
|
| 404 |
static int read_sysfs_capacity_from_charge(char const *dir, /* {{{ */ |
| 405 |
char const *power_supply, |
| 406 |
char const *plugin_instance) { |
| 407 |
gauge_t capacity_charged = NAN; |
| 408 |
gauge_t capacity_full = NAN; |
| 409 |
gauge_t capacity_design = NAN; |
| 410 |
gauge_t voltage_min_design = NAN; |
| 411 |
int status; |
| 412 |
|
| 413 |
status = sysfs_file_to_gauge(dir, power_supply, "voltage_min_design", |
| 414 |
&voltage_min_design); |
| 415 |
if (status != 0) |
| 416 |
return status; |
| 417 |
voltage_min_design *= SYSFS_FACTOR; |
| 418 |
|
| 419 |
status = |
| 420 |
sysfs_file_to_gauge(dir, power_supply, "charge_now", &capacity_charged); |
| 421 |
if (status != 0) |
| 422 |
return status; |
| 423 |
capacity_charged *= voltage_min_design; |
| 424 |
|
| 425 |
status = |
| 426 |
sysfs_file_to_gauge(dir, power_supply, "charge_full", &capacity_full); |
| 427 |
if (status != 0) |
| 428 |
return status; |
| 429 |
capacity_full *= voltage_min_design; |
| 430 |
|
| 431 |
status = sysfs_file_to_gauge(dir, power_supply, "charge_full_design", |
| 432 |
&capacity_design); |
| 433 |
if (status != 0) |
| 434 |
return status; |
| 435 |
capacity_design *= voltage_min_design; |
| 436 |
|
| 437 |
submit_capacity(plugin_instance, capacity_charged * SYSFS_FACTOR, |
| 438 |
capacity_full * SYSFS_FACTOR, capacity_design * SYSFS_FACTOR); |
| 439 |
return 0; |
| 440 |
} /* }}} int read_sysfs_capacity_from_charge */ |
| 441 |
|
| 442 |
static int read_sysfs_callback(char const *dir, /* {{{ */ |
| 443 |
char const *power_supply, void *user_data) { |
| 444 |
int *battery_index = user_data; |
| 445 |
|
| 446 |
char const *plugin_instance; |
| 447 |
char buffer[32]; |
| 448 |
gauge_t v = NAN; |
| 449 |
bool discharging = false; |
| 450 |
int status; |
| 451 |
|
| 452 |
/* Ignore non-battery directories, such as AC power. */ |
| 453 |
status = |
| 454 |
sysfs_file_to_buffer(dir, power_supply, "type", buffer, sizeof(buffer)); |
| 455 |
if (status != 0) |
| 456 |
return 0; |
| 457 |
if (strcasecmp("Battery", buffer) != 0) |
| 458 |
return 0; |
| 459 |
|
| 460 |
(void)sysfs_file_to_buffer(dir, power_supply, "status", buffer, |
| 461 |
sizeof(buffer)); |
| 462 |
if (strcasecmp("Discharging", buffer) == 0) |
| 463 |
discharging = true; |
| 464 |
|
| 465 |
/* FIXME: This is a dirty hack for backwards compatibility: The battery |
| 466 |
* plugin, for a very long time, has had the plugin_instance |
| 467 |
* hard-coded to "0". So, to keep backwards compatibility, we'll use |
| 468 |
* "0" for the first battery we find and the power_supply name for all |
| 469 |
* following. This should be reverted in a future major version. */ |
| 470 |
plugin_instance = (*battery_index == 0) ? "0" : power_supply; |
| 471 |
(*battery_index)++; |
| 472 |
|
| 473 |
if (sysfs_file_to_gauge(dir, power_supply, "energy_now", &v) == 0) |
| 474 |
read_sysfs_capacity(dir, power_supply, plugin_instance); |
| 475 |
else |
| 476 |
read_sysfs_capacity_from_charge(dir, power_supply, plugin_instance); |
| 477 |
|
| 478 |
if (sysfs_file_to_gauge(dir, power_supply, "power_now", &v) == 0) { |
| 479 |
if (discharging) |
| 480 |
v *= -1.0; |
| 481 |
battery_submit(plugin_instance, "power", v * SYSFS_FACTOR); |
| 482 |
} |
| 483 |
if (sysfs_file_to_gauge(dir, power_supply, "current_now", &v) == 0) { |
| 484 |
if (discharging) |
| 485 |
v *= -1.0; |
| 486 |
battery_submit(plugin_instance, "current", v * SYSFS_FACTOR); |
| 487 |
} |
| 488 |
|
| 489 |
if (sysfs_file_to_gauge(dir, power_supply, "voltage_now", &v) == 0) |
| 490 |
battery_submit(plugin_instance, "voltage", v * SYSFS_FACTOR); |
| 491 |
|
| 492 |
return 0; |
| 493 |
} /* }}} int read_sysfs_callback */ |
| 494 |
|
| 495 |
static int read_sysfs(void) /* {{{ */ |
| 496 |
{ |
| 497 |
int status; |
| 498 |
int battery_counter = 0; |
| 499 |
|
| 500 |
if (access(SYSFS_PATH, R_OK) != 0) |
| 501 |
return ENOENT; |
| 502 |
|
| 503 |
status = walk_directory(SYSFS_PATH, read_sysfs_callback, |
| 504 |
/* user_data = */ &battery_counter, |
| 505 |
/* include hidden */ 0); |
| 506 |
return status; |
| 507 |
} /* }}} int read_sysfs */ |
| 508 |
|
| 509 |
static int read_acpi_full_capacity(char const *dir, /* {{{ */ |
| 510 |
char const *power_supply, |
| 511 |
gauge_t *ret_capacity_full, |
| 512 |
gauge_t *ret_capacity_design) |
| 513 |
|
| 514 |
{ |
| 515 |
char filename[PATH_MAX]; |
| 516 |
char buffer[1024]; |
| 517 |
|
| 518 |
FILE *fh; |
| 519 |
|
| 520 |
snprintf(filename, sizeof(filename), "%s/%s/info", dir, power_supply); |
| 521 |
fh = fopen(filename, "r"); |
| 522 |
if (fh == NULL) |
| 523 |
return errno; |
| 524 |
|
| 525 |
/* last full capacity: 40090 mWh */ |
| 526 |
while (fgets(buffer, sizeof(buffer), fh) != NULL) { |
| 527 |
gauge_t *value_ptr; |
| 528 |
int fields_num; |
| 529 |
char *fields[8]; |
| 530 |
int index; |
| 531 |
|
| 532 |
if (strncmp("last full capacity:", buffer, strlen("last full capacity:")) == |
| 533 |
0) { |
| 534 |
value_ptr = ret_capacity_full; |
| 535 |
index = 3; |
| 536 |
} else if (strncmp("design capacity:", buffer, |
| 537 |
strlen("design capacity:")) == 0) { |
| 538 |
value_ptr = ret_capacity_design; |
| 539 |
index = 2; |
| 540 |
} else { |
| 541 |
continue; |
| 542 |
} |
| 543 |
|
| 544 |
fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields)); |
| 545 |
if (fields_num <= index) |
| 546 |
continue; |
| 547 |
|
| 548 |
strtogauge(fields[index], value_ptr); |
| 549 |
} |
| 550 |
|
| 551 |
fclose(fh); |
| 552 |
return 0; |
| 553 |
} /* }}} int read_acpi_full_capacity */ |
| 554 |
|
| 555 |
static int read_acpi_callback(char const *dir, /* {{{ */ |
| 556 |
char const *power_supply, void *user_data) { |
| 557 |
int *battery_index = user_data; |
| 558 |
|
| 559 |
gauge_t power = NAN; |
| 560 |
gauge_t voltage = NAN; |
| 561 |
gauge_t capacity_charged = NAN; |
| 562 |
gauge_t capacity_full = NAN; |
| 563 |
gauge_t capacity_design = NAN; |
| 564 |
bool charging = false; |
| 565 |
bool is_current = false; |
| 566 |
|
| 567 |
char const *plugin_instance; |
| 568 |
char filename[PATH_MAX]; |
| 569 |
char buffer[1024]; |
| 570 |
|
| 571 |
FILE *fh; |
| 572 |
|
| 573 |
snprintf(filename, sizeof(filename), "%s/%s/state", dir, power_supply); |
| 574 |
fh = fopen(filename, "r"); |
| 575 |
if (fh == NULL) { |
| 576 |
if ((errno == EAGAIN) || (errno == EINTR) || (errno == ENOENT)) |
| 577 |
return 0; |
| 578 |
else |
| 579 |
return errno; |
| 580 |
} |
| 581 |
|
| 582 |
/* |
| 583 |
* [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state |
| 584 |
* [11:00] <@tokkee> present: yes |
| 585 |
* [11:00] <@tokkee> capacity state: ok |
| 586 |
* [11:00] <@tokkee> charging state: charging |
| 587 |
* [11:00] <@tokkee> present rate: 1724 mA |
| 588 |
* [11:00] <@tokkee> remaining capacity: 4136 mAh |
| 589 |
* [11:00] <@tokkee> present voltage: 12428 mV |
| 590 |
*/ |
| 591 |
while (fgets(buffer, sizeof(buffer), fh) != NULL) { |
| 592 |
char *fields[8]; |
| 593 |
int numfields; |
| 594 |
|
| 595 |
numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields)); |
| 596 |
if (numfields < 3) |
| 597 |
continue; |
| 598 |
|
| 599 |
if ((strcmp(fields[0], "charging") == 0) && |
| 600 |
(strcmp(fields[1], "state:") == 0)) { |
| 601 |
if (strcmp(fields[2], "charging") == 0) |
| 602 |
charging = true; |
| 603 |
else |
| 604 |
charging = false; |
| 605 |
continue; |
| 606 |
} |
| 607 |
|
| 608 |
/* The unit of "present rate" depends on the battery. Modern |
| 609 |
* batteries export power (watts), older batteries (used to) |
| 610 |
* export current (amperes). We check the fourth column and try |
| 611 |
* to find old batteries this way. */ |
| 612 |
if ((strcmp(fields[0], "present") == 0) && |
| 613 |
(strcmp(fields[1], "rate:") == 0)) { |
| 614 |
strtogauge(fields[2], &power); |
| 615 |
|
| 616 |
if ((numfields >= 4) && (strcmp("mA", fields[3]) == 0)) |
| 617 |
is_current = true; |
| 618 |
} else if ((strcmp(fields[0], "remaining") == 0) && |
| 619 |
(strcmp(fields[1], "capacity:") == 0)) |
| 620 |
strtogauge(fields[2], &capacity_charged); |
| 621 |
else if ((strcmp(fields[0], "present") == 0) && |
| 622 |
(strcmp(fields[1], "voltage:") == 0)) |
| 623 |
strtogauge(fields[2], &voltage); |
| 624 |
} /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */ |
| 625 |
|
| 626 |
fclose(fh); |
| 627 |
|
| 628 |
if (!charging) |
| 629 |
power *= -1.0; |
| 630 |
|
| 631 |
/* FIXME: This is a dirty hack for backwards compatibility: The battery |
| 632 |
* plugin, for a very long time, has had the plugin_instance |
| 633 |
* hard-coded to "0". So, to keep backwards compatibility, we'll use |
| 634 |
* "0" for the first battery we find and the power_supply name for all |
| 635 |
* following. This should be reverted in a future major version. */ |
| 636 |
plugin_instance = (*battery_index == 0) ? "0" : power_supply; |
| 637 |
(*battery_index)++; |
| 638 |
|
| 639 |
read_acpi_full_capacity(dir, power_supply, &capacity_full, &capacity_design); |
| 640 |
|
| 641 |
submit_capacity(plugin_instance, capacity_charged * PROC_ACPI_FACTOR, |
| 642 |
capacity_full * PROC_ACPI_FACTOR, |
| 643 |
capacity_design * PROC_ACPI_FACTOR); |
| 644 |
|
| 645 |
battery_submit(plugin_instance, is_current ? "current" : "power", |
| 646 |
power * PROC_ACPI_FACTOR); |
| 647 |
battery_submit(plugin_instance, "voltage", voltage * PROC_ACPI_FACTOR); |
| 648 |
|
| 649 |
return 0; |
| 650 |
} /* }}} int read_acpi_callback */ |
| 651 |
|
| 652 |
static int read_acpi(void) /* {{{ */ |
| 653 |
{ |
| 654 |
int status; |
| 655 |
int battery_counter = 0; |
| 656 |
|
| 657 |
if (access(PROC_ACPI_PATH, R_OK) != 0) |
| 658 |
return ENOENT; |
| 659 |
|
| 660 |
status = walk_directory(PROC_ACPI_PATH, read_acpi_callback, |
| 661 |
/* user_data = */ &battery_counter, |
| 662 |
/* include hidden */ 0); |
| 663 |
return status; |
| 664 |
} /* }}} int read_acpi */ |
| 665 |
|
| 666 |
static int read_pmu(void) /* {{{ */ |
| 667 |
{ |
| 668 |
int i = 0; |
| 669 |
/* The upper limit here is just a safeguard. If there is a system with |
| 670 |
* more than 100 batteries, this can easily be increased. */ |
| 671 |
for (; i < 100; i++) { |
| 672 |
FILE *fh; |
| 673 |
|
| 674 |
char buffer[1024]; |
| 675 |
char filename[PATH_MAX]; |
| 676 |
char plugin_instance[DATA_MAX_NAME_LEN]; |
| 677 |
|
| 678 |
gauge_t current = NAN; |
| 679 |
gauge_t voltage = NAN; |
| 680 |
gauge_t charge = NAN; |
| 681 |
|
| 682 |
snprintf(filename, sizeof(filename), PROC_PMU_PATH_FORMAT, i); |
| 683 |
if (access(filename, R_OK) != 0) |
| 684 |
break; |
| 685 |
|
| 686 |
snprintf(plugin_instance, sizeof(plugin_instance), "%i", i); |
| 687 |
|
| 688 |
fh = fopen(filename, "r"); |
| 689 |
if (fh == NULL) { |
| 690 |
if (errno == ENOENT) |
| 691 |
break; |
| 692 |
else if ((errno == EAGAIN) || (errno == EINTR)) |
| 693 |
continue; |
| 694 |
else |
| 695 |
return errno; |
| 696 |
} |
| 697 |
|
| 698 |
while (fgets(buffer, sizeof(buffer), fh) != NULL) { |
| 699 |
char *fields[8]; |
| 700 |
int numfields; |
| 701 |
|
| 702 |
numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields)); |
| 703 |
if (numfields < 3) |
| 704 |
continue; |
| 705 |
|
| 706 |
if (strcmp("current", fields[0]) == 0) |
| 707 |
strtogauge(fields[2], ¤t); |
| 708 |
else if (strcmp("voltage", fields[0]) == 0) |
| 709 |
strtogauge(fields[2], &voltage); |
| 710 |
else if (strcmp("charge", fields[0]) == 0) |
| 711 |
strtogauge(fields[2], &charge); |
| 712 |
} |
| 713 |
|
| 714 |
fclose(fh); |
| 715 |
fh = NULL; |
| 716 |
|
| 717 |
battery_submit(plugin_instance, "charge", charge / 1000.0); |
| 718 |
battery_submit(plugin_instance, "current", current / 1000.0); |
| 719 |
battery_submit(plugin_instance, "voltage", voltage / 1000.0); |
| 720 |
} |
| 721 |
|
| 722 |
if (i == 0) |
| 723 |
return ENOENT; |
| 724 |
return 0; |
| 725 |
} /* }}} int read_pmu */ |
| 726 |
|
| 727 |
static int battery_read(void) /* {{{ */ |
| 728 |
{ |
| 729 |
int status; |
| 730 |
|
| 731 |
if (query_statefs) |
| 732 |
return battery_read_statefs(); |
| 733 |
|
| 734 |
DEBUG("battery plugin: Trying sysfs ..."); |
| 735 |
status = read_sysfs(); |
| 736 |
if (status == 0) |
| 737 |
return 0; |
| 738 |
|
| 739 |
DEBUG("battery plugin: Trying acpi ..."); |
| 740 |
status = read_acpi(); |
| 741 |
if (status == 0) |
| 742 |
return 0; |
| 743 |
|
| 744 |
DEBUG("battery plugin: Trying pmu ..."); |
| 745 |
status = read_pmu(); |
| 746 |
if (status == 0) |
| 747 |
return 0; |
| 748 |
|
| 749 |
ERROR("battery plugin: All available input methods failed."); |
| 750 |
return -1; |
| 751 |
} /* }}} int battery_read */ |
| 752 |
#endif /* KERNEL_LINUX */ |
| 753 |
|
| 754 |
static int battery_config(oconfig_item_t *ci) { |
| 755 |
for (int i = 0; i < ci->children_num; i++) { |
| 756 |
oconfig_item_t *child = ci->children + i; |
| 757 |
|
| 758 |
if (strcasecmp("ValuesPercentage", child->key) == 0) |
| 759 |
cf_util_get_boolean(child, &report_percent); |
| 760 |
else if (strcasecmp("ReportDegraded", child->key) == 0) |
| 761 |
cf_util_get_boolean(child, &report_degraded); |
| 762 |
else if (strcasecmp("QueryStateFS", child->key) == 0) |
| 763 |
cf_util_get_boolean(child, &query_statefs); |
| 764 |
else |
| 765 |
WARNING("battery plugin: Ignoring unknown " |
| 766 |
"configuration option \"%s\".", |
| 767 |
child->key); |
| 768 |
} |
| 769 |
|
| 770 |
return 0; |
| 771 |
} /* }}} int battery_config */ |
| 772 |
|
| 773 |
void module_register(void) { |
| 774 |
plugin_register_complex_config("battery", battery_config); |
| 775 |
plugin_register_read("battery", battery_read); |
| 776 |
} /* void module_register */ |