chore(inputs.sflow): Deprecate plugin and add migration to netflow (#15320)
This commit is contained in:
parent
28cfad68d2
commit
ad16cf4d79
|
|
@ -0,0 +1,5 @@
|
||||||
|
//go:build !custom || (migrations && (inputs || inputs.sflow))
|
||||||
|
|
||||||
|
package all
|
||||||
|
|
||||||
|
import _ "github.com/influxdata/telegraf/migrations/inputs_sflow" // register migration
|
||||||
|
|
@ -0,0 +1,68 @@
|
||||||
|
package inputs_sflow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/influxdata/toml"
|
||||||
|
"github.com/influxdata/toml/ast"
|
||||||
|
|
||||||
|
"github.com/influxdata/telegraf/migrations"
|
||||||
|
"github.com/influxdata/telegraf/migrations/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
const msg = `
|
||||||
|
Replacement 'inputs.netflow' will output a different metric format.
|
||||||
|
Please adapt your queries!
|
||||||
|
`
|
||||||
|
|
||||||
|
// Define "old" data structure
|
||||||
|
type sflow struct {
|
||||||
|
ServiceAddress string `toml:"service_address"`
|
||||||
|
ReadBufferSize string `toml:"read_buffer_size"`
|
||||||
|
common.InputOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migration function
|
||||||
|
func migrate(tbl *ast.Table) ([]byte, string, error) {
|
||||||
|
// Decode the old data structure
|
||||||
|
var old sflow
|
||||||
|
if err := toml.UnmarshalTable(tbl, &old); err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fill common options
|
||||||
|
plugin := make(map[string]interface{})
|
||||||
|
old.InputOptions.Migrate()
|
||||||
|
general, err := toml.Marshal(old.InputOptions)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", fmt.Errorf("marshalling general options failed: %w", err)
|
||||||
|
}
|
||||||
|
if err := toml.Unmarshal(general, &plugin); err != nil {
|
||||||
|
return nil, "", fmt.Errorf("re-unmarshalling general options failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use a map for the new plugin and fill in the data
|
||||||
|
plugin["service_address"] = old.ServiceAddress
|
||||||
|
if old.ReadBufferSize != "" {
|
||||||
|
plugin["read_buffer_size"] = old.ReadBufferSize
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the corresponding metric configurations
|
||||||
|
cfg := migrations.CreateTOMLStruct("inputs", "netflow")
|
||||||
|
cfg.Add("inputs", "netflow", plugin)
|
||||||
|
|
||||||
|
// Marshal the new configuration
|
||||||
|
buf, err := toml.Marshal(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
buf = append(buf, []byte("\n")...)
|
||||||
|
|
||||||
|
// Create the new content to output
|
||||||
|
return buf, msg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register the migration function for the plugin type
|
||||||
|
func init() {
|
||||||
|
migrations.AddPluginMigration("inputs.sflow", migrate)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,62 @@
|
||||||
|
package inputs_sflow_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/influxdata/telegraf/config"
|
||||||
|
_ "github.com/influxdata/telegraf/migrations/inputs_sflow" // register migration
|
||||||
|
_ "github.com/influxdata/telegraf/plugins/inputs/netflow" // register plugin
|
||||||
|
_ "github.com/influxdata/telegraf/plugins/inputs/sflow" // register plugin
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCases(t *testing.T) {
|
||||||
|
// Get all directories in testdata
|
||||||
|
folders, err := os.ReadDir("testcases")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
for _, f := range folders {
|
||||||
|
// Only handle folders
|
||||||
|
if !f.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run(f.Name(), func(t *testing.T) {
|
||||||
|
testcasePath := filepath.Join("testcases", f.Name())
|
||||||
|
inputFile := filepath.Join(testcasePath, "telegraf.conf")
|
||||||
|
expectedFile := filepath.Join(testcasePath, "expected.conf")
|
||||||
|
|
||||||
|
// Read the expected output
|
||||||
|
expected := config.NewConfig()
|
||||||
|
require.NoError(t, expected.LoadConfig(expectedFile))
|
||||||
|
require.NotEmpty(t, expected.Inputs)
|
||||||
|
|
||||||
|
// Read the input data
|
||||||
|
input, remote, err := config.LoadConfigFile(inputFile)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.False(t, remote)
|
||||||
|
require.NotEmpty(t, input)
|
||||||
|
|
||||||
|
// Migrate
|
||||||
|
output, n, err := config.ApplyMigrations(input)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotEmpty(t, output)
|
||||||
|
require.GreaterOrEqual(t, n, uint64(1))
|
||||||
|
actual := config.NewConfig()
|
||||||
|
require.NoError(t, actual.LoadConfigData(output))
|
||||||
|
|
||||||
|
// Test the output
|
||||||
|
require.Len(t, actual.Inputs, len(expected.Inputs))
|
||||||
|
actualIDs := make([]string, 0, len(expected.Inputs))
|
||||||
|
expectedIDs := make([]string, 0, len(expected.Inputs))
|
||||||
|
for i := range actual.Inputs {
|
||||||
|
actualIDs = append(actualIDs, actual.Inputs[i].ID())
|
||||||
|
expectedIDs = append(expectedIDs, expected.Inputs[i].ID())
|
||||||
|
}
|
||||||
|
require.ElementsMatchf(t, expectedIDs, actualIDs, "generated config: %s", string(output))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
[[inputs.netflow]]
|
||||||
|
service_address = "udp://:6343"
|
||||||
|
read_buffer_size = "32KiB"
|
||||||
|
fieldinclude = ["a"]
|
||||||
|
name_override = "foo"
|
||||||
|
[inputs.netflow.tags]
|
||||||
|
foo = "bar"
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
[[inputs.sflow]]
|
||||||
|
service_address = "udp://:6343"
|
||||||
|
read_buffer_size = "32KiB"
|
||||||
|
|
||||||
|
name_override = "foo"
|
||||||
|
fieldpass = ["a"]
|
||||||
|
[inputs.sflow.tags]
|
||||||
|
foo = "bar"
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
[[inputs.netflow]]
|
||||||
|
service_address = "udp://:6343"
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
# SFlow V5 Protocol Listener
|
||||||
|
[[inputs.sflow]]
|
||||||
|
## Address to listen for sFlow packets.
|
||||||
|
## example: service_address = "udp://:6343"
|
||||||
|
## service_address = "udp4://:6343"
|
||||||
|
## service_address = "udp6://:6343"
|
||||||
|
service_address = "udp://:6343"
|
||||||
|
|
||||||
|
## Set the size of the operating system's receive buffer.
|
||||||
|
## example: read_buffer_size = "64KiB"
|
||||||
|
# read_buffer_size = ""
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
[[inputs.netflow]]
|
||||||
|
service_address = "udp://:6343"
|
||||||
|
read_buffer_size = "32KiB"
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
# SFlow V5 Protocol Listener
|
||||||
|
[[inputs.sflow]]
|
||||||
|
## Address to listen for sFlow packets.
|
||||||
|
## example: service_address = "udp://:6343"
|
||||||
|
## service_address = "udp4://:6343"
|
||||||
|
## service_address = "udp6://:6343"
|
||||||
|
service_address = "udp://:6343"
|
||||||
|
|
||||||
|
## Set the size of the operating system's receive buffer.
|
||||||
|
## example: read_buffer_size = "64KiB"
|
||||||
|
read_buffer_size = "32KiB"
|
||||||
|
|
@ -49,6 +49,10 @@ var Deprecations = map[string]telegraf.DeprecationInfo{
|
||||||
Since: "1.15.0",
|
Since: "1.15.0",
|
||||||
Notice: "use 'inputs.tail' with 'grok' data format instead",
|
Notice: "use 'inputs.tail' with 'grok' data format instead",
|
||||||
},
|
},
|
||||||
|
"sflow": {
|
||||||
|
Since: "1.31.0",
|
||||||
|
Notice: "use 'inputs.netflow' instead",
|
||||||
|
},
|
||||||
"snmp_legacy": {
|
"snmp_legacy": {
|
||||||
Since: "1.0.0",
|
Since: "1.0.0",
|
||||||
RemovalIn: "1.30.0",
|
RemovalIn: "1.30.0",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue