Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MQTT output. #241

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove hostname setting in the MQTT output.
  • Loading branch information
shirou committed Oct 5, 2015
commit d9aa0f89a2c085b21d3c1e9004f974f66926e244
26 changes: 5 additions & 21 deletions outputs/mqtt/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"crypto/x509"
"fmt"
"io/ioutil"
"os"
"strings"
"sync"

Expand All @@ -27,7 +26,6 @@ type MQTT struct {
Database string
Timeout t.Duration
TopicPrefix string
Hostname string

Client *paho.Client
Opts *paho.ClientOptions
Expand All @@ -42,18 +40,12 @@ var sampleConfig = `
# ex: prefix/host/web01.example.com/mem/available
# 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 = "telegraf"
# password = "metricsmetricsmetricsmetrics"
`

func (m *MQTT) Connect() error {
m.setHostname()

var err error
m.Lock()
defer m.Unlock()
Expand Down Expand Up @@ -92,6 +84,10 @@ func (m *MQTT) Write(bp client.BatchPoints) error {
if len(bp.Points) == 0 {
return nil
}
hostname, ok := bp.Tags["host"]
if !ok {
hostname = ""
}

for _, p := range bp.Points {
var t []string
Expand All @@ -102,7 +98,7 @@ func (m *MQTT) Write(bp client.BatchPoints) error {
if len(tm) < 2 {
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, "/")

var value string
Expand Down Expand Up @@ -208,18 +204,6 @@ func getCertPool(pemPath string) (*x509.CertPool, error) {
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() {
outputs.Add("mqtt", func() outputs.Output {
return &MQTT{}
Expand Down