Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: adapt to HN8010TS #2

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 19 additions & 54 deletions exporter/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,83 +3,48 @@ package main
import (
"fmt"

"github.com/chickenzord/go-huawei-client/pkg/eg8145v5"
"github.com/chickenzord/go-huawei-client/pkg/hn8010ts"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog/log"
)

type RouterCollector struct {
client *eg8145v5.Client
client *hn8010ts.Client

deviceOnline *prometheus.GaugeVec
resourceUsage *prometheus.GaugeVec
levels *prometheus.GaugeVec
}

func NewRouterCollector(cfg *eg8145v5.Config) *RouterCollector {
func NewRouterCollector(cfg *hn8010ts.Config) *RouterCollector {
return &RouterCollector{
client: eg8145v5.NewClient(*cfg),
deviceOnline: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "router",
Name: "device_online",
Help: "Device online status in the router",
client: hn8010ts.NewClient(*cfg),
levels: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "huawei_ont_optic",
Name: "level",
Help: "Optical power",
}, []string{
"mac_address", "hostname",
}),
resourceUsage: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "router",
Name: "resource_usage",
Help: "Router resource usages",
}, []string{
"type", "unit",
"direction",
}),
}
}

func (c *RouterCollector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(c.deviceOnline, ch)
prometheus.DescribeByCollect(c.resourceUsage, ch)
prometheus.DescribeByCollect(c.levels, ch)
}

