-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
225 lines (212 loc) · 6.7 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
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
// apcmetrics - APC UPS metrics exporter for Prometheus
//
// Copyright 2021 Nick Pillitteri
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package apcmetrics
import (
"context"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
func NewApcCollector(client *ApcClient, timeout time.Duration, logger log.Logger) prometheus.Collector {
return &apcCollector{
client: client,
timeout: timeout,
logger: logger,
// These descriptions mostly come from the apcupsd manual.
// http://www.apcupsd.org/manual/manual.html#status-report-fields
info: prometheus.NewDesc(
"apc_info",
"Info about the UPS",
[]string{"hostname", "version", "ups_name", "model", "driver", "ups_mode"},
nil,
),
status: prometheus.NewDesc(
"apc_status",
"Current status of the UPS",
[]string{"status"},
nil,
),
timeLeft: prometheus.NewDesc(
"apc_time_left",
"Remaining runtime left on the batteries in seconds",
nil,
nil,
),
loadPercent: prometheus.NewDesc(
"apc_load_percent",
"Percentage of load capacity",
nil,
nil,
),
chargePercent: prometheus.NewDesc(
"apc_charge_percent",
"Percentage of charge of the batteries",
nil,
nil,
),
lineVoltage: prometheus.NewDesc(
"apc_line_voltage",
"Current line voltage",
nil,
nil,
),
lowTransferVoltage: prometheus.NewDesc(
"apc_low_transfer_voltage",
"Line voltage below which the UPS will switch to batteries",
nil,
nil,
),
highTransferVoltage: prometheus.NewDesc(
"apc_high_transfer_voltage",
"Line voltage above which the UPS will switch to batteries",
nil,
nil,
),
batteryVoltage: prometheus.NewDesc(
"apc_battery_voltage",
"Battery voltage",
nil,
nil,
),
nominalBatteryVoltage: prometheus.NewDesc(
"apc_nominal_battery_voltage",
"Nominal battery voltage",
nil,
nil,
),
nominalInputVoltage: prometheus.NewDesc(
"apc_nominal_input_voltage",
"Nominal input voltage",
nil,
nil,
),
nominalWattage: prometheus.NewDesc(
"apc_nominal_wattage",
"Max power the UPS is designed to supply",
nil,
nil,
),
batteryDate: prometheus.NewDesc(
"apc_battery_date",
"Date the batteries were last replaced as a UNIX timestamp",
nil,
nil,
),
lastTimeOnBattery: prometheus.NewDesc(
"apc_last_time_on_battery",
"Last transfer on to batteries as a UNIX timestamp",
nil,
nil,
),
lastTimeOffBattery: prometheus.NewDesc(
"apc_last_time_off_battery",
"Last transfer off of batteries as a UNIX timestamp",
nil,
nil,
),
lastSelfTest: prometheus.NewDesc(
"apc_last_self_test",
"Last self test as a UNIX timestamp",
nil,
nil,
),
}
}
type apcCollector struct {
client *ApcClient
timeout time.Duration
logger log.Logger
info *prometheus.Desc
status *prometheus.Desc
timeLeft *prometheus.Desc
loadPercent *prometheus.Desc
chargePercent *prometheus.Desc
lineVoltage *prometheus.Desc
lowTransferVoltage *prometheus.Desc
highTransferVoltage *prometheus.Desc
batteryVoltage *prometheus.Desc
nominalBatteryVoltage *prometheus.Desc
nominalInputVoltage *prometheus.Desc
nominalWattage *prometheus.Desc
batteryDate *prometheus.Desc
lastTimeOnBattery *prometheus.Desc
lastTimeOffBattery *prometheus.Desc
lastSelfTest *prometheus.Desc
}
func (a *apcCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- a.info
ch <- a.status
ch <- a.timeLeft
ch <- a.loadPercent
ch <- a.chargePercent
ch <- a.lineVoltage
ch <- a.lowTransferVoltage
ch <- a.highTransferVoltage
ch <- a.batteryVoltage
ch <- a.nominalBatteryVoltage
ch <- a.nominalInputVoltage
ch <- a.nominalWattage
ch <- a.batteryDate
ch <- a.lastTimeOnBattery
ch <- a.lastTimeOffBattery
ch <- a.lastSelfTest
}
func (a *apcCollector) Collect(ch chan<- prometheus.Metric) {
ctx, cancel := context.WithTimeout(context.Background(), a.timeout)
defer cancel()
status, err := a.client.Status(ctx)
if err != nil {
level.Error(a.logger).Log("msg", "unable to determine UPS status", "err", err)
return
}
ch <- prometheus.MustNewConstMetric(
a.info,
prometheus.GaugeValue,
1,
status.Hostname,
status.Version,
status.UpsName,
status.Model,
status.Driver,
status.UpsMode,
)
ch <- prometheus.MustNewConstMetric(a.status, prometheus.GaugeValue, 1, status.Status)
ch <- prometheus.MustNewConstMetric(a.timeLeft, prometheus.GaugeValue, status.TimeLeft.Seconds())
ch <- prometheus.MustNewConstMetric(a.loadPercent, prometheus.GaugeValue, float64(status.LoadPercent))
ch <- prometheus.MustNewConstMetric(a.chargePercent, prometheus.GaugeValue, float64(status.ChargePercent))
ch <- prometheus.MustNewConstMetric(a.lineVoltage, prometheus.GaugeValue, float64(status.LineVoltage))
ch <- prometheus.MustNewConstMetric(a.lowTransferVoltage, prometheus.GaugeValue, float64(status.LowTransferVoltage))
ch <- prometheus.MustNewConstMetric(a.highTransferVoltage, prometheus.GaugeValue, float64(status.HighTransferVoltage))
ch <- prometheus.MustNewConstMetric(a.batteryVoltage, prometheus.GaugeValue, float64(status.BatteryVoltage))
ch <- prometheus.MustNewConstMetric(a.nominalBatteryVoltage, prometheus.GaugeValue, float64(status.NominalBatteryVoltage))
ch <- prometheus.MustNewConstMetric(a.nominalInputVoltage, prometheus.GaugeValue, float64(status.NominalInputVoltage))
ch <- prometheus.MustNewConstMetric(a.nominalWattage, prometheus.GaugeValue, float64(status.NominalWattage))
if !status.BatteryDate.IsZero() {
ch <- prometheus.MustNewConstMetric(a.batteryDate, prometheus.GaugeValue, float64(status.BatteryDate.Unix()))
}
if !status.LastTimeOnBattery.IsZero() {
ch <- prometheus.MustNewConstMetric(a.lastTimeOnBattery, prometheus.GaugeValue, float64(status.LastTimeOnBattery.Unix()))
}
if !status.LastTimeOffBattery.IsZero() {
ch <- prometheus.MustNewConstMetric(a.lastTimeOffBattery, prometheus.GaugeValue, float64(status.LastTimeOffBattery.Unix()))
}
if !status.LastSelfTest.IsZero() {
ch <- prometheus.MustNewConstMetric(a.lastSelfTest, prometheus.GaugeValue, float64(status.LastSelfTest.Unix()))
}
}