-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
233 lines (190 loc) · 5.82 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"regexp"
"strings"
"sync"
"syscall"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
hapLog "github.com/brutella/hap/log"
"github.com/ryansouza/aranet4-exporter/aranet"
)
const incorrectArgumentExitCode = 2
const bluetoothMacPattern = "^([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}$"
type AranetConfig struct {
ID string
Name string
}
func (a *AranetConfig) Set(s string) error {
splits := strings.Split(s, "=")
if !isValidBluetoothMac(splits[0]) {
return fmt.Errorf("not a valid Bluetooth MAC address")
}
if len(splits) > 2 {
return fmt.Errorf("invalid Aranet device: must be of format {ID} or {ID}={name}")
}
if len(splits) > 1 {
a.Name = splits[1]
} else {
a.Name = splits[0]
}
a.ID = splits[0]
return nil
}
func isValidBluetoothMac(a string) bool {
matched, err := regexp.MatchString(bluetoothMacPattern, a)
if err != nil {
panic(err)
}
return matched
}
var aranets []AranetConfig
var homekit bool
var listen string
var verbose bool
var stateDir string
func init() {
flag.BoolVar(&verbose, "verbose", false, "verbose logging")
flag.StringVar(&listen, "listen", ":9963", "address to expose Prometheus metrics on")
flag.BoolVar(&homekit, "homekit", false, "enable HomeKit support")
flag.StringVar(&stateDir, "state", getDefaultStateDir(), "directory to store persistent state")
flag.Func("device", "monitor an Aranet4 with format {ID} or {ID}={name}. may be specified multiple "+
"times. examples: -device D8:9B:67:AA:BB:CC=bedroom -device D8:9B:67:AA:BB:DD", parseAranet)
flag.Parse()
if len(aranets) == 0 {
fmt.Fprintf(os.Stderr, "Error: -device flag is required.\n\n")
printHelpAndExit()
}
}
// getDefaultStateDir returns a suitable default location for the state that may need to persist between runs of this
// program.
func getDefaultStateDir() string {
// STATE_DIRECTORY may be set by systemd.
if v, ok := os.LookupEnv("STATE_DIRECTORY"); ok {
return v
}
return "./"
}
func printHelpAndExit() {
fmt.Fprintln(os.Stderr, "Usage:")
flag.PrintDefaults()
os.Exit(incorrectArgumentExitCode)
}
func parseAranet(s string) error {
a := AranetConfig{}
if err := a.Set(s); err != nil {
return err
}
aranets = append(aranets, a)
return nil
}
func googleQRCode(uri string) string {
return "https://chart.googleapis.com/chart?cht=qr&chl=" + uri + "&chs=400x400"
}
func serveMetricsHTTP(shutdownContext context.Context, shutdownWait *sync.WaitGroup, reg *prometheus.Registry) {
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
server := http.Server{Addr: listen}
go func() {
<-shutdownContext.Done()
log.Println("Stopping httpserver...")
server.Shutdown(context.Background())
}()
shutdownWait.Add(1)
go func() {
defer shutdownWait.Done()
log.Printf("Prometheus metrics: %s/metrics\n", server.Addr)
err := server.ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("http.Server closed prematurely: %T %v", err, err)
}
log.Println("Stopped httpserver.")
}()
}
func serveHomekitServer(shutdownContext context.Context, shutdownWait *sync.WaitGroup, bridge *aranet.Bridge) {
shutdownWait.Add(1)
go func() {
defer shutdownWait.Done()
log.Printf("Running homekit server with pin: %s\n", bridge.Pin)
log.Printf("Homekit setup QR code: %s\n", googleQRCode(bridge.SetupURI))
e := bridge.Serve(shutdownContext)
log.Printf("Stopped homekit server: %v\n", e)
}()
}
func main() {
shutdownContext, shutdown := context.WithCancel(context.Background())
shutdownWait := sync.WaitGroup{}
seenIds := map[string]bool{}
seenNames := map[string]bool{}
collectedAranets := []aranet.AranetData{}
collectedAccessories := []*aranet.Accessory{}
for _, config := range aranets {
if _, found := seenIds[config.ID]; found {
fmt.Fprintf(os.Stderr, "Error: duplicate Bluetooth IDs are not allowed: %s\n", config.ID)
os.Exit(incorrectArgumentExitCode)
}
if _, found := seenNames[config.Name]; found {
fmt.Fprintf(os.Stderr, "Error: duplicate device names are not allowed: %s\n", config.Name)
os.Exit(incorrectArgumentExitCode)
}
a := aranet.New(shutdownContext, config.ID, config.Name)
seenIds[config.ID] = true
seenNames[config.Name] = true
shutdownWait.Add(1)
go func() {
defer shutdownWait.Done()
a.RunUpdateLoop(verbose)
}()
collectedAranets = append(collectedAranets, a)
collectedAccessories = append(collectedAccessories, a.Accessory())
}
aranetCollector := &aranet.Collector{Aranets: collectedAranets}
var bridge *aranet.Bridge
if homekit {
hapLog.Debug.Enable()
// TODO
bridge = aranet.NewBridge(filepath.Join(stateDir, "homekit"), "12344321", "RNDM", collectedAccessories...)
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
signal.Stop(sigs)
fmt.Println()
log.Printf("Got signal %v, shutting down...\n", strings.ToUpper(sig.String()))
shutdown()
}()
reg := prometheus.NewPedanticRegistry()
reg.MustRegister(
prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
prometheus.NewGoCollector(),
)
reg.MustRegister(aranetCollector)
serveMetricsHTTP(shutdownContext, &shutdownWait, reg)
if bridge != nil {
serveHomekitServer(shutdownContext, &shutdownWait, bridge)
}
waitForShutdown(shutdownContext, &shutdownWait)
}
func waitForShutdown(shutdownContext context.Context, shutdownWait *sync.WaitGroup) {
<-shutdownContext.Done()
shutdownWaitCtx, shutdownComplete := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
go func() {
shutdownWait.Wait()
shutdownComplete()
}()
<-shutdownWaitCtx.Done()
err := shutdownWaitCtx.Err()
if err == context.DeadlineExceeded {
log.Printf("Graceful shutdown did not finish within timeout")
}
}