Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(inputs.modbus): Error out on requests with no fields defined. #11469

Merged
merged 1 commit into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions plugins/inputs/modbus/configuration_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package modbus

import (
_ "embed"
"errors"
"fmt"
"hash/maphash"
)
Expand Down Expand Up @@ -65,6 +66,12 @@ func (c *ConfigurationPerRequest) Check() error {
def.Measurement = "modbus"
}

// Reject any configuration without fields as it
// makes no sense to not define anything but a request.
if len(def.Fields) == 0 {
return errors.New("found request section without fields")
}

// Check the fields
for fidx, f := range def.Fields {
// Check the input type for all fields except the bit-field ones.
Expand Down
18 changes: 18 additions & 0 deletions plugins/inputs/modbus/modbus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1924,3 +1924,21 @@ func TestRequestsStartingWithOmits(t *testing.T) {
acc.Wait(len(expected))
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
}

func TestRequestsEmptyFields(t *testing.T) {
modbus := Modbus{
Name: "Test",
Controller: "tcp://localhost:1502",
ConfigurationType: "request",
Log: testutil.Logger{},
}
modbus.Requests = []requestDefinition{
{
SlaveID: 1,
ByteOrder: "ABCD",
RegisterType: "holding",
},
}
err := modbus.Init()
require.EqualError(t, err, `configuraton invalid: found request section without fields`)
}