Virtual Machine CPU RDY Alarm – PowerCLI Script

As I know and I have read it in many blogs that DRS is not sensitive to CPU ready time and when your cluster has enough CPU frequency and memory, cluster is balanced. There is a simple solution to resolve the issue or reduce the issue in your environment. Of course, this is not true solution and you need to analyze your platform, calculate your requirements especially CPU cores for your VMs and add more physical cores to by adding more physical servers.

Create Alarm

At first step, you need to define an alarm in your vCenter for virtual machine CPU ready:

  1. Create a user-defined alarm and enter a name as you wish.
  2. In Triggers tab, select “VM CPU Ready Time (ms) and define waning and error values and condition length. Default values are fine for most platforms.
  3. Just that, click on OK and our job is done.
  4. If you have virtual machines with high CPU ready, it will be detected by vCenter and alarm will be generated.
New Alarm
Alarm Triggers

Note: Please copy your alarm name and paste it in the below script. Also you can change exception time, if you need to run it after working hours.

The Script

#====================================================================#
#   SCRIPT:        CPU_Ready_Alarm.ps1                               #
#   FUNCTION:      Detect, analyaze CPU ready alarms and migrate     # 
#                  problomatic VMs to another host                   #
#   CREATED:       30/01/2015                                        #
#   MODIFIED:      10/08/2015                                        #
#   OWNER:         Davoud Teimoru / Teimouri.net                     #
#   VERSION:       v.1.1                                             #
#====================================================================#
#   CHANGELOG:                                                       #
#                                                                    #
#   v.1.1                                                            #
#                                                                    #
#    - Add exception time for detection and migration                #
#                                                                    #
#   USAGE:                                                           #
#                                                                    #
#    - Edit script in Windows PowerShell ISE / Notepad ++            #
#    - Connect to your vCenter via PowerCLI and run the script       #
#    - Enjoy!                                                        #
#                                                                    #
#                                                                    #
#   NOTE:                                                            #
#                                                                    #
#    - Run this script only on a vCenter                             #
#    - This is a script for help you but it's not true solution      #
#                                                                    #
#                                                                    #
#====================================================================#     
      

$StartTime="09:00"
$EndTime="23:00"
Do{
    If (((Get-Date -Format HH:MM) -lt $StartTime) -Or ((Get-Date -Format HH:MM) -gt $EndTime))
    {
        $StartTimeSecond=((Get-Date -Format HH:MM) - $StartTime) /60
        CLS
        Write-Host
        Write-Host "There is an exception time and script can't be run during the exception."
        Write-Host "The script will be run at $startTime.$StartTimeSecond"
        Write-Host
        Break;
    }
    CLS #Clear Screen
    $Alarms=$null #Remove all items from Alarms array
    $VMS=$null #
    Write-Host "Starting to alarm processing"
    $Alarms=Get-Datacenter | where {$_.ExtensionData.triggeredAlarmState} | %{
      $_.ExtensionData.triggeredAlarmState |Select @{N="Entity";E={Get-View $_.Entity | Select -ExpandProperty Name}},
        @{N="Alarm";E={Get-View $_.Alarm | Select -ExpandProperty Info | Select -ExpandProperty Name}}
    }

    Foreach ($Alarm in $Alarms)
    {
        If ($Alarm.Alarm -eq "Virtual machine cpu ready") #Replace this alarm name with your alarm name.
            {
                $VMS=$Alarm.Entity
            }

    }
    $ESXiHosts=Get-VMHost|select-object Name
    $ArrayList=[System.Collections.ArrayList]@()
    Foreach ($ESXiHost in $ESXiHosts)
    {
        $ArrayList.Add($ESXiHost)|Out-Null
    }
    Foreach ($VM in $VMS)
    {

        $TheVMHost=@()
        $TheVMHost=Get-VMHost -VM $VM | Select-Object Name |Select -ExpandProperty Name
        $ArrayList.Remove($TheVMHost)
        $MigrateHost=$ArrayList|Get-Random|Select -ExpandProperty Name
        Write-Host "Moving $VM from $TheVMHost to $MigrateHost"
        Move-VM -VM $VM -Destination $MigrateHost
    }
CLS
If ($VMS -eq $Null) 
{
  Write-Host "No Alarm is detected!"
  }
Write-Host "Starting to sleep for 900 seconds"
Start-Sleep -S 900
}
While (1 -ne 1)

Further Reading

VMware Tools Client – Interact with a VM without Network Connectivity

Java.net.SocketException: No buffer space available – VMware View Connection Server

[Review]: What’s Remote Direct Memory Access(RDMA)?

Davoud Teimouri

Professional blogger, vExpert 2015/2016/2017/2018/2019/2020/2021/2022/2023, vExpert NSX, vExpert PRO, vExpert Security, vExpert EUC, VCA, MCITP. This blog is started with simple posts and now, it has large following readers.

Leave a Reply

Your email address will not be published. Required fields are marked *