func (c *RouterCollector) Collect(ch chan<- prometheus.Metric) {
if err := c.client.Session(func(client *eg8145v5.Client) error {
devices, err := client.ListUserDevices()
if err := c.client.Session(func(client *hn8010ts.Client) error {
opticInfo, err := client.GetOpticInfo()
if err != nil {
return fmt.Errorf("failed to list user devices: %w", err)
}

for _, device := range devices {
deviceOnline := c.deviceOnline.With(prometheus.Labels{
"mac_address": device.MacAddr,
"hostname": device.HostName,
})

if device.Online() {
deviceOnline.Set(1)
} else {
deviceOnline.Set(0)
}

ch <- deviceOnline
}

usage, err := client.GetResourceUsage()
if err != nil {
return fmt.Errorf("failed to get resource usage: %w", err)
}

memoryUsage := c.resourceUsage.With(prometheus.Labels{
"type": "memory",
"unit": "percent",
})
memoryUsage.Set(float64(usage.Memory))
ch <- memoryUsage
rx := c.levels.WithLabelValues("rx")
rx.Set(float64(opticInfo.RXPower))
ch <- rx

cpuUsage := c.resourceUsage.With(prometheus.Labels{
"type": "cpu",
"unit": "percent",
})
cpuUsage.Set(float64(usage.CPU))
ch <- cpuUsage
tx := c.levels.WithLabelValues("tx")
tx.Set(float64(opticInfo.TXPower))
ch <- tx

return nil
}); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"os"

"github.com/chickenzord/go-huawei-client/pkg/eg8145v5"
"github.com/chickenzord/go-huawei-client/pkg/hn8010ts"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -38,7 +38,7 @@ func main() {
}

r := prometheus.NewRegistry()
r.MustRegister(NewRouterCollector(&eg8145v5.Config{
r.MustRegister(NewRouterCollector(&hn8010ts.Config{
URL: cfg.Router.URL,
Username: cfg.Router.Username,
Password: cfg.Router.Password,
Expand Down
45 changes: 44 additions & 1 deletion pkg/eg8145v5/client.go → pkg/hn8010ts/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eg8145v5
package hn8010ts

import (
"encoding/base64"
Expand Down Expand Up @@ -327,3 +327,46 @@ func (c *Client) GetResourceUsage() (*ResourceUsage, error) {

return &usage, nil
}

// GetResourceUsage
// Get current resource usages. Client must be authenticated.
func (c *Client) GetOpticInfo() (*OpticInfo, error) {
c.m.Lock()
defer c.m.Unlock()

req, err := http.NewRequest(http.MethodGet, c.baseURL+"/html/amp/opticinfo/opticinfo.asp", nil)
if err != nil {
return nil, err
}

req.Header.Set("User-Agent", c.userAgent)
req.Header.Set("Referer", c.baseURL)

res, err := c.h.Do(req)
if err != nil {
return nil, err
}

doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, err
}

scriptContent := doc.Find("script:not([src])").First().Text()
if scriptContent == "" {
return nil, fmt.Errorf("cannot find the script")
}

s := js.Script{
Name: "opticinfo.asp.js",
Content: scriptContent + OpticInfoFuncScript,
}

var opticInfo OpticInfo

if err := s.EvalJSON(&opticInfo, OpticInfoFuncName); err != nil {
return nil, err
}

return &opticInfo, nil
}
2 changes: 1 addition & 1 deletion pkg/eg8145v5/config.go → pkg/hn8010ts/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eg8145v5
package hn8010ts

type Config struct {
URL string
Expand Down
19 changes: 18 additions & 1 deletion pkg/eg8145v5/types.go → pkg/hn8010ts/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eg8145v5
package hn8010ts

import "strings"

Expand Down Expand Up @@ -40,3 +40,20 @@ type ResourceUsage struct {
Memory int // Memory usage in percent (0-100)
CPU int // CPU usage in percent (0-100)
}

var (
OpticInfoFuncScript = `
function getOpticInfo() {
return {
TXPower: Number(opticInfo.transOpticPower.slice(0, -1)),
RXPower: Number(opticInfo.revOpticPower.slice(0, -1)),
}
}
`
OpticInfoFuncName = "getOpticInfo"
)

type OpticInfo struct {
TXPower float32
RXPower float32
}
34 changes: 13 additions & 21 deletions wei/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

"github.com/chickenzord/go-huawei-client/pkg/eg8145v5"
"github.com/chickenzord/go-huawei-client/pkg/hn8010ts"
"github.com/joho/godotenv"
"github.com/urfave/cli/v2"
)
Expand All @@ -30,14 +30,9 @@ var (
},
Commands: []*cli.Command{
{
Name: "devices",
Subcommands: []*cli.Command{
{
Name: "list",
Description: "List all devices",
Action: devicesList,
},
},
Name: "opticinfo",
Description: "Show optic info",
Action: opticInfo,
},
{
Name: "top",
Expand All @@ -58,24 +53,22 @@ func main() {
}
}

func devicesList(ctx *cli.Context) error {
cfg := &eg8145v5.Config{
func opticInfo(ctx *cli.Context) error {
cfg := &hn8010ts.Config{
URL: ctx.String("url"),
Username: ctx.String("username"),
Password: ctx.String("password"),
}

client := eg8145v5.NewClient(*cfg)
client := hn8010ts.NewClient(*cfg)

if err := client.Session(func(c *eg8145v5.Client) error {
devices, err := c.ListUserDevices()
if err := client.Session(func(c *hn8010ts.Client) error {
opticInfo, err := c.GetOpticInfo()
if err != nil {
return err
}

for _, d := range devices {
fmt.Println(d.HostName, d.DevStatus)
}
fmt.Printf("RX Level: %.2f TX Level: %.2f\n", opticInfo.RXPower, opticInfo.TXPower)

return nil
}); err != nil {
Expand All @@ -84,17 +77,16 @@ func devicesList(ctx *cli.Context) error {

return nil
}

func top(ctx *cli.Context) error {
cfg := &eg8145v5.Config{
cfg := &hn8010ts.Config{
URL: ctx.String("url"),
Username: ctx.String("username"),
Password: ctx.String("password"),
}

client := eg8145v5.NewClient(*cfg)
client := hn8010ts.NewClient(*cfg)

if err := client.Session(func(c *eg8145v5.Client) error {
if err := client.Session(func(c *hn8010ts.Client) error {
usage, err := c.GetResourceUsage()
if err != nil {
return err
Expand Down