diff --git a/database/influxdb/stats_test.go b/database/influxdb/stats_test.go index 946c134..31d5fb4 100644 --- a/database/influxdb/stats_test.go +++ b/database/influxdb/stats_test.go @@ -10,13 +10,13 @@ import ( ) func TestGlobalStats(t *testing.T) { - stats := runtime.NewGlobalStats(createTestNodes()) + stats := runtime.NewGlobalStats(createTestNodes(), "ffhb01") assert := assert.New(t) fields := GlobalStatsFields(stats) // check fields - assert.EqualValues(3, fields["nodes"]) + assert.EqualValues(2, fields["nodes"]) } func createTestNodes() *runtime.Nodes { @@ -32,6 +32,9 @@ func createTestNodes() *runtime.Nodes { Hardware: data.Hardware{ Model: "TP-Link 841", }, + System: data.System{ + SiteCode: "ffhb01", + }, }, } nodeData.NodeInfo.Software.Firmware.Release = "2016.1.6+entenhausen1" @@ -47,6 +50,9 @@ func createTestNodes() *runtime.Nodes { Hardware: data.Hardware{ Model: "TP-Link 841", }, + System: data.System{ + SiteCode: "ffhb01", + }, }, }) diff --git a/respond/collector.go b/respond/collector.go index f6f5935..65b87ab 100644 --- a/respond/collector.go +++ b/respond/collector.go @@ -22,6 +22,7 @@ type Collector struct { iface string db database.Connection nodes *runtime.Nodes + sites []string interval time.Duration // Interval for multicast packets stop chan interface{} } @@ -207,6 +208,11 @@ func (coll *Collector) saveResponse(addr net.UDPAddr, res *data.ResponseData) { // Process the data and update IP address node := coll.nodes.Update(nodeID, res) node.Address = addr.IP + if nodeinfo := node.Nodeinfo; nodeinfo != nil { + if len(nodeinfo.System.SiteCode) > 0 { + coll.sites = append(coll.sites, nodeinfo.System.SiteCode) + } + } // Store statistics in InfluxDB if coll.db != nil && node.Statistics != nil { @@ -249,7 +255,8 @@ func (coll *Collector) globalStatsWorker() { // saves global statistics func (coll *Collector) saveGlobalStats() { - stats := runtime.NewGlobalStats(coll.nodes) - - coll.db.AddStatistics(stats, time.Now()) + for _, site := range coll.sites { + stats := runtime.NewGlobalStats(coll.nodes, site) + coll.db.AddStatistics(stats, time.Now()) + } } diff --git a/runtime/stats.go b/runtime/stats.go index ec5bb64..04e1895 100644 --- a/runtime/stats.go +++ b/runtime/stats.go @@ -17,32 +17,33 @@ type GlobalStats struct { } //NewGlobalStats returns global statistics for InfluxDB -func NewGlobalStats(nodes *Nodes) (result *GlobalStats) { +func NewGlobalStats(nodes *Nodes, site string) (result *GlobalStats) { result = &GlobalStats{ Firmwares: make(CounterMap), Models: make(CounterMap), } - nodes.Lock() for _, node := range nodes.List { if node.Online { - result.Nodes++ - if stats := node.Statistics; stats != nil { - result.Clients += stats.Clients.Total - result.ClientsWifi24 += stats.Clients.Wifi24 - result.ClientsWifi5 += stats.Clients.Wifi5 - result.ClientsWifi += stats.Clients.Wifi - } - if node.Gateway { - result.Gateways++ - } if info := node.Nodeinfo; info != nil { - result.Models.Increment(info.Hardware.Model) - result.Firmwares.Increment(info.Software.Firmware.Release) + if len(site) == 0 || info.System.SiteCode == site { + result.Nodes++ + if stats := node.Statistics; stats != nil { + result.Clients += stats.Clients.Total + result.ClientsWifi24 += stats.Clients.Wifi24 + result.ClientsWifi5 += stats.Clients.Wifi5 + result.ClientsWifi += stats.Clients.Wifi + } + if node.Gateway { + result.Gateways++ + } + + result.Models.Increment(info.Hardware.Model) + result.Firmwares.Increment(info.Software.Firmware.Release) + } } } } - nodes.Unlock() return } diff --git a/runtime/stats_test.go b/runtime/stats_test.go index fd0bfcb..2be4bf0 100644 --- a/runtime/stats_test.go +++ b/runtime/stats_test.go @@ -9,7 +9,7 @@ import ( ) func TestGlobalStats(t *testing.T) { - stats := NewGlobalStats(createTestNodes()) + stats := NewGlobalStats(createTestNodes(), "ffhb") assert := assert.New(t) assert.EqualValues(1, stats.Gateways) @@ -39,6 +39,9 @@ func createTestNodes() *Nodes { Hardware: data.Hardware{ Model: "TP-Link 841", }, + System: data.System{ + SiteCode: "ffhb", + }, }, } nodeData.NodeInfo.Software.Firmware.Release = "2016.1.6+entenhausen1" @@ -54,6 +57,9 @@ func createTestNodes() *Nodes { Hardware: data.Hardware{ Model: "TP-Link 841", }, + System: data.System{ + SiteCode: "ffhb", + }, }, }) @@ -63,6 +69,9 @@ func createTestNodes() *Nodes { Hardware: data.Hardware{ Model: "Xeon Multi-Core", }, + System: data.System{ + SiteCode: "ffhb", + }, }, })