From 2380c7de9a36de9855a26f6982638bdeae1de1ba Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Fri, 20 Jan 2017 23:23:31 +0100 Subject: [PATCH] first implementation of neighbours count --- data/statistics.go | 2 +- meshviewer/meshviewer.go | 4 +- models/node.go | 31 ++++++++++++++- models/node_test.go | 84 ++++++++++++++++++++++++++++++++++++++++ models/nodes_test.go | 23 ----------- models/stats_test.go | 5 +++ 6 files changed, 122 insertions(+), 27 deletions(-) create mode 100644 models/node_test.go diff --git a/data/statistics.go b/data/statistics.go index 30e08bf..6718a33 100644 --- a/data/statistics.go +++ b/data/statistics.go @@ -19,7 +19,7 @@ type Statistics struct { Total uint32 `json:"total"` Running uint32 `json:"running"` } `json:"processes,omitempty"` - MeshVpn *MeshVPN `json:"mesh_vpn,omitempty"` + MeshVPN *MeshVPN `json:"mesh_vpn,omitempty"` Traffic struct { Tx *Traffic `json:"tx"` Rx *Traffic `json:"rx"` diff --git a/meshviewer/meshviewer.go b/meshviewer/meshviewer.go index ff1135e..b721efa 100644 --- a/meshviewer/meshviewer.go +++ b/meshviewer/meshviewer.go @@ -51,7 +51,7 @@ type Statistics struct { Total uint32 `json:"total"` Running uint32 `json:"running"` } `json:"processes,omitempty"` - MeshVpn *data.MeshVPN `json:"mesh_vpn,omitempty"` + MeshVPN *data.MeshVPN `json:"mesh_vpn,omitempty"` Traffic struct { Tx *data.Traffic `json:"tx"` Rx *data.Traffic `json:"rx"` @@ -81,7 +81,7 @@ func NewStatistics(stats *data.Statistics) *Statistics { Uptime: stats.Uptime, Idletime: stats.Idletime, Processes: stats.Processes, - MeshVpn: stats.MeshVpn, + MeshVPN: stats.MeshVPN, Traffic: stats.Traffic, Clients: total, } diff --git a/models/node.go b/models/node.go index 004af52..9a49e5f 100644 --- a/models/node.go +++ b/models/node.go @@ -18,7 +18,7 @@ type Node struct { Neighbours *data.Neighbours `json:"-"` } -// Returns tags and fields for InfluxDB +// ToInflux Returns tags and fields for InfluxDB func (node *Node) ToInflux() (tags imodels.Tags, fields imodels.Fields) { stats := node.Statistics @@ -51,6 +51,35 @@ func (node *Node) ToInflux() (tags imodels.Tags, fields imodels.Fields) { tags.SetString("hostname", nodeinfo.Hostname) } + if neighbours := node.Neighbours; neighbours != nil { + // VPN Neighbours are Neighbours but includet in one protocol + vpn := 0 + if meshvpn := stats.MeshVPN; meshvpn != nil { + for _, group := range meshvpn.Groups { + for _, link := range group.Peers { + if link.Established > 1 { + vpn++ + } + } + } + } + fields["neighbours.vpn"] = vpn + + // protocol: Batman Advance + batadv := 0 + for _, batadvNeighbours := range neighbours.Batadv { + batadv += len(batadvNeighbours.Neighbours) + } + fields["neighbours.batadv"] = batadv + + // protocol: LLDP + lldp := len(neighbours.LLDP) + fields["neighbours.lldp"] = lldp + + // total is the sum of all protocols + fields["neighbours.total"] = batadv + lldp + } + if t := stats.Traffic.Rx; t != nil { fields["traffic.rx.bytes"] = int64(t.Bytes) fields["traffic.rx.packets"] = t.Packets diff --git a/models/node_test.go b/models/node_test.go new file mode 100644 index 0000000..641dec1 --- /dev/null +++ b/models/node_test.go @@ -0,0 +1,84 @@ +package models + +import ( + "testing" + + "github.com/FreifunkBremen/respond-collector/data" + "github.com/stretchr/testify/assert" +) + +func TestToInflux(t *testing.T) { + assert := assert.New(t) + + node := Node{ + Statistics: &data.Statistics{ + NodeID: "foobar", + LoadAverage: 0.5, + Wireless: data.WirelessStatistics{ + &data.WirelessAirtime{Frequency: 5500}, + }, + Traffic: struct { + Tx *data.Traffic `json:"tx"` + Rx *data.Traffic `json:"rx"` + Forward *data.Traffic `json:"forward"` + MgmtTx *data.Traffic `json:"mgmt_tx"` + MgmtRx *data.Traffic `json:"mgmt_rx"` + }{ + Tx: &data.Traffic{Dropped: 1321}, + Rx: &data.Traffic{Bytes: 1213}, + Forward: &data.Traffic{Bytes: 1322}, + MgmtTx: &data.Traffic{Packets: 2327}, + MgmtRx: &data.Traffic{Bytes: 2331}, + }, + MeshVPN: &data.MeshVPN{ + Groups: map[string]*data.MeshVPNPeerGroup{ + "ffhb": &data.MeshVPNPeerGroup{ + Peers: map[string]*data.MeshVPNPeerLink{ + "vpn01": &data.MeshVPNPeerLink{Established: 3}, + "vpn02": &data.MeshVPNPeerLink{}, + "vpn03": &data.MeshVPNPeerLink{Established: 0}, + }, + }, + }, + }, + }, + Nodeinfo: &data.NodeInfo{ + Owner: &data.Owner{ + Contact: "nobody", + }, + Wireless: &data.Wireless{ + TxPower24: 3, + Channel24: 4, + }, + }, + Neighbours: &data.Neighbours{ + Batadv: map[string]data.BatadvNeighbours{ + "a-interface": data.BatadvNeighbours{ + Neighbours: map[string]data.BatmanLink{ + "b-neigbourinterface": data.BatmanLink{}, + }, + }, + }, + }, + } + + tags, fields := node.ToInflux() + + assert.Equal("foobar", tags.GetString("nodeid")) + assert.Equal("nobody", tags.GetString("owner")) + assert.Equal(0.5, fields["load"]) + assert.Equal(0, fields["neighbours.lldp"]) + assert.Equal(1, fields["neighbours.batadv"]) + assert.Equal(1, fields["neighbours.vpn"]) + assert.Equal(1, fields["neighbours.total"]) + + assert.Equal(uint32(3), fields["wireless.txpower24"]) + assert.Equal(uint32(5500), fields["airtime11a.frequency"]) + assert.Equal("", tags.GetString("frequency5500")) + + assert.Equal(int64(1213), fields["traffic.rx.bytes"]) + assert.Equal(float64(1321), fields["traffic.tx.dropped"]) + assert.Equal(int64(1322), fields["traffic.forward.bytes"]) + assert.Equal(int64(2331), fields["traffic.mgmt_rx.bytes"]) + assert.Equal(float64(2327), fields["traffic.mgmt_tx.packets"]) +} diff --git a/models/nodes_test.go b/models/nodes_test.go index af88e7a..e96ca1e 100644 --- a/models/nodes_test.go +++ b/models/nodes_test.go @@ -73,26 +73,3 @@ func TestUpdateNodes(t *testing.T) { assert.Equal(1, len(nodes.List)) } - -func TestToInflux(t *testing.T) { - assert := assert.New(t) - - node := Node{ - Statistics: &data.Statistics{ - NodeID: "foobar", - LoadAverage: 0.5, - }, - Nodeinfo: &data.NodeInfo{ - Owner: &data.Owner{ - Contact: "nobody", - }, - }, - Neighbours: &data.Neighbours{}, - } - - tags, fields := node.ToInflux() - - assert.Equal("foobar", tags.GetString("nodeid")) - assert.Equal("nobody", tags.GetString("owner")) - assert.Equal(0.5, fields["load"]) -} diff --git a/models/stats_test.go b/models/stats_test.go index 00af87f..9608ef0 100644 --- a/models/stats_test.go +++ b/models/stats_test.go @@ -23,6 +23,11 @@ func TestGlobalStats(t *testing.T) { // check firmwares assert.EqualValues(1, len(stats.Firmwares)) assert.EqualValues(1, stats.Firmwares["2016.1.6+entenhausen1"]) + + fields := stats.Fields() + + // check fields + assert.EqualValues(3, fields["nodes"]) } func TestNodesV1(t *testing.T) {