Skip to content

Commit

Permalink
Add support to read and write config from controller release 3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
corny committed Oct 24, 2024
1 parent 065d00c commit 3664b1b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ Triax EoC Exporter

This is a [Prometheus](https://prometheus.io/) exporter for
[Triax EoC controllers](https://www.triax.com/products/ethernet-over-coax).
It has been tested with the [EoC controller software](https://www.triax.com/product/ethernet-over-coax-software-update/) version 3.4.7.

## Features

* Exporting metrics for prometheus
* HTTP-Proxy to read API responses like the endpoint list
* Renaming of endpoints
* HTTP-Proxy for reading/writing controller configurations

## Installation

Expand Down
16 changes: 16 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ func (c *Client) Get(ctx context.Context, path string, res interface{}) error {
return c.ApiRequest(ctx, http.MethodGet, path, nil, res)
}

const luaConfigPath = "/cgi.lua/config"

// GetConfig fetches the configuration from the controller
func (c *Client) GetConfig(ctx context.Context) (json.RawMessage, error) {
msg := json.RawMessage{}
err := c.Get(ctx, luaConfigPath, &msg)
return msg, err
}

// SetConfig sets the configuration in the controller
func (c *Client) SetConfig(ctx context.Context, raw json.RawMessage) error {
res := json.RawMessage{}
err := c.ApiRequest(ctx, http.MethodPost, luaConfigPath, raw, res)
return err
}

func (c *Client) SetCookie(nameAndValue string) {
i := strings.Index(nameAndValue, "=")
if i <= 0 {
Expand Down
38 changes: 14 additions & 24 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func (cfg *Config) Start(listenAddress, version, date string) {

router.GET("/controllers", cfg.listControllersHandler)
router.GET("/controllers/:target/metrics", cfg.targetMiddleware(cfg.metricsHandler))
router.GET("/controllers/:target/api/*path", cfg.targetMiddleware(cfg.apiHandler))
//router.PUT("/controllers/:target/nodes/:mac", cfg.targetMiddleware(cfg.updateNodeHandler))
router.GET("/controllers/:target/config", cfg.targetMiddleware(cfg.getConfigHandler))
router.POST("/controllers/:target/config", cfg.targetMiddleware(cfg.updateConfigHandler))

slog.Info("Starting exporter", "listenAddress", listenAddress, "version", version, "builtDate", date)
slog.Info("Server stopped", "reason", http.ListenAndServe(listenAddress, router))
Expand Down Expand Up @@ -77,49 +77,39 @@ func (cfg *Config) metricsHandler(client *client.Client, w http.ResponseWriter,
h.ServeHTTP(w, r)
}

/*
// handler for updating nodes
func (cfg *Config) updateNodeHandler(client *client.Client, w http.ResponseWriter, r *http.Request, params httprouter.Params) {
// handler for updating configs

func (cfg *Config) updateConfigHandler(client *client.Client, w http.ResponseWriter, r *http.Request, params httprouter.Params) {
defer r.Body.Close()

// parse MAC address parameter
mac, err := net.ParseMAC(params.ByName("mac"))
if err != nil {
http.Error(w, fmt.Sprintf("invalid MAC address: %s", err), http.StatusBadRequest)
return
}
jsonBody := json.RawMessage{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&jsonBody)

// decode request body
req := triax.UpdateRequest{}
err = json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

// execute update
err = client.UpdateNode(r.Context(), mac, req)
err = client.SetConfig(r.Context(), jsonBody)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}

w.WriteHeader(http.StatusNoContent)
}
*/

// proxy handler for API GET requests.
func (cfg *Config) apiHandler(client *client.Client, w http.ResponseWriter, r *http.Request, params httprouter.Params) {
defer r.Body.Close()
// handler for getting configs
func (cfg *Config) getConfigHandler(client *client.Client, w http.ResponseWriter, r *http.Request, params httprouter.Params) {
config, err := client.GetConfig(r.Context())

msg := json.RawMessage{}
err := client.Get(r.Context(), params.ByName("path"), &msg)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}

io.Copy(w, bytes.NewReader(msg))
io.Copy(w, bytes.NewReader(config))
}

type indexVariables struct {
Expand Down Expand Up @@ -148,7 +138,7 @@ var tmpl = template.Must(template.New("index").Option("missingkey=error").Parse(
<dt>{{.Alias}}</dt>
<dd>
<a href="/controllers/{{.Alias}}/metrics">Metrics</a>,
<a href="/controllers/{{.Alias}}/api/node/status/">Status</a>
<a href="/controllers/{{.Alias}}/config">Config</a>
</dd>
{{end}}
</dl>
Expand Down

0 comments on commit 3664b1b

Please sign in to comment.