Skip to content

Commit

Permalink
Fix windows event logs to only starts once (aws#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
okankoAMZ authored Sep 15, 2023
1 parent 47e08aa commit 6ec4040
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions plugins/inputs/windows_event_log/windows_event_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/influxdata/telegraf"
Expand All @@ -25,6 +26,8 @@ const (
forcePullInterval = 250 * time.Millisecond
)

var startOnlyOnce sync.Once

type EventConfig struct {
Name string `toml:"event_name"`
Levels []string `toml:"event_levels"`
Expand Down Expand Up @@ -77,6 +80,13 @@ func (s *Plugin) FindLogSrc() []logs.LogSrc {
* We can do any initialization in this method.
*/
func (s *Plugin) Start(acc telegraf.Accumulator) error {
alreadyRan := true
startOnlyOnce.Do(func() {
alreadyRan = false
})
if alreadyRan {
return nil
}
for _, eventConfig := range s.Events {
// Assume no 2 EventConfigs have the same combination of:
// LogGroupName, LogStreamName, Name.
Expand Down
20 changes: 20 additions & 0 deletions plugins/inputs/windows_event_log/windows_event_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

// TestGetStateFilePathGood tests getStateFilePath with good input.
Expand Down Expand Up @@ -101,3 +103,21 @@ func TestGetStateFilePathSpecialChars(t *testing.T) {
t.Errorf("expected non-nil")
}
}

func TestWindowsDuplicateStart(t *testing.T) {
fileStateFolder := filepath.Join(t.TempDir(), "CloudWatchAgentTest")
plugin := Plugin{
FileStateFolder: fileStateFolder,
}
ec := EventConfig{
LogGroupName: "My Group/:::",
LogStreamName: "My::Stream// ",
Name: "System Event//Log::",
}
plugin.Events = append(plugin.Events, ec)
require.Equal(t, 0, len(plugin.newEvents), "Start should be ran only once so there should be only 1 new event")
plugin.Start(nil)
require.Equal(t, 1, len(plugin.newEvents), "Start should be ran only once so there should be only 1 new event")
plugin.Start(nil)
require.Equal(t, 1, len(plugin.newEvents), "Start should be ran only once so there should be only 1 new event")
}

0 comments on commit 6ec4040

Please sign in to comment.