Path: blob/main/pkg/integrations/v2/snmp_exporter/snmp_exporter.go
5412 views
// Package snmp_exporter embeds https://github.com/prometheus/snmp_exporter1package snmp_exporter23import (4"fmt"56"github.com/go-kit/log"7snmp_common "github.com/grafana/agent/pkg/integrations/snmp_exporter/common"8integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"9"github.com/grafana/agent/pkg/integrations/v2/common"10snmp_config "github.com/prometheus/snmp_exporter/config"11)1213// DefaultConfig holds the default settings for the snmp_exporter integration.14var DefaultConfig = Config{15WalkParams: make(map[string]snmp_config.WalkParams),16SnmpConfigFile: "",17}1819// Config configures the SNMP integration.20type Config struct {21WalkParams map[string]snmp_config.WalkParams `yaml:"walk_params,omitempty"`22SnmpConfigFile string `yaml:"config_file,omitempty"`23SnmpTargets []SNMPTarget `yaml:"snmp_targets"`24Common common.MetricsConfig `yaml:",inline"`2526globals integrations_v2.Globals27}2829// SNMPTarget defines a target device to be used by the integration.30type SNMPTarget struct {31Name string `yaml:"name"`32Target string `yaml:"address"`33Module string `yaml:"module"`34WalkParams string `yaml:"walk_params,omitempty"`35}3637// ApplyDefaults applies the integration's default configuration.38func (c *Config) ApplyDefaults(globals integrations_v2.Globals) error {39c.Common.ApplyDefaults(globals.SubsystemOpts.Metrics.Autoscrape)40return nil41}4243// Identifier returns a string that identifies the integration.44func (c *Config) Identifier(globals integrations_v2.Globals) (string, error) {45if c.Common.InstanceKey != nil {46return *c.Common.InstanceKey, nil47}48return c.Name(), nil49}5051// NewIntegration creates a new SNMP integration.52func (c *Config) NewIntegration(log log.Logger, globals integrations_v2.Globals) (integrations_v2.Integration, error) {53var modules *snmp_config.Config54var err error55if c.SnmpConfigFile != "" {56modules, err = snmp_config.LoadFile(c.SnmpConfigFile)57if err != nil {58return nil, fmt.Errorf("failed to load snmp config from file %v: %w", c.SnmpConfigFile, err)59}60} else {61modules, err = snmp_common.LoadEmbeddedConfig()62if err != nil {63return nil, fmt.Errorf("failed to load embedded snmp config: %w", err)64}65}66c.globals = globals67sh := &snmpHandler{68cfg: c,69modules: modules,70log: log,71}72return sh, nil73}7475// UnmarshalYAML implements yaml.Unmarshaler for Config.76func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {77*c = DefaultConfig7879// This should technically be accomplished by assigning DefaultConfig, right?80// But in the reload case the existing values in this map are not purged and81// an unmarshal error is thrown stating that they key already exists in the map.82c.WalkParams = make(map[string]snmp_config.WalkParams)8384type plain Config85return unmarshal((*plain)(c))86}8788// Name returns the name of the integration.89func (c *Config) Name() string {90return "snmp"91}9293func init() {94integrations_v2.Register(&Config{}, integrations_v2.TypeSingleton)95}969798