-
Notifications
You must be signed in to change notification settings - Fork 180
/
events.go
112 lines (91 loc) · 3.44 KB
/
events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package events
import (
"fmt"
"strconv"
"github.com/aws/amazon-cloudwatch-agent/tool/data"
"github.com/aws/amazon-cloudwatch-agent/tool/processors"
"github.com/aws/amazon-cloudwatch-agent/tool/processors/tracesconfig"
"github.com/aws/amazon-cloudwatch-agent/tool/runtime"
"github.com/aws/amazon-cloudwatch-agent/tool/util"
"github.com/aws/amazon-cloudwatch-agent/translator"
)
const (
VERBOSE = "VERBOSE" //5
INFORMATION = "INFORMATION" //4
WARNING = "WARNING" //3
ERROR = "ERROR" //2
CRITICAL = "CRITICAL" //1
WindowsEventLog = "Windows event log"
EventFormatXMLDescription = "XML: XML format in Windows Event Viewer"
EventFormatPlainTextDescription = "Plain Text: Legacy CloudWatch Windows Agent (SSM Plugin) Format"
EventFormatXML = "xml"
EventFormatPlainText = "text"
)
var Processor processors.Processor = &processor{}
type processor struct{}
func (p *processor) Process(ctx *runtime.Context, config *data.Config) {
monitorEvents(ctx, config)
}
func (p *processor) NextProcessor(ctx *runtime.Context, config *data.Config) interface{} {
return tracesconfig.Processor
}
func monitorEvents(ctx *runtime.Context, config *data.Config) {
yes := util.Yes(fmt.Sprintf("Do you want to monitor any %s?", WindowsEventLog))
if !yes {
return
}
eventFormatDefaultOption := 1
for {
logsConf := config.LogsConf()
eventName := util.AskWithDefault(fmt.Sprintf("%s name:", WindowsEventLog), "System")
availableEventLevels := []string{VERBOSE, INFORMATION, WARNING, ERROR, CRITICAL}
eventLevels := []string{}
for _, eventLevel := range availableEventLevels {
yes = util.Yes(
fmt.Sprintf("Do you want to monitor %s level events for %s %s ?",
eventLevel,
WindowsEventLog,
eventName))
if yes {
eventLevels = append(eventLevels, eventLevel)
}
}
logGroupName := util.AskWithDefault("Log group name:", eventName)
logStreamNameHint := "{instance_id}"
if ctx.IsOnPrem {
logStreamNameHint = "{hostname}"
}
logStreamName := util.AskWithDefault("Log stream name:", logStreamNameHint)
logGroupDefaultOption := 1
logGroupClass := util.Choice("Which log group class would you like to have for this log group?", logGroupDefaultOption, []string{util.StandardLogGroupClass, util.InfrequentAccessLogGroupClass})
if logGroupClass == util.StandardLogGroupClass {
logGroupClass = util.StandardLogGroupClass
logGroupDefaultOption = 1
} else {
logGroupClass = util.InfrequentAccessLogGroupClass
logGroupDefaultOption = 2
}
eventFormat := util.Choice("In which format do you want to store windows event to CloudWatch Logs?", eventFormatDefaultOption, []string{EventFormatXMLDescription, EventFormatPlainTextDescription})
if eventFormat == EventFormatXMLDescription {
eventFormat = EventFormatXML
eventFormatDefaultOption = 1
} else {
eventFormat = EventFormatPlainText
eventFormatDefaultOption = 2
}
keys := translator.ValidRetentionInDays
retentionInDays := util.Choice("Log Group Retention in days", 1, keys)
retention := -1
i, err := strconv.Atoi(retentionInDays)
if err == nil {
retention = i
}
logsConf.AddWindowsEvent(eventName, logGroupName, logStreamName, eventFormat, eventLevels, retention, logGroupClass)
yes = util.Yes(fmt.Sprintf("Do you want to specify any additional %s to monitor?", WindowsEventLog))
if !yes {
return
}
}
}