Tree wide: Don't set vl.host to hostname_g in plugin code.
[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
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) == 0) {
76     battery_submit("charge", v.gauge, NULL);
77     success++;
78   } else if (parse_value_file(STATEFS_ROOT "Capacity", &v, DS_TYPE_GAUGE) == 0) {
79     // Use capacity as a charge estimate if ChargePercentage is not available
80     battery_submit("charge", v.gauge, NULL);
81     success++;
82   } else {
83     WARNING("battery plugin: Neither \""STATEFS_ROOT"ChargePercentage\" "
84             "nor \""STATEFS_ROOT"Capacity\" could be read.");
85   }
86
87   struct {
88     char *path;
89     char *type;
90     char *type_instance;
91     gauge_t factor;
92   } metrics[] = {
93     {STATEFS_ROOT "Current",       "current",     NULL,  1e-6}, // from uA to A
94     {STATEFS_ROOT "Energy",        "energy_wh",   NULL,  1e-6}, // from uWh to Wh
95     {STATEFS_ROOT "Power",         "power",       NULL,  1e-6}, // from uW to W
96     {STATEFS_ROOT "Temperature",   "temperature", NULL,   0.1}, // from 10xC to C
97     {STATEFS_ROOT "TimeUntilFull", "duration",    "full", 1.0},
98     {STATEFS_ROOT "TimeUntilLow",  "duration",    "low",  1.0},
99     {STATEFS_ROOT "Voltage",       "voltage",     NULL,  1e-6}, // from uV to V
100   };
101
102   for (size_t i = 0; i < STATIC_ARRAY_SIZE(metrics); i++) {
103     if (parse_value_file(metrics[i].path, &v, DS_TYPE_GAUGE) != 0) {
104       WARNING("battery plugin: Reading \"%s\" failed.", metrics[i].path);
105       continue;
106     }
107
108     battery_submit(metrics[i].type, v.gauge * metrics[i].factor, metrics[i].type_instance);
109     success++;
110   }
111
112   if (success == 0) {
113     ERROR("battery plugin: statefs backend: none of the statistics are available");
114     return (-1);
115   }
116
117   return (0);
118 }