Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(inputs.upsd): handle battery.runtime as float value #13412

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions plugins/inputs/upsd/upsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ type Upsd struct {
ForceFloat bool `toml:"force_float"`

Log telegraf.Logger `toml:"-"`

batteryRuntimeTypeWarningIssued bool
}

func (*Upsd) SampleConfig() string {
Expand Down Expand Up @@ -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{}{
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/upsd/upsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down