2 * collectd - src/statefs_battery.c
3 * Copyright (C) 2016 rinigus
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
16 The above copyright notice and this permission notice shall be included in all
17 copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * rinigus <http://github.com/rinigus>
30 Battery stats are collected from StateFS Battery namespace. Reported
39 timefull and timelow seconds
43 https://git.merproject.org/mer-core/statefs-providers/blob/master/src/power_udev/provider_power_udev.cpp
49 #include "utils/common/common.h"
53 #define STATEFS_ROOT "/run/state/namespaces/Battery/"
55 static void battery_submit(const char *type, gauge_t value,
56 const char *type_instance) {
57 value_list_t vl = VALUE_LIST_INIT;
59 vl.values = &(value_t){.gauge = value};
61 sstrncpy(vl.plugin, "battery", sizeof(vl.plugin));
62 /* statefs supports 1 battery at present */
63 sstrncpy(vl.plugin_instance, "0", sizeof(vl.plugin_instance));
64 sstrncpy(vl.type, type, sizeof(vl.type));
65 if (type_instance != NULL)
66 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
67 plugin_dispatch_values(&vl);
70 /* cannot be static, is referred to from battery.c */
71 int battery_read_statefs(void) {
75 if (parse_value_file(STATEFS_ROOT "ChargePercentage", &v, DS_TYPE_GAUGE) ==
77 battery_submit("charge", v.gauge, NULL);
79 } else if (parse_value_file(STATEFS_ROOT "Capacity", &v, DS_TYPE_GAUGE) ==
81 // Use capacity as a charge estimate if ChargePercentage is not available
82 battery_submit("charge", v.gauge, NULL);
85 WARNING("battery plugin: Neither \"" STATEFS_ROOT "ChargePercentage\" "
86 "nor \"" STATEFS_ROOT "Capacity\" could be read.");
92 const char *type_instance;
95 {STATEFS_ROOT "Current", "current", NULL, 1e-6}, // from uA to A
96 {STATEFS_ROOT "Energy", "energy_wh", NULL, 1e-6}, // from uWh to Wh
97 {STATEFS_ROOT "Power", "power", NULL, 1e-6}, // from uW to W
98 {STATEFS_ROOT "Temperature", "temperature", NULL, 0.1}, // from 10xC to C
99 {STATEFS_ROOT "TimeUntilFull", "duration", "full", 1.0},
100 {STATEFS_ROOT "TimeUntilLow", "duration", "low", 1.0},
101 {STATEFS_ROOT "Voltage", "voltage", NULL, 1e-6}, // from uV to V
104 for (size_t i = 0; i < STATIC_ARRAY_SIZE(metrics); i++) {
105 if (parse_value_file(metrics[i].path, &v, DS_TYPE_GAUGE) != 0) {
106 WARNING("battery plugin: Reading \"%s\" failed.", metrics[i].path);
110 battery_submit(metrics[i].type, v.gauge * metrics[i].factor,
111 metrics[i].type_instance);
116 ERROR("battery plugin: statefs backend: none of the statistics are "