-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
103 lines (80 loc) · 2.52 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
package main
import (
"flag"
"fmt"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
var systemSerialID = ""
var (
listenAddress = flag.String("web.listen-address",
getEnv("LISTEN_ADDR", "127.0.0.1:9226"),
"Address on which to expose metrics and web interface.")
clientPrefix = flag.String("mqtt.client_prefix",
getEnv("MQTT_CLIENT_PREFIX", "victron_exporter"),
"Prefix for MQTT clientID")
pollInterval = flag.Duration("victron.poll_interval",
getDurationEnv("VICTRON_POLL_INTERVAL", 10*time.Second),
"CGX MQTT poll interval")
host = flag.String("mqtt.host",
getEnv("MQTT_HOST", ""),
"CGX IP address or hostname")
port = flag.Int("mqtt.port",
getIntEnv("MQTT_PORT", 8883),
"CGX MQTT PORT")
secure = flag.Bool("mqtt.secure",
getBoolEnv("MQTT_SECURE", true),
"CGX SSL-enabled communication")
username = flag.String("mqtt.username",
getEnv("MQTT_USERNAME", ""),
"Victron MQTT Cloud Username")
password = flag.String("mqtt.password",
getEnv("MQTT_PASSWORD", ""),
"Victron MQTT Cloud Password")
logLevel = flag.Int("log.level",
getIntEnv("LOG_LEVEL", 2),
"Log level: 0=debug, 1=info, 2=warn, 3=error")
)
func main() {
flag.Parse()
setLogLevel(*logLevel)
log.WithField("address", *listenAddress).Info("victron_exporter listening")
http.Handle("/metrics", promhttp.Handler())
go func() {
err := http.ListenAndServe(*listenAddress, nil)
if err != nil {
log.WithField("address", *listenAddress).WithError(err).Fatal("failed to listen on address")
}
}()
mqttOpts := mqttConnectionConfig{*host, *port, *secure, *username, *password}
go func() {
err := listen(*clientPrefix+"_sub", mqttOpts, "#")
if err != nil {
log.WithError(err).Fatal("failed to establish mqtt subscription connection")
}
}()
client, err := connect(*clientPrefix+"_pub", mqttOpts)
if err != nil {
log.WithError(err).Fatal("failed to establish mqtt publish connection")
}
timer := time.NewTicker(*pollInterval)
for range timer.C {
if !client.IsConnectionOpen() {
log.Debug("mqtt connection not yet established")
continue
}
// Check whether we've heard back from victron mqtt yet...
if systemSerialID == "" {
log.Debug("awaiting system serial ID response from Victron mqtt bus")
continue
}
token := client.Publish(fmt.Sprintf("R/%s/system/0/Serial", systemSerialID), 1, false, "")
for !token.WaitTimeout(5 * time.Second) {
if err := token.Error(); err != nil {
log.WithError(err).Error("mqtt publish failed")
}
}
}
}