| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665 |
- package adapters
- import (
- "fmt"
- "log"
- "math"
- "net"
- "os"
- "strings"
- "time"
- "github.com/gnacho/netpulse/server-go/internal/alerts"
- npsnmp "github.com/gnacho/netpulse/server-go/internal/snmp"
- "golang.org/x/net/icmp"
- "golang.org/x/net/ipv4"
- )
- // snmpSessionConfig traduce la configuración de la unidad al contrato del
- // poller. En v1/v2c va la comunidad y en v3 las credenciales USM (#2042):
- // mandar las dos cosas no rompe nada, pero deja el error más claro cuando
- // falta lo que la versión pide.
- func snmpSessionConfig(cfg RouterConfig) npsnmp.Config {
- c := npsnmp.Config{
- Host: cfg.Host,
- Port: cfg.SnmpPort,
- Version: npsnmp.ParseVersion(cfg.SnmpVersion),
- }
- if cfg.SnmpVersion == "3" {
- c.V3User = cfg.SnmpV3User
- c.V3SecurityLevel = cfg.SnmpV3SecurityLevel
- c.V3AuthProto = cfg.SnmpV3AuthProto
- c.V3AuthPass = cfg.SnmpV3AuthPass
- c.V3PrivProto = cfg.SnmpV3PrivProto
- c.V3PrivPass = cfg.SnmpV3PrivPass
- } else {
- c.Community = cfg.SnmpCommunity
- }
- return c
- }
- func (l *Live) pollRouterSNMP(cfg RouterConfig) (*routerPolled, error) {
- // issue #414: si no ha pasado el intervalo configurado, reutiliza el último
- // sondeo real para no martillear al switch con SNMP cada 5 s.
- interval := cfg.SnmpPollInterval
- if interval <= 0 {
- interval = 60
- }
- now := l.now()
- l.mu.Lock()
- last, hasLast := l.lastPolled[cfg.ID]
- lastPoll := l.snmpLastPoll[cfg.ID]
- l.mu.Unlock()
- if hasLast && last != nil && now.Sub(lastPoll) < time.Duration(interval)*time.Second {
- return last, nil
- }
- session, err := npsnmp.NewSession(snmpSessionConfig(cfg))
- if err != nil {
- // #930: error de sesión = fallo del poll SNMP (lastErr + consecFail).
- l.recordSnmpFailure(cfg, err)
- return nil, fmt.Errorf("snmp %s: %w", cfg.Host, err)
- }
- defer npsnmp.CloseSession(session)
- sysInfo, sysErr := npsnmp.PollSystem(session)
- if sysErr != nil {
- log.Printf("[netpulse] SNMP system %s: %v", cfg.LogLabel(), sysErr)
- }
- // #1256 + #1725: temperatura del chasis en switches MikroTik. Se lee la
- // health table por el NOMBRE de la sonda ("cpu-temperature"), no por el
- // índice 52 con `.0` que #1256 fijó de un SwOS: en RouterOS la tabla está
- // ahí mismo pero en otra fila, y el Get nunca contestaba (medido:
- // NoSuchObject en un CRS305 con 51 °C disponibles). Best-effort: los
- // switches que no exponen nada dejan hasTemp false (vitals a null como
- // antes, #441).
- descr := ""
- if sysInfo != nil {
- descr = sysInfo.Descr
- }
- mikroTemp, hasTemp := npsnmp.PollMikrotikTemp(session, descr)
- // Netgear FastPath (XS708T, GS728TXP/S3300…): CPU por proceso y sensores
- // de temperatura en su MIB privada (4526.11). Best-effort igual que el
- // MikroTik: sin esos árboles, ok=false y los vitales siguen a null.
- ngCpu, hasNgCpu, ngTemp, hasNgTemp := npsnmp.PollNetgearVitals(session)
- // #1725: HOST-RESOURCES (hrProcessorLoad / hrStorageTable "main memory")
- // para los equipos sin MIB privada de vitales (RouterOS por SNMP es el
- // caso real). Se pregunta primero y una sola vez por equipo
- // (hostResourcesKnown), porque hay agentes que descartan el árbol en
- // silencio y cada operación así cuesta DefaultTimeout×(Retries+1)=10 s.
- hrCpu, hasHrCpu, hrRam, hasHrRam := 0, false, 0, false
- if l.hostResourcesKnown(cfg.ID, func() bool { return npsnmp.HostResourcesPresent(session) }) {
- hrCpu, hasHrCpu, hrRam, hasHrRam = npsnmp.PollHostResourcesVitals(session)
- }
- // #1036: MAC propia del switch (dot1dBaseBridgeAddress). Sin ella el
- // equipo no tenía MAC en flota ni casaba por MAC en topología. Cache:
- // si el OID no contesta en este poll se conserva la última conocida.
- brMac, _ := npsnmp.PollBridgeAddress(session)
- l.mu.Lock()
- if brMac == "" {
- brMac = l.snmpBrMac[cfg.ID]
- } else {
- l.snmpBrMac[cfg.ID] = brMac
- }
- l.mu.Unlock()
- // #1767: ENTITY-MIB identity (firmware and short model). Generic RFC
- // 6933 columns; one walk, and a switch without the table leaves both
- // fields empty. Best-effort like the bridge MAC above.
- entity, _ := npsnmp.PollEntityInfo(session)
- // #1802: TP-Link firmware ignores ENTITY-MIB and HOST-RESOURCES-MIB but
- // publishes the same identity and vitals in its enterprise subtree. The
- // sysObjectID selects the vendor before paying for its OIDs; the private
- // MIB wins over the generic fallback, as the panel of the unit does.
- tpLink := npsnmp.TpLinkInfo{}
- if sysInfo != nil && npsnmp.IsTpLink(sysInfo.ObjectID) {
- tpLink = npsnmp.PollTpLinkInfo(session)
- }
- // Netgear FastPath publica además el chip del switch en su subtree
- // (BCM53412 / BCM53346). Ninguna otra fuente SNMP lo da: las unidades
- // OpenWrt lo leen por agente, y un gestionado sin agente no tiene dónde
- // decirlo. El firmware y el modelo corto también están aquí, pero ENTITY-MIB
- // ya los responde en esta familia, así que se usan solo de respaldo.
- netgear := npsnmp.NetgearIdentity{}
- if sysInfo != nil && npsnmp.IsNetgear(sysInfo.ObjectID) {
- netgear = npsnmp.PollNetgearIdentity(session)
- }
- ports, ifErr := npsnmp.PollIfTable(session)
- if ifErr != nil {
- log.Printf("[netpulse] SNMP ifTable %s: %v", cfg.LogLabel(), ifErr)
- }
- fdbPoll, fdbErr := npsnmp.PollFdbTable(session)
- fdb := fdbPoll.Entries
- if fdbErr != nil {
- log.Printf("[netpulse] SNMP FDB %s: %v", cfg.LogLabel(), fdbErr)
- } else {
- // #928: visibilidad mínima del resultado del FDB. Se loguea cuando el
- // conteo cambia respecto al ciclo anterior (o en el primer poll), no
- // cada 60 s: suficiente para diagnosticar "walk vacío/fallido" sin
- // inundar el journal. #948: la línea lleva además los PDUs crudos de
- // cada walk para distinguir "tabla vacía" de "datos no utilizables".
- l.mu.Lock()
- last, seen := l.snmpFdbCount[cfg.ID]
- l.snmpFdbCount[cfg.ID] = len(fdb)
- l.mu.Unlock()
- if !seen || last != len(fdb) {
- log.Printf("[netpulse] SNMP FDB %s: %d entries (source %s, pdus dot1d=%d dot1q=%d)", cfg.LogLabel(), len(fdb), fdbPoll.Source, fdbPoll.RawDot1d, fdbPoll.RawDot1q)
- }
- }
- // #930: salud del poll SNMP. Un poll se considera fallido cuando la
- // sesión falla (arriba), el Get de sistema no responde o el walk ifTable
- // no devuelve puertos. En fallo NO se devuelve error al caller (el router
- // puede estar vivo: es la comunidad/el puerto lo que falla); el error se
- // refleja en los contadores y, al 3.er fallo, en el ping de respaldo.
- if len(ports) == 0 {
- pollErr := ifErr
- if pollErr == nil {
- if sysErr != nil {
- pollErr = sysErr
- } else {
- pollErr = fmt.Errorf("snmp ifTable %s: no ports", cfg.Host)
- }
- }
- l.recordSnmpFailure(cfg, pollErr)
- } else {
- l.recordSnmpSuccess(cfg)
- }
- // #931: vecinos LLDP del switch vía LLDP-MIB (mismo contrato que la
- // sonda lldpd de los routers, #300). Alimenta routerPolled.lldp y con
- // ello la fase LLDP-first de la topología (inferLldpLinks). Un switch
- // sin LLDP devuelve (nil, nil) sin ruido; el error solo se loguea.
- lldpRem, lldpErr := npsnmp.PollLldpRemTable(session)
- if lldpErr != nil {
- log.Printf("[netpulse] SNMP LLDP %s: %v", cfg.LogLabel(), lldpErr)
- }
- portIdxToName := map[int]string{}
- for _, p := range ports {
- portIdxToName[p.Index] = p.DisplayName()
- }
- ethPorts := make([]EthPort, 0, len(ports))
- for _, p := range ports {
- name := p.DisplayName()
- label := name
- ethPorts = append(ethPorts, EthPort{
- ID: fmt.Sprintf("snmp-%d", p.Index),
- Label: label,
- Up: p.OperUp,
- Speed: p.SpeedString(),
- Iface: name,
- Family: p.Family,
- RxBytes: p.RxBytes,
- TxBytes: p.TxBytes,
- RxErrs: p.RxErrors,
- TxErrs: p.TxErrors,
- })
- }
- for i := range ethPorts {
- ethPorts[i].Snmp = true
- }
- prevPorts := l.snmpPrevPorts(cfg.ID)
- l.mu.Lock()
- l.snmpPortCache(cfg.ID, ethPorts)
- l.mu.Unlock()
- for i := range ethPorts {
- prev, ok := prevPorts[ethPorts[i].ID]
- if !ok {
- continue
- }
- dt := time.Since(prev.at).Seconds()
- if dt <= 0 {
- continue
- }
- if ethPorts[i].RxBytes >= prev.rxBytes {
- ethPorts[i].RxBps = math.Round(float64(ethPorts[i].RxBytes-prev.rxBytes)*8/dt*10) / 10
- }
- if ethPorts[i].TxBytes >= prev.txBytes {
- ethPorts[i].TxBps = math.Round(float64(ethPorts[i].TxBytes-prev.txBytes)*8/dt*10) / 10
- }
- }
- // #661: el path SNMP nunca persistía las muestras de puerto, así que el
- // historial de tráfico del switch quedaba vacío aunque el sondeo leyera los
- // contadores correctamente. Ahora se registran (misma ruta que SSH/agente).
- l.recordPortSamples(cfg.ID, ethPorts)
- // FDB → mapa MAC→puerto. Antes se descartaban las entradas cuyo ifIndex no
- // resolvía a un puerto conocido, lo que dejaba el switch con 0 dispositivos
- // conectados aunque el FDB viniera lleno (#661). Se cae a bridgePort y a un
- // nombre generado para no perder MACs (el conteo de clientes solo necesita
- // la clave; el nombre es secundario).
- fdbMap := snmpFdbMap(fdb, portIdxToName)
- selfMacs := snmpSelfMacs(fdb, portIdxToName)
- l.portMon.Observe(cfg.ID, ethPorts, l.engine)
- // #661: agregado de red del switch (suma de la tasa de todos los puertos)
- // para que la tarjeta de la flota pinte un sparkline con bps reales en
- // lugar de la línea plana a 0 (los switches SNMP sí tienen contadores de
- // bytes, a diferencia de los beacons, que solo reportan tramas).
- netPtr := snmpAggregateNet(ethPorts)
- var uptimeSec float64
- if sysInfo != nil {
- uptimeSec = float64(sysInfo.UpTimeSec)
- }
- p := &routerPolled{
- cfg: cfg,
- uptimeSec: uptimeSec,
- hasUptime: sysInfo != nil,
- net: netPtr,
- ports: ethPorts,
- fdb: fdbMap,
- selfMacs: selfMacs,
- brMac: brMac,
- lldp: snmpLldpNeighbors(lldpRem, portIdxToName),
- polledAt: now.UnixMilli(),
- }
- // #1767: ENTITY-MIB is optional identity, not a vitals source.
- p.snmpFirmware = entity.Firmware
- p.snmpModelShort = entity.ModelShort
- // #1802: fill only what the generic readers could not answer.
- if p.snmpFirmware == "" {
- p.snmpFirmware = tpLink.Firmware
- }
- if p.snmpModelShort == "" {
- p.snmpModelShort = tpLink.Hardware
- }
- if p.snmpFirmware == "" {
- p.snmpFirmware = netgear.Firmware
- }
- if p.snmpModelShort == "" {
- p.snmpModelShort = netgear.ModelShort
- }
- // El chip no tiene equivalente genérico: lo que dice el firmware del
- // switch es la única fuente, y se conserva tal cual (sin recorte ni
- // sustituto por arquitectura, que es lo que #43 y #45 retiraron).
- if netgear.SoC != "" {
- p.snmpSoC = netgear.SoC
- }
- if tpLink.HasCPU {
- p.cpu, p.hasCpu = tpLink.CPU, true
- }
- if tpLink.HasMemory {
- p.ram, p.hasRam = tpLink.Memory, true
- }
- if sysInfo != nil {
- // #1256: sysDescr declara el modelo completo del equipo (p. ej.
- // "CSS610-8G-2S+ SwOS v2.21") y sysName el hostname que define el
- // usuario; buildRouter los reparte entre Model y Name.
- p.sysDescr = sysInfo.Descr
- p.sysName = sysInfo.Name
- }
- p.temp, p.hasTemp = mikroTemp, hasTemp
- if !p.hasTemp && hasNgTemp {
- p.temp, p.hasTemp = ngTemp, true
- }
- if hasNgCpu {
- p.cpu, p.hasCpu = ngCpu, true
- }
- // #1725: HOST-RESOURCES solo rellena huecos (veáse
- // applyHostResourcesVitals).
- applyHostResourcesVitals(p, hrCpu, hasHrCpu, hrRam, hasHrRam)
- l.mu.Lock()
- l.snmpLastPoll[cfg.ID] = now
- l.mu.Unlock()
- return p, nil
- }
- // snmpHrEntry (#1725): si el agente de este equipo publica HOST-RESOURCES, y
- // cuándo se preguntó por última vez.
- type snmpHrEntry struct {
- at time.Time
- ok bool
- }
- // snmpHrProbeTTL: cada cuánto se vuelve a preguntar. Un firmware nuevo puede
- // añadir el árbol, y seis horas es un margen con el que nadie espera ver una
- // vital distinta; a cambio, un equipo que lo descarta en silencio paga UN
- // timeout por ventana en lugar de uno por poll (pollRouterSNMP corre cada
- // snmp_poll_interval, 60 s por defecto).
- const snmpHrProbeTTL = 6 * time.Hour
- // hostResourcesKnown cachea la respuesta de la pregunta "¿este agente tiene
- // HOST-RESOURCES?". El probe va como callback porque el paquete adapters no
- // importa gosnmp: la sesión SNMP sigue siendo un detalle de internal/snmp.
- func (l *Live) hostResourcesKnown(routerID string, probe func() bool) bool {
- now := l.now()
- l.mu.Lock()
- entry, seen := l.snmpHr[routerID]
- l.mu.Unlock()
- if seen && now.Sub(entry.at) < snmpHrProbeTTL {
- return entry.ok
- }
- ok := probe()
- l.mu.Lock()
- if l.snmpHr == nil {
- l.snmpHr = map[string]snmpHrEntry{}
- }
- l.snmpHr[routerID] = snmpHrEntry{at: now, ok: ok}
- l.mu.Unlock()
- return ok
- }
- // applyHostResourcesVitals (#1725): HOST-RESOURCES es respaldo, no segunda
- // opinión. Una lectura privada (la health table de MikroTik, el FastPath de
- // Netgear) es lo que el panel del propio equipo muestra; el árbol genérico mide
- // de otra forma, así que aquí solo rellena lo que quedó sin bandera. Y sin
- // bandera no inventa: ok=false no escribe ni un cero (#441).
- func applyHostResourcesVitals(p *routerPolled, cpu int, okCpu bool, ramPct int, okRam bool) {
- if p == nil {
- return
- }
- if !p.hasCpu && okCpu {
- p.cpu, p.hasCpu = cpu, true
- }
- if !p.hasRam && okRam {
- p.ram, p.hasRam = ramPct, true
- }
- }
- // snmpPollStat (issue #930): salud del sondeo SNMP por router. En memoria,
- // protegido por l.mu. failingOpen marca el incidente ABIERTO (alerta emitida
- // y aún no recuperada) para no repetir la alerta hasta la recuperación.
- type snmpPollStat struct {
- Ok int64
- Fail int64
- ConsecFail int64
- LastOkMs int64
- LastFailMs int64
- LastErr string
- failingOpen bool
- }
- // snmpStat devuelve (creando si falta) el contador de salud SNMP del router.
- // Debe llamarse con l.mu tomado.
- func (l *Live) snmpStat(id string) *snmpPollStat {
- if l.snmpPollStats == nil {
- l.snmpPollStats = map[string]*snmpPollStat{}
- }
- s := l.snmpPollStats[id]
- if s == nil {
- s = &snmpPollStat{}
- l.snmpPollStats[id] = s
- }
- return s
- }
- // recordSnmpSuccess registra un poll SNMP con datos (#930): ok++,
- // consecFail=0. Si había un incidente abierto, lo resuelve y emite la alerta
- // ok de recuperación (patrón agente outdated/updated).
- func (l *Live) recordSnmpSuccess(cfg RouterConfig) {
- name := cfg.Name
- if name == "" {
- name = cfg.Host
- }
- now := time.Now().UnixMilli()
- l.mu.Lock()
- s := l.snmpStat(cfg.ID)
- s.Ok++
- s.ConsecFail = 0
- s.LastOkMs = now
- wasOpen := s.failingOpen
- s.failingOpen = false
- l.mu.Unlock()
- if wasOpen {
- log.Printf("[netpulse] SNMP recuperado %s", cfg.LogLabel())
- failID := fmt.Sprintf("alert-snmp-failing-%s", cfg.ID)
- l.engine.Resolve(failID)
- l.engine.Emit(AlertEvent{
- ID: fmt.Sprintf("alert-snmp-recovered-%s-%d", cfg.ID, now),
- Category: alerts.CatSystem, Urgent: false,
- Severity: "ok",
- Title: "SNMP polling recovered on " + name,
- Description: fmt.Sprintf("SNMP polling for %s is responding again", name),
- Type: alerts.TypeSnmpRecovered,
- Vars: map[string]string{"router": name},
- Resolves: []string{failID}, // #1349
- Time: "just now", RouterID: cfg.ID,
- })
- }
- }
- // recordSnmpFailure registra un poll SNMP fallido (#930): fail++,
- // consecFail++, lastErr. En la transición al 3.er fallo seguido hace un ping
- // de respaldo al host: si responde, emite una alerta warn "snmp-failing"
- // (una por incidente); si no responde, solo loguea (la caída del router ya
- // la cubre el alerteo de offline).
- func (l *Live) recordSnmpFailure(cfg RouterConfig, err error) {
- name := cfg.Name
- if name == "" {
- name = cfg.Host
- }
- now := time.Now().UnixMilli()
- l.mu.Lock()
- s := l.snmpStat(cfg.ID)
- s.Fail++
- s.ConsecFail++
- s.LastFailMs = now
- s.LastErr = err.Error()
- consec := s.ConsecFail
- open := s.failingOpen
- lastErr := s.LastErr
- l.mu.Unlock()
- switch {
- case consec == 1:
- log.Printf("[netpulse] SNMP failure %s: %v", cfg.LogLabel(), err)
- case consec == 3 && !open:
- pingFn := l.ping
- if pingFn == nil {
- pingFn = pingHost
- }
- if !pingFn(cfg.Host) {
- log.Printf("[netpulse] SNMP %s: 3 consecutive failures and no ping response (device down), no SNMP alert", cfg.LogLabel())
- return
- }
- // Marcar incidente abierto (re-check bajo lock para no duplicar en
- // carreras entre goroutines de distintos routers).
- l.mu.Lock()
- s2 := l.snmpStat(cfg.ID)
- already := s2.failingOpen
- s2.failingOpen = true
- l.mu.Unlock()
- if already {
- return
- }
- l.engine.Emit(AlertEvent{
- ID: fmt.Sprintf("alert-snmp-failing-%s", cfg.ID),
- Category: alerts.CatSystem, Urgent: false,
- Severity: "warn",
- Title: "SNMP polling failing on " + name,
- Description: fmt.Sprintf("%s responds to ping but SNMP polling has failed 3 times (%s) - check community/port", name, lastErr),
- Hint: alerts.HintFor(alerts.HintSnmpFailing),
- Type: alerts.HintSnmpFailing,
- Vars: map[string]string{"router": name, "error": lastErr},
- Time: "just now", RouterID: cfg.ID,
- })
- }
- }
- // pingHost hace un ping ICMP (echo request) sin privilegios (#930): usa un
- // socket UDP no privilegiado (ListenPacket("udp4"), soportado por el
- // ping_group_range del kernel) en vez de un socket raw que requeriría root.
- // Devuelve true si el host responde al echo dentro de ~2 s.
- func pingHost(host string) bool {
- ip, err := net.ResolveIPAddr("ip4", host)
- if err != nil {
- return false
- }
- c, err := icmp.ListenPacket("udp4", "0.0.0.0")
- if err != nil {
- return false
- }
- defer c.Close()
- msg := icmp.Message{
- Type: ipv4.ICMPTypeEcho,
- Code: 0,
- Body: &icmp.Echo{
- ID: os.Getpid() & 0xffff,
- Seq: 1,
- Data: []byte("netpulse"),
- },
- }
- b, err := msg.Marshal(nil)
- if err != nil {
- return false
- }
- if _, err := c.WriteTo(b, &net.UDPAddr{IP: ip.IP}); err != nil {
- return false
- }
- if err := c.SetReadDeadline(time.Now().Add(2 * time.Second)); err != nil {
- return false
- }
- rb := make([]byte, 1500)
- for {
- n, _, err := c.ReadFrom(rb)
- if err != nil {
- return false
- }
- rm, err := icmp.ParseMessage(1, rb[:n])
- if err != nil {
- continue
- }
- if rm.Type == ipv4.ICMPTypeEchoReply {
- if echo, ok := rm.Body.(*icmp.Echo); ok && echo.ID == os.Getpid()&0xffff {
- return true
- }
- }
- }
- }
- type snmpPortSample struct {
- at time.Time
- rxBytes uint64
- txBytes uint64
- }
- func (l *Live) snmpPrevPorts(routerID string) map[string]snmpPortSample {
- l.mu.Lock()
- defer l.mu.Unlock()
- if l.snmpPorts == nil {
- return nil
- }
- return l.snmpPorts[routerID]
- }
- func (l *Live) snmpPortCache(routerID string, ports []EthPort) {
- if l.snmpPorts == nil {
- l.snmpPorts = map[string]map[string]snmpPortSample{}
- }
- samples := map[string]snmpPortSample{}
- now := time.Now()
- for _, p := range ports {
- samples[p.ID] = snmpPortSample{at: now, rxBytes: p.RxBytes, txBytes: p.TxBytes}
- }
- l.snmpPorts[routerID] = samples
- }
- // snmpFdbMap convierte el FDB (MAC→puerto) en el mapa que consume la
- // topología/dispositivos. #661: NO descarta entradas cuyo ifIndex no resuelve
- // a un puerto conocido (antes eso dejaba el switch con 0 dispositivos);
- // cae a bridgePort y, en último caso, a un nombre generado.
- func snmpFdbMap(fdb []npsnmp.FdbEntry, portIdxToName map[int]string) map[string]string {
- out := map[string]string{}
- for _, e := range fdb {
- name := ""
- if n, ok := portIdxToName[e.IfIndex]; ok {
- name = n
- } else if n, ok := portIdxToName[e.BridgePortIndex]; ok {
- name = n
- }
- if name == "" {
- name = fmt.Sprintf("port-%d", e.IfIndex)
- }
- if snmpInternalPort(name) {
- continue
- }
- out[e.MAC] = name
- }
- return out
- }
- // snmpSelfMacs: MACs aprendidas en los puertos internos de la unidad (CPU,
- // stack, mgmt). Son identidades del propio equipo, no clientes: sirven para
- // absorberlas también donde las aprenda un vecino (p.ej. la br-lan del
- // gateway al gestionarlo), que es el segundo fantasma de #1489.
- func snmpSelfMacs(fdb []npsnmp.FdbEntry, portIdxToName map[int]string) []string {
- out := []string{}
- for _, e := range fdb {
- name := ""
- if n, ok := portIdxToName[e.IfIndex]; ok {
- name = n
- } else if n, ok := portIdxToName[e.BridgePortIndex]; ok {
- name = n
- }
- if name == "" || !snmpInternalPort(name) {
- continue
- }
- out = append(out, e.MAC)
- }
- return out
- }
- // snmpInternalPort: puerto interno del propio switch (CPU/stack/mgmt). La MAC
- // que aparece ahí es del equipo, no de un cliente: un NETGEAR aprende su
- // propio CPU MAC en su puerto interno y lo publicaba como cliente fantasma
- // (#1489). Heurística sobre el nombre del puerto, que es lo único que expone
- // la ifTable de estos switches para esa interfaz.
- func snmpInternalPort(name string) bool {
- u := strings.ToUpper(name)
- return strings.Contains(u, "CPU") || strings.Contains(u, "INTERNAL")
- }
- // snmpLldpNeighbors convierte los vecinos LLDP-MIB del switch al contrato
- // LldpNeighbor (#931). El puerto se nombra como en el FDB (DisplayName del
- // ifTable vía ifIndex; fallback al número de puerto de bridge, paridad con
- // snmpFdbMap #661) para que inferLldpLinks case el vecino con las MACs
- // aprendidas en esa boca.
- func snmpLldpNeighbors(rem []npsnmp.LldpRemEntry, portIdxToName map[int]string) []LldpNeighbor {
- if len(rem) == 0 {
- return nil
- }
- out := make([]LldpNeighbor, 0, len(rem))
- for _, e := range rem {
- port := portIdxToName[e.IfIndex]
- if port == "" {
- port = portIdxToName[e.LocalPortNum]
- }
- if port == "" {
- port = fmt.Sprintf("port-%d", e.LocalPortNum)
- }
- out = append(out, LldpNeighbor{
- Port: port,
- ChassisMac: e.ChassisMac,
- Chassis: e.Chassis,
- Mgmt: e.Mgmt,
- Caps: e.Caps,
- PortDesc: e.PortDesc,
- ChassisFromLocDesc: e.ChassisFromLocDesc,
- })
- }
- return out
- }
- // snmpAggregateNet suma la tasa (bps) de todos los puertos para dar una
- // métrica agregada al switch SNMP (#661). Devuelve nil si todo está a 0
- // (primera muestra sin delta o sin tráfico).
- func snmpAggregateNet(ports []EthPort) *NetDevBps {
- var aggRx, aggTx float64
- for i := range ports {
- aggRx += ports[i].RxBps
- aggTx += ports[i].TxBps
- }
- if aggRx == 0 && aggTx == 0 {
- return nil
- }
- rx, tx := aggRx, aggTx
- return &NetDevBps{RxBps: &rx, TxBps: &tx}
- }
|