Skip to content

Instantly share code, notes, and snippets.

@jayankandathil
Created July 19, 2018 18:53
Show Gist options
  • Save jayankandathil/9153ec3b4d0ef3aee512b96b0581ed59 to your computer and use it in GitHub Desktop.
Save jayankandathil/9153ec3b4d0ef3aee512b96b0581ed59 to your computer and use it in GitHub Desktop.
Collect CloudWatch metrics for an EBS volume in AWS using PowerShell
# 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/rds-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"
$ebsvolumeid = "vol-0byourvolidadfb4"
$logfilepath = "C:\TEMP\"
$logfile = "EBSvolume-Metadata.txt"
# Set AWS Region
Set-DefaultAWSRegion -Region $region
$ebsvolume = Get-EC2Volume -VolumeId $ebsvolumeid
Get-EC2Volume -VolumeId $ebsvolumeid
$attachments = $ebsvolume.Attachment
foreach ($attachment in $attachments)
{
$instance = $attachment.InstanceId
}
$dimension = New-Object Amazon.CloudWatch.Model.Dimension
$dimension.set_Name("VolumeId")
$dimension.set_Value($ebsvolume.VolumeId)
Add-Content -Path $logfilepath$logfile -Value $ebsvolume.VolumeId
Add-Content -Path $logfilepath$logfile -Value $instance
Add-Content -Path $logfilepath$logfile -Value $ebsvolume.AvailabilityZone
Add-Content -Path $logfilepath$logfile -Value $ebsvolume.VolumeType.Value
Add-Content -Path $logfilepath$logfile -Value $ebsvolume.CreateTime
Add-Content -Path $logfilepath$logfile -Value $ebsvolume.Status.Value
Add-Content -Path $logfilepath$logfile -Value $ebsvolume.Iops
Add-Content -Path $logfilepath$logfile -Value $ebsvolume.Size
Add-Content -Path $logfilepath$logfile -Value $ebsvolume.State.Value
# ------------
# BurstBalance (applies only to gp2 volumes)
# ------------
$logfile = "EBSvolume-BurstBalance-gp2_only.csv"
# Get minimum BurstBalance by the minute
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EBS -MetricName BurstBalance -StartTime $start -EndTime $end -Period 60 -Statistic Minimum
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.Minimum
$logentry = [string]$datapoint.Timestamp + "," + [single]$datapoint.Minimum
# Log the observations to file
Add-Content -Path $logfilepath$logfile -Value $logentry
}
# ---------------
# VolumeReadBytes
# ---------------
$logfile = "EBSvolume-READBytes_Sum.csv"
# Get the sum of VolumeREADBytes by the minute
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EBS -MetricName VolumeReadBytes -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
}
# ---------------
# VolumeWriteBytes
# ---------------
$logfile = "EBSvolume-WRITEBytes_Sum.csv"
# Get the sum of VolumeWRITEBytes by the minute
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EBS -MetricName VolumeWriteBytes -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 IOPS
# ---------
$logfile = "EBSvolume-READ_IOPS.csv"
# Get maximum READ IOPS by the minute
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EBS -MetricName VolumeReadOps -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
}
# ---------
# WRITE IOPS
# ---------
$logfile = "EBSvolume-WRITE_IOPS.csv"
# Get maximum READ IOPS by the minute
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EBS -MetricName VolumeWriteOps -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
}
# ------------
# Queue Length
# ------------
$logfile = "EBSvolume-QueueLength.csv"
# Get maximum READ IOPS by the minute
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EBS -MetricName VolumeQueueLength -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
}
# ------------
# Throughput % (applies only to Provisioned IOPS io1 volumes)
# ------------
$logfile = "EBSvolume-Throughput_Percent-pIOPS_only.csv"
# Get maximum READ IOPS by the minute
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EBS -MetricName VolumeThroughputPercentage -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
}
# ------------------------
# Consumed READ/WRITE IOPS (applies only to Provisioned IOPS io1 volumes)
# ------------------------
$logfile = "EBSvolume-Consumed_READ-WRITE_IOPS-pIOPS_only.csv"
# Get maximum READ IOPS by the minute
$data = Get-CWMetricStatistic -Dimension $dimension -Namespace AWS/EBS -MetricName VolumeConsumedReadWriteOps -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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment