Home > vSphere API > How to Get Event Type with vSphere API?

How to Get Event Type with vSphere API?

August 19th, 2010 Leave a comment Go to comments

There is a recent question asking how to get the type of event from vSphere API in my previous blog. On one hand, you can clearly see the types of events on a vSphere Client, for instance “info”, “warning”, “error”, and “user.” On the other hand, you cannot find any information about the type from a given event itself using vSphere API.

Strictly speaking an event just indicates something has happened. That is it. You can categorize it differently depending on your goal. The Event type itself in vSphere API models an event as what it is, not about how you look at it. This is a right design philosophy, but turns out to be a little tricky for you to figure out the type of an event.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

How does vSphere Client do the trick?

The trick is with the EventManager, which has a property called description typed as EventDescription[]. The EventDescription data object includes category sub-property which holds information about the possible categories/types of all events, namely “info”, “warning”, “error”, and “user.” This is exactly you find from vSphere Client.

Another sub-property of EventDescription is called eventInfo typed as EventDescriptionEventDetail[]. Each EventDescriptionEventDetail object has a key, a category, and several format strings. So the mapping is here! The following is a screen shot showing the “HostAddFailedEvent” as “error.” The reason I used Eclipse is because MOB does not show eventInfo correctly. You can optionally use the VI Java Browser.

The solution is now clear. All you need to do is to retrieve the eventInfo sub-property and establish the mapping locally for lookup. In Java, you can load them into a HashMap object for fast look up.

Note that the mapping is just what VMware looks at these events. You can change the mapping for your applications when appropriate. That is why I said current design follows right philosopy.

One important performance tip is that you should NOT retrieve it twice in a session because eventInfo doesn’t change with a particular server. I wouldn’t recommend you to persist it locally either because it’s that expensive to get it.

Following is the Java code if you want to inspect the eventInfo property.

import java.net.URL;

import com.vmware.vim25.EventDescription;
import com.vmware.vim25.TaskInfoState;
import com.vmware.vim25.VirtualDevice;
import com.vmware.vim25.VirtualDisk;
import com.vmware.vim25.mo.EventManager;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.Task;
import com.vmware.vim25.mo.VirtualMachine;

/**
* @author Steve Jin
*/

public class FindEventType
{
public static void main(String[] args) throws Exception
{
ServiceInstance si = new ServiceInstance(new URL("<a href="https://server/sdk">https://server/sdk</a>"), "root", "password", true);
EventManager em = si.getEventManager();

EventDescription ed = em.getDescription();
// set a break point here to inspect the ed variable for more info

si.getServerConnection().logout();
   }
}

Categories: vSphere API Tags: , ,
  1. Vishal
    September 7th, 2010 at 01:48 | #1

    Steve,

    Why is there no DatacenterRemovedEvent? what do i get when datacenter is removed?

    Thanks
    Vishal

  2. Vishal
    September 7th, 2010 at 02:45 | #2

    Steve,

    If i capture an event lets say DatacenterCreated event, How do i know its severity in VIClient. I understand that EventDescription has the events defined, but i an trying to look at the event get the severity code and look up the EventDescriptionEventDetail array. event after reading this blog that was not clear.

  3. September 11th, 2010 at 00:30 | #3

    Good question. I will write a blog on this soon. Stay tuned.

    -Steve

  4. September 11th, 2010 at 00:33 | #4

    You can look up the EventDescriptionDetail array by matching the key with the event type. Once you find one item in the array, you can get its category: error, warning, info, and user.

    -Steve

  5. Vishal
    September 13th, 2010 at 12:10 | #5

    Here is the code that works, Not sure if there is better way of doing it though, This seems inefficient and prone for errors.

    Sample code—

    private void initialize() {
    _eventManager = si.getEventManager();
    _rootFolder = si.getRootFolder();
    eventDesc = _eventManager.getDescription();
    eventInfo = eventDesc.getEventInfo();
    }

    private void processEvent(Event anEvent) {
    System.out.println(“\n———-” + “\n Severity: “+ getSeverity(anEvent.getClass().getName())+” Event ID: ” + anEvent.getKey() + ” Event: ”
    + anEvent.getClass().getName() + ” FullFormattedMessage: ” + anEvent.getFullFormattedMessage()
    + “\n———-\n”);

    }
    private String getSeverity(String eventKey) {
    // strip com.vmware.vim25. from eventKey
    int beginIndex = 17; // skip 17 chars
    String eventName = eventKey.substring(beginIndex);
    for (EventDescriptionEventDetail info: eventInfo) {
    if (info.key.equals(eventName))
    return info.category;
    }
    return “Unknown”;
    }

    Output

    ——–

    Event Details follows:

    ———-
    Severity: info Event ID: 942261 Event: com.vmware.vim25.DatacenterRenamedEvent FullFormattedMessage: Renamed datacenter from XXX-Datacenter2 to XXX-Datacenter3
    ———-

  6. Vishal
    September 14th, 2010 at 14:06 | #6

    Steve,

    Thanks for the blog, Any comment on my implementation, is there any way we can optimize it, it seems too round about way of getting the severity. The requirement i have from customers is to see the same severity for the events.

  7. September 14th, 2010 at 19:14 | #7

    Hi Vishal,

    As you pointed out, your implementation can be optimized. You want to load the event info array into a HashMap. It would speed up look up dramatically.

    Steve

  8. Sharninder
    June 15th, 2015 at 05:05 | #8

    Hi Steve,

    Why isn’t there any event for moving a host into a cluster. I’m talking specifically about Moving the host by dragging and dropping it using the vsphere client. When I to this, the only event I seem to get is a TaskEvent, which is not useful to find out the source and target of the event for example. When I add a host to a cluster, I get hostAddedEvent which has much more info.

  1. September 14th, 2010 at 00:10 | #1