2022-05-24 21:49:47 +08:00
|
|
|
//go:generate ../../../tools/readme_config_includer/generator
|
2018-07-14 14:22:59 +08:00
|
|
|
package file
|
|
|
|
|
|
|
|
|
|
import (
|
2022-05-24 21:49:47 +08:00
|
|
|
_ "embed"
|
2018-07-14 14:22:59 +08:00
|
|
|
"fmt"
|
2021-09-29 05:16:32 +08:00
|
|
|
"io"
|
2020-07-08 03:43:32 +08:00
|
|
|
"os"
|
2019-11-14 05:00:41 +08:00
|
|
|
"path/filepath"
|
2018-07-14 14:22:59 +08:00
|
|
|
|
2020-07-08 03:43:32 +08:00
|
|
|
"github.com/dimchansky/utfbom"
|
2022-05-24 21:49:47 +08:00
|
|
|
|
2018-07-14 14:22:59 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
"github.com/influxdata/telegraf/internal/globpath"
|
2020-07-08 03:43:32 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/common/encoding"
|
2018-07-14 14:22:59 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
|
)
|
|
|
|
|
|
2022-05-24 21:49:47 +08:00
|
|
|
// DO NOT REMOVE THE NEXT TWO LINES! This is required to embedd the sampleConfig data.
|
|
|
|
|
//go:embed sample.conf
|
|
|
|
|
var sampleConfig string
|
|
|
|
|
|
2018-07-14 14:22:59 +08:00
|
|
|
type File struct {
|
2020-07-08 03:43:32 +08:00
|
|
|
Files []string `toml:"files"`
|
|
|
|
|
FileTag string `toml:"file_tag"`
|
|
|
|
|
CharacterEncoding string `toml:"character_encoding"`
|
2018-07-14 14:22:59 +08:00
|
|
|
|
2022-02-04 00:15:38 +08:00
|
|
|
parserFunc telegraf.ParserFunc
|
|
|
|
|
filenames []string
|
|
|
|
|
decoder *encoding.Decoder
|
2018-07-14 14:22:59 +08:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 21:49:47 +08:00
|
|
|
func (*File) SampleConfig() string {
|
|
|
|
|
return sampleConfig
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 03:43:32 +08:00
|
|
|
func (f *File) Init() error {
|
|
|
|
|
var err error
|
|
|
|
|
f.decoder, err = encoding.NewDecoder(f.CharacterEncoding)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-14 14:22:59 +08:00
|
|
|
func (f *File) Gather(acc telegraf.Accumulator) error {
|
|
|
|
|
err := f.refreshFilePaths()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for _, k := range f.filenames {
|
|
|
|
|
metrics, err := f.readMetric(k)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, m := range metrics {
|
2019-11-14 05:00:41 +08:00
|
|
|
if f.FileTag != "" {
|
|
|
|
|
m.AddTag(f.FileTag, filepath.Base(k))
|
|
|
|
|
}
|
2020-07-08 03:43:32 +08:00
|
|
|
acc.AddMetric(m)
|
2018-07-14 14:22:59 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 00:15:38 +08:00
|
|
|
func (f *File) SetParserFunc(fn telegraf.ParserFunc) {
|
|
|
|
|
f.parserFunc = fn
|
2018-07-14 14:22:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *File) refreshFilePaths() error {
|
|
|
|
|
var allFiles []string
|
|
|
|
|
for _, file := range f.Files {
|
|
|
|
|
g, err := globpath.Compile(file)
|
|
|
|
|
if err != nil {
|
2022-05-17 06:21:46 +08:00
|
|
|
return fmt.Errorf("could not compile glob %q: %w", file, err)
|
2018-07-14 14:22:59 +08:00
|
|
|
}
|
|
|
|
|
files := g.Match()
|
|
|
|
|
if len(files) <= 0 {
|
2022-05-17 06:21:46 +08:00
|
|
|
return fmt.Errorf("could not find file(s): %v", file)
|
2018-07-14 14:22:59 +08:00
|
|
|
}
|
2018-12-19 06:23:25 +08:00
|
|
|
allFiles = append(allFiles, files...)
|
2018-07-14 14:22:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f.filenames = allFiles
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *File) readMetric(filename string) ([]telegraf.Metric, error) {
|
2020-07-08 03:43:32 +08:00
|
|
|
file, err := os.Open(filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
r, _ := utfbom.Skip(f.decoder.Reader(file))
|
2021-09-29 05:16:32 +08:00
|
|
|
fileContents, err := io.ReadAll(r)
|
2018-07-14 14:22:59 +08:00
|
|
|
if err != nil {
|
2022-05-17 06:21:46 +08:00
|
|
|
return nil, fmt.Errorf("could not read %q: %w", filename, err)
|
2022-02-04 00:15:38 +08:00
|
|
|
}
|
|
|
|
|
parser, err := f.parserFunc()
|
|
|
|
|
if err != nil {
|
2022-05-17 06:21:46 +08:00
|
|
|
return nil, fmt.Errorf("could not instantiate parser: %w", err)
|
2018-07-14 14:22:59 +08:00
|
|
|
}
|
2022-05-17 06:21:46 +08:00
|
|
|
metrics, err := parser.Parse(fileContents)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return metrics, fmt.Errorf("could not parse %q: %w", filename, err)
|
|
|
|
|
}
|
|
|
|
|
return metrics, err
|
2018-07-14 14:22:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
inputs.Add("file", func() telegraf.Input {
|
|
|
|
|
return &File{}
|
|
|
|
|
})
|
|
|
|
|
}
|