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

This commit is contained in:
Dan Clough 2023-06-09 08:37:19 -05:00 committed by GitHub
parent cba7369903
commit 037eb2f545
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 8 deletions

View File

@ -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{

View File

@ -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"