switchporttraffic.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // switchporttraffic.go — per-client traffic for wired devices learned only in
  2. // a managed SNMP switch's FDB (issue #1543).
  3. //
  4. // A managed switch has no per-MAC accounting (nlbwmon is an OpenWrt/agent
  5. // feature), and switch-local flows (e.g. a camera streaming to an NVR on the
  6. // same switch) never cross the gateway, so the gateway's per-MAC rate reports
  7. // only the tiny gateway-bound slice and the client looks idle. The switch's own
  8. // port byte counters (ifTable octets, surfaced as EthPort.RxBps/TxBps) DO see
  9. // the whole flow, so we attribute a port's rx+tx rate to the clients the
  10. // switch's FDB learns on it, split evenly so a trunk carrying several MACs is
  11. // not duplicated onto each. Only SNMP-enabled routers are consulted and a
  12. // client's rate is only ever raised, so agent-reported clients and any higher
  13. // existing figure are left untouched.
  14. package adapters
  15. import "strings"
  16. // applySnmpSwitchPortTraffic raises each online client attributed to an
  17. // SNMP-enabled router to its FDB port's rate (Mbps), split across the online
  18. // clients sharing that port. It runs at the end of buildDevices, before
  19. // inferTopology fills Device.Port, so the client's port is resolved from the
  20. // router's own FDB (mac -> port, uppercased) rather than Device.Port.
  21. func (l *Live) applySnmpSwitchPortTraffic(polled map[string]*routerPolled, devices []Device) {
  22. type portKey struct{ router, port string }
  23. // Mbps per (router, port name). Indexed by every alias a port can carry so
  24. // the FDB's name (its DisplayName = Label/Iface) resolves.
  25. rate := map[portKey]float64{}
  26. for rID, p := range polled {
  27. if p == nil || !p.cfg.SnmpEnabled {
  28. continue
  29. }
  30. for _, ep := range p.ports {
  31. bps := ep.RxBps + ep.TxBps
  32. if bps <= 0 {
  33. continue
  34. }
  35. mp := bps / 1e6
  36. for _, name := range []string{ep.ID, ep.Label, ep.Iface} {
  37. if name != "" {
  38. rate[portKey{rID, name}] = mp
  39. }
  40. }
  41. }
  42. }
  43. if len(rate) == 0 {
  44. return
  45. }
  46. // A client's port comes from the switch's own FDB (mac -> port), the same
  47. // uppercased-MAC key the topology step uses.
  48. resolve := func(d *Device) (portKey, bool) {
  49. if !d.Online || d.RouterID == "" {
  50. return portKey{}, false
  51. }
  52. p := polled[d.RouterID]
  53. if p == nil || !p.cfg.SnmpEnabled || len(p.fdb) == 0 {
  54. return portKey{}, false
  55. }
  56. port, ok := p.fdb[strings.ToUpper(d.MAC)]
  57. if !ok {
  58. return portKey{}, false
  59. }
  60. k := portKey{d.RouterID, port}
  61. return k, rate[k] > 0
  62. }
  63. keys := make([]portKey, len(devices))
  64. matched := make([]bool, len(devices))
  65. share := map[portKey]int{}
  66. for i := range devices {
  67. if k, ok := resolve(&devices[i]); ok {
  68. keys[i], matched[i] = k, true
  69. share[k]++
  70. }
  71. }
  72. for i := range devices {
  73. if !matched[i] || share[keys[i]] <= 0 {
  74. continue
  75. }
  76. if mp := rate[keys[i]] / float64(share[keys[i]]); mp > devices[i].TrafficMbps {
  77. devices[i].TrafficMbps = mp
  78. }
  79. }
  80. }