in Python

[Script] View Virtual Machine Events

ViewVmEvents is a python script that can be used to view a specific event related to a VMware virtual machine. The events type can be one of the following: Reboot, Shutdown or Standby.

I was experimenting with the Pyvmomi SDK for the VMware vSphere API, on how I can quickly view a virtual machine event without going through the manual process of connecting to the vSphere Web Client =>  Login => Search …

How it Work ?

The managed object that is used to collect the events is the EventManager with the QueryEvents method, but before you can do this, you should build an Event Filter Specification using EventFilterSpec() method and then configure it’s properties.

The following code snippet is responsible for doing this:

 eMgrRef = content.eventManager
filter = vim.event.EventFilterSpec.ByEntity(entity=v, recursion="self")                
filter_spec = vim.event.EventFilterSpec()
filter_spec.eventTypeId = str(eventType[args.event])
filter_spec.entity = filter                
event_res = eMgrRef.QueryEvents(filter_spec)

The script can run on Windows or Linux and it take the following arguments:

  vCenter         vCenter Server FQDN or IP
  UserName        Username to connect to the vCenter
  Password        Password to connect to the vCenter
  VirtualMachine  Virtual machine name (Case-sensitive)
  EventType       Event type: Reboot, Shutdown, Standby

If you specify the correct informations, the script will then search for the virtual machine and print any event that you specify.
Here is a simple output from running the script on my home lab:

viewmevents command line

viewvmevent result

You can get the script from my Github repo.

Share your thoughts