Просмотр исходного кода

feat(routers): list the virtual interfaces the agent already reports (#1761)

The agent path only built EthPorts for physical ports, so the interfaces
NetIf carries (vlan, bridge, bond, tun, wg) never reached the router page.
The collapsed section that groups them by family has been there since
#1125, but on an agent-only fleet it was always empty: family is only set
on the SNMP path.

Build them from NetIf, with the counters and the same rate calculation the
physical ports use, and keep them out of the port monitor: /proc/net/dev
has no link state, so a watchdog over them would only produce noise. Up
means "has carried traffic" for the same reason.

Refs #1757
Nacho 1 месяц назад
Родитель
Сommit
158ef617dc
2 измененных файлов с 210 добавлено и 2 удалено
  1. 103 2
      server-go/internal/adapters/agent.go
  2. 107 0
      server-go/internal/adapters/virtual_ports_test.go

+ 103 - 2
server-go/internal/adapters/agent.go

@@ -627,6 +627,102 @@ type agentIfSample struct {
 // injectPortRates copia absolutos + rates de rates (keyed por iface física)
 // sobre las bocas: match por Iface si la boca la declara, si no por ID (en
 // DSA coinciden; en swconfig el ID es numérico y solo Iface casa).
+// appendVirtualPorts turns the interfaces NetIf carries that are not physical
+// ports into EthPorts, so the router page can list them with their traffic
+// (#1757). The agent path never built them: NetIf only fed the rates of the
+// physical ports. Up means "has carried traffic", because /proc/net/dev has no
+// link state and inventing one would be worse than saying nothing.
+func appendVirtualPorts(ports []EthPort, ifaces map[string]probe.IfCounters, rates map[string]probe.IfRate) []EthPort {
+	if len(ifaces) == 0 {
+		return ports
+	}
+	seen := make(map[string]bool, len(ports))
+	for _, p := range ports {
+		if p.Iface != "" {
+			seen[p.Iface] = true
+		}
+		if p.ID != "" {
+			seen[p.ID] = true
+		}
+	}
+	names := make([]string, 0, len(ifaces))
+	for name := range ifaces {
+		if name == "lo" || seen[name] {
+			continue
+		}
+		names = append(names, name)
+	}
+	if len(names) == 0 {
+		return ports
+	}
+	sort.Strings(names)
+	for _, name := range names {
+		c := ifaces[name]
+		p := EthPort{
+			ID:      "if-" + name,
+			Label:   name,
+			Iface:   name,
+			Up:      c.Rx+c.Tx > 0,
+			Family:  virtualIfaceFamily(name),
+			RxBytes: c.Rx,
+			TxBytes: c.Tx,
+			RxErrs:  c.RxErr,
+			TxErrs:  c.TxErr,
+		}
+		if r, ok := rates[name]; ok {
+			if r.RxBps != nil {
+				p.RxBps = *r.RxBps
+			}
+			if r.TxBps != nil {
+				p.TxBps = *r.TxBps
+			}
+		}
+		ports = append(ports, p)
+	}
+	return ports
+}
+
+// virtualIfaceFamily names the family the router page groups by, the same
+// values the SNMP path gets from ifType.
+func virtualIfaceFamily(name string) string {
+	switch {
+	case strings.Contains(name, "."), strings.HasPrefix(name, "vlan"):
+		return "vlan"
+	case strings.HasPrefix(name, "br"), name == "bridge":
+		return "bridge"
+	case strings.HasPrefix(name, "bond"), strings.HasPrefix(name, "lag"):
+		return "lag"
+	case strings.HasPrefix(name, "tun"), strings.HasPrefix(name, "tap"), strings.HasPrefix(name, "wg"),
+		strings.HasPrefix(name, "pppoe"), strings.HasPrefix(name, "gre"), strings.HasPrefix(name, "sit"),
+		strings.HasPrefix(name, "vti"), strings.HasPrefix(name, "ip6tnl"):
+		return "tunnel"
+	}
+	return "virtual"
+}
+
+// linkPorts drops the interfaces that are not physical links: they have no
+// link state to watch, so the port monitor has nothing to say about them
+// (#1757).
+func linkPorts(ports []EthPort) []EthPort {
+	n := 0
+	for _, p := range ports {
+		if p.Family != "" && p.Family != "ethernet" {
+			n++
+		}
+	}
+	if n == 0 {
+		return ports
+	}
+	out := make([]EthPort, 0, len(ports)-n)
+	for _, p := range ports {
+		if p.Family != "" && p.Family != "ethernet" {
+			continue
+		}
+		out = append(out, p)
+	}
+	return out
+}
+
 func injectPortRates(ports []EthPort, rates map[string]probe.IfRate) {
 	for i := range ports {
 		key := ports[i].Iface
@@ -921,10 +1017,15 @@ func (l *Live) polledFromAgent(cfg RouterConfig, p *probe.Payload) *routerPolled
 		prev := l.lastAgentIf[cfg.ID]
 		l.lastAgentIf[cfg.ID] = &agentIfSample{at: now, ifaces: p.Data.NetIf}
 		l.mu.Unlock()
+		var rates map[string]probe.IfRate
 		if prev != nil && now.Sub(prev.at).Seconds() > 0 && len(out.ports) > 0 {
-			rates := probe.IfRates(prev.ifaces, p.Data.NetIf, now.Sub(prev.at).Seconds())
+			rates = probe.IfRates(prev.ifaces, p.Data.NetIf, now.Sub(prev.at).Seconds())
 			injectPortRates(out.ports, rates)
 		}
+		// #1757: las interfaces que no son bocas físicas (vlan, bridge,
+		// bond, tun, wg) también cuentan. La página del router las lista
+		// aparte por su familia, con el tráfico que ya trae NetIf.
+		out.ports = appendVirtualPorts(out.ports, p.Data.NetIf, rates)
 	}
 
 	l.mu.Lock()
@@ -974,7 +1075,7 @@ func (l *Live) polledFromAgent(cfg RouterConfig, p *probe.Payload) *routerPolled
 		if samples, source, ok := resolveClientBwSources(out.wireless, out.clientBw); ok {
 			l.recordClientBwSamples(cfg.ID, time.Unix(p.Ts, 0), samples, source)
 		}
-		l.portMon.Observe(cfg.ID, out.ports, l.engine)
+		l.portMon.Observe(cfg.ID, linkPorts(out.ports), l.engine)
 	}
 
 	if out.leases == nil {

+ 107 - 0
server-go/internal/adapters/virtual_ports_test.go

@@ -0,0 +1,107 @@
+package adapters
+
+import (
+	"testing"
+
+	"github.com/gnacho/netpulse/agent/probe"
+)
+
+func TestAppendVirtualPortsListsWhatNetIfCarries(t *testing.T) {
+	ports := []EthPort{{ID: "eth0", Iface: "eth0", Label: "WAN", Up: true}}
+	ifaces := map[string]probe.IfCounters{
+		"eth0":    {Rx: 1000, Tx: 2000},
+		"br-lan":  {Rx: 500, Tx: 700},
+		"vlan10":  {Rx: 0, Tx: 0},
+		"wg0":     {Rx: 10, Tx: 20},
+		"lo":      {Rx: 999999, Tx: 999999},
+		"br-wan2": {Rx: 1, Tx: 0},
+	}
+	rxBps, txBps := 800.0, 200.0
+	rates := map[string]probe.IfRate{
+		"br-lan": {IfCounters: ifaces["br-lan"], RxBps: &rxBps, TxBps: &txBps},
+	}
+
+	out := appendVirtualPorts(ports, ifaces, rates)
+
+	if len(out) != 5 {
+		t.Fatalf("expected 4 physical plus 4 virtual ports, got %d: %+v", len(out), out)
+	}
+	// The physical port is left as it was and not duplicated.
+	if out[0].ID != "eth0" || out[0].Label != "WAN" || !out[0].Up {
+		t.Fatalf("physical port changed: %+v", out[0])
+	}
+	byName := map[string]EthPort{}
+	for _, p := range out[1:] {
+		byName[p.Label] = p
+	}
+	if _, ok := byName["lo"]; ok {
+		t.Fatal("loopback must not be listed")
+	}
+	if _, ok := byName["eth0"]; ok {
+		t.Fatal("a physical port must not be added twice")
+	}
+	if p := byName["br-lan"]; p.Family != "bridge" || p.ID != "if-br-lan" || p.Iface != "br-lan" {
+		t.Fatalf("bridge port wrong: %+v", p)
+	}
+	if p := byName["br-lan"]; !p.Up || p.RxBps != 800 || p.TxBps != 200 {
+		t.Fatalf("bridge traffic wrong: %+v", p)
+	}
+	if p := byName["vlan10"]; p.Family != "vlan" || p.Up {
+		t.Fatalf("idle vlan should be listed and not up: %+v", p)
+	}
+	if p := byName["wg0"]; p.Family != "tunnel" || !p.Up {
+		t.Fatalf("tunnel wrong: %+v", p)
+	}
+	// Rates only reach the interface they belong to.
+	if p := byName["wg0"]; p.RxBps != 0 || p.TxBps != 0 {
+		t.Fatalf("rates leaked into another interface: %+v", p)
+	}
+}
+
+func TestAppendVirtualPortsWithoutNetIfChangesNothing(t *testing.T) {
+	ports := []EthPort{{ID: "eth0", Iface: "eth0"}}
+	if out := appendVirtualPorts(ports, nil, nil); len(out) != 1 || out[0].ID != "eth0" {
+		t.Fatalf("expected the same ports, got %+v", out)
+	}
+}
+
+func TestVirtualIfaceFamilyNamesTheFamilies(t *testing.T) {
+	cases := map[string]string{
+		"br-lan":    "bridge",
+		"bridge":    "bridge",
+		"vlan10":    "vlan",
+		"eth0.10":   "vlan",
+		"bond0":     "lag",
+		"lagg0":     "lag",
+		"wg0":       "tunnel",
+		"tun0":      "tunnel",
+		"pppoe-wan": "tunnel",
+		"gre1":      "tunnel",
+		"eth1":      "virtual",
+		"foo0":      "virtual",
+	}
+	for name, want := range cases {
+		if got := virtualIfaceFamily(name); got != want {
+			t.Errorf("virtualIfaceFamily(%q) = %q, want %q", name, got, want)
+		}
+	}
+}
+
+func TestLinkPortsKeepsOnlyPhysicalLinks(t *testing.T) {
+	ports := []EthPort{
+		{ID: "eth0", Iface: "eth0"},
+		{ID: "if-br-lan", Iface: "br-lan", Family: "bridge"},
+		{ID: "snmp-2", Iface: "eth1", Family: "ethernet"},
+		{ID: "if-vlan10", Iface: "vlan10", Family: "vlan"},
+	}
+	out := linkPorts(ports)
+	if len(out) != 2 {
+		t.Fatalf("expected the two physical links, got %+v", out)
+	}
+	if out[0].ID != "eth0" || out[1].ID != "snmp-2" {
+		t.Fatalf("wrong ports kept: %+v", out)
+	}
+	if same := linkPorts(out); len(same) != 2 || &same[0] != &out[0] {
+		t.Fatal("with nothing to drop the same slice should come back")
+	}
+}