Skip to content

Commit 5f907e6

Browse files
authored
Add a "snmp_context" argument to the URL (#1163)
Signed-off-by: Paulin Todev <[email protected]>
1 parent 5a4a581 commit 5f907e6

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ is not necessary within the Prometheus configuration file.
9292
Metrics concerning the operation of the exporter itself are available at the
9393
endpoint <http://localhost:9116/metrics>.
9494

95+
It is possible to supply an optional `snmp_context` parameter in the URL, like this:
96+
<http://localhost:9116/snmp?auth=my_secure_v3&module=ddwrt&target=192.0.0.8&snmp_context=vrf-mgmt>
97+
The `snmp_context` parameter in the URL would override the `context_name` parameter in the `snmp.yml` file.
98+
9599
## Multi-Module Handling
96100
The multi-module functionality allows you to specify multiple modules, enabling the retrieval of information from several modules in a single scrape.
97101
The concurrency can be specified using the snmp-exporter option `--snmp.module-concurrency` (the default is 1).

collector/collector.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,11 @@ type Collector struct {
299299
logger log.Logger
300300
metrics Metrics
301301
concurrency int
302+
snmpContext string
302303
}
303304

304-
func New(ctx context.Context, target, authName string, auth *config.Auth, modules []*NamedModule, logger log.Logger, metrics Metrics, conc int) *Collector {
305-
return &Collector{ctx: ctx, target: target, authName: authName, auth: auth, modules: modules, logger: logger, metrics: metrics, concurrency: conc}
305+
func New(ctx context.Context, target, authName, snmpContext string, auth *config.Auth, modules []*NamedModule, logger log.Logger, metrics Metrics, conc int) *Collector {
306+
return &Collector{ctx: ctx, target: target, authName: authName, auth: auth, modules: modules, logger: logger, metrics: metrics, concurrency: conc, snmpContext: snmpContext}
306307
}
307308

308309
// Describe implements Prometheus.Collector.
@@ -429,7 +430,7 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
429430
// Set the options.
430431
client.SetOptions(func(g *gosnmp.GoSNMP) {
431432
g.Context = ctx
432-
c.auth.ConfigureSNMP(g)
433+
c.auth.ConfigureSNMP(g, c.snmpContext)
433434
})
434435
if err = client.Connect(); err != nil {
435436
level.Info(logger).Log("msg", "Error connecting to target", "err", err)

config/config.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (c *Module) UnmarshalYAML(unmarshal func(interface{}) error) error {
129129
}
130130

131131
// ConfigureSNMP sets the various version and auth settings.
132-
func (c Auth) ConfigureSNMP(g *gosnmp.GoSNMP) {
132+
func (c Auth) ConfigureSNMP(g *gosnmp.GoSNMP, snmpContext string) {
133133
switch c.Version {
134134
case 1:
135135
g.Version = gosnmp.Version1
@@ -139,7 +139,12 @@ func (c Auth) ConfigureSNMP(g *gosnmp.GoSNMP) {
139139
g.Version = gosnmp.Version3
140140
}
141141
g.Community = string(c.Community)
142-
g.ContextName = c.ContextName
142+
143+
if snmpContext == "" {
144+
g.ContextName = c.ContextName
145+
} else {
146+
g.ContextName = snmpContext
147+
}
143148

144149
// v3 security settings.
145150
g.SecurityModel = gosnmp.UserSecurityModel

main.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger, exporter
102102
authName = "public_v2"
103103
}
104104

105+
snmpContext := query.Get("snmp_context")
106+
if len(query["snmp_context"]) > 1 {
107+
http.Error(w, "'snmp_context' parameter must only be specified once", http.StatusBadRequest)
108+
snmpRequestErrors.Inc()
109+
return
110+
}
111+
105112
queryModule := query["module"]
106113
if len(queryModule) == 0 {
107114
queryModule = append(queryModule, "if_mib")
@@ -141,7 +148,7 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger, exporter
141148
sc.RUnlock()
142149
logger = log.With(logger, "auth", authName, "target", target)
143150
registry := prometheus.NewRegistry()
144-
c := collector.New(r.Context(), target, authName, auth, nmodules, logger, exporterMetrics, *concurrency)
151+
c := collector.New(r.Context(), target, authName, snmpContext, auth, nmodules, logger, exporterMetrics, *concurrency)
145152
registry.MustRegister(c)
146153
// Delegate http serving to Prometheus client library, which will call collector.Collect.
147154
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})

0 commit comments

Comments
 (0)