适用于cloudwatch自定义指标的PowerShell脚本

时间:2018-03-17 09:42:10

标签: powershell

下面是我用来监控逻辑磁盘空间并将其发布到cloudwatch的脚本,任何人都知道如何修改此脚本以获取内存使用情况?

*enter code here
**#Parameters
$computerName = ""; 
$deviceId = "C:"; 
$instanceId = ""; 

#Send To CloudWatch
$metricName = "EBS Volume Usage";  
$deviceCommand = "DeviceID='" + $deviceId + "'";  
$unit = "Count";  
$disk = Get-WmiObject Win32_LogicalDisk -ComputerName $computerName -Filter   $deviceCommand | Select-Object Size,FreeSpace;  
$value = (100 - $disk.FreeSpace / $disk.Size * 100);  
Write-Host "Posting Volume Usage To CloudWatch: $($value)";  
aws cloudwatch put-metric-data --metric-name $metricName --namespace "Volume   Usage" --value $value --dimensions InstanceId=$instanceId,DeviceID=$deviceId;  
Write-Host "Done";***  

1 个答案:

答案 0 :(得分:0)

与磁盘信息的Win32_LogicalDisk相同,您可以使用win32_OperatingSystem获取内存信息:

#Send To CloudWatch
$metricName = "OS Memory Usage"
$memory = Get-WmiObject -Class win32_OperatingSystem | select TotalVisibleMemorySize,FreePhysicalMemory
$value = ($memory.TotalVisibleMemorySize - $memory.FreePhysicalMemory)  
Write-Host "Posting Memory Usage To CloudWatch: $($value)"
aws cloudwatch put-metric-data --metric-name $metricName --namespace "Memory Usage" --value $value --dimensions InstanceId=$instanceId