/
power.go
193 lines (168 loc) · 8.87 KB
/
power.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
package collector
import (
"encoding/xml"
"fmt"
"strings"
"github.com/Juniper/go-netconf/netconf"
"github.com/prometheus/client_golang/prometheus"
)
var (
powerSubsystem = "power"
powerLabel = []string{"module"}
powerModuleZoneLabel = []string{"module", "zone"}
powerZoneLabels = []string{"zone"}
powerDesc = map[string]*prometheus.Desc{
"State": colPromDesc(powerSubsystem, "module_state", "Module Power State (1 = Online, 0 = Offline).", powerLabel),
"CapacityActual": colPromDesc(powerSubsystem, "module_capacity_actual_watts", "Module Actual Capacity in Watts", powerLabel),
"CapacityMax": colPromDesc(powerSubsystem, "module_capacity_maximum_watts", "Module Maximum Capacity in Watts", powerLabel),
"ACInputState": colPromDesc(powerSubsystem, "module_ac_input_state", "Module AC Input State (1 = OK, 0 = Not OK).", powerLabel),
"ACExpectedFeeds": colPromDesc(powerSubsystem, "module_ac_input_expected_feeds", "Module AC Input Expected Feeds.", powerLabel),
"ACConnectedFeeds": colPromDesc(powerSubsystem, "module_ac_input_connected_feeds", "Module AC Input Connected Feeds.", powerLabel),
"DCOutputPower": colPromDesc(powerSubsystem, "module_dc_output_watts", "Module DC Output Watts (Power).", powerModuleZoneLabel),
"DCOutputCurrent": colPromDesc(powerSubsystem, "module_dc_output_amperes", "Module DC Output Amps (Current).", powerModuleZoneLabel),
"DCOutputVoltage": colPromDesc(powerSubsystem, "module_dc_output_volts", "Module DC Output Volts (Voltage).", powerModuleZoneLabel),
"DCOutputLoad": colPromDesc(powerSubsystem, "module_dc_output_load_ratio", "Module DC Output Load as a Percent.", powerModuleZoneLabel),
"CapacityZoneActual": colPromDesc(powerSubsystem, "system_zone_capacity_actual_watts", "System Zone Actual Capacity in Watts", powerZoneLabels),
"CapacityZoneMax": colPromDesc(powerSubsystem, "system_zone_capacity_maximum_watts", "System Zone Maximum Capacity in Watts", powerZoneLabels),
"CapacityZoneAllocated": colPromDesc(powerSubsystem, "system_zone_allocated_watts", "System Zone Allocated Capacity in Watts", powerZoneLabels),
"CapacityZoneRemaining": colPromDesc(powerSubsystem, "system_zone_remaining_watts", "System Zone Remaining Capacity in Watts", powerZoneLabels),
"CapacityZoneUsage": colPromDesc(powerSubsystem, "system_zone_usage_watts", "System Zone Usage in Watts", powerZoneLabels),
"CapacitySysActual": colPromDesc(powerSubsystem, "system_capacity_actual_watts", "System Actual Capacity in Watts", nil),
"CapacitySysMax": colPromDesc(powerSubsystem, "system_capacity_maximum_watts", "System Maximum Capacity in Watts", nil),
"CapacitySysRemaining": colPromDesc(powerSubsystem, "system_remaining_watts", "System Remaining Capacity in Watts", nil),
"DCUsage": colPromDesc(powerSubsystem, "module_dc_usage_watts", "Module DC Usage in Watts.", powerLabel),
}
totalPowerErrors = 0.0
)
// PowerCollector collects power metrics, implemented as per the Collector interface.
type PowerCollector struct{}
// NewPowerCollector returns a new PowerCollector .
func NewPowerCollector() *PowerCollector {
return &PowerCollector{}
}
// Name of the collector.
func (*PowerCollector) Name() string {
return powerSubsystem
}
// Get metrics and send to the Prometheus.Metric channel.
func (c *PowerCollector) Get(ch chan<- prometheus.Metric, conf Config) ([]error, float64) {
errors := []error{}
s, err := netconf.DialSSH(conf.SSHTarget, conf.SSHClientConfig)
if err != nil {
totalPowerErrors++
errors = append(errors, fmt.Errorf("could not connect to %q: %s", conf.SSHTarget, err))
return errors, totalPowerErrors
}
defer s.Close()
reply, err := s.Exec(netconf.RawMethod(`<get-power-usage-information-detail></get-power-usage-information-detail>`))
if err != nil {
totalPowerErrors++
errors = append(errors, fmt.Errorf("could not execute netconf RPC call: %s", err))
return errors, totalPowerErrors
}
if err := processPowerNetconfReply(reply, ch); err != nil {
totalPowerErrors++
errors = append(errors, err)
}
return errors, totalPowerErrors
}
func processPowerNetconfReply(reply *netconf.RPCReply, ch chan<- prometheus.Metric) error {
var netconfReply powerRPCReply
if err := xml.Unmarshal([]byte(reply.RawReply), &netconfReply); err != nil {
return fmt.Errorf("could not unmarshal netconf reply xml: %s", err)
}
for _, powerData := range netconfReply.PowerUsageInformation.PowerUsageItem {
labels := []string{strings.TrimSpace(powerData.Name.Text)}
powerState := 0.0
if powerData.State.Text == "Online" {
powerState = 1.0
}
ch <- prometheus.MustNewConstMetric(powerDesc["State"], prometheus.GaugeValue, powerState, labels...)
newGauge(ch, powerDesc["CapacityActual"], powerData.PemCapacityDetail.CapacityActual.Text, labels...)
newGauge(ch, powerDesc["CapacityMax"], powerData.PemCapacityDetail.CapacityMax.Text, labels...)
powerACInputState := 0.0
if powerData.AcInputDetail.AcInput.Text == "OK" {
powerACInputState = 1.0
}
ch <- prometheus.MustNewConstMetric(powerDesc["ACInputState"], prometheus.GaugeValue, powerACInputState, labels...)
newGauge(ch, powerDesc["ACExpectedFeeds"], powerData.AcInputDetail.AcExpectFeed.Text, labels...)
newGauge(ch, powerDesc["ACConnectedFeeds"], powerData.AcInputDetail.AcActualFeed.Text, labels...)
labelsDCOutput := []string{strings.TrimSpace(powerData.Name.Text), strings.TrimSpace(powerData.DcOutputDetail.Zone.Text)}
newGauge(ch, powerDesc["DCOutputPower"], powerData.DcOutputDetail.DcPower.Text, labelsDCOutput...)
newGauge(ch, powerDesc["DCOutputCurrent"], powerData.DcOutputDetail.DcCurrent.Text, labelsDCOutput...)
newGauge(ch, powerDesc["DCOutputVoltage"], powerData.DcOutputDetail.DcVoltage.Text, labelsDCOutput...)
newGauge(ch, powerDesc["DCOutputLoad"], powerData.DcOutputDetail.DcLoad.Text, labelsDCOutput...)
}
for _, powerSystem := range netconfReply.PowerUsageInformation.PowerUsageSystem {
for _, zone := range powerSystem.PowerUsageZoneInformation {
labels := []string{strings.TrimSpace(zone.Zone.Text)}
newGauge(ch, powerDesc["CapacityZoneActual"], zone.CapacityActual.Text, labels...)
newGauge(ch, powerDesc["CapacityZoneMax"], zone.CapacityMax.Text, labels...)
newGauge(ch, powerDesc["CapacityZoneAllocated"], zone.CapacityAllocated.Text, labels...)
newGauge(ch, powerDesc["CapacityZoneRemaining"], zone.CapacityRemaining.Text, labels...)
newGauge(ch, powerDesc["CapacityZoneUsage"], zone.CapacityActualUsage.Text, labels...)
}
newGauge(ch, powerDesc["CapacitySysActual"], powerSystem.CapacitySysActual.Text)
newGauge(ch, powerDesc["CapacitySysMax"], powerSystem.CapacitySysMax.Text)
newGauge(ch, powerDesc["CapacitySysRemaining"], powerSystem.CapacitySysRemaining.Text)
}
for _, fruItem := range netconfReply.PowerUsageInformation.PowerUsageFruItem {
newGauge(ch, powerDesc["DCUsage"], fruItem.DcPower.Text, fruItem.Name.Text)
}
return nil
}
type powerRPCReply struct {
PowerUsageInformation powerInformation `xml:"power-usage-information"`
}
type powerInformation struct {
PowerUsageItem []powerItem `xml:"power-usage-item"`
PowerUsageSystem []powerSystem `xml:"power-usage-system"`
PowerUsageFruItem []powerFRUItem `xml:"power-usage-fru-item"`
}
type powerFRUItem struct {
Name powerText `xml:"name"`
DcPower powerText `xml:"dc-power"`
}
type powerSystem struct {
PowerUsageZoneInformation []powerZone `xml:"power-usage-zone-information"`
CapacitySysActual powerText `xml:"capacity-sys-actual"`
CapacitySysMax powerText `xml:"capacity-sys-max"`
CapacitySysRemaining powerText `xml:"capacity-sys-remaining"`
}
type powerZone struct {
Zone powerText `xml:"zone"`
CapacityActual powerText `xml:"capacity-actual"`
CapacityMax powerText `xml:"capacity-max"`
CapacityAllocated powerText `xml:"capacity-allocated"`
CapacityRemaining powerText `xml:"capacity-remaining"`
CapacityActualUsage powerText `xml:"capacity-actual-usage"`
}
type powerItem struct {
Name powerText `xml:"name"`
State powerText `xml:"state"`
PemCapacityDetail powerPEMCapacity `xml:"pem-capacity-detail"`
AcInputDetail powerACInput `xml:"ac-input-detail"`
DcOutputDetail powerDCOutput `xml:"dc-output-detail"`
}
type powerDCOutput struct {
DcPower powerText `xml:"dc-power"`
Zone powerText `xml:"zone"`
DcCurrent powerText `xml:"dc-current"`
DcVoltage powerText `xml:"dc-voltage"`
DcLoad powerText `xml:"dc-load"`
}
type powerACInput struct {
AcInput powerText `xml:"ac-input"`
AcExpectFeed powerText `xml:"ac-expect-feed"`
AcActualFeed powerText `xml:"ac-actual-feed"`
}
type powerPEMCapacity struct {
CapacityActual powerText `xml:"capacity-actual"`
CapacityMax powerText `xml:"capacity-max"`
}
type powerText struct {
Text string `xml:",chardata"`
}
type powerTemp struct {
Temp string `xml:"celsius,attr"`
}