Created
December 3, 2021 01:20
-
-
Save Rugby-Ball/8488e4e048a581c1ff76b8172dc34c7c to your computer and use it in GitHub Desktop.
This will pull from a date range, the AVG CPU utilization for an EC2 Instance, and save to a CSV file. #AWS #EC2 #Public #Utility #AWS_Cloud_Watch
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
| # ec2-cpu-utilization-report.ps1 | |
| <# | |
| Description: This will pull from a date range, the AVG CPU utilization for an EC2 Instance, and save to a CSV file. | |
| Written: Ed Walsh | |
| PowerShell.Core tested: Not Tested | |
| Version: 1.0.0 | |
| Create Date: 12/2/2021 | |
| Revised Date: 12/2/2021 | |
| #> | |
| #UTC Time Entered Below - NOTE The report csv FILE NAME will show date range in UTC time. | |
| $StartTime = Get-Date -Date "2021-10-28 00:00:00" | |
| $EndTime = Get-Date -Date "2021-11-01 23:59:59" | |
| #Instance ID of target machine. | |
| $InstanceID = 'i-04XXX72XXXXXXXXX' | |
| $region = 'us-east-1' | |
| $timestamp = get-date -format yyyyMMddHHmmss | |
| $subfolder = if (($PSVersionTable.PSEdition) -eq "Core") { if ( $True -eq $iswindows ) { "\Documents\" } Else { "" } } Else {"\Documents\"} | |
| $mydocuments = $home + $subfolder #Old Way of doing it# [environment]::getfolderpath("mydocuments") | |
| $fileName = "EC2-CPU-Utilization-report-for-dateRange-$(get-date -Date $StartTime -format yyyyMMddHHmm)-to-$(get-date -Date $EndTime -format yyyyMMddHHmm)-ReportRunOn-"+ [string]$timestamp + ".csv" | |
| $filePath = Join-Path $mydocuments $fileName | |
| Write-Host "Starting Cloud-Watch CPU Utilization Query" | |
| #Initialize objects used in the query | |
| $CPU = [Amazon.CloudWatch.Model.MetricDataQuery]::new() | |
| $CPU.Id = 'cpuUtil01' | |
| $CPU.Label = 'CPUUtilization' | |
| $CPUMetricStat = [Amazon.CloudWatch.Model.MetricStat]::new() | |
| $CPUMetricStat.Stat = 'Maximum' | |
| $CPUMetricStat.Period = 300 #Five minute(s) Avg | |
| $CPUMetricStatMetric = [Amazon.CloudWatch.Model.Metric]::new() | |
| $CPUMetricStatMetric.MetricName = 'CPUUtilization' | |
| $CPUMetricStatMetric.Namespace = 'AWS/EC2' | |
| $CPUMetricStatMetricDimension = [Amazon.CloudWatch.Model.Dimension]::new() | |
| $CPUMetricStatMetricDimension.Name = 'InstanceId' | |
| $CPUMetricStatMetricDimension.Value = $InstanceID | |
| $CPUMetricStatMetric.Dimensions = $CPUMetricStatMetricDimension | |
| $CPUMetricStat.Metric = $CPUMetricStatMetric | |
| $CPU.MetricStat = $CPUMetricStat | |
| $TotalMinutes = ($EndTime - $StartTime).TotalMinutes | |
| $datapoints = $TotalMinutes / ($CPUMetricStat.Period / 60) | |
| #Warn user if this has a lot of datapoints and will take some time to run. | |
| if ($datapoints -gt 8000) { Write-Host "You have $datapoints datapoints, this will take a few minutes to run. Be patient."} | |
| $Metrics = (Get-CWMetricData -MetricDataQuery $CPU -ScanBy 'TimestampAscending' -UtcStartTime $StartTime -UtcEndTime $EndTime -Region $region ).MetricDataResults | |
| $maxCount = [math]::Max($metrics.TimeStamps.Count, $metrics.Values.Count) | |
| $result = for($i = 0; $i -lt $maxCount; $i++) | |
| { | |
| [pscustomobject]@{ | |
| InstanceID = $CPUMetricStatMetricDimension.Value | |
| "TimeStamp-UTC" = $metrics.TimeStamps[$i] | |
| "CPU-AVG" = $metrics.Values[$i] | |
| } | |
| } | |
| $result | Export-Csv -NoTypeInformation -Append -Path $filepath | |
| Write-Output "Exported to: $filePath" | |
| Write-Output "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|||||||////////////////////////////////////" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment