VMware Cloud Community
Tat-Man
Enthusiast
Enthusiast

Average Memory Memory utilisation report

Afternoon.  I have a client that has a small but heavily used environment.

2 x clusters with 2 x hosts in each cluster.  They have now unable to have a failure as they are consuming too much memory to allow us to perform patching as we can not put a host into maintenance as there are now not enough memory to support 1 host out of the cluster.

I am trying to get a report out that shows the average memory utilisation of each VM over 30 days.

The script is below.

 #Editable Varialbles.

$vCenterServerName = "removed"

$User = "removed"

$Password = "removed"

$Export = "removed"

$From = "removed"

$To = "removed"

$Subject = "Average Capacity Report"

$emailbody = "Please find attached the Average Capacity Report"

$SMTPserver = "removed"

#Non-Editable Variables.

$Attachment = $Export

#Virtual Centre Connection.

Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

Connect-VIServer $vCenterServerName -AllLinked -User $User -Password $Password

#Collection Virtual Server Information.

Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name, Host, NumCpu, MemoryMB, `

@{N="CPU Usage (Average), Mhz" ; E={[Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}}, `

@{N="Memory Usage (Average), %" ; E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}} , `

@{N="Network Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat net.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}} , `

@{N="Disk Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat disk.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}} |`

#Update export file location to be relevant to your network.

Export-Csv $Export

#Send Email Message.

Send-Mailmessage -From $From -To $To -Subject $Subject -Attachments $Attachment $emailbody -Priority Normal -SmtpServer $SMTPServer

Disconnect-VIServer $vCenterServerName -Force -confirm:$false 

 

When the script runs and emails it produces a csv and emails it which is what I am after but the output has columns in it below.  I am able to populate all fields except the Host field. What do I need to add to my script to populate the Host field.  I thought I could pull it from the Host area when looking at the summary of the VM.

NameHostNumCpuMemoryMBCPU Usage (Average), MhzMemory Usage (Average), %Network Usage (Average), KBps

Disk Usage (Average), KBps

0 Kudos
12 Replies
LucD
Leadership
Leadership

On the Select line, replace the Host entry with the calculated property

@{N='Host';E={$_.VMHost.Name}}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Tat-Man
Enthusiast
Enthusiast

Thank you very much. appreciate your assistance.

I edited the following line -

 Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name, Host, NumCpu, MemoryMB, ` 

To be

 Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name, @{N='Host';E={$_.VMHost.Name}}, NumCpu, MemoryMB, ` 

 

Is there a way to add in the cluster name?  Could I add in @{N='Cluster';E={$_.Cluster.Name}}

 

0 Kudos
LucD
Leadership
Leadership

No, but you could add

@{N='Cluster';E={(Get-Cluster -VM $_).Name}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Tat-Man
Enthusiast
Enthusiast

Thank you kind sir.

 

Much appreciative for the assistance.

 

0 Kudos
Tat-Man
Enthusiast
Enthusiast

Last one.

Looking at getting the active memory utilisation of the VM.

 

So if assigned 20GB of memory but actually using 1.8GB, what would I require.

 

Thanks Again.

0 Kudos
LucD
Leadership
Leadership

Isn't that what 'Memory Usage (Average), %' is already showing you?

Another option is looking at the Host memory used.

@{N='Memory Used MB';E={$_.ExtensionData.Summary.QuickStats.HostMemoryUsage}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Tat-Man
Enthusiast
Enthusiast

Hi Lucid.

Maybe I was incorrect in my explanation.

I want to see the memory usage of the VM. not the % in ths case the the % is a average over the days of collection..

So if they have 20GB assigned but are at this time consuming 1.8GB of memory on the server, can I show that as per the screen capture.

If not that is fine.

 

0 Kudos
LucD
Leadership
Leadership

Did you try the calculated property as well?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Tat-Man
Enthusiast
Enthusiast

Yes that gave me what was assigned to the VM not was in use by the VM.

I am not script writer, I google for them and try to make them work, so I might be wrong in my question.

I see utilisation of 20GB but 1.8GB memory Active for a VM.

0 Kudos
LucD
Leadership
Leadership

That would be

@{N='Memory Used MB';E={$_.ExtensionData.Summary.QuickStats.ActiveMemory}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Tat-Man
Enthusiast
Enthusiast

Thanks for that, but unfortunately field is blank.

 

NameClusterHostNumCpuMemoryMBCurrent Memory Used, MBMemory Usage (Average), %CPU Usage (Average), MhzNetwork Usage (Average), KBpsDisk Usage (Average), KBps
removedremovedremoved420480 7.52428.5364.83341.27
removedremovedremoved416384 1.3234.680.053.59

 

they would hopefully read the 2200 for the first one and  163 as MB for Current Memory Used in MB.

Script - #Virtual Centre Connection.

Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

Connect-VIServer $vCenterServerName -AllLinked -User $User -Password $Password

#Collection Virtual Server Information.

Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name, @{N='Cluster';E={(Get-Cluster -VM $_).Name}}, @{N='Host';E={$_.VMHost.Name}}, NumCpu, MemoryMB, `

@{N="Current Memory Used, MB";E={$_.ExtensionData.Summary.QuickStats.MemoryActive}}, ` @{N="Memory Usage (Average), %" ; E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}}, `

@{N="CPU Usage (Average), Mhz" ; E={[Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}}, `

@{N="Network Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat net.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}}, `

@{N="Disk Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat disk.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}} |`

#Update export file location to be relevant to your network.

Export-Csv $Export

#Send Email Message.

Send-Mailmessage -From $From -To $To -Subject $Subject -Attachments $Attachment $emailbody -Priority Normal -SmtpServer $SMTPServer

Disconnect-VIServer $vCenterServerName -Force -confirm:$false

0 Kudos
LucD
Leadership
Leadership

That is ActiveMemory, not MemoryActive.


PS1: you don't need those back-ticks at the end of the lines
PS2: if your PowerCLI installation is > 6.5R1, you don't need the Add-PSSnapin anymore

Connect-VIServer $vCenterServerName -AllLinked -User $User -Password $Password

#Collection Virtual Server Information.

Get-VM | Where-Object { $_.PowerState -eq "PoweredOn" } |
Select-Object Name, @{N = 'Cluster'; E = { (Get-Cluster -VM $_).Name } }, @{N = 'Host'; E = { $_.VMHost.Name } }, NumCpu, MemoryMB,
@{N = "Current Memory Used, MB"; E = { $_.ExtensionData.Summary.QuickStats.ActiveMemory } },
@{N = "Memory Usage (Average), %" ; E = { [Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average), 2) } },
@{N = "CPU Usage (Average), Mhz" ; E = { [Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average), 2) } },
@{N = "Network Usage (Average), KBps" ; E = { [Math]::Round((($_ | Get-Stat -Stat net.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average), 2) } },
@{N = "Disk Usage (Average), KBps" ; E = { [Math]::Round((($_ | Get-Stat -Stat disk.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average), 2) } } |

#Update export file location to be relevant to your network.
Export-Csv $Export
#Send Email Message.
Send-MailMessage -From $From -To $To -Subject $Subject -Attachments $Attachment $emailbody -Priority Normal -SmtpServer $SMTPServer

Disconnect-VIServer $vCenterServerName -Force -Confirm:$false

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos