-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
132 lines (127 loc) · 3.48 KB
/
metrics.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
package gomnik
import "github.com/prometheus/client_golang/prometheus"
type Metrics struct {
PVVoltage *prometheus.GaugeVec
PVCurrent *prometheus.GaugeVec
ACCurrent *prometheus.GaugeVec
ACVoltage *prometheus.GaugeVec
ACPower *prometheus.GaugeVec
ACFrequency *prometheus.GaugeVec
EnergyToday prometheus.Gauge
EnergyTotal prometheus.Gauge
HoursTotal prometheus.Gauge
}
func valueOrDefault(v interface{}, s uint) (o float64) {
switch u := v.(type) {
case uint16:
if u == 0xffff {
return -1.0
}
o = float64(u) / float64(s)
case uint32:
if u == 0xffffffff {
return -1.0
}
o = float64(u) / float64(s)
}
return
}
func (m *Metrics) SetFromResponse(r Response) {
m.PVVoltage.WithLabelValues("1").Set(valueOrDefault(r.PVVoltage1, 10))
m.PVVoltage.WithLabelValues("2").Set(valueOrDefault(r.PVVoltage2, 10))
m.PVVoltage.WithLabelValues("3").Set(valueOrDefault(r.PVVoltage3, 10))
m.PVCurrent.WithLabelValues("1").Set(valueOrDefault(r.PVCurrent1, 10))
m.PVCurrent.WithLabelValues("2").Set(valueOrDefault(r.PVCurrent2, 10))
m.PVCurrent.WithLabelValues("3").Set(valueOrDefault(r.PVCurrent3, 10))
m.ACCurrent.WithLabelValues("1").Set(valueOrDefault(r.ACCurrent1, 10))
m.ACCurrent.WithLabelValues("2").Set(valueOrDefault(r.ACCurrent2, 10))
m.ACCurrent.WithLabelValues("3").Set(valueOrDefault(r.ACCurrent3, 10))
m.ACVoltage.WithLabelValues("1").Set(valueOrDefault(r.ACVoltage1, 10))
m.ACVoltage.WithLabelValues("2").Set(valueOrDefault(r.ACVoltage2, 10))
m.ACVoltage.WithLabelValues("3").Set(valueOrDefault(r.ACVoltage3, 10))
m.ACPower.WithLabelValues("1").Set(valueOrDefault(r.ACPower1, 1))
m.ACPower.WithLabelValues("2").Set(valueOrDefault(r.ACPower2, 1))
m.ACPower.WithLabelValues("3").Set(valueOrDefault(r.ACPower3, 1))
m.ACFrequency.WithLabelValues("1").Set(valueOrDefault(r.ACFrequency1, 100))
m.ACFrequency.WithLabelValues("2").Set(valueOrDefault(r.ACFrequency2, 100))
m.ACFrequency.WithLabelValues("3").Set(valueOrDefault(r.ACFrequency3, 100))
m.EnergyToday.Set(valueOrDefault(r.EToday, 100))
m.EnergyTotal.Set(valueOrDefault(r.ETotal, 10))
m.HoursTotal.Set(valueOrDefault(r.HTotal, 1))
return
}
func NewMetrics(r *prometheus.Registry) (m *Metrics) {
m = &Metrics{}
m.PVVoltage = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "omnik_pv_voltage",
Help: "",
},
[]string{
"string",
},
)
m.PVCurrent = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "omnik_pv_current",
Help: "",
},
[]string{
"string",
},
)
m.ACCurrent = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "omnik_ac_current",
Help: "",
},
[]string{
"phase",
},
)
m.ACVoltage = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "omnik_ac_voltage",
Help: "Voltage of AC outputs",
},
[]string{
"phase",
},
)
m.ACPower = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "omnik_ac_power",
Help: "Power of AC outputs",
},
[]string{
"phase",
},
)
m.ACFrequency = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "omnik_ac_frequency",
Help: "",
},
[]string{
"phase",
},
)
m.EnergyToday = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "omnik_energy_today",
Help: "",
})
m.EnergyTotal = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "omnik_energy_total",
Help: "",
})
m.HoursTotal = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "omnik_hours_total",
Help: "",
})
r.MustRegister(
m.PVVoltage,
m.PVCurrent,
m.ACCurrent,
m.ACVoltage,
m.ACFrequency,
m.ACPower,
m.EnergyToday,
m.EnergyTotal,
m.HoursTotal,
)
return
}