-
Notifications
You must be signed in to change notification settings - Fork 61
/
aws-cloudwatch-custom-metrics.ps1
88 lines (82 loc) · 3.43 KB
/
aws-cloudwatch-custom-metrics.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
}