2016-01-08 00:26:33 +00:00
|
|
|
package graphite
|
|
|
|
|
|
|
|
import (
|
2016-02-04 13:28:20 +00:00
|
|
|
"bytes"
|
2016-01-08 00:26:33 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
2016-01-08 00:26:33 +00:00
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
2016-01-27 21:15:31 +00:00
|
|
|
"sort"
|
2016-01-08 00:26:33 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Graphite struct {
|
|
|
|
// URL is only for backwards compatability
|
2016-02-04 13:28:20 +00:00
|
|
|
Servers []string
|
|
|
|
Prefix string
|
|
|
|
Timeout int
|
|
|
|
MetricsNameBuilder map[string][]string
|
|
|
|
conns []net.Conn
|
2016-01-08 00:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-01-17 00:29:02 +00:00
|
|
|
# TCP endpoint for your graphite instance.
|
|
|
|
servers = ["localhost:2003"]
|
2016-01-08 00:26:33 +00:00
|
|
|
# Prefix metrics name
|
2016-01-17 00:29:02 +00:00
|
|
|
prefix = ""
|
|
|
|
# timeout in seconds for the write connection to graphite
|
|
|
|
timeout = 2
|
2016-02-04 13:28:20 +00:00
|
|
|
# # Build custom metric name from tags for each plugins
|
|
|
|
# [graphite.metricsnamebuilder]
|
|
|
|
# # Igore unlisted tags and put metric name after disk name
|
|
|
|
# diskio = ["host","{{metric}}","name","{{field}}"]
|
|
|
|
# disk = ["host","{{metric}}","name","{{field}}"]
|
2016-01-08 00:26:33 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (g *Graphite) Connect() error {
|
|
|
|
// Set default values
|
|
|
|
if g.Timeout <= 0 {
|
|
|
|
g.Timeout = 2
|
|
|
|
}
|
|
|
|
if len(g.Servers) == 0 {
|
|
|
|
g.Servers = append(g.Servers, "localhost:2003")
|
|
|
|
}
|
|
|
|
// Get Connections
|
|
|
|
var conns []net.Conn
|
|
|
|
for _, server := range g.Servers {
|
|
|
|
conn, err := net.DialTimeout("tcp", server, time.Duration(g.Timeout)*time.Second)
|
|
|
|
if err == nil {
|
|
|
|
conns = append(conns, conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.conns = conns
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphite) Close() error {
|
|
|
|
// Closing all connections
|
|
|
|
for _, conn := range g.conns {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphite) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphite) Description() string {
|
2016-01-17 00:29:02 +00:00
|
|
|
return "Configuration for Graphite server to send metrics to"
|
2016-01-08 00:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Choose a random server in the cluster to write to until a successful write
|
|
|
|
// occurs, logging each unsuccessful. If all servers fail, return error.
|
2016-01-27 23:15:14 +00:00
|
|
|
func (g *Graphite) Write(metrics []telegraf.Metric) error {
|
2016-01-08 00:26:33 +00:00
|
|
|
// Prepare data
|
|
|
|
var bp []string
|
2016-01-27 21:15:31 +00:00
|
|
|
for _, metric := range metrics {
|
2016-01-08 00:26:33 +00:00
|
|
|
// Convert UnixNano to Unix timestamps
|
2016-01-27 21:15:31 +00:00
|
|
|
timestamp := metric.UnixNano() / 1000000000
|
|
|
|
for field_name, value := range metric.Fields() {
|
2016-01-08 00:26:33 +00:00
|
|
|
// Convert value
|
|
|
|
value_str := fmt.Sprintf("%#v", value)
|
2016-01-27 21:15:31 +00:00
|
|
|
// Write graphite metric
|
2016-02-04 13:28:20 +00:00
|
|
|
graphitePoint := fmt.Sprintf("%s %s %d\n",
|
|
|
|
g.buildMetricName(metric, field_name),
|
|
|
|
value_str,
|
|
|
|
timestamp)
|
2016-01-08 00:26:33 +00:00
|
|
|
bp = append(bp, graphitePoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
graphitePoints := strings.Join(bp, "")
|
|
|
|
|
|
|
|
// This will get set to nil if a successful write occurs
|
|
|
|
err := errors.New("Could not write to any Graphite server in cluster\n")
|
|
|
|
// Send data to a random server
|
|
|
|
p := rand.Perm(len(g.conns))
|
|
|
|
for _, n := range p {
|
|
|
|
if _, e := fmt.Fprintf(g.conns[n], graphitePoints); e != nil {
|
|
|
|
// Error
|
|
|
|
log.Println("ERROR: " + err.Error())
|
|
|
|
// Let's try the next one
|
|
|
|
} else {
|
|
|
|
// Success
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// try to reconnect
|
|
|
|
if err != nil {
|
|
|
|
g.Connect()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2016-02-04 13:28:20 +00:00
|
|
|
func (g *Graphite) buildMetricName(metric telegraf.Metric, fieldName string) string {
|
|
|
|
metricName := bytes.NewBufferString(g.Prefix)
|
|
|
|
metricName.WriteString(".")
|
2016-01-27 21:15:31 +00:00
|
|
|
tags := metric.Tags()
|
2016-02-04 13:28:20 +00:00
|
|
|
metricsTemplate, ok := g.MetricsNameBuilder[metric.Name()]
|
|
|
|
if !ok {
|
|
|
|
metricsTemplate = []string{"host"}
|
|
|
|
for k := range tags {
|
|
|
|
if k != "host" {
|
|
|
|
metricsTemplate = append(metricsTemplate, k)
|
|
|
|
}
|
2016-01-27 21:15:31 +00:00
|
|
|
}
|
2016-02-04 13:28:20 +00:00
|
|
|
sort.Strings(metricsTemplate[1:])
|
|
|
|
if metric.Name() != fieldName {
|
|
|
|
metricsTemplate = append(metricsTemplate, "{{metric}}")
|
2016-01-27 21:15:31 +00:00
|
|
|
}
|
2016-02-04 13:28:20 +00:00
|
|
|
metricsTemplate = append(metricsTemplate, "{{field}}")
|
2016-01-27 21:15:31 +00:00
|
|
|
}
|
2016-02-04 13:28:20 +00:00
|
|
|
tags["{{metric}}"] = metric.Name()
|
|
|
|
tags["{{field}}"] = fieldName
|
|
|
|
for _, tagName := range metricsTemplate {
|
|
|
|
tagValue, ok := tags[tagName]
|
|
|
|
if !ok || tagValue == "" {
|
|
|
|
continue
|
2016-01-27 21:15:31 +00:00
|
|
|
}
|
2016-02-04 13:28:20 +00:00
|
|
|
metricName.WriteString(strings.Replace(tagValue, ".", "_", -1))
|
|
|
|
metricName.WriteString(".")
|
2016-01-27 21:15:31 +00:00
|
|
|
}
|
2016-02-04 13:28:20 +00:00
|
|
|
return strings.Trim(metricName.String(), ".")
|
2016-01-27 21:15:31 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 00:26:33 +00:00
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
outputs.Add("graphite", func() telegraf.Output {
|
2016-01-08 00:26:33 +00:00
|
|
|
return &Graphite{}
|
|
|
|
})
|
|
|
|
}
|