-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbattery.go
83 lines (73 loc) · 1.84 KB
/
battery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"errors"
"math"
"github.com/distatus/battery"
mp "github.com/mackerelio/go-mackerel-plugin"
)
// BatteryPlugin battery plugin for Mackerel.
type BatteryPlugin struct{}
// GraphDefinition returns the graph definition.
func (p BatteryPlugin) GraphDefinition() map[string]mp.Graphs {
return map[string]mp.Graphs{
"battery.capacity": {
Label: "Battery Capacity / mAh",
Unit: mp.UnitFloat,
Metrics: []mp.Metrics{
{Name: "design", Label: "Design capacity"},
{Name: "max", Label: "Max capacity"},
{Name: "current", Label: "Current capacity"},
},
},
"battery.percentage": {
Label: "Battery Capacity / %",
Unit: mp.UnitPercentage,
Metrics: []mp.Metrics{
{Name: "current_per_max", Label: "Current per max capacity"},
},
},
"battery.voltage": {
Label: "Battery Voltage / V",
Unit: mp.UnitFloat,
Metrics: []mp.Metrics{
{Name: "current_voltage", Label: "Current voltage"},
},
},
}
}
// FetchMetrics returns the metrics.
func (p BatteryPlugin) FetchMetrics() (map[string]float64, error) {
bat, err := battery.Get(0)
var errPartial battery.ErrPartial
if err != nil && !errors.As(err, &errPartial) {
return nil, err
}
if errPartial.Design != nil {
bat.Design = math.NaN()
}
if errPartial.Voltage != nil {
bat.Voltage = math.NaN()
}
if errPartial.Full != nil {
bat.Full = math.NaN()
}
if errPartial.Current != nil {
bat.Current = math.NaN()
}
metrics := map[string]float64{
"design": bat.Design / bat.Voltage,
"max": bat.Full / bat.Voltage,
"current": bat.Current / bat.Voltage,
"current_per_max": bat.Current / bat.Full * 100,
"current_voltage": bat.Voltage,
}
for k, v := range metrics {
if math.IsNaN(v) {
delete(metrics, k)
}
}
return metrics, nil
}
func main() {
mp.NewMackerelPlugin(BatteryPlugin{}).Run()
}