-
Notifications
You must be signed in to change notification settings - Fork 0
/
enphase-prometheus.go
362 lines (323 loc) · 10 KB
/
enphase-prometheus.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package main
import (
"bufio"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"reflect"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
arrayLocations map[string]geo
registry *prometheus.Registry
reportedWatts = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "reported_watts",
Help: "watts reported by individual inverters.",
}, []string{"serial_number", "x", "y"})
totalWatts = promauto.NewGauge(prometheus.GaugeOpts{
Name: "total_watts",
Help: "total watts reported by the system.",
})
wattHoursToday = promauto.NewGauge(prometheus.GaugeOpts{
Name: "watt_hours_today",
Help: "total watt hours today",
})
wattHoursSevenDays = promauto.NewGauge(prometheus.GaugeOpts{
Name: "watt_hours_seven_days",
Help: "total watt hours past seven days",
})
wattHoursLifetime = promauto.NewGauge(prometheus.GaugeOpts{
Name: "watt_hours_lifetime",
Help: "watt hours produced over the lifetime of this device",
})
p = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "active_power",
Help: "active power reported by the meter, in watts.",
}, []string{"type", "phase"})
q = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "reactive_power",
Help: "reactive power reported by the meter, in watts.",
}, []string{"type", "phase"})
s = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "apparent_power",
Help: "apparent power reported by the meter, in watts.",
}, []string{"type", "phase"})
v = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "voltage",
Help: "voltage reported by the meter, in volts.",
}, []string{"type", "phase"})
i = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "amperage",
Help: "current reported by the meter, in amperes.",
}, []string{"type", "phase"})
f = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "frequency",
Help: "frequency reported by the meter, in hertz.",
}, []string{"type", "phase"})
pf = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "power_factor_ratio",
Help: "the power factor ratio of the meter.",
}, []string{"type", "phase"})
)
type production struct {
WattHoursToday int `json:"wattHoursToday"`
WattHoursSevenDays int `json:"wattHoursSevenDays"`
WattHoursLifetime int `json:"wattHoursLifetime"`
WattsNow int `json:"wattsNow"`
}
type inverter struct {
SerialNumber string `json:"serialNumber"`
LastReportDate int `json:"lastReportDate"`
DevType int `json:"devType"`
LastReportWatts int `json:"lastReportWatts"`
MaxReportWatts int `json:"maxReportWatts"`
}
// Generated with https://mholt.github.io/json-to-go/
type arrayLayout struct {
SystemID int `json:"system_id"`
Rotation int `json:"rotation"`
Dimensions struct {
XMin int `json:"x_min"`
XMax int `json:"x_max"`
YMin int `json:"y_min"`
YMax int `json:"y_max"`
} `json:"dimensions"`
Arrays []struct {
ArrayID int `json:"array_id"`
Label string `json:"label"`
X int `json:"x"`
Y int `json:"y"`
Azimuth int `json:"azimuth"`
Modules []struct {
ModuleID int `json:"module_id"`
Rotation int `json:"rotation"`
X int `json:"x"`
Y int `json:"y"`
Inverter struct {
InverterID int `json:"inverter_id"`
SerialNum string `json:"serial_num"`
} `json:"inverter"`
} `json:"modules"`
Dimensions struct {
XMin int `json:"x_min"`
XMax int `json:"x_max"`
YMin int `json:"y_min"`
YMax int `json:"y_max"`
} `json:"dimensions"`
Tilt int `json:"tilt"`
ArrayTypeName string `json:"array_type_name"`
PcuCount int `json:"pcu_count"`
PvModuleDetails struct {
Manufacturer string `json:"manufacturer"`
Model string `json:"model"`
Type interface{} `json:"type"`
PowerRating interface{} `json:"power_rating"`
} `json:"pv_module_details"`
} `json:"arrays"`
Haiku string `json:"haiku"`
}
type geo struct {
X int
Y int
}
type phase struct {
P float64 `json:"p"`
Q float64 `json:"q"`
S float64 `json:"s"`
V float64 `json:"v"`
I float64 `json:"i"`
Pf float64 `json:"pf"`
F float64 `json:"f"`
}
type threePhase struct {
PhA phase `json:"ph-a"`
PhB phase `json:"ph-b"`
PhC phase `json:"ph-c"`
}
func getInverterJSON() ([]byte, error) {
log.Println("Getting system json from " + os.Getenv("ENVOY_URL") + "/api/v1/production/inverters")
client := &http.Client{
Timeout: time.Second * 3600,
Transport: &bearerAuthTransport{
Token: os.Getenv("AUTH_TOKEN"),
},
}
resp, err := client.Get(os.Getenv("ENVOY_URL") + "/api/v1/production/inverters")
if err != nil {
return []byte("[]"), err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
checkErr(err)
return body, nil
}
func getSystemJSON() ([]byte, error) {
log.Println("Getting system json from " + os.Getenv("ENVOY_URL") + "/api/v1/production")
client := &http.Client{
Timeout: time.Second * 3600,
Transport: &bearerAuthTransport{
Token: os.Getenv("AUTH_TOKEN"),
},
}
resp, err := client.Get(os.Getenv("ENVOY_URL") + "/api/v1/production")
checkErr(err)
if resp.StatusCode != http.StatusOK {
return []byte("[]"), fmt.Errorf("received http status %d", resp.StatusCode)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
checkErr(err)
return body, nil
}
func streams() {
log.Println("Initializing stream.")
registry.MustRegister(p)
registry.MustRegister(q)
registry.MustRegister(s)
registry.MustRegister(v)
registry.MustRegister(i)
registry.MustRegister(pf)
registry.MustRegister(f)
var gauges []*prometheus.GaugeVec = []*prometheus.GaugeVec{p, q, s, v, i, pf, f}
go func() {
client := &http.Client{
Timeout: time.Second * 3600,
Transport: &bearerAuthTransport{
Token: os.Getenv("AUTH_TOKEN"),
},
}
retries := 1
for {
log.Println("Reading from stream.")
resp, err := client.Get(os.Getenv("ENVOY_URL") + "/stream/meter")
if err == nil {
retries = 1
reader := bufio.NewReader(resp.Body)
var stream map[string]threePhase
line, err := reader.ReadBytes('\n')
for err == nil {
if len(line) > 2 {
line = line[6:]
json.Unmarshal(line, &stream)
for phaseType := range stream {
for i, gauge := range gauges {
vA := reflect.ValueOf(stream[phaseType].PhA)
(*gauge).With(prometheus.Labels{"type": phaseType, "phase": "ph-a"}).Set(vA.Field(i).Interface().(float64))
vB := reflect.ValueOf(stream[phaseType].PhB)
(*gauge).With(prometheus.Labels{"type": phaseType, "phase": "ph-b"}).Set(vB.Field(i).Interface().(float64))
vC := reflect.ValueOf(stream[phaseType].PhC)
(*gauge).With(prometheus.Labels{"type": phaseType, "phase": "ph-c"}).Set(vC.Field(i).Interface().(float64))
}
}
}
line, err = reader.ReadBytes('\n')
}
} else {
log.Println("Error reading from stream.")
log.Println(err.Error())
retries *= 2
time.Sleep(time.Duration(retries) * 100 * time.Millisecond)
if retries > 300 {
retries = 300
}
}
}
}()
}
func metrics() {
log.Println("Initializing metrics.")
if os.Getenv("ARRAY_LAYOUT") != "" {
log.Println("Reading layout information.")
arrayLocations = make(map[string]geo)
var arrayDefinition arrayLayout
json.Unmarshal([]byte(os.Getenv("ARRAY_LAYOUT")), &arrayDefinition)
for _, solarArray := range arrayDefinition.Arrays {
for _, module := range solarArray.Modules {
arrayLocations[module.Inverter.SerialNum] = geo{
X: module.X,
Y: module.Y,
}
}
}
}
registry.MustRegister(reportedWatts)
registry.MustRegister(totalWatts)
registry.MustRegister(wattHoursToday)
registry.MustRegister(wattHoursSevenDays)
registry.MustRegister(wattHoursLifetime)
go func() {
for {
log.Println("Retrieving metrics.")
inverterJSON, _ := getInverterJSON()
var inverters []inverter
json.Unmarshal(inverterJSON, &inverters)
log.Println("Received data from", len(inverters), "inverters.")
for _, inverter := range inverters {
if val, ok := arrayLocations[inverter.SerialNumber]; ok {
reportedWatts.With(prometheus.Labels{"serial_number": inverter.SerialNumber, "x": strconv.Itoa(val.X), "y": strconv.Itoa(val.Y)}).Set(float64(inverter.LastReportWatts))
} else {
reportedWatts.With(prometheus.Labels{"serial_number": inverter.SerialNumber, "x": "0", "y": "0"}).Set(float64(inverter.LastReportWatts))
}
}
systemJSON, err := getSystemJSON()
if err == nil {
var system production
json.Unmarshal(systemJSON, &system)
log.Println("Received system data, current total watts is", system.WattsNow)
totalWatts.Set(float64(system.WattsNow))
wattHoursSevenDays.Set(float64(system.WattHoursSevenDays))
wattHoursLifetime.Set(float64(system.WattHoursLifetime))
wattHoursToday.Set(float64(system.WattHoursToday))
} else {
totalWatts.Set(float64(0))
log.Println("Error retrieving system data.")
}
sleep := 10
if os.Getenv("SLEEP_SECONDS") != "" {
sleep, err = strconv.Atoi(os.Getenv("SLEEP_SECONDS"))
checkErr(err)
}
time.Sleep(time.Duration(sleep) * time.Second)
}
}()
}
func main() {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
http.Handle("/metrics", initPrometheus())
metrics()
streams()
port := "80"
if os.Getenv("PORT") != "" {
port = os.Getenv("PORT")
}
err := http.ListenAndServe(":"+port, nil)
checkErr(err)
log.Println("Server ready to serve.")
}
func checkErr(err error) {
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
}
func initPrometheus() http.Handler {
registry = prometheus.NewRegistry()
return promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
}
// bearerAuthTransport adds the Authorization header to requests
type bearerAuthTransport struct {
Token string
}
// RoundTrip executes a single HTTP transaction and adds the Authorization header
func (bat *bearerAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", "Bearer "+bat.Token)
return http.DefaultTransport.RoundTrip(req)
}