Remove hostname setting in the MQTT output.

This commit is contained in:
Shirou WAKAYAMA 2015-10-05 11:23:16 +09:00
parent 0cd7d6ae55
commit d9aa0f89a2
1 changed files with 5 additions and 21 deletions

View File

@ -6,7 +6,6 @@ import (
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"strings" "strings"
"sync" "sync"
@ -27,7 +26,6 @@ type MQTT struct {
Database string Database string
Timeout t.Duration Timeout t.Duration
TopicPrefix string TopicPrefix string
Hostname string
Client *paho.Client Client *paho.Client
Opts *paho.ClientOptions Opts *paho.ClientOptions
@ -42,18 +40,12 @@ var sampleConfig = `
# ex: prefix/host/web01.example.com/mem/available # ex: prefix/host/web01.example.com/mem/available
# topic_prefix = "prefix" # topic_prefix = "prefix"
# Set hostname used in the sending topic. if empty use os.Hostname().
# This is not inherit from agent config
# hostname = "mytelegraf"
# username and password to connect MQTT server. # username and password to connect MQTT server.
# username = "telegraf" # username = "telegraf"
# password = "metricsmetricsmetricsmetrics" # password = "metricsmetricsmetricsmetrics"
` `
func (m *MQTT) Connect() error { func (m *MQTT) Connect() error {
m.setHostname()
var err error var err error
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
@ -92,6 +84,10 @@ func (m *MQTT) Write(bp client.BatchPoints) error {
if len(bp.Points) == 0 { if len(bp.Points) == 0 {
return nil return nil
} }
hostname, ok := bp.Tags["host"]
if !ok {
hostname = ""
}
for _, p := range bp.Points { for _, p := range bp.Points {
var t []string var t []string
@ -102,7 +98,7 @@ func (m *MQTT) Write(bp client.BatchPoints) error {
if len(tm) < 2 { if len(tm) < 2 {
tm = []string{p.Measurement, "stat"} tm = []string{p.Measurement, "stat"}
} }
t = append(t, "host", m.Hostname, tm[0], tm[1]) t = append(t, "host", hostname, tm[0], tm[1])
topic := strings.Join(t, "/") topic := strings.Join(t, "/")
var value string var value string
@ -208,18 +204,6 @@ func getCertPool(pemPath string) (*x509.CertPool, error) {
return certs, nil return certs, nil
} }
// setHostname overwrites default hostname.
// TODO: should use agent.Hostname
func (m *MQTT) setHostname() {
if m.Hostname == "" {
hostname, err := os.Hostname()
if err != nil {
hostname = ""
}
m.Hostname = hostname
}
}
func init() { func init() {
outputs.Add("mqtt", func() outputs.Output { outputs.Add("mqtt", func() outputs.Output {
return &MQTT{} return &MQTT{}