htwnrver
Enthusiast
Enthusiast

Log of storage vmotions

Jump to solution

I'm looking for a powerCLI script that will create a log of storage vmotions done for the month.  I think it will involve using Get-ViEvent but not sure how to corelate the VM name, source datastore, destination datastore, and date into one report.  Ideally I would like to see it like this.

Date, VM Name, Source_datastore, Destination_datastore

Thank you,

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

You can fetch the svMotions like this

$events = Get-VIEvent -MaxSamples [int]::MaxValue -Start (Get-Date).AddMonths(-1) |
where {$_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.relocate"}

but afaik the source and destination datastores are not in the event.

It could be that this is changed in vSphere 5 (don't have access to a vSphere 5 box right now).


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

View solution in original post

0 Kudos
17 Replies
LucD
Leadership
Leadership

You can fetch the svMotions like this

$events = Get-VIEvent -MaxSamples [int]::MaxValue -Start (Get-Date).AddMonths(-1) |
where {$_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.relocate"}

but afaik the source and destination datastores are not in the event.

It could be that this is changed in vSphere 5 (don't have access to a vSphere 5 box right now).


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

0 Kudos
htwnrver
Enthusiast
Enthusiast

Looks like a good start.  With powercli 5.0 I get the following

Get-VIEvent : Cannot bind parameter 'MaxSamples'. Cannot convert value "[int]::
MaxValue" to type "System.Int32". Error: "Input string was not in a correct for
mat."
At line:1 char:34
0 Kudos
LucD
Leadership
Leadership

That expression should be in parenthesis

$events = Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddMonths(-1) |
where {$_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.relocate"}


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

0 Kudos
Shoganator
Enthusiast
Enthusiast

Hi htwnrver,

I am not sure why [int]::MaxValue is not working - I believe this is specifying to the cmdlet to use the maximum supported value for this type of query. So instead of "[int]::MaxValue" you could try replacing that with a standard integer value such as 500. This should in theory get the last 500 samples then and the cmdlet should work for you.

*edit* - thanks LucD Smiley Happy Parenthesis explains why it wasn't working!

Also another small typo correction for LucD's script - in the DescriptionId part - it is missing the "s". Furthermore, for vSphere 5 I believe the DescriptionId is a bit different. Try the following:

$events = Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddMonths(-1) |
where {$_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "StorageResourceManager.applyRecommendation"}

StorageResourceManager.applyRecommendation is what I get as a DescriptionId for tasks when I storage vMotion a VM in my vSphere 5 lab environment, so the above should work.

My personal blog: http://www.shogan.co.uk .::. Twitter: shogan85 .::. if an answer has helped or solved your question, please don't forget to mark as "Answered" or "Helpful"!
0 Kudos
LucD
Leadership
Leadership

Thanks for catching the typo, I corrected the missing 's'.

The [int]::MaxValue is not working because parameter type checking. The cmdlet sees this coming in as a [string], hence the message.

With the parenthesis we force the PS engine to first convert the expression to an actual [int].


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

0 Kudos
Shoganator
Enthusiast
Enthusiast

Ah that makes sense - the same then in mathematics where the parenthesis are handled first in calculations? MaxValue is working nicely now with those parenthesis added - thanks.

I have tested in a vSphere 4.0 environment - Info.DescriptionId has a value of VirtualMachine.relocate in 4.0 (and 4.1 too I assume). In my vSphere 5 lab, it appears to be Info.DescriptionId with a value of "StorageResourceManager.applyRecommendation" for storage vMotions...

My personal blog: http://www.shogan.co.uk .::. Twitter: shogan85 .::. if an answer has helped or solved your question, please don't forget to mark as "Answered" or "Helpful"!
0 Kudos
Shoganator
Enthusiast
Enthusiast

I was curious to know why I was getting different results with storage vMotion using vSphere 5 and using the above examples to fetch svMotion events, so I had a bit of a further play around with this in my lab setup this evening.

The reason why I saw svMotions only when searching for events using this cmdlet:

$events = Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddMonths(-1) | Where {$_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "StorageResourceManager.applyRecommendation"} | Select CreatedTime, UserName, FullFormattedMessage

is because of SDRS and Datastore Clusters it would seem. When you svMotion a VM between Datastore Clusters, the event seems to be come through under Recent Tasks as "Apply Storage DRS recommendations". However, if you svMotion the VM into a normal Datastore, which is not part of a Datastore cluster, then it comes through as "Relocate virtual machine". This is the difference, and hence the reason when I first tried earlier that I saw the event only when searching for "StorageResourceManager.applyRecommendation". (I was testing svMotion by moving a VM between two datastore clusters).

Now, I disassembled a datastore cluster to create a couple of normal datastores and migrated some VMs around these - the events now come up as expected as "Relocate virtual machine".

So what that means is if you are using Datastore Clusters with vSphere 5 only, then you should use the example above in this post. If you are using normal datastores in vSphere 4 or 5, then use Luc's example in his first post. Or, if you want to cover all your bases and are using both normal Datastores and Datastore clusters, then the following should work:

$events = Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddMonths(-1) | Where { $_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.relocate" -or $_.Info.DescriptionId -eq "StorageResourceManager.applyRecommendation"} | Select CreatedTime, UserName, FullFormattedMessage

This will give you a list of both types of storage vMotion that have occurred.

Here is an example in my environment (vSphere 5) where I have svMotioned between DS clusters and normal Datastores:

http://dl.dropbox.com/u/450727/svmotion-differences.jpg

Message was edited by: Shoganator - Syntax highlighting!

My personal blog: http://www.shogan.co.uk .::. Twitter: shogan85 .::. if an answer has helped or solved your question, please don't forget to mark as "Answered" or "Helpful"!
0 Kudos
htwnrver
Enthusiast
Enthusiast

Ah - we are only licensed for Enteprise so havne't got to play with StorageDRS yet.

0 Kudos
Shoganator
Enthusiast
Enthusiast

Aha, that answers which one you need to use then. Anyway, hopefully the above is helpful for others looking at what you were after that have a StorageDRS / Datastore cluster environment! Smiley Happy

My personal blog: http://www.shogan.co.uk .::. Twitter: shogan85 .::. if an answer has helped or solved your question, please don't forget to mark as "Answered" or "Helpful"!
0 Kudos
prabhurj
Contributor
Contributor

This looks good, But if we get the "Target" & "VMname" also that would be great.. can some one help me..

0 Kudos
LucD
Leadership
Leadership

Have a look at my Get the vMotion/svMotion history post.


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

0 Kudos
vmk2014
Expert
Expert

LucD,

I'm trying to fetch the storage vmtion logs to find out what caused the storage exhausted, but it throws an error. Need your help

PS D:\vmk> .\svmlogs.ps1

Get-VIEvent : Cannot bind parameter 'MaxSamples'. Cannot convert value "[int]::MaxValue" to type "System.Int32".

Error: "Input string was not in a correct format."

At D:\vmk\svmlogs.ps1:1 char:35

+ $events = Get-VIEvent -MaxSamples [int]::MaxValue -Start (Get-Date).A ...

+                                   ~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Get-VIEvent], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetEvent

Thanks

vmk

0 Kudos
LucD
Leadership
Leadership

Place brackets around the value

$events = Get-VIEvent -MaxSamples ([int]::MaxValue)


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

0 Kudos
vmk2014
Expert
Expert

LucD,

Can we fetch for last 1 day or 1 week  first ? I want see whether its really able to collect data because 1 months is long data.

Week and day both want to try

thanks

vmk

0 Kudos
LucD
Leadership
Leadership

In my post Get The VMotion/SvMotion History, I created the function Get-MotionHistory.

That function accepts Days, Hours or Minutes as a parameter.
Have a look at the Sample Usage section in that post.


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

0 Kudos
vmk2014
Expert
Expert

Tried, but it runs forever. Can it be faster ?

$events = Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-1) |

where {$_.GetType().Name -eq "TaskEvent" -and $_.Info.DescriptionId -eq "VirtualMachine.relocate"}

0 Kudos
LucD
Leadership
Leadership

You could use my Get-VIEventPlus function, which is included in that post I referenced in my previous reply.
But retrieving events from vCenter is not very fast I'm afraid.


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