Skip to content

Commit 36316ee

Browse files
authored
Add SNMPv3 trap support to snmp_trap input plugin (#7294)
Extend snmp_trap input plugin to support SNMPv3 traps. MD5 and SHA1 authentication protocols are supported, and DES, AES, AES192, AES256, AES192c and AES256c privacy protocols are supported.
1 parent aa8cefe commit 36316ee

File tree

5 files changed

+1120
-17
lines changed

5 files changed

+1120
-17
lines changed

go.mod

+1-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ require (
5757
github.com/gofrs/uuid v2.1.0+incompatible
5858
github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d
5959
github.com/golang/geo v0.0.0-20190916061304-5b978397cfec
60-
github.com/golang/mock v1.4.3 // indirect
6160
github.com/golang/protobuf v1.3.5
6261
github.com/google/go-cmp v0.4.0
6362
github.com/google/go-github v17.0.0+incompatible
@@ -113,7 +112,7 @@ require (
113112
github.com/shirou/gopsutil v2.20.2+incompatible
114113
github.com/shopspring/decimal v0.0.0-20200105231215-408a2507e114 // indirect
115114
github.com/sirupsen/logrus v1.4.2
116-
github.com/soniah/gosnmp v1.22.0
115+
github.com/soniah/gosnmp v1.25.0
117116
github.com/streadway/amqp v0.0.0-20180528204448-e5adc2ada8b8
118117
github.com/stretchr/testify v1.5.1
119118
github.com/tbrandon/mbserver v0.0.0-20170611213546-993e1772cc62

go.sum

+2-4
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,6 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
247247
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
248248
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
249249
github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
250-
github.com/golang/mock v1.4.3 h1:GV+pQPG/EUUbkh47niozDcADz6go/dUwhVzdUQHIVRw=
251-
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
252250
github.com/golang/protobuf v0.0.0-20161109072736-4bd1920723d7/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
253251
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
254252
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -530,8 +528,8 @@ github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo
530528
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
531529
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
532530
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
533-
github.com/soniah/gosnmp v1.22.0 h1:jVJi8+OGvR+JHIaZKMmnyNP0akJd2vEgNatybwhZvxg=
534-
github.com/soniah/gosnmp v1.22.0/go.mod h1:DuEpAS0az51+DyVBQwITDsoq4++e3LTNckp2GoasF2I=
531+
github.com/soniah/gosnmp v1.25.0 h1:0y8vpjD07NPmnT+wojnUrKkYLX9Fxw1jI4cGTumWugQ=
532+
github.com/soniah/gosnmp v1.25.0/go.mod h1:8YvfZxH388NIIw2A+X5z2Oh97VcNhtmxDLt5QeUzVuQ=
535533
github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
536534
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
537535
github.com/streadway/amqp v0.0.0-20180528204448-e5adc2ada8b8 h1:l6epF6yBwuejBfhGkM5m8VSNM/QAm7ApGyH35ehA7eQ=

plugins/inputs/snmp_trap/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ information.
3333
# service_address = "udp://:162"
3434
## Timeout running snmptranslate command
3535
# timeout = "5s"
36+
## Snmp version
37+
# version = "2c"
38+
## SNMPv3 authentication and encryption options.
39+
##
40+
## Security Name.
41+
# sec_name = "myuser"
42+
## Authentication protocol; one of "MD5", "SHA" or "".
43+
# auth_protocol = "MD5"
44+
## Authentication password.
45+
# auth_password = "pass"
46+
## Security Level; one of "noAuthNoPriv", "authNoPriv", or "authPriv".
47+
# sec_level = "authNoPriv"
48+
## Context Name.
49+
# context_name = ""
50+
## Privacy protocol used for encrypted messages; one of "DES", "AES", "AES192", "AES192C", "AES256", "AES256C" or "".
51+
# priv_protocol = ""
52+
## Privacy password used for encrypted messages.
53+
# priv_password = ""
3654
```
3755

3856
#### Using a Privileged Port

plugins/inputs/snmp_trap/snmp_trap.go

+114
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ type mibEntry struct {
3131
type SnmpTrap struct {
3232
ServiceAddress string `toml:"service_address"`
3333
Timeout internal.Duration `toml:"timeout"`
34+
Version string `toml:"version"`
35+
36+
// Settings for version 3
37+
ContextName string `toml:"context_name"`
38+
// Values: "noAuthNoPriv", "authNoPriv", "authPriv"
39+
SecLevel string `toml:"sec_level"`
40+
SecName string `toml:"sec_name"`
41+
// Values: "MD5", "SHA", "". Default: ""
42+
AuthProtocol string `toml:"auth_protocol"`
43+
AuthPassword string `toml:"auth_password"`
44+
// Values: "DES", "AES", "". Default: ""
45+
PrivProtocol string `toml:"priv_protocol"`
46+
PrivPassword string `toml:"priv_password"`
47+
EngineID string `toml:"-"`
48+
EngineBoots uint32 `toml:"-"`
49+
EngineTime uint32 `toml:"-"`
3450

3551
acc telegraf.Accumulator
3652
listener *gosnmp.TrapListener
@@ -58,6 +74,24 @@ var sampleConfig = `
5874
# service_address = "udp://:162"
5975
## Timeout running snmptranslate command
6076
# timeout = "5s"
77+
## Snmp version, defaults to 2c
78+
# version = "2c"
79+
## SNMPv3 authentication and encryption options.
80+
##
81+
## Security Name.
82+
# sec_name = "myuser"
83+
## Authentication protocol; one of "MD5", "SHA" or "".
84+
# auth_protocol = "MD5"
85+
## Authentication password.
86+
# auth_password = "pass"
87+
## Security Level; one of "noAuthNoPriv", "authNoPriv", or "authPriv".
88+
# sec_level = "authNoPriv"
89+
## Context Name.
90+
# context_name = ""
91+
## Privacy protocol used for encrypted messages; one of "DES", "AES", "AES192", "AES192C", "AES256", "AES256C" or "".
92+
# priv_protocol = ""
93+
## Privacy password used for encrypted messages.
94+
# priv_password = ""
6195
`
6296

6397
func (s *SnmpTrap) SampleConfig() string {
@@ -78,6 +112,7 @@ func init() {
78112
timeFunc: time.Now,
79113
ServiceAddress: "udp://:162",
80114
Timeout: defaultTimeout,
115+
Version: "2c",
81116
}
82117
})
83118
}
@@ -105,6 +140,85 @@ func (s *SnmpTrap) Start(acc telegraf.Accumulator) error {
105140
s.listener.OnNewTrap = makeTrapHandler(s)
106141
s.listener.Params = gosnmp.Default
107142

143+
switch s.Version {
144+
case "3":
145+
s.listener.Params.Version = gosnmp.Version3
146+
case "2c":
147+
s.listener.Params.Version = gosnmp.Version2c
148+
case "1":
149+
s.listener.Params.Version = gosnmp.Version1
150+
default:
151+
s.listener.Params.Version = gosnmp.Version2c
152+
}
153+
154+
if s.listener.Params.Version == gosnmp.Version3 {
155+
s.listener.Params.ContextName = s.ContextName
156+
s.listener.Params.SecurityModel = gosnmp.UserSecurityModel
157+
158+
switch strings.ToLower(s.SecLevel) {
159+
case "noauthnopriv", "":
160+
s.listener.Params.MsgFlags = gosnmp.NoAuthNoPriv
161+
case "authnopriv":
162+
s.listener.Params.MsgFlags = gosnmp.AuthNoPriv
163+
case "authpriv":
164+
s.listener.Params.MsgFlags = gosnmp.AuthPriv
165+
default:
166+
return fmt.Errorf("unknown security level '%s'", s.SecLevel)
167+
}
168+
169+
var authenticationProtocol gosnmp.SnmpV3AuthProtocol
170+
switch strings.ToLower(s.AuthProtocol) {
171+
case "md5":
172+
authenticationProtocol = gosnmp.MD5
173+
case "sha":
174+
authenticationProtocol = gosnmp.SHA
175+
//case "sha224":
176+
// authenticationProtocol = gosnmp.SHA224
177+
//case "sha256":
178+
// authenticationProtocol = gosnmp.SHA256
179+
//case "sha384":
180+
// authenticationProtocol = gosnmp.SHA384
181+
//case "sha512":
182+
// authenticationProtocol = gosnmp.SHA512
183+
case "":
184+
authenticationProtocol = gosnmp.NoAuth
185+
default:
186+
return fmt.Errorf("unknown authentication protocol '%s'", s.AuthProtocol)
187+
}
188+
189+
var privacyProtocol gosnmp.SnmpV3PrivProtocol
190+
switch strings.ToLower(s.PrivProtocol) {
191+
case "aes":
192+
privacyProtocol = gosnmp.AES
193+
case "des":
194+
privacyProtocol = gosnmp.DES
195+
case "aes192":
196+
privacyProtocol = gosnmp.AES192
197+
case "aes192c":
198+
privacyProtocol = gosnmp.AES192C
199+
case "aes256":
200+
privacyProtocol = gosnmp.AES256
201+
case "aes256c":
202+
privacyProtocol = gosnmp.AES256C
203+
case "":
204+
privacyProtocol = gosnmp.NoPriv
205+
default:
206+
return fmt.Errorf("unknown privacy protocol '%s'", s.PrivProtocol)
207+
}
208+
209+
s.listener.Params.SecurityParameters = &gosnmp.UsmSecurityParameters{
210+
AuthoritativeEngineID: s.EngineID,
211+
AuthoritativeEngineBoots: s.EngineBoots,
212+
AuthoritativeEngineTime: s.EngineTime,
213+
UserName: s.SecName,
214+
PrivacyProtocol: privacyProtocol,
215+
PrivacyPassphrase: s.PrivPassword,
216+
AuthenticationPassphrase: s.AuthPassword,
217+
AuthenticationProtocol: authenticationProtocol,
218+
}
219+
220+
}
221+
108222
// wrap the handler, used in unit tests
109223
if nil != s.makeHandlerWrapper {
110224
s.listener.OnNewTrap = s.makeHandlerWrapper(s.listener.OnNewTrap)

0 commit comments

Comments
 (0)