Skip to content

Commit

Permalink
dont change config - just use the context name if passed
Browse files Browse the repository at this point in the history
  • Loading branch information
dszarmach committed Feb 14, 2023
1 parent cc28509 commit b7c7c00
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
12 changes: 6 additions & 6 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) {
results := ScrapeResults{}
// Set the options.
snmp := gosnmp.GoSNMP{}
Expand All @@ -125,7 +125,6 @@ func ScrapeTarget(ctx context.Context, target string, config *config.Module, log
snmp.Timeout = config.WalkParams.Timeout
snmp.UseUnconnectedUDPSocket = config.WalkParams.UseUnconnectedUDPSocket
snmp.LocalAddr = *srcAddress

// Allow a set of OIDs that aren't in a strictly increasing order
if config.WalkParams.AllowNonIncreasingOIDs {
snmp.AppOpts = make(map[string]interface{})
Expand Down Expand Up @@ -158,7 +157,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 @@ -261,12 +260,13 @@ func buildMetricTree(metrics []*config.Metric) *MetricNode {
type collector struct {
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 +277,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

0 comments on commit b7c7c00

Please sign in to comment.