telegraf/plugins/outputs/graphite/graphite.go

123 lines
2.8 KiB
Go
Raw Normal View History

2016-01-08 00:26:33 +00:00
package graphite
import (
"errors"
"log"
"math/rand"
"net"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/outputs"
"github.com/influxdata/telegraf/plugins/serializers"
2016-01-08 00:26:33 +00:00
)
type Graphite struct {
// URL is only for backwards compatability
Servers []string
Prefix string
Template string
Timeout int
conns []net.Conn
2016-01-08 00:26:33 +00:00
}
var sampleConfig = `
## TCP endpoint for your graphite instance.
## If multiple endpoints are configured, output will be load balanced.
## Only one of the endpoints will be written to with each iteration.
servers = ["localhost:2003"]
## Prefix metrics name
prefix = ""
## Graphite output template
## see https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
template = "host.tags.measurement.field"
## timeout in seconds for the write connection to graphite
timeout = 2
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 {
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.
func (g *Graphite) Write(metrics []telegraf.Metric) error {
2016-01-08 00:26:33 +00:00
// Prepare data
var batch []byte
s, err := serializers.NewGraphiteSerializer(g.Prefix, g.Template)
if err != nil {
return err
}
2016-01-08 00:26:33 +00:00
for _, metric := range metrics {
buf, err := s.Serialize(metric)
if err != nil {
log.Printf("E! Error serializing some metrics to graphite: %s", err.Error())
2016-01-08 00:26:33 +00:00
}
batch = append(batch, buf...)
2016-01-08 00:26:33 +00:00
}
// This will get set to nil if a successful write occurs
err = errors.New("Could not write to any Graphite server in cluster\n")
2016-01-08 00:26:33 +00:00
// Send data to a random server
p := rand.Perm(len(g.conns))
for _, n := range p {
if g.Timeout > 0 {
g.conns[n].SetWriteDeadline(time.Now().Add(time.Duration(g.Timeout) * time.Second))
}
if _, e := g.conns[n].Write(batch); e != nil {
2016-01-08 00:26:33 +00:00
// Error
log.Println("E! Graphite Error: " + e.Error())
2016-01-08 00:26:33 +00:00
// Let's try the next one
} else {
// Success
err = nil
break
}
}
// try to reconnect
if err != nil {
g.Connect()
}
return err
}
func init() {
outputs.Add("graphite", func() telegraf.Output {
2016-01-08 00:26:33 +00:00
return &Graphite{}
})
}