@cryptotaxi247 / netdata-1 / commits / 13e419cee

feat(go.d/snmp): add SNMPv3 context name support (#22175)

Costa Tsaousis committed Apr 9, 2026 at 15:27 UTC 13e419cee6eac94bd236fbd9fdebb58c20ec00a6
9 files changed +30 -1
src/go/plugin/go.d/collector/snmp/collector_test.go
+2
@@ -369,6 +369,7 @@ func prepareV3Config() Config {
369 AuthKey: "auth_key",
370 PrivProto: strings.ToLower(gosnmp.AES.String()),
371 PrivKey: "priv_key",
372 + ContextName: "test-context",
373 }
374 return cfg
375 }
@@ -425,6 +426,7 @@ func setMockClientSetterExpect(m *snmpmock.MockHandler) {
426 m.EXPECT().SetSecurityModel(gomock.Any()).AnyTimes()
427 m.EXPECT().SetMsgFlags(gomock.Any()).AnyTimes()
428 m.EXPECT().SetSecurityParameters(gomock.Any()).AnyTimes()
429 + m.EXPECT().SetContextName(gomock.Any()).AnyTimes()
430 m.EXPECT().MaxRepetitions().Return(uint32(25)).AnyTimes()
431 }
432
src/go/plugin/go.d/collector/snmp/config.go
+1
@@ -37,6 +37,7 @@ type (
37 AuthKey string `yaml:"auth_key,omitempty" json:"auth_key"`
38 PrivProto string `yaml:"priv_proto,omitempty" json:"priv_proto"`
39 PrivKey string `yaml:"priv_key,omitempty" json:"priv_key"`
40 + ContextName string `yaml:"context_name,omitempty" json:"context_name"`
41 }
42 OptionsConfig struct {
43 Port int `yaml:"port,omitempty" json:"port"`
src/go/plugin/go.d/collector/snmp/config_schema.json
+9
@@ -186,6 +186,12 @@
186 "priv_key": {
187 "title": "Privacy passphrase",
188 "type": "string"
189 + },
190 + "context_name": {
191 + "title": "Context name",
192 + "description": "SNMPv3 context name used to address a specific MIB view on multi-context agents (e.g. virtual routers, logical partitions, snmpsim-simulated devices). Leave empty to use the default context.",
193 + "type": "string",
194 + "default": ""
195 }
196 }
197 },
@@ -310,6 +316,9 @@
316 },
317 "priv_key": {
318 "ui:widget": "password"
319 + },
320 + "context_name": {
321 + "ui:help": "Advanced. Usually left empty. Required when targeting a specific context on multi-context agents or when driving snmpsim virtual devices."
322 }
323 },
324 "ping": {
src/go/plugin/go.d/collector/snmp/init.go
+4
@@ -60,6 +60,10 @@ func (c *Collector) initSNMPClient() (gosnmp.Handler, error) {
60 PrivacyProtocol: snmputils.ParseSNMPv3PrivProtocol(c.User.PrivProto),
61 PrivacyPassphrase: c.User.PrivKey,
62 })
63 + // Context name addresses a specific MIB view on multi-context
64 + // agents (virtual routers, logical partitions, snmpsim virtual
65 + // devices). Empty string means the default context.
66 + client.SetContextName(c.User.ContextName)
67 default:
68 return nil, fmt.Errorf("invalid SNMP version: %s", c.Options.Version)
69 }
src/go/plugin/go.d/collector/snmp/metadata.yaml
+5
@@ -351,6 +351,11 @@ modules:
351 description: Privacy protocol pass phrase for SNMPv3 messages.
352 default_value: ""
353 required: false
354 + - name: user.context_name
355 + group: SNMPv3
356 + description: SNMPv3 context name used to address a specific MIB view on multi-context agents (e.g. virtual routers, logical partitions, snmpsim-simulated devices). Leave empty to use the default context.
357 + default_value: ""
358 + required: false
359
360 - name: options.version
361 group: SNMP transport
src/go/plugin/go.d/collector/snmp/testdata/config.json
+2 -1
@@ -18,7 +18,8 @@
18 "auth_proto": "ok",
19 "auth_key": "ok",
20 "priv_proto": "ok",
21 - "priv_key": "ok"
21 + "priv_key": "ok",
22 + "context_name": "ok"
23 },
24 "options": {
25 "port": 123,
src/go/plugin/go.d/collector/snmp/testdata/config.yaml
+1
@@ -21,6 +21,7 @@ user:
21 auth_key: "ok"
22 priv_proto: "ok"
23 priv_key: "ok"
24 + context_name: "ok"
25
26 options:
27 port: 123
src/go/plugin/go.d/discovery/sdext/discoverer/snmpsd/config.go
+3
@@ -58,6 +58,8 @@ type (
58 PrivacyProtocol string `yaml:"priv_protocol,omitempty" json:"priv_protocol,omitempty"`
59 // PrivacyPassphrase is the privacy passphrase (for SNMPv3)
60 PrivacyPassphrase string `yaml:"priv_password,omitempty" json:"priv_password,omitempty"`
61 + // ContextName is the SNMPv3 context name. Empty means the default context.
62 + ContextName string `yaml:"context_name,omitempty" json:"context_name,omitempty"`
63 }
64 )
65
@@ -143,5 +145,6 @@ func setCredential(client gosnmp.Handler, cred CredentialConfig) {
145 PrivacyProtocol: snmputils.ParseSNMPv3PrivProtocol(cred.PrivacyProtocol),
146 PrivacyPassphrase: cred.PrivacyPassphrase,
147 })
148 + client.SetContextName(cred.ContextName)
149 }
150 }
src/go/plugin/go.d/pkg/snmputils/utils.go
+3
@@ -85,6 +85,9 @@ func SnmpClientConnInfo(c gosnmp.Handler) string {
85 info.WriteString(fmt.Sprintf(",community='%s'", c.Community()))
86 case gosnmp.Version3:
87 info.WriteString(fmt.Sprintf(",security_level='%d,%s'", c.MsgFlags(), c.SecurityParameters().Description()))
88 + if ctx := c.ContextName(); ctx != "" {
89 + info.WriteString(fmt.Sprintf(",context_name='%s'", ctx))
90 + }
91 }
92 return info.String()
93 }