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

fix(roamevents): only open SSH to units that can run logread (#1944) (#1950)

Nacho 3 недель назад
Родитель
Сommit
b63dfbba14

+ 1 - 0
server-go/cmd/netpulse/main.go

@@ -359,6 +359,7 @@ func run() error {
 				for _, r := range routerstore.ListRouters(dbHandle.DB) {
 					out = append(out, roamevents.RouterHost{
 						ID: r.ID, Host: r.Host, Name: r.Name, AgentOnly: r.AgentOnly,
+						Type: r.Type, Disabled: r.Disabled,
 					})
 				}
 				return out

+ 21 - 1
server-go/internal/roamevents/collector.go

@@ -52,6 +52,26 @@ type RouterHost struct {
 	Host      string
 	Name      string
 	AgentOnly bool
+	// Type distingue las unidades que corren logread (OpenWrt y GL.iNet) de
+	// las que solo hablan SNMP o publican por su cuenta (#1944).
+	Type string
+	// Disabled (#1085): unidad pausada, sin sondeo.
+	Disabled bool
+}
+
+// Eligible: solo se abre SSH en unidades que puedan tener el log de hostapd,
+// DAWN o usteer. Un managed-switch o un external solo tienen SNMP y el dial
+// acaba en handshake fallido cada ciclo (#1944).
+func (h RouterHost) Eligible() bool {
+	if h.Host == "" || h.AgentOnly || h.Disabled {
+		return false
+	}
+	switch h.Type {
+	case "", "openwrt", "glinet":
+		return true
+	default:
+		return false
+	}
 }
 
 // Runner es la abstracción de SSH (satisfecha por *sshpool.Pool).
@@ -116,7 +136,7 @@ func (c *Collector) tick() {
 		return
 	}
 	for _, h := range hosts {
-		if h.AgentOnly {
+		if !h.Eligible() {
 			continue
 		}
 		select {

+ 31 - 0
server-go/internal/roamevents/collector_1944_test.go

@@ -0,0 +1,31 @@
+package roamevents
+
+import "testing"
+
+// TestRouterHostEligible (#1944): el collector abre SSH en cada ciclo, así que
+// la lista tiene que excluir lo que no corre logread. Antes solo se miraba
+// AgentOnly y las unidades SNMP-only del despliegue recibían un dial SSH por
+// minuto que el equipo registraba como handshake fallido.
+func TestRouterHostEligible(t *testing.T) {
+	cases := []struct {
+		name string
+		host RouterHost
+		want bool
+	}{
+		{"openwrt sin tipo", RouterHost{Host: "rt1.lan"}, true},
+		{"openwrt", RouterHost{Host: "rt1.lan", Type: "openwrt"}, true},
+		{"glinet", RouterHost{Host: "flint2.lan", Type: "glinet"}, true},
+		{"managed-switch SNMP-only", RouterHost{Host: "sw1.lan", Type: "managed-switch"}, false},
+		{"external", RouterHost{Host: "ext.example", Type: "external"}, false},
+		{"agent-only", RouterHost{Host: "rt2.lan", AgentOnly: true}, false},
+		{"pausada", RouterHost{Host: "rt3.lan", Disabled: true}, false},
+		{"sin host", RouterHost{Type: "openwrt"}, false},
+	}
+	for _, tc := range cases {
+		t.Run(tc.name, func(t *testing.T) {
+			if got := tc.host.Eligible(); got != tc.want {
+				t.Fatalf("Eligible() = %v, want %v", got, tc.want)
+			}
+		})
+	}
+}