2016-01-08 00:26:33 +00:00
|
|
|
package graphite
|
|
|
|
|
|
|
|
import (
|
2017-06-13 20:42:11 +00:00
|
|
|
"crypto/tls"
|
2016-01-08 00:26:33 +00:00
|
|
|
"errors"
|
2017-01-24 20:50:29 +00:00
|
|
|
"io"
|
2016-01-08 00:26:33 +00:00
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"time"
|
2016-02-10 22:50:07 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2018-05-04 23:33:23 +00:00
|
|
|
tlsint "github.com/influxdata/telegraf/internal/tls"
|
2016-02-10 22:50:07 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
|
|
|
"github.com/influxdata/telegraf/plugins/serializers"
|
2016-01-08 00:26:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Graphite struct {
|
2018-05-21 22:59:56 +00:00
|
|
|
GraphiteTagSupport bool
|
2017-11-01 00:00:06 +00:00
|
|
|
// URL is only for backwards compatibility
|
2016-04-08 22:04:45 +00:00
|
|
|
Servers []string
|
|
|
|
Prefix string
|
|
|
|
Template string
|
|
|
|
Timeout int
|
|
|
|
conns []net.Conn
|
2018-05-04 23:33:23 +00:00
|
|
|
tlsint.ClientConfig
|
2016-01-08 00:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## TCP endpoint for your graphite instance.
|
2016-07-14 15:06:00 +00:00
|
|
|
## If multiple endpoints are configured, output will be load balanced.
|
|
|
|
## Only one of the endpoints will be written to with each iteration.
|
2016-01-17 00:29:02 +00:00
|
|
|
servers = ["localhost:2003"]
|
2016-02-18 21:26:51 +00:00
|
|
|
## Prefix metrics name
|
2016-01-17 00:29:02 +00:00
|
|
|
prefix = ""
|
2016-04-08 22:04:45 +00:00
|
|
|
## Graphite output template
|
|
|
|
## see https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
|
|
|
|
template = "host.tags.measurement.field"
|
2018-05-21 22:59:56 +00:00
|
|
|
## Enable Graphite tags support
|
|
|
|
## see http://graphite.readthedocs.io/en/latest/tags.html
|
|
|
|
## Defaults to "false"
|
|
|
|
# graphite_tag_support = true
|
2016-02-18 21:26:51 +00:00
|
|
|
## timeout in seconds for the write connection to graphite
|
2016-01-17 00:29:02 +00:00
|
|
|
timeout = 2
|
2017-06-13 20:42:11 +00:00
|
|
|
|
2018-05-04 23:33:23 +00:00
|
|
|
## Optional TLS Config
|
|
|
|
# tls_ca = "/etc/telegraf/ca.pem"
|
|
|
|
# tls_cert = "/etc/telegraf/cert.pem"
|
|
|
|
# tls_key = "/etc/telegraf/key.pem"
|
|
|
|
## Use TLS but skip chain & host verification
|
2017-06-13 20:42:11 +00:00
|
|
|
# insecure_skip_verify = false
|
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")
|
|
|
|
}
|
2017-06-13 20:42:11 +00:00
|
|
|
|
|
|
|
// Set tls config
|
2018-05-04 23:33:23 +00:00
|
|
|
tlsConfig, err := g.ClientConfig.TLSConfig()
|
2017-06-13 20:42:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-08 00:26:33 +00:00
|
|
|
// Get Connections
|
|
|
|
var conns []net.Conn
|
|
|
|
for _, server := range g.Servers {
|
2017-06-13 20:42:11 +00:00
|
|
|
// Dialer with timeout
|
|
|
|
d := net.Dialer{Timeout: time.Duration(g.Timeout) * time.Second}
|
|
|
|
|
|
|
|
// Get secure connection if tls config is set
|
|
|
|
var conn net.Conn
|
2018-05-04 23:33:23 +00:00
|
|
|
if tlsConfig != nil {
|
|
|
|
conn, err = tls.DialWithDialer(&d, "tcp", server, tlsConfig)
|
2017-06-13 20:42:11 +00:00
|
|
|
} else {
|
|
|
|
conn, err = d.Dial("tcp", server)
|
|
|
|
}
|
|
|
|
|
2016-01-08 00:26:33 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-24 20:50:29 +00:00
|
|
|
// We need check eof as we can write to nothing without noticing anything is wrong
|
|
|
|
// the connection stays in a close_wait
|
|
|
|
// We can detect that by finding an eof
|
|
|
|
// if not for this, we can happily write and flush without getting errors (in Go) but getting RST tcp packets back (!)
|
|
|
|
// props to Tv via the authors of carbon-relay-ng` for this trick.
|
|
|
|
func checkEOF(conn net.Conn) {
|
|
|
|
b := make([]byte, 1024)
|
|
|
|
conn.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
|
|
|
|
num, err := conn.Read(b)
|
|
|
|
if err == io.EOF {
|
|
|
|
log.Printf("E! Conn %s is closed. closing conn explicitly", conn)
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// just in case i misunderstand something or the remote behaves badly
|
|
|
|
if num != 0 {
|
|
|
|
log.Printf("I! conn %s .conn.Read data? did not expect that. data: %s\n", conn, b[:num])
|
|
|
|
}
|
|
|
|
// Log non-timeout errors or close.
|
|
|
|
if e, ok := err.(net.Error); !(ok && e.Timeout()) {
|
|
|
|
log.Printf("E! conn %s checkEOF .conn.Read returned err != EOF, which is unexpected. closing conn. error: %s\n", conn, err)
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2016-11-22 12:51:57 +00:00
|
|
|
var batch []byte
|
2018-05-21 22:59:56 +00:00
|
|
|
s, err := serializers.NewGraphiteSerializer(g.Prefix, g.Template, g.GraphiteTagSupport)
|
2016-02-10 22:50:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-08 00:26:33 +00:00
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
for _, metric := range metrics {
|
2016-11-22 12:51:57 +00:00
|
|
|
buf, err := s.Serialize(metric)
|
2016-02-10 22:50:07 +00:00
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Error serializing some metrics to graphite: %s", err.Error())
|
2016-01-08 00:26:33 +00:00
|
|
|
}
|
2016-11-22 12:51:57 +00:00
|
|
|
batch = append(batch, buf...)
|
2016-01-08 00:26:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 23:27:24 +00:00
|
|
|
err = g.send(batch)
|
|
|
|
|
|
|
|
// try to reconnect and retry to send
|
|
|
|
if err != nil {
|
|
|
|
log.Println("E! Graphite: Reconnecting and retrying: ")
|
|
|
|
g.Connect()
|
|
|
|
err = g.send(batch)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphite) send(batch []byte) error {
|
2016-01-08 00:26:33 +00:00
|
|
|
// This will get set to nil if a successful write occurs
|
2018-01-17 23:27:24 +00:00
|
|
|
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 {
|
2016-07-12 20:44:11 +00:00
|
|
|
if g.Timeout > 0 {
|
|
|
|
g.conns[n].SetWriteDeadline(time.Now().Add(time.Duration(g.Timeout) * time.Second))
|
|
|
|
}
|
2017-01-24 20:50:29 +00:00
|
|
|
checkEOF(g.conns[n])
|
2016-11-22 12:51:57 +00:00
|
|
|
if _, e := g.conns[n].Write(batch); e != nil {
|
2016-01-08 00:26:33 +00:00
|
|
|
// Error
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Println("E! Graphite Error: " + e.Error())
|
2018-01-17 23:27:24 +00:00
|
|
|
// Close explicitely
|
|
|
|
g.conns[n].Close()
|
2016-01-08 00:26:33 +00:00
|
|
|
// Let's try the next one
|
|
|
|
} else {
|
|
|
|
// Success
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 23:27:24 +00:00
|
|
|
|
2016-01-08 00:26:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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{}
|
|
|
|
})
|
|
|
|
}
|