2015-09-10 18:34:36 +00:00
|
|
|
package opentsdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2016-07-18 19:01:36 +00:00
|
|
|
"encoding/json"
|
|
|
|
//"os"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"bytes"
|
2015-09-10 18:34:36 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/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
|
|
|
|
2016-07-18 19:01:36 +00:00
|
|
|
UseHttp bool
|
|
|
|
|
2015-09-14 10:28:10 +00:00
|
|
|
Debug bool
|
2015-09-10 18:34:36 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 23:00:06 +00:00
|
|
|
var sanitizedChars = strings.NewReplacer("@", "-", "*", "-", " ", "_",
|
2016-05-25 10:37:48 +00:00
|
|
|
`%`, "-", "#", "-", "$", "-", ":", "_")
|
2016-04-28 23:00:06 +00:00
|
|
|
|
2015-09-10 18:34:36 +00:00
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## prefix for metrics keys
|
2015-10-15 21:53:29 +00:00
|
|
|
prefix = "my.specific.prefix."
|
2015-09-10 18:34:36 +00:00
|
|
|
|
2015-10-15 21:53:29 +00:00
|
|
|
## Telnet Mode ##
|
2016-07-18 19:01:36 +00:00
|
|
|
## DNS name of the OpenTSDB server
|
2015-10-15 21:53:29 +00:00
|
|
|
host = "opentsdb.example.com"
|
2015-09-10 18:34:36 +00:00
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## Port of the OpenTSDB server in telnet mode
|
2015-10-15 21:53:29 +00:00
|
|
|
port = 4242
|
2015-09-14 10:28:10 +00:00
|
|
|
|
2016-07-18 19:01:36 +00:00
|
|
|
## Use Http PUT API
|
|
|
|
useHttp = false
|
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## Debug true - Prints OpenTSDB communication
|
2015-10-15 21:53:29 +00:00
|
|
|
debug = false
|
2015-09-10 18:34:36 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type MetricLine struct {
|
|
|
|
Metric string
|
|
|
|
Timestamp int64
|
|
|
|
Value string
|
|
|
|
Tags string
|
|
|
|
}
|
|
|
|
|
2016-07-18 19:01:36 +00:00
|
|
|
type HttpMetric struct {
|
|
|
|
Metric string `json:"metric"`
|
|
|
|
Timestamp int64 `json:"timestamp"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
Tags map[string]string `json:"tags"`
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:34:36 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func (o *OpenTSDB) Write(metrics []telegraf.Metric) error {
|
|
|
|
if len(metrics) == 0 {
|
2015-09-10 18:34:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-07-18 19:01:36 +00:00
|
|
|
|
|
|
|
if o.UseHttp {
|
|
|
|
return o.WriteHttp(metrics)
|
|
|
|
} else {
|
|
|
|
return o.WriteTelnet(metrics)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OpenTSDB) WriteHttp(metrics []telegraf.Metric) error {
|
|
|
|
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
|
|
|
|
enc := json.NewEncoder(b)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
io.WriteString(b, "[")
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
|
|
|
|
for _, m := range metrics {
|
|
|
|
now := m.UnixNano() / 1000000000
|
|
|
|
for fieldName, value := range m.Fields() {
|
|
|
|
metricValue, buildError := buildValue(value)
|
|
|
|
if buildError != nil {
|
|
|
|
fmt.Printf("OpenTSDB: %s\n", buildError.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > 0 {
|
|
|
|
io.WriteString(b, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
metric := &HttpMetric{
|
|
|
|
Metric: sanitizedChars.Replace(fmt.Sprintf("%s%s_%s",
|
|
|
|
o.Prefix, m.Name(), fieldName)),
|
|
|
|
Tags: m.Tags(),
|
|
|
|
Timestamp: now,
|
|
|
|
Value: metricValue,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := enc.Encode(metric); err != nil {
|
|
|
|
return fmt.Errorf("Metric serialization error %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
io.WriteString(b, "]")
|
|
|
|
|
|
|
|
uri := fmt.Sprintf("http://%s:%d/api/put", o.Host, o.Port)
|
|
|
|
resp, err := http.Post(uri, "applicaton/json", b)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error when sending metrics: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == 200 {
|
|
|
|
fmt.Println("Sent metrics !")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OpenTSDB) WriteTelnet(metrics []telegraf.Metric) error {
|
2015-12-19 21:30:37 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
2015-09-10 18:34:36 +00:00
|
|
|
// 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-12-19 21:30:37 +00:00
|
|
|
defer connection.Close()
|
2015-09-10 18:34:36 +00:00
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
for _, m := range metrics {
|
|
|
|
for _, metric := range buildMetrics(m, now, o.Prefix) {
|
2015-12-19 21:30:37 +00:00
|
|
|
messageLine := fmt.Sprintf("put %s %v %s %s\n",
|
|
|
|
metric.Metric, metric.Timestamp, metric.Value, metric.Tags)
|
|
|
|
if o.Debug {
|
|
|
|
fmt.Print(messageLine)
|
|
|
|
}
|
|
|
|
_, err := connection.Write([]byte(messageLine))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("OpenTSDB: Telnet writing error %s", err.Error())
|
|
|
|
}
|
2015-09-10 18:34:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func buildTags(mTags map[string]string) []string {
|
|
|
|
tags := make([]string, len(mTags))
|
2015-09-10 18:34:36 +00:00
|
|
|
index := 0
|
2016-01-27 23:15:14 +00:00
|
|
|
for k, v := range mTags {
|
2016-04-28 23:00:06 +00:00
|
|
|
tags[index] = sanitizedChars.Replace(fmt.Sprintf("%s=%s", k, v))
|
|
|
|
index++
|
2015-09-10 18:34:36 +00:00
|
|
|
}
|
|
|
|
sort.Strings(tags)
|
|
|
|
return tags
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func buildMetrics(m telegraf.Metric, now time.Time, prefix string) []*MetricLine {
|
2015-12-19 21:30:37 +00:00
|
|
|
ret := []*MetricLine{}
|
2016-01-27 23:15:14 +00:00
|
|
|
for fieldName, value := range m.Fields() {
|
2015-12-19 21:30:37 +00:00
|
|
|
metric := &MetricLine{
|
2016-04-28 23:00:06 +00:00
|
|
|
Metric: sanitizedChars.Replace(fmt.Sprintf("%s%s_%s",
|
|
|
|
prefix, m.Name(), fieldName)),
|
2015-12-19 21:30:37 +00:00
|
|
|
Timestamp: now.Unix(),
|
|
|
|
}
|
|
|
|
|
|
|
|
metricValue, buildError := buildValue(value)
|
|
|
|
if buildError != nil {
|
|
|
|
fmt.Printf("OpenTSDB: %s\n", buildError.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
metric.Value = metricValue
|
2016-01-27 23:15:14 +00:00
|
|
|
tagsSlice := buildTags(m.Tags())
|
2015-12-19 21:30:37 +00:00
|
|
|
metric.Tags = fmt.Sprint(strings.Join(tagsSlice, " "))
|
|
|
|
ret = append(ret, metric)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildValue(v interface{}) (string, error) {
|
2015-09-10 18:34:36 +00:00
|
|
|
var retv string
|
|
|
|
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() {
|
2016-01-27 21:21:36 +00:00
|
|
|
outputs.Add("opentsdb", func() telegraf.Output {
|
2015-09-10 18:34:36 +00:00
|
|
|
return &OpenTSDB{}
|
|
|
|
})
|
|
|
|
}
|