Sanitize strings from /sys/class/power_supply

Avoid panic on invalid UTF-8 from /sys/class/power_supply by
sanitizing strings parsed from the kernel.
* Add a broken string to the test fixtures.

Fixes: https://github.com/prometheus/node_exporter/issues/1979

Signed-off-by: Ben Kochie <superq@gmail.com>
This commit is contained in:
Ben Kochie 2021-03-02 16:46:47 +01:00
parent dd99513e37
commit 23e5b245a4
No known key found for this signature in database
GPG Key ID: C646B23C9E3245F1
4 changed files with 5 additions and 3 deletions

View File

@ -6,6 +6,7 @@
* [BUGFIX] * [BUGFIX]
* [BUGFIX] Handle errors from disabled PSI subsystem #1983 * [BUGFIX] Handle errors from disabled PSI subsystem #1983
* [BUGFIX] Sanitize strings from /sys/class/power_supply #1984
## 1.1.1 / 2021-02-12 ## 1.1.1 / 2021-02-12

View File

@ -2613,7 +2613,7 @@ node_power_supply_energy_watthour{power_supply="BAT0"} 36.58
# HELP node_power_supply_info info of /sys/class/power_supply/<power_supply>. # HELP node_power_supply_info info of /sys/class/power_supply/<power_supply>.
# TYPE node_power_supply_info gauge # TYPE node_power_supply_info gauge
node_power_supply_info{power_supply="AC",type="Mains"} 1 node_power_supply_info{power_supply="AC",type="Mains"} 1
node_power_supply_info{capacity_level="Normal",manufacturer="LGC",model_name="LNV-45N1",power_supply="BAT0",serial_number="38109",status="Discharging",technology="Li-ion",type="Battery"} 1 node_power_supply_info{capacity_level="Normal",manufacturer="LGC",model_name="LNV-45N1<EFBFBD>",power_supply="BAT0",serial_number="38109",status="Discharging",technology="Li-ion",type="Battery"} 1
# HELP node_power_supply_online online value of /sys/class/power_supply/<power_supply>. # HELP node_power_supply_online online value of /sys/class/power_supply/<power_supply>.
# TYPE node_power_supply_online gauge # TYPE node_power_supply_online gauge
node_power_supply_online{power_supply="AC"} 0 node_power_supply_online{power_supply="AC"} 0

View File

@ -1298,7 +1298,7 @@ Mode: 444
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/class/power_supply/BAT0/model_name Path: sys/class/power_supply/BAT0/model_name
Lines: 1 Lines: 1
LNV-45N1 LNV-45N1タタ
Mode: 444 Mode: 444
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Directory: sys/class/power_supply/BAT0/power Directory: sys/class/power_supply/BAT0/power

View File

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"os" "os"
"regexp" "regexp"
"strings"
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -153,7 +154,7 @@ func (c *powerSupplyClassCollector) Update(ch chan<- prometheus.Metric) error {
} { } {
if value != "" { if value != "" {
keys = append(keys, name) keys = append(keys, name)
values = append(values, value) values = append(values, strings.ToValidUTF8(value, "<22>"))
} }
} }