Skip to content

Commit

Permalink
Add support for EoC controller v3.4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
corny committed Nov 30, 2023
1 parent 96d176a commit 3f67d4e
Show file tree
Hide file tree
Showing 27 changed files with 951 additions and 361 deletions.
16 changes: 8 additions & 8 deletions triax/api-types.go → backend/v2/api-types.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package triax
package v2

import (
"bytes"
"encoding/json"
"strings"
)

const loginPath = "login/"
const loginPath = "api/login/"

// request for /api/login.
type loginRequest struct {
Expand All @@ -20,7 +20,7 @@ type loginResponse struct {
Message string `json:"message"`
}

const boardPath = "system/board/"
const boardPath = "api/system/board/"

// Board is the response from /api/board/
type Board struct {
Expand All @@ -44,7 +44,7 @@ type Board struct {
System string `json:"system"`
}

const ghnStatusPath = "ghn/status/"
const ghnStatusPath = "api/ghn/status/"

// response from /api/ghn/status.
type ghnStatusResponse []struct {
Expand All @@ -54,7 +54,7 @@ type ghnStatusResponse []struct {
Registered uint `json:"registered"`
}

const sysinfoPath = "system/info/"
const sysinfoPath = "api/system/info/"

// response from /api/system/info.
type sysinfoResponse struct {
Expand All @@ -68,7 +68,7 @@ type sysinfoResponse struct {
Load float64 `json:"load"`
}

const syseocPath = "config/system/eoc/"
const syseocPath = "api/config/system/eoc/"

// response from /config/system/eoc.
type syseocResponse struct {
Expand Down Expand Up @@ -103,7 +103,7 @@ func (mal MacAddrList) Index(s string) int {
return -1
}

const nodeStatusPath = "node/status/"
const nodeStatusPath = "api/node/status/"

// response from /api/node/status/. The key is a mangeled form
// of the AP's MAC address, and should be ignored.
Expand All @@ -118,7 +118,7 @@ type nodeStatusResponse map[string]struct {
Statistics Statistics `json:"statistics"`
Status string `json:"status"`
Statusid int `json:"statusid"`
Sysinfo struct {
Sysinfo *struct {
Load float64 `json:"load"`
Uptime uint `json:"uptime"`
} `json:"sysinfo"`
Expand Down
33 changes: 33 additions & 0 deletions backend/v2/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package v2

import (
"context"
"net/http"

"github.com/digineo/triax-eoc-exporter/client"
"github.com/digineo/triax-eoc-exporter/types"
)

func init() {
client.Register(New)
}

func New(ctx context.Context, c *client.Client) (types.Backend, error) {
b := backend{c}

req := loginRequest{Username: c.Username, Password: c.Password}
res := loginResponse{}
_, err := c.ApiRequestRaw(ctx, http.MethodPost, loginPath, &req, &res)

if err != nil {
return nil, err
}

c.SetCookie(res.Cookie)

return &b, nil
}

type backend struct {
*client.Client
}
10 changes: 10 additions & 0 deletions backend/v2/client_board.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package v2

import (
"context"
)

func (c *backend) Board(ctx context.Context) (board Board, err error) {
err = c.Get(ctx, boardPath, &board)
return
}
11 changes: 7 additions & 4 deletions triax/client_metrics.go → backend/v2/client_metrics.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package triax
package v2

import (
"context"
Expand All @@ -8,7 +8,7 @@ import (
"time"
)

func (c *Client) Metrics(ctx context.Context) (*Metrics, error) {
func (c *backend) Metrics(ctx context.Context) (*Metrics, error) {
sysinfo := sysinfoResponse{} // uptime, memory
syseoc := syseocResponse{} // EoC port names
ghn := ghnStatusResponse{} // G.HN port status
Expand Down Expand Up @@ -61,12 +61,15 @@ func (c *Client) Metrics(ctx context.Context) (*Metrics, error) {
ep.MAC = node.Mac
ep.Status = node.Statusid
ep.StatusText = node.Status
ep.Uptime = node.Sysinfo.Uptime
ep.Load = node.Sysinfo.Load
ep.GhnPortNumber = -1
ep.GhnStats = node.GhnStats
ep.Statistics = node.Statistics

if sysinfo := node.Sysinfo; sysinfo != nil {
ep.Uptime = &sysinfo.Uptime
ep.Load = &sysinfo.Load
}

if node.RegTimestamp != "" {
val, err := strconv.Atoi(node.RegTimestamp)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion triax/client_test.go → backend/v2/client_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package triax
package v2

import (
"encoding/json"
Expand Down
8 changes: 4 additions & 4 deletions triax/client_update.go → backend/v2/client_update.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package triax
package v2

import (
"context"
Expand All @@ -14,7 +14,7 @@ type UpdateRequest struct {
const hexDigit = "0123456789abcdef"

// UpdateNode updates the name of the given node
func (c *Client) UpdateNode(ctx context.Context, mac net.HardwareAddr, req UpdateRequest) error {
func (c *backend) UpdateNode(ctx context.Context, mac net.HardwareAddr, req UpdateRequest) error {
if len(mac) == 0 {
return errors.New("invalid MAC address")
}
Expand All @@ -28,10 +28,10 @@ func (c *Client) UpdateNode(ctx context.Context, mac net.HardwareAddr, req Updat
buf = append(buf, hexDigit[b&0xF])
}

err := c.apiRequest(ctx, http.MethodPut, "config/nodes/node_"+string(buf)+"/", req, nil)
err := c.ApiRequest(ctx, http.MethodPut, "config/nodes/node_"+string(buf)+"/", req, nil)
if err != nil {
return err
}

return c.apiRequest(ctx, http.MethodPost, "config/nodes/commit/", nil, nil)
return c.ApiRequest(ctx, http.MethodPost, "config/nodes/commit/", nil, nil)
}
86 changes: 86 additions & 0 deletions backend/v2/collect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package v2

import (
"context"
"fmt"
"strconv"

"github.com/digineo/triax-eoc-exporter/types"
"github.com/prometheus/client_golang/prometheus"
)

func (b *backend) Collect(ctx context.Context, ch chan<- prometheus.Metric) error {
const C, G = prometheus.CounterValue, prometheus.GaugeValue

metric := func(desc *prometheus.Desc, typ prometheus.ValueType, v float64, label ...string) {
ch <- prometheus.MustNewConstMetric(desc, typ, v, label...)
}
counterMetric := func(counters *Counters, node, ifname string) {
metric(types.CounterBytes, C, float64(counters.RxByte), node, ifname, "rx")
metric(types.CounterBytes, C, float64(counters.TxByte), node, ifname, "tx")
metric(types.CounterPackets, C, float64(counters.RxPacket), node, ifname, "rx")
metric(types.CounterPackets, C, float64(counters.TxPacket), node, ifname, "tx")
metric(types.CounterErrors, C, float64(counters.RxErr), node, ifname, "rx")
metric(types.CounterErrors, C, float64(counters.TxErr), node, ifname, "tx")
}

board, err := b.Board(ctx)
if err != nil {
return err
}
metric(types.CtrlInfo, C, 1, board.Serial, board.EthMac, board.Release.Revision)

m, err := b.Metrics(ctx)
if err != nil {
return err
}

metric(types.CtrlUptime, C, float64(m.Uptime))
metric(types.CtrlLoad, G, m.Load)

metric(types.CtrlMemoryTotal, G, float64(m.Memory.Total))
metric(types.CtrlMemoryFree, G, float64(m.Memory.Free))
metric(types.CtrlMemoryBuffered, G, float64(m.Memory.Buffered))
metric(types.CtrlMemoryShared, G, float64(m.Memory.Shared))

if ports := m.GhnPorts; ports != nil {
for _, port := range ports {
number := strconv.Itoa(port.Number)
metric(types.CtrlGhnNumRegistered, G, float64(port.EndpointsRegistered), number)
metric(types.CtrlGhnNumOnline, G, float64(port.EndpointsOnline), number)
}
}

if nodes := m.Endpoints; nodes != nil {
for _, node := range nodes {
metric(types.NodeStatus, G, float64(node.Status), node.Name)

if node.Uptime != nil {
metric(types.NodeUptime, C, float64(*node.Uptime), node.Name)
} else {
metric(types.NodeOffline, C, float64(node.OfflineSince.Unix()), node.Name)
}

// ethernet statistics
for _, stats := range node.Statistics.Ethernet {
if stats.Link {
counterMetric(&stats.Counters, node.Name, fmt.Sprintf("eth%d", stats.Port))
}
}

// wireless statistics
for _, stats := range node.Statistics.Wireless {
metric(types.NodeClients, G, float64(stats.Clients), node.Name, strconv.Itoa(stats.Band))
counterMetric(&stats.Counters, node.Name, fmt.Sprintf("wifi%d", stats.Band))
}

// ghn statistics
if stats := node.GhnStats; stats != nil {
metric(types.GhnRxbps, G, float64(stats.Rxbps), node.Name)
metric(types.GhnTxbps, G, float64(stats.Txbps), node.Name)
}
}
}

return nil
}
6 changes: 3 additions & 3 deletions triax/metrics.go → backend/v2/metrics.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package triax
package v2

import "time"

Expand All @@ -7,8 +7,8 @@ type EndpointMetrics struct {
MAC string
Status int
StatusText string
Uptime uint
Load float64
Uptime *uint
Load *float64
GhnPortNumber int
GhnPortMac string
GhnStats *GhnStats
Expand Down
2 changes: 1 addition & 1 deletion triax/quoted_int.go → backend/v2/quoted_int.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package triax
package v2

import (
"bytes"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 3f67d4e

Please sign in to comment.