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

Allow snmp_context to be passed as optional parameter to override the snmp.ContextName #845

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ ifneq ($(shell which gotestsum),)
endif
endif

PROMU_VERSION ?= 0.13.0
PROMU_VERSION ?= 0.14.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not modify Makefile.common in this PR. We automatically manage this file.

PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz

SKIP_GOLANGCI_LINT :=
Expand Down
19 changes: 10 additions & 9 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type ScrapeResults struct {
retries uint64
}

func ScrapeTarget(ctx context.Context, target string, config *config.Module, logger log.Logger) (ScrapeResults, error) {
func ScrapeTarget(ctx context.Context, target string, snmp_context string, config *config.Module, logger log.Logger) (ScrapeResults, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go style does not use underscores for varibale names. Please follow the Go style guide.

Suggested change
func ScrapeTarget(ctx context.Context, target string, snmp_context string, config *config.Module, logger log.Logger) (ScrapeResults, error) {
func ScrapeTarget(ctx context.Context, target string, snmpContext string, config *config.Module, logger log.Logger) (ScrapeResults, error) {

results := ScrapeResults{}
// Set the options.
snmp := gosnmp.GoSNMP{}
Expand Down Expand Up @@ -158,7 +158,7 @@ func ScrapeTarget(ctx context.Context, target string, config *config.Module, log
}

// Configure auth.
config.WalkParams.ConfigureSNMP(&snmp)
config.WalkParams.ConfigureSNMP(&snmp, snmp_context)

// Do the actual walk.
err := snmp.Connect()
Expand Down Expand Up @@ -259,14 +259,15 @@ func buildMetricTree(metrics []*config.Metric) *MetricNode {
}

type collector struct {
ctx context.Context
target string
module *config.Module
logger log.Logger
ctx context.Context
target string
snmp_context string
module *config.Module
logger log.Logger
}

func New(ctx context.Context, target string, module *config.Module, logger log.Logger) *collector {
return &collector{ctx: ctx, target: target, module: module, logger: logger}
func New(ctx context.Context, target string, snmp_context string, module *config.Module, logger log.Logger) *collector {
return &collector{ctx: ctx, target: target, snmp_context: snmp_context, module: module, logger: logger}
}

// Describe implements Prometheus.Collector.
Expand All @@ -277,7 +278,7 @@ func (c collector) Describe(ch chan<- *prometheus.Desc) {
// Collect implements Prometheus.Collector.
func (c collector) Collect(ch chan<- prometheus.Metric) {
start := time.Now()
results, err := ScrapeTarget(c.ctx, c.target, c.module, c.logger)
results, err := ScrapeTarget(c.ctx, c.target, c.snmp_context, c.module, c.logger)
if err != nil {
level.Info(c.logger).Log("msg", "Error scraping target", "err", err)
ch <- prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "Error scraping target", nil, nil), err)
Expand Down
9 changes: 6 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (c *Module) UnmarshalYAML(unmarshal func(interface{}) error) error {
}

// ConfigureSNMP sets the various version and auth settings.
func (c WalkParams) ConfigureSNMP(g *gosnmp.GoSNMP) {
func (c WalkParams) ConfigureSNMP(g *gosnmp.GoSNMP, snmp_context string) {
switch c.Version {
case 1:
g.Version = gosnmp.Version1
Expand All @@ -135,8 +135,11 @@ func (c WalkParams) ConfigureSNMP(g *gosnmp.GoSNMP) {
g.Version = gosnmp.Version3
}
g.Community = string(c.Auth.Community)
g.ContextName = c.Auth.ContextName

if snmp_context == "" {
g.ContextName = c.Auth.ContextName
} else {
g.ContextName = snmp_context
}
// v3 security settings.
g.SecurityModel = gosnmp.UserSecurityModel
usm := &gosnmp.UsmSecurityParameters{
Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger) {
if moduleName == "" {
moduleName = "if_mib"
}

snmp_context := query.Get("snmp_context")
if len(query["snmp_context"]) > 1 {
http.Error(w, "'snmp_context' parameter must only be specified once", 400)
snmpRequestErrors.Inc()
return
}

sc.RLock()
module, ok := (*(sc.C))[moduleName]
sc.RUnlock()
Expand All @@ -98,7 +106,7 @@ func handler(w http.ResponseWriter, r *http.Request, logger log.Logger) {

start := time.Now()
registry := prometheus.NewRegistry()
c := collector.New(r.Context(), target, module, logger)
c := collector.New(r.Context(), target, snmp_context, module, logger)
registry.MustRegister(c)
// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
Expand Down