yanic/stats_db.go

128 lines
2.5 KiB
Go
Raw Normal View History

2016-03-12 03:36:02 +01:00
package main
import (
"log"
"sync"
"time"
"github.com/ffdo/node-informant/gluon-collector/data"
"github.com/influxdata/influxdb/client/v2"
)
const (
saveInterval = time.Second * 5
)
type StatsDb struct {
points chan *client.Point
wg sync.WaitGroup
client client.Client
}
func NewStatsDb() *StatsDb {
// Make client
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://localhost:8086",
Username: config.Influxdb.Username,
Password: config.Influxdb.Password,
})
if err != nil {
panic(err)
}
db := &StatsDb{
client: c,
points: make(chan *client.Point, 500),
}
// start worker
db.wg.Add(1)
go db.worker()
return db
}
func (c *StatsDb) Add(stats *data.StatisticsStruct) {
tags := map[string]string{
"nodeid": stats.NodeId,
}
fields := map[string]interface{}{
"load": stats.LoadAverage,
2016-03-12 03:58:36 +01:00
"processes.open": stats.Processes.Running,
2016-03-12 03:36:02 +01:00
"clients.wifi": stats.Clients.Wifi,
"clients.total": stats.Clients.Total,
"traffic.forward": stats.Traffic.Forward,
"traffic.rx": stats.Traffic.Rx,
"traffic.tx": stats.Traffic.Tx,
"traffic.mgmt.rx": stats.Traffic.MgmtRx,
"traffic.mgmt.tx": stats.Traffic.MgmtTx,
"traffic.memory.buffers": stats.Memory.Buffers,
"traffic.memory.cached": stats.Memory.Cached,
"traffic.memory.free": stats.Memory.Free,
"traffic.memory.total": stats.Memory.Total,
}
point, err := client.NewPoint("node", tags, fields, time.Now())
if err != nil {
panic(err)
}
c.points <- point
}
func (c *StatsDb) Close() {
close(c.points)
c.wg.Wait()
c.client.Close()
}
2016-03-12 03:58:36 +01:00
// stores data points in batches into the influxdb
2016-03-12 03:36:02 +01:00
func (c *StatsDb) worker() {
lastSent := time.Now()
2016-03-12 03:58:36 +01:00
open := true // channel open?
2016-03-12 03:36:02 +01:00
bpConfig := client.BatchPointsConfig{
Database: config.Influxdb.Database,
Precision: "m",
}
var bp client.BatchPoints
var err error
var dirty bool
2016-03-12 03:58:36 +01:00
for open {
2016-03-12 03:36:02 +01:00
// create new batch points?
if bp == nil {
if bp, err = client.NewBatchPoints(bpConfig); err != nil {
panic(err)
}
}
// wait for new points
select {
case point, ok := <-c.points:
if ok {
bp.AddPoint(point)
dirty = true
} else {
2016-03-12 03:58:36 +01:00
open = false
2016-03-12 03:36:02 +01:00
}
case <-time.After(time.Second):
// nothing
}
2016-03-12 03:58:36 +01:00
// write batch now?
if dirty && (!open || lastSent.Add(saveInterval).Before(time.Now())) {
2016-03-12 03:36:02 +01:00
log.Println("saving", len(bp.Points()), "points")
2016-03-12 03:58:36 +01:00
if err = c.client.Write(bp); err != nil {
2016-03-12 03:36:02 +01:00
panic(err)
}
lastSent = time.Now()
dirty = false
bp = nil
}
}
c.wg.Done()
}