-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathcollector_ipmi_native.go
258 lines (226 loc) · 7.26 KB
/
collector_ipmi_native.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
// Copyright 2025 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"math"
"strconv"
"github.com/bougou/go-ipmi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus-community/ipmi_exporter/freeipmi"
)
var (
sensorStateNativeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "sensor", "state"),
"Indicates the severity of the state reported by an IPMI sensor (0=nominal, 1=warning, 2=critical, 3=non-recoverable).",
[]string{"id", "name", "type"},
nil,
)
sensorValueNativeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "sensor", "value"),
"Generic data read from an IPMI sensor of unknown type, relying on labels for context.",
[]string{"id", "name", "type"},
nil,
)
fanSpeedRPMNativeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "fan_speed", "rpm"),
"Fan speed in rotations per minute.",
[]string{"id", "name"},
nil,
)
fanSpeedRatioNativeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "fan_speed", "ratio"),
"Fan speed as a proportion of the maximum speed.",
[]string{"id", "name"},
nil,
)
fanSpeedStateNativeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "fan_speed", "state"),
"Reported state of a fan speed sensor (0=nominal, 1=warning, 2=critical, 3=non-recoverable).",
[]string{"id", "name"},
nil,
)
temperatureNativeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "temperature", "celsius"),
"Temperature reading in degree Celsius.",
[]string{"id", "name"},
nil,
)
temperatureStateNativeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "temperature", "state"),
"Reported state of a temperature sensor (0=nominal, 1=warning, 2=critical, 3=non-recoverable).",
[]string{"id", "name"},
nil,
)
voltageNativeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "voltage", "volts"),
"Voltage reading in Volts.",
[]string{"id", "name"},
nil,
)
voltageStateNativeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "voltage", "state"),
"Reported state of a voltage sensor (0=nominal, 1=warning, 2=critical, 3=non-recoverable).",
[]string{"id", "name"},
nil,
)
currentNativeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "current", "amperes"),
"Current reading in Amperes.",
[]string{"id", "name"},
nil,
)
currentStateNativeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "current", "state"),
"Reported state of a current sensor (0=nominal, 1=warning, 2=critical, 3=non-recoverable).",
[]string{"id", "name"},
nil,
)
powerNativeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "power", "watts"),
"Power reading in Watts.",
[]string{"id", "name"},
nil,
)
powerStateNativeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "power", "state"),
"Reported state of a power sensor (0=nominal, 1=warning, 2=critical, 3=non-recoverable).",
[]string{"id", "name"},
nil,
)
)
type IPMINativeCollector struct{}
func (c IPMINativeCollector) Name() CollectorName {
// The name is intentionally the same as the non-native collector
return IPMICollectorName
}
func (c IPMINativeCollector) Cmd() string {
return ""
}
func (c IPMINativeCollector) Args() []string {
return []string{}
}
func (c IPMINativeCollector) Collect(_ freeipmi.Result, ch chan<- prometheus.Metric, target ipmiTarget) (int, error) {
excludeIDs := target.config.ExcludeSensorIDs
targetHost := targetName(target.host)
filter := func(sensor *ipmi.Sensor) bool {
for _, id := range excludeIDs {
if id == int64(sensor.Number) {
return false
}
}
return true
}
client, err := NewNativeClient(target)
if err != nil {
return 0, err
}
res, err := client.GetSensors(context.TODO(), filter)
if err != nil {
return 0, err
}
// results, err := freeipmi.GetSensorData(result, excludeIds)
// if err != nil {
// logger.Error("Failed to collect sensor data", "target", targetHost, "error", err)
// return 0, err
// }
for _, data := range res {
var state float64
switch data.Status() {
case "ok":
state = 0
case "lnc", "unc": // lower/upper non-critical
state = 1
case "lcr", "ucr": // lower/upper critical
state = 2
case "lnr", "unr": // lower/upper non-recoverable
state = 3 // TODO this is new
case "N/A":
state = math.NaN()
default:
logger.Error("Unknown sensor state", "target", targetHost, "state", data.Status())
state = math.NaN()
}
logger.Debug("Got values", "target", targetHost, "data", fmt.Sprintf("%+v", data))
// TODO this could be greatly improved, now that we have structured data available
switch data.SensorUnit.BaseUnit {
case ipmi.SensorUnitType_RPM:
if data.SensorUnit.Percentage {
collectTypedSensorNative(ch, fanSpeedRatioNativeDesc, fanSpeedStateNativeDesc, state, data, 0.01)
} else {
collectTypedSensorNative(ch, fanSpeedRPMNativeDesc, fanSpeedStateNativeDesc, state, data, 1.0)
}
case ipmi.SensorUnitType_DegreesC:
collectTypedSensorNative(ch, temperatureNativeDesc, temperatureStateNativeDesc, state, data, 1.0)
case ipmi.SensorUnitType_Amps:
collectTypedSensorNative(ch, currentNativeDesc, currentStateNativeDesc, state, data, 1.0)
case ipmi.SensorUnitType_Volts:
collectTypedSensorNative(ch, voltageNativeDesc, voltageStateNativeDesc, state, data, 1.0)
case ipmi.SensorUnitType_Watts:
collectTypedSensorNative(ch, powerNativeDesc, powerStateNativeDesc, state, data, 1.0)
default:
collectGenericSensorNative(ch, state, data)
}
}
return 1, nil
}
func (c IPMINativeCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- sensorStateDesc
ch <- sensorValueDesc
ch <- fanSpeedRPMDesc
ch <- fanSpeedRatioDesc
ch <- fanSpeedStateDesc
ch <- temperatureDesc
ch <- temperatureStateDesc
ch <- voltageDesc
ch <- voltageStateDesc
ch <- currentDesc
ch <- currentStateDesc
ch <- powerDesc
ch <- powerStateDesc
}
func collectTypedSensorNative(ch chan<- prometheus.Metric, desc, stateDesc *prometheus.Desc, state float64, data *ipmi.Sensor, scale float64) {
ch <- prometheus.MustNewConstMetric(
desc,
prometheus.GaugeValue,
data.Value*scale,
strconv.FormatInt(int64(data.Number), 10),
data.Name,
)
ch <- prometheus.MustNewConstMetric(
stateDesc,
prometheus.GaugeValue,
state,
strconv.FormatInt(int64(data.Number), 10),
data.Name,
)
}
func collectGenericSensorNative(ch chan<- prometheus.Metric, state float64, data *ipmi.Sensor) {
ch <- prometheus.MustNewConstMetric(
sensorValueNativeDesc,
prometheus.GaugeValue,
data.Value,
strconv.FormatInt(int64(data.Number), 10),
data.Name,
data.SensorType.String(),
)
ch <- prometheus.MustNewConstMetric(
sensorStateNativeDesc,
prometheus.GaugeValue,
state,
strconv.FormatInt(int64(data.Number), 10),
data.Name,
data.SensorType.String(),
)
}