robbiet480 / go.nut

A Golang library for interacting with NUT (Network UPS Tools)
https://godoc.org/github.com/robbiet480/go.nut
MIT License
31 stars 15 forks source link

Errors with spaces in UPS desc #12

Open dacarson opened 6 months ago

dacarson commented 6 months ago

Using this library via nut_exporter, I was getting errors with my UPSs: ts=2024-03-28T15:25:22.903Z caller=nut_collector.go:131 level=error msg="Failure getting the list of UPS devices" err="error reading response: EOF" I tracked it down to getting the list of UPSs. When I have spaces in the description, it seems to be failing. When I changed the spaces to underscores, it worked.

This fails with EOF:

Trying ::1...
Connected to localhost.
Escape character is '^]'.
LIST UPS
BEGIN LIST UPS
UPS hotwater "Hotwater UPS"
UPS pi "Raspberry Pi UPSPlus"
END LIST UPS
Connection closed by foreign host.

This works:

Trying ::1...
Connected to localhost.
Escape character is '^]'.
LIST UPS
BEGIN LIST UPS
UPS hotwater "Hotwater_UPS"
UPS pi "RaspberryPi_UPSPlus"
END LIST UPS
Connection closed by foreign host.
robbiet480 commented 5 months ago

It's breaking because of this line I believe.. I don't actively use the library anymore but happy to accept a PR!

dacarson commented 2 months ago

Thanks for getting back to me.

It looks like your code is working fine for the for loop - however, Go is not a language that I am very familiar with.

If I read the tip-of-tree code correctly... for _, line := range resp { the value for line should be UPS hotwater "Hotwater UPS"

if strings.HasPrefix(line, "UPS ") { this is true...

splitLine := strings.Split(strings.TrimPrefix(line, "UPS "), ``"``) the value for splitLine should be array with 2 elements hotwater, Hotwater UPS

newUPS, err := NewUPS(strings.TrimSuffix(splitLine[0], " "), c) this line only takes the first element, aka hotwater so it should work fine.

In the project that I am getting is: err="error reading response: EOF" so maybe it is within the NewUPS() function?