Skip to content

Commit

Permalink
Fix experimental metrics on Livebox 7 (#17)
Browse files Browse the repository at this point in the history
* Only discover enabled interfaces

* Improve error messages

* netdev: use getSSIDStats for WLAN interfaces
  • Loading branch information
Tomy2e authored Aug 14, 2024
1 parent 4950409 commit 632a088
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 30 deletions.
14 changes: 3 additions & 11 deletions internal/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ package collector

import (
"log"
"reflect"
"time"

"github.com/Tomy2e/livebox-exporter/pkg/reflect"
)

const slowCollectThreshold = 10 * time.Second

func getType(myvar any) string {
t := reflect.TypeOf(myvar)
if t.Kind() == reflect.Ptr {
return t.Elem().Name()
}

return t.Name()
}

func warnOnSlowCollect(collector any, startTime time.Time) {
if ts := time.Since(startTime); ts > slowCollectThreshold {
log.Printf("WARN: Collect was slow (%s) for %s", ts, getType(collector))
log.Printf("WARN: Collect was slow (%s) for %s", ts, reflect.GetType(collector))
}
}
2 changes: 1 addition & 1 deletion internal/poller/interface_homelan.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (im *InterfaceHomeLanMbits) Poll(ctx context.Context) error {
"get",
nil,
), &stats); err != nil {
return err
return fmt.Errorf("failed to get stats for interface: %s: %w", itf.Name, err)
}

counters := &bitrate.Counters{
Expand Down
64 changes: 48 additions & 16 deletions internal/poller/interface_netdev.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,59 @@ func (im *InterfaceNetDevMbits) Collectors() []prometheus.Collector {
}
}

func (im *InterfaceNetDevMbits) getNetDevStats(ctx context.Context, interfaceName string) (uint64, uint64, error) {
var stats struct {
Status struct {
RxBytes uint64
TxBytes uint64
} `json:"status"`
}

if err := im.client.Request(ctx, request.New(
fmt.Sprintf("NeMo.Intf.%s", interfaceName),
"getNetDevStats",
nil,
), &stats); err != nil {
return 0, 0, err
}

return stats.Status.RxBytes, stats.Status.TxBytes, nil
}

func (im *InterfaceNetDevMbits) getSSIDStats(ctx context.Context, interfaceName string) (uint64, uint64, error) {
var stats struct {
Status struct {
BytesReceived uint64
BytesSent uint64
} `json:"status"`
}

if err := im.client.Request(ctx, request.New(
fmt.Sprintf("NeMo.Intf.%s", interfaceName),
"getSSIDStats",
nil,
), &stats); err != nil {
return 0, 0, err
}

return stats.Status.BytesReceived, stats.Status.BytesSent, nil
}

// Poll polls the current bandwidth usage.
func (im *InterfaceNetDevMbits) Poll(ctx context.Context) error {
for _, itf := range im.interfaces {
var stats struct {
Status struct {
RxBytes uint64 `json:"RxBytes"`
TxBytes uint64 `json:"TxBytes"`
} `json:"status"`
}
var (
counters = &bitrate.Counters{}
err error
)

if err := im.client.Request(ctx, request.New(
fmt.Sprintf("NeMo.Intf.%s", itf.Name),
"getNetDevStats",
nil,
), &stats); err != nil {
return err
if itf.IsWLAN() {
counters.Rx, counters.Tx, err = im.getSSIDStats(ctx, itf.Name)
} else {
counters.Rx, counters.Tx, err = im.getNetDevStats(ctx, itf.Name)
}

counters := &bitrate.Counters{
Tx: stats.Status.TxBytes,
Rx: stats.Status.RxBytes,
if err != nil {
return fmt.Errorf("failed to get stats for interface (WLAN=%t): %s: %w", itf.IsWLAN(), itf.Name, err)
}

if !itf.IsWAN() {
Expand Down
8 changes: 7 additions & 1 deletion internal/poller/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package poller

import (
"context"
"fmt"

"github.com/Tomy2e/livebox-exporter/pkg/reflect"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -33,7 +35,11 @@ func (p Pollers) Poll(ctx context.Context) error {
for _, poller := range p {
poller := poller
eg.Go(func() error {
return poller.Poll(ctx)
if err := poller.Poll(ctx); err != nil {
return fmt.Errorf("%s: %w", reflect.GetType(poller), err)
}

return nil
})
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/livebox/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func DiscoverInterfaces(ctx context.Context, client *livebox.Client) ([]*Interfa
ctx,
request.New("NeMo.Intf.data", "getMIBs", map[string]interface{}{
"traverse": "all",
"flag": "statmon && !vlan",
// Only discover enabled interfaces: https://github.com/Tomy2e/livebox-exporter/issues/15
"flag": "statmon && !vlan && enabled",
}),
&mibs,
); err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/reflect/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package reflect

import "reflect"

func GetType(myvar any) string {
t := reflect.TypeOf(myvar)
if t.Kind() == reflect.Ptr {
return t.Elem().Name()
}

return t.Name()
}

0 comments on commit 632a088

Please sign in to comment.