From 037eb2f5459631530a93227a66913594f98bbf03 Mon Sep 17 00:00:00 2001 From: Dan Clough Date: Fri, 9 Jun 2023 08:37:19 -0500 Subject: [PATCH] fix(inputs.upsd): Handle float battery.runtime value (#13412) --- plugins/inputs/upsd/upsd.go | 16 +++++++++------- plugins/inputs/upsd/upsd_test.go | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/plugins/inputs/upsd/upsd.go b/plugins/inputs/upsd/upsd.go index d1b9b1005..8e3c55ee6 100644 --- a/plugins/inputs/upsd/upsd.go +++ b/plugins/inputs/upsd/upsd.go @@ -30,8 +30,6 @@ type Upsd struct { ForceFloat bool `toml:"force_float"` Log telegraf.Logger `toml:"-"` - - batteryRuntimeTypeWarningIssued bool } func (*Upsd) SampleConfig() string { @@ -67,10 +65,14 @@ func (u *Upsd) gatherUps(acc telegraf.Accumulator, name string, variables []nut. // For compatibility with the apcupsd plugin's output we map the status string status into a bit-format status := u.mapStatus(metrics, tags) - timeLeftS, ok := metrics["battery.runtime"].(int64) - if !ok && !u.batteryRuntimeTypeWarningIssued { - u.Log.Warnf("'battery.runtime' type is not int64") - u.batteryRuntimeTypeWarningIssued = true + timeLeftS, err := internal.ToFloat64(metrics["battery.runtime"]) + if err != nil { + u.Log.Warnf("'battery.runtime' type is not supported: %w", err) + } + + timeLeftNS, err := internal.ToInt64(timeLeftS * 1_000_000_000) + if err != nil { + u.Log.Warnf("converting 'battery.runtime' to 'time_left_ns' failed: %w", err) } fields := map[string]interface{}{ @@ -80,7 +82,7 @@ func (u *Upsd) gatherUps(acc telegraf.Accumulator, name string, variables []nut. "ups_status": metrics["ups.status"], //Compatibility with apcupsd metrics format - "time_left_ns": timeLeftS * 1_000_000_000, + "time_left_ns": timeLeftNS, } floatValues := map[string]string{ diff --git a/plugins/inputs/upsd/upsd_test.go b/plugins/inputs/upsd/upsd_test.go index 486b40d25..b335f8fa4 100644 --- a/plugins/inputs/upsd/upsd_test.go +++ b/plugins/inputs/upsd/upsd_test.go @@ -245,7 +245,7 @@ VAR fake device.model "Model 12345" VAR fake input.voltage "242.0" VAR fake ups.load "23.0" VAR fake battery.charge "100.0" -VAR fake battery.runtime "600" +VAR fake battery.runtime "600.00" VAR fake output.voltage "230.0" VAR fake battery.voltage "13.4" VAR fake input.voltage.nominal "230.0"