-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollector.go
364 lines (336 loc) · 9.26 KB
/
collector.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
363
364
package main
import (
"log"
"sync"
"github.com/jens18/gotracer"
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "solar"
)
type solarCollector struct {
mutex sync.Mutex
scrapeFailures prometheus.Counter
panelVoltage *prometheus.Desc
panelCurrent *prometheus.Desc
panelPower *prometheus.Desc
batteryVoltage *prometheus.Desc
batteryCurrent *prometheus.Desc
batterySOC *prometheus.Desc
batteryTemp *prometheus.Desc
batteryMaxVoltage *prometheus.Desc
batteryMinVoltage *prometheus.Desc
deviceTemp *prometheus.Desc
loadActive *prometheus.Desc
loadVoltage *prometheus.Desc
loadCurrent *prometheus.Desc
loadPower *prometheus.Desc
energyConsumedDaily *prometheus.Desc
energyConsumedMonthly *prometheus.Desc
energyConsumedAnnual *prometheus.Desc
energyConsumedTotal *prometheus.Desc
energyGeneratedDaily *prometheus.Desc
energyGeneratedMonthly *prometheus.Desc
energyGeneratedAnnual *prometheus.Desc
energyGeneratedTotal *prometheus.Desc
}
// newSolarCollector makes a new solarCollector objects with fully initialized metrics descriptors
func newSolarCollector() *solarCollector {
return &solarCollector{
scrapeFailures: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "controller_comm_failures_total",
Help: "Number of communications errors while connecting to the solar controller.",
}),
panelVoltage: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "panel_voltage"),
"Solar panel voltage (V).",
nil, // no labels yet
nil,
),
panelCurrent: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "panel_current"),
"Solar panel current (A).",
nil, // no labels yet
nil,
),
panelPower: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "panel_power"),
"Solar panel power (W).",
nil, // no labels yet
nil,
),
batteryVoltage: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "battery_voltage"),
"Battery voltage (V).",
nil, // no labels yet
nil,
),
batteryCurrent: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "battery_current"),
"Battery current (A).",
nil, // no labels yet
nil,
),
batterySOC: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "battery_soc"),
"Battery State of Charge (%).",
nil, // no labels yet
nil,
),
batteryTemp: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "battery_temp"),
"Battery temperature (external sensor) (Celcius).",
nil, // no labels yet
nil,
),
batteryMaxVoltage: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "battery_max_voltage"),
"Maximum battery voltage (V).",
nil, // no labels yet
nil,
),
batteryMinVoltage: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "battery_min_voltage"),
"Minimum battery voltage (V).",
nil, // no labels yet
nil,
),
deviceTemp: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "device_temp"),
"Device temperature (controller sensor) (Celcius).",
nil, // no labels yet
nil,
),
loadActive: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "load_active"),
"Load output is active (bool)",
nil, // no labels yet
nil,
),
loadVoltage: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "load_voltage"),
"Load voltage (V).",
nil, // no labels yet
nil,
),
loadCurrent: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "load_current"),
"Load current (A).",
nil, // no labels yet
nil,
),
loadPower: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "load_power"),
"Load power (W).",
nil, // no labels yet
nil,
),
energyConsumedDaily: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "energy_consumed_daily"),
"Controller calculated daily consumption, (kWh)",
nil, // no labels yet
nil,
),
energyConsumedMonthly: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "energy_consumed_monthly"),
"Controller calculated monthly consumption, (kWh)",
nil, // no labels yet
nil,
),
energyConsumedAnnual: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "energy_consumed_annual"),
"Controller calculated annual consumption, (kWh)",
nil, // no labels yet
nil,
),
energyConsumedTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "energy_consumed_taotal"),
"Controller calculated total consumption, (kWh)",
nil, // no labels yet
nil,
),
energyGeneratedDaily: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "energy_generated_daily"),
"Controller calculated daily power generation, (kWh)",
nil, // no labels yet
nil,
),
energyGeneratedMonthly: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "energy_generated_monthly"),
"Controller calculated monthly power generation, (kWh)",
nil, // no labels yet
nil,
),
energyGeneratedAnnual: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "energy_generated_annual"),
"Controller calculated annual power generation, (kWh)",
nil, // no labels yet
nil,
),
energyGeneratedTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "energy_generated_total"),
"Controller calculated total power generation, (kWh)",
nil, // no labels yet
nil,
),
}
}
// Describe sends the descriptors of each metric over to the provided channel.
// The corresponding metric values are sent separately.
func (c *solarCollector) Describe(ch chan<- *prometheus.Desc) {
// Describe the Collector's member that are of type Desc
ds := []*prometheus.Desc{
c.panelVoltage,
}
for _, d := range ds {
ch <- d
}
// Describe the other types
c.scrapeFailures.Describe(ch)
}
// Collect gather the metrics values and sends them.
// The call is protected from concurrent collects with a mutex lock.
func (c *solarCollector) Collect(ch chan<- prometheus.Metric) {
c.mutex.Lock() // To protect metrics from concurrent collects.
defer c.mutex.Unlock()
if err := c.collect(ch); err != nil {
log.Printf("Error getting solar controller data: %s", err)
c.scrapeFailures.Inc()
c.scrapeFailures.Collect(ch)
}
return
}
// collect will execute the actual data collection
func (c *solarCollector) collect(ch chan<- prometheus.Metric) error {
// fetch the status of the controller
tracer, err := gotracer.Status("/dev/ttyUSB0")
if err != nil {
return err
}
/*
* report the collected data
*/
// store boolean values as a float (1 == true, 0 == false)
var loadIsActive float64
// Panel array
ch <- prometheus.MustNewConstMetric(
c.panelVoltage,
prometheus.GaugeValue,
float64(tracer.ArrayVoltage),
)
ch <- prometheus.MustNewConstMetric(
c.panelCurrent,
prometheus.GaugeValue,
float64(tracer.ArrayCurrent),
)
ch <- prometheus.MustNewConstMetric(
c.panelPower,
prometheus.GaugeValue,
float64(tracer.ArrayPower),
)
// Batteries
ch <- prometheus.MustNewConstMetric(
c.batteryCurrent,
prometheus.GaugeValue,
float64(tracer.BatteryCurrent),
)
ch <- prometheus.MustNewConstMetric(
c.batteryVoltage,
prometheus.GaugeValue,
float64(tracer.BatteryVoltage),
)
ch <- prometheus.MustNewConstMetric(
c.batterySOC,
prometheus.GaugeValue,
float64(tracer.BatterySOC),
)
ch <- prometheus.MustNewConstMetric(
c.batteryTemp,
prometheus.GaugeValue,
float64(tracer.BatteryTemp),
)
ch <- prometheus.MustNewConstMetric(
c.batteryMinVoltage,
prometheus.GaugeValue,
float64(tracer.BatteryMinVoltage),
)
ch <- prometheus.MustNewConstMetric(
c.batteryMaxVoltage,
prometheus.GaugeValue,
float64(tracer.BatteryMaxVoltage),
)
// Load output
if tracer.Load {
loadIsActive = 1
}
ch <- prometheus.MustNewConstMetric(
c.loadActive,
prometheus.GaugeValue,
loadIsActive,
)
ch <- prometheus.MustNewConstMetric(
c.loadVoltage,
prometheus.GaugeValue,
float64(tracer.LoadVoltage),
)
ch <- prometheus.MustNewConstMetric(
c.loadCurrent,
prometheus.GaugeValue,
float64(tracer.LoadCurrent),
)
ch <- prometheus.MustNewConstMetric(
c.loadPower,
prometheus.GaugeValue,
float64(tracer.LoadPower),
)
// controller infos
ch <- prometheus.MustNewConstMetric(
c.deviceTemp,
prometheus.GaugeValue,
float64(tracer.DeviceTemp),
)
// energy consumed
ch <- prometheus.MustNewConstMetric(
c.energyConsumedDaily,
prometheus.GaugeValue,
float64(tracer.EnergyConsumedDaily),
)
ch <- prometheus.MustNewConstMetric(
c.energyConsumedMonthly,
prometheus.GaugeValue,
float64(tracer.EnergyConsumedMonthly),
)
ch <- prometheus.MustNewConstMetric(
c.energyConsumedAnnual,
prometheus.GaugeValue,
float64(tracer.EnergyConsumedAnnual),
)
ch <- prometheus.MustNewConstMetric(
c.energyConsumedTotal,
prometheus.GaugeValue,
float64(tracer.EnergyConsumedTotal),
)
// energy generated
ch <- prometheus.MustNewConstMetric(
c.energyGeneratedDaily,
prometheus.GaugeValue,
float64(tracer.EnergyGeneratedDaily),
)
ch <- prometheus.MustNewConstMetric(
c.energyGeneratedMonthly,
prometheus.GaugeValue,
float64(tracer.EnergyGeneratedMonthly),
)
ch <- prometheus.MustNewConstMetric(
c.energyGeneratedAnnual,
prometheus.GaugeValue,
float64(tracer.EnergyGeneratedAnnual),
)
ch <- prometheus.MustNewConstMetric(
c.energyGeneratedTotal,
prometheus.GaugeValue,
float64(tracer.EnergyGeneratedTotal),
)
return nil
}