1
0
Fork 0

Adding upstream version 1.34.4.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-24 07:26:29 +02:00
parent e393c3af3f
commit 4978089aab
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
4963 changed files with 677545 additions and 0 deletions

View file

@ -0,0 +1,53 @@
# File statistics Input Plugin
This plugin gathers metrics about file existence, size, and other file
statistics.
⭐ Telegraf v0.13.0
🏷️ system
💻 all
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
In addition to the plugin-specific configuration settings, plugins support
additional global and plugin configuration settings. These settings are used to
modify metrics, tags, and field or create aliases and configure ordering, etc.
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
## Configuration
```toml @sample.conf
# Read stats about given file(s)
[[inputs.filestat]]
## Files to gather stats about.
## These accept standard unix glob matching rules, but with the addition of
## ** as a "super asterisk". See https://github.com/gobwas/glob.
files = ["/etc/telegraf/telegraf.conf", "/var/log/**.log"]
## If true, read the entire file and calculate an md5 checksum.
md5 = false
```
## Metrics
### Measurements & Fields
- filestat
- exists (int, 0 | 1)
- size_bytes (int, bytes)
- modification_time (int, unix time nanoseconds)
- md5 (optional, string)
### Tags
- All measurements have the following tags:
- file (the path the to file, as specified in the config)
## Example Output
```text
filestat,file=/tmp/foo/bar,host=tyrion exists=0i 1507218518192154351
filestat,file=/Users/sparrc/ws/telegraf.conf,host=tyrion exists=1i,size=47894i,modification_time=1507152973123456789i 1507218518192154351
```

View file

