Skip to content

Commit da2a4e5

Browse files
Jonher937tamcore
authored andcommitted
Add string reason for the last transfer to battery
1 parent a4bac9c commit da2a4e5

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

upscollector.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type UPSCollector struct {
3434
LastTransferOnBatteryTimeSeconds *prometheus.Desc
3535
LastTransferOffBatteryTimeSeconds *prometheus.Desc
3636
LastSelftestTimeSeconds *prometheus.Desc
37+
LastTransferOnBatteryReason *prometheus.Desc
3738
NominalPowerWatts *prometheus.Desc
3839
InternalTemperatureCelsius *prometheus.Desc
3940

@@ -152,6 +153,13 @@ func NewUPSCollector(ss StatusSource) *UPSCollector {
152153
nil,
153154
),
154155

156+
LastTransferOnBatteryReason: prometheus.NewDesc(
157+
prometheus.BuildFQName(namespace, "", "last_transfer_on_battery_reason"),
158+
"Reason for last transfer to battery.",
159+
[]string{"reason", "ups"},
160+
nil,
161+
),
162+
155163
NominalPowerWatts: prometheus.NewDesc(
156164
prometheus.BuildFQName(namespace, "", "nominal_power_watts"),
157165
"Nominal power output in watts.",
@@ -189,6 +197,7 @@ func (c *UPSCollector) Describe(ch chan<- *prometheus.Desc) {
189197
c.LastTransferOnBatteryTimeSeconds,
190198
c.LastTransferOffBatteryTimeSeconds,
191199
c.LastSelftestTimeSeconds,
200+
c.LastTransferOnBatteryReason,
192201
c.NominalPowerWatts,
193202
c.InternalTemperatureCelsius,
194203
}
@@ -313,6 +322,14 @@ func (c *UPSCollector) Collect(ch chan<- prometheus.Metric) {
313322
s.UPSName,
314323
)
315324

325+
ch <- prometheus.MustNewConstMetric(
326+
c.LastTransferOnBatteryReason,
327+
prometheus.GaugeValue,
328+
1,
329+
s.LastTransfer,
330+
s.UPSName,
331+
)
332+
316333
ch <- prometheus.MustNewConstMetric(
317334
c.NominalPowerWatts,
318335
prometheus.GaugeValue,

upscollector_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func TestUPSCollector(t *testing.T) {
4343
XOnBattery: time.Unix(100001, 0),
4444
XOffBattery: time.Unix(100002, 0),
4545
LastSelftest: time.Unix(100003, 0),
46+
LastTransfer: "Input frequency out of range",
4647
NominalPower: 50.0,
4748
InternalTemp: 26.4,
4849
},
@@ -63,6 +64,7 @@ func TestUPSCollector(t *testing.T) {
6364
regexp.MustCompile(`apcupsd_last_transfer_on_battery_time_seconds{ups="bar"} 100001`),
6465
regexp.MustCompile(`apcupsd_last_transfer_off_battery_time_seconds{ups="bar"} 100002`),
6566
regexp.MustCompile(`apcupsd_last_selftest_time_seconds{ups="bar"} 100003`),
67+
regexp.MustCompile(`apcupsd_last_transfer_on_battery_reason{reason="Input frequency out of range",ups="bar"} 1`),
6668
regexp.MustCompile(`apcupsd_nominal_power_watts{ups="bar"} 50`),
6769
regexp.MustCompile(`apcupsd_internal_temperature_celsius{ups="bar"} 26.4`),
6870
},

0 commit comments

Comments
 (0)