telegraf/plugins/outputs/riemann/riemann.go

102 lines
1.7 KiB
Go
Raw Normal View History

2015-10-22 21:50:19 +00:00
package riemann
import (
"errors"
"fmt"
"os"
"github.com/amir/raidman"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/outputs"
2015-10-22 21:50:19 +00:00
)
type Riemann struct {
URL string
Transport string
client *raidman.Client
2015-10-22 21:50:19 +00:00
}
var sampleConfig = `
### URL of server
2015-10-22 21:50:19 +00:00
url = "localhost:5555"
### transport protocol to use either tcp or udp
2015-10-22 21:50:19 +00:00
transport = "tcp"
`
func (r *Riemann) Connect() error {
c, err := raidman.Dial(r.Transport, r.URL)
if err != nil {
return err
}
r.client = c
2015-10-22 21:50:19 +00:00
return nil
}
func (r *Riemann) Close() error {
r.client.Close()
return nil
}
func (r *Riemann) SampleConfig() string {
return sampleConfig
}
func (r *Riemann) Description() string {
return "Configuration for the Riemann server to send metrics to"
}
func (r *Riemann) Write(metrics []telegraf.Metric) error {
if len(metrics) == 0 {
2015-10-22 21:50:19 +00:00
return nil
}
var events []*raidman.Event
for _, p := range metrics {
2015-12-19 21:55:44 +00:00
evs := buildEvents(p)
for _, ev := range evs {
events = append(events, ev)
}
2015-10-22 21:50:19 +00:00
}
var senderr = r.client.SendMulti(events)
if senderr != nil {
return errors.New(fmt.Sprintf("FAILED to send riemann message: %s\n",
senderr))
}
return nil
}
func buildEvents(p telegraf.Metric) []*raidman.Event {
2015-12-19 21:55:44 +00:00
events := []*raidman.Event{}
for fieldName, value := range p.Fields() {
host, ok := p.Tags()["host"]
if !ok {
hostname, err := os.Hostname()
if err != nil {
host = "unknown"
} else {
host = hostname
}
2015-10-22 21:50:19 +00:00
}
2015-12-19 21:55:44 +00:00
event := &raidman.Event{
Host: host,
Service: p.Name() + "_" + fieldName,
Metric: value,
}
events = append(events, event)
2015-10-22 21:50:19 +00:00
}
2015-12-19 21:55:44 +00:00
return events
2015-10-22 21:50:19 +00:00
}
func init() {
outputs.Add("riemann", func() telegraf.Output {
2015-10-22 21:50:19 +00:00
return &Riemann{}
})
}