2015-10-22 21:50:19 +00:00
|
|
|
package riemann
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-10-11 11:28:20 +00:00
|
|
|
"log"
|
2017-01-27 22:54:59 +00:00
|
|
|
"net/url"
|
2015-10-22 21:50:19 +00:00
|
|
|
"os"
|
2016-02-03 22:06:41 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2017-03-27 22:49:45 +00:00
|
|
|
"time"
|
2015-10-22 21:50:19 +00:00
|
|
|
|
|
|
|
"github.com/amir/raidman"
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2017-03-27 22:49:45 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
2015-10-22 21:50:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Riemann struct {
|
2017-01-27 22:54:59 +00:00
|
|
|
URL string
|
|
|
|
TTL float32
|
|
|
|
Separator string
|
|
|
|
MeasurementAsAttribute bool
|
|
|
|
StringAsState bool
|
|
|
|
TagKeys []string
|
|
|
|
Tags []string
|
|
|
|
DescriptionText string
|
2017-03-27 22:49:45 +00:00
|
|
|
Timeout internal.Duration
|
2015-10-22 21:50:19 +00:00
|
|
|
|
2015-11-18 21:32:29 +00:00
|
|
|
client *raidman.Client
|
2015-10-22 21:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2017-01-27 22:54:59 +00:00
|
|
|
## The full TCP or UDP URL of the Riemann server
|
|
|
|
url = "tcp://localhost:5555"
|
|
|
|
|
|
|
|
## Riemann event TTL, floating-point time in seconds.
|
|
|
|
## Defines how long that an event is considered valid for in Riemann
|
|
|
|
# ttl = 30.0
|
|
|
|
|
|
|
|
## Separator to use between measurement and field name in Riemann service name
|
|
|
|
## This does not have any effect if 'measurement_as_attribute' is set to 'true'
|
|
|
|
separator = "/"
|
|
|
|
|
|
|
|
## Set measurement name as Riemann attribute 'measurement', instead of prepending it to the Riemann service name
|
|
|
|
# measurement_as_attribute = false
|
|
|
|
|
|
|
|
## Send string metrics as Riemann event states.
|
|
|
|
## Unless enabled all string metrics will be ignored
|
|
|
|
# string_as_state = false
|
|
|
|
|
|
|
|
## A list of tag keys whose values get sent as Riemann tags.
|
|
|
|
## If empty, all Telegraf tag values will be sent as tags
|
|
|
|
# tag_keys = ["telegraf","custom_tag"]
|
|
|
|
|
|
|
|
## Additional Riemann tags to send.
|
|
|
|
# tags = ["telegraf-output"]
|
|
|
|
|
|
|
|
## Description for Riemann event
|
|
|
|
# description_text = "metrics collected from telegraf"
|
2017-03-27 22:49:45 +00:00
|
|
|
|
|
|
|
## Riemann client write timeout, defaults to "5s" if not set.
|
|
|
|
# timeout = "5s"
|
2015-10-22 21:50:19 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (r *Riemann) Connect() error {
|
2017-01-27 22:54:59 +00:00
|
|
|
parsed_url, err := url.Parse(r.URL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-22 21:50:19 +00:00
|
|
|
|
2017-03-27 22:49:45 +00:00
|
|
|
client, err := raidman.DialWithTimeout(parsed_url.Scheme, parsed_url.Host, r.Timeout.Duration)
|
2015-10-22 21:50:19 +00:00
|
|
|
if err != nil {
|
2016-04-12 17:20:27 +00:00
|
|
|
r.client = nil
|
2015-10-22 21:50:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-27 22:54:59 +00:00
|
|
|
r.client = client
|
2015-10-22 21:50:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Riemann) Close() error {
|
2017-01-27 22:54:59 +00:00
|
|
|
if r.client != nil {
|
|
|
|
r.client.Close()
|
|
|
|
r.client = nil
|
2016-04-12 17:20:27 +00:00
|
|
|
}
|
2015-10-22 21:50:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Riemann) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Riemann) Description() string {
|
|
|
|
return "Configuration for the Riemann server to send metrics to"
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func (r *Riemann) Write(metrics []telegraf.Metric) error {
|
|
|
|
if len(metrics) == 0 {
|
2015-10-22 21:50:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-12 17:20:27 +00:00
|
|
|
if r.client == nil {
|
2017-01-27 22:54:59 +00:00
|
|
|
if err := r.Connect(); err != nil {
|
|
|
|
return fmt.Errorf("Failed to (re)connect to Riemann: %s", err.Error())
|
2016-04-12 17:20:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 22:54:59 +00:00
|
|
|
// build list of Riemann events to send
|
2015-10-22 21:50:19 +00:00
|
|
|
var events []*raidman.Event
|
2017-01-27 22:54:59 +00:00
|
|
|
for _, m := range metrics {
|
|
|
|
evs := r.buildRiemannEvents(m)
|
2015-12-19 21:55:44 +00:00
|
|
|
for _, ev := range evs {
|
|
|
|
events = append(events, ev)
|
|
|
|
}
|
2015-10-22 21:50:19 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 22:54:59 +00:00
|
|
|
if err := r.client.SendMulti(events); err != nil {
|
|
|
|
r.Close()
|
|
|
|
return fmt.Errorf("Failed to send riemann message: %s", err)
|
2015-10-22 21:50:19 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-27 22:54:59 +00:00
|
|
|
func (r *Riemann) buildRiemannEvents(m telegraf.Metric) []*raidman.Event {
|
2015-12-19 21:55:44 +00:00
|
|
|
events := []*raidman.Event{}
|
2017-01-27 22:54:59 +00:00
|
|
|
for fieldName, value := range m.Fields() {
|
|
|
|
// get host for Riemann event
|
|
|
|
host, ok := m.Tags()["host"]
|
2015-12-19 21:55:44 +00:00
|
|
|
if !ok {
|
2017-01-27 22:54:59 +00:00
|
|
|
if hostname, err := os.Hostname(); err == nil {
|
2015-12-19 21:55:44 +00:00
|
|
|
host = hostname
|
2017-01-27 22:54:59 +00:00
|
|
|
} else {
|
|
|
|
host = "unknown"
|
2015-12-19 21:55:44 +00:00
|
|
|
}
|
2015-10-22 21:50:19 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 21:55:44 +00:00
|
|
|
event := &raidman.Event{
|
2017-01-27 22:54:59 +00:00
|
|
|
Host: host,
|
|
|
|
Ttl: r.TTL,
|
|
|
|
Description: r.DescriptionText,
|
|
|
|
Time: m.Time().Unix(),
|
|
|
|
|
|
|
|
Attributes: r.attributes(m.Name(), m.Tags()),
|
|
|
|
Service: r.service(m.Name(), fieldName),
|
|
|
|
Tags: r.tags(m.Tags()),
|
2015-12-19 21:55:44 +00:00
|
|
|
}
|
2016-02-03 22:06:41 +00:00
|
|
|
|
|
|
|
switch value.(type) {
|
|
|
|
case string:
|
2017-01-27 22:54:59 +00:00
|
|
|
// only send string metrics if explicitly enabled, skip otherwise
|
|
|
|
if !r.StringAsState {
|
|
|
|
log.Printf("D! Riemann event states disabled, skipping metric value [%s]\n", value)
|
|
|
|
continue
|
|
|
|
}
|
2016-02-03 22:06:41 +00:00
|
|
|
event.State = value.(string)
|
2017-01-27 22:54:59 +00:00
|
|
|
case int, int64, uint64, float32, float64:
|
2016-02-03 22:06:41 +00:00
|
|
|
event.Metric = value
|
2017-01-27 22:54:59 +00:00
|
|
|
default:
|
|
|
|
log.Printf("D! Riemann does not support metric value [%s]\n", value)
|
|
|
|
continue
|
2016-02-03 22:06:41 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 21:55:44 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-27 22:54:59 +00:00
|
|
|
func (r *Riemann) attributes(name string, tags map[string]string) map[string]string {
|
|
|
|
if r.MeasurementAsAttribute {
|
|
|
|
tags["measurement"] = name
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(tags, "host") // exclude 'host' tag
|
|
|
|
return tags
|
|
|
|
}
|
2016-02-03 22:06:41 +00:00
|
|
|
|
2017-01-27 22:54:59 +00:00
|
|
|
func (r *Riemann) service(name string, field string) string {
|
|
|
|
var serviceStrings []string
|
2016-02-03 22:06:41 +00:00
|
|
|
|
2017-01-27 22:54:59 +00:00
|
|
|
// if measurement is not enabled as an attribute then prepend it to service name
|
|
|
|
if !r.MeasurementAsAttribute {
|
|
|
|
serviceStrings = append(serviceStrings, name)
|
2016-02-03 22:06:41 +00:00
|
|
|
}
|
2017-01-27 22:54:59 +00:00
|
|
|
serviceStrings = append(serviceStrings, field)
|
2016-02-03 22:06:41 +00:00
|
|
|
|
2017-01-27 22:54:59 +00:00
|
|
|
return strings.Join(serviceStrings, r.Separator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Riemann) tags(tags map[string]string) []string {
|
|
|
|
// always add specified Riemann tags
|
|
|
|
values := r.Tags
|
|
|
|
|
|
|
|
// if tag_keys are specified, add those and return tag list
|
|
|
|
if len(r.TagKeys) > 0 {
|
|
|
|
for _, tagName := range r.TagKeys {
|
|
|
|
value, ok := tags[tagName]
|
|
|
|
if ok {
|
|
|
|
values = append(values, value)
|
|
|
|
}
|
2016-02-03 22:06:41 +00:00
|
|
|
}
|
2017-01-27 22:54:59 +00:00
|
|
|
return values
|
2016-02-03 22:06:41 +00:00
|
|
|
}
|
2017-01-27 22:54:59 +00:00
|
|
|
|
|
|
|
// otherwise add all values from telegraf tag key/value pairs
|
|
|
|
var keys []string
|
|
|
|
for key := range tags {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
if key != "host" { // exclude 'host' tag
|
|
|
|
values = append(values, tags[key])
|
|
|
|
}
|
2016-02-03 22:06:41 +00:00
|
|
|
}
|
2017-01-27 22:54:59 +00:00
|
|
|
return values
|
2016-02-03 22:06:41 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 21:50:19 +00:00
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
outputs.Add("riemann", func() telegraf.Output {
|
2017-03-27 22:49:45 +00:00
|
|
|
return &Riemann{
|
|
|
|
Timeout: internal.Duration{Duration: time.Second * 5},
|
|
|
|
}
|
2015-10-22 21:50:19 +00:00
|
|
|
})
|
|
|
|
}
|