Events – Part 8 – vMotion history

Another idea triggered by a post in the PowerCLI Community. Lars wanted to know where his VMs had been running in the past.

Since vSphere doesn’t maintain any historical data with the guests themselves, we have to fall back on the Tasks and Events to create such a report. The basic algorithm to query the tasks, and their related events, is already published in Events – Part 3 : Auditing VM device changes. But to get a historical record of the servers where your guests have been hosted requires a bit more logic in the script.

The script

There are 2 distinct parts in the script.

  1. Get all the vMotion tasks. A vMotion can be user– or DRS-initiated. These produce different tasks.
  2. Compile the final report by only looking at the selected guests

Annotations

Line 1-2: Specify how far back you want to report on through the $start variable

Line 5: The vMotion tasks can be triggered by a user or by DRS.

Line 10-13: We collect all the guests on which we want to report. This can be changed at will, just make sure the hash table $vmHash is populated.

Line 15-66: This is the same logic as was used in Events – Part 3 : Auditing VM device changes

Line 69-70: We group the discovered vMotions per guest and then we sort the groups descending on the Count property. This is required because we will later use the Export-Csv cmdlet and if the array holds rows of varying length, the longest row should be first.

Line 76-80: For the vMotioned guest we first make an entry with the original location at time $start.

Line 82-86: For each vMotion we add the same properties Time, Host, DRS and User, but each time with an incremented suffix number.

Line 89: A guest that was vMotioned is removed from hash table to avoid duplicate entries later one.

Line 93-100: The guest that were not vMotioned during the reporting interval are added to the report.

Sample output

The script produces a CSV file that contains quite a bit of information.

A guest that was not vMotioned during the reporting interval will only have a Time1 and Host1 column.

Guests that were vMotioned will have additional Timen and Hostn columns. Notice the DRSn column which indicates if a vMotion was triggered by DRS or by a user.  The Usern column will display the username if it was a user-invoked vMotion.

16 Comments

    Dmitriy

    Hello!

    I have a problem with outdata in csv:
    The column HOST does not contain information about esxi hosts (Empty cells) in csv file. Other columns are contained.

      Mohamed Elsaid

      I have the same problem,, could you resolve it?

    Mel

    Thanks a lot for this script, it saved me a ton of time and it works great.

    I do throw some errors that “Hosts” is deprecated and we should use “VMHosts” instead, but the script worked perfectly anyhow it just threw the warning.

    Thanks again for the excellent script, I’ll be keeping this one on standby as my VCenter event log buffer runs out pretty fast in our environment.

      LucD

      Thanks.
      You can get rid of those warnings by running

      Set-PowerCLIConfiguration -DisplayDeprecationWarnings:$false

        Ritam9

        Hi Luc ,
        Thanks for the script first. I need a small help. This script is working for me . However if i want to collect it for a particular VM , say “Test_VM”, how to fit that in the script. I have tried couple of things in the below line which are there in line 10-13. Could you give me an example. I am novice in powercli, could not make it running.

        Below is the script part where I have tried to change but did not work —

        [Line 10-13: We collect all the guests on which we want to report. This can be changed at will, just make sure the hash table $vmHash is populated.]

        # Get the guest for which we want the report
        $vmHash = @{}
        Get-Datacenter -Name “MyDC” | Get-VM | %{
        $vmHash[$_.Name] = $_.Host
        }

          LucD

          You were nearly there.

          The 1st change is lines 9-13. Change them to

          $vmName = 'MyVM'
          # Get the guest for which we want the report
          $vmHash = @{}
          Get-Datacenter -Name "MyDC" | Get-VM -Name $vmName| %{
          $vmHash[$_.Name] = $_.Host
          }

          The 2nd part is lines 41-52. Those need to be replaced by

          "VmBeingHotMigratedEvent" {
          if($task.EntityName -eq $vmName){
          $migrations += New-Object PSObject -Property @{
          VMname = $task.EntityName
          Source = $event.Host.Name
          Destination = $event.DestHost.Name
          Start = $task.StartTime
          Finish = $task.CompleteTime
          Result = $task.State
          User = $task.Reason.UserName
          DRS = &{if($task.DescriptionId -like "Drm.*"){$true}else{$false}}
          }
          }
          }

          I hope that works for you.

    Dave

    I’m new to powershell so bare with me but how do I get it to produce a csv file. I have executed the script and it completes successfully but doesn’t produce a file. Do I have to add something additional in?

      LucD

      Hi Dave,
      If you ran the script literaly like it in the post, you should find a CSV file C:\vMotion-history.csv

    Paul

    Great script, my thanks. I note though that the vMotion times differ from the VI Client times and seem to be 11 hours behind the C# client’s log. Any reason for this?

      admin

      Hi Paul,
      Thanks.
      The times in the events are always in UTC, while in the VI CLient they are in your local timezone.
      Could it be that your timezone is currently 11 hours behind UTC ?

    Wu-Tang Dan

    This post is awesome! I was able to show some of my application owners that their servers can be vMotioned for a future project by showing them how often they have moved in the past.

    Thanks again.

    Muhammad

    Thx for this posting.

    I wanted to ask if there is a built in report in web vCenter to get a report of bumber of vMotions in a DRS cluster or is custom powershell script the only way?

    Thank you,
    Muhammad

    kze

    Hi LucD,

    Will it show vmotion history when the host is currently down? We are trying to determine where the vm gone after a host failure.

      LucD

      @kze, yes it will.
      The events are gathered from the vCenter database. As long as the vCenter is there you should be able to get the events.

    Gary Stephens

    Great post — but I have a question. Could there be a bug in the script? I ran it as-is and got data for every VM. I changed the block on line 11-13 to this:

    Get-Datacenter -Name “my.favorite.dc.name” | Get-VM | %{
    where{$vmHash.ContainsKey($_.Name)
    $vmHash[$_.Name] = $_.Host
    }
    }

    This resulted in filtering the report to VMs that I initalized into @vmHash. I’m new to Powershell and VMWare, so I thought I’d check.

    thanks again for the blog,

    Gary

    Kayser Soze

    There is a DRS history tab on the VI Client – is it possible to create a script to export that info?

    thanks.

Leave a Reply

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

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.