forked from suprememoocow/victron-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (71 loc) · 2.03 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
package main
import (
"flag"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
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: *host,
port: *port,
secure: *secure,
username: *username,
password: *password,
}
client, err := newVictronClient(*clientPrefix, *pollInterval, mqttOpts)
if err != nil {
log.WithError(err).Fatal("failed to establish mqtt connection")
}
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
go func() {
time.Sleep(10 * time.Second)
log.Fatal("graceful shutdown timed out")
}()
client.Close()
}