Configure custom metric of AWS CloudWatch Agent for Windows EC2 Instance [%CPU,%Memory,%DiskSpace]

烈酒焚心 提交于 2019-12-11 18:19:36

问题


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 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}"

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 bit

The script is working for Windows Server 2012, 2016 so far tested.

I will use same config.json file to configure custom cloudwatch agent metric.

来源:https://stackoverflow.com/questions/57217693/configure-custom-metric-of-aws-cloudwatch-agent-for-windows-ec2-instance-cpu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!