2015-05-29 19:50:05 +00:00
|
|
|
package graphite
|
|
|
|
|
2015-06-08 20:29:08 +00:00
|
|
|
import (
|
2015-06-23 21:23:55 +00:00
|
|
|
"fmt"
|
2015-06-23 19:35:39 +00:00
|
|
|
"strings"
|
|
|
|
|
2015-06-08 20:29:08 +00:00
|
|
|
"github.com/influxdb/influxdb/toml"
|
2015-06-23 19:35:39 +00:00
|
|
|
"github.com/influxdb/influxdb/tsdb"
|
2015-06-08 20:29:08 +00:00
|
|
|
)
|
2015-05-29 19:50:05 +00:00
|
|
|
|
|
|
|
const (
|
2015-06-08 20:12:42 +00:00
|
|
|
// DefaultBindAddress is the default binding interface if none is specified.
|
|
|
|
DefaultBindAddress = ":2003"
|
|
|
|
|
2015-05-29 19:50:05 +00:00
|
|
|
// DefaultDatabase is the default database if none is specified.
|
|
|
|
DefaultDatabase = "graphite"
|
|
|
|
|
2015-06-08 20:12:42 +00:00
|
|
|
// DefaultProtocol is the default IP protocol used by the Graphite input.
|
|
|
|
DefaultProtocol = "tcp"
|
2015-06-08 20:49:08 +00:00
|
|
|
|
|
|
|
// DefaultConsistencyLevel is the default write consistency for the Graphite input.
|
|
|
|
DefaultConsistencyLevel = "one"
|
2015-05-29 19:50:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config represents the configuration for Graphite endpoints.
|
|
|
|
type Config struct {
|
2015-06-08 20:49:08 +00:00
|
|
|
BindAddress string `toml:"bind-address"`
|
|
|
|
Database string `toml:"database"`
|
|
|
|
Enabled bool `toml:"enabled"`
|
|
|
|
Protocol string `toml:"protocol"`
|
|
|
|
BatchSize int `toml:"batch-size"`
|
|
|
|
BatchTimeout toml.Duration `toml:"batch-timeout"`
|
|
|
|
ConsistencyLevel string `toml:"consistency-level"`
|
2015-06-23 05:28:52 +00:00
|
|
|
Templates []string `toml:"templates"`
|
2015-06-23 19:35:39 +00:00
|
|
|
Tags []string `toml:"tags"`
|
2015-05-29 19:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfig returns a new Config with defaults.
|
|
|
|
func NewConfig() Config {
|
|
|
|
return Config{
|
2015-06-08 20:49:08 +00:00
|
|
|
BindAddress: DefaultBindAddress,
|
|
|
|
Database: DefaultDatabase,
|
|
|
|
Protocol: DefaultProtocol,
|
|
|
|
ConsistencyLevel: DefaultConsistencyLevel,
|
2015-05-29 19:50:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 23:40:08 +00:00
|
|
|
// WithDefaults takes the given config and returns a new config with any required
|
|
|
|
// default values set.
|
|
|
|
func (c *Config) WithDefaults() *Config {
|
|
|
|
d := *c
|
|
|
|
if d.BindAddress == "" {
|
|
|
|
d.BindAddress = DefaultBindAddress
|
|
|
|
}
|
|
|
|
if d.Database == "" {
|
|
|
|
d.Database = DefaultDatabase
|
|
|
|
}
|
|
|
|
if d.Protocol == "" {
|
|
|
|
d.Protocol = DefaultProtocol
|
|
|
|
}
|
|
|
|
if d.ConsistencyLevel == "" {
|
|
|
|
d.ConsistencyLevel = DefaultConsistencyLevel
|
|
|
|
}
|
|
|
|
return &d
|
|
|
|
}
|
2015-06-23 19:35:39 +00:00
|
|
|
|
|
|
|
func (c *Config) DefaultTags() tsdb.Tags {
|
|
|
|
tags := tsdb.Tags{}
|
|
|
|
for _, t := range c.Tags {
|
|
|
|
parts := strings.Split(t, "=")
|
|
|
|
tags[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
return tags
|
|
|
|
}
|
2015-06-23 21:23:55 +00:00
|
|
|
|
|
|
|
func (c *Config) Validate() error {
|
|
|
|
if err := c.validateTemplates(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.validateTags(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) validateTemplates() error {
|
|
|
|
for i, t := range c.Templates {
|
|
|
|
parts := strings.Fields(t)
|
|
|
|
// Ensure template string is non-empty
|
|
|
|
if len(parts) == 0 {
|
|
|
|
return fmt.Errorf("missing template at position: %d", i)
|
|
|
|
}
|
|
|
|
if len(parts) == 1 && parts[0] == "" {
|
|
|
|
return fmt.Errorf("missing template at position: %d", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parts) > 3 {
|
|
|
|
return fmt.Errorf("invalid template format: '%s'", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
template := t
|
|
|
|
filter := ""
|
|
|
|
tags := ""
|
|
|
|
if len(parts) >= 2 {
|
|
|
|
filter = parts[0]
|
|
|
|
template = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parts) == 3 {
|
|
|
|
tags = parts[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the template has one and only one measurement
|
|
|
|
if err := c.validateTemplate(template); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Validate filter expression is valid
|
|
|
|
if err := c.validateFilter(filter); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tags != "" {
|
|
|
|
// Validate tags
|
|
|
|
for _, tagStr := range strings.Split(tags, ",") {
|
|
|
|
if err := c.validateTag(tagStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) validateTags() error {
|
|
|
|
for _, t := range c.Tags {
|
|
|
|
if err := c.validateTag(t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) validateTemplate(template string) error {
|
|
|
|
hasMeasurement := false
|
|
|
|
for _, p := range strings.Split(template, ".") {
|
|
|
|
if p == "measurement" || p == "measurement*" {
|
|
|
|
if hasMeasurement {
|
|
|
|
return fmt.Errorf("multiple measurements in template `%s`", template)
|
|
|
|
}
|
|
|
|
hasMeasurement = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasMeasurement {
|
|
|
|
return fmt.Errorf("no measurement in template `%s`", template)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) validateFilter(filter string) error {
|
|
|
|
for _, p := range strings.Split(filter, ".") {
|
|
|
|
if p == "" {
|
|
|
|
return fmt.Errorf("filter contains blank section: %s", filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(p, "*") && p != "*" {
|
|
|
|
return fmt.Errorf("invalid filter wildcard section: %s", filter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) validateTag(keyValue string) error {
|
|
|
|
parts := strings.Split(keyValue, "=")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return fmt.Errorf("invalid template tags: '%s'", keyValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
if parts[0] == "" || parts[1] == "" {
|
|
|
|
return fmt.Errorf("invalid template tags: %s'", keyValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|