Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/prometheus/exporter/snmp/snmp.go
4096 views
1
package snmp
2
3
import (
4
"time"
5
6
"github.com/grafana/agent/component"
7
"github.com/grafana/agent/component/discovery"
8
"github.com/grafana/agent/component/prometheus/exporter"
9
"github.com/grafana/agent/pkg/integrations"
10
"github.com/grafana/agent/pkg/integrations/snmp_exporter"
11
"github.com/grafana/agent/pkg/river/rivertypes"
12
snmp_config "github.com/prometheus/snmp_exporter/config"
13
)
14
15
func init() {
16
component.Register(component.Registration{
17
Name: "prometheus.exporter.snmp",
18
Args: Arguments{},
19
Exports: exporter.Exports{},
20
Build: exporter.NewMultiTarget(createExporter, "snmp", buildSNMPTargets),
21
})
22
}
23
24
func createExporter(opts component.Options, args component.Arguments) (integrations.Integration, error) {
25
a := args.(Arguments)
26
return a.Convert().NewIntegration(opts.Logger)
27
}
28
29
// buildSNMPTargets creates the exporter's discovery targets based on the defined SNMP targets.
30
func buildSNMPTargets(baseTarget discovery.Target, args component.Arguments) []discovery.Target {
31
var targets []discovery.Target
32
33
a := args.(Arguments)
34
for _, tgt := range a.Targets {
35
target := make(discovery.Target)
36
for k, v := range baseTarget {
37
target[k] = v
38
}
39
40
target["job"] = target["job"] + "/" + tgt.Name
41
target["__param_target"] = tgt.Target
42
if tgt.Module != "" {
43
target["__param_module"] = tgt.Module
44
}
45
if tgt.WalkParams != "" {
46
target["__param_walk_params"] = tgt.WalkParams
47
}
48
49
targets = append(targets, target)
50
}
51
52
return targets
53
}
54
55
// SNMPTarget defines a target to be used by the exporter.
56
type SNMPTarget struct {
57
Name string `river:",label"`
58
Target string `river:"address,attr"`
59
Module string `river:"module,attr,optional"`
60
WalkParams string `river:"walk_params,attr,optional"`
61
}
62
63
type TargetBlock []SNMPTarget
64
65
// Convert converts the component's TargetBlock to a slice of integration's SNMPTarget.
66
func (t TargetBlock) Convert() []snmp_exporter.SNMPTarget {
67
targets := make([]snmp_exporter.SNMPTarget, 0, len(t))
68
for _, target := range t {
69
targets = append(targets, snmp_exporter.SNMPTarget{
70
Name: target.Name,
71
Target: target.Target,
72
Module: target.Module,
73
WalkParams: target.WalkParams,
74
})
75
}
76
return targets
77
}
78
79
type Auth struct {
80
Community rivertypes.Secret `river:"community,attr,optional"`
81
SecurityLevel string `river:"security_level,attr,optional"`
82
Username string `river:"username,attr,optional"`
83
Password rivertypes.Secret `river:"password,attr,optional"`
84
AuthProtocol string `river:"auth_protocol,attr,optional"`
85
PrivProtocol string `river:"priv_protocol,attr,optional"`
86
PrivPassword rivertypes.Secret `river:"priv_password,attr,optional"`
87
ContextName string `river:"context_name,attr,optional"`
88
}
89
90
// Convert converts the component's Auth to the integration's Auth.
91
func (a Auth) Convert() snmp_config.Auth {
92
return snmp_config.Auth{
93
Community: snmp_config.Secret(a.Community),
94
SecurityLevel: a.SecurityLevel,
95
Username: a.Username,
96
Password: snmp_config.Secret(a.Password),
97
AuthProtocol: a.AuthProtocol,
98
PrivProtocol: a.PrivProtocol,
99
PrivPassword: snmp_config.Secret(a.PrivPassword),
100
ContextName: a.ContextName,
101
}
102
}
103
104
type WalkParam struct {
105
Name string `river:",label"`
106
Version int `river:"version,attr,optional"`
107
MaxRepetitions uint32 `river:"max_repetitions,attr,optional"`
108
Retries int `river:"retries,attr,optional"`
109
Timeout time.Duration `river:"timeout,attr,optional"`
110
Auth Auth `river:"auth,block,optional"`
111
UseUnconnectedUDPSocket bool `river:"use_unconnected_udp_socket,attr,optional"`
112
}
113
114
type WalkParams []WalkParam
115
116
// Convert converts the component's WalkParams to the integration's WalkParams.
117
func (w WalkParams) Convert() map[string]snmp_config.WalkParams {
118
walkParams := make(map[string]snmp_config.WalkParams)
119
for _, walkParam := range w {
120
walkParams[walkParam.Name] = snmp_config.WalkParams{
121
Version: walkParam.Version,
122
MaxRepetitions: walkParam.MaxRepetitions,
123
Retries: walkParam.Retries,
124
Timeout: walkParam.Timeout,
125
Auth: walkParam.Auth.Convert(),
126
UseUnconnectedUDPSocket: walkParam.UseUnconnectedUDPSocket,
127
}
128
}
129
return walkParams
130
}
131
132
type Arguments struct {
133
ConfigFile string `river:"config_file,attr"`
134
Targets TargetBlock `river:"target,block"`
135
WalkParams WalkParams `river:"walk_param,block,optional"`
136
}
137
138
// UnmarshalRiver implements River unmarshalling for Arguments.
139
func (a *Arguments) UnmarshalRiver(f func(interface{}) error) error {
140
type args Arguments
141
return f((*args)(a))
142
}
143
144
// Convert converts the component's Arguments to the integration's Config.
145
func (a *Arguments) Convert() *snmp_exporter.Config {
146
return &snmp_exporter.Config{
147
SnmpConfigFile: a.ConfigFile,
148
SnmpTargets: a.Targets.Convert(),
149
WalkParams: a.WalkParams.Convert(),
150
}
151
}
152
153