Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/snmp_exporter/snmp_exporter.go
5398 views
1
// Package snmp_exporter embeds https://github.com/prometheus/snmp_exporter
2
package snmp_exporter
3
4
import (
5
"context"
6
"fmt"
7
"net/http"
8
"net/url"
9
10
"github.com/go-kit/log"
11
"github.com/grafana/agent/pkg/integrations"
12
"github.com/grafana/agent/pkg/integrations/config"
13
snmp_common "github.com/grafana/agent/pkg/integrations/snmp_exporter/common"
14
snmp_config "github.com/prometheus/snmp_exporter/config"
15
)
16
17
// DefaultConfig holds the default settings for the snmp_exporter integration.
18
var DefaultConfig = Config{
19
WalkParams: make(map[string]snmp_config.WalkParams),
20
SnmpConfigFile: "",
21
SnmpTargets: make([]SNMPTarget, 0),
22
}
23
24
// SNMPTarget defines a target device to be used by the integration.
25
type SNMPTarget struct {
26
Name string `yaml:"name"`
27
Target string `yaml:"address"`
28
Module string `yaml:"module"`
29
WalkParams string `yaml:"walk_params,omitempty"`
30
}
31
32
// Config configures the SNMP integration.
33
type Config struct {
34
WalkParams map[string]snmp_config.WalkParams `yaml:"walk_params,omitempty"`
35
SnmpConfigFile string `yaml:"config_file,omitempty"`
36
SnmpTargets []SNMPTarget `yaml:"snmp_targets"`
37
}
38
39
// UnmarshalYAML implements yaml.Unmarshaler for Config.
40
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
41
*c = DefaultConfig
42
43
type plain Config
44
return unmarshal((*plain)(c))
45
}
46
47
// Name returns the name of the integration.
48
func (c *Config) Name() string {
49
return "snmp"
50
}
51
52
// InstanceKey returns the hostname:port of the agent.
53
func (c *Config) InstanceKey(agentKey string) (string, error) {
54
return agentKey, nil
55
}
56
57
// NewIntegration creates a new SNMP integration.
58
func (c *Config) NewIntegration(l log.Logger) (integrations.Integration, error) {
59
return New(l, c)
60
}
61
62
func init() {
63
integrations.RegisterIntegration(&Config{})
64
}
65
66
// New creates a new snmp_exporter integration
67
func New(log log.Logger, c *Config) (integrations.Integration, error) {
68
var modules *snmp_config.Config
69
var err error
70
if c.SnmpConfigFile != "" {
71
modules, err = snmp_config.LoadFile(c.SnmpConfigFile)
72
if err != nil {
73
return nil, fmt.Errorf("failed to load snmp config from file %v: %w", c.SnmpConfigFile, err)
74
}
75
} else {
76
modules, err = snmp_common.LoadEmbeddedConfig()
77
if err != nil {
78
return nil, fmt.Errorf("failed to load embedded snmp config: %w", err)
79
}
80
}
81
82
// The `name` and `address` fields are mandatory for the SNMP targets are mandatory.
83
// Enforce this check and fail the creation of the integration if they're missing.
84
for _, target := range c.SnmpTargets {
85
if target.Name == "" || target.Target == "" {
86
return nil, fmt.Errorf("failed to load snmp_targets; the `name` and `address` fields are mandatory")
87
}
88
}
89
90
sh := &snmpHandler{
91
cfg: c,
92
modules: modules,
93
log: log,
94
}
95
integration := &Integration{
96
sh: sh,
97
}
98
99
return integration, nil
100
}
101
102
// Integration is the SNMP integration. The integration scrapes metrics
103
// from the host Linux-based system.
104
type Integration struct {
105
sh *snmpHandler
106
}
107
108
// MetricsHandler implements Integration.
109
func (i *Integration) MetricsHandler() (http.Handler, error) {
110
return i.sh, nil
111
}
112
113
// Run satisfies Integration.Run.
114
func (i *Integration) Run(ctx context.Context) error {
115
// We don't need to do anything here, so we can just wait for the context to
116
// finish.
117
<-ctx.Done()
118
return ctx.Err()
119
}
120
121
// ScrapeConfigs satisfies Integration.ScrapeConfigs.
122
func (i *Integration) ScrapeConfigs() []config.ScrapeConfig {
123
var res []config.ScrapeConfig
124
for _, target := range i.sh.cfg.SnmpTargets {
125
queryParams := url.Values{}
126
queryParams.Add("target", target.Target)
127
if target.Module != "" {
128
queryParams.Add("module", target.Module)
129
}
130
if target.WalkParams != "" {
131
queryParams.Add("walk_params", target.WalkParams)
132
}
133
res = append(res, config.ScrapeConfig{
134
JobName: i.sh.cfg.Name() + "/" + target.Name,
135
MetricsPath: "/metrics",
136
QueryParams: queryParams,
137
})
138
}
139
return res
140
}
141
142