private EventHistoryCollector createEventHistoryCollector(ManagedObject mo, String[] events) throws InvalidState, RuntimeFault, RemoteException { if (eventManager == null) { s_logger.error("Cannot create EventHistoryCollector, eventManager is null"); return null; } EventFilterSpec eventFilterSpec = new EventFilterSpec(); eventFilterSpec.setType(events); // Create an Entity Event Filter Spec to // specify the MoRef of the MO to be get events filtered for EventFilterSpecByEntity entitySpec = new EventFilterSpecByEntity(); entitySpec.setEntity(mo.getMOR()); entitySpec.setRecursion(EventFilterSpecRecursionOption.children); // set the entity spec in the EventFilter eventFilterSpec.setEntity(entitySpec); if (vcenterConnectedTime != null) { EventFilterSpecByTime timeSpec = new EventFilterSpecByTime(); timeSpec.setBeginTime(vcenterConnectedTime); // set the time spec in the EventFilter eventFilterSpec.setTime(timeSpec); } // create the EventHistoryCollector to monitor events for a VM // and get the ManagedObjectReference of the EventHistoryCollector // returned EventHistoryCollector collector = eventManager.createCollectorForEvents(eventFilterSpec); collector.setCollectorPageSize(1000); collectors.add(collector); return collector; }
public static void main(String[] args) throws Exception { if(args.length != 3) { System.out.println("Usage: java QueryEvents " + "<url> <username> <password>"); return; } ServiceInstance si = new ServiceInstance( new URL(args[0]), args[1], args[2], true); // get the latest event and print it out EventManager evtMgr = si.getEventManager(); Event latestEvent = evtMgr.getLatestEvent(); printEvent(latestEvent); // create a filter spec for querying events EventFilterSpec efs = new EventFilterSpec(); // limit to only error and warning efs.setType(new String[] {"VmFailedToPowerOnEvent", "HostConnectionLostEvent"}); // limit to error and warning only efs.setCategory(new String[] {"error", "warning"}); // limit to the children of root folder EventFilterSpecByEntity eFilter = new EventFilterSpecByEntity(); eFilter.setEntity(si.getRootFolder().getMOR()); eFilter.setRecursion( EventFilterSpecRecursionOption.children); // limit to the events happened since a month ago EventFilterSpecByTime tFilter = new EventFilterSpecByTime(); Calendar startTime = si.currentTime(); startTime.roll(Calendar.MONTH, false); tFilter.setBeginTime(startTime); efs.setTime(tFilter); // limit to the user of "administrator" EventFilterSpecByUsername uFilter = new EventFilterSpecByUsername(); uFilter.setSystemUser(false); uFilter.setUserList(new String[] {"administrator"}); Event[] events = evtMgr.queryEvents(efs); // print each of the events for(int i=0; events!=null && i<events.length; i++) { System.out.println("\nEvent #" + i); printEvent(events[i]); } si.getServerConnection().logout(); }