@ -0,0 +1,141 @@
//go:generate ../../../tools/readme_config_includer/generator
package filestat
import (
"crypto/md5" //nolint:gosec // G501: Blocklisted import crypto/md5: weak cryptographic primitive - md5 hash is what is desired in this case
_ "embed"
"encoding/hex"
"io"
"os"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal/globpath"
"github.com/influxdata/telegraf/plugins/inputs"
)
//go:embed sample.conf
var sampleConfig string
type FileStat struct {
Md5 bool `toml:"md5"`
Files []string `toml:"files"`
Log telegraf.Logger `toml:"-"`
// maps full file paths to globmatch obj
globs map[string]*globpath.GlobPath
// files that were missing - we only log the first time it's not found.
missingFiles map[string]bool
// files that had an error in Stat - we only log the first error.
filesWithErrors map[string]bool
}
func (*FileStat) SampleConfig() string {
return sampleConfig
}
func (f *FileStat) Gather(acc telegraf.Accumulator) error {
var err error
for _, filepath := range f.Files {
// Get the compiled glob object for this filepath
g, ok := f.globs[filepath]
if !ok {
if g, err = globpath.Compile(filepath); err != nil {
acc.AddError(err)
continue
}
f.globs[filepath] = g
}
files := g.Match()
if len(files) == 0 {
acc.AddFields("filestat",
map[string]interface{}{
"exists": int64(0),
},
map[string]string{
"file": filepath,
})
continue
}
for _, fileName := range files {
tags := map[string]string{
"file": fileName,
}
fields := map[string]interface{}{
"exists": int64(1),
}
fileInfo, err := os.Stat(fileName)
if os.IsNotExist(err) {
fields["exists"] = int64(0)
acc.AddFields("filestat", fields, tags)
if !f.missingFiles[fileName] {
f.Log.Warnf("File %q not found", fileName)
f.missingFiles[fileName] = true
}
continue
}
f.missingFiles[fileName] = false
if fileInfo == nil {
if !f.filesWithErrors[fileName] {
f.filesWithErrors[fileName] = true
f.Log.Errorf("Unable to get info for file %q: %v",
fileName, err)
}
} else {
f.filesWithErrors[fileName] = false
fields["size_bytes"] = fileInfo.Size()
fields["modification_time"] = fileInfo.ModTime().UnixNano()
}
if f.Md5 {
md5Hash, err := getMd5(fileName)
if err != nil {
acc.AddError(err)
} else {
fields["md5_sum"] = md5Hash
}
}
acc.AddFields("filestat", fields, tags)
}
}
return nil
}
// Read given file and calculate a md5 hash.
func getMd5(file string) (string, error) {
of, err := os.Open(file)
if err != nil {
return "", err
}
defer of.Close()
//nolint:gosec // G401: Use of weak cryptographic primitive - md5 hash is what is desired in this case
hash := md5.New()
_, err = io.Copy(hash, of)
if err != nil {
// fatal error
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
func newFileStat() *FileStat {
return &FileStat{
globs: make(map[string]*globpath.GlobPath),
missingFiles: make(map[string]bool),
filesWithErrors: make(map[string]bool),
}
}
func init() {
inputs.Add("filestat", func() telegraf.Input {
return newFileStat()
})
}

View file

@ -0,0 +1,211 @@
//go:build !windows
// TODO: Windows - should be enabled for Windows when super asterisk is fixed on Windows
// https://github.com/influxdata/telegraf/issues/6248
package filestat
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf/testutil"
)
var testdataDir = getTestdataDir()
func TestGatherNoMd5(t *testing.T) {
fs := newFileStat()
fs.Log = testutil.Logger{}
fs.Files = []string{
filepath.Join(testdataDir, "log1.log"),
filepath.Join(testdataDir, "log2.log"),
filepath.Join(testdataDir, "non_existent_file"),
}
acc := testutil.Accumulator{}
require.NoError(t, acc.GatherError(fs.Gather))
tags1 := map[string]string{
"file": filepath.Join(testdataDir, "log1.log"),
}
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))
tags2 := map[string]string{
"file": filepath.Join(testdataDir, "log2.log"),
}
require.True(t, acc.HasPoint("filestat", tags2, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags2, "exists", int64(1)))
tags3 := map[string]string{
"file": filepath.Join(testdataDir, "non_existent_file"),
}
require.True(t, acc.HasPoint("filestat", tags3, "exists", int64(0)))
}
func TestGatherExplicitFiles(t *testing.T) {
fs := newFileStat()
fs.Log = testutil.Logger{}
fs.Md5 = true
fs.Files = []string{
filepath.Join(testdataDir, "log1.log"),
filepath.Join(testdataDir, "log2.log"),
filepath.Join(testdataDir, "non_existent_file"),
}
acc := testutil.Accumulator{}
require.NoError(t, acc.GatherError(fs.Gather))
tags1 := map[string]string{
"file": filepath.Join(testdataDir, "log1.log"),
}
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags1, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
tags2 := map[string]string{
"file": filepath.Join(testdataDir, "log2.log"),
}
require.True(t, acc.HasPoint("filestat", tags2, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags2, "exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags2, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
tags3 := map[string]string{
"file": filepath.Join(testdataDir, "non_existent_file"),
}
require.True(t, acc.HasPoint("filestat", tags3, "exists", int64(0)))
}
func TestNonExistentFile(t *testing.T) {
fs := newFileStat()
fs.Log = testutil.Logger{}
fs.Md5 = true
fs.Files = []string{
"/non/existant/file",
}
acc := testutil.Accumulator{}
require.NoError(t, acc.GatherError(fs.Gather))
acc.AssertContainsFields(t, "filestat", map[string]interface{}{"exists": int64(0)})
require.False(t, acc.HasField("filestat", "error"))
require.False(t, acc.HasField("filestat", "md5_sum"))
require.False(t, acc.HasField("filestat", "size_bytes"))
require.False(t, acc.HasField("filestat", "modification_time"))
}
func TestGatherGlob(t *testing.T) {
fs := newFileStat()
fs.Log = testutil.Logger{}
fs.Md5 = true
fs.Files = []string{
filepath.Join(testdataDir, "*.log"),
}
acc := testutil.Accumulator{}
require.NoError(t, acc.GatherError(fs.Gather))
tags1 := map[string]string{
"file": filepath.Join(testdataDir, "log1.log"),
}
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags1, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
tags2 := map[string]string{
"file": filepath.Join(testdataDir, "log2.log"),
}
require.True(t, acc.HasPoint("filestat", tags2, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags2, "exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags2, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
}
func TestGatherSuperAsterisk(t *testing.T) {
fs := newFileStat()
fs.Log = testutil.Logger{}
fs.Md5 = true
fs.Files = []string{
filepath.Join(testdataDir, "**"),
}
acc := testutil.Accumulator{}
require.NoError(t, acc.GatherError(fs.Gather))
tags1 := map[string]string{
"file": filepath.Join(testdataDir, "log1.log"),
}
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags1, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
tags2 := map[string]string{
"file": filepath.Join(testdataDir, "log2.log"),
}
require.True(t, acc.HasPoint("filestat", tags2, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags2, "exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags2, "md5_sum", "d41d8cd98f00b204e9800998ecf8427e"))
tags3 := map[string]string{
"file": filepath.Join(testdataDir, "test.conf"),
}
require.True(t, acc.HasPoint("filestat", tags3, "size_bytes", int64(104)))
require.True(t, acc.HasPoint("filestat", tags3, "exists", int64(1)))
require.True(t, acc.HasPoint("filestat", tags3, "md5_sum", "5a7e9b77fa25e7bb411dbd17cf403c1f"))
}
func TestModificationTime(t *testing.T) {
fs := newFileStat()
fs.Log = testutil.Logger{}
fs.Files = []string{
filepath.Join(testdataDir, "log1.log"),
}
acc := testutil.Accumulator{}
require.NoError(t, acc.GatherError(fs.Gather))
tags1 := map[string]string{
"file": filepath.Join(testdataDir, "log1.log"),
}
require.True(t, acc.HasPoint("filestat", tags1, "size_bytes", int64(0)))
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(1)))
require.True(t, acc.HasInt64Field("filestat", "modification_time"))
}
func TestNoModificationTime(t *testing.T) {
fs := newFileStat()
fs.Log = testutil.Logger{}
fs.Files = []string{
filepath.Join(testdataDir, "non_existent_file"),
}
acc := testutil.Accumulator{}
require.NoError(t, acc.GatherError(fs.Gather))
tags1 := map[string]string{
"file": filepath.Join(testdataDir, "non_existent_file"),
}
require.True(t, acc.HasPoint("filestat", tags1, "exists", int64(0)))
require.False(t, acc.HasInt64Field("filestat", "modification_time"))
}
func TestGetMd5(t *testing.T) {
md5, err := getMd5(filepath.Join(testdataDir, "test.conf"))
require.NoError(t, err)
require.Equal(t, "5a7e9b77fa25e7bb411dbd17cf403c1f", md5)
_, err = getMd5("/tmp/foo/bar/fooooo")
require.Error(t, err)
}
func getTestdataDir() string {
dir, err := os.Getwd()
if err != nil {
// if we cannot even establish the test directory, further progress is meaningless
panic(err)
}
return filepath.Join(dir, "testdata")
}

View file

@ -0,0 +1,9 @@
# Read stats about given file(s)
[[inputs.filestat]]
## Files to gather stats about.
## These accept standard unix glob matching rules, but with the addition of
## ** as a "super asterisk". See https://github.com/gobwas/glob.
files = ["/etc/telegraf/telegraf.conf", "/var/log/**.log"]
## If true, read the entire file and calculate an md5 checksum.
md5 = false

View file

View file

View file

@ -0,0 +1,5 @@
# this is a fake testing config file
# for testing the filestat plugin
option1 = "foo"
option2 = "bar"