battery plugin: Add StateFS backend.
[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 "common.h"
49 #include "plugin.h"
50
51 #include <stdio.h>
52
53 #define STATEFS_ROOT "/run/state/namespaces/Battery/"
54 #define BUFFER_SIZE 512
55
56 static int submitted_this_run = 0;
57
58 static void battery_submit(const char *type, gauge_t value,
59                            const char *type_instance) {
60   value_t values[1];
61   value_list_t vl = VALUE_LIST_INIT;
62
63   values[0].gauge = value;
64
65   vl.values = values;
66   vl.values_len = 1;
67   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
68   sstrncpy(vl.plugin, "battery", sizeof(vl.plugin));
69   /* statefs supports 1 battery at present */
70   sstrncpy(vl.plugin_instance, "0", sizeof(vl.plugin_instance));
71   sstrncpy(vl.type, type, sizeof(vl.type));
72   if (type_instance != NULL)
73     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
74   plugin_dispatch_values(&vl);
75
76   submitted_this_run++;
77 }
78
79 static _Bool getvalue(const char *fname, gauge_t *value) {
80   FILE *fh;
81   char buffer[BUFFER_SIZE];
82
83   if ((fh = fopen(fname, "r")) == NULL) {
84     WARNING("battery plugin: cannot open StateFS file %s", fname);
85     return (0);
86   }
87
88   if (fgets(buffer, STATIC_ARRAY_SIZE(buffer), fh) == NULL) {
89     fclose(fh);
90     return (0); // empty file
91   }
92
93   (*value) = atof(buffer);
94
95   fclose(fh);
96
97   return (1);
98 }
99
100 /* cannot be static, is referred to from battery.c */
101 int battery_read_statefs(void) {
102   gauge_t value = NAN;
103
104   submitted_this_run = 0;
105
106   if (getvalue(STATEFS_ROOT "ChargePercentage", &value))
107     battery_submit("charge", value, NULL);
108   // Use capacity as a charge estimate if ChargePercentage is not available
109   else if (getvalue(STATEFS_ROOT "Capacity", &value))
110     battery_submit("charge", value, NULL);
111
112   if (getvalue(STATEFS_ROOT "Current", &value))
113     battery_submit("current", value * 1e-6, NULL); // from uA to A
114
115   if (getvalue(STATEFS_ROOT "Energy", &value))
116     battery_submit("energy_wh", value * 1e-6, NULL); // from uWh to Wh
117
118   if (getvalue(STATEFS_ROOT "Power", &value))
119     battery_submit("power", value * 1e-6, NULL); // from uW to W
120
121   if (getvalue(STATEFS_ROOT "Temperature", &value))
122     battery_submit("temperature", value * 0.1, NULL); // from 10xC to C
123
124   if (getvalue(STATEFS_ROOT "TimeUntilFull", &value))
125     battery_submit("duration", value, "full");
126
127   if (getvalue(STATEFS_ROOT "TimeUntilLow", &value))
128     battery_submit("duration", value, "low");
129
130   if (getvalue(STATEFS_ROOT "Voltage", &value))
131     battery_submit("voltage", value * 1e-6, NULL); // from uV to V
132
133   if (submitted_this_run == 0) {
134     ERROR("battery plugin: statefs backend: none of the statistics are "
135           "available");
136     return (-1);
137   }
138
139   return (0);
140 }