How to Get Event Type with vSphere API?

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();
   }
}

This entry was posted in vSphere API and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

8 Comments

  1. Vishal
    Posted September 7, 2010 at 1:48 am | Permalink

    Steve,

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

    Thanks
    Vishal

  2. Vishal
    Posted September 7, 2010 at 2:45 am | Permalink

    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. Posted September 11, 2010 at 12:30 am | Permalink

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

    -Steve

  4. Posted September 11, 2010 at 12:33 am | Permalink

    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
    Posted September 13, 2010 at 12:10 pm | Permalink

    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
    Posted September 14, 2010 at 2:06 pm | Permalink

    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. Posted September 14, 2010 at 7:14 pm | Permalink

    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
    Posted June 15, 2015 at 5:05 am | Permalink

    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.

One Trackback

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  • NEED HELP?


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.