Skip to content

Commit

Permalink
[snmp] Add agent_group tag for snmp integration (#32004)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreYang authored Dec 13, 2024
1 parent 7c5ac02 commit bbbad32
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 35 deletions.
26 changes: 26 additions & 0 deletions comp/haagent/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.

// Package haagenthelpers provides helpers for haagent component
package haagenthelpers

import (
"github.com/DataDog/datadog-agent/pkg/config/model"
)

// IsEnabled returns true if HA Agent is enabled
func IsEnabled(agentConfig model.Reader) bool {
return agentConfig.GetBool("ha_agent.enabled")
}

// GetGroup returns HA Agent group
func GetGroup(agentConfig model.Reader) string {
return agentConfig.GetString("ha_agent.group")
}

// GetHaAgentTags returns HA Agent related tags
func GetHaAgentTags(agentConfig model.Reader) []string {
return []string{"agent_group:" + GetGroup(agentConfig)}
}
33 changes: 33 additions & 0 deletions comp/haagent/helpers/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.

package haagenthelpers

import (
"testing"

"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/stretchr/testify/assert"
)

func TestIsEnabled(t *testing.T) {
cfg := config.NewMock(t)
assert.False(t, IsEnabled(cfg))

cfg.SetWithoutSource("ha_agent.enabled", true)
assert.True(t, IsEnabled(cfg))
}

func TestGetGroup(t *testing.T) {
cfg := config.NewMock(t)
cfg.SetWithoutSource("ha_agent.group", "my-group")
assert.Equal(t, "my-group", GetGroup(cfg))
}

func TestGetHaAgentTags(t *testing.T) {
cfg := config.NewMock(t)
cfg.SetWithoutSource("ha_agent.group", "my-group")
assert.Equal(t, []string{"agent_group:my-group"}, GetHaAgentTags(cfg))
}
5 changes: 3 additions & 2 deletions comp/haagent/impl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package haagentimpl

import (
"github.com/DataDog/datadog-agent/comp/core/config"
helpers "github.com/DataDog/datadog-agent/comp/haagent/helpers"
)

// validHaIntegrations represent the list of integrations that will be considered as
Expand All @@ -30,7 +31,7 @@ type haAgentConfigs struct {

func newHaAgentConfigs(agentConfig config.Component) *haAgentConfigs {
return &haAgentConfigs{
enabled: agentConfig.GetBool("ha_agent.enabled"),
group: agentConfig.GetString("ha_agent.group"),
enabled: helpers.IsEnabled(agentConfig),
group: helpers.GetGroup(agentConfig),
}
}
5 changes: 3 additions & 2 deletions comp/metadata/host/hostimpl/hosttags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"time"

haagenthelpers "github.com/DataDog/datadog-agent/comp/haagent/helpers"
"github.com/DataDog/datadog-agent/pkg/config/env"
"github.com/DataDog/datadog-agent/pkg/config/model"
configUtils "github.com/DataDog/datadog-agent/pkg/config/utils"
Expand Down Expand Up @@ -133,8 +134,8 @@ func Get(ctx context.Context, cached bool, conf model.Reader) *Tags {
hostTags = appendToHostTags(hostTags, clusterNameTags)
}

if conf.GetBool("ha_agent.enabled") {
hostTags = appendToHostTags(hostTags, []string{"agent_group:" + conf.GetString("ha_agent.group")})
if haagenthelpers.IsEnabled(conf) {
hostTags = appendToHostTags(hostTags, haagenthelpers.GetHaAgentTags(conf))
}

gceTags := []string{}
Expand Down
17 changes: 14 additions & 3 deletions pkg/collector/corechecks/snmp/internal/devicecheck/devicecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (

"go.uber.org/atomic"

"github.com/DataDog/datadog-agent/comp/core/config"
haagenthelpers "github.com/DataDog/datadog-agent/comp/haagent/helpers"
"github.com/DataDog/datadog-agent/pkg/collector/externalhost"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
configUtils "github.com/DataDog/datadog-agent/pkg/config/utils"
"github.com/DataDog/datadog-agent/pkg/metrics/servicecheck"
"github.com/DataDog/datadog-agent/pkg/util/hostname/validate"
Expand Down Expand Up @@ -66,12 +67,13 @@ type DeviceCheck struct {
diagnoses *diagnoses.Diagnoses
interfaceBandwidthState report.InterfaceBandwidthState
cacheKey string
agentConfig config.Component
}

const cacheKeyPrefix = "snmp-tags"

// NewDeviceCheck returns a new DeviceCheck
func NewDeviceCheck(config *checkconfig.CheckConfig, ipAddress string, sessionFactory session.Factory) (*DeviceCheck, error) {
func NewDeviceCheck(config *checkconfig.CheckConfig, ipAddress string, sessionFactory session.Factory, agentConfig config.Component) (*DeviceCheck, error) {
newConfig := config.CopyWithNewIP(ipAddress)

var devicePinger pinger.Pinger
Expand All @@ -94,6 +96,7 @@ func NewDeviceCheck(config *checkconfig.CheckConfig, ipAddress string, sessionFa
diagnoses: diagnoses.NewDeviceDiagnoses(newConfig.DeviceID),
interfaceBandwidthState: report.MakeInterfaceBandwidthState(),
cacheKey: cacheKey,
agentConfig: agentConfig,
}

d.readTagsFromCache()
Expand Down Expand Up @@ -244,11 +247,19 @@ func (d *DeviceCheck) setDeviceHostExternalTags() {
if deviceHostname == "" || err != nil {
return
}
agentTags := configUtils.GetConfiguredTags(pkgconfigsetup.Datadog(), false)
agentTags := d.buildExternalTags()
log.Debugf("Set external tags for device host, host=`%s`, agentTags=`%v`", deviceHostname, agentTags)
externalhost.SetExternalTags(deviceHostname, common.SnmpExternalTagsSourceType, agentTags)
}

func (d *DeviceCheck) buildExternalTags() []string {
agentTags := configUtils.GetConfiguredTags(d.agentConfig, false)
if haagenthelpers.IsEnabled(d.agentConfig) {
agentTags = append(agentTags, haagenthelpers.GetHaAgentTags(d.agentConfig)...)
}
return agentTags
}

func (d *DeviceCheck) getValuesAndTags() (bool, []string, *valuestore.ResultValueStore, error) {
var deviceReachable bool
var checkErrors []string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

agentconfig "github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/pkg/aggregator/mocksender"
"github.com/DataDog/datadog-agent/pkg/metrics/servicecheck"
"github.com/DataDog/datadog-agent/pkg/version"
Expand Down Expand Up @@ -57,7 +58,7 @@ profiles:
config, err := checkconfig.NewCheckConfig(rawInstanceConfig, rawInitConfig)
assert.Nil(t, err)

deviceCk, err := NewDeviceCheck(config, "1.2.3.4", sessionFactory)
deviceCk, err := NewDeviceCheck(config, "1.2.3.4", sessionFactory, agentconfig.NewMock(t))
assert.Nil(t, err)

sender := mocksender.NewMockSender("123") // required to initiate aggregator
Expand Down Expand Up @@ -197,7 +198,7 @@ global_metrics:
config, err := checkconfig.NewCheckConfig(rawInstanceConfig, rawInitConfig)
assert.Nil(t, err)

deviceCk, err := NewDeviceCheck(config, "1.2.3.4", sessionFactory)
deviceCk, err := NewDeviceCheck(config, "1.2.3.4", sessionFactory, agentconfig.NewMock(t))
assert.Nil(t, err)

sender := mocksender.NewMockSender("123") // required to initiate aggregator
Expand Down Expand Up @@ -246,7 +247,7 @@ community_string: public
config, err := checkconfig.NewCheckConfig(rawInstanceConfig, rawInitConfig)
assert.Nil(t, err)

deviceCk, err := NewDeviceCheck(config, "1.2.3.4", session.NewMockSession)
deviceCk, err := NewDeviceCheck(config, "1.2.3.4", session.NewMockSession, agentconfig.NewMock(t))
assert.Nil(t, err)

sender := mocksender.NewMockSender("123") // required to initiate aggregator
Expand Down Expand Up @@ -277,7 +278,7 @@ community_string: public
config, err := checkconfig.NewCheckConfig(rawInstanceConfig, rawInitConfig)
assert.Nil(t, err)

deviceCk, err := NewDeviceCheck(config, "1.2.3.4", session.NewMockSession)
deviceCk, err := NewDeviceCheck(config, "1.2.3.4", session.NewMockSession, agentconfig.NewMock(t))
assert.Nil(t, err)

hostname, err := deviceCk.GetDeviceHostname()
Expand Down Expand Up @@ -343,7 +344,7 @@ profiles:
config, err := checkconfig.NewCheckConfig(rawInstanceConfig, rawInitConfig)
assert.Nil(t, err)

deviceCk, err := NewDeviceCheck(config, "1.2.3.4", sessionFactory)
deviceCk, err := NewDeviceCheck(config, "1.2.3.4", sessionFactory, agentconfig.NewMock(t))
assert.Nil(t, err)

snmpTags := []string{"snmp_device:1.2.3.4", "device_ip:1.2.3.4", "device_id:default:1.2.3.4", "snmp_profile:f5-big-ip", "device_vendor:f5", "snmp_host:foo_sys_name",
Expand Down Expand Up @@ -648,7 +649,7 @@ profiles:
config, err := checkconfig.NewCheckConfig(rawInstanceConfig, rawInitConfig)
assert.Nil(t, err)

deviceCk, err := NewDeviceCheck(config, "1.2.3.4", sessionFactory)
deviceCk, err := NewDeviceCheck(config, "1.2.3.4", sessionFactory, agentconfig.NewMock(t))
assert.Nil(t, err)

sender := mocksender.NewMockSender("123") // required to initiate aggregator
Expand Down Expand Up @@ -695,7 +696,7 @@ profiles:
config, err := checkconfig.NewCheckConfig(rawInstanceConfig, rawInitConfig)
assert.Nil(t, err)

deviceCk, err := NewDeviceCheck(config, "1.2.3.4", sessionFactory)
deviceCk, err := NewDeviceCheck(config, "1.2.3.4", sessionFactory, agentconfig.NewMock(t))
assert.Nil(t, err)

// override pinger with mock pinger
Expand Down Expand Up @@ -846,7 +847,7 @@ profiles:
config, err := checkconfig.NewCheckConfig(rawInstanceConfig, rawInitConfig)
assert.Nil(t, err)

deviceCk, err := NewDeviceCheck(config, "1.2.3.4", sessionFactory)
deviceCk, err := NewDeviceCheck(config, "1.2.3.4", sessionFactory, agentconfig.NewMock(t))
assert.Nil(t, err)

// override pinger with mock pinger
Expand Down Expand Up @@ -967,3 +968,37 @@ profiles:
sender.AssertNotCalled(t, "Gauge", pingAvgRttMetric, mock.Anything, mock.Anything, mock.Anything)
sender.AssertNotCalled(t, "Gauge", pingPacketLoss, mock.Anything, mock.Anything, mock.Anything)
}

func TestDeviceCheck_buildExternalTags(t *testing.T) {
// GIVEN
profile.SetConfdPathAndCleanProfiles()
sess := session.CreateFakeSession()
sessionFactory := func(*checkconfig.CheckConfig) (session.Session, error) {
return sess, nil
}

// language=yaml
rawInstanceConfig := []byte(`
ip_address: 1.2.3.4
community_string: public
collect_topology: false
`)
// language=yaml
rawInitConfig := []byte(``)

config, err := checkconfig.NewCheckConfig(rawInstanceConfig, rawInitConfig)
assert.Nil(t, err)

cfg := agentconfig.NewMock(t)
cfg.SetWithoutSource("ha_agent.enabled", true)
cfg.SetWithoutSource("ha_agent.group", "my-group")

deviceCk, err := NewDeviceCheck(config, "1.2.3.4", sessionFactory, cfg)
assert.Nil(t, err)

// WHEN
externalTags := deviceCk.buildExternalTags()

// THEN
assert.Equal(t, []string{"agent_group:my-group"}, externalTags)
}
7 changes: 5 additions & 2 deletions pkg/collector/corechecks/snmp/internal/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"sync"
"time"

"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/pkg/persistentcache"
"github.com/DataDog/datadog-agent/pkg/util/log"
"go.uber.org/atomic"
Expand Down Expand Up @@ -43,6 +44,7 @@ type Discovery struct {
discoveredDevices map[checkconfig.DeviceDigest]Device

sessionFactory session.Factory
agentConfig config.Component
}

// Device implements and store results from the Service interface for the SNMP listener
Expand Down Expand Up @@ -237,7 +239,7 @@ func (d *Discovery) getDevicesFound() []string {
}

func (d *Discovery) createDevice(deviceDigest checkconfig.DeviceDigest, subnet *snmpSubnet, deviceIP string, writeCache bool) {
deviceCk, err := devicecheck.NewDeviceCheck(subnet.config, deviceIP, d.sessionFactory)
deviceCk, err := devicecheck.NewDeviceCheck(subnet.config, deviceIP, d.sessionFactory, d.agentConfig)
if err != nil {
// should not happen since the deviceCheck is expected to be valid at this point
// and are only changing the device ip
Expand Down Expand Up @@ -335,11 +337,12 @@ func (d *Discovery) writeCache(subnet *snmpSubnet) {
}

// NewDiscovery return a new Discovery instance
func NewDiscovery(config *checkconfig.CheckConfig, sessionFactory session.Factory) *Discovery {
func NewDiscovery(config *checkconfig.CheckConfig, sessionFactory session.Factory, agentConfig config.Component) *Discovery {
return &Discovery{
discoveredDevices: make(map[checkconfig.DeviceDigest]Device),
stop: make(chan struct{}),
config: config,
sessionFactory: sessionFactory,
agentConfig: agentConfig,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/gosnmp/gosnmp"
"github.com/stretchr/testify/assert"

agentconfig "github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/snmp/internal/checkconfig"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/snmp/internal/session"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
Expand Down Expand Up @@ -59,7 +60,7 @@ func TestDiscovery(t *testing.T) {
DiscoveryWorkers: 1,
IgnoredIPAddresses: map[string]bool{"192.168.0.5": true},
}
discovery := NewDiscovery(checkConfig, sessionFactory)
discovery := NewDiscovery(checkConfig, sessionFactory, agentconfig.NewMock(t))
discovery.Start()
assert.NoError(t, waitForDiscoveredDevices(discovery, 7, 2*time.Second))
discovery.Stop()
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestDiscoveryCache(t *testing.T) {
DiscoveryInterval: 3600,
DiscoveryWorkers: 1,
}
discovery := NewDiscovery(checkConfig, sessionFactory)
discovery := NewDiscovery(checkConfig, sessionFactory, agentconfig.NewMock(t))
discovery.Start()
assert.NoError(t, waitForDiscoveredDevices(discovery, 4, 2*time.Second))
discovery.Stop()
Expand Down Expand Up @@ -141,7 +142,7 @@ func TestDiscoveryCache(t *testing.T) {
DiscoveryInterval: 3600,
DiscoveryWorkers: 0, // no workers, the devices will be loaded from cache
}
discovery2 := NewDiscovery(checkConfig, sessionFactory)
discovery2 := NewDiscovery(checkConfig, sessionFactory, agentconfig.NewMock(t))
discovery2.Start()
assert.NoError(t, waitForDiscoveredDevices(discovery2, 4, 2*time.Second))
discovery2.Stop()
Expand Down Expand Up @@ -180,7 +181,7 @@ func TestDiscoveryTicker(t *testing.T) {
DiscoveryInterval: 1,
DiscoveryWorkers: 1,
}
discovery := NewDiscovery(checkConfig, sessionFactory)
discovery := NewDiscovery(checkConfig, sessionFactory, agentconfig.NewMock(t))
discovery.Start()
time.Sleep(1500 * time.Millisecond)
discovery.Stop()
Expand Down Expand Up @@ -227,7 +228,7 @@ func TestDiscovery_checkDevice(t *testing.T) {
}

var sess *session.MockSession
discovery := NewDiscovery(checkConfig, session.NewMockSession)
discovery := NewDiscovery(checkConfig, session.NewMockSession, agentconfig.NewMock(t))

checkDeviceOnce := func() {
sess = session.CreateMockSession()
Expand Down Expand Up @@ -315,7 +316,7 @@ func TestDiscovery_createDevice(t *testing.T) {
DiscoveryAllowedFailures: 3,
Namespace: "default",
}
discovery := NewDiscovery(checkConfig, session.NewMockSession)
discovery := NewDiscovery(checkConfig, session.NewMockSession, agentconfig.NewMock(t))
ipAddr, ipNet, err := net.ParseCIDR(checkConfig.Network)
assert.Nil(t, err)
startingIP := ipAddr.Mask(ipNet.Mask)
Expand Down
Loading

0 comments on commit bbbad32

Please sign in to comment.