Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/v2/snmp_exporter/snmp_exporter.go
5412 views
1
// Package snmp_exporter embeds https://github.com/prometheus/snmp_exporter
2
package snmp_exporter
3
4
import (
5
"fmt"
6
7
"github.com/go-kit/log"
8
snmp_common "github.com/grafana/agent/pkg/integrations/snmp_exporter/common"
9
integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"
10
"github.com/grafana/agent/pkg/integrations/v2/common"
11
snmp_config "github.com/prometheus/snmp_exporter/config"
12
)
13
14
// DefaultConfig holds the default settings for the snmp_exporter integration.
15
var DefaultConfig = Config{
16
WalkParams: make(map[string]snmp_config.WalkParams),
17
SnmpConfigFile: "",
18
}
19
20
// Config configures the SNMP integration.
21
type Config struct {
22
WalkParams map[string]snmp_config.WalkParams `yaml:"walk_params,omitempty"`
23
SnmpConfigFile string `yaml:"config_file,omitempty"`
24
SnmpTargets []SNMPTarget `yaml:"snmp_targets"`
25
Common common.MetricsConfig `yaml:",inline"`
26
27
globals integrations_v2.Globals
28
}
29
30
// SNMPTarget defines a target device to be used by the integration.
31
type SNMPTarget struct {
32
Name string `yaml:"name"`
33
Target string `yaml:"address"`
34
Module string `yaml:"module"`
35
WalkParams string `yaml:"walk_params,omitempty"`
36
}
37
38
// ApplyDefaults applies the integration's default configuration.
39
func (c *Config) ApplyDefaults(globals integrations_v2.Globals) error {
40
c.Common.ApplyDefaults(globals.SubsystemOpts.Metrics.Autoscrape)
41
return nil
42
}
43
44
// Identifier returns a string that identifies the integration.
45
func (c *Config) Identifier(globals integrations_v2.Globals) (string, error) {
46
if c.Common.InstanceKey != nil {
47
return *c.Common.InstanceKey, nil
48
}
49
return c.Name(), nil
50
}
51
52
// NewIntegration creates a new SNMP integration.
53
func (c *Config) NewIntegration(log log.Logger, globals integrations_v2.Globals) (integrations_v2.Integration, error) {
54
var modules *snmp_config.Config
55
var err error
56
if c.SnmpConfigFile != "" {
57
modules, err = snmp_config.LoadFile(c.SnmpConfigFile)
58
if err != nil {
59
return nil, fmt.Errorf("failed to load snmp config from file %v: %w", c.SnmpConfigFile, err)
60
}
61
} else {
62
modules, err = snmp_common.LoadEmbeddedConfig()
63
if err != nil {
64
return nil, fmt.Errorf("failed to load embedded snmp config: %w", err)
65
}
66
}
67
c.globals = globals
68
sh := &snmpHandler{
69
cfg: c,
70
modules: modules,
71
log: log,
72
}
73
return sh, nil
74
}
75
76
// UnmarshalYAML implements yaml.Unmarshaler for Config.
77
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
78
*c = DefaultConfig
79
80
// This should technically be accomplished by assigning DefaultConfig, right?
81
// But in the reload case the existing values in this map are not purged and
82
// an unmarshal error is thrown stating that they key already exists in the map.
83
c.WalkParams = make(map[string]snmp_config.WalkParams)
84
85
type plain Config
86
return unmarshal((*plain)(c))
87
}
88
89
// Name returns the name of the integration.
90
func (c *Config) Name() string {
91
return "snmp"
92
}
93
94
func init() {
95
integrations_v2.Register(&Config{}, integrations_v2.TypeSingleton)
96
}
97
98