Skip to content

Commit

Permalink
Allow rdsquerier to run in snmp core check
Browse files Browse the repository at this point in the history
  • Loading branch information
vicweiss committed Oct 9, 2024
1 parent 2abc3be commit 28291f0
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmd/agent/subcommands/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ import (
"github.com/DataDog/datadog-agent/pkg/util/optional"
"github.com/DataDog/datadog-agent/pkg/version"

rdnsquerier "github.com/DataDog/datadog-agent/comp/rdnsquerier/def"

// runtime init routines
ddruntime "github.com/DataDog/datadog-agent/pkg/runtime"
)
Expand Down Expand Up @@ -255,6 +257,7 @@ func run(log log.Component,
settings settings.Component,
_ optional.Option[gui.Component],
_ agenttelemetry.Component,
rdnsquerier rdnsquerier.Component,
) error {
defer func() {
stopAgent()
Expand Down Expand Up @@ -319,6 +322,7 @@ func run(log log.Component,
cloudfoundrycontainer,
jmxlogger,
settings,
rdnsquerier,
); err != nil {
return err
}
Expand Down Expand Up @@ -494,6 +498,7 @@ func startAgent(
_ cloudfoundrycontainer.Component,
jmxLogger jmxlogger.Component,
settings settings.Component,
rdnsquerier rdnsquerier.Component,
) error {
var err error

Expand Down Expand Up @@ -571,6 +576,7 @@ func startAgent(

// TODO: (components) - Until the checks are components we set there context so they can depends on components.
check.InitializeInventoryChecksContext(invChecks)
check.InitializeRDNSQuerierContext(rdnsquerier)

// Init JMX runner and inject dogstatsd component
jmxfetch.InitRunner(server, jmxLogger)
Expand Down
4 changes: 4 additions & 0 deletions pkg/cli/subcommands/check/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import (
integrations "github.com/DataDog/datadog-agent/comp/logs/integrations/def"
"github.com/DataDog/datadog-agent/comp/metadata/inventorychecks"
"github.com/DataDog/datadog-agent/comp/metadata/inventorychecks/inventorychecksimpl"
rdnsquerier "github.com/DataDog/datadog-agent/comp/rdnsquerier/def"
"github.com/DataDog/datadog-agent/comp/remote-config/rcservice"
"github.com/DataDog/datadog-agent/comp/remote-config/rcservicemrf"
"github.com/DataDog/datadog-agent/comp/serializer/compression/compressionimpl"
Expand Down Expand Up @@ -264,6 +265,7 @@ func run(
jmxLogger jmxlogger.Component,
telemetry telemetry.Component,
logReceiver optional.Option[integrations.Component],
rdnsquerier rdnsquerier.Component,
) error {
previousIntegrationTracing := false
previousIntegrationTracingExhaustive := false
Expand All @@ -288,6 +290,8 @@ func run(

// TODO: (components) - Until the checks are components we set there context so they can depends on components.
check.InitializeInventoryChecksContext(invChecks)
check.InitializeRDNSQuerierContext(rdnsquerier)

pkgcollector.InitPython(common.GetPythonPaths()...)
commonchecks.RegisterChecks(wmeta, config, telemetry)

Expand Down
26 changes: 25 additions & 1 deletion pkg/collector/check/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"

"github.com/DataDog/datadog-agent/comp/metadata/inventorychecks"
rdnsquerier "github.com/DataDog/datadog-agent/comp/rdnsquerier/def"
)

// checkContext holds a list of reference to different components used by Go and Python checks.
Expand All @@ -20,7 +21,8 @@ import (
// of C to Go. This way python checks can submit metadata to inventorychecks through the 'SetCheckMetadata' python
// method.
type checkContext struct {
ic inventorychecks.Component
ic inventorychecks.Component
rdnsquerier rdnsquerier.Component
}

var ctx checkContext
Expand All @@ -47,10 +49,32 @@ func InitializeInventoryChecksContext(ic inventorychecks.Component) {
}
}

// GetRDNSQuerierContext returns a reference to the rdnsquerier component for Python and Go checks to use.
func GetRDNSQuerierContext() (rdnsquerier.Component, error) {
checkContextMutex.Lock()
defer checkContextMutex.Unlock()

if ctx.rdnsquerier == nil {
return nil, errors.New("rdnsquerier context was not set")
}
return ctx.rdnsquerier, nil
}

// InitializeRDNSQuerierContext set the reference to rdnsquerier in checkContext
func InitializeRDNSQuerierContext(rdnsquerier rdnsquerier.Component) {
checkContextMutex.Lock()
defer checkContextMutex.Unlock()

if ctx.rdnsquerier == nil {
ctx.rdnsquerier = rdnsquerier
}
}

// ReleaseContext reset to nil all the references hold by the current context
func ReleaseContext() {
checkContextMutex.Lock()
defer checkContextMutex.Unlock()

ctx.ic = nil
ctx.rdnsquerier = nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"time"

"github.com/DataDog/datadog-agent/comp/forwarder/eventplatform"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/networkdevice/profile/profiledefinition"
"github.com/DataDog/datadog-agent/pkg/networkdevice/utils"

"github.com/DataDog/datadog-agent/pkg/collector/check"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/snmp/internal/checkconfig"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/snmp/internal/common"
"github.com/DataDog/datadog-agent/pkg/collector/corechecks/snmp/internal/lldp"
Expand Down Expand Up @@ -214,6 +216,8 @@ func buildNetworkDeviceMetadata(deviceID string, idTags []string, config *checkc
vendor = config.ProfileDef.Device.Vendor
}

hostname := lookupHostnameWithRDNS(config.IPAddress)

return devicemetadata.DeviceMetadata{
ID: deviceID,
IDTags: idTags,
Expand All @@ -238,9 +242,37 @@ func buildNetworkDeviceMetadata(deviceID string, idTags []string, config *checkc
OsHostname: osHostname,
DeviceType: deviceType,
Integration: common.SnmpIntegrationName,
RDNSHostname: hostname,
}
}

func lookupHostnameWithRDNS(ip string) string {
if rdnsquerier, err := check.GetRDNSQuerierContext(); err == nil {
var hostname string
var wg sync.WaitGroup

wg.Add(1)
err := rdnsquerier.GetHostname(
net.ParseIP(ip).To4(),
func(h string) {
hostname = h
wg.Done()
},
func(h string, err error) {
hostname = h
wg.Done()
},
)
if err != nil {
log.Tracef("Error resolving reverse DNS enrichment for source IP address: %v error: %v", ip, err)
wg.Done()
}
wg.Wait()
return hostname
}
return ""
}

func getProfileVersion(config *checkconfig.CheckConfig) uint64 {
var profileVersion uint64
if config.ProfileDef != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/networkdevice/metadata/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type DeviceMetadata struct {
OsHostname string `json:"os_hostname,omitempty"`
Integration string `json:"integration,omitempty"` // indicates the source of the data SNMP, meraki_api, etc.
DeviceType string `json:"device_type,omitempty"`
RDNSHostname string `json:"rdns_hostname,omitempty"`
}

// DeviceOID device scan oid data
Expand Down

0 comments on commit 28291f0

Please sign in to comment.