1

I am looking to pull the cloudwatch metrics for volume/disk space utilization and i was able to do it with below aws powershell script additionally to that i am also trying to figure out a way to associate size of the volume/Disk in windows and linux not able to figure out a way.The size in the code doesnot really provide the correct size as one instance may have more then one volume attached.

Any suggestions ?

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
        }
    }
 }

}

1 Answers1

0

Try:

$diskC = Get-WmiObject Win32_LogicalDisk -ComputerName remotecomputer -Filter "DeviceID='C:'" |
Select-Object Size

$diskC.Size
ExploringApple
  • 1,348
  • 2
  • 17
  • 30