我已经编写了一个PowerShell脚本来自动执行Windows EC2实例上的AWS CloudWatch Agent的下载,安装,配置和启动。
现在,我有一项任务需要获取CloudWatch Agent的指标,例如Windows EC2实例的%CPU使用率,%内存使用率和%Disk空间使用率,这些需要在config.json上定义,我们使用amazon-cloudwatch- agent-config-wizard.exe向导。
请帮助我获取在Windows EC2实例的cloudwatch自定义指标上配置的%CPU使用率,%内存使用率和%Disk空间使用率,它们需要使用AutoScalingGroupName配置config.json文件。
我不想做很多复杂的事情,我只需要配置具有%CPU使用率,%内存使用率和%磁盘空间使用率的自定义指标的config.json文件,即可在Windows EC2实例的cloudwatch自定义指标上进行配置。
我在搜索引擎中进行了搜索,无法找到确切的答案来帮助我配置config.json文件。 我已经搜索了示例AWS CloudWatch Agent指标,无法找到config.json,它给出了%CPU使用率,%内存使用率和%磁盘空间使用率。
AWSCoudWatchAgentInstall.ps1
:
$file = "C:\AmazonCloudWatchAgent.zip"
$date = Get-date -Format "ddMMyyyy"
#Function to start Windows AmazonCloudWatchAgent service
Function start_service()
{
Start-Service -Name AmazonCloudWatchAgent
Start-Sleep 10
echo "AmazonCloudWatchAgent windows service started"
}
#Function to configure Cloud Watch agent service
Function configure_config()
{
Set-Location -Path 'C:\Program Files\Amazon\AmazonCloudWatchAgent\'
Copy-Item -Path $PSScriptRoot\config.json -Destination "C:\Program Files\Amazon\AmazonCloudWatchAgent\" -Force
echo "Copied config.json to Home Dir for Cloudwatch C:\Program Files\Amazon\AmazonCloudWatchAgent\"
Start-Process "cmd.exe" "/c $PSScriptRoot\execute.bat"
Start-Sleep -s 10
echo "Amazon-cloudwatch agent configuration completed"
Rename-Item -Path "C:\AmazonCloudWatchAgent" -NewName "C:\AmazonCloudWatchAgent-$date" -ErrorAction stop
echo "Rename folder C:\AmazonCloudWatchAgent with today date"
Remove-Item -Path C:\AmazonCloudWatchAgent.zip -Force
echo "Removed Zip file C:\AmazonCloudWatchAgent.zip"
}
# Function to install Windows service
Function install_service()
{
Set-Location -Path "C:\AmazonCloudWatchAgent"
Start-Process "cmd.exe" "/c $PSScriptRoot\install.bat"
Start-Sleep 15
echo "Amazon Cloud Watch Agent Installed on Windows, please verfiy on service console"
}
# Function to download zip file of cloudwatch agent
Function web_request_status()
{
Invoke-WebRequest -Uri "https://s3.amazonaws.com/amazoncloudwatch-agent/windows/amd64/latest/AmazonCloudWatchAgent.zip" -Outfile c:\AmazonCloudWatchAgent.zip
echo "Amazon Cloud Watch Agent downloading..."
Start-Sleep -s 15
Start-Process "cmd.exe" "/c $PSScriptRoot\uzip_file.bat"
echo "Unzip AmazonCloudWatchAgent.zip file under location C:\AmazonCloudWatchAgent"
Start-Sleep -s 10
}
# Set ExecutionPolicy and check for file exist
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
if (Test-Path $file)
{
echo "File $file already exists" ;
echo "Downloading latest version.."
New-Item -ItemType Directory -Path "C:\$date" -Force
Copy-Item -Path C:\AmazonCloudWatchAgent.zip -Destination C:\$date -Force
echo "File $file copied as a backup under C drive on today's date folder in format ddMMyyyy"
}
else
{
echo "File C:\AmazonCloudWatchAgent.zip does not exist"
}
web_request_status
install_service
configure_config
start_service
unzip_specific_file.bat
:
@echo off
setlocal
Call :UnZipFile "C:\AmazonCloudWatchAgent\" "c:\AmazonCloudWatchAgent.zip"
exit /b
:UnZipFile <ExtractTo> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%
install.bat
:
@echo off
cd C:\AmazonCloudWatchAgent\
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ".\install.ps1""' -Verb RunAs}"
execute.bat
:
@echo off
cd "c:\Program Files\Amazon\AmazonCloudWatchAgent"
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "C:\Program Files\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1 -a fetch-config -m ec2 -c file:config.json -s""' -Verb RunAs}"
用于自定义指标的Windows CloudWatch Agent的 config.json
文件需要配置。
{
"metrics": {
"append_dimensions": {
"AutoScalingGroupName": "${aws:AutoScalingGroupName}",
"ImageId": "${aws:ImageId}",
"InstanceId": "${aws:InstanceId}",
"InstanceType": "${aws:InstanceType}"
},
"metrics_collected": {
"LogicalDisk": {
"measurement": [
"% Free Space"
],
"metrics_collection_interval": 300,
"resources": [
"*"
]
},
"Memory": {
"measurement": [
"% Committed Bytes In Use"
],
"metrics_collection_interval": 300
},
"Paging File": {
"measurement": [
"% Usage"
],
"metrics_collection_interval": 300,
"resources": [
"*"
]
},
"PhysicalDisk": {
"measurement": [
"% Disk Time",
"Disk Write Bytes/sec",
"Disk Read Bytes/sec",
"Disk Writes/sec",
"Disk Reads/sec"
],
"metrics_collection_interval": 300,
"resources": [
"*"
]
},
"Processor": {
"measurement": [
"% User Time",
"% Idle Time",
"% Interrupt Time"
],
"metrics_collection_interval": 300,
"resources": [
"*"
]
}
}
}
}
请帮助我获取CloudWatch代理的自定义指标以显示在config.json
上,该指标将显示Windows EC2实例的CPU使用率,内存使用率和磁盘空间使用率!
OS:Windows OS 64位
该脚本目前可用于Windows Server 2012、2016。
我将使用相同的config.json
文件来配置自定义cloudwatch代理指标。
答案 0 :(得分:0)
使用这些 AWS docs验证您是否配置了所有正确的权限。您可能只是缺少正确的IAM权限。
要进行进一步的故障排除,您可以通过在“指标”之前添加以下部分(确保更新区域)来启用调试功能:{:
"agent": {
"metrics_collection_interval": 60,
"region": "us-east-1",
"logfile": "c:\\ProgramData\\Amazon\\CloudWatchAgent\\Logs\\amazon-cloudwatch-agent.log",
"debug": true,
"run_as_user": "cwagent"
},
"metrics": {
...
添加此内容后,请用.\amazon-cloudwatch-agent-ctl.ps1 -a fetch-config -m ec2 -c file:'C:\Program Files\Amazon\AmazonCloudWatchAgent\config.json' -s
检查以下位置的日志: