2015-04-01 16:34:32 +00:00
|
|
|
package tivan
|
|
|
|
|
|
|
|
import (
|
2015-05-18 21:10:12 +00:00
|
|
|
"fmt"
|
2015-04-01 16:34:32 +00:00
|
|
|
"log"
|
2015-04-06 16:32:10 +00:00
|
|
|
"net/url"
|
2015-04-07 16:56:40 +00:00
|
|
|
"os"
|
2015-04-01 16:34:32 +00:00
|
|
|
"sort"
|
2015-04-07 16:23:35 +00:00
|
|
|
"time"
|
2015-04-01 16:34:32 +00:00
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2015-05-18 19:15:15 +00:00
|
|
|
type runningPlugin struct {
|
|
|
|
name string
|
|
|
|
plugin plugins.Plugin
|
|
|
|
}
|
|
|
|
|
2015-04-01 16:34:32 +00:00
|
|
|
type Agent struct {
|
|
|
|
Interval Duration
|
|
|
|
Debug bool
|
|
|
|
HTTP string
|
2015-04-07 16:56:40 +00:00
|
|
|
Hostname string
|
2015-04-01 16:34:32 +00:00
|
|
|
|
|
|
|
Config *Config
|
|
|
|
|
2015-05-18 19:15:15 +00:00
|
|
|
plugins []*runningPlugin
|
2015-04-06 16:32:10 +00:00
|
|
|
|
|
|
|
conn *client.Client
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
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-07 16:56:40 +00:00
|
|
|
if agent.Hostname == "" {
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
agent.Hostname = hostname
|
|
|
|
|
|
|
|
if config.Tags == nil {
|
|
|
|
config.Tags = map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Tags["host"] = agent.Hostname
|
|
|
|
}
|
|
|
|
|
2015-04-07 00:24:24 +00:00
|
|
|
return agent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (agent *Agent) Connect() error {
|
|
|
|
config := agent.Config
|
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
u, err := url.Parse(config.URL)
|
|
|
|
if err != nil {
|
2015-04-07 00:24:24 +00:00
|
|
|
return 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 {
|
2015-04-07 00:24:24 +00:00
|
|
|
return err
|
2015-04-06 16:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
agent.conn = c
|
2015-04-01 16:34:32 +00:00
|
|
|
|
2015-04-07 00:24:24 +00:00
|
|
|
return nil
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) LoadPlugins() ([]string, error) {
|
|
|
|
var names []string
|
|
|
|
|
2015-05-18 21:10:12 +00:00
|
|
|
for _, name := range a.Config.PluginsDeclared() {
|
|
|
|
creator, ok := plugins.Plugins[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Undefined but requested plugin: %s", name)
|
|
|
|
}
|
2015-05-18 18:54:59 +00:00
|
|
|
|
2015-05-18 21:10:12 +00:00
|
|
|
plugin := creator()
|
2015-04-07 18:53:30 +00:00
|
|
|
|
|
|
|
err := a.Config.Apply(name, plugin)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-18 19:15:15 +00:00
|
|
|
a.plugins = append(a.plugins, &runningPlugin{name, plugin})
|
2015-04-01 16:34:32 +00:00
|
|
|
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-07 00:24:24 +00:00
|
|
|
acc.Debug = a.Debug
|
|
|
|
|
2015-04-01 16:34:32 +00:00
|
|
|
for _, plugin := range a.plugins {
|
2015-05-18 19:15:15 +00:00
|
|
|
acc.Prefix = plugin.name + "_"
|
|
|
|
err := plugin.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
|
2015-05-18 18:54:41 +00:00
|
|
|
acc.Time = time.Now()
|
2015-04-06 16:32:10 +00:00
|
|
|
acc.Database = a.Config.Database
|
|
|
|
|
|
|
|
_, err := a.conn.Write(acc.BatchPoints)
|
|
|
|
return err
|
2015-04-01 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 21:10:12 +00:00
|
|
|
func (a *Agent) TestAllPlugins() error {
|
|
|
|
var names []string
|
|
|
|
|
|
|
|
for name, _ := range plugins.Plugins {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(names)
|
|
|
|
|
|
|
|
var acc BatchPoints
|
|
|
|
acc.Debug = true
|
|
|
|
|
|
|
|
fmt.Printf("* Testing all plugins with default configuration\n")
|
|
|
|
|
|
|
|
for _, name := range names {
|
|
|
|
plugin := plugins.Plugins[name]()
|
|
|
|
|
|
|
|
fmt.Printf("* Plugin: %s\n", name)
|
|
|
|
|
|
|
|
acc.Prefix = name + "_"
|
|
|
|
err := plugin.Gather(&acc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-07 00:24:24 +00:00
|
|
|
func (a *Agent) Test() error {
|
|
|
|
var acc BatchPoints
|
|
|
|
|
|
|
|
acc.Debug = true
|
|
|
|
|
|
|
|
for _, plugin := range a.plugins {
|
2015-05-18 19:15:15 +00:00
|
|
|
acc.Prefix = plugin.name + "_"
|
|
|
|
err := plugin.plugin.Gather(&acc)
|
2015-04-07 00:24:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-07 16:23:58 +00:00
|
|
|
func (a *Agent) Run(shutdown chan struct{}) error {
|
|
|
|
if a.conn == nil {
|
|
|
|
err := a.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 16:34:32 +00:00
|
|
|
ticker := time.NewTicker(a.Interval.Duration)
|
|
|
|
|
|
|
|
for {
|
|
|
|
err := a.crank()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error in plugins: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-shutdown:
|
2015-04-07 16:23:58 +00:00
|
|
|
return nil
|
2015-04-01 16:34:32 +00:00
|
|
|
case <-ticker.C:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|