VMware Cloud Community
rassini
VMware Employee
VMware Employee
Jump to solution

Script to count VMotions per VM

Looking for a script that will report how many times a VM has migrated witing a give timeframe. -Start date to End date.

Output it to .csv file.

Main purpose is to check if the same VM9s) are constantly migrating with DRS automatic set.

Yes I could set DRS to "partially automatic" and track it manually but that won't capture the overnight activity.

Thanks.

Tags (3)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That was a rather old script, this is an optimised version for the latest PowerCLI version.

$days = 5			# Number of days back
$tasknumber = 999	# Windowsize for task collector
$eventnumber = 100	# Windowsize for event collector

$report = @()

$taskMgr = Get-View TaskManager
$eventMgr = Get-View eventManager

$filter = New-Object VMware.Vim.TaskFilterSpec
$filter.Time = New-Object VMware.Vim.TaskFilterSpecByTime
$filter.Time.beginTime = (Get-Date).AddDays(-$days)
$filter.Time.timeType = "startedTime"

$tcollection = Get-View ($taskMgr.CreateCollectorForTasks($filter))

$dummy = $tcollection.RewindCollector
$tasks = $tcollection.ReadNextTasks($tasknumber)

$tasks | Where-Object {$_.DescriptionId -like "Drm*"} | Sort-Object StartTime | % {
	$row = "" | Select vmName, StartTime, State, From, To
	$row.StartTime = $_.StartTime
	$row.vmName = $_.EntityName
	$row.State = $_.State
	$efilter = New-Object VMware.Vim.EventFilterSpec
	$efilter.eventChainId = $_.EventChainId

	$ecollection = Get-View ($eventMgr.CreateCollectorForEvents($efilter))
	$events = $ecollection.ReadNextEvents($eventnumber)
	foreach($event in $events){
		switch($event.GetType()){
			"VMware.Vim.DrsVmMigratedEvent" {
				$row.From = $event.SourceHost.Name
			}
			"VMware.Vim.VmBeingHotMigratedEvent"{
				$row.To = $event.DestHost.Name
			}
		}
	}
	$report += $row
# By default 32 event collectors are allowed. Destroy this event collector.
	$ecollection.DestroyCollector()
}
$report | Export-Csv "C:\DRS-vmotion.csv" -NoTypeInformation -UseCulture
# By default 32 task collectors are allowed. Destroy this task collector.
$tcollection.DestroyCollector()


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

View solution in original post

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

Did you already look at the scripts I gave in ?


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

Reply
0 Kudos
rassini
VMware Employee
VMware Employee
Jump to solution

LucD,

Will this identify which VM is being moved?

Thanks,

rassini

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes the name of the guest is in the $_.EntityName property.


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That was a rather old script, this is an optimised version for the latest PowerCLI version.

$days = 5			# Number of days back
$tasknumber = 999	# Windowsize for task collector
$eventnumber = 100	# Windowsize for event collector

$report = @()

$taskMgr = Get-View TaskManager
$eventMgr = Get-View eventManager

$filter = New-Object VMware.Vim.TaskFilterSpec
$filter.Time = New-Object VMware.Vim.TaskFilterSpecByTime
$filter.Time.beginTime = (Get-Date).AddDays(-$days)
$filter.Time.timeType = "startedTime"

$tcollection = Get-View ($taskMgr.CreateCollectorForTasks($filter))

$dummy = $tcollection.RewindCollector
$tasks = $tcollection.ReadNextTasks($tasknumber)

$tasks | Where-Object {$_.DescriptionId -like "Drm*"} | Sort-Object StartTime | % {
	$row = "" | Select vmName, StartTime, State, From, To
	$row.StartTime = $_.StartTime
	$row.vmName = $_.EntityName
	$row.State = $_.State
	$efilter = New-Object VMware.Vim.EventFilterSpec
	$efilter.eventChainId = $_.EventChainId

	$ecollection = Get-View ($eventMgr.CreateCollectorForEvents($efilter))
	$events = $ecollection.ReadNextEvents($eventnumber)
	foreach($event in $events){
		switch($event.GetType()){
			"VMware.Vim.DrsVmMigratedEvent" {
				$row.From = $event.SourceHost.Name
			}
			"VMware.Vim.VmBeingHotMigratedEvent"{
				$row.To = $event.DestHost.Name
			}
		}
	}
	$report += $row
# By default 32 event collectors are allowed. Destroy this event collector.
	$ecollection.DestroyCollector()
}
$report | Export-Csv "C:\DRS-vmotion.csv" -NoTypeInformation -UseCulture
# By default 32 task collectors are allowed. Destroy this task collector.
$tcollection.DestroyCollector()


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

Reply
0 Kudos
rassini
VMware Employee
VMware Employee
Jump to solution

LucD,

Thanks. This is exactly what I was looking for.

rassini

Reply
0 Kudos