[TASK] add autoupdater to influxdb database (#93)

This commit is contained in:
Geno 2017-11-14 19:33:50 +01:00 committed by GitHub
parent 037ff80193
commit 0d8fddbcb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 81 additions and 14 deletions

View File

@ -12,13 +12,14 @@ import (
) )
const ( const (
MeasurementLink = "link" // Measurement for per-link statistics MeasurementLink = "link" // Measurement for per-link statistics
MeasurementNode = "node" // Measurement for per-node statistics MeasurementNode = "node" // Measurement for per-node statistics
MeasurementGlobal = "global" // Measurement for summarized global statistics MeasurementGlobal = "global" // Measurement for summarized global statistics
CounterMeasurementFirmware = "firmware" // Measurement for firmware statistics CounterMeasurementFirmware = "firmware" // Measurement for firmware statistics
CounterMeasurementModel = "model" // Measurement for model statistics CounterMeasurementModel = "model" // Measurement for model statistics
batchMaxSize = 500 CounterMeasurementAutoupdater = "autoupdater" // Measurement for autoupdater
batchTimeout = 5 * time.Second batchMaxSize = 500
batchTimeout = 5 * time.Second
) )
type Connection struct { type Connection struct {

View File

@ -12,6 +12,7 @@ func (conn *Connection) InsertGlobals(stats *runtime.GlobalStats, time time.Time
conn.addPoint(MeasurementGlobal, nil, GlobalStatsFields(stats), time) conn.addPoint(MeasurementGlobal, nil, GlobalStatsFields(stats), time)
conn.addCounterMap(CounterMeasurementModel, stats.Models, time) conn.addCounterMap(CounterMeasurementModel, stats.Models, time)
conn.addCounterMap(CounterMeasurementFirmware, stats.Firmwares, time) conn.addCounterMap(CounterMeasurementFirmware, stats.Firmwares, time)
conn.addCounterMap(CounterMeasurementAutoupdater, stats.Autoupdater, time)
} }
// GlobalStatsFields returns fields for InfluxDB // GlobalStatsFields returns fields for InfluxDB

View File

@ -37,6 +37,8 @@ func createTestNodes() *runtime.Nodes {
}, },
} }
nodeData.Nodeinfo.Software.Firmware.Release = "2016.1.6+entenhausen1" nodeData.Nodeinfo.Software.Firmware.Release = "2016.1.6+entenhausen1"
nodeData.Nodeinfo.Software.Autoupdater.Enabled = true
nodeData.Nodeinfo.Software.Autoupdater.Branch = "stable"
nodes.AddNode(nodeData) nodes.AddNode(nodeData)
nodes.AddNode(&runtime.Node{ nodes.AddNode(&runtime.Node{

View File

@ -60,6 +60,11 @@ func (conn *Connection) InsertNode(node *runtime.Node) {
tags.SetString("model", nodeinfo.Hardware.Model) tags.SetString("model", nodeinfo.Hardware.Model)
tags.SetString("firmware_base", nodeinfo.Software.Firmware.Base) tags.SetString("firmware_base", nodeinfo.Software.Firmware.Base)
tags.SetString("firmware_release", nodeinfo.Software.Firmware.Release) tags.SetString("firmware_release", nodeinfo.Software.Firmware.Release)
if nodeinfo.Software.Autoupdater.Enabled {
tags.SetString("autoupdater", nodeinfo.Software.Autoupdater.Branch)
} else {
tags.SetString("autoupdater", runtime.DISABLED_AUTOUPDATER)
}
} }

View File

@ -58,6 +58,15 @@ func TestToInflux(t *testing.T) {
Network: data.Network{ Network: data.Network{
Mac: "DEADMAC", Mac: "DEADMAC",
}, },
Software: data.Software{
Autoupdater: struct {
Enabled bool `json:"enabled,omitempty"`
Branch string `json:"branch,omitempty"`
}{
Enabled: true,
Branch: "testing",
},
},
}, },
Neighbours: &data.Neighbours{ Neighbours: &data.Neighbours{
NodeID: "deadbeef", NodeID: "deadbeef",
@ -80,15 +89,36 @@ func TestToInflux(t *testing.T) {
Network: data.Network{ Network: data.Network{
Mac: "BAFF1E5", Mac: "BAFF1E5",
}, },
Software: data.Software{
Autoupdater: struct {
Enabled bool `json:"enabled,omitempty"`
Branch string `json:"branch,omitempty"`
}{
Enabled: false,
},
},
},
Statistics: &data.Statistics{
NodeID: "foobar",
},
}
// do not add a empty statistics of a node
droppednode := &runtime.Node{
Nodeinfo: &data.NodeInfo{
NodeID: "notfound",
Network: data.Network{
Mac: "instats",
},
}, },
Statistics: &data.Statistics{}, Statistics: &data.Statistics{},
} }
points := testPoints(node, neigbour) points := testPoints(node, neigbour, droppednode)
var fields map[string]interface{} var fields map[string]interface{}
var tags map[string]string var tags map[string]string
assert.Len(points, 2) assert.Len(points, 3)
// first point contains the neighbour // first point contains the neighbour
sPoint := points[0] sPoint := points[0]
@ -97,6 +127,7 @@ func TestToInflux(t *testing.T) {
assert.EqualValues("deadbeef", tags["nodeid"]) assert.EqualValues("deadbeef", tags["nodeid"])
assert.EqualValues("nobody", tags["owner"]) assert.EqualValues("nobody", tags["owner"])
assert.EqualValues("testing", tags["autoupdater"])
assert.EqualValues(0.5, fields["load"]) assert.EqualValues(0.5, fields["load"])
assert.EqualValues(0, fields["neighbours.lldp"]) assert.EqualValues(0, fields["neighbours.lldp"])
assert.EqualValues(1, fields["neighbours.batadv"]) assert.EqualValues(1, fields["neighbours.batadv"])
@ -113,7 +144,7 @@ func TestToInflux(t *testing.T) {
assert.EqualValues(int64(2331), fields["traffic.mgmt_rx.bytes"]) assert.EqualValues(int64(2331), fields["traffic.mgmt_rx.bytes"])
assert.EqualValues(float64(2327), fields["traffic.mgmt_tx.packets"]) assert.EqualValues(float64(2327), fields["traffic.mgmt_tx.packets"])
// second point contains the neighbour // second point contains the link
nPoint := points[1] nPoint := points[1]
tags = nPoint.Tags() tags = nPoint.Tags()
fields, _ = nPoint.Fields() fields, _ = nPoint.Fields()
@ -125,6 +156,11 @@ func TestToInflux(t *testing.T) {
"target.mac": "BAFF1E5", "target.mac": "BAFF1E5",
}, tags) }, tags)
assert.EqualValues(80, fields["tq"]) assert.EqualValues(80, fields["tq"])
// third point contains the neighbour
nPoint = points[2]
tags = nPoint.Tags()
assert.EqualValues("disabled", tags["autoupdater"])
} }
// Processes data and returns the InfluxDB points // Processes data and returns the InfluxDB points

