2017-06-01 20:52:14 +00:00
|
|
|
package graphite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2017-09-27 11:55:02 +00:00
|
|
|
|
2019-01-17 12:26:16 +00:00
|
|
|
"github.com/bdlm/log"
|
2017-09-27 11:55:02 +00:00
|
|
|
"github.com/fgrosse/graphigo"
|
2019-01-17 12:26:16 +00:00
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/database"
|
2017-06-01 20:52:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-11-30 11:45:28 +00:00
|
|
|
MeasurementNode = "node" // Measurement for per-node statistics
|
|
|
|
MeasurementGlobal = "global" // Measurement for summarized global statistics
|
|
|
|
CounterMeasurementFirmware = "firmware" // Measurement for firmware statistics
|
|
|
|
CounterMeasurementModel = "model" // Measurement for model statistics
|
|
|
|
CounterMeasurementAutoupdater = "autoupdater" // Measurement for autoupdater
|
2017-06-01 20:52:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Connection struct {
|
|
|
|
database.Connection
|
|
|
|
client graphigo.Client
|
|
|
|
points chan []graphigo.Metric
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config map[string]interface{}
|
|
|
|
|
|
|
|
func (c Config) Address() string {
|
|
|
|
return c["address"].(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) Prefix() string {
|
|
|
|
return c["prefix"].(string)
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:26:17 +00:00
|
|
|
func Connect(configuration map[string]interface{}) (database.Connection, error) {
|
2017-06-01 20:52:14 +00:00
|
|
|
var config Config
|
|
|
|
|
2017-12-31 04:26:17 +00:00
|
|
|
config = configuration
|
2017-06-01 20:52:14 +00:00
|
|
|
|
|
|
|
con := &Connection{
|
|
|
|
client: graphigo.Client{
|
|
|
|
Address: config.Address(),
|
|
|
|
Prefix: config.Prefix(),
|
|
|
|
},
|
|
|
|
points: make(chan []graphigo.Metric, 1000),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := con.client.Connect(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
con.wg.Add(1)
|
|
|
|
go con.addWorker()
|
|
|
|
|
|
|
|
return con, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) Close() {
|
|
|
|
close(c.points)
|
|
|
|
if c.client.Connection != nil {
|
|
|
|
c.client.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) addWorker() {
|
|
|
|
defer c.wg.Done()
|
|
|
|
defer c.Close()
|
|
|
|
for point := range c.points {
|
|
|
|
err := c.client.SendAll(point)
|
|
|
|
if err != nil {
|
2019-01-17 12:26:16 +00:00
|
|
|
log.WithField("database", "graphite").Fatal(err)
|
2017-06-01 20:52:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) addPoint(point []graphigo.Metric) {
|
|
|
|
c.points <- point
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
database.RegisterAdapter("graphite", Connect)
|
|
|
|
}
|