diff --git a/pkg/receiver/activedirectoryinvreceiver/README.md b/pkg/receiver/activedirectoryinvreceiver/README.md index fcc1171d8f..a63a6e22ea 100644 --- a/pkg/receiver/activedirectoryinvreceiver/README.md +++ b/pkg/receiver/activedirectoryinvreceiver/README.md @@ -15,11 +15,12 @@ receivers: base_dn: "CN=Users,DC=exampledomain,DC=com" # User attributes + # default = [name, mail, department, manager, memberOf] attributes: [name, mail, department, manager, memberOf] # The polling interval. - # default = 60 - poll_interval: 60 + # default = 24h + poll_interval: 24h ``` The full list of settings exposed for this receiver are documented in @@ -33,7 +34,7 @@ receivers: activedirectoryinv: base_dn: "CN=Users,DC=exampledomain,DC=com" attributes: [name, mail, department, manager, memberOf] - poll_interval: 60 + poll_interval: 24h exporters: logging: diff --git a/pkg/receiver/activedirectoryinvreceiver/adinvreceiver.go b/pkg/receiver/activedirectoryinvreceiver/adinvreceiver.go index e4487067aa..2ff4d2254d 100644 --- a/pkg/receiver/activedirectoryinvreceiver/adinvreceiver.go +++ b/pkg/receiver/activedirectoryinvreceiver/adinvreceiver.go @@ -17,6 +17,7 @@ package activedirectoryinvreceiver import ( "context" "fmt" + "runtime" "sync" "time" @@ -79,6 +80,9 @@ func newLogsReceiver(cfg *ADConfig, logger *zap.Logger, client Client, consumer // Start the logs receiver func (l *ADReceiver) Start(ctx context.Context, _ component.Host) error { + if !supportedOS() { + return errSupportedOS + } l.logger.Debug("Starting to poll for active directory inventory records") l.wg.Add(1) go l.startPolling(ctx) @@ -96,7 +100,13 @@ func (l *ADReceiver) Shutdown(_ context.Context) error { // Start polling for Active Directory inventory records func (l *ADReceiver) startPolling(ctx context.Context) { defer l.wg.Done() - t := time.NewTicker(l.config.PollInterval * time.Second) + duration, err := time.ParseDuration(l.config.PollInterval) + if err != nil { + l.logger.Error("Failed to parse poll interval", zap.Error(err)) + return + } + l.logger.Info("Polling interval: ", zap.Duration("interval", duration)) + t := time.NewTicker(duration) for { select { case <-ctx.Done(): @@ -145,10 +155,10 @@ func (r *ADReceiver) poll(ctx context.Context) error { resourceLogs := &rl _ = resourceLogs.ScopeLogs().AppendEmpty() root, err := r.client.Open(r.config.DN, resourceLogs) - r.traverse(root, r.config.Attributes, resourceLogs) if err != nil { return err } + r.traverse(root, r.config.Attributes, resourceLogs) err = r.consumer.ConsumeLogs(ctx, logs) if err != nil { r.logger.Error("Error consuming log", zap.Error(err)) @@ -171,3 +181,7 @@ func setUserAttributes(user Object, attrs []string, resourceLogs *plog.ResourceL logRecord.SetTimestamp(observedTime) logRecord.Body().SetStr(attributes) } + +func supportedOS() bool { + return (runtime.GOOS == "windows") +} diff --git a/pkg/receiver/activedirectoryinvreceiver/adinvreceiver_test.go b/pkg/receiver/activedirectoryinvreceiver/adinvreceiver_test.go index 20d3456cfc..df1be6d735 100644 --- a/pkg/receiver/activedirectoryinvreceiver/adinvreceiver_test.go +++ b/pkg/receiver/activedirectoryinvreceiver/adinvreceiver_test.go @@ -102,7 +102,7 @@ func TestStart(t *testing.T) { func TestPoll(t *testing.T) { cfg := CreateDefaultConfig().(*ADConfig) cfg.DN = "CN=Guest,CN=Users,DC=exampledomain,DC=com" - cfg.PollInterval = 1 + cfg.PollInterval = "1s" cfg.Attributes = []string{"name"} sink := &consumertest.LogsSink{} diff --git a/pkg/receiver/activedirectoryinvreceiver/config.go b/pkg/receiver/activedirectoryinvreceiver/config.go index 21d479ee79..da67bd3251 100644 --- a/pkg/receiver/activedirectoryinvreceiver/config.go +++ b/pkg/receiver/activedirectoryinvreceiver/config.go @@ -23,16 +23,22 @@ import ( // ADConfig defines configuration for Active Directory Inventory receiver. type ADConfig struct { - DN string `mapstructure:"base_dn"` // DN is the base distinguished name to search from - Attributes []string `mapstructure:"attributes"` - PollInterval time.Duration `mapstructure:"poll_interval"` + DN string `mapstructure:"base_dn"` // DN is the base distinguished name to search from + Attributes []string `mapstructure:"attributes"` + PollInterval string `mapstructure:"poll_interval"` } var ( errInvalidDN = errors.New("Base DN is incorrect, it must be in the format of CN=Users,DC=exampledomain,DC=com") - errInvalidPollInterval = errors.New("poll interval is incorrect, it must be a duration greater than one second") + errInvalidPollInterval = errors.New("poll interval is incorrect, invalid duration") + errSupportedOS = errors.New(typeStr + " is only supported on Windows.") ) +func isValidDuration(durationStr string) bool { + _, err := time.ParseDuration(durationStr) + return err == nil +} + // Validate validates all portions of the relevant config func (c *ADConfig) Validate() error { @@ -47,7 +53,7 @@ func (c *ADConfig) Validate() error { return errInvalidDN } - if c.PollInterval < 0 { + if !isValidDuration(c.PollInterval) { return errInvalidPollInterval } diff --git a/pkg/receiver/activedirectoryinvreceiver/config_test.go b/pkg/receiver/activedirectoryinvreceiver/config_test.go index 29452d3f48..9cce54064f 100644 --- a/pkg/receiver/activedirectoryinvreceiver/config_test.go +++ b/pkg/receiver/activedirectoryinvreceiver/config_test.go @@ -31,7 +31,7 @@ func TestValidate(t *testing.T) { config: ADConfig{ DN: "CN=Guest,CN=Users,DC=exampledomain,DC=com", Attributes: []string{"name"}, - PollInterval: 60, + PollInterval: "60s", }, }, { @@ -39,7 +39,7 @@ func TestValidate(t *testing.T) { config: ADConfig{ DN: "DC=exampledomain,DC=com", Attributes: []string{"name"}, - PollInterval: 60, + PollInterval: "60s", }, }, { @@ -47,7 +47,7 @@ func TestValidate(t *testing.T) { config: ADConfig{ DN: "CN=Guest,OU=Users,DC=exampledomain,DC=com", Attributes: []string{"name"}, - PollInterval: 60, + PollInterval: "24h", }, }, { @@ -62,7 +62,7 @@ func TestValidate(t *testing.T) { config: ADConfig{ DN: "CN=Users,DC=exampledomain,DC=com", Attributes: []string{"name"}, - PollInterval: -1, + PollInterval: "test", }, expectedErr: errInvalidPollInterval, }, diff --git a/pkg/receiver/activedirectoryinvreceiver/factory.go b/pkg/receiver/activedirectoryinvreceiver/factory.go index cd82693e62..3adb1f7006 100644 --- a/pkg/receiver/activedirectoryinvreceiver/factory.go +++ b/pkg/receiver/activedirectoryinvreceiver/factory.go @@ -40,8 +40,8 @@ func NewFactory() receiver.Factory { func CreateDefaultConfig() component.Config { return &ADConfig{ DN: "CN=Guest,CN=Users,DC=exampledomain,DC=com", - Attributes: []string{"name"}, - PollInterval: 60, + Attributes: []string{"name", "mail", "department", "manager", "memberOf"}, + PollInterval: "24h", } }