Created
July 19, 2018 21:42
-
-
Save jayankandathil/e564270e2c11c55035cb84fa9dff8889 to your computer and use it in GitHub Desktop.
Collect CloudWatch metrics for an EC2 instance in AWS using PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author : Jayan Kandathil (Adobe Managed Services) | |
# Date : July 19, 2018 | |
# https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html | |
# https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/ec2-metricscollected.html | |
# https://aws.amazon.com/blogs/developer/writing-and-archiving-custom-metrics-using-amazon-cloudwatch-and-aws-tools-for-powershell/ | |
# Get authentication credentials | |
Set-AWSCredential -AccessKey YOURAWSACCESSKEY -SecretKey yOUR354aws39875358sECRET93453-96kEY -SessionToken yOURawssESSIONtOKENEaDKGld+XcXoR/yOUtHINKtHISiSreAL2XD1k9tgQ/TKIjhXXTXxM | |
# Set Test Start Time and Test End Time (remember to adjust for your time zone - CloudWatch data is in UTC) | |
$start = "2018-07-17T20:12:00Z" | |
$end = "2018-07-18T20:12:00Z" | |
$region = "us-west-2" | |
$instanceid = "i-yourinstanceidea6a16" | |
$logfilepath = "C:\TEMP\" | |
$logfile = "EC2instance-Metadata.txt" | |
# Set AWS Region | |
Set-DefaultAWSRegion -Region $region | |
$instance = (Get-EC2Instance -InstanceId $instanceid).Instances | |
Add-Content -Path $logfilepath$logfile -Value $instance.InstanceId | |
Add-Content -Path $logfilepath$logfile -Value $instance.InstanceType | |
Add-Content -Path $logfilepath$logfile -Value $instance.LaunchTime.Date | |
Add-Content -Path $logfilepath$logfile -Value $instance.Hypervisor | |
Add-Content -Path $logfilepath$logfile -Value $instance.State.Name | |
Add-Content -Path $logfilepath$logfile -Value $instance.VirtualizationType | |
Add-Content -Path $logfilepath$logfile -Value $instance.CpuOptions.CoreCount | |
Add-Content -Path $logfilepath$logfile -Value $instance.CpuOptions.ThreadsPerCore | |
Add-Content -Path $logfilepath$logfile -Value $instance.EbsOptimized | |
Add-Content -Path $logfilepath$logfile -Value $instance.ImageId | |
Add-Content -Path $logfilepath$logfile -Value $instance.InstanceLifecycle | |
Add-Content -Path $logfilepath$logfile -Value $instance.Placement.Affinity | |
Add-Content -Path $logfilepath$logfile -Value $instance.Placement.SpreadDomain | |
Add-Content -Path $logfilepath$logfile -Value $instance.Placement.AvailabilityZone | |
Add-Content -Path $logfilepath$logfile -Value $instance.Placement.Tenancy | |
Add-Content -Path $logfilepath$logfile -Value $instance.PrivateDnsName | |
Add-Content -Path $logfilepath$logfile -Value $instance.PrivateIpAddress | |
Add-Content -Path $logfilepath$logfile -Value $instance.PublicDnsName | |
Add-Content -Path $logfilepath$logfile -Value $instance.PublicIpAddress | |
Add-Content -Path $logfilepath$logfile -Value $instance.SriovNetSupport | |
Add-Content -Path $logfilepath$logfile -Value $instance.VpcId | |
Add-Content -Path $logfilepath$logfile -Value $instance.SubnetId | |
$logentry ="" | |
$blockdevicemappings = $instance.BlockDeviceMappings | |
foreach ($mapping in $blockdevicemappings) | |
{ | |
$logentry = $logentry + $mapping.Ebs.VolumeId + " | " | |
} | |
Add-Content -Path $logfilepath$logfile -Value $logentry | |
$dimension = New-Object Amazon.CloudWatch.Model.Dimension | |
$dimension.set_Name("InstanceId") | |
$dimension.set_Value($instance.InstanceId) | |
# ---- | |
# CPU | |
# ---- | |
$logfile = "EC2instance-CPU_Percent.csv" | |
# Get maximum CPU utilization by the minute | |
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EC2 -MetricName CPUUtilization -StartTime $start -EndTime $end -Period 60 -Statistic Maximum | |
Write-Host -foregroundcolor Yellow "Number of datapoints is " $data.Datapoints.Count | |
# Sort by timestamp | |
$datapoints = $data.Datapoints | Sort-Object -Property Timestamp | |
foreach ($datapoint in $datapoints) | |
{ | |
# Print the observations to console | |
[single]$datapoint.Maximum | |
$logentry = [string]$datapoint.Timestamp + "," + [single]$datapoint.Maximum | |
# Log the observations to file | |
Add-Content -Path $logfilepath$logfile -Value $logentry | |
} | |
# --------- | |
# READ Ops/Min (applies only if the instance has a direct-host-attached ephemeral drive) | |
# --------- | |
$logfile = "EC2instance-ephemeraldisks-READ_OpsPerMinute.csv" | |
# Get READ ops by the minute | |
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EC2 -MetricName DiskReadOps -StartTime $start -EndTime $end -Period 60 -Statistic Sum | |
Write-Host -foregroundcolor Yellow "Number of datapoints is " $data.Datapoints.Count | |
# Sort by timestamp | |
$datapoints = $data.Datapoints | Sort-Object -Property Timestamp | |
foreach ($datapoint in $datapoints) | |
{ | |
# Print the observations to console | |
[single]$datapoint.Sum | |
$logentry = [string]$datapoint.Timestamp + "," + [single]$datapoint.Sum | |
# Log the observations to file | |
Add-Content -Path $logfilepath$logfile -Value $logentry | |
} | |
# ------------- | |
# WRITE Ops/Min (applies only if the instance has a direct-host-attached ephemeral drive) | |
# ------------- | |
$logfile = "EC2instance-ephemeraldisks-WRITE_OpsPerMinute.csv" | |
# Get READ ops by the minute | |
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EC2 -MetricName DiskWriteOps -StartTime $start -EndTime $end -Period 60 -Statistic Sum | |
Write-Host -foregroundcolor Yellow "Number of datapoints is " $data.Datapoints.Count | |
# Sort by timestamp | |
$datapoints = $data.Datapoints | Sort-Object -Property Timestamp | |
foreach ($datapoint in $datapoints) | |
{ | |
# Print the observations to console | |
[single]$datapoint.Sum | |
$logentry = [string]$datapoint.Timestamp + "," + [single]$datapoint.Sum | |
# Log the observations to file | |
Add-Content -Path $logfilepath$logfile -Value $logentry | |
} | |
# --------- | |
# READ Bytes (applies only if the instance has a direct-host-attached ephemeral drive) | |
# --------- | |
$logfile = "EC2instance-ephemeraldisks-READ_Bytes.csv" | |
# Get READ ops by the minute | |
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EC2 -MetricName DiskReadBytes -StartTime $start -EndTime $end -Period 60 -Statistic Sum | |
Write-Host -foregroundcolor Yellow "Number of datapoints is " $data.Datapoints.Count | |
# Sort by timestamp | |
$datapoints = $data.Datapoints | Sort-Object -Property Timestamp | |
foreach ($datapoint in $datapoints) | |
{ | |
# Print the observations to console | |
[single]$datapoint.Sum | |
$logentry = [string]$datapoint.Timestamp + "," + [single]$datapoint.Sum | |
# Log the observations to file | |
Add-Content -Path $logfilepath$logfile -Value $logentry | |
} | |
# --------- | |
# WRITE Bytes (applies only if the instance has a direct-host-attached ephemeral drive) | |
# --------- | |
$logfile = "EC2instance-ephemeraldisks-WRITE_Bytes.csv" | |
# Get READ ops by the minute | |
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EC2 -MetricName DiskWriteBytes -StartTime $start -EndTime $end -Period 60 -Statistic Sum | |
Write-Host -foregroundcolor Yellow "Number of datapoints is " $data.Datapoints.Count | |
# Sort by timestamp | |
$datapoints = $data.Datapoints | Sort-Object -Property Timestamp | |
foreach ($datapoint in $datapoints) | |
{ | |
# Print the observations to console | |
[single]$datapoint.Sum | |
$logentry = [string]$datapoint.Timestamp + "," + [single]$datapoint.Sum | |
# Log the observations to file | |
Add-Content -Path $logfilepath$logfile -Value $logentry | |
} | |
# ----------- | |
# Network In | |
# ----------- | |
$logfile = "EC2instance-NetworkIn_Bytes.csv" | |
# Get READ ops by the minute | |
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EC2 -MetricName NetworkIn -StartTime $start -EndTime $end -Period 60 -Statistic Sum | |
Write-Host -foregroundcolor Yellow "Number of datapoints is " $data.Datapoints.Count | |
# Sort by timestamp | |
$datapoints = $data.Datapoints | Sort-Object -Property Timestamp | |
foreach ($datapoint in $datapoints) | |
{ | |
# Print the observations to console | |
[single]$datapoint.Sum | |
$logentry = [string]$datapoint.Timestamp + "," + [single]$datapoint.Sum | |
# Log the observations to file | |
Add-Content -Path $logfilepath$logfile -Value $logentry | |
} | |
# ----------- | |
# Network Out | |
# ----------- | |
$logfile = "EC2instance-NetworkIn_Bytes.csv" | |
# Get READ ops by the minute | |
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EC2 -MetricName NetworkOut -StartTime $start -EndTime $end -Period 60 -Statistic Sum | |
Write-Host -foregroundcolor Yellow "Number of datapoints is " $data.Datapoints.Count | |
# Sort by timestamp | |
$datapoints = $data.Datapoints | Sort-Object -Property Timestamp | |
foreach ($datapoint in $datapoints) | |
{ | |
# Print the observations to console | |
[single]$datapoint.Sum | |
$logentry = [string]$datapoint.Timestamp + "," + [single]$datapoint.Sum | |
# Log the observations to file | |
Add-Content -Path $logfilepath$logfile -Value $logentry | |
} | |
# ---------------- | |
# EBS READ Ops/Min | |
# ---------------- | |
$logfile = "EC2instance-EBSvolumes-READ_OpsPerMinute.csv" | |
# Get READ ops by the minute | |
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EC2 -MetricName EBSReadOps -StartTime $start -EndTime $end -Period 60 -Statistic Sum | |
Write-Host -foregroundcolor Yellow "Number of datapoints is " $data.Datapoints.Count | |
# Sort by timestamp | |
$datapoints = $data.Datapoints | Sort-Object -Property Timestamp | |
foreach ($datapoint in $datapoints) | |
{ | |
# Print the observations to console | |
[single]$datapoint.Sum | |
$logentry = [string]$datapoint.Timestamp + "," + [single]$datapoint.Sum | |
# Log the observations to file | |
Add-Content -Path $logfilepath$logfile -Value $logentry | |
} | |
# ------------------ | |
# EBS WRITE Ops/Min | |
# ----------------- | |
$logfile = "EC2instance-EBSvolumes-WRITE_OpsPerMinute.csv" | |
# Get READ ops by the minute | |
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EC2 -MetricName EBSWriteOps -StartTime $start -EndTime $end -Period 60 -Statistic Sum | |
Write-Host -foregroundcolor Yellow "Number of datapoints is " $data.Datapoints.Count | |
# Sort by timestamp | |
$datapoints = $data.Datapoints | Sort-Object -Property Timestamp | |
foreach ($datapoint in $datapoints) | |
{ | |
# Print the observations to console | |
[single]$datapoint.Sum | |
$logentry = [string]$datapoint.Timestamp + "," + [single]$datapoint.Sum | |
# Log the observations to file | |
Add-Content -Path $logfilepath$logfile -Value $logentry | |
} | |
# -------------- | |
# EBS READ Bytes | |
# -------------- | |
$logfile = "EC2instance-EBSvolumes-READ_Bytes.csv" | |
# Get READ ops by the minute | |
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EC2 -MetricName EBSReadBytes -StartTime $start -EndTime $end -Period 60 -Statistic Sum | |
Write-Host -foregroundcolor Yellow "Number of datapoints is " $data.Datapoints.Count | |
# Sort by timestamp | |
$datapoints = $data.Datapoints | Sort-Object -Property Timestamp | |
foreach ($datapoint in $datapoints) | |
{ | |
# Print the observations to console | |
[single]$datapoint.Sum | |
$logentry = [string]$datapoint.Timestamp + "," + [single]$datapoint.Sum | |
# Log the observations to file | |
Add-Content -Path $logfilepath$logfile -Value $logentry | |
} | |
# --------------- | |
# EBS WRITE Bytes | |
# --------------- | |
$logfile = "EC2instance-EBSvolumes-WRITE_Bytes.csv" | |
# Get READ ops by the minute | |
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EC2 -MetricName EBSWriteBytes -StartTime $start -EndTime $end -Period 60 -Statistic Sum | |
Write-Host -foregroundcolor Yellow "Number of datapoints is " $data.Datapoints.Count | |
# Sort by timestamp | |
$datapoints = $data.Datapoints | Sort-Object -Property Timestamp | |
foreach ($datapoint in $datapoints) | |
{ | |
# Print the observations to console | |
[single]$datapoint.Sum | |
$logentry = [string]$datapoint.Timestamp + "," + [single]$datapoint.Sum | |
# Log the observations to file | |
Add-Content -Path $logfilepath$logfile -Value $logentry | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment