2015-04-01 16:34:32 +00:00
|
|
|
package tivan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2015-04-06 16:32:10 +00:00
|
|
|
"net/url"
|
2015-04-01 16:34:32 +00:00
|
|
|
"sort"
|
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
"github.com/influxdb/influxdb/client"
|
2015-04-01 16:34:32 +00:00
|
|
|
"github.com/influxdb/tivan/plugins"
|
|
|
|
"github.com/vektra/cypress"
|
|
|
|
)
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
type Metrics interface {
|
|
|
|
Receive(*cypress.Message) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Agent struct {
|
|
|
|
Interval Duration
|
|
|
|
Debug bool
|
|
|
|
HTTP string
|
|
|
|
|
|
|
|
Config *Config
|
|
|
|
|
|
|
|
plugins []plugins.Plugin
|
2015-04-06 16:32:10 +00:00
|
|
|
|
|
|
|
conn *client.Client
|
2015-04-01 16:34:32 +00:00
|
|
|
|
|
|
|
eachInternal []func()
|
|
|
|
}
|
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
func NewAgent(config *Config) (*Agent, error) {
|
|
|
|
agent := &Agent{Config: config}
|
2015-04-01 16:34:32 +00:00
|
|
|
|
|
|
|
err := config.Apply("agent", agent)
|
|
|
|
if err != nil {
|
2015-04-06 16:32:10 +00:00
|
|
|
return nil, err
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
u, err := url.Parse(config.URL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
c, err := client.NewClient(client.Config{
|
|
|
|
URL: *u,
|
|
|
|
Username: config.Username,
|
|
|
|
Password: config.Password,
|
|
|
|
UserAgent: config.UserAgent,
|
|
|
|
})
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
agent.conn = c
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
return agent, nil
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) LoadPlugins() ([]string, error) {
|
|
|
|
var names []string
|
|
|
|
|
|
|
|
for name, creator := range plugins.Plugins {
|
|
|
|
a.plugins = append(a.plugins, creator())
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(names)
|
|
|
|
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) crank() error {
|
2015-04-06 16:32:10 +00:00
|
|
|
var acc BatchPoints
|
|
|
|
|
2015-04-01 16:34:32 +00:00
|
|
|
for _, plugin := range a.plugins {
|
2015-04-06 16:32:10 +00:00
|
|
|
err := plugin.Gather(&acc)
|
2015-04-01 16:34:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
acc.Tags = a.Config.Tags
|
|
|
|
acc.Timestamp = time.Now()
|
|
|
|
acc.Database = a.Config.Database
|
|
|
|
|
|
|
|
_, err := a.conn.Write(acc.BatchPoints)
|
|
|
|
return err
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) Run(shutdown chan struct{}) {
|
|
|
|
ticker := time.NewTicker(a.Interval.Duration)
|
|
|
|
|
|
|
|
for {
|
|
|
|
err := a.crank()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error in plugins: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range a.eachInternal {
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-shutdown:
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|