The web UI serves the router's own state at /metrics (token needed); router.metrics.nodeExporter runs node_exporter for the machine's and opens its port only to the zones named. The Prometheus page edits those settings and shows the scrape configuration. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
168 lines
4.6 KiB
Go
168 lines
4.6 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"net/http"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// The Prometheus page: node_exporter (router.metrics.nodeExporter) and how to
|
|
// scrape the router's own /metrics. The settings are saved like those of any
|
|
// page and applied with the rest.
|
|
|
|
// ExporterForm is router.metrics.nodeExporter.
|
|
type ExporterForm struct {
|
|
Enabled bool
|
|
Port string
|
|
Zones []string
|
|
}
|
|
|
|
const defaultExporterPort = "9100"
|
|
|
|
func (s Settings) exporter() ExporterForm {
|
|
router, _ := s["router"].(map[string]any)
|
|
m, _ := router["metrics"].(map[string]any)
|
|
n, _ := m["nodeExporter"].(map[string]any)
|
|
f := ExporterForm{Enabled: boolean(n, false, "enable"), Port: str(n, "port"), Zones: stringList(n, "zones")}
|
|
if f.Port == "" {
|
|
f.Port = defaultExporterPort
|
|
}
|
|
return f
|
|
}
|
|
|
|
// Ports the router itself serves on: node_exporter cannot take them.
|
|
var exporterReserved = map[int]string{22: "SSH", 53: "DNS", 67: "DHCP", 68: "DHCP", 80: "the web UI", 443: "the web UI"}
|
|
|
|
func (s Settings) applyExporter(f ExporterForm) error {
|
|
errs := FormErrors{}
|
|
f.Port = strings.TrimSpace(f.Port)
|
|
if f.Port == "" {
|
|
f.Port = defaultExporterPort
|
|
}
|
|
port, err := strconv.Atoi(f.Port)
|
|
switch {
|
|
case err != nil || port < 1 || port > 65535:
|
|
errs["port"] = "a port number between 1 and 65535"
|
|
case exporterReserved[port] != "":
|
|
errs["port"] = "port " + f.Port + " is used by " + exporterReserved[port]
|
|
}
|
|
known := s.zones()
|
|
var zones []any
|
|
for _, z := range f.Zones {
|
|
if z == "wan" {
|
|
errs["zones"] = "wan is the internet: node_exporter must not be open to it. Scrape it from a LAN or through a WireGuard tunnel."
|
|
break
|
|
}
|
|
if !slices.Contains(known, z) {
|
|
errs["zones"] = "unknown zone " + z
|
|
break
|
|
}
|
|
zones = append(zones, z)
|
|
}
|
|
if len(errs) > 0 {
|
|
return errs
|
|
}
|
|
router := obj(s, "router")
|
|
if !f.Enabled && len(zones) == 0 && f.Port == defaultExporterPort {
|
|
// Nothing to say: leave the settings as they would be without this page.
|
|
if m, ok := router["metrics"].(map[string]any); ok {
|
|
delete(m, "nodeExporter")
|
|
if len(m) == 0 {
|
|
delete(router, "metrics")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
n := obj(obj(router, "metrics"), "nodeExporter")
|
|
n["enable"] = f.Enabled
|
|
if f.Port == defaultExporterPort {
|
|
delete(n, "port")
|
|
} else {
|
|
n["port"] = float64(port)
|
|
}
|
|
if len(zones) > 0 {
|
|
n["zones"] = zones
|
|
} else {
|
|
delete(n, "zones")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type zoneChoice struct {
|
|
Name string
|
|
Checked bool
|
|
}
|
|
|
|
type exporterData struct {
|
|
Form ExporterForm
|
|
Errors FormErrors
|
|
Zones []zoneChoice // the zones that can be chosen
|
|
Host string // how the browser reached the router, for the example
|
|
Target string // host:port for node_exporter
|
|
}
|
|
|
|
func (a *app) exporterData(r *http.Request, s Settings, f ExporterForm, errs FormErrors) exporterData {
|
|
d := exporterData{Form: f, Errors: errs, Host: r.Host}
|
|
host := r.Host
|
|
if h, _, err := net.SplitHostPort(r.Host); err == nil {
|
|
host = h
|
|
}
|
|
d.Target = net.JoinHostPort(host, f.Port)
|
|
names := s.zones()
|
|
for _, z := range f.Zones { // a zone that is chosen stays listed even if it is gone
|
|
if !slices.Contains(names, z) {
|
|
names = append(names, z)
|
|
}
|
|
}
|
|
for _, z := range names {
|
|
d.Zones = append(d.Zones, zoneChoice{Name: z, Checked: slices.Contains(f.Zones, z)})
|
|
}
|
|
return d
|
|
}
|
|
|
|
func (a *app) exporterPage(w http.ResponseWriter, r *http.Request, sess session) {
|
|
p := page{Title: "Prometheus", CSRF: sess.csrf, LoggedIn: true, Flash: r.URL.Query().Get("msg")}
|
|
s, err := loadSettings(a.sys.settingsPath())
|
|
if err != nil {
|
|
p.Error = err.Error()
|
|
a.render(w, "exporter", p)
|
|
return
|
|
}
|
|
p.Data = a.exporterData(r, s, s.exporter(), nil)
|
|
a.render(w, "exporter", p)
|
|
}
|
|
|
|
func (a *app) exporterSave(w http.ResponseWriter, r *http.Request, sess session) {
|
|
p := page{Title: "Prometheus", CSRF: sess.csrf, LoggedIn: true}
|
|
s, err := loadSettings(a.sys.settingsPath())
|
|
if err != nil {
|
|
p.Error = err.Error()
|
|
a.render(w, "exporter", p)
|
|
return
|
|
}
|
|
if err := r.ParseForm(); err != nil {
|
|
p.Error = err.Error()
|
|
a.render(w, "exporter", p)
|
|
return
|
|
}
|
|
f := ExporterForm{Enabled: r.PostFormValue("enabled") == "on", Port: r.PostFormValue("port"), Zones: r.PostForm["zone"]}
|
|
if err := s.applyExporter(f); err != nil {
|
|
var fe FormErrors
|
|
errors.As(err, &fe)
|
|
p.Error = "Please correct the marked fields."
|
|
p.Data = a.exporterData(r, s, f, fe)
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
a.render(w, "exporter", p)
|
|
return
|
|
}
|
|
if err := s.save(a.sys.settingsPath()); err != nil {
|
|
p.Error = "Saving: " + err.Error()
|
|
a.render(w, "exporter", p)
|
|
return
|
|
}
|
|
back(w, r, "Saved. Apply to start or stop node_exporter and to open the port.")
|
|
}
|