-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
147 lines (122 loc) · 3.23 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"time"
"github.com/karalabe/hid"
)
const (
vendorID = 0x09ae
productID = 0x2012
)
type formatFunc func(data []byte, d *hid.Device, name string, w io.Writer)
type feature struct {
name string
reportID byte
length int
format formatFunc
}
func intFormatter(data []byte, d *hid.Device, name string, w io.Writer) {
var v uint64
for i, b := range data {
v |= uint64(b) << (8 * i)
}
fmt.Fprintf(w, "# TYPE %s gauge\n", name)
fmt.Fprintf(w, "%s{path=\"%s\",product=\"%s\"} %d\n", name, d.Path, d.Product, v)
}
func oneTenthFloatFormatter(data []byte, d *hid.Device, name string, w io.Writer) {
var v uint64
for i, b := range data {
v |= uint64(b) << (8 * i)
}
fmt.Fprintf(w, "# TYPE %s gauge\n", name)
fmt.Fprintf(w, "%s{path=\"%s\",product=\"%s\"} %f\n", name, d.Path, d.Product, float64(v)/10.0)
}
func statusFormatter(data []byte, d *hid.Device, name string, w io.Writer) {
bitOn := func(b byte, i int) int {
if b&(1<<i) != 0 {
return 1
} else {
return 0
}
}
fields := []string{
"shutdown_imminent",
"ac_present",
"charging",
"discharging",
"needs_replacement",
"below_remaining_capacity",
"fully_charged",
"fully_discharged",
}
for i, f := range fields {
n := name + "_" + f
fmt.Fprintf(w, "# TYPE %s gauge\n", n)
fmt.Fprintf(w, "%s{path=\"%s\",product=\"%s\"} %d\n", n, d.Path, d.Product, bitOn(data[0], i))
}
}
var features = []feature{
{"tripplite_config_voltage", 48, 1, intFormatter},
{"tripplite_config_frequency_hz", 2, 1, intFormatter},
{"tripplite_config_power_watts", 3, 2, intFormatter},
{"tripplite_input_voltage", 24, 2, oneTenthFloatFormatter},
{"tripplite_input_frequency_hz", 25, 2, oneTenthFloatFormatter},
{"tripplite_output_voltage", 27, 2, oneTenthFloatFormatter},
{"tripplite_output_power_watts", 71, 2, intFormatter},
{"tripplite_current_charge_pct", 52, 1, intFormatter},
{"tripplite_run_time_to_empty_minutes", 53, 2, intFormatter},
{"tripplite_status", 50, 1, statusFormatter},
}
func main() {
var addr string
flag.StringVar(&addr, "addr", ":9528", "Prometheus exporter listen address")
flag.Parse()
var dinfos []hid.DeviceInfo
for len(dinfos) == 0 {
dinfos = hid.Enumerate(vendorID, productID)
if len(dinfos) == 0 {
fmt.Println("No devices found, waiting 5 seconds")
time.Sleep(5 * time.Second)
}
}
var devices []*hid.Device
for _, di := range dinfos {
d, err := di.Open()
if err != nil {
fmt.Println(err)
continue
}
devices = append(devices, d)
}
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/metrics", http.StatusMovedPermanently)
})
mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
for _, d := range devices {
for _, f := range features {
data := make([]byte, f.length+1)
data[0] = f.reportID
_, err := d.GetFeatureReport(data)
if err != nil {
fmt.Println(err)
continue
}
f.format(data[1:], d, f.name, w)
}
}
})
s := http.Server{
Addr: addr,
Handler: mux,
}
fmt.Println("Starting Prometheus exporter on", addr)
if err := s.ListenAndServe(); err != nil {
fmt.Printf("Unable to run HTTP server: %v", err)
os.Exit(1)
}
}