Skip to content

Commit

Permalink
feat(inputs.s7comm): Add optional connection type setting (#15000)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hongliukai authored Mar 15, 2024
1 parent fde7779 commit a2ec012
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ require (
github.com/redis/go-redis/v9 v9.2.1
github.com/riemann/riemann-go-client v0.5.1-0.20211206220514-f58f10cdce16
github.com/robbiet480/go.nut v0.0.0-20220219091450-bd8f121e1fa1
github.com/robinson/gos7 v0.0.0-20231031082500-fb5a72fd499e
github.com/robinson/gos7 v0.0.0-20240315073918-1f14519e4846
github.com/safchain/ethtool v0.3.0
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/sensu/sensu-go/api/core/v2 v2.16.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2071,8 +2071,8 @@ github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff h1:+6NUiITWwE5q1
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff/go.mod h1:xvqspoSXJTIpemEonrMDFq6XzwHYYgToXWj5eRX1OtY=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/robinson/gos7 v0.0.0-20231031082500-fb5a72fd499e h1:Ofp6C2iX58K698sGpIXZFp3furntNlhIjeyLkcrAiek=
github.com/robinson/gos7 v0.0.0-20231031082500-fb5a72fd499e/go.mod h1:AMHIeh1KJ7Xa2RVOMHdv9jXKrpw0D4EWGGQMHLb2doc=
github.com/robinson/gos7 v0.0.0-20240315073918-1f14519e4846 h1:CnAbtX0j07ZVR/TnD5V6ypFTrASJlfr+fc4OY2da9eg=
github.com/robinson/gos7 v0.0.0-20240315073918-1f14519e4846/go.mod h1:AMHIeh1KJ7Xa2RVOMHdv9jXKrpw0D4EWGGQMHLb2doc=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.1.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
Expand Down
4 changes: 4 additions & 0 deletions plugins/inputs/s7comm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
rack = 0
slot = 0

## Connection or drive type of S7 protocol
## Available options are "PD" (programming device), "OP" (operator panel) or "basic" (S7 basic communication).
# connection_type = "PD"

## Max count of fields to be bundled in one batch-request. (PDU size)
# pdu_size = 20

Expand Down
15 changes: 14 additions & 1 deletion plugins/inputs/s7comm/s7comm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ var (
// see https://support.industry.siemens.com/cs/document/36479/date_and_time-format-for-s7-?dti=0&lc=en-DE
"DT": 0x0F, // Date and time (7 byte)
}

connectionTypeMap = map[string]int{
"PD": 1,
"OP": 2,
"basic": 3,
}
)

type metricFieldDefinition struct {
Expand Down Expand Up @@ -84,6 +90,7 @@ type S7comm struct {
Server string `toml:"server"`
Rack int `toml:"rack"`
Slot int `toml:"slot"`
ConnectionType string `toml:"connection_type"`
BatchMaxSize int `toml:"pdu_size"`
Timeout config.Duration `toml:"timeout"`
DebugConnection bool `toml:"debug_connection"`
Expand Down Expand Up @@ -113,6 +120,12 @@ func (s *S7comm) Init() error {
if s.Slot < 0 {
return errors.New("'slot' has to be specified")
}
if s.ConnectionType == "" {
s.ConnectionType = "PD"
}
if _, found := connectionTypeMap[s.ConnectionType]; !found {
return fmt.Errorf("invalid 'connection_type' %q", s.ConnectionType)
}
if len(s.Configs) == 0 {
return errors.New("no metric defined")
}
Expand All @@ -127,7 +140,7 @@ func (s *S7comm) Init() error {
}

// Create handler for the connection
s.handler = gos7.NewTCPClientHandler(s.Server, s.Rack, s.Slot)
s.handler = gos7.NewTCPClientHandlerWithConnectType(s.Server, s.Rack, s.Slot, connectionTypeMap[s.ConnectionType])
s.handler.Timeout = time.Duration(s.Timeout)
if s.DebugConnection {
s.handler.Logger = log.New(os.Stderr, "D! [inputs.s7comm]", log.LstdFlags)
Expand Down
4 changes: 4 additions & 0 deletions plugins/inputs/s7comm/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
rack = 0
slot = 0

## Connection or drive type of S7 protocol
## Available options are "PD" (programming device), "OP" (operator panel) or "basic" (S7 basic communication).
# connection_type = "PD"

## Max count of fields to be bundled in one batch-request. (PDU size)
# pdu_size = 20

Expand Down

0 comments on commit a2ec012

Please sign in to comment.