2015-09-10 18:34:36 +00:00
|
|
|
package opentsdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-09-21 14:29:22 +00:00
|
|
|
"log"
|
2015-09-10 18:34:36 +00:00
|
|
|
"net"
|
2016-07-18 19:01:36 +00:00
|
|
|
"net/url"
|
2017-09-14 00:30:52 +00:00
|
|
|
"regexp"
|
2015-09-10 18:34:36 +00:00
|
|
|
"sort"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2017-09-14 00:30:52 +00:00
|
|
|
var (
|
|
|
|
allowedChars = regexp.MustCompile(`[^a-zA-Z0-9-_./\p{L}]`)
|
|
|
|
hypenChars = strings.NewReplacer(
|
|
|
|
"@", "-",
|
|
|
|
"*", "-",
|
|
|
|
`%`, "-",
|
|
|
|
"#", "-",
|
|
|
|
"$", "-")
|
2017-09-27 18:36:41 +00:00
|
|
|
defaultSeperator = "_"
|
2017-09-14 00:30:52 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
HttpBatchSize int
|
|
|
|
|
2015-09-14 10:28:10 +00:00
|
|
|
Debug bool
|
2017-09-27 18:29:40 +00:00
|
|
|
|
|
|
|
Separator string
|
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
|
|
|
|
## 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-18 19:01:36 +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-18 19:01:36 +00:00
|
|
|
## Number of data points to send to OpenTSDB in Http requests.
|
|
|
|
## Not used with telnet API.
|
|
|
|
httpBatchSize = 50
|
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## Debug true - Prints OpenTSDB communication
|
2015-10-15 21:53:29 +00:00
|
|
|
debug = false
|
2017-09-27 18:29:40 +00:00
|
|
|
|
2017-09-27 18:36:41 +00:00
|
|
|
## Separator separates measurement name from field
|
|
|
|
separator = "_"
|
2015-09-10 18:34:36 +00:00
|
|
|
`
|
|
|
|
|
2016-07-18 19:01:36 +00:00
|
|
|
func ToLineFormat(tags map[string]string) string {
|
|
|
|
tagsArray := make([]string, len(tags))
|
|
|
|
index := 0
|
|
|
|
for k, v := range tags {
|
|
|
|
tagsArray[index] = fmt.Sprintf("%s=%s", k, v)
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
sort.Strings(tagsArray)
|
|
|
|
return strings.Join(tagsArray, " ")
|
2015-09-10 18:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OpenTSDB) Connect() error {
|
2017-01-23 19:19:51 +00:00
|
|
|
if !strings.HasPrefix(o.Host, "http") && !strings.HasPrefix(o.Host, "tcp") {
|
|
|
|
o.Host = "tcp://" + o.Host
|
|
|
|
}
|
2015-09-10 18:34:36 +00:00
|
|
|
// Test Connection to OpenTSDB Server
|
2016-07-18 19:01:36 +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 {
|
2017-01-23 19:19:51 +00:00
|
|
|
return fmt.Errorf("OpenTSDB TCP address cannot be resolved: %s", err)
|
2015-09-10 18:34:36 +00:00
|
|
|
}
|
|
|
|
connection, err := net.DialTCP("tcp", nil, tcpAddr)
|
|
|
|
if err != nil {
|
2017-01-23 19:19:51 +00:00
|
|
|
return fmt.Errorf("OpenTSDB Telnet connect fail: %s", err)
|
2015-09-10 18:34:36 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
2015-12-19 21:30:37 +00:00
|
|
|
|
2016-07-18 19:01:36 +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)
|
2016-12-13 14:15:51 +00:00
|
|
|
} else if u.Scheme == "http" || u.Scheme == "https" {
|
2016-07-18 19:01:36 +00:00
|
|
|
return o.WriteHttp(metrics, u)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Unknown scheme in host parameter.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OpenTSDB) WriteHttp(metrics []telegraf.Metric, u *url.URL) error {
|
|
|
|
http := openTSDBHttp{
|
|
|
|
Host: u.Host,
|
|
|
|
Port: o.Port,
|
2016-12-13 14:15:51 +00:00
|
|
|
Scheme: u.Scheme,
|
|
|
|
User: u.User,
|
2016-07-18 19:01:36 +00:00
|
|
|
BatchSize: o.HttpBatchSize,
|
|
|
|
Debug: o.Debug,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range metrics {
|
|
|
|
now := m.UnixNano() / 1000000000
|
|
|
|
tags := cleanTags(m.Tags())
|
|
|
|
|
|
|
|
for fieldName, value := range m.Fields() {
|
2016-09-20 18:26:08 +00:00
|
|
|
switch value.(type) {
|
|
|
|
case int64:
|
|
|
|
case uint64:
|
|
|
|
case float64:
|
|
|
|
default:
|
2016-09-21 14:29:22 +00:00
|
|
|
log.Printf("D! OpenTSDB does not support metric value: [%s] of type [%T].\n", value, value)
|
2016-07-18 19:01:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
metric := &HttpMetric{
|
2017-09-27 18:29:40 +00:00
|
|
|
Metric: sanitize(fmt.Sprintf("%s%s%s%s",
|
|
|
|
o.Prefix, m.Name(), o.Separator, fieldName)),
|
2016-07-18 19:01:36 +00:00
|
|
|
Tags: tags,
|
|
|
|
Timestamp: now,
|
2016-09-20 18:26:08 +00:00
|
|
|
Value: value,
|
2016-07-18 19:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := http.sendDataPoint(metric); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := http.flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-18 19:01:36 +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-18 19:01:36 +00:00
|
|
|
now := m.UnixNano() / 1000000000
|
|
|
|
tags := ToLineFormat(cleanTags(m.Tags()))
|
|
|
|
|
|
|
|
for fieldName, value := range m.Fields() {
|
2017-01-13 11:35:36 +00:00
|
|
|
switch value.(type) {
|
|
|
|
case int64:
|
|
|
|
case uint64:
|
|
|
|
case float64:
|
|
|
|
default:
|
|
|
|
log.Printf("D! OpenTSDB does not support metric value: [%s] of type [%T].\n", value, value)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-07-18 19:01:36 +00:00
|
|
|
metricValue, buildError := buildValue(value)
|
|
|
|
if buildError != nil {
|
2016-09-21 14:29:22 +00:00
|
|
|
log.Printf("E! OpenTSDB: %s\n", buildError.Error())
|
2016-07-18 19:01:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-12-19 21:30:37 +00:00
|
|
|
messageLine := fmt.Sprintf("put %s %v %s %s\n",
|
2017-09-27 18:29:40 +00:00
|
|
|
sanitize(fmt.Sprintf("%s%s%s%s", o.Prefix, m.Name(), o.Separator, fieldName)),
|
2016-07-18 19:01:36 +00:00
|
|
|
now, metricValue, tags)
|
|
|
|
|
2015-12-19 21:30:37 +00:00
|
|
|
_, 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-18 19:01:36 +00:00
|
|
|
func cleanTags(tags map[string]string) map[string]string {
|
|
|
|
tagSet := make(map[string]string, len(tags))
|
|
|
|
for k, v := range tags {
|
2017-09-14 00:30:52 +00:00
|
|
|
tagSet[sanitize(k)] = sanitize(v)
|
2015-12-19 21:30:37 +00:00
|
|
|
}
|
2016-07-18 19:01:36 +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
|
|
|
|
}
|
|
|
|
|
2017-09-14 00:30:52 +00:00
|
|
|
func sanitize(value string) string {
|
|
|
|
// Apply special hypenation rules to preserve backwards compatibility
|
|
|
|
value = hypenChars.Replace(value)
|
|
|
|
// Replace any remaining illegal chars
|
|
|
|
return allowedChars.ReplaceAllLiteralString(value, "_")
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:34:36 +00:00
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
outputs.Add("opentsdb", func() telegraf.Output {
|
2017-09-27 18:36:41 +00:00
|
|
|
return &OpenTSDB{
|
|
|
|
Separator: defaultSeperator,
|
|
|
|
}
|
2015-09-10 18:34:36 +00:00
|
|
|
})
|
|
|
|
}
|