-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jesse Russell
committed
Nov 26, 2018
1 parent
383397f
commit 30aa303
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
### This script configures a server for usage with AWS custom CloudWatch metrics. It creates scheduled tasks to run the custom metrics script on a schedule. ### | ||
# Specify path to cloudwatch custom metric script | ||
$path = "C:\path\to\script.ps1" | ||
# Set Execution Policy | ||
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force | ||
# Install AWS Powershell Module | ||
Install-Module AWSPowershell -Force | ||
# Create scheduled task for custom CloudWatch metrics | ||
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument "-File `"$path`"" | ||
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 30) | ||
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "AWSCloudWatch" -Description "Task that reports custom CloudWatch metrics" | ||
# Start AWSCloudWatch task | ||
Start-ScheduledTask -TaskName "AWSCloudWatch" | ||
# Create scheduled task for starting AWSCloudWatch task on startup | ||
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-Command "Start-ScheduledTask -TaskName AWSCloudWatch' | ||
$trigger = New-ScheduledTaskTrigger -AtStartup | ||
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "AWSCloudWatchInit" -Description "Starts indefinite repetition of AWSCloudWatch task" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
### This script creates custom CloudWatch metrics in AWS and sends metric data ### | ||
# Fill $param, $namespace, and $instanceID with the proper values # | ||
Import-Module AWSPowerShell | ||
# AWS Defaults data | ||
$param = @{ | ||
region = ''; | ||
AccessKey = ''; | ||
SecretKey = ''; | ||
} | ||
# Initialize AWS | ||
Initialize-AWSDefaults @param -Verbose | ||
# Instance and namespace variables | ||
$namespace = "" | ||
$instanceID = "" | ||
|
||
# Memory | ||
$ram = [math]::Round((Get-WmiObject Win32_OperatingSystem | Select-Object -ExpandProperty TotalVisibleMemorySize)/1MB, 2) | ||
$data = New-Object Amazon.CloudWatch.Model.MetricDatum | ||
$dim = New-Object Amazon.CloudWatch.Model.Dimension | ||
$dim.Name = "instanceID" | ||
$dim.Value = $instanceID | ||
$data.Dimensions = $dim | ||
$data.Timestamp = (Get-Date).ToUniversalTime() | ||
$data.MetricName = "Total Memory" | ||
$data.Unit = "Gigabytes" | ||
$data.Value = "$($ram)" | ||
Write-CWMetricData -Namespace $namespace -MetricData $data -Verbose | ||
|
||
$free = [math]::Round((Get-WmiObject Win32_OperatingSystem | Select-Object -ExpandProperty FreePhysicalMemory)/1MB, 2) | ||
$data = New-Object Amazon.CloudWatch.Model.MetricDatum | ||
$dim = New-Object Amazon.CloudWatch.Model.Dimension | ||
$dim.Name = "instanceID" | ||
$dim.Value = $instanceID | ||
$data.Dimensions = $dim | ||
$data.Timestamp = (Get-Date).ToUniversalTime() | ||
$data.MetricName = "Free Memory" | ||
$data.Unit = "Gigabytes" | ||
$data.Value = "$($free)" | ||
Write-CWMetricData -Namespace $namespace -MetricData $data -Verbose | ||
|
||
$data = New-Object Amazon.CloudWatch.Model.MetricDatum | ||
$dim = New-Object Amazon.CloudWatch.Model.Dimension | ||
$dim.Name = "instanceID" | ||
$dim.Value = $instanceID | ||
$data.Dimensions = $dim | ||
$data.Timestamp = (Get-Date).ToUniversalTime() | ||
$data.MetricName = "Memory Usage" | ||
$data.Unit = "Percent" | ||
$data.Value = "$((($ram - $free) / $ram) * 100)" | ||
Write-CWMetricData -Namespace $namespace -MetricData $data -Verbose | ||
|
||
# Disk | ||
$disks = Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3" | Select-Object @{Label="DriveLetter"; Expression={$_.DeviceID}},@{Label="Name"; Expression={$_.VolumeName}},@{Label="Free"; Expression={“{0:N2}” -f ($_.FreeSpace / 1GB)}},@{Label="Total"; Expression={“{0:N2}” -f ($_.Size / 1GB)}} | ||
foreach ($disk in $disks) { | ||
$drive = $disk.DriveLetter | ||
$data = New-Object Amazon.CloudWatch.Model.MetricDatum | ||
$dim = New-Object Amazon.CloudWatch.Model.Dimension | ||
$dim.Name = "instanceID" | ||
$dim.Value = $instanceID | ||
$data.Dimensions = $dim | ||
$data.Timestamp = (Get-Date).ToUniversalTime() | ||
$data.Unit = "Gigabytes" | ||
$data.Value = "$($disk.Total)" | ||
$data.MetricName = "$drive Total" | ||
Write-CWMetricData -Namespace $namespace -MetricData $data -Verbose | ||
|
||
$data = New-Object Amazon.CloudWatch.Model.MetricDatum | ||
$dim = New-Object Amazon.CloudWatch.Model.Dimension | ||
$dim.Name = "instanceID" | ||
$dim.Value = $instanceID | ||
$data.Dimensions = $dim | ||
$data.Timestamp = (Get-Date).ToUniversalTime() | ||
$data.Unit = "Gigabytes" | ||
$data.Value = "$($disk.Free)" | ||
$data.MetricName = "$drive Free" | ||
Write-CWMetricData -Namespace $namespace -MetricData $data -Verbose | ||
|
||
$data = New-Object Amazon.CloudWatch.Model.MetricDatum | ||
$dim = New-Object Amazon.CloudWatch.Model.Dimension | ||
$dim.Name = "instanceID" | ||
$dim.Value = $instanceID | ||
$data.Dimensions = $dim | ||
$data.Timestamp = (Get-Date).ToUniversalTime() | ||
$data.MetricName = "$drive Usage" | ||
$data.Unit = "Percent" | ||
$data.Value = "$((($disk.Total - $disk.Free) / $disk.Total) * 100)" | ||
Write-CWMetricData -Namespace $namespace -MetricData $data -Verbose | ||
} |