2016-02-10 22:50:07 +00:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2016-12-23 15:18:27 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins"
|
2016-02-10 22:50:07 +00:00
|
|
|
"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 = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## Files to write to, "stdout" is a specially handled file.
|
2016-02-10 22:50:07 +00:00
|
|
|
files = ["stdout", "/tmp/metrics.out"]
|
|
|
|
|
2016-03-31 23:50:24 +00:00
|
|
|
## Data format to output.
|
2016-02-18 21:26:51 +00:00
|
|
|
## Each data format has it's own unique set of configuration options, read
|
|
|
|
## more about them here:
|
|
|
|
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
|
2016-02-10 22:50:07 +00:00
|
|
|
data_format = "influx"
|
|
|
|
`
|
|
|
|
|
|
|
|
func (f *File) SetSerializer(serializer serializers.Serializer) {
|
|
|
|
f.serializer = serializer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Connect() error {
|
|
|
|
writers := []io.Writer{}
|
2016-02-19 18:56:33 +00:00
|
|
|
|
|
|
|
if len(f.Files) == 0 {
|
|
|
|
f.Files = []string{"stdout"}
|
|
|
|
}
|
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
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)"
|
|
|
|
}
|
|
|
|
|
2016-12-23 15:18:27 +00:00
|
|
|
func (f *File) Write(metrics []plugins.Metric) error {
|
2016-02-10 22:50:07 +00:00
|
|
|
if len(metrics) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, metric := range metrics {
|
2016-12-02 11:36:22 +00:00
|
|
|
b, err := f.serializer.Serialize(metric)
|
2016-02-10 22:50:07 +00:00
|
|
|
if err != nil {
|
2016-12-02 11:36:22 +00:00
|
|
|
return fmt.Errorf("failed to serialize message: %s", err)
|
|
|
|
}
|
|
|
|
_, err = f.writer.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write message: %s, %s", metric.Serialize(), err)
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-12-23 15:18:27 +00:00
|
|
|
outputs.Add("file", func() plugins.Output {
|
2016-02-10 22:50:07 +00:00
|
|
|
return &File{}
|
|
|
|
})
|
|
|
|
}
|