-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Serializer plugins, and 'file' output plugin
- Loading branch information
Showing
20 changed files
with
629 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# file Output Plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package file | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/plugins/outputs" | ||
"github.com/influxdata/telegraf/plugins/serializers" | ||
) | ||
|
||
type File struct { | ||
Files []string | ||
|
||
writer io.Writer | ||
closers []io.Closer | ||
|
||
serializer serializers.Serializer | ||
} | ||
|
||
var sampleConfig = ` | ||
### Files to write to, "stdout" is a specially handled file. | ||
files = ["stdout", "/tmp/metrics.out"] | ||
### Data format to output. This can be "influx" or "graphite" | ||
### Each data format has it's own unique set of configuration options, read | ||
### more about them here: | ||
### https://github.com/influxdata/telegraf/blob/master/DATA_FORMATS_OUTPUT.md | ||
data_format = "influx" | ||
` | ||
|
||
func (f *File) SetSerializer(serializer serializers.Serializer) { | ||
f.serializer = serializer | ||
} | ||
|
||
func (f *File) Connect() error { | ||
writers := []io.Writer{} | ||
for _, file := range f.Files { | ||
if file == "stdout" { | ||
writers = append(writers, os.Stdout) | ||
f.closers = append(f.closers, os.Stdout) | ||
} else { | ||
var of *os.File | ||
var err error | ||
if _, err := os.Stat(file); os.IsNotExist(err) { | ||
of, err = os.Create(file) | ||
} else { | ||
of, err = os.OpenFile(file, os.O_APPEND|os.O_WRONLY, os.ModeAppend) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
writers = append(writers, of) | ||
f.closers = append(f.closers, of) | ||
} | ||
} | ||
f.writer = io.MultiWriter(writers...) | ||
return nil | ||
} | ||
|
||
func (f *File) Close() error { | ||
var errS string | ||
for _, c := range f.closers { | ||
if err := c.Close(); err != nil { | ||
errS += err.Error() + "\n" | ||
} | ||
} | ||
if errS != "" { | ||
return fmt.Errorf(errS) | ||
} | ||
return nil | ||
} | ||
|
||
func (f *File) SampleConfig() string { | ||
return sampleConfig | ||
} | ||
|
||
func (f *File) Description() string { | ||
return "Send telegraf metrics to file(s)" | ||
} | ||
|
||
func (f *File) Write(metrics []telegraf.Metric) error { | ||
if len(metrics) == 0 { | ||
return nil | ||
} | ||
|
||
for _, metric := range metrics { | ||
values, err := f.serializer.Serialize(metric) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, value := range values { | ||
_, err = f.writer.Write([]byte(value + "\n")) | ||
if err != nil { | ||
return fmt.Errorf("FAILED to write message: %s, %s", value, err) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
outputs.Add("file", func() telegraf.Output { | ||
return &File{} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.