VMware Cloud Community
Ambarish08
Contributor
Contributor

Looking for Power Cli script which will shoot an Email alert if any VM is vMotion more than 05 times in same Day triggered by DRS vmotion ?

Hi Team,

Looking for Power Cli script  which will shoot an Email alert if any VM is vMotion more than 05 times in same Day triggered by DRS  vmotion ?

9 Replies
LucD
Leadership
Leadership

Is "same day" the last 24 hours or today starting at midnight?

Will you schedule that script in an external task scheduler (like Windows Task Scheduler)?


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

Reply
0 Kudos
Ambarish08
Contributor
Contributor

In last 24 hours if the VM is moving more than 05 times then script should trigger report at MidNight or if possible

When 05th  vmotion is completed for VM in a Single Day.

I would like to schedule hourly check using same script in Windows PowerCli

Reply
0 Kudos
LucD
Leadership
Leadership

Ok, but the question was, which scheduler.

If on a Windows station, the Windows Task scheduler can be used.

If on a Linux station, something like cron can be used.


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

Reply
0 Kudos
Ambarish08
Contributor
Contributor

on a Windows station, the Windows Task scheduler 

Reply
0 Kudos
scott28tt
VMware Employee
VMware Employee

Out of interest, do you have what you consider to be excessive migrations and if so what sort of issue is that causing you?


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
Reply
0 Kudos
Ambarish08
Contributor
Contributor

In this case  we would like to track specific VM migrations which becomes of Victim of vmotions due to DRS

One reason we found that Parawise imbalance in vSphere 6.5

It is noticed that due to frequent vmotions ( vmware stun) operations inside applications Guest are getting impacted.

So need to identify VM which preferred by DRS into Migrations list 

So accordingly  I can tune Guest OS / VMware (vmx) file parameters for specific VM

Reply
0 Kudos
scott28tt
VMware Employee
VMware Employee

Ok thanks.


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
Reply
0 Kudos
LucD
Leadership
Leadership

This is a two-part answer.

1) The script to monitor DRS initiated vMotions

You need to save this in a .ps1 file.

That file will be called by the Scheduled Task

$threshold = 5

$head = @'

<Title>DRS vMotion Report</Title>

<style>

body { background-color:#E5E4E2;

       font-family:Monospace;

       font-size:10pt; }

td, th { border:0px solid black;

         border-collapse:collapse;

         white-space:pre; }

th { color:white;

     background-color:black; }

table, tr, td, th { padding: 2px; margin: 0px ;white-space:pre; }

tr:nth-child(odd) {background-color: lightgray}

table { width:95%;margin-left:5px; margin-bottom:20px;}

h2 {

font-family:Tahoma;

color:#6D7B8D;

}

.footer

{ color:green;

  margin-left:10px;

  font-family:Tahoma;

  font-size:8pt;

  font-style:italic;

}

</style>

'@


$vms = Get-VM

$start = (Get-Date).AddHours(-24)


$report = Get-VIEvent -Entity $vms -Start $start -MaxSamples ([int]::MaxValue) |

where{$_ -is [VMware.Vim.DrsVmMigratedEvent]} |

Group-Object -Property {$_.Vm.Name} |

ForEach-Object -Process {

    if($_.Group.Count -ge $threshold){

        New-Object -TypeName PSObject -Property ([ordered]@{

            VM = $_.Name

            StartInterval = $start

            NumberofDRSMigrations = $_.Group.Count

        })

    }

}


if($report){

    $sMail = @{

        From = 'vcsa@domain'

        To = 'me@domain'

        Subject = 'Excessive DRS migrations'

        Body = $report | ConvertTo-Html -Head $head | Out-String

        BodyAsHtml = $true

        SmtpServer = 'mail.domain'

    }

    Send-MailMessage @sMail

}

2) The creation of a Scheduled Task.

Note that you need to run this from a PS session started As Administrator

This script needs the ScheduledTasks module to be installed.
This Scheduled Task will run every hour for 7 days.

#Requires -Modules ScheduledTasks


$user = 'NT AUTHORITY\SYSTEM'


$sAction = @{

   Execute = 'Powershell.exe'

   Argument = '-NoProfile -WindowStyle Hidden -File C:\Scripts\DRS-Report.ps1'

}

$action = New-ScheduledTaskAction @sAction


$sTrigger = @{

   Once = $true

   At = '0am'

   RepetitionInterval = New-TimeSpan -Hours 1

   RepetitionDuration = New-TimeSpan -Days 7

}

$trigger = New-ScheduledTaskTrigger @sTrigger


$principal = New-ScheduledTaskPrincipal -UserId $user -LogonType 'ServiceAccount' -RunLevel Highest

$sTask = @{

   Action = $action

   Trigger = $trigger

   Principal = $principal

   TaskName = 'Test Task'

   Description = 'Testing a scheduled task'

}

$task = Register-ScheduledTask @sTask


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

Ambarish08
Contributor
Contributor

Thanks i will check & get back to you...

Reply
0 Kudos