Path: blob/main/component/prometheus/exporter/snmp/snmp.go
4096 views
package snmp12import (3"time"45"github.com/grafana/agent/component"6"github.com/grafana/agent/component/discovery"7"github.com/grafana/agent/component/prometheus/exporter"8"github.com/grafana/agent/pkg/integrations"9"github.com/grafana/agent/pkg/integrations/snmp_exporter"10"github.com/grafana/agent/pkg/river/rivertypes"11snmp_config "github.com/prometheus/snmp_exporter/config"12)1314func init() {15component.Register(component.Registration{16Name: "prometheus.exporter.snmp",17Args: Arguments{},18Exports: exporter.Exports{},19Build: exporter.NewMultiTarget(createExporter, "snmp", buildSNMPTargets),20})21}2223func createExporter(opts component.Options, args component.Arguments) (integrations.Integration, error) {24a := args.(Arguments)25return a.Convert().NewIntegration(opts.Logger)26}2728// buildSNMPTargets creates the exporter's discovery targets based on the defined SNMP targets.29func buildSNMPTargets(baseTarget discovery.Target, args component.Arguments) []discovery.Target {30var targets []discovery.Target3132a := args.(Arguments)33for _, tgt := range a.Targets {34target := make(discovery.Target)35for k, v := range baseTarget {36target[k] = v37}3839target["job"] = target["job"] + "/" + tgt.Name40target["__param_target"] = tgt.Target41if tgt.Module != "" {42target["__param_module"] = tgt.Module43}44if tgt.WalkParams != "" {45target["__param_walk_params"] = tgt.WalkParams46}4748targets = append(targets, target)49}5051return targets52}5354// SNMPTarget defines a target to be used by the exporter.55type SNMPTarget struct {56Name string `river:",label"`57Target string `river:"address,attr"`58Module string `river:"module,attr,optional"`59WalkParams string `river:"walk_params,attr,optional"`60}6162type TargetBlock []SNMPTarget6364// Convert converts the component's TargetBlock to a slice of integration's SNMPTarget.65func (t TargetBlock) Convert() []snmp_exporter.SNMPTarget {66targets := make([]snmp_exporter.SNMPTarget, 0, len(t))67for _, target := range t {68targets = append(targets, snmp_exporter.SNMPTarget{69Name: target.Name,70Target: target.Target,71Module: target.Module,72WalkParams: target.WalkParams,73})74}75return targets76}7778type Auth struct {79Community rivertypes.Secret `river:"community,attr,optional"`80SecurityLevel string `river:"security_level,attr,optional"`81Username string `river:"username,attr,optional"`82Password rivertypes.Secret `river:"password,attr,optional"`83AuthProtocol string `river:"auth_protocol,attr,optional"`84PrivProtocol string `river:"priv_protocol,attr,optional"`85PrivPassword rivertypes.Secret `river:"priv_password,attr,optional"`86ContextName string `river:"context_name,attr,optional"`87}8889// Convert converts the component's Auth to the integration's Auth.90func (a Auth) Convert() snmp_config.Auth {91return snmp_config.Auth{92Community: snmp_config.Secret(a.Community),93SecurityLevel: a.SecurityLevel,94Username: a.Username,95Password: snmp_config.Secret(a.Password),96AuthProtocol: a.AuthProtocol,97PrivProtocol: a.PrivProtocol,98PrivPassword: snmp_config.Secret(a.PrivPassword),99ContextName: a.ContextName,100}101}102103type WalkParam struct {104Name string `river:",label"`105Version int `river:"version,attr,optional"`106MaxRepetitions uint32 `river:"max_repetitions,attr,optional"`107Retries int `river:"retries,attr,optional"`108Timeout time.Duration `river:"timeout,attr,optional"`109Auth Auth `river:"auth,block,optional"`110UseUnconnectedUDPSocket bool `river:"use_unconnected_udp_socket,attr,optional"`111}112113type WalkParams []WalkParam114115// Convert converts the component's WalkParams to the integration's WalkParams.116func (w WalkParams) Convert() map[string]snmp_config.WalkParams {117walkParams := make(map[string]snmp_config.WalkParams)118for _, walkParam := range w {119walkParams[walkParam.Name] = snmp_config.WalkParams{120Version: walkParam.Version,121MaxRepetitions: walkParam.MaxRepetitions,122Retries: walkParam.Retries,123Timeout: walkParam.Timeout,124Auth: walkParam.Auth.Convert(),125UseUnconnectedUDPSocket: walkParam.UseUnconnectedUDPSocket,126}127}128return walkParams129}130131type Arguments struct {132ConfigFile string `river:"config_file,attr"`133Targets TargetBlock `river:"target,block"`134WalkParams WalkParams `river:"walk_param,block,optional"`135}136137// UnmarshalRiver implements River unmarshalling for Arguments.138func (a *Arguments) UnmarshalRiver(f func(interface{}) error) error {139type args Arguments140return f((*args)(a))141}142143// Convert converts the component's Arguments to the integration's Config.144func (a *Arguments) Convert() *snmp_exporter.Config {145return &snmp_exporter.Config{146SnmpConfigFile: a.ConfigFile,147SnmpTargets: a.Targets.Convert(),148WalkParams: a.WalkParams.Convert(),149}150}151152153