Skip to content

Commit

Permalink
Add a snmp_context argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ptodev committed May 22, 2024
1 parent eb01156 commit 5901816
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 32 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ Main (unreleased)

- A new `loki.rules.kubernetes` component that discovers `PrometheusRule` Kubernetes resources and loads them into a Loki Ruler instance. (@EStork09)

- A new `snmp_context` configuration argument for `prometheus.exporter.snmp` and the `snmp` Static mode integration.
It overrides the `context_name` parameter in the SNMP configuration file. (@ptodev)

### Bugfixes

- Fix panic for `prometheus.exporter.snmp` and snmp_exporter integration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ The following blocks are supported inside the definition of
The `target` block defines an individual SNMP target.
The `target` block may be specified multiple times to define multiple targets. The label of the block is required and will be used in the target's `job` label.

| Name | Type | Description | Default | Required |
| ------------- | -------- | ----------------------------------- | ------- | -------- |
| `address` | `string` | The address of SNMP device. | | yes |
| `module` | `string` | SNMP module to use for polling. | `""` | no |
| `auth` | `string` | SNMP authentication profile to use. | `""` | no |
| `walk_params` | `string` | Config to use for this target. | `""` | no |
| Name | Type | Description | Default | Required |
| -------------- | -------- | --------------------------------------------------------------------- | ------- | -------- |
| `address` | `string` | The address of SNMP device. | | yes |
| `module` | `string` | SNMP module to use for polling. | `""` | no |
| `auth` | `string` | SNMP authentication profile to use. | `""` | no |
| `walk_params` | `string` | Config to use for this target. | `""` | no |
| `snmp_context` | `string` | Override the `context_name` parameter in the SNMP configuration file. | `""` | no |

### walk_param block

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ Full reference of options:
# walk_param config to use for this snmp_target
[walk_params: <string> | default = ""]
# snmp_context overrides the `context_name` parameter in the SNMP configuration file.
[snmp_context: <string> | default = ""]
```
## walk_param config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ which is an embedded version of
[`snmp_exporter`](https://github.com/prometheus/snmp_exporter). This allows collection of SNMP metrics from the network devices with ease.

{{< admonition type="note" >}}
`snmp config` uses the latest configuration introduced in version 0.23 of the Prometheus `snmp_exporter`.
`snmp config` uses the latest configuration introduced in version 0.26 of the Prometheus `snmp_exporter`.
{{< /admonition >}}

## Quick configuration example
Expand Down Expand Up @@ -167,6 +167,9 @@ Full reference of options:
# walk_param config to use for this snmp_target
[walk_params: <string> | default = ""]
# snmp_context overrides the `context_name` parameter in the SNMP configuration file.
[snmp_context: <string> | default = ""]
```
## walk_param config
Expand Down
22 changes: 12 additions & 10 deletions internal/component/prometheus/exporter/snmp/snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ func buildSNMPTargets(baseTarget discovery.Target, args component.Arguments) []d

// SNMPTarget defines a target to be used by the exporter.
type SNMPTarget struct {
Name string `river:",label"`
Target string `river:"address,attr"`
Module string `river:"module,attr,optional"`
Auth string `river:"auth,attr,optional"`
WalkParams string `river:"walk_params,attr,optional"`
Name string `river:",label"`
Target string `river:"address,attr"`
Module string `river:"module,attr,optional"`
Auth string `river:"auth,attr,optional"`
WalkParams string `river:"walk_params,attr,optional"`
SNMPContext string `river:"snmp_context,attr,optional"`
}

type TargetBlock []SNMPTarget
Expand All @@ -77,11 +78,12 @@ func (t TargetBlock) Convert() []snmp_exporter.SNMPTarget {
targets := make([]snmp_exporter.SNMPTarget, 0, len(t))
for _, target := range t {
targets = append(targets, snmp_exporter.SNMPTarget{
Name: target.Name,
Target: target.Target,
Module: target.Module,
Auth: target.Auth,
WalkParams: target.WalkParams,
Name: target.Name,
Target: target.Target,
Module: target.Module,
Auth: target.Auth,
WalkParams: target.WalkParams,
SNMPContext: target.SNMPContext,
})
}
return targets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ func toSnmpExporter(config *snmp_exporter.Config) *snmp.Arguments {
targets := make([]snmp.SNMPTarget, len(config.SnmpTargets))
for i, t := range config.SnmpTargets {
targets[i] = snmp.SNMPTarget{
Name: common.SanitizeIdentifierPanics(t.Name),
Target: t.Target,
Module: t.Module,
Auth: t.Auth,
WalkParams: t.WalkParams,
Name: common.SanitizeIdentifierPanics(t.Name),
Target: t.Target,
Module: t.Module,
Auth: t.Auth,
WalkParams: t.WalkParams,
SNMPContext: t.SNMPContext,
}
}

Expand Down Expand Up @@ -67,11 +68,12 @@ func toSnmpExporterV2(config *snmp_exporter_v2.Config) *snmp.Arguments {
targets := make([]snmp.SNMPTarget, len(config.SnmpTargets))
for i, t := range config.SnmpTargets {
targets[i] = snmp.SNMPTarget{
Name: common.SanitizeIdentifierPanics(t.Name),
Target: t.Target,
Module: t.Module,
Auth: t.Auth,
WalkParams: t.WalkParams,
Name: common.SanitizeIdentifierPanics(t.Name),
Target: t.Target,
Module: t.Module,
Auth: t.Auth,
WalkParams: t.WalkParams,
SNMPContext: t.SNMPContext,
}
}

Expand Down
14 changes: 9 additions & 5 deletions static/integrations/snmp_exporter/snmp_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ var DefaultConfig = Config{

// SNMPTarget defines a target device to be used by the integration.
type SNMPTarget struct {
Name string `yaml:"name"`
Target string `yaml:"address"`
Module string `yaml:"module"`
Auth string `yaml:"auth"`
WalkParams string `yaml:"walk_params,omitempty"`
Name string `yaml:"name"`
Target string `yaml:"address"`
Module string `yaml:"module"`
Auth string `yaml:"auth"`
WalkParams string `yaml:"walk_params,omitempty"`
SNMPContext string `yaml:"snmp_context,omitempty"`
}

// Config configures the SNMP integration.
Expand Down Expand Up @@ -199,6 +200,9 @@ func (i *Integration) ScrapeConfigs() []config.ScrapeConfig {
if target.WalkParams != "" {
queryParams.Add("walk_params", target.WalkParams)
}
if target.SNMPContext != "" {
queryParams.Add("snmp_context", target.SNMPContext)
}
res = append(res, config.ScrapeConfig{
JobName: i.sh.cfg.Name() + "/" + target.Name,
MetricsPath: "/metrics",
Expand Down
6 changes: 6 additions & 0 deletions static/integrations/v2/snmp_exporter/snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func (sh *snmpHandler) Targets(ep integrations.Endpoint) []*targetgroup.Group {
})
}

if t.SNMPContext != "" {
labelSet = labelSet.Merge(model.LabelSet{
"__param_snmp_context": model.LabelValue(t.SNMPContext),
})
}

if t.Auth != "" {
labelSet = labelSet.Merge(model.LabelSet{
"__param_auth": model.LabelValue(t.Auth),
Expand Down

0 comments on commit 5901816

Please sign in to comment.