2017-09-29 23:13:08 +00:00
|
|
|
package wavefront
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
2018-12-21 19:26:07 +00:00
|
|
|
wavefront "github.com/wavefronthq/wavefront-sdk-go/senders"
|
2017-09-29 23:13:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Wavefront struct {
|
2018-12-21 19:26:07 +00:00
|
|
|
Url string
|
|
|
|
Token string
|
2017-09-29 23:13:08 +00:00
|
|
|
Host string
|
|
|
|
Port int
|
2018-12-21 19:26:07 +00:00
|
|
|
Prefix string
|
2017-09-29 23:13:08 +00:00
|
|
|
SimpleFields bool
|
|
|
|
MetricSeparator string
|
|
|
|
ConvertPaths bool
|
|
|
|
ConvertBool bool
|
|
|
|
UseRegex bool
|
|
|
|
SourceOverride []string
|
|
|
|
StringToNumber map[string][]map[string]float64
|
2018-12-21 19:26:07 +00:00
|
|
|
|
|
|
|
sender wavefront.Sender
|
2017-09-29 23:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// catch many of the invalid chars that could appear in a metric or tag name
|
|
|
|
var sanitizedChars = strings.NewReplacer(
|
|
|
|
"!", "-", "@", "-", "#", "-", "$", "-", "%", "-", "^", "-", "&", "-",
|
|
|
|
"*", "-", "(", "-", ")", "-", "+", "-", "`", "-", "'", "-", "\"", "-",
|
|
|
|
"[", "-", "]", "-", "{", "-", "}", "-", ":", "-", ";", "-", "<", "-",
|
|
|
|
">", "-", ",", "-", "?", "-", "/", "-", "\\", "-", "|", "-", " ", "-",
|
|
|
|
"=", "-",
|
|
|
|
)
|
|
|
|
|
|
|
|
// instead of Replacer which may miss some special characters we can use a regex pattern, but this is significantly slower than Replacer
|
|
|
|
var sanitizedRegex = regexp.MustCompile("[^a-zA-Z\\d_.-]")
|
|
|
|
|
2018-12-21 19:26:07 +00:00
|
|
|
var tagValueReplacer = strings.NewReplacer("*", "-")
|
2017-09-29 23:13:08 +00:00
|
|
|
|
|
|
|
var pathReplacer = strings.NewReplacer("_", "_")
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2018-12-21 19:26:07 +00:00
|
|
|
## Url for Wavefront Direct Ingestion or using HTTP with Wavefront Proxy
|
|
|
|
## If using Wavefront Proxy, also specify port. example: http://proxyserver:2878
|
|
|
|
url = "https://metrics.wavefront.com"
|
|
|
|
|
|
|
|
## Authentication Token for Wavefront. Only required if using Direct Ingestion
|
|
|
|
#token = "DUMMY_TOKEN"
|
|
|
|
|
|
|
|
## DNS name of the wavefront proxy server. Do not use if url is specified
|
|
|
|
#host = "wavefront.example.com"
|
2017-09-29 23:13:08 +00:00
|
|
|
|
2018-12-21 19:26:07 +00:00
|
|
|
## Port that the Wavefront proxy server listens on. Do not use if url is specified
|
|
|
|
#port = 2878
|
2017-09-29 23:13:08 +00:00
|
|
|
|
|
|
|
## prefix for metrics keys
|
|
|
|
#prefix = "my.specific.prefix."
|
|
|
|
|
2018-12-21 19:26:07 +00:00
|
|
|
## whether to use "value" for name of simple fields. default is false
|
2017-09-29 23:13:08 +00:00
|
|
|
#simple_fields = false
|
|
|
|
|
2018-12-21 19:26:07 +00:00
|
|
|
## character to use between metric and field name. default is . (dot)
|
2017-09-29 23:13:08 +00:00
|
|
|
#metric_separator = "."
|
|
|
|
|
2018-12-21 19:26:07 +00:00
|
|
|
## Convert metric name paths to use metricSeparator character
|
|
|
|
## When true will convert all _ (underscore) characters in final metric name. default is true
|
2017-09-29 23:13:08 +00:00
|
|
|
#convert_paths = true
|
|
|
|
|
|
|
|
## Use Regex to sanitize metric and tag names from invalid characters
|
2018-12-21 19:26:07 +00:00
|
|
|
## Regex is more thorough, but significantly slower. default is false
|
2017-09-29 23:13:08 +00:00
|
|
|
#use_regex = false
|
|
|
|
|
|
|
|
## point tags to use as the source name for Wavefront (if none found, host will be used)
|
2018-12-21 19:26:07 +00:00
|
|
|
#source_override = ["hostname", "address", "agent_host", "node_host"]
|
2017-09-29 23:13:08 +00:00
|
|
|
|
2018-12-21 19:26:07 +00:00
|
|
|
## whether to convert boolean values to numeric values, with false -> 0.0 and true -> 1.0. default is true
|
2017-09-29 23:13:08 +00:00
|
|
|
#convert_bool = true
|
|
|
|
|
|
|
|
## Define a mapping, namespaced by metric prefix, from string values to numeric values
|
2018-12-21 19:26:07 +00:00
|
|
|
## deprecated in 1.9; use the enum processor plugin
|
2017-09-29 23:13:08 +00:00
|
|
|
#[[outputs.wavefront.string_to_number.elasticsearch]]
|
|
|
|
# green = 1.0
|
|
|
|
# yellow = 0.5
|
|
|
|
# red = 0.0
|
|
|
|
`
|
|
|
|
|
|
|
|
type MetricPoint struct {
|
|
|
|
Metric string
|
|
|
|
Value float64
|
|
|
|
Timestamp int64
|
|
|
|
Source string
|
|
|
|
Tags map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wavefront) Connect() error {
|
2018-12-21 19:26:07 +00:00
|
|
|
|
|
|
|
if len(w.StringToNumber) > 0 {
|
|
|
|
log.Print("W! [outputs.wavefront] The string_to_number option is deprecated; please use the enum processor instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.Url != "" {
|
|
|
|
log.Printf("D! [outputs.wavefront] connecting over http/https using Url: %s", w.Url)
|
|
|
|
sender, err := wavefront.NewDirectSender(&wavefront.DirectConfiguration{
|
|
|
|
Server: w.Url,
|
|
|
|
Token: w.Token,
|
|
|
|
FlushIntervalSeconds: 5,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Wavefront: Could not create Wavefront Sender for Url: %s", w.Url)
|
|
|
|
}
|
|
|
|
w.sender = sender
|
|
|
|
} else {
|
|
|
|
log.Printf("D! Output [wavefront] connecting over tcp using Host: %s and Port: %d", w.Host, w.Port)
|
|
|
|
sender, err := wavefront.NewProxySender(&wavefront.ProxyConfiguration{
|
|
|
|
Host: w.Host,
|
|
|
|
MetricsPort: w.Port,
|
|
|
|
FlushIntervalSeconds: 5,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Wavefront: Could not create Wavefront Sender for Host: %s and Port: %d", w.Host, w.Port)
|
|
|
|
}
|
|
|
|
w.sender = sender
|
|
|
|
}
|
|
|
|
|
2017-09-29 23:13:08 +00:00
|
|
|
if w.ConvertPaths && w.MetricSeparator == "_" {
|
|
|
|
w.ConvertPaths = false
|
|
|
|
}
|
|
|
|
if w.ConvertPaths {
|
|
|
|
pathReplacer = strings.NewReplacer("_", w.MetricSeparator)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wavefront) Write(metrics []telegraf.Metric) error {
|
|
|
|
|
|
|
|
for _, m := range metrics {
|
2018-12-21 19:26:07 +00:00
|
|
|
for _, point := range buildMetrics(m, w) {
|
|
|
|
err := w.sender.SendMetric(point.Metric, point.Value, point.Timestamp, point.Source, point.Tags)
|
2017-09-29 23:13:08 +00:00
|
|
|
if err != nil {
|
2018-12-21 19:26:07 +00:00
|
|
|
return fmt.Errorf("Wavefront sending error: %s", err.Error())
|
2017-09-29 23:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildMetrics(m telegraf.Metric, w *Wavefront) []*MetricPoint {
|
|
|
|
ret := []*MetricPoint{}
|
|
|
|
|
|
|
|
for fieldName, value := range m.Fields() {
|
|
|
|
var name string
|
|
|
|
if !w.SimpleFields && fieldName == "value" {
|
|
|
|
name = fmt.Sprintf("%s%s", w.Prefix, m.Name())
|
|
|
|
} else {
|
|
|
|
name = fmt.Sprintf("%s%s%s%s", w.Prefix, m.Name(), w.MetricSeparator, fieldName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.UseRegex {
|
|
|
|
name = sanitizedRegex.ReplaceAllLiteralString(name, "-")
|
|
|
|
} else {
|
|
|
|
name = sanitizedChars.Replace(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.ConvertPaths {
|
|
|
|
name = pathReplacer.Replace(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
metric := &MetricPoint{
|
|
|
|
Metric: name,
|
2018-03-28 00:30:51 +00:00
|
|
|
Timestamp: m.Time().Unix(),
|
2017-09-29 23:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
metricValue, buildError := buildValue(value, metric.Metric, w)
|
|
|
|
if buildError != nil {
|
2018-12-21 19:26:07 +00:00
|
|
|
log.Printf("D! [outputs.wavefront] %s\n", buildError.Error())
|
2017-09-29 23:13:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
metric.Value = metricValue
|
|
|
|
|
|
|
|
source, tags := buildTags(m.Tags(), w)
|
|
|
|
metric.Source = source
|
|
|
|
metric.Tags = tags
|
|
|
|
|
|
|
|
ret = append(ret, metric)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildTags(mTags map[string]string, w *Wavefront) (string, map[string]string) {
|
2018-06-11 21:54:08 +00:00
|
|
|
|
|
|
|
// Remove all empty tags.
|
|
|
|
for k, v := range mTags {
|
|
|
|
if v == "" {
|
|
|
|
delete(mTags, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 19:26:07 +00:00
|
|
|
// find source, use source_override property if needed
|
2017-09-29 23:13:08 +00:00
|
|
|
var source string
|
2018-08-13 23:37:06 +00:00
|
|
|
if s, ok := mTags["source"]; ok {
|
|
|
|
source = s
|
|
|
|
delete(mTags, "source")
|
|
|
|
} else {
|
|
|
|
sourceTagFound := false
|
|
|
|
for _, s := range w.SourceOverride {
|
|
|
|
for k, v := range mTags {
|
|
|
|
if k == s {
|
|
|
|
source = v
|
|
|
|
mTags["telegraf_host"] = mTags["host"]
|
|
|
|
sourceTagFound = true
|
|
|
|
delete(mTags, k)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if sourceTagFound {
|
2017-09-29 23:13:08 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 23:37:06 +00:00
|
|
|
|
|
|
|
if !sourceTagFound {
|
|
|
|
source = mTags["host"]
|
2017-09-29 23:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-21 19:26:07 +00:00
|
|
|
source = tagValueReplacer.Replace(source)
|
2017-09-29 23:13:08 +00:00
|
|
|
|
2018-12-21 19:26:07 +00:00
|
|
|
// remove default host tag
|
2017-09-29 23:13:08 +00:00
|
|
|
delete(mTags, "host")
|
|
|
|
|
2018-12-21 19:26:07 +00:00
|
|
|
// sanitize tag keys and values
|
|
|
|
tags := make(map[string]string)
|
|
|
|
for k, v := range mTags {
|
|
|
|
var key string
|
|
|
|
if w.UseRegex {
|
|
|
|
key = sanitizedRegex.ReplaceAllLiteralString(k, "-")
|
|
|
|
} else {
|
|
|
|
key = sanitizedChars.Replace(k)
|
|
|
|
}
|
|
|
|
val := tagValueReplacer.Replace(v)
|
|
|
|
tags[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
return source, tags
|
2017-09-29 23:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildValue(v interface{}, name string, w *Wavefront) (float64, error) {
|
|
|
|
switch p := v.(type) {
|
|
|
|
case bool:
|
|
|
|
if w.ConvertBool {
|
|
|
|
if p {
|
|
|
|
return 1, nil
|
|
|
|
} else {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case int64:
|
|
|
|
return float64(v.(int64)), nil
|
|
|
|
case uint64:
|
|
|
|
return float64(v.(uint64)), nil
|
|
|
|
case float64:
|
|
|
|
return v.(float64), nil
|
|
|
|
case string:
|
|
|
|
for prefix, mappings := range w.StringToNumber {
|
|
|
|
if strings.HasPrefix(name, prefix) {
|
|
|
|
for _, mapping := range mappings {
|
|
|
|
val, hasVal := mapping[string(p)]
|
|
|
|
if hasVal {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("unexpected type: %T, with value: %v, for: %s", v, v, name)
|
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("unexpected type: %T, with value: %v, for: %s", v, v, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, fmt.Errorf("unexpected type: %T, with value: %v, for: %s", v, v, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wavefront) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wavefront) Description() string {
|
|
|
|
return "Configuration for Wavefront server to send metrics to"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wavefront) Close() error {
|
2018-12-21 19:26:07 +00:00
|
|
|
w.sender.Close()
|
2017-09-29 23:13:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
outputs.Add("wavefront", func() telegraf.Output {
|
|
|
|
return &Wavefront{
|
2018-12-21 19:26:07 +00:00
|
|
|
Token: "DUMMY_TOKEN",
|
2017-09-29 23:13:08 +00:00
|
|
|
MetricSeparator: ".",
|
|
|
|
ConvertPaths: true,
|
|
|
|
ConvertBool: true,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|