-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
191 lines (164 loc) · 5.39 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
package main
import (
"errors"
"fmt"
"net/http"
"os"
"time"
"github.com/spf13/viper"
"github.com/loafoe/go-envoy"
"log/slog"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var metricNamePrefix = "envoy_"
var (
registry = prometheus.NewRegistry()
productionWattsNow = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: metricNamePrefix + "production_watts_now",
Help: "Watts being produced now",
}, []string{"gateway"})
productionWattHourLifetime = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: metricNamePrefix + "production_wh_lifetime",
Help: "Watt-hour generated over lifetime",
}, []string{"gateway"})
sessionRefreshes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: metricNamePrefix + "session_refreshes",
Help: "Number of session refreshes during runtime",
}, []string{"gateway"})
sessionUses = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: metricNamePrefix + "session_uses",
Help: "Number of session reuses during runtime",
}, []string{"gateway"})
jwtRefreshes = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: metricNamePrefix + "jwt_refreshes",
Help: "Number of JWT token refreshes during runtime",
}, []string{"gateway"})
inverterLastReportWatts = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: metricNamePrefix + "inverter_last_report_watts",
Help: "Generated watts by inverter",
}, []string{"gateway", "serial"})
)
func init() {
registry.MustRegister(productionWattsNow)
registry.MustRegister(productionWattHourLifetime)
registry.MustRegister(jwtRefreshes)
registry.MustRegister(sessionRefreshes)
registry.MustRegister(sessionUses)
registry.MustRegister(inverterLastReportWatts)
}
func main() {
viper.SetEnvPrefix("envoy")
viper.SetConfigName("envoy")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/envoy/")
viper.SetDefault("address", "https://envoy.local")
viper.SetDefault("listen", "0.0.0.0:8899")
viper.SetDefault("debug", false)
viper.SetDefault("refresh", 20)
if err := viper.ReadInConfig(); err != nil {
var configFileNotFoundError viper.ConfigFileNotFoundError
if errors.As(err, &configFileNotFoundError) {
slog.Info("Config file not found")
}
}
// Logger
programLevel := new(slog.LevelVar)
h := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: programLevel})
slog.SetDefault(slog.New(h))
if viper.GetBool("debug") {
programLevel.Set(slog.LevelDebug)
}
slog.Debug("Settings", "AllSettings", viper.AllSettings())
username := viper.GetString("username")
password := viper.GetString("password")
serial := viper.GetString("serial")
address := viper.GetString("address")
listenAddr := viper.GetString("listen")
jwt := viper.GetString("jwt")
debug := viper.GetBool("debug")
refresh := viper.GetInt("refresh")
if serial == "" { // Discovery via mDNS
discover, err := envoy.Discover()
if err != nil {
slog.Error("Missing serial and failed discovery", "error", err)
os.Exit(2)
}
slog.Info("Using discovered envoy", "envoy_ip", discover.IPV4, "serial", discover.Serial)
serial = discover.Serial
if discover.IPV4 != "<nil>" {
address = fmt.Sprintf("https://%s", discover.IPV4)
}
}
e, err := envoy.NewClient(username, password, serial,
envoy.WithGatewayAddress(address),
envoy.WithDebug(debug),
envoy.WithJWT(jwt),
envoy.WithNotification(¬ification{serial: serial, logger: slog.Default()}))
if err != nil {
slog.Error("Quitting because of error opening envoy", "error", err)
os.Exit(3)
}
go func() {
for {
cr, err := e.CommCheck()
if err != nil {
e.InvalidateSession() // Token expired?
}
if cr != nil {
slog.Info("Found devices", "count", len(*cr))
}
prod, err := e.Production()
if err != nil {
slog.Error("error getting production data", "error", err)
}
if prod != nil && len(prod.Production) > 0 {
productionWattsNow.WithLabelValues(serial).Set(prod.Production[0].WNow)
productionWattHourLifetime.WithLabelValues(serial).Set(prod.Production[0].WhLifetime)
}
inverters, err := e.Inverters()
if err != nil {
slog.Error("error getting inverters data", "error", err)
}
if inverters != nil {
for _, inverter := range *inverters {
inverterLastReportWatts.WithLabelValues(serial, inverter.SerialNumber).Set(float64(inverter.LastReportWatts))
}
}
time.Sleep(time.Duration(refresh) * time.Second)
}
}()
slog.Info("Start listening", slog.String("address", listenAddr))
http.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
err = http.ListenAndServe(listenAddr, nil)
slog.Info("Program exit", "result", err)
}
type notification struct {
logger *slog.Logger
lastSession string
serial string
uses float64
}
func (n *notification) JWTRefreshed(_ string) {
n.logger.Debug("JWT refreshed")
jwtRefreshes.WithLabelValues(n.serial).Inc()
}
func (n *notification) JWTError(err error) {
n.logger.Error("JWT error", "error", err)
}
func (n *notification) SessionRefreshed(s string) {
n.logger.Debug("Session refreshed", "session", s)
sessionRefreshes.WithLabelValues(n.serial).Inc()
}
func (n *notification) SessionUsed(s string) {
if n.lastSession != s {
n.lastSession = s
n.uses = 0
}
n.uses = n.uses + 1
sessionUses.WithLabelValues(n.serial).Set(n.uses)
n.logger.Debug("Session used", "session", s)
}
func (n *notification) SessionError(err error) {
n.logger.Error("session error", "error", err)
}