-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path30_Initialize-CloudWatchLogging.ps1
141 lines (121 loc) · 5.34 KB
/
30_Initialize-CloudWatchLogging.ps1
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<#
.SYNOPSIS
Enables CloudWatch logging for different applications.
.DESCRIPTION
Configures the CloudWatch logging agent on the machine to push application logs from various locations to a specific log group.
Different log files or application logs are set up to be pushed to different log streams, facilitating easier identification of related logs.
.PARAMETER CloudWatchLogGroupName
(Optional) The name of the CloudWatch log group to which the application logs are pushed.
.EXAMPLE
Initialize-CloudWatchLogging -CloudWatchLogGroupName "<NAME_OF_CLOUDWATCH_LOG_GROUP>"
.Link
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-cloudwatch-agent-configuration-file.html
.NOTES
Copyright 2023-2024 The MathWorks, Inc.
#>
function Initialize-CloudWatchLogging {
param(
[Parameter(Mandatory = $false)]
[string] $CloudWatchLogGroupName
)
Write-Output 'Starting Initialize-CloudWatchLogging...'
$ConfigPath = "$Env:ProgramFiles\Amazon\AmazonCloudWatchAgent\config.json"
$WindowsVersion = (Get-ComputerInfo).OsName
if ($WindowsVersion -eq 'Microsoft Windows Server 2019 Datacenter') {
$UserDataExecutionLogPath = "$($Env:ProgramData.Replace('\','/'))/Amazon/EC2-Windows/Launch/Log/UserDataExecution.log"
}
elseif ($WindowsVersion -eq 'Microsoft Windows Server 2022 Datacenter') {
$UserDataExecutionLogPath = "$($Env:ProgramData.Replace('\','/'))/Amazon/EC2Launch/log/agent.log"
}
else {
throw "Unsupported platform: $WindowsVersion"
}
if ($CloudWatchLogGroupName -and -not (Test-Path $ConfigPath)) {
$ConfigContent = @"
{
"agent": {
"metrics_collection_interval": 60
},
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "$($Env:TMP.Replace('\','/'))/mathworks_*.log",
"log_group_name": "$($CloudWatchLogGroupName)",
"log_stream_name": "matlab_installation"
},
{
"file_path": "$($Env:TMP.Replace('\','/'))/aws_*.log",
"log_group_name": "$($CloudWatchLogGroupName)",
"log_stream_name": "matlab_activation"
},
{
"file_path": "$($Env:TMP.Replace('\','/'))/matlab_crash_dump*",
"log_group_name": "$($CloudWatchLogGroupName)",
"log_stream_name": "matlab_crashes"
},
{
"file_path": "$($Env:ProgramData.Replace('\','/'))/NICE/dcv/log/server.*log*",
"log_group_name": "$($CloudWatchLogGroupName)",
"log_stream_name": "dcv_server"
},
{
"file_path": "$($Env:ProgramData.Replace('\','/'))/NICE/dcv/log/agentsession.*log*",
"log_group_name": "$($CloudWatchLogGroupName)",
"log_stream_name": "dcv_sessionlauncher"
},
{
"file_path": "$($Env:ProgramData.Replace('\','/'))/NICE/dcv/log/agent.*log*",
"log_group_name": "$($CloudWatchLogGroupName)",
"log_stream_name": "dcv_agent"
},
{
"file_path": "$UserDataExecutionLogPath",
"log_group_name": "$($CloudWatchLogGroupName)",
"log_stream_name": "user_data_execution"
},
{
"file_path": "$($Env:ProgramData.Replace('\','/'))/MathWorks/startup.log",
"log_group_name": "$($CloudWatchLogGroupName)",
"log_stream_name": "startup"
}
]
},
"windows_events": {
"collect_list": [
{
"event_name": "Microsoft-Windows-TerminalServices-LocalSessionManager/Operational",
"event_levels": [
"INFORMATION",
"WARNING",
"ERROR",
"CRITICAL"
],
"log_group_name": "$($CloudWatchLogGroupName)",
"log_stream_name": "windows_rdp_events"
}
]
}
}
}
}
"@
Set-Content -Path $ConfigPath -Value $ConfigContent
# In this command:
# -a fetch-config causes the agent to load the latest version of the CloudWatch agent configuration file;
# -m tells the agent the host is on ec2;
# -s starts the agent;
# -c points to the configuration file
& "$Env:ProgramFiles\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1" -a fetch-config -m ec2 -s -c file:$ConfigPath
}
Write-Output 'Done with Initialize-CloudWatchLogging.'
}
try {
Initialize-CloudWatchLogging -CloudWatchLogGroupName $Env:CloudLogName
}
catch {
$ScriptPath = $MyInvocation.MyCommand.Path
Write-Output "ERROR - An error occurred while running script 'Initialize-CloudWatchLogging': $ScriptPath. Error: $_"
throw
}