Outputs enhancement to require Description and SampleConfig functions

Closes #142
This commit is contained in:
Cameron Sparr 2015-08-25 17:59:12 -06:00
parent 846fd31121
commit a00510a73c
5 changed files with 97 additions and 47 deletions

View File

@ -5,6 +5,7 @@
- [#136](https://github.com/influxdb/telegraf/issues/136): Add a -usage flag for printing usage of a single plugin. - [#136](https://github.com/influxdb/telegraf/issues/136): Add a -usage flag for printing usage of a single plugin.
- [#137](https://github.com/influxdb/telegraf/issues/137): Memcached: fix when a value contains a space - [#137](https://github.com/influxdb/telegraf/issues/137): Memcached: fix when a value contains a space
- [#138](https://github.com/influxdb/telegraf/issues/138): MySQL server address tag. - [#138](https://github.com/influxdb/telegraf/issues/138): MySQL server address tag.
- [#142](https://github.com/influxdb/telegraf/pull/142): Add Description and SampleConfig funcs to output interface
### Bugfixes ### Bugfixes
- [#128](https://github.com/influxdb/telegraf/issues/128): system_load measurement missing. - [#128](https://github.com/influxdb/telegraf/issues/128): system_load measurement missing.

View File

@ -8,6 +8,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/influxdb/telegraf/outputs"
"github.com/influxdb/telegraf/plugins" "github.com/influxdb/telegraf/plugins"
"github.com/naoina/toml" "github.com/naoina/toml"
"github.com/naoina/toml/ast" "github.com/naoina/toml/ast"
@ -326,9 +327,6 @@ type hasDescr interface {
var header = `# Telegraf configuration var header = `# Telegraf configuration
# If this file is missing an [agent] section, you must first generate a
# valid config with 'telegraf -sample-config > telegraf.toml'
# Telegraf is entirely plugin driven. All metrics are gathered from the # Telegraf is entirely plugin driven. All metrics are gathered from the
# declared plugins. # declared plugins.
@ -348,40 +346,28 @@ 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
[outputs.influxdb]
# The full HTTP endpoint URL for your InfluxDB instance
url = "http://localhost:8086" # required.
# The target database for metrics. This database must already exist
database = "telegraf" # required.
# Connection timeout (for the connection with InfluxDB), formatted as a string.
# Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
# If not provided, will default to 0 (no timeout)
# timeout = "5s"
# username = "telegraf"
# password = "metricsmetricsmetricsmetrics"
# Set the user agent for the POSTs (can be useful for log differentiation)
# user_agent = "telegraf"
# Tags can also be specified via a normal map, but only one form at a time: # Tags can also be specified via a normal map, but only one form at a time:
[tags]
# [tags] # dc = "us-east-1"
# dc = "us-east-1" }
# Configuration for telegraf itself # Configuration for telegraf itself
# [agent] [agent]
# interval = "10s" # interval = "10s"
# debug = false # debug = false
# hostname = "prod3241" # hostname = "prod3241"
# PLUGINS ###############################################################################
# OUTPUTS #
###############################################################################
[outputs]
`
var header2 = `
###############################################################################
# PLUGINS #
###############################################################################
` `
@ -389,34 +375,52 @@ database = "telegraf" # required.
func PrintSampleConfig() { func PrintSampleConfig() {
fmt.Printf(header) fmt.Printf(header)
var names []string // Print Outputs
var onames []string
for name := range plugins.Plugins { for oname := range outputs.Outputs {
names = append(names, name) onames = append(onames, oname)
} }
sort.Strings(onames)
sort.Strings(names) for _, oname := range onames {
creator := outputs.Outputs[oname]
output := creator()
for _, name := range names { fmt.Printf("\n# %s\n[outputs.%s]\n", output.Description(), oname)
creator := plugins.Plugins[name]
plugin := creator()
fmt.Printf("# %s\n[%s]\n", plugin.Description(), name)
var config string
config = strings.TrimSpace(plugin.SampleConfig())
config := output.SampleConfig()
if config == "" {
fmt.Printf(" # no configuration\n\n")
} else {
fmt.Printf(config)
}
}
fmt.Printf(header2)
// Print Plugins
var pnames []string
for pname := range plugins.Plugins {
pnames = append(pnames, pname)
}
sort.Strings(pnames)
for _, pname := range pnames {
creator := plugins.Plugins[pname]
plugin := creator()
fmt.Printf("# %s\n[%s]\n", plugin.Description(), pname)
config := plugin.SampleConfig()
if config == "" { if config == "" {
fmt.Printf(" # no configuration\n\n") fmt.Printf(" # no configuration\n\n")
} else { } else {
fmt.Printf("\n")
lines := strings.Split(config, "\n") lines := strings.Split(config, "\n")
for _, line := range lines { for _, line := range lines {
fmt.Printf("%s\n", line) fmt.Printf("%s\n", line)
} }
fmt.Printf("\n") fmt.Printf("\n")
} }
} }

View File

@ -21,6 +21,14 @@ type Datadog struct {
client *http.Client client *http.Client
} }
var sampleConfig = `
# Datadog API key
apikey = "my-secret-key" # required.
# Connection timeout.
# timeout = "5s"
`
type TimeSeries struct { type TimeSeries struct {
Series []*Metric `json:"series"` Series []*Metric `json:"series"`
} }
@ -91,6 +99,14 @@ func (d *Datadog) Write(bp client.BatchPoints) error {
return nil return nil
} }
func (d *Datadog) SampleConfig() string {
return sampleConfig
}
func (d *Datadog) Description() string {
return "Configuration for DataDog API to send metrics to."
}
func (d *Datadog) authenticatedUrl() string { func (d *Datadog) authenticatedUrl() string {
q := url.Values{ q := url.Values{
"api_key": []string{d.Apikey}, "api_key": []string{d.Apikey},

View File

@ -22,6 +22,25 @@ type InfluxDB struct {
conn *client.Client conn *client.Client
} }
var sampleConfig = `
# The full HTTP endpoint URL for your InfluxDB instance
url = "http://localhost:8086" # required.
# The target database for metrics. This database must already exist
database = "telegraf" # required.
# Connection timeout (for the connection with InfluxDB), formatted as a string.
# Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
# If not provided, will default to 0 (no timeout)
# timeout = "5s"
# username = "telegraf"
# password = "metricsmetricsmetricsmetrics"
# Set the user agent for the POSTs (can be useful for log differentiation)
# user_agent = "telegraf"
`
func (i *InfluxDB) Connect() error { func (i *InfluxDB) Connect() error {
u, err := url.Parse(i.URL) u, err := url.Parse(i.URL)
if err != nil { if err != nil {
@ -57,6 +76,14 @@ func (i *InfluxDB) Close() error {
return nil return nil
} }
func (i *InfluxDB) SampleConfig() string {
return sampleConfig
}
func (i *InfluxDB) Description() string {
return "Configuration for influxdb server to send metrics to"
}
func (i *InfluxDB) Write(bp client.BatchPoints) error { func (i *InfluxDB) Write(bp client.BatchPoints) error {
bp.Database = i.Database bp.Database = i.Database
if _, err := i.conn.Write(bp); err != nil { if _, err := i.conn.Write(bp); err != nil {

View File

@ -7,6 +7,8 @@ import (
type Output interface { type Output interface {
Connect() error Connect() error
Close() error Close() error
Description() string
SampleConfig() string
Write(client.BatchPoints) error Write(client.BatchPoints) error
} }