2015-09-10 18:34:36 +00:00
|
|
|
package opentsdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2016-07-22 17:02:26 +00:00
|
|
|
"net/url"
|
2016-07-21 13:54:09 +00:00
|
|
|
"sort"
|
2015-09-10 18:34:36 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
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-22 17:02:26 +00:00
|
|
|
HttpBatchSize int
|
2016-07-18 19:01:36 +00:00
|
|
|
|
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
|
|
|
|
2016-07-18 19:01:36 +00:00
|
|
|
## DNS name of the OpenTSDB server
|
2016-07-22 17:02:26 +00:00
|
|
|
## Using "opentsdb.example.com" or "tcp://opentsdb.example.com" will use the
|
|
|
|
## telnet API. "http://opentsdb.example.com" will use the Http API.
|
2015-10-15 21:53:29 +00:00
|
|
|
host = "opentsdb.example.com"
|
2015-09-10 18:34:36 +00:00
|
|
|
|
2016-07-22 17:02:26 +00:00
|
|
|
## Port of the OpenTSDB server
|
2015-10-15 21:53:29 +00:00
|
|
|
port = 4242
|
2015-09-14 10:28:10 +00:00
|
|
|
|
2016-07-20 16:51:47 +00:00
|
|
|
## Number of data points to send to OpenTSDB in Http requests.
|
2016-07-22 17:02:26 +00:00
|
|
|
## Not used with telnet API.
|
|
|
|
httpBatchSize = 50
|
2016-07-20 16:51:47 +00:00
|
|
|
|
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
|
|
|
`
|
2016-07-22 12:29:55 +00:00
|
|
|
|
2016-07-20 18:31:16 +00:00
|
|
|
type TagSet map[string]string
|
|
|
|
|
|
|
|
func (t TagSet) ToLineFormat() string {
|
2016-07-21 13:54:09 +00:00
|
|
|
tags := make([]string, len(t))
|
|
|
|
index := 0
|
2016-07-20 18:31:16 +00:00
|
|
|
for k, v := range t {
|
2016-07-21 13:54:09 +00:00
|
|
|
tags[index] = fmt.Sprintf("%s=%s", k, v)
|
|
|
|
index++
|
2016-07-20 18:31:16 +00:00
|
|
|
}
|
2016-07-21 13:54:09 +00:00
|
|
|
sort.Strings(tags)
|
|
|
|
return strings.Join(tags, " ")
|
2016-07-20 18:31:16 +00:00
|
|
|
}
|
2015-09-10 18:34:36 +00:00
|
|
|
|
|
|
|
func (o *OpenTSDB) Connect() error {
|
|
|
|
// Test Connection to OpenTSDB Server
|
2016-07-22 17:02:26 +00:00
|
|
|
u, err := url.Parse(o.Host)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error in parsing host url: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
uri := fmt.Sprintf("%s:%d", u.Host, o.Port)
|
2015-09-10 18:34:36 +00:00
|
|
|
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
|
|
|
|
2016-07-22 17:02:26 +00:00
|
|
|
u, err := url.Parse(o.Host)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error in parsing host url: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if u.Scheme == "" || u.Scheme == "tcp" {
|
|
|
|
return o.WriteTelnet(metrics, u)
|
|
|
|
} else if u.Scheme == "http" {
|
|
|
|
return o.WriteHttp(metrics, u)
|
2016-07-18 19:01:36 +00:00
|
|
|
} else {
|
2016-07-22 17:02:26 +00:00
|
|
|
return fmt.Errorf("Unknown scheme in host parameter.")
|
2016-07-18 19:01:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 17:02:26 +00:00
|
|
|
func (o *OpenTSDB) WriteHttp(metrics []telegraf.Metric, u *url.URL) error {
|
2016-07-20 16:51:47 +00:00
|
|
|
http := openTSDBHttp{
|
2016-07-22 17:02:26 +00:00
|
|
|
Host: u.Host,
|
2016-07-22 12:29:55 +00:00
|
|
|
Port: o.Port,
|
2016-07-22 17:02:26 +00:00
|
|
|
BatchSize: o.HttpBatchSize,
|
2016-07-22 12:29:55 +00:00
|
|
|
Debug: o.Debug,
|
2016-07-20 16:51:47 +00:00
|
|
|
}
|
2016-07-18 19:01:36 +00:00
|
|
|
|
|
|
|
for _, m := range metrics {
|
|
|
|
now := m.UnixNano() / 1000000000
|
2016-07-20 18:31:16 +00:00
|
|
|
tags := cleanTags(m.Tags())
|
|
|
|
|
2016-07-18 19:01:36 +00:00
|
|
|
for fieldName, value := range m.Fields() {
|
|
|
|
metricValue, buildError := buildValue(value)
|
|
|
|
if buildError != nil {
|
|
|
|
fmt.Printf("OpenTSDB: %s\n", buildError.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-07-22 12:29:55 +00:00
|
|
|
metric := &HttpMetric{
|
|
|
|
Metric: sanitizedChars.Replace(fmt.Sprintf("%s%s_%s",
|
|
|
|
o.Prefix, m.Name(), fieldName)),
|
|
|
|
Tags: tags,
|
2016-07-20 18:31:16 +00:00
|
|
|
Timestamp: now,
|
2016-07-22 12:29:55 +00:00
|
|
|
Value: metricValue,
|
|
|
|
}
|
2016-07-18 19:01:36 +00:00
|
|
|
|
2016-07-22 12:29:55 +00:00
|
|
|
if err := http.sendDataPoint(metric); err != nil {
|
2016-07-20 16:51:47 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-07-18 19:01:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 12:29:55 +00:00
|
|
|
if err := http.flush(); err != nil {
|
2016-07-20 16:51:47 +00:00
|
|
|
return err
|
2016-07-18 19:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-22 17:02:26 +00:00
|
|
|
func (o *OpenTSDB) WriteTelnet(metrics []telegraf.Metric, u *url.URL) error {
|
2015-09-10 18:34:36 +00:00
|
|
|
// Send Data with telnet / socket communication
|
2016-07-22 17:02:26 +00:00
|
|
|
uri := fmt.Sprintf("%s:%d", u.Host, o.Port)
|
2015-09-10 18:34:36 +00:00
|
|
|
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 {
|
2016-07-20 18:31:16 +00:00
|
|
|
now := m.UnixNano() / 1000000000
|
|
|
|
tags := cleanTags(m.Tags()).ToLineFormat()
|
|
|
|
|
|
|
|
for fieldName, value := range m.Fields() {
|
|
|
|
metricValue, buildError := buildValue(value)
|
|
|
|
if buildError != nil {
|
|
|
|
fmt.Printf("OpenTSDB: %s\n", buildError.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-12-19 21:30:37 +00:00
|
|
|
messageLine := fmt.Sprintf("put %s %v %s %s\n",
|
2016-07-22 12:29:55 +00:00
|
|
|
sanitizedChars.Replace(fmt.Sprintf("%s%s_%s", o.Prefix, m.Name(), fieldName)),
|
2016-07-20 18:31:16 +00:00
|
|
|
now, metricValue, tags)
|
|
|
|
|
2015-12-19 21:30:37 +00:00
|
|
|
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-07-20 18:31:16 +00:00
|
|
|
func cleanTags(tags map[string]string) TagSet {
|
|
|
|
tagSet := make(map[string]string, len(tags))
|
|
|
|
for k, v := range tags {
|
|
|
|
tagSet[sanitizedChars.Replace(k)] = sanitizedChars.Replace(v)
|
2015-12-19 21:30:37 +00:00
|
|
|
}
|
2016-07-20 18:31:16 +00:00
|
|
|
return tagSet
|
2015-12-19 21:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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{}
|
|
|
|
})
|
|
|
|
}
|