title: Configure custom metric of AWS CloudWatch Agent for Windows EC2 Instance [%CPU,%Memory,%DiskSpace]
body: <p>I have written a PowerShell script to automate of download,install,configuration and start of AWS CloudWatch Agent on Windows EC2 Instance. </p>
  
  <p>Right now I have a task to fetch Metrics of CloudWatch Agent such as %CPU Usage, %Memory Usage &amp; %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.</p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p><code>AWSCoudWatchAgentInstall.ps1</code>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>$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
  </code></pre>
  
  <p><code>unzip_specific_file.bat</code>:</p>
  
  <pre class="lang-batch prettyprint-override"><code>@echo off
  setlocal
  Call :UnZipFile "C:\AmazonCloudWatchAgent\" "c:\AmazonCloudWatchAgent.zip"
  exit /b
  
  :UnZipFile &lt;ExtractTo&gt; &lt;newzipfile&gt;
  set vbs="%temp%\_.vbs"
  if exist %vbs% del /f /q %vbs%
  &gt;%vbs%  echo Set fso = CreateObject("Scripting.FileSystemObject")
  &gt;&gt;%vbs% echo If NOT fso.FolderExists(%1) Then
  &gt;&gt;%vbs% echo fso.CreateFolder(%1)
  &gt;&gt;%vbs% echo End If
  &gt;&gt;%vbs% echo set objShell = CreateObject("Shell.Application")
  &gt;&gt;%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
  &gt;&gt;%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
  &gt;&gt;%vbs% echo Set fso = Nothing
  &gt;&gt;%vbs% echo Set objShell = Nothing
  cscript //nologo %vbs%
  if exist %vbs% del /f /q %vbs%
  </code></pre>
  
  <p><code>install.bat</code>:</p>
  
  <pre class="lang-batch prettyprint-override"><code>@echo off
  cd C:\AmazonCloudWatchAgent\
  PowerShell -NoProfile -ExecutionPolicy Bypass -Command "&amp; {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ".\install.ps1""' -Verb RunAs}"
  </code></pre>
  
  <p><code>execute.bat</code>:</p>
  
  <pre class="lang-batch prettyprint-override"><code>@echo off
  cd "c:\Program Files\Amazon\AmazonCloudWatchAgent"
  PowerShell -NoProfile -ExecutionPolicy Bypass -Command "&amp; {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}"
  </code></pre>
  
  <p><code>config.json</code> file for Windows CloudWatch Agent for custom metric need to configure.</p>
  
  <pre class="lang-json prettyprint-override"><code>{
      "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": [
                      "*"
                  ]
              }
          }
      }
  }
  </code></pre>
  
  <p>Kindly please help me to get custom metrics of CloudWatch Agent to present on <code>config.json</code> that will show %Usgae of CPU, %Usage of Memory and %Usgae of Disk Space for Windows EC2 Instance!</p>
  
  <p>OS: Windows OS 64 bit</p>
  
  <p>The script is working for Windows Server 2012, 2016 so far tested. </p>
  
  <p>I will use same <code>config.json</code> file to configure custom cloudwatch agent metric.</p>