Skip to content
This repository was archived by the owner on Feb 18, 2026. It is now read-only.

Commit b51b132

Browse files
committed
Make the collector return an error if unable to fetch device list
The collector returned HTTP 200 even in case it occurred while fetching device stats from the box.
1 parent 61e4fa5 commit b51b132

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

collector.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,20 @@ func (fc *fritzCollector) Describe(ch chan<- *prometheus.Desc) {
3737

3838
func (fc *fritzCollector) Collect(ch chan<- prometheus.Metric) {
3939
var err error
40-
41-
fritzClient.Lock()
42-
l, err := fritzClient.List()
43-
fritzClient.Unlock()
40+
l, err := fritzClient.SafeList()
4441

4542
if err != nil {
4643
log.Println("Unable to collect data:", err)
44+
ch <- prometheus.NewInvalidMetric(fc.InfoDesc, err)
45+
ch <- prometheus.NewInvalidMetric(fc.PresentDesc, err)
46+
ch <- prometheus.NewInvalidMetric(fc.TemperatureDesc, err)
47+
ch <- prometheus.NewInvalidMetric(fc.TemperatureOffsetDesc, err)
48+
ch <- prometheus.NewInvalidMetric(fc.EnergyWhDesc, err)
49+
ch <- prometheus.NewInvalidMetric(fc.PowerWDesc, err)
50+
ch <- prometheus.NewInvalidMetric(fc.SwitchState, err)
51+
ch <- prometheus.NewInvalidMetric(fc.SwitchMode, err)
52+
ch <- prometheus.NewInvalidMetric(fc.SwitchBoxLock, err)
53+
ch <- prometheus.NewInvalidMetric(fc.SwitchDeviceLock, err)
4754
return
4855
}
4956

collector_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"net/url"
55
"os"
66
"path"
7-
"sync"
87
"testing"
98

109
"github.com/bpicode/fritzctl/fritz"
@@ -23,10 +22,7 @@ func TestCollector(t *testing.T) {
2322
t.Fatalf("Failed to parse mock server url: %v", err)
2423
}
2524

26-
fritzClient = client{
27-
HomeAuto: fritz.NewHomeAuto(fritz.URL(fbURL)),
28-
Mutex: &sync.Mutex{},
29-
}
25+
fritzClient = NewClient(fritz.URL(fbURL))
3026
fc := NewFritzCollector()
3127

3228
fixture := "test.metrics"

main.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,27 @@ type client struct {
2323
*sync.Mutex
2424
}
2525

26+
func (c *client) SafeLogin() error {
27+
c.Lock()
28+
defer c.Unlock()
29+
return c.Login()
30+
}
31+
32+
func (c *client) SafeList() (*fritz.Devicelist, error) {
33+
c.Lock()
34+
defer c.Unlock()
35+
return c.List()
36+
}
37+
38+
func NewClient(options ...fritz.Option) *client {
39+
return &client{
40+
HomeAuto: fritz.NewHomeAuto(options...),
41+
Mutex: &sync.Mutex{},
42+
}
43+
}
44+
2645
var (
27-
fritzClient client
46+
fritzClient *client
2847
fbURL *url.URL
2948
username = flag.String("username", "", "FRITZ!Box username.")
3049
password = flag.String("password", "", "FRITZ!Box password.")
@@ -74,34 +93,33 @@ func main() {
7493
fritz.Credentials(*username, *password),
7594
fritz.URL(fbURL),
7695
}
96+
7797
if *noVerify {
7898
options = append(options, fritz.SkipTLSVerify())
7999
}
80100

81101
if !*noVerify && len(*certificatePath) > 0 {
82102
crt, err := ioutil.ReadFile(*certificatePath)
83-
if err == nil {
84-
options = append(options, fritz.Certificate(crt))
85-
} else {
86-
log.Fatalf("Unable to read certificate file: %v\n", err)
103+
if err != nil {
104+
log.Fatalln("Unable to read certificate file:", err)
87105
}
106+
options = append(options, fritz.Certificate(crt))
88107
}
89108

90-
fritzClient = client{
91-
HomeAuto: fritz.NewHomeAuto(options...),
92-
Mutex: &sync.Mutex{},
109+
fritzClient = NewClient(options...)
110+
111+
if err := fritzClient.SafeLogin(); err != nil {
112+
log.Fatalln("Login failed:", err)
93113
}
94114

95115
// Refresh login every 10 minutes
96116
go func() {
97117
for {
98-
fritzClient.Lock()
99-
err := fritzClient.Login()
118+
time.Sleep(10 * time.Minute)
119+
err := fritzClient.SafeLogin()
100120
if err != nil {
101121
log.Println("Login refresh failed:", err)
102122
}
103-
fritzClient.Unlock()
104-
time.Sleep(10 * time.Minute)
105123
}
106124
}()
107125

0 commit comments

Comments
 (0)