add global stats to influxdb

This commit is contained in:
Martin Geno 2016-07-22 22:16:01 +02:00
parent e1b84dc21b
commit 959521b209
2 changed files with 43 additions and 1 deletions

View File

@ -171,6 +171,36 @@ func (nodes *Nodes) worker() {
}
}
func (nodes *Nodes) GetStats() map[string]interface{} {
var nodesCount uint32
var clientsCount uint32
var clientsWifiCount uint32
var clientsWifi24Count uint32
var clientsWifi5Count uint32
nodes.Lock()
for _, node := range nodes.List {
if node.Flags.Online {
nodesCount += 1
if stats := node.Statistics; stats != nil {
clientsCount += stats.Clients.Total
clientsWifi24Count += stats.Clients.Wifi24
clientsWifi5Count += stats.Clients.Wifi5
clientsWifiCount += stats.Clients.Wifi
}
}
}
nodes.Unlock()
return map[string]interface{}{
"nodes": nodesCount,
"clients.total": clientsCount,
"clients.wifi": clientsWifiCount,
"clients.wifi24": clientsWifi24Count,
"clients.wifi5": clientsWifi5Count,
}
}
func (nodes *Nodes) load() {
path := nodes.config.Nodes.NodesPath
log.Println("loading", path)

View File

@ -18,6 +18,7 @@ const (
type StatsDb struct {
points chan *client.Point
wg sync.WaitGroup
nodes *models.Nodes
client client.Client
}
@ -36,6 +37,7 @@ func NewStatsDb() *StatsDb {
db := &StatsDb{
client: c,
points: make(chan *client.Point, 500),
nodes: nodes,
}
// start worker
@ -143,11 +145,21 @@ func (c *StatsDb) worker() {
var err error
var writeNow, closed bool
timer := time.NewTimer(batchDuration)
globalDuration := time.Second * time.Duration(config.Nodes.SaveInterval)
globalTimer := time.NewTimer(globalDuration)
for !closed {
// wait for new points
select {
case <-globalTimer.C:
point, err := client.NewPoint("global", nil, nodes.GetStats(), time.Now())
if err != nil {
panic(err)
}
c.points <- point
globalTimer.Reset(globalDuration)
log.Print("saving global point")
case point, ok := <-c.points:
if ok {
if bp == nil {
@ -180,7 +192,7 @@ func (c *StatsDb) worker() {
bp = nil
}
}
globalTimer.Stop()
timer.Stop()
c.wg.Done()
}