简体   繁体   中英

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

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.

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' -s

Check the following locations for logs:

  • C:\\ProgramData\\Amazon\\CloudWatchAgent\\Logs
  • C:\\ProgramData\\Amazon\\AmazonCloudWatchAgent\\Logs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM