move tags to influxdb struct, update all sample configs

This commit is contained in:
JP 2015-08-07 15:31:25 -05:00
parent 48c10f9454
commit 91f6c4b740
9 changed files with 19 additions and 50 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
pkg/ pkg/
tivan tivan
.vagrant .vagrant
telegraf

View File

@ -57,18 +57,12 @@ func NewAgent(config *Config) (*Agent, error) {
agent.Hostname = hostname agent.Hostname = hostname
} }
if config.Tags == nil {
config.Tags = map[string]string{}
}
config.Tags["host"] = agent.Hostname
return agent, nil return agent, nil
} }
func (a *Agent) Connect() error { func (a *Agent) Connect() error {
for _, o := range a.outputs { for _, o := range a.outputs {
err := o.output.Connect() err := o.output.Connect(a.Hostname)
if err != nil { if err != nil {
return err return err
} }
@ -157,7 +151,6 @@ func (a *Agent) crankParallel() error {
close(points) close(points)
var bp BatchPoints var bp BatchPoints
bp.Tags = a.Config.Tags
bp.Time = time.Now() bp.Time = time.Now()
for sub := range points { for sub := range points {
@ -181,7 +174,6 @@ func (a *Agent) crank() error {
} }
} }
acc.Tags = a.Config.Tags
acc.Time = time.Now() acc.Time = time.Now()
return a.flush(&acc) return a.flush(&acc)
@ -202,7 +194,6 @@ func (a *Agent) crankSeparate(shutdown chan struct{}, plugin *runningPlugin) err
return err return err
} }
acc.Tags = a.Config.Tags
acc.Time = time.Now() acc.Time = time.Now()
err = a.flush(&acc) err = a.flush(&acc)

View File

@ -102,7 +102,7 @@ func main() {
close(shutdown) close(shutdown)
}() }()
log.Print("InfluxDB Agent running") log.Print("Telegraf Agent running")
log.Printf("Loaded outputs: %s", strings.Join(outputs, " ")) log.Printf("Loaded outputs: %s", strings.Join(outputs, " "))
log.Printf("Loaded plugins: %s", strings.Join(plugins, " ")) log.Printf("Loaded plugins: %s", strings.Join(plugins, " "))
if ag.Debug { if ag.Debug {
@ -111,10 +111,6 @@ func main() {
ag.Interval, ag.Debug, ag.Hostname) ag.Interval, ag.Debug, ag.Hostname)
} }
if len(outputs) > 0 {
log.Printf("Tags enabled: %v", config.ListTags())
}
if *fPidfile != "" { if *fPidfile != "" {
f, err := os.Create(*fPidfile) f, err := os.Create(*fPidfile)
if err != nil { if err != nil {

View File

@ -34,8 +34,6 @@ func (d *Duration) UnmarshalTOML(b []byte) error {
// will be logging to, as well as all the plugins that the user has // will be logging to, as well as all the plugins that the user has
// specified // specified
type Config struct { type Config struct {
Tags map[string]string
agent *ast.Table agent *ast.Table
plugins map[string]*ast.Table plugins map[string]*ast.Table
outputs map[string]*ast.Table outputs map[string]*ast.Table
@ -200,7 +198,6 @@ func LoadConfig(path string) (*Config, error) {
} }
c := &Config{ c := &Config{
Tags: make(map[string]string),
plugins: make(map[string]*ast.Table), plugins: make(map[string]*ast.Table),
outputs: make(map[string]*ast.Table), outputs: make(map[string]*ast.Table),
} }
@ -214,10 +211,6 @@ func LoadConfig(path string) (*Config, error) {
switch name { switch name {
case "agent": case "agent":
c.agent = subtbl c.agent = subtbl
case "tags":
if err := toml.UnmarshalTable(subtbl, c.Tags); err != nil {
return nil, errInvalidConfig
}
case "outputs": case "outputs":
for outputName, outputVal := range subtbl.Fields { for outputName, outputVal := range subtbl.Fields {
outputSubtbl, ok := outputVal.(*ast.Table) outputSubtbl, ok := outputVal.(*ast.Table)
@ -234,20 +227,6 @@ func LoadConfig(path string) (*Config, error) {
return c, nil return c, nil
} }
// ListTags returns a string of tags specified in the config,
// line-protocol style
func (c *Config) ListTags() string {
var tags []string
for k, v := range c.Tags {
tags = append(tags, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(tags)
return strings.Join(tags, " ")
}
type hasConfig interface { type hasConfig interface {
BasicConfig() string BasicConfig() string
} }
@ -280,8 +259,11 @@ var header = `# Telegraf configuration
# NOTE: The configuration has a few required parameters. They are marked # NOTE: The configuration has a few required parameters. They are marked
# with 'required'. Be sure to edit those to make this configuration work. # with 'required'. Be sure to edit those to make this configuration work.
# OUTPUTS
[outputs]
# Configuration for influxdb server to send metrics to # Configuration for influxdb server to send metrics to
[influxdb] [outputs.influxdb]
# The full HTTP endpoint URL for your InfluxDB instance # The full HTTP endpoint URL for your InfluxDB instance
url = "http://localhost:8086" # required. url = "http://localhost:8086" # required.
@ -298,12 +280,8 @@ database = "telegraf" # required.
# Set the user agent for the POSTs (can be useful for log differentiation) # Set the user agent for the POSTs (can be useful for log differentiation)
# user_agent = "telegraf" # user_agent = "telegraf"
# tags = { "dc": "us-east-1" }
# Tags can also be specified via a normal map, but only one form at a time: # tags = { "dc" = "us-east-1" }
# [influxdb.tags]
# dc = "us-east-1"
# Configuration for telegraf itself # Configuration for telegraf itself
# [agent] # [agent]

View File

@ -39,9 +39,7 @@ database = "telegraf" # required.
# Set the user agent for the POSTs (can be useful for log differentiation) # Set the user agent for the POSTs (can be useful for log differentiation)
# user_agent = "telegraf" # user_agent = "telegraf"
# Tags can also be specified via a normal map, but only one form at a time: # tags = { "dc" = "us-east-1" }
# [tags]
# dc = "us-east-1"
# Configuration for telegraf itself # Configuration for telegraf itself
# [agent] # [agent]

View File

@ -13,11 +13,12 @@ type InfluxDB struct {
Password string Password string
Database string Database string
UserAgent string UserAgent string
Tags map[string]string
conn *client.Client conn *client.Client
} }
func (i *InfluxDB) Connect() error { func (i *InfluxDB) Connect(host string) error {
u, err := url.Parse(i.URL) u, err := url.Parse(i.URL)
if err != nil { if err != nil {
return err return err
@ -34,12 +35,18 @@ func (i *InfluxDB) Connect() error {
return err return err
} }
if i.Tags == nil {
i.Tags = make(map[string]string)
}
i.Tags["host"] = host
i.conn = c i.conn = c
return nil return nil
} }
func (i *InfluxDB) Write(bp client.BatchPoints) error { func (i *InfluxDB) Write(bp client.BatchPoints) error {
bp.Database = i.Database bp.Database = i.Database
bp.Tags = i.Tags
if _, err := i.conn.Write(bp); err != nil { if _, err := i.conn.Write(bp); err != nil {
return err return err
} }

View File

@ -5,7 +5,7 @@ import (
) )
type Output interface { type Output interface {
Connect() error Connect(string) error
Write(client.BatchPoints) error Write(client.BatchPoints) error
} }

BIN
telegraf

Binary file not shown.

View File

@ -9,9 +9,7 @@ url = "http://localhost:8086"
username = "root" username = "root"
password = "root" password = "root"
database = "telegraf" database = "telegraf"
tags = { "dc" = "us-phx-1" }
[tags]
dc = "us-phx-1"
[redis] [redis]
address = ":6379" address = ":6379"