2016-02-10 22:50:07 +00:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-06-04 00:34:48 +00:00
|
|
|
"log"
|
2016-02-10 22:50:07 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2019-06-04 00:38:21 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2019-06-02 00:11:47 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/rotate"
|
2016-02-10 22:50:07 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
|
|
|
"github.com/influxdata/telegraf/plugins/serializers"
|
|
|
|
)
|
|
|
|
|
|
|
|
type File struct {
|
2019-06-04 00:38:21 +00:00
|
|
|
Files []string `toml:"files"`
|
|
|
|
RotationInterval internal.Duration `toml:"rotation_interval"`
|
|
|
|
RotationMaxSize internal.Size `toml:"rotation_max_size"`
|
|
|
|
RotationMaxArchives int `toml:"rotation_max_archives"`
|
2016-02-10 22:50:07 +00:00
|
|
|
|
2019-06-04 00:38:21 +00:00
|
|
|
writer io.Writer
|
|
|
|
closers []io.Closer
|
2016-02-10 22:50:07 +00:00
|
|
|
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"]
|
|
|
|
|
2019-06-04 00:38:21 +00:00
|
|
|
## The file will be rotated after the time interval specified. When set
|
|
|
|
## to 0 no time based rotation is performed.
|
|
|
|
# rotation_interval = "0d"
|
|
|
|
|
|
|
|
## The logfile will be rotated when it becomes larger than the specified
|
|
|
|
## size. When set to 0 no size based rotation is performed.
|
|
|
|
# rotation_max_size = "0MB"
|
|
|
|
|
|
|
|
## Maximum number of rotated archives to keep, any older logs are deleted.
|
|
|
|
## If set to -1, no archives are removed.
|
|
|
|
# rotation_max_archives = 5
|
2019-06-02 00:11:47 +00:00
|
|
|
|
2016-03-31 23:50:24 +00:00
|
|
|
## Data format to output.
|
2017-04-27 21:59:18 +00:00
|
|
|
## Each data format has its own unique set of configuration options, read
|
2016-02-18 21:26:51 +00:00
|
|
|
## 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 {
|
2019-06-02 00:11:47 +00:00
|
|
|
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" {
|
2019-06-02 00:11:47 +00:00
|
|
|
writers = append(writers, os.Stdout)
|
2016-02-10 22:50:07 +00:00
|
|
|
} else {
|
2019-06-04 00:38:21 +00:00
|
|
|
of, err := rotate.NewFileWriter(
|
|
|
|
file, f.RotationInterval.Duration, f.RotationMaxSize.Size, f.RotationMaxArchives)
|
2016-02-10 22:50:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-04 00:38:21 +00:00
|
|
|
|
2019-06-02 00:11:47 +00:00
|
|
|
writers = append(writers, of)
|
2016-02-10 22:50:07 +00:00
|
|
|
f.closers = append(f.closers, of)
|
|
|
|
}
|
|
|
|
}
|
2019-06-02 00:11:47 +00:00
|
|
|
f.writer = io.MultiWriter(writers...)
|
2016-02-10 22:50:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Close() error {
|
2019-03-22 20:59:30 +00:00
|
|
|
var err error
|
2016-02-10 22:50:07 +00:00
|
|
|
for _, c := range f.closers {
|
2019-03-22 20:59:30 +00:00
|
|
|
errClose := c.Close()
|
|
|
|
if errClose != nil {
|
|
|
|
err = errClose
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-22 20:59:30 +00:00
|
|
|
return err
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-04-23 22:08:04 +00:00
|
|
|
var writeErr error = nil
|
2019-06-02 00:11:47 +00:00
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
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 {
|
2019-06-04 00:34:48 +00:00
|
|
|
log.Printf("D! [outputs.file] Could not serialize metric: %v", err)
|
2016-12-02 11:36:22 +00:00
|
|
|
}
|
2018-04-23 22:08:04 +00:00
|
|
|
|
2019-06-02 00:11:47 +00:00
|
|
|
_, err = f.writer.Write(b)
|
|
|
|
if err != nil {
|
2019-06-04 00:38:21 +00:00
|
|
|
writeErr = fmt.Errorf("E! [outputs.file] failed to write message: %v", err)
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-02 00:11:47 +00:00
|
|
|
|
2018-04-23 22:08:04 +00:00
|
|
|
return writeErr
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
outputs.Add("file", func() telegraf.Output {
|
|
|
|
return &File{}
|
|
|
|
})
|
|
|
|
}
|