本文介绍了为Windows EC2实例配置AWS CloudWatch代理的自定义指标[%CPU,%Memory,%DiskSpace]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我已经编写了一个PowerShell脚本来自动执行Windows EC2实例上的AWS CloudWatch Agent的下载,安装,配置和启动。 现在,我有一个任务来获取CloudWatch代理的指标,例如%CPU使用率,%内存使用率&需要在config.json上定义的Windows EC2实例的磁盘空间使用百分比,我们使用amazon-cloudwatch-agent-config-wizard.exe向导进行配置。 请帮助我获取在Windows EC2实例的cloudwatch自定义指标上配置的%CPU使用率,%内存使用率和%Disk空间使用率,它们需要使用AutoScalingGroupName配置config.json文件。 我不想做很多复杂的事情,我只需要使用具有%CPU使用率,%Memory使用率和%Disk空间使用率的自定义指标的config.json文件在Windows EC2实例的cloudwatch自定义指标上。 我在搜索引擎上进行了搜索,无法找到确切的答案来帮助我配置config.json文件。 我已经搜索了示例AWS CloudWatch Agent指标,无法找到config.json,它给了我%CPU使用率,%内存使用率和%磁盘空间使用率。 AWSCoudWatchAgentInstall.ps1 : $ file = C:\AmazonCloudWatchAgent.zip $ date =获取日期-Format ddMMyyyy #Function启动Windows AmazonCloudWatchAgent服务函数start_service() { Start-Service -Name AmazonCloudWatchAgent Start-Sleep 10 echo AmazonCloudWatchAgent Windows服务已启动 } #用于配置Cloud Watch代理服务的功能函数configure_config() { Set-Location -Path'C:\Program Files\Amazon \AmazonCloudWatchAgent\'复制项目-Path $ PSScriptRoot\config.json-目标 C:\Program Files\Amazon\AmazonCloudWatchAgent\-强制 echo复制的配置.json到Cloudwatch C的Home Dir:\Program Files\Amazon\AmazonCloudWatchAgent\ 开始处理 cmd.exe / c $ PSScriptRoot\execute.bat 开始-Sleep -s 10 echo Amazon-cloudwatch a gent配置已完成 重命名项-路径 C:\AmazonCloudWatchAgent -NewName C:\AmazonCloudWatchAgent- $ date -ErrorAction stop echo今天重命名文件夹C:\AmazonCloudWatchAgent date Remove-Item-路径C:\AmazonCloudWatchAgent.zip-强制 echo已删除的Zip文件C:\AmazonCloudWatchAgent.zip } #安装功能Windows服务函数install_service() {设置位置-路径 C:\AmazonCloudWatchAgent 启动进程 cmd.exe / c $ PSScriptRoot\ install.bat 开始睡眠15 echo Amazon Cloud Watch Agent已安装在Windows上,请在服务控制台上确认 } #下载zip的功能cloudwatch代理的文件函数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陶氏nloading ... 开始睡眠-s 15 启动过程 cmd.exe / c $ PSScriptRoot\uzip_file.bat echo解压AmazonCloudWatchAgent.zip文件位置C:\AmazonCloudWatchAgent 开始睡眠-s 10 } #设置ExecutionPolicy并检查文件是否存在 Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine if(Test-Path $ file) { echo文件$ file已经存在; echo正在下载最新版本。 New-Item -ItemType目录-Path C:\ $ date-强制 Copy-Item -Path C:\AmazonCloudWatchAgent.zip -目标C:\ $ date-强制 echo文件$ file作为备份复制到今天的日期文件夹中的C驱动器下,格式为ddMMyyyy } else { echo文件C:\AmazonCloudWatchAgent.zip不存在 } web_request_status install_service configure_config start_service unzip_specific_file.bat : @echo off setlocal 调用:UnZipFile C:\AmazonCloudWatchAgent\ c: CloudAmazonCloudWatchAgent.zip 退出/ b :UnZipFile< ExtractTo> < newzipfile> 设置vbs =%temp%\_.vbs (如果存在)%vbs%del / f / q /%vbs%>%vbs%echo Set fso = CreateObject( Scripting.FileSystemObject)>%vbs%echo如果不是fso.FolderExists(%1),则>%vbs%echo fso.CreateFolder(%1) >>%vbs%echo结束如果>%vbs%echo设置objShell = CreateObject( Shell.Application)>%vbs%echo设置FilesInZip = objShell。 NameSpace(%2)。项>>%vbs%echo objShell.NameSpace(%1).CopyHere(FilesInZip)>>%vbs%echo Set fso = Nothing >>%vbs%echo Set objShell = Nothing cscript //没有徽标%vbs%(如果存在)%vbs%del / f / q%vbs% install.bat : @echo off cd C:\AmazonCloudWatchAgent\ PowerShell -NoProfile -ExecutionPolicy Bypass -Command& {开始-Process PowerShell -ArgumentList'-NoProfile -ExecutionPolicy绕过-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 -fetch-config -m ec2 -c文件:config.json -s''-Verb RunAs} config用于自定义指标的Windows CloudWatch Agent的.json 文件需要配置。 { metrics:{ append_dimensions:{ AutoScalingGroupName: $ {aws:AutoScalingGroupName}, ImageId: $ {aws :ImageId}, InstanceId: $ {aws:InstanceId}, InstanceType: $ {aws:InstanceType} }, metrics_collected :{{ LogicalDisk:{ measurement:[可用空间% ], metrics_collection_interval:300, 资源:[ * ] },内存:{测量:[使用的已提交字节百分比 ], metrics_collection_interval:300 }, Paging File:{ measurement:[%Usage ], metrics_collection_interval:300,资源:[ * ] }, PhysicalDisk:{ measurement :[[%Disk Time, Disk Write Bytes / sec, Disk Read Bytes / sec, Disk Writes / sec,磁盘读取/秒 ], metrics_collection_interval:300,资源:[ * ] },处理器:{ measurement:[%User Time,%Idle Time ,%中断时间 ], metrics_collection_interval:300,资源:[ * ] } } } } 请帮助我获得了CloudWatch Agent的自定义指标,该指标将显示在 config.json 上,其中将显示Windows EC2实例的CPU使用率%Usgae,内存使用率%Usgae和磁盘空间%Usgae! 操作系统:Windows OS 64 bit 该脚本适用于Windows Server 2012(到目前为止已测试)。 我将使用相同的 config.json 文件配置自定义cloudwatch代理指标。解决方案使用以下 AWS文档确认您已配置了所有正确的权限。 要进行进一步的故障排除,您可以通过在指标之前添加以下部分(确保更新区域)来启用调试功能,以进行进一步的故障排除:{: agent:{ metrics_collection_interval:60, region: us-east-1, logfile: c:\\ProgramData\\Amazon\\CloudWatchAgent\\Logs\\amazon-cloudwatch-agent.log ,调试:true, run_as_user: cwagent },指标:{ ... 添加完后,用重新导入config.json。\amazon-cloudwatch- agent-ctl.ps1 -a fetch-config -m ec2 -c文件:'C:\Program Files\Amazon\AmazonCloudWatchAgent\config.json'-s 检查以下位置的日志: C:\ProgramData\Amazon\CloudWatchAgent \日志 C:\ProgramData\Amazon\AmazonCloudWatchAgent\Logs I have written a PowerShell script to automate of download,install,configuration and start of AWS CloudWatch Agent on Windows EC2 Instance. Right now I have a task to fetch Metrics of CloudWatch Agent such as %CPU Usage, %Memory Usage & %Disk Space usage of Windows EC2 Instance that would need to define on config.json which we configure using amazon-cloudwatch-agent-config-wizard.exe wizard.Please help me to get %CPU usage, %Memory Usage and %Disk Space usage configure on cloudwatch custom metric for Windows EC2 Instance which need to configure for config.json file with AutoScalingGroupName already exists. I don't want to do much complex thing, i just need config.json file with custom metric of %CPU usage, %Memory Usage and %Disk Space usage configure on cloudwatch custom metric for Windows EC2 Instance.I have searched over search engine, unable to find out exact answer to help me configure for config.json file. I have searched for sample AWS CloudWatch Agent metric, unable to find config.json which give me %CPU Usage,%Memory Usage and %Disk Space Usage. AWSCoudWatchAgentInstall.ps1:$file = "C:\AmazonCloudWatchAgent.zip"$date = Get-date -Format "ddMMyyyy"#Function to start Windows AmazonCloudWatchAgent serviceFunction start_service(){ Start-Service -Name AmazonCloudWatchAgent Start-Sleep 10 echo "AmazonCloudWatchAgent windows service started"}#Function to configure Cloud Watch agent serviceFunction 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 serviceFunction 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 agentFunction 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 existSet-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachineif (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_statusinstall_serviceconfigure_configstart_serviceunzip_specific_file.bat:@echo offsetlocalCall :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 = Nothingcscript //nologo %vbs%if exist %vbs% del /f /q %vbs%install.bat:@echo offcd C:\AmazonCloudWatchAgent\PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ".\install.ps1""' -Verb RunAs}"execute.bat:@echo offcd "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}"config.json file for Windows CloudWatch Agent for custom metric need to configure.{ "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": [ "*" ] } } }}Kindly please help me to get custom metrics of CloudWatch Agent to present on config.json that will show %Usgae of CPU, %Usage of Memory and %Usgae of Disk Space for Windows EC2 Instance!OS: Windows OS 64 bitThe script is working for Windows Server 2012, 2016 so far tested. I will use same config.json file to configure custom cloudwatch agent metric. 解决方案 Use these AWS docs verify you have all the correct permissions configured. It is possible you are simply missing the correct IAM permissions.For further troubleshooting you can enable debugging by adding the following section(make sure to update region) before "metrics": {:"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": { ...After this has been added re-inport your config.json with .\amazon-cloudwatch-agent-ctl.ps1 -a fetch-config -m ec2 -c file:'C:\Program Files\Amazon\AmazonCloudWatchAgent\config.json' -sCheck the following locations for logs:C:\ProgramData\Amazon\CloudWatchAgent\LogsC:\ProgramData\Amazon\AmazonCloudWatchAgent\Logs 这篇关于为Windows EC2实例配置AWS CloudWatch代理的自定义指标[%CPU,%Memory,%DiskSpace]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-11 06:53