Skip to content

Commit

Permalink
Add LANUserTable metrics.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed Sep 9, 2023
1 parent 1438fac commit 31fb5b5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
20 changes: 20 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
// List of XML RPC getter function codes.
const (
FnCMSystemInfo = "2"
FnLANUserTable = "123"
FnCMState = "136"
)

Expand Down Expand Up @@ -59,6 +60,25 @@ func (c *CMSystemInfo) UnmarshalXML(d *xml.Decoder, start xml.StartElement) erro
return nil
}

// LANUserTable is a list of connected devices.
type LANUserTable struct {
Ethernet []LANUserTableClientInfo `xml:"Ethernet"`
WIFI []LANUserTableClientInfo `xml:"WIFI"`
}

// LANUserTableClientInfo is a device connected to the router.
type LANUserTableClientInfo struct {
Interface string `xml:"interface"`
IPv4Addr string `xml:"IPv4Addr"`
Index string `xml:"index"`
InterfaceID string `xml:"interfaceid"`
Hostname string `xml:"hostname"`
MACAddr string `xml:"MACAddr"`
Method string `xml:"method"`
LeaseTime string `xml:"leaseTime"`
Speed string `xml:"speed"`
}

// CMState shows cable modem state.
type CMState struct {
TunnerTemperature int `xml:"TunnerTemperature"`
Expand Down
45 changes: 45 additions & 0 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (c *Collector) ServeHTTP(w http.ResponseWriter, r *http.Request) {
reg := prometheus.NewRegistry()
c.collectCMState(r.Context(), reg, client)
c.collectCMSSystemInfo(r.Context(), reg, client)
c.collectLANUserTable(r.Context(), reg, client)

Check failure on line 46 in collector.go

View workflow job for this annotation

GitHub Actions / test

cannot use client (variable of type MetricsClient) as *ConnectBox value in argument to c.collectLANUserTable: need type assertion

h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
Expand Down Expand Up @@ -103,6 +104,50 @@ func (c *Collector) collectCMSSystemInfo(
}
}

func (c *Collector) collectLANUserTable(
ctx context.Context,
reg *prometheus.Registry,
client *ConnectBox,
) {
clientGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "connect_box_lan_client",
Help: "cm_docsis_mode.",
}, []string{
"connection",
"interface",
"ipv4_addr",
"hostname",
"MACAddr",
})

reg.MustRegister(clientGauge)

var data LANUserTable
err := client.GetMetrics(ctx, FnLANUserTable, &data)
if err == nil {
for _, c := range data.Ethernet {
clientGauge.WithLabelValues(
"ethernet",
c.Interface,
c.IPv4Addr,
c.Hostname,
c.MACAddr,
).Set(1)
}
for _, c := range data.WIFI {
clientGauge.WithLabelValues(
"wifi",
c.Interface,
c.IPv4Addr,
c.Hostname,
c.MACAddr,
).Set(1)
}
} else {
log.Printf("Failed to get LANUserTable: %v", err)
}
}

func (c *Collector) collectCMState(
ctx context.Context,
reg *prometheus.Registry,
Expand Down

0 comments on commit 31fb5b5

Please sign in to comment.