View File

@ -1,5 +1,7 @@
package runtime package runtime
const DISABLED_AUTOUPDATER = "disabled"
// CounterMap to manage multiple values // CounterMap to manage multiple values
type CounterMap map[string]uint32 type CounterMap map[string]uint32
@ -12,15 +14,17 @@ type GlobalStats struct {
Gateways uint32 Gateways uint32
Nodes uint32 Nodes uint32
Firmwares CounterMap Firmwares CounterMap
Models CounterMap Models CounterMap
Autoupdater CounterMap
} }
//NewGlobalStats returns global statistics for InfluxDB //NewGlobalStats returns global statistics for InfluxDB
func NewGlobalStats(nodes *Nodes) (result *GlobalStats) { func NewGlobalStats(nodes *Nodes) (result *GlobalStats) {
result = &GlobalStats{ result = &GlobalStats{
Firmwares: make(CounterMap), Firmwares: make(CounterMap),
Models: make(CounterMap), Models: make(CounterMap),
Autoupdater: make(CounterMap),
} }
nodes.RLock() nodes.RLock()
@ -39,6 +43,11 @@ func NewGlobalStats(nodes *Nodes) (result *GlobalStats) {
if info := node.Nodeinfo; info != nil { if info := node.Nodeinfo; info != nil {
result.Models.Increment(info.Hardware.Model) result.Models.Increment(info.Hardware.Model)
result.Firmwares.Increment(info.Software.Firmware.Release) result.Firmwares.Increment(info.Software.Firmware.Release)
if info.Software.Autoupdater.Enabled {
result.Autoupdater.Increment(info.Software.Autoupdater.Branch)
} else {
result.Autoupdater.Increment(DISABLED_AUTOUPDATER)
}
} }
} }
} }

View File

@ -24,6 +24,10 @@ func TestGlobalStats(t *testing.T) {
// check firmwares // check firmwares
assert.Len(stats.Firmwares, 1) assert.Len(stats.Firmwares, 1)
assert.EqualValues(1, stats.Firmwares["2016.1.6+entenhausen1"]) assert.EqualValues(1, stats.Firmwares["2016.1.6+entenhausen1"])
// check autoupdater
assert.Len(stats.Autoupdater, 2)
assert.EqualValues(1, stats.Autoupdater["stable"])
} }
func createTestNodes() *Nodes { func createTestNodes() *Nodes {
@ -58,6 +62,15 @@ func createTestNodes() *Nodes {
Hardware: data.Hardware{ Hardware: data.Hardware{
Model: "TP-Link 841", Model: "TP-Link 841",
}, },
Software: data.Software{
Autoupdater: struct {
Enabled bool `json:"enabled,omitempty"`
Branch string `json:"branch,omitempty"`
}{
Enabled: true,
Branch: "stable",
},
},
}, },
}) })