[influxdb] site_code tag to global, firmware and model

This commit is contained in:
Martin Geno 2017-02-19 00:18:54 +01:00
parent 5d6f046e39
commit 1d7d56131d
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
4 changed files with 44 additions and 21 deletions

View File

@ -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",
},
},
})

View File

@ -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())
}
}

View File

@ -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
}

View File

@ -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",
},
},
})