Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
96
plugins/inputs/multifile/README.md
Normal file
96
plugins/inputs/multifile/README.md
Normal file
|
@ -0,0 +1,96 @@
|
|||
# Multifile Input Plugin
|
||||
|
||||
This plugin reads the combined data from multiple files into a single metric,
|
||||
creating one field or tag per file. This is often useful creating custom
|
||||
metrics from the `/sys` or `/proc` filesystems.
|
||||
|
||||
> [!NOTE]
|
||||
> To parse metrics from a single file you should use the [file][file_plugin]
|
||||
> input plugin instead.
|
||||
|
||||
⭐ Telegraf v1.10.0
|
||||
🏷️ system
|
||||
💻 all
|
||||
|
||||
[file_plugin]: /plugins/inputs/file/README.md
|
||||
|
||||
## 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
|
||||
# Aggregates the contents of multiple files into a single point
|
||||
[[inputs.multifile]]
|
||||
## Base directory where telegraf will look for files.
|
||||
## Omit this option to use absolute paths.
|
||||
base_dir = "/sys/bus/i2c/devices/1-0076/iio:device0"
|
||||
|
||||
## If true discard all data when a single file can't be read.
|
||||
## Else, Telegraf omits the field generated from this file.
|
||||
# fail_early = true
|
||||
|
||||
## Files to parse each interval.
|
||||
[[inputs.multifile.file]]
|
||||
file = "in_pressure_input"
|
||||
dest = "pressure"
|
||||
conversion = "float"
|
||||
[[inputs.multifile.file]]
|
||||
file = "in_temp_input"
|
||||
dest = "temperature"
|
||||
conversion = "float(3)"
|
||||
[[inputs.multifile.file]]
|
||||
file = "in_humidityrelative_input"
|
||||
dest = "humidityrelative"
|
||||
conversion = "float(3)"
|
||||
```
|
||||
|
||||
## Metrics
|
||||
|
||||
Each file table can contain the following options:
|
||||
|
||||
* `file`:
|
||||
Path of the file to be parsed, relative to the `base_dir`.
|
||||
* `dest`:
|
||||
Name of the field/tag key, defaults to `$(basename file)`.
|
||||
* `conversion`:
|
||||
Data format used to parse the file contents:
|
||||
* `float(X)`: Converts the input value into a float and divides by the Xth
|
||||
power of 10. Effectively just moves the decimal left X places. For example
|
||||
a value of `123` with `float(2)` will result in `1.23`.
|
||||
* `float`: Converts the value into a float with no adjustment.
|
||||
Same as `float(0)`.
|
||||
* `int`: Converts the value into an integer.
|
||||
* `string`, `""`: No conversion.
|
||||
* `bool`: Converts the value into a boolean.
|
||||
* `tag`: File content is used as a tag.
|
||||
|
||||
## Example Output
|
||||
|
||||
This example shows a BME280 connected to a Raspberry Pi, using the sample
|
||||
config.
|
||||
|
||||
```text
|
||||
multifile pressure=101.343285156,temperature=20.4,humidityrelative=48.9 1547202076000000000
|
||||
```
|
||||
|
||||
To reproduce this, connect a BMP280 to the board's GPIO pins and register the
|
||||
BME280 device driver
|
||||
|
||||
```sh
|
||||
cd /sys/bus/i2c/devices/i2c-1
|
||||
echo bme280 0x76 > new_device
|
||||
```
|
||||
|
||||
The kernel driver provides the following files in
|
||||
`/sys/bus/i2c/devices/1-0076/iio:device0`:
|
||||
|
||||
* `in_humidityrelative_input`: `48900`
|
||||
* `in_pressure_input`: `101.343285156`
|
||||
* `in_temp_input`: `20400`
|
116
plugins/inputs/multifile/multifile.go
Normal file
116
plugins/inputs/multifile/multifile.go
Normal file
|
@ -0,0 +1,116 @@
|
|||
//go:generate ../../../tools/readme_config_includer/generator
|
||||
package multifile
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/plugins/inputs"
|
||||
)
|
||||
|
||||
//go:embed sample.conf
|
||||
var sampleConfig string
|
||||
|
||||
type MultiFile struct {
|
||||
BaseDir string `toml:"base_dir"`
|
||||
FailEarly bool `toml:"fail_early"`
|
||||
Files []file `toml:"file"`
|
||||
}
|
||||
|
||||
type file struct {
|
||||
Name string `toml:"file"`
|
||||
Dest string `toml:"dest"`
|
||||
Conversion string `toml:"conversion"`
|
||||
}
|
||||
|
||||
func (*MultiFile) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
func (m *MultiFile) Init() error {
|
||||
for i, file := range m.Files {
|
||||
if m.BaseDir != "" {
|
||||
m.Files[i].Name = path.Join(m.BaseDir, file.Name)
|
||||
}
|
||||
if file.Dest == "" {
|
||||
m.Files[i].Dest = path.Base(file.Name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MultiFile) Gather(acc telegraf.Accumulator) error {
|
||||
now := time.Now()
|
||||
fields := make(map[string]interface{})
|
||||
tags := make(map[string]string)
|
||||
|
||||
for _, file := range m.Files {
|
||||
fileContents, err := os.ReadFile(file.Name)
|
||||
|
||||
if err != nil {
|
||||
if m.FailEarly {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
vStr := string(bytes.TrimSpace(bytes.Trim(fileContents, "\x00")))
|
||||
|
||||
if file.Conversion == "tag" {
|
||||
tags[file.Dest] = vStr
|
||||
continue
|
||||
}
|
||||
|
||||
var value interface{}
|
||||
|
||||
var d int
|
||||
if _, errfmt := fmt.Sscanf(file.Conversion, "float(%d)", &d); errfmt == nil || file.Conversion == "float" {
|
||||
var v float64
|
||||
v, err = strconv.ParseFloat(vStr, 64)
|
||||
value = v / math.Pow10(d)
|
||||
}
|
||||
|
||||
if file.Conversion == "int" {
|
||||
value, err = strconv.ParseInt(vStr, 10, 64)
|
||||
}
|
||||
|
||||
if file.Conversion == "string" || file.Conversion == "" {
|
||||
value = vStr
|
||||
}
|
||||
|
||||
if file.Conversion == "bool" {
|
||||
value, err = strconv.ParseBool(vStr)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if m.FailEarly {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if value == nil {
|
||||
return fmt.Errorf("invalid conversion %v", file.Conversion)
|
||||
}
|
||||
|
||||
fields[file.Dest] = value
|
||||
}
|
||||
|
||||
acc.AddGauge("multifile", fields, tags, now)
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
inputs.Add("multifile", func() telegraf.Input {
|
||||
return &MultiFile{
|
||||
FailEarly: true,
|
||||
}
|
||||
})
|
||||
}
|
78
plugins/inputs/multifile/multifile_test.go
Normal file
78
plugins/inputs/multifile/multifile_test.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
package multifile
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
)
|
||||
|
||||
func TestFileTypes(t *testing.T) {
|
||||
wd, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
|
||||
m := MultiFile{
|
||||
BaseDir: path.Join(wd, `testdata`),
|
||||
FailEarly: true,
|
||||
Files: []file{
|
||||
{Name: `bool.txt`, Dest: `examplebool`, Conversion: `bool`},
|
||||
{Name: `float.txt`, Dest: `examplefloat`, Conversion: `float`},
|
||||
{Name: `int.txt`, Dest: `examplefloatX`, Conversion: `float(3)`},
|
||||
{Name: `int.txt`, Dest: `exampleint`, Conversion: `int`},
|
||||
{Name: `string.txt`, Dest: `examplestring`},
|
||||
{Name: `tag.txt`, Dest: `exampletag`, Conversion: `tag`},
|
||||
{Name: `int.txt`, Conversion: `int`},
|
||||
},
|
||||
}
|
||||
|
||||
var acc testutil.Accumulator
|
||||
|
||||
require.NoError(t, m.Init())
|
||||
require.NoError(t, m.Gather(&acc))
|
||||
require.Equal(t, map[string]string{"exampletag": "test"}, acc.Metrics[0].Tags)
|
||||
require.Equal(t, map[string]interface{}{
|
||||
"examplebool": true,
|
||||
"examplestring": "hello world",
|
||||
"exampleint": int64(123456),
|
||||
"int.txt": int64(123456),
|
||||
"examplefloat": 123.456,
|
||||
"examplefloatX": 123.456,
|
||||
}, acc.Metrics[0].Fields)
|
||||
}
|
||||
|
||||
func failEarly(failEarly bool, t *testing.T) error {
|
||||
wd, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
|
||||
m := MultiFile{
|
||||
BaseDir: path.Join(wd, `testdata`),
|
||||
FailEarly: failEarly,
|
||||
Files: []file{
|
||||
{Name: `int.txt`, Dest: `exampleint`, Conversion: `int`},
|
||||
{Name: `int.txt`, Dest: `exampleerror`, Conversion: `bool`},
|
||||
},
|
||||
}
|
||||
|
||||
var acc testutil.Accumulator
|
||||
|
||||
require.NoError(t, m.Init())
|
||||
err = m.Gather(&acc)
|
||||
|
||||
if err == nil {
|
||||
require.Equal(t, map[string]interface{}{
|
||||
"exampleint": int64(123456),
|
||||
}, acc.Metrics[0].Fields)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func TestFailEarly(t *testing.T) {
|
||||
err := failEarly(false, t)
|
||||
require.NoError(t, err)
|
||||
err = failEarly(true, t)
|
||||
require.Error(t, err)
|
||||
}
|
23
plugins/inputs/multifile/sample.conf
Normal file
23
plugins/inputs/multifile/sample.conf
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Aggregates the contents of multiple files into a single point
|
||||
[[inputs.multifile]]
|
||||
## Base directory where telegraf will look for files.
|
||||
## Omit this option to use absolute paths.
|
||||
base_dir = "/sys/bus/i2c/devices/1-0076/iio:device0"
|
||||
|
||||
## If true discard all data when a single file can't be read.
|
||||
## Else, Telegraf omits the field generated from this file.
|
||||
# fail_early = true
|
||||
|
||||
## Files to parse each interval.
|
||||
[[inputs.multifile.file]]
|
||||
file = "in_pressure_input"
|
||||
dest = "pressure"
|
||||
conversion = "float"
|
||||
[[inputs.multifile.file]]
|
||||
file = "in_temp_input"
|
||||
dest = "temperature"
|
||||
conversion = "float(3)"
|
||||
[[inputs.multifile.file]]
|
||||
file = "in_humidityrelative_input"
|
||||
dest = "humidityrelative"
|
||||
conversion = "float(3)"
|
1
plugins/inputs/multifile/testdata/bool.txt
vendored
Normal file
1
plugins/inputs/multifile/testdata/bool.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
true
|
1
plugins/inputs/multifile/testdata/float.txt
vendored
Normal file
1
plugins/inputs/multifile/testdata/float.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
123.456
|
1
plugins/inputs/multifile/testdata/int.txt
vendored
Normal file
1
plugins/inputs/multifile/testdata/int.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
123456
|
1
plugins/inputs/multifile/testdata/string.txt
vendored
Normal file
1
plugins/inputs/multifile/testdata/string.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
hello world
|
1
plugins/inputs/multifile/testdata/tag.txt
vendored
Normal file
1
plugins/inputs/multifile/testdata/tag.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
test
|
Loading…
Add table
Add a link
Reference in a new issue