-
Notifications
You must be signed in to change notification settings - Fork 485
/
snmp_exporter.go
202 lines (182 loc) · 5.98 KB
/
snmp_exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Package snmp_exporter embeds https://github.com/prometheus/snmp_exporter
package snmp_exporter
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/go-kit/log"
"github.com/grafana/agent/pkg/integrations"
"github.com/grafana/agent/pkg/integrations/config"
snmp_common "github.com/grafana/agent/pkg/integrations/snmp_exporter/common"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/snmp_exporter/collector"
snmp_config "github.com/prometheus/snmp_exporter/config"
)
// DefaultConfig holds the default settings for the snmp_exporter integration.
var DefaultConfig = Config{
WalkParams: make(map[string]snmp_config.WalkParams),
SnmpConfigFile: "",
SnmpTargets: make([]SNMPTarget, 0),
SnmpConfig: snmp_config.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"`
}
// Config configures the SNMP integration.
type Config struct {
WalkParams map[string]snmp_config.WalkParams `yaml:"walk_params,omitempty"`
SnmpConfigFile string `yaml:"config_file,omitempty"`
SnmpTargets []SNMPTarget `yaml:"snmp_targets"`
SnmpConfig snmp_config.Config `yaml:"snmp_config,omitempty"`
}
// UnmarshalYAML implements yaml.Unmarshaler for Config.
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultConfig
type plain Config
return unmarshal((*plain)(c))
}
// Name returns the name of the integration.
func (c *Config) Name() string {
return "snmp"
}
// InstanceKey returns the hostname:port of the agent.
func (c *Config) InstanceKey(agentKey string) (string, error) {
return agentKey, nil
}
// NewIntegration creates a new SNMP integration.
func (c *Config) NewIntegration(l log.Logger) (integrations.Integration, error) {
return New(l, c)
}
func init() {
integrations.RegisterIntegration(&Config{})
}
// New creates a new snmp_exporter integration
func New(log log.Logger, c *Config) (integrations.Integration, error) {
snmpCfg, err := LoadSNMPConfig(c.SnmpConfigFile, &c.SnmpConfig)
if err != nil {
return nil, err
}
// The `name` and `address` fields are mandatory for the SNMP targets are mandatory.
// Enforce this check and fail the creation of the integration if they're missing.
for _, target := range c.SnmpTargets {
if target.Name == "" || target.Target == "" {
return nil, fmt.Errorf("failed to load snmp_targets; the `name` and `address` fields are mandatory")
}
}
sh := &snmpHandler{
cfg: c,
snmpCfg: snmpCfg,
log: log,
}
integration := &Integration{
sh: sh,
}
return integration, nil
}
// LoadSNMPConfig loads the SNMP configuration from the given file. If the file is empty, it will
// load the embedded configuration.
func LoadSNMPConfig(snmpConfigFile string, snmpCfg *snmp_config.Config) (*snmp_config.Config, error) {
var err error
if snmpConfigFile != "" {
snmpCfg, err = snmp_config.LoadFile([]string{snmpConfigFile})
if err != nil {
return nil, fmt.Errorf("failed to load snmp config from file %v: %w", snmpConfigFile, err)
}
} else {
if len(snmpCfg.Modules) == 0 && len(snmpCfg.Auths) == 0 { // If the user didn't specify a config, load the embedded config.
snmpCfg, err = snmp_common.LoadEmbeddedConfig()
if err != nil {
return nil, fmt.Errorf("failed to load embedded snmp config: %w", err)
}
}
}
return snmpCfg, nil
}
func NewSNMPMetrics(reg prometheus.Registerer) collector.Metrics {
buckets := prometheus.ExponentialBuckets(0.0001, 2, 15)
return collector.Metrics{
SNMPCollectionDuration: promauto.With(reg).NewHistogramVec(
prometheus.HistogramOpts{
Namespace: namespace,
Name: "collection_duration_seconds",
Help: "Duration of collections by the SNMP exporter",
},
[]string{"module"},
),
SNMPUnexpectedPduType: promauto.With(reg).NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Name: "unexpected_pdu_type_total",
Help: "Unexpected Go types in a PDU.",
},
),
SNMPDuration: promauto.With(reg).NewHistogram(
prometheus.HistogramOpts{
Namespace: namespace,
Name: "packet_duration_seconds",
Help: "A histogram of latencies for SNMP packets.",
Buckets: buckets,
},
),
SNMPPackets: promauto.With(reg).NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Name: "packets_total",
Help: "Number of SNMP packet sent, including retries.",
},
),
SNMPRetries: promauto.With(reg).NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Name: "packet_retries_total",
Help: "Number of SNMP packet retries.",
},
),
}
}
// Integration is the SNMP integration. The integration scrapes metrics
// from the host Linux-based system.
type Integration struct {
sh *snmpHandler
}
// MetricsHandler implements Integration.
func (i *Integration) MetricsHandler() (http.Handler, error) {
return i.sh, nil
}
// Run satisfies Integration.Run.
func (i *Integration) Run(ctx context.Context) error {
// We don't need to do anything here, so we can just wait for the context to
// finish.
<-ctx.Done()
return ctx.Err()
}
// ScrapeConfigs satisfies Integration.ScrapeConfigs.
func (i *Integration) ScrapeConfigs() []config.ScrapeConfig {
var res []config.ScrapeConfig
for _, target := range i.sh.cfg.SnmpTargets {
queryParams := url.Values{}
queryParams.Add("target", target.Target)
if target.Module != "" {
queryParams.Add("module", target.Module)
}
if target.Auth != "" {
queryParams.Add("auth", target.Auth)
}
if target.WalkParams != "" {
queryParams.Add("walk_params", target.WalkParams)
}
res = append(res, config.ScrapeConfig{
JobName: i.sh.cfg.Name() + "/" + target.Name,
MetricsPath: "/metrics",
QueryParams: queryParams,
})
}
return res
}