VMware Cloud Community
E4F
Contributor
Contributor
Jump to solution

mem.usage.average Question

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

0 Kudos
1 Solution

Accepted Solutions
mcowger
Immortal
Immortal
Jump to solution

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

}

--Matt VCDX #52 blog.cowger.us

View solution in original post

0 Kudos
7 Replies
mcowger
Immortal
Immortal
Jump to solution

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).

--Matt VCDX #52 blog.cowger.us
E4F
Contributor
Contributor
Jump to solution

Right, and I have done this but I want the positive number.

0 Kudos
mcowger
Immortal
Immortal
Jump to solution

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?

--Matt VCDX #52 blog.cowger.us
E4F
Contributor
Contributor
Jump to solution

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

0 Kudos
mcowger
Immortal
Immortal
Jump to solution

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

}

--Matt VCDX #52 blog.cowger.us
0 Kudos
E4F
Contributor
Contributor
Jump to solution

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 }

0 Kudos
AlbertWT
Virtuoso
Virtuoso
Jump to solution

Hi,

Why does the result is always the same for all of my clusters ?

0
Cluster Name           : High Performance Production Cluster
Memory Usage (%)       : 0
Amount Under Threshold : 70
GB Used                : 0
GB Free                : 144
0
Cluster Name           : Production Cluster
Memory Usage (%)       : 0
Amount Under Threshold : 70
GB Used                : 0
GB Free                : 144
0
Cluster Name           : Test&Dev Cluster
Memory Usage (%)       : 0
Amount Under Threshold : 70
GB 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))}}  }

/* Please feel free to provide any comments or input you may have. */
0 Kudos