We want to ensure that our ESX clusters are running under 70% memory utilization. Is it possible to subtract 70 from Memory Usage (%) in the following script?
foreach($cluster in Get-Cluster | Sort-Object ){ $esx = $cluster | Get-VMHost; $ds = Get-Datastore -VMHost $esx | where {$_.Type -eq "VMFS"}; $cluster | Select @{N="Cluster Name";E={$cluster.Name}}, @{N="Memory Usage (%)";E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}} }
Easy enough;
foreach($cluster in Get-Cluster | Sort-Object ){
$esx = $cluster | Get-VMHost;
$ds = Get-Datastore -VMHost $esx | where {$_.Type -eq "VMFS"};
$cluster | Select @{N="Cluster Name";E={$cluster.Name}},
@{N="Memory Usage (%)";E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}},
@{N="Amount Under Threshold";E={70 - [Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}},
@{N="GB Used";E={144/100 * [Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}},
@{N="GB Free";E={144 - (144/100 * [Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2))}}
}
Yup:
foreach($cluster in Get-Cluster | Sort-Object ){
$esx = $cluster | Get-VMHost;
$ds = Get-Datastore -VMHost $esx | where {$_.Type -eq "VMFS"};
$cluster | Select @{N="Cluster Name";E={$cluster.Name}}, @{N="Memory Usage (%)";E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)-70}}
}
That just blindly subtracts 70 from the returned value (which will result in a negative number).
Right, and I have done this but I want the positive number.
If you subtract 70 from a number less than 70, you will get a negative number.
Lets change the question.
if the mem usage is 55%, what would you want this function to output?
If its 75%, what would you want to output?
Here is what I am getting now
Memory Usage (%)
------------
67.27
69
63.13
65.57
45.24
58.49
69.32
70.41
68.34
58.75
25.56
36.9
5.48
2.04
6.07
Here is what I am trying to do with the finished script
ESX Hosts per Cluster * RAM
Not Sure how to add this to the current scipt.
Foreach ($CLUS in Get-Cluster | Sort-Object Name ) {$VMHosts = Get-CLuster $CLUS | Get-VMHost; ($VMhosts.count * 147456) / 1024 }
foreach($cluster in Get-Cluster | Sort-Object ) {
$esx = $cluster | Get-VMHost
$ds = Get-Datastore -VMHost $esx | where {$_.Type -eq "VMFS"}
$cluster | Select @{N="Cluster Name";E={$cluster.Name}},
@{N="Memory Usage (%)";E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}}
}
Memory Usage (%) Under 70 (%) MB RAM Avail (calc X%*144GB*hosts)
------------ ------------ ------------
67.27 x% X GB available
69
63.13
65.57
45.24
58.49
69.32
70.41
68.34
58.75
25.56
36.9
5.48
2.04
6.07
Easy enough;
foreach($cluster in Get-Cluster | Sort-Object ){
$esx = $cluster | Get-VMHost;
$ds = Get-Datastore -VMHost $esx | where {$_.Type -eq "VMFS"};
$cluster | Select @{N="Cluster Name";E={$cluster.Name}},
@{N="Memory Usage (%)";E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}},
@{N="Amount Under Threshold";E={70 - [Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}},
@{N="GB Used";E={144/100 * [Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}},
@{N="GB Free";E={144 - (144/100 * [Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2))}}
}
Thanks for all of your help!!! This is very close to what I am trying to do.
The last column should be
3.26%*144GB*hosts
(Amount Under Threshold * 147456) * Number Of ESX Hosts
This will get the number of ESX hosts
Foreach ($CLUS in Get-Cluster | Sort-Object Name ) {$VMHosts = Get-CLuster $CLUS | Get-VMHost; ($VMhosts.count * 147456) / 1024 }
Hi,
Why does the result is always the same for all of my clusters ?
0Cluster Name : High Performance Production ClusterMemory Usage (%) : 0Amount Under Threshold : 70GB Used : 0GB Free : 1440Cluster Name : Production ClusterMemory Usage (%) : 0Amount Under Threshold : 70GB Used : 0GB Free : 1440Cluster Name : Test&Dev ClusterMemory Usage (%) : 0Amount Under Threshold : 70GB Used : 0
and here's the script after combining the snippet that you suggested before:
foreach($cluster in Get-Cluster | Sort-Object Name) { $esx = Get-CLuster $cluster | Get-VMHost ($VMhosts.count * 147456) / 1024 $ds = Get-Datastore -VMHost $esx | where {$_.Type -eq "VMFS"}; $cluster | Select @{N="Cluster Name";E={$cluster.Name}}, @{N="Memory Usage (%)";E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}}, @{N="Amount Under Threshold";E={70 - [Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}}, @{N="GB Used";E={144/100 * [Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}}, @{N="GB Free";E={144 - (144/100 * [Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2))}} }