From 5abe206e2794130fbb66146f1b6317e615c30053 Mon Sep 17 00:00:00 2001 From: Tomy Guichard Date: Tue, 23 May 2023 20:55:05 +0200 Subject: [PATCH] Add deviceinfo metrics (#5) --- README.md | 26 ++++---- internal/collector/deviceinfo.go | 101 +++++++++++++++++++++++++++++++ main.go | 3 + 3 files changed, 119 insertions(+), 11 deletions(-) create mode 100644 internal/collector/deviceinfo.go diff --git a/README.md b/README.md index 2494f96..8464fd9 100644 --- a/README.md +++ b/README.md @@ -7,17 +7,21 @@ FTTH subscription. This exporter currently exposes the following metrics: -| Name | Type | Description | Labels | Experimental | -| ---------------------------------- | ----- | ------------------------------------------------- | --------- | ------------ | -| livebox_interface_rx_mbits | gauge | Received Mbits per second | interface | No | -| livebox_interface_tx_mbits | gauge | Transmitted Mbits per second | interface | No | -| livebox_devices_total | gauge | The total number of active devices | type | No | -| livebox_interface_homelan_rx_mbits | gauge | Received Mbits per second | interface | Yes | -| livebox_interface_homelan_tx_mbits | gauge | Transmitted Mbits per second | interface | Yes | -| livebox_interface_netdev_rx_mbits | gauge | Received Mbits per second | interface | Yes | -| livebox_interface_netdev_tx_mbits | gauge | Transmitted Mbits per second | interface | Yes | -| livebox_wan_rx_mbits | gauge | Received Mbits per second on the WAN interface | | Yes | -| livebox_wan_tx_mbits | gauge | Transmitted Mbits per second on the WAN interface | | Yes | +| Name | Type | Description | Labels | Experimental | +| --------------------------------------- | ----- | ------------------------------------------------- | --------- | ------------ | +| livebox_interface_rx_mbits | gauge | Received Mbits per second | interface | No | +| livebox_interface_tx_mbits | gauge | Transmitted Mbits per second | interface | No | +| livebox_devices_total | gauge | The total number of active devices | type | No | +| livebox_deviceinfo_reboots_total | gauge | Number of Livebox reboots | | No | +| livebox_deviceinfo_uptime_seconds_total | gauge | Livebox current uptime | | No | +| livebox_deviceinfo_memory_total_bytes | gauge | Livebox system total memory | | No | +| livebox_deviceinfo_memory_usage_bytes | gauge | Livebox system used memory | | No | +| livebox_interface_homelan_rx_mbits | gauge | Received Mbits per second | interface | Yes | +| livebox_interface_homelan_tx_mbits | gauge | Transmitted Mbits per second | interface | Yes | +| livebox_interface_netdev_rx_mbits | gauge | Received Mbits per second | interface | Yes | +| livebox_interface_netdev_tx_mbits | gauge | Transmitted Mbits per second | interface | Yes | +| livebox_wan_rx_mbits | gauge | Received Mbits per second on the WAN interface | | Yes | +| livebox_wan_tx_mbits | gauge | Transmitted Mbits per second on the WAN interface | | Yes | Experimental metrics are not enabled by default, use the `-experimental` command-line option to enable them. diff --git a/internal/collector/deviceinfo.go b/internal/collector/deviceinfo.go new file mode 100644 index 0000000..e317cfc --- /dev/null +++ b/internal/collector/deviceinfo.go @@ -0,0 +1,101 @@ +package collector + +import ( + "context" + "log" + "sync" + + "github.com/Tomy2e/livebox-api-client" + "github.com/Tomy2e/livebox-api-client/api/request" + "github.com/prometheus/client_golang/prometheus" +) + +var _ prometheus.Collector = &DeviceInfo{} + +// DeviceInfo implements a prometheus Collector that returns Livebox specific metrics. +type DeviceInfo struct { + client livebox.Client + numberOfRebootsMetric *prometheus.Desc + uptimeMetric *prometheus.Desc + memoryTotalMetric *prometheus.Desc + memoryUsageMetric *prometheus.Desc +} + +// NewDeviceInfo returns a new DeviceInfo collector using the specified client. +func NewDeviceInfo(client livebox.Client) *DeviceInfo { + return &DeviceInfo{ + client: client, + numberOfRebootsMetric: prometheus.NewDesc( + "livebox_deviceinfo_reboots_total", + "Number of Livebox reboots.", + nil, nil, + ), + uptimeMetric: prometheus.NewDesc( + "livebox_deviceinfo_uptime_seconds_total", + "Livebox current uptime.", + nil, nil, + ), + memoryTotalMetric: prometheus.NewDesc( + "livebox_deviceinfo_memory_total_bytes", + "Livebox system total memory.", + nil, nil, + ), + memoryUsageMetric: prometheus.NewDesc( + "livebox_deviceinfo_memory_usage_bytes", + "Livebox system used memory.", + nil, nil, + ), + } +} + +// Describe currently does nothing. +func (d *DeviceInfo) Describe(c chan<- *prometheus.Desc) {} + +func (d *DeviceInfo) deviceInfo(c chan<- prometheus.Metric) { + var deviceInfo struct { + Status struct { + NumberOfReboots float64 `json:"NumberOfReboots"` + UpTime float64 `json:"UpTime"` + } `json:"status"` + } + if err := d.client.Request(context.TODO(), request.New("DeviceInfo", "get", nil), &deviceInfo); err != nil { + log.Printf("WARN: DeviceInfo collector failed: %s", err) + return + } + + c <- prometheus.MustNewConstMetric(d.numberOfRebootsMetric, prometheus.GaugeValue, deviceInfo.Status.NumberOfReboots) + c <- prometheus.MustNewConstMetric(d.uptimeMetric, prometheus.GaugeValue, deviceInfo.Status.UpTime) +} + +func (d *DeviceInfo) memoryStatus(c chan<- prometheus.Metric) { + var memoryStatus struct { + Status struct { + Total float64 `json:"Total"` + Free float64 `json:"Free"` + } `json:"status"` + } + if err := d.client.Request(context.TODO(), request.New("DeviceInfo.MemoryStatus", "get", nil), &memoryStatus); err != nil { + log.Printf("WARN: MemoryStatus collector failed: %s", err) + return + } + + c <- prometheus.MustNewConstMetric(d.memoryTotalMetric, prometheus.GaugeValue, 1000*memoryStatus.Status.Total) + c <- prometheus.MustNewConstMetric(d.memoryUsageMetric, prometheus.GaugeValue, 1000*(memoryStatus.Status.Total-memoryStatus.Status.Free)) +} + +// Collect collects all DeviceInfo metrics. +func (d *DeviceInfo) Collect(c chan<- prometheus.Metric) { + wg := sync.WaitGroup{} + wg.Add(1) + go func() { + d.deviceInfo(c) + wg.Done() + }() + + wg.Add(1) + go func() { + d.memoryStatus(c) + wg.Done() + }() + wg.Wait() +} diff --git a/main.go b/main.go index fea4ac2..fd4b671 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "time" "github.com/Tomy2e/livebox-api-client" + "github.com/Tomy2e/livebox-exporter/internal/collector" "github.com/Tomy2e/livebox-exporter/internal/poller" exporterLivebox "github.com/Tomy2e/livebox-exporter/pkg/livebox" "github.com/prometheus/client_golang/prometheus" @@ -130,6 +131,8 @@ func main() { )..., ) + registry.MustRegister(collector.NewDeviceInfo(client)) + go func() { for { if err := pollers.Poll(ctx); err != nil {