From 3dd888ac7737b658125381acae50ac1d74f27304 Mon Sep 17 00:00:00 2001 From: Dan Clough <14355743+danclough@users.noreply.github.com> Date: Thu, 8 Jun 2023 20:43:08 -0500 Subject: [PATCH 1/2] Treat battery.runtime as possible float value --- plugins/inputs/upsd/upsd.go | 14 +++++++++----- plugins/inputs/upsd/upsd_test.go | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/plugins/inputs/upsd/upsd.go b/plugins/inputs/upsd/upsd.go index d1b9b1005ff04..c150f4885205f 100644 --- a/plugins/inputs/upsd/upsd.go +++ b/plugins/inputs/upsd/upsd.go @@ -67,10 +67,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 +84,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 486b40d25ce99..b335f8fa4aac7 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" From 8fb947f3c697cbd797f849a0801e4f51948ad70b Mon Sep 17 00:00:00 2001 From: Dan Clough <14355743+danclough@users.noreply.github.com> Date: Thu, 8 Jun 2023 20:52:12 -0500 Subject: [PATCH 2/2] Remove unused bool flag --- plugins/inputs/upsd/upsd.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/inputs/upsd/upsd.go b/plugins/inputs/upsd/upsd.go index c150f4885205f..8e3c55ee654ee 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 {