MQTT Consumer Input plugin

This commit is contained in:
Cameron Sparr
2016-02-09 15:03:46 -07:00
parent 6c353e8b8f
commit 8d0f50a6fd
16 changed files with 554 additions and 44 deletions

View File

@@ -29,6 +29,10 @@ type GraphiteParser struct {
matcher *matcher
}
func (p *GraphiteParser) SetDefaultTags(tags map[string]string) {
p.DefaultTags = tags
}
func NewGraphiteParser(
separator string,
templates []string,
@@ -104,13 +108,14 @@ func (p *GraphiteParser) Parse(buf []byte) ([]telegraf.Metric, error) {
metrics := make([]telegraf.Metric, 0)
var errStr string
buffer := bytes.NewBuffer(buf)
reader := bufio.NewReader(buffer)
for {
// Read up to the next newline.
buf, err := reader.ReadBytes('\n')
if err == io.EOF {
return metrics, nil
break
}
if err != nil && err != io.EOF {
return metrics, err
@@ -118,10 +123,19 @@ func (p *GraphiteParser) Parse(buf []byte) ([]telegraf.Metric, error) {
// Trim the buffer, even though there should be no padding
line := strings.TrimSpace(string(buf))
if metric, err := p.ParseLine(line); err == nil {
metric, err := p.ParseLine(line)
if err == nil {
metrics = append(metrics, metric)
} else {
errStr += err.Error() + "\n"
}
}
if errStr != "" {
return metrics, fmt.Errorf(errStr)
}
return metrics, nil
}
// Parse performs Graphite parsing of a single line.

View File

@@ -55,3 +55,7 @@ func (p *InfluxParser) ParseLine(line string) (telegraf.Metric, error) {
return metrics[0], nil
}
func (p *InfluxParser) SetDefaultTags(tags map[string]string) {
p.DefaultTags = tags
}

View File

@@ -67,6 +67,10 @@ func (p *JSONParser) ParseLine(line string) (telegraf.Metric, error) {
return metrics[0], nil
}
func (p *JSONParser) SetDefaultTags(tags map[string]string) {
p.DefaultTags = tags
}
type JSONFlattener struct {
Fields map[string]interface{}
}

View File

@@ -28,6 +28,11 @@ type Parser interface {
// ie, "cpu.usage.idle 90"
// and parses it into a telegraf metric.
ParseLine(line string) (telegraf.Metric, error)
// SetDefaultTags tells the parser to add all of the given tags
// to each parsed metric.
// NOTE: do _not_ modify the map after you've passed it here!!
SetDefaultTags(tags map[string]string)
}
// Config is a struct that covers the data types needed for all parser types,