Path: blob/main/pkg/integrations/snmp_exporter/snmp_exporter.go
5398 views
// Package snmp_exporter embeds https://github.com/prometheus/snmp_exporter1package snmp_exporter23import (4"context"5"fmt"6"net/http"7"net/url"89"github.com/go-kit/log"10"github.com/grafana/agent/pkg/integrations"11"github.com/grafana/agent/pkg/integrations/config"12snmp_common "github.com/grafana/agent/pkg/integrations/snmp_exporter/common"13snmp_config "github.com/prometheus/snmp_exporter/config"14)1516// DefaultConfig holds the default settings for the snmp_exporter integration.17var DefaultConfig = Config{18WalkParams: make(map[string]snmp_config.WalkParams),19SnmpConfigFile: "",20SnmpTargets: make([]SNMPTarget, 0),21}2223// SNMPTarget defines a target device to be used by the integration.24type SNMPTarget struct {25Name string `yaml:"name"`26Target string `yaml:"address"`27Module string `yaml:"module"`28WalkParams string `yaml:"walk_params,omitempty"`29}3031// Config configures the SNMP integration.32type Config struct {33WalkParams map[string]snmp_config.WalkParams `yaml:"walk_params,omitempty"`34SnmpConfigFile string `yaml:"config_file,omitempty"`35SnmpTargets []SNMPTarget `yaml:"snmp_targets"`36}3738// UnmarshalYAML implements yaml.Unmarshaler for Config.39func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {40*c = DefaultConfig4142type plain Config43return unmarshal((*plain)(c))44}4546// Name returns the name of the integration.47func (c *Config) Name() string {48return "snmp"49}5051// InstanceKey returns the hostname:port of the agent.52func (c *Config) InstanceKey(agentKey string) (string, error) {53return agentKey, nil54}5556// NewIntegration creates a new SNMP integration.57func (c *Config) NewIntegration(l log.Logger) (integrations.Integration, error) {58return New(l, c)59}6061func init() {62integrations.RegisterIntegration(&Config{})63}6465// New creates a new snmp_exporter integration66func New(log log.Logger, c *Config) (integrations.Integration, error) {67var modules *snmp_config.Config68var err error69if c.SnmpConfigFile != "" {70modules, err = snmp_config.LoadFile(c.SnmpConfigFile)71if err != nil {72return nil, fmt.Errorf("failed to load snmp config from file %v: %w", c.SnmpConfigFile, err)73}74} else {75modules, err = snmp_common.LoadEmbeddedConfig()76if err != nil {77return nil, fmt.Errorf("failed to load embedded snmp config: %w", err)78}79}8081// The `name` and `address` fields are mandatory for the SNMP targets are mandatory.82// Enforce this check and fail the creation of the integration if they're missing.83for _, target := range c.SnmpTargets {84if target.Name == "" || target.Target == "" {85return nil, fmt.Errorf("failed to load snmp_targets; the `name` and `address` fields are mandatory")86}87}8889sh := &snmpHandler{90cfg: c,91modules: modules,92log: log,93}94integration := &Integration{95sh: sh,96}9798return integration, nil99}100101// Integration is the SNMP integration. The integration scrapes metrics102// from the host Linux-based system.103type Integration struct {104sh *snmpHandler105}106107// MetricsHandler implements Integration.108func (i *Integration) MetricsHandler() (http.Handler, error) {109return i.sh, nil110}111112// Run satisfies Integration.Run.113func (i *Integration) Run(ctx context.Context) error {114// We don't need to do anything here, so we can just wait for the context to115// finish.116<-ctx.Done()117return ctx.Err()118}119120// ScrapeConfigs satisfies Integration.ScrapeConfigs.121func (i *Integration) ScrapeConfigs() []config.ScrapeConfig {122var res []config.ScrapeConfig123for _, target := range i.sh.cfg.SnmpTargets {124queryParams := url.Values{}125queryParams.Add("target", target.Target)126if target.Module != "" {127queryParams.Add("module", target.Module)128}129if target.WalkParams != "" {130queryParams.Add("walk_params", target.WalkParams)131}132res = append(res, config.ScrapeConfig{133JobName: i.sh.cfg.Name() + "/" + target.Name,134MetricsPath: "/metrics",135QueryParams: queryParams,136})137}138return res139}140141142