Merge pull request #3329 from efuss/fix-3311
[collectd.git] / src / battery_statefs.c
1 /**
2  * collectd - src/statefs_battery.c
3  * Copyright (C) 2016 rinigus
4  *
5  *
6 The MIT License (MIT)
7
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:
15
16 The above copyright notice and this permission notice shall be included in all
17 copies or substantial portions of the Software.
18
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
25 SOFTWARE.
26
27  * Authors:
28  *   rinigus <http://github.com/rinigus>
29
30  Battery stats are collected from StateFS Battery namespace. Reported
31  units are as follows:
32
33  capacity %
34  charge %
35  current A
36  energy Wh
37  power W
38  temperature C
39  timefull and timelow seconds
40  voltage V
41
42  Provider at
43  https://git.merproject.org/mer-core/statefs-providers/blob/master/src/power_udev/provider_power_udev.cpp
44
45  **/
46
47 #include "collectd.h"
48 #include "plugin.h"
49 #include "utils/common/common.h"
50
51 #include <stdio.h>
52
53 #define STATEFS_ROOT "/run/state/namespaces/Battery/"
54
55 static void battery_submit(const char *type, gauge_t value,
56                            const char *type_instance) {
57   value_list_t vl = VALUE_LIST_INIT;
58
59   vl.values = &(value_t){.gauge = value};
60   vl.values_len = 1;
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);
68 }
69
70 /* cannot be static, is referred to from battery.c */
71 int battery_read_statefs(void) {
72   value_t v;
73   int success = 0;
74
75   if (parse_value_file(STATEFS_ROOT "ChargePercentage", &v, DS_TYPE_GAUGE) ==
76       0) {
77     battery_submit("charge", v.gauge, NULL);
78     success++;
79   } else if (parse_value_file(STATEFS_ROOT "Capacity", &v, DS_TYPE_GAUGE) ==
80              0) {
81     // Use capacity as a charge estimate if ChargePercentage is not available
82     battery_submit("charge", v.gauge, NULL);
83     success++;
84   } else {
85     WARNING("battery plugin: Neither \"" STATEFS_ROOT "ChargePercentage\" "
86             "nor \"" STATEFS_ROOT "Capacity\" could be read.");
87   }
88
89   struct {
90     const char *path;
91     const char *type;
92     const char *type_instance;
93     gauge_t factor;
94   } metrics[] = {
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
102   };
103
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);
107       continue;
108     }
109
110     battery_submit(metrics[i].type, v.gauge * metrics[i].factor,
111                    metrics[i].type_instance);
112     success++;
113   }
114
115   if (success == 0) {
116     ERROR("battery plugin: statefs backend: none of the statistics are "
117           "available");
118     return -1;
119   }
120
121   return 0;
122 }