VMware Cloud Community
ganesh24pal
Contributor
Contributor

How can we check ESX Host Resource utilization through PowerCLI ?

Helo Team,

How can we check ESX Host Resource utilization through PowerCLI ?

Means CPU , Disk, Memory, Network utilization . So we able to monitor and make decision on right time.

I have Two ESX server 5.

I tried some command but not succeed need your help.

Thanks and Regards,

Ganesh Pal

Reply
0 Kudos
18 Replies
naiksidd
Enthusiast
Enthusiast

here are two fab links from LucD

PowerCLI & vSphere statistics - Part 1 - The basics - LucD notes

PowerCLI & vSphere statistics – Part 2 – Come together - LucD notes

I think this will help you a lot, (it did help me)

this is for reference , just a pointer. may be he has better script now

Reply
0 Kudos
ganesh24pal
Contributor
Contributor

Helo Team,

I am getting below error.

==============================================================================================

PowerCLI C:\> Get-Stat -Entity (Get-VMHost $esxName) -Stat cpu.usage.average -Ma

xSamples 1 -IntervalMins 5

Get-VMHost : Cannot validate argument on parameter 'Name'. The argument is null

or empty. Supply an argument that is not null or empty and then try the comman

d again.

At line:1 char:29

+ Get-Stat -Entity (Get-VMHost <<<<  $esxName) -Stat cpu.usage.average -MaxSamp

