From 8215e950e97555bc0f77c8968cea5c06fa32f4ea Mon Sep 17 00:00:00 2001 From: Tomy Guichard Date: Fri, 2 Jun 2023 00:42:35 +0200 Subject: [PATCH] Allow using a custom livebox address --- README.md | 8 ++-- go.mod | 2 +- go.sum | 4 +- internal/collector/deviceinfo.go | 4 +- internal/poller/devices.go | 4 +- internal/poller/interface.go | 4 +- internal/poller/interface_homelan.go | 4 +- internal/poller/interface_netdev.go | 4 +- internal/poller/wan.go | 4 +- main.go | 60 +++++++++++++++++++++++++++- pkg/livebox/discovery.go | 2 +- 11 files changed, 79 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 8464fd9..fbd7a39 100644 --- a/README.md +++ b/README.md @@ -40,9 +40,11 @@ The exporter accepts the following command-line options: The exporter reads the following environment variables: -| Name | Description | Default value | -| -------------- | --------------------------------------------------------------------------------------------------------- | ------------- | -| ADMIN_PASSWORD | Password of the Livebox "admin" user. The exporter will exit if this environment variable is not defined. | | +| Name | Description | Default value | +| --------------- | --------------------------------------------------------------------------------------------------------- | -------------------- | +| ADMIN_PASSWORD | Password of the Livebox "admin" user. The exporter will exit if this environment variable is not defined. | | +| LIVEBOX_ADDRESS | Address of the Livebox. | `http://192.168.1.1` | +| LIVEBOX_CACERT | Optional path to a PEM-encoded CA certificate file on the local disk. | | ### Docker diff --git a/go.mod b/go.mod index f497aca..1929fff 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/Tomy2e/livebox-exporter go 1.18 require ( - github.com/Tomy2e/livebox-api-client v0.0.0-20230304114924-a629a6a185e7 + github.com/Tomy2e/livebox-api-client v0.0.0-20230524112450-31caca47cbd8 github.com/prometheus/client_golang v1.14.0 golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 golang.org/x/sync v0.1.0 diff --git a/go.sum b/go.sum index acacb92..ada3d6e 100644 --- a/go.sum +++ b/go.sum @@ -33,8 +33,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/Tomy2e/livebox-api-client v0.0.0-20230304114924-a629a6a185e7 h1:aX04myQJxWqjP1I1S8jiE8fKyXSu4CHKMBOCUMTwlCI= -github.com/Tomy2e/livebox-api-client v0.0.0-20230304114924-a629a6a185e7/go.mod h1:3uvJQHRP5V3BKPTzWCOmUMxZrPbJNl45Wu7ueX9L8QM= +github.com/Tomy2e/livebox-api-client v0.0.0-20230524112450-31caca47cbd8 h1:rVK1emWX3JW3nNhLdl76M4RJgmMj/hcgQVOJmrh2r9Y= +github.com/Tomy2e/livebox-api-client v0.0.0-20230524112450-31caca47cbd8/go.mod h1:3uvJQHRP5V3BKPTzWCOmUMxZrPbJNl45Wu7ueX9L8QM= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= diff --git a/internal/collector/deviceinfo.go b/internal/collector/deviceinfo.go index e317cfc..37827d8 100644 --- a/internal/collector/deviceinfo.go +++ b/internal/collector/deviceinfo.go @@ -14,7 +14,7 @@ var _ prometheus.Collector = &DeviceInfo{} // DeviceInfo implements a prometheus Collector that returns Livebox specific metrics. type DeviceInfo struct { - client livebox.Client + client *livebox.Client numberOfRebootsMetric *prometheus.Desc uptimeMetric *prometheus.Desc memoryTotalMetric *prometheus.Desc @@ -22,7 +22,7 @@ type DeviceInfo struct { } // NewDeviceInfo returns a new DeviceInfo collector using the specified client. -func NewDeviceInfo(client livebox.Client) *DeviceInfo { +func NewDeviceInfo(client *livebox.Client) *DeviceInfo { return &DeviceInfo{ client: client, numberOfRebootsMetric: prometheus.NewDesc( diff --git a/internal/poller/devices.go b/internal/poller/devices.go index a244969..5794118 100644 --- a/internal/poller/devices.go +++ b/internal/poller/devices.go @@ -13,12 +13,12 @@ var _ Poller = &DevicesTotal{} // DevicesTotal allows to poll the total number of active devices. type DevicesTotal struct { - client livebox.Client + client *livebox.Client devicesTotal *prometheus.GaugeVec } // NewDevicesTotal returns a new DevicesTotal poller. -func NewDevicesTotal(client livebox.Client) *DevicesTotal { +func NewDevicesTotal(client *livebox.Client) *DevicesTotal { return &DevicesTotal{ client: client, devicesTotal: prometheus.NewGaugeVec(prometheus.GaugeOpts{ diff --git a/internal/poller/interface.go b/internal/poller/interface.go index 03c152e..824587a 100644 --- a/internal/poller/interface.go +++ b/internal/poller/interface.go @@ -15,12 +15,12 @@ var _ Poller = &InterfaceMbits{} // InterfaceMbits allows to poll the current bandwidth usage on the Livebox // interfaces. type InterfaceMbits struct { - client livebox.Client + client *livebox.Client txMbits, rxMbits *prometheus.GaugeVec } // NewInterfaceMbits returns a new InterfaceMbits poller. -func NewInterfaceMbits(client livebox.Client) *InterfaceMbits { +func NewInterfaceMbits(client *livebox.Client) *InterfaceMbits { return &InterfaceMbits{ client: client, txMbits: prometheus.NewGaugeVec(prometheus.GaugeOpts{ diff --git a/internal/poller/interface_homelan.go b/internal/poller/interface_homelan.go index 02e882b..368efd9 100644 --- a/internal/poller/interface_homelan.go +++ b/internal/poller/interface_homelan.go @@ -22,14 +22,14 @@ var _ Poller = &InterfaceHomeLanMbits{} // InterfaceHomeLanMbits is an experimental poller to get the current bandwidth // usage on the Livebox interfaces. type InterfaceHomeLanMbits struct { - client livebox.Client + client *livebox.Client interfaces []*exporterLivebox.Interface bitrate *bitrate.Bitrate txMbits, rxMbits *prometheus.GaugeVec } // NewInterfaceHomeLanMbits returns a new InterfaceMbits poller. -func NewInterfaceHomeLanMbits(client livebox.Client, interfaces []*exporterLivebox.Interface) *InterfaceHomeLanMbits { +func NewInterfaceHomeLanMbits(client *livebox.Client, interfaces []*exporterLivebox.Interface) *InterfaceHomeLanMbits { return &InterfaceHomeLanMbits{ client: client, interfaces: interfaces, diff --git a/internal/poller/interface_netdev.go b/internal/poller/interface_netdev.go index 306e91f..94966e3 100644 --- a/internal/poller/interface_netdev.go +++ b/internal/poller/interface_netdev.go @@ -16,14 +16,14 @@ var _ Poller = &InterfaceNetDevMbits{} // InterfaceNetDevMbits is an experimental poller to get the current bandwidth // usage on the Livebox interfaces. type InterfaceNetDevMbits struct { - client livebox.Client + client *livebox.Client interfaces []*exporterLivebox.Interface bitrate *bitrate.Bitrate txMbits, rxMbits *prometheus.GaugeVec } // NewInterfaceNetDevMbits returns a new InterfaceNetDevMbits poller. -func NewInterfaceNetDevMbits(client livebox.Client, interfaces []*exporterLivebox.Interface) *InterfaceNetDevMbits { +func NewInterfaceNetDevMbits(client *livebox.Client, interfaces []*exporterLivebox.Interface) *InterfaceNetDevMbits { return &InterfaceNetDevMbits{ client: client, interfaces: interfaces, diff --git a/internal/poller/wan.go b/internal/poller/wan.go index 90089f0..058fdc6 100644 --- a/internal/poller/wan.go +++ b/internal/poller/wan.go @@ -14,13 +14,13 @@ var _ Poller = &WANMbits{} // WANMbits is an experimental poller to get the current bandwidth usage on the // WAN interface of the Livebox. type WANMbits struct { - client livebox.Client + client *livebox.Client bitrate *bitrate.Bitrate txMbits, rxMbits prometheus.Gauge } // NewWANMbits returns a new WANMbits poller. -func NewWANMbits(client livebox.Client) *WANMbits { +func NewWANMbits(client *livebox.Client) *WANMbits { return &WANMbits{ client: client, bitrate: bitrate.New(InterfaceHomeLanMbitsMinDelay), diff --git a/main.go b/main.go index fd4b671..1d58e79 100644 --- a/main.go +++ b/main.go @@ -2,9 +2,12 @@ package main import ( "context" + "crypto/tls" + "crypto/x509" "errors" "flag" "fmt" + "io/ioutil" "log" "net/http" "os" @@ -37,10 +40,14 @@ var experimentalMetrics = []string{ func parseExperimentalFlag( ctx context.Context, - client livebox.Client, + client *livebox.Client, experimental string, pollingFrequency *uint, ) (pollers []poller.Poller) { + if experimental == "" { + return nil + } + var ( interfaces []*exporterLivebox.Interface err error @@ -96,6 +103,37 @@ func parseExperimentalFlag( return } +func getHTTPClient() (*http.Client, error) { + liveboxCACertPath := os.Getenv("LIVEBOX_CACERT") + + if liveboxCACertPath == "" { + return http.DefaultClient, nil + } + + // Get the SystemCertPool, continue with an empty pool on error. + rootCAs, _ := x509.SystemCertPool() + if rootCAs == nil { + rootCAs = x509.NewCertPool() + } + + certs, err := ioutil.ReadFile(liveboxCACertPath) + if err != nil { + return nil, fmt.Errorf("failed to read livebox CA cert: %w", err) + } + + if ok := rootCAs.AppendCertsFromPEM(certs); !ok { + return nil, errors.New("no livebox CA cert was successfully added") + } + + return &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + RootCAs: rootCAs, + }, + }, + }, nil +} + func main() { pollingFrequency := flag.Uint("polling-frequency", defaultPollingFrequency, "Polling frequency") listen := flag.String("listen", ":8080", "Listening address") @@ -110,10 +148,28 @@ func main() { log.Fatal("ADMIN_PASSWORD environment variable must be set") } + liveboxAddress := os.Getenv("LIVEBOX_ADDRESS") + if liveboxAddress == "" { + liveboxAddress = livebox.DefaultAddress + } + + httpClient, err := getHTTPClient() + if err != nil { + log.Fatal(err) + } + + client, err := livebox.NewClient( + adminPassword, + livebox.WithAddress(liveboxAddress), + livebox.WithHTTPClient(httpClient), + ) + if err != nil { + log.Fatalf("Failed to create Livebox client: %v", err) + } + var ( ctx = context.Background() registry = prometheus.NewRegistry() - client = livebox.NewClient(adminPassword) pollers = poller.Pollers{ poller.NewDevicesTotal(client), poller.NewInterfaceMbits(client), diff --git a/pkg/livebox/discovery.go b/pkg/livebox/discovery.go index 6e496f7..0433a92 100644 --- a/pkg/livebox/discovery.go +++ b/pkg/livebox/discovery.go @@ -27,7 +27,7 @@ func (i *Interface) IsWLAN() bool { } // DiscoverInterfaces discovers network interfaces on the Livebox. -func DiscoverInterfaces(ctx context.Context, client livebox.Client) ([]*Interface, error) { +func DiscoverInterfaces(ctx context.Context, client *livebox.Client) ([]*Interface, error) { var mibs struct { Status struct { Base map[string]struct {