2015-09-10 18:34:36 +00:00
|
|
|
package opentsdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
"github.com/influxdb/influxdb/client/v2"
|
2015-09-10 19:27:34 +00:00
|
|
|
"github.com/influxdb/telegraf/outputs"
|
2015-09-10 18:34:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OpenTSDB struct {
|
|
|
|
Prefix string
|
|
|
|
|
|
|
|
Host string
|
|
|
|
Port int
|
2015-09-14 10:28:10 +00:00
|
|
|
|
|
|
|
Debug bool
|
2015-09-10 18:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2015-10-15 21:53:29 +00:00
|
|
|
# prefix for metrics keys
|
|
|
|
prefix = "my.specific.prefix."
|
2015-09-10 18:34:36 +00:00
|
|
|
|
2015-10-15 21:53:29 +00:00
|
|
|
## Telnet Mode ##
|
|
|
|
# DNS name of the OpenTSDB server in telnet mode
|
|
|
|
host = "opentsdb.example.com"
|
2015-09-10 18:34:36 +00:00
|
|
|
|
2015-10-15 21:53:29 +00:00
|
|
|
# Port of the OpenTSDB server in telnet mode
|
|
|
|
port = 4242
|
2015-09-14 10:28:10 +00:00
|
|
|
|
2015-10-15 21:53:29 +00:00
|
|
|
# Debug true - Prints OpenTSDB communication
|
|
|
|
debug = false
|
2015-09-10 18:34:36 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type MetricLine struct {
|
|
|
|
Metric string
|
|
|
|
Timestamp int64
|
|
|
|
Value string
|
|
|
|
Tags string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OpenTSDB) Connect() error {
|
|
|
|
// Test Connection to OpenTSDB Server
|
|
|
|
uri := fmt.Sprintf("%s:%d", o.Host, o.Port)
|
|
|
|
tcpAddr, err := net.ResolveTCPAddr("tcp", uri)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("OpenTSDB: TCP address cannot be resolved")
|
|
|
|
}
|
|
|
|
connection, err := net.DialTCP("tcp", nil, tcpAddr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("OpenTSDB: Telnet connect fail")
|
|
|
|
}
|
2015-10-16 22:13:32 +00:00
|
|
|
defer connection.Close()
|
2015-09-10 18:34:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
func (o *OpenTSDB) Write(points []*client.Point) error {
|
|
|
|
if len(points) == 0 {
|
2015-09-10 18:34:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var timeNow = time.Now()
|
|
|
|
// Send Data with telnet / socket communication
|
|
|
|
uri := fmt.Sprintf("%s:%d", o.Host, o.Port)
|
|
|
|
tcpAddr, _ := net.ResolveTCPAddr("tcp", uri)
|
|
|
|
connection, err := net.DialTCP("tcp", nil, tcpAddr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("OpenTSDB: Telnet connect fail")
|
|
|
|
}
|
2015-10-16 22:13:32 +00:00
|
|
|
for _, pt := range points {
|
2015-09-10 18:34:36 +00:00
|
|
|
metric := &MetricLine{
|
2015-10-16 22:13:32 +00:00
|
|
|
Metric: fmt.Sprintf("%s%s", o.Prefix, pt.Name()),
|
2015-09-10 18:34:36 +00:00
|
|
|
Timestamp: timeNow.Unix(),
|
|
|
|
}
|
2015-10-16 22:13:32 +00:00
|
|
|
|
|
|
|
metricValue, buildError := buildValue(pt)
|
2015-09-14 10:28:10 +00:00
|
|
|
if buildError != nil {
|
|
|
|
fmt.Printf("OpenTSDB: %s\n", buildError.Error())
|
|
|
|
continue
|
2015-09-10 18:34:36 +00:00
|
|
|
}
|
2015-09-14 10:28:10 +00:00
|
|
|
metric.Value = metricValue
|
2015-09-10 18:34:36 +00:00
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
tagsSlice := buildTags(pt.Tags())
|
2015-09-10 18:34:36 +00:00
|
|
|
metric.Tags = fmt.Sprint(strings.Join(tagsSlice, " "))
|
|
|
|
|
|
|
|
messageLine := fmt.Sprintf("put %s %v %s %s\n", metric.Metric, metric.Timestamp, metric.Value, metric.Tags)
|
2015-09-14 10:28:10 +00:00
|
|
|
if o.Debug {
|
|
|
|
fmt.Print(messageLine)
|
|
|
|
}
|
2015-09-10 18:34:36 +00:00
|
|
|
_, err := connection.Write([]byte(messageLine))
|
|
|
|
if err != nil {
|
2015-10-07 00:43:40 +00:00
|
|
|
return fmt.Errorf("OpenTSDB: Telnet writing error %s", err.Error())
|
2015-09-10 18:34:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
defer connection.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
func buildTags(ptTags map[string]string) []string {
|
|
|
|
tags := make([]string, len(ptTags))
|
2015-09-10 18:34:36 +00:00
|
|
|
index := 0
|
|
|
|
for k, v := range ptTags {
|
|
|
|
tags[index] = fmt.Sprintf("%s=%s", k, v)
|
|
|
|
index += 1
|
|
|
|
}
|
|
|
|
sort.Strings(tags)
|
|
|
|
return tags
|
|
|
|
}
|
|
|
|
|
2015-10-16 22:13:32 +00:00
|
|
|
func buildValue(pt *client.Point) (string, error) {
|
2015-09-10 18:34:36 +00:00
|
|
|
var retv string
|
2015-10-16 22:13:32 +00:00
|
|
|
var v = pt.Fields()["value"]
|
2015-09-10 18:34:36 +00:00
|
|
|
switch p := v.(type) {
|
|
|
|
case int64:
|
|
|
|
retv = IntToString(int64(p))
|
|
|
|
case uint64:
|
|
|
|
retv = UIntToString(uint64(p))
|
|
|
|
case float64:
|
|
|
|
retv = FloatToString(float64(p))
|
|
|
|
default:
|
2015-09-14 10:28:10 +00:00
|
|
|
return retv, fmt.Errorf("unexpected type %T with value %v for OpenTSDB", v, v)
|
2015-09-10 18:34:36 +00:00
|
|
|
}
|
|
|
|
return retv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func IntToString(input_num int64) string {
|
|
|
|
return strconv.FormatInt(input_num, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UIntToString(input_num uint64) string {
|
|
|
|
return strconv.FormatUint(input_num, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FloatToString(input_num float64) string {
|
|
|
|
return strconv.FormatFloat(input_num, 'f', 6, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OpenTSDB) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OpenTSDB) Description() string {
|
|
|
|
return "Configuration for OpenTSDB server to send metrics to"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OpenTSDB) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
outputs.Add("opentsdb", func() outputs.Output {
|
|
|
|
return &OpenTSDB{}
|
|
|
|
})
|
|
|
|
}
|