les 1 -IntervalMins 5

    + CategoryInfo          : InvalidData: (Smiley Happy https://communities.vmware.com/Get-VMHost, ParameterBindingV

  alidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom

  ation.ViCore.Cmdlets.Commands.GetVMHost

PowerCLI C:\>

Regards,

Ganesh Pal 8451000743

Reply
0 Kudos
LucD
Leadership
Leadership

What is in the variable $esxName, and how did you get the value in there ?

Does

Get-VMHost -Name $esxName

return anything ?


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

Reply
0 Kudos
kunaludapi
Expert
Expert

Try below script.

$vmhosts =  get-vmhost

Foreach ($esxname in $vmhosts) {

    $CPUstat = Get-Stat -Entity $esxname -Stat cpu.usage.average -IntervalMins 5 | Measure-Object value -Average | select-object -ExpandProperty Average

    $memstat = Get-Stat -Entity $esxname -Stat mem.usage.average -IntervalMins 5 | Measure-Object value -Average | select-object -ExpandProperty Average

    $obj =  New-Object PSobject

    $obj | Add-Member -MemberType NoteProperty -Name EsxiName -Value $esxname.Name

    $obj | Add-Member -MemberType NoteProperty -Name cpuaverage -Value $([math]::round($CPUstat))

    $obj | Add-Member -MemberType NoteProperty -Name Memaverage -Value $([math]::round($memstat))

    $obj

}

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
Reply
0 Kudos
ganesh24pal
Contributor
Contributor

Helo LucD,

no its giving error.

===============================

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-VMHo
st -Name $esxName
Get-VMHost : Cannot validate argument on parameter 'Name'. The argument is null
or empty. Supply an argument that is not null or empty and then try the comman
d again.
At line:1 char:17
+ Get-VMHost -Name <<<<  $esxName
    + CategoryInfo          : InvalidData: (:) [Get-VMHost], ParameterBindingV
   alidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.GetVMHost

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

=========================================================================

-------

Reply
0 Kudos
LucD
Leadership
Leadership

There doesn't seem to be any value in the variable $esxName.

Can you put the hostname of one of your ESXi servers in there ?


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

Reply
0 Kudos
ganesh24pal
Contributor
Contributor

Helo Kunaludapi,

Its works, but hoe can I export it in *.CSV format, ands also want to mail with thrash hold.

==============================================================

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> $vmhosts
=  get-vmhost
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Foreach
($esxname in $vmhosts) {
>>
>>     $CPUstat = Get-Stat -Entity $esxname -Stat cpu.usage.average -IntervalMin
s 5 | Measure-Object value -Average | select-object -ExpandProperty Average
>>
>>     $memstat = Get-Stat -Entity $esxname -Stat mem.usage.average -IntervalMin
s 5 | Measure-Object value -Average | select-object -ExpandProperty Average
>>
>>     $obj =  New-Object PSobject
>>
>>     $obj | Add-Member -MemberType NoteProperty -Name EsxiName -Value $esxname
.Name
>>
>>     $obj | Add-Member -MemberType NoteProperty -Name cpuaverage -Value $([mat
h]::round($CPUstat))
>>
>>     $obj | Add-Member -MemberType NoteProperty -Name Memaverage -Value $([mat
h]::round($memstat))
>>
>>     $obj
>>
>> }
>>

EsxiName                                   cpuaverage                Memaverage
--------                                   ----------                ----------
10.7.1.232                                         26                        85


PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

=============================================================================

Thanks,

Ganesh Pal

Reply
0 Kudos
ganesh24pal
Contributor
Contributor

Dear LucD,

Now its works, I don't Know how ?

Please find output.

======================================

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-VMHo
st -Name $esxName

WARNING: 3 columns do not fit into the display and were removed.

Name                 ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz
----                 --------------- ---------- ------ ----------- -----------
10.7.1.232           Connected       PoweredOn       8        7672       19200


PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

============================================================================================

Thanks,

Ganesh Pal

Reply
0 Kudos
kunaludapi
Expert
Expert

$report = @()

$vmhosts =  get-vmhost

Foreach ($esxname in $vmhosts) {

    $CPUstat = Get-Stat -Entity $esxname -Stat cpu.usage.average -IntervalMins 5 | Measure-Object value -Average | select-object -ExpandProperty Average

    $memstat = Get-Stat -Entity $esxname -Stat mem.usage.average -IntervalMins 5 | Measure-Object value -Average | select-object -ExpandProperty Average

    $obj =  New-Object PSobject

    $obj | Add-Member -MemberType NoteProperty -Name EsxiName -Value $esxname.Name

    $obj | Add-Member -MemberType NoteProperty -Name cpuaverage -Value $([math]::round($CPUstat))

    $obj | Add-Member -MemberType NoteProperty -Name Memaverage -Value $([math]::round($memstat))

    $report += $obj

}

$report | export-csv -NoTypeInformation c:\statsreport.csv

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
kunaludapi
Expert
Expert

After the same script include below script, and modify your exchange server address and emial ids, it will send email for treshold.

$EmailBody = @()

foreach ($stat in $report) {

    if ($stat.cpuaverage -gt 80 -or $stat.memaverage -gt 80) {

        $emailbody += $stat

    }

}

$emailbody | export-csv -NoTypeInformation c:\temp\Statswithalerts.csv

Send-MailMessage -SmtpServer smtp.domain.com -From vCenteralerts-noreply@domain.com -To Youremail@domain.com -Subject "Attachement for vCenter threshold" -Attachments c:\temp\Statswithalerts.csv

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
ganesh24pal
Contributor
Contributor

Hi Kunaldapi,

Thanks its works, I have some query.

1) is it output in percentage ?

2) we can sent alert mail to System Admin ? Purpose of this, If HOST have Overload then System Admin can move its VMs to available Host.

Reply
0 Kudos
kunaludapi
Expert
Expert

1) Yes, Output is in %.

2) Yes, You can send email to System admin, you will need to specify system admin email in the -to.

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
Reply
0 Kudos
ganesh24pal
Contributor
Contributor

Hi Kunaludapi,

Thanks, its works fine now. I have some more query.

1) Could I use this script run every 15 minutes, how can I do this ?

2) In my Environment there is 5 Vsphere  server , I want to set if 1 server having CPU utilization above 80 % or Memory Utilization above 90%, then VM's should move from Overloaded to available Vsphere server which utilizing less resource on that time.

3) Please help me where I get best documents to learn more this command or script its interesting . I can manage lots of thins with PowerCLI.

Thanks a lot for guiding.


Reply
0 Kudos
kunaludapi
Expert
Expert

1) Could I use this script run every 15 minutes, how can I do this ?

---- use below link to schedule a task

http://www.virtu-al.net/2009/07/10/running-a-powercli-scheduled-task/

2) In my Environment there is 5 Vsphere  server , I want to set if 1 server having CPU utilization above 80 % or Memory Utilization above 90%, then VM's should move from Overloaded to available Vsphere server which utilizing less resource on that time.

