Skip to content

Commit

Permalink
Add ONT metrics (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomy2e authored Aug 14, 2024
1 parent 632a088 commit 1313c00
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 17 deletions.
37 changes: 20 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ 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_device_active | gauge | Status of the device | name, type, mac | No |
| livebox_device_rx_mbits | gauge | Received Mbits per second by device | name, type, mac, source | No |
| livebox_device_tx_mbits | gauge | Transmitted Mbits per second by device | name, type, mac, source | 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 |
| 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_device_active | gauge | Status of the device | name, type, mac | No |
| livebox_device_rx_mbits | gauge | Received Mbits per second by device | name, type, mac, source | No |
| livebox_device_tx_mbits | gauge | Transmitted Mbits per second by device | name, type, mac, source | 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_ont_temperature_celsius | gauge | Current ONT temperature | | No |
| livebox_ont_downstream_current_rate_bytes | gauge | Current ONT downstream rate | | No |
| livebox_ont_upstream_current_rate_bytes | gauge | Current ONT upstream rate | | 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.
Expand Down
80 changes: 80 additions & 0 deletions internal/collector/ont.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package collector

import (
"context"
"log"
"slices"
"time"

"github.com/Tomy2e/livebox-api-client"
"github.com/Tomy2e/livebox-api-client/api/request"
exporterLivebox "github.com/Tomy2e/livebox-exporter/pkg/livebox"
"github.com/prometheus/client_golang/prometheus"
)

const gponInterfaceName = "veip0"

// ONT implements a prometheus Collector that returns ONT specific metrics.
type ONT struct {
client *livebox.Client

enabled bool

temperatureMetric *prometheus.Desc
downstreamCurrRateMetric *prometheus.Desc
upstreamCurrRateMetric *prometheus.Desc
}

// NewONT returns a new ONT collector using the specified client.
func NewONT(client *livebox.Client, interfaces []*exporterLivebox.Interface) *ONT {
return &ONT{
client: client,
// Do not enable this collector if veip0 interface is not found.
enabled: slices.ContainsFunc(interfaces, func(itf *exporterLivebox.Interface) bool { return itf.Name == gponInterfaceName }),
temperatureMetric: prometheus.NewDesc(
"livebox_ont_temperature_celsius",
"Current ONT temperature.",
nil, nil,
),
downstreamCurrRateMetric: prometheus.NewDesc(
"livebox_ont_downstream_current_rate_bytes",
"Current ONT downstream rate.",
nil, nil,
),
upstreamCurrRateMetric: prometheus.NewDesc(
"livebox_ont_upstream_current_rate_bytes",
"Current ONT upstream rate.",
nil, nil,
),
}
}

// Describe currently does nothing.
func (d *ONT) Describe(_ chan<- *prometheus.Desc) {}

// Collect collects all ONT metrics.
func (d *ONT) Collect(c chan<- prometheus.Metric) {
defer warnOnSlowCollect(d, time.Now())

// Skip if GPON interface does not exist
if !d.enabled {
return
}

var ont struct {
Status struct {
Temperature float64 `json:"Temperature"`
DownstreamCurrRate float64 `json:"DownstreamCurrRate"`
UpstreamCurrRate float64 `json:"UpstreamCurrRate"`
} `json:"status"`
}

if err := d.client.Request(context.TODO(), request.New("NeMo.Intf.veip0", "get", nil), &ont); err != nil {
log.Printf("WARN: ONT collector: failed to get gpon interface: %s", err)
return
}

c <- prometheus.MustNewConstMetric(d.temperatureMetric, prometheus.GaugeValue, ont.Status.Temperature)
c <- prometheus.MustNewConstMetric(d.downstreamCurrRateMetric, prometheus.GaugeValue, 1000*ont.Status.DownstreamCurrRate)
c <- prometheus.MustNewConstMetric(d.upstreamCurrRateMetric, prometheus.GaugeValue, 1000*ont.Status.UpstreamCurrRate)
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func main() {
registry.MustRegister(
collector.NewDeviceInfo(client),
collector.NewDevices(client, interfaces),
collector.NewONT(client, interfaces),
writeHeaderVec,
)

Expand Down

0 comments on commit 1313c00

Please sign in to comment.