我希望拉动cloudwatch指标以获得卷/磁盘空间利用率,而且我能够使用下面的aws powershell脚本来做到这一点,我还试图找出一种方法来关联卷/磁盘的大小Windows和Linux无法找到方法。代码中的大小并没有真正提供正确的大小,因为一个实例可能附加了多个卷。
有什么建议吗?
function GetEC2InstanceName ($instanceId)
{
$tags = (Get-EC2Instance).RunningInstance | Where-Object {$_.instanceId -eq $instanceId} |select Tag
$tagName = $tags.Tag | Where-Object {$_.Key -eq "Name"} | select -ExpandProperty Value
return $tagName
}
$volumesByInstance = @{}
$mountByInstance = @{}
Get-CWMetricList -Namespace 'System/Linux' -MetricName 'DiskSpaceUtilization' | % {
$instanceId = ($_.Dimensions | Where-Object {$_.Name -eq 'InstanceId'} | Select -ExpandProperty Value)
$mountpath = ($_.Dimensions | Where-Object {$_.Name -eq 'MountPath'} | Select -ExpandProperty Value)
$volume = ($_.Dimensions | Where-Object {$_.Name -eq 'Filesystem'} | Select -ExpandProperty Value)
if (-not $volumesByInstance.ContainsKey($volume)) {
$volumesByInstance[$instanceId] = @($volume)
$mountByInstance[$instanceId] = @($mountpath)
} else {
$volumesByInstance[$instanceId] += $volume
$mountByInstance[$instanceId] += $mountpath
}
}
$endDate = Get-Date
$startDate = $endDate.AddDays(-2)
$period = [TimeSpan]::FromDays(1).TotalSeconds
$linux = @()
$volumesByInstance.Keys | % {
$instanceId = $_
$mountByInstance[$instanceId] | % {
$mountpath = $_
$volumesByInstance[$instanceId] | % {
$filesystem = $_
(Get-CWMetricStatistics `
-Namespace 'System/Linux' -MetricName 'DiskSpaceUtilization' `
-Dimension @{name="InstanceId"; value="$instanceId"}, @{name="Filesystem"; value="$filesystem"}, @{name="MountPath"; value="$mountpath"} `
-StartTime $startDate -EndTime $endDate -Period $period `
-Statistics ('Maximum', 'Minimum')).Datapoints | % {
$InstanceName = GetEC2InstanceName $instanceId
$size = (Get-EC2Volume -filter @(
@{
name='attachment.instance-id'
values=$instanceId
}
)) | Select -ExpandProperty Size
$linuxObject = New-Object -TypeName PSObject -Prop (@{
'InstanceId'=$instanceId;
'Filesystem'=$filesystem;
'MountPath'=$mountpath;
'Max Utilization (%)'=$_.Maximum;
'Min Utilization (%)'=$_.Minimum;
'Name'=$InstanceName;
'Size'=$size;
'Timestamp'=$_.Timestamp
})
$linux += $linuxObject
}
}
}
}
答案 0 :(得分:0)
尝试:
$diskC = Get-WmiObject Win32_LogicalDisk -ComputerName remotecomputer -Filter "DeviceID='C:'" |
Select-Object Size
$diskC.Size