---- Use vcenter DRS feature to auto load balance

3) Please help me where I get best documents to learn more this command or script its interesting . I can manage lots of thins with PowerCLI.

---- To learn a powercli, I would suggest you to get little hands on and foundation on Microsoft Powershell, for Powershell good book and blogs are available on the the internet,

Below are the some of the good books to learn powercli

     Learning Powercli - Pact Publishing -- by authers Robert van den Nieuwendijk

     VMware vSphere Powercli Reference: Automating vSphere Administration -- by 5 authors -- including LucD Smiley Happy

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
Reply
0 Kudos
ganesh24pal
Contributor
Contributor

2) In my Environment there is 5 Vsphere  server , I want to set if 1 server having CPU utilization above 80 % or Memory Utilization above 90%, then VM's should move from Overloaded to available Vsphere server which utilizing less resource on that time.

---- Use vcenter DRS feature to auto load balance

Q:- is it possible through PowerCLI ?Myself looking to do vi power CLI script.

Reply
0 Kudos
kunaludapi
Expert
Expert

Check out below link may be helpful, DRS is always 100% best practice and best way.


http://ryanmangansitblog.com/2013/03/08/vmware-host-cluster-vm-memory-balancing-script-implementatio...

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
Reply
0 Kudos
ganesh24pal
Contributor
Contributor


Dear Kunaludapi,

Thanks for your support, myself convert in to the script and output is coming  but giving some problem.

=====================error======================


Name                           Port  User
----                           ----  ----
vsa.ganesh.com                 443   root
Property 'Count' cannot be found on this object. Make sure that it exists.
At C:\Scheduled tasks\Vmware DRS Script.ps1:87 char:39
+        $strDest = $objHosts[$objHosts. <<<< Count-1].Name;
    + CategoryInfo          : InvalidOperation: (.:OperatorToken) [], RuntimeE
   xception
    + FullyQualifiedErrorId : PropertyNotFoundStrict


==================================================================

PFA:- both script and output.

Reply
0 Kudos
ganesh24pal
Contributor
Contributor

Dear Kunaludapi and Team,

I run the script on my production environment and its try to moving but facing some error regarding destination. We had not configured cluster.

==========================Error Output==========================

Move-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.Vi
Core.Types.V1.Inventory.VIContainer' required by parameter 'Destination'. Speci
fied method is not supported.
At C:\Scheduled tasks\Vmware DRS Script.ps1:103 char:53
+                        $objVM | move-vm -destination <<<<  (get-vmhost $strDe
st);
    + CategoryInfo          : InvalidArgument: (:) [Move-VM], ParameterBinding
   Exception
    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor
   e.Cmdlets.Commands.MoveVM

Move-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.Vi
Core.Types.V1.Inventory.VIContainer' required by parameter 'Destination'. Speci
fied method is not supported.
At C:\Scheduled tasks\Vmware DRS Script.ps1:103 char:53
+                        $objVM | move-vm -destination <<<<  (get-vmhost $strDe
st);
    + CategoryInfo          : InvalidArgument: (:) [Move-VM], ParameterBinding
   Exception
    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor
   e.Cmdlets.Commands.MoveVM

Move-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.Vi
Core.Types.V1.Inventory.VIContainer' required by parameter 'Destination'. Speci
fied method is not supported.
At C:\Scheduled tasks\Vmware DRS Script.ps1:103 char:53
+                        $objVM | move-vm -destination <<<<  (get-vmhost $strDe
st);
    + CategoryInfo          : InvalidArgument: (:) [Move-VM], ParameterBinding
   Exception
    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor
   e.Cmdlets.Commands.MoveVM

Move-VM : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.Vi
Core.Types.V1.Inventory.VIContainer' required by parameter 'Destination'. Speci
fied method is not supported.
At C:\Scheduled tasks\Vmware DRS Script.ps1:103 char:53
+                        $objVM | move-vm -destination <<<<  (get-vmhost $strDe
st);
    + CategoryInfo          : InvalidArgument: (:) [Move-VM], ParameterBinding
   Exception
    + FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor
   e.Cmdlets.Commands.MoveVM

======================================================================

Reply
0 Kudos