I tried the following code. I am able to see the event in vSphere but not able to capture with code.
I am able to see the descriptions related to event on this particular vCenter. To my un
derstanding this vCenter should capture those events.
When i checked the ouput at the sametime.
I don't have any event related to DVPorts.
Please suggest what might have caused this not to capture and how to go about this.
Any configuration at the vCenter causing this because i am able capture for other vCenter.
![]()
![]()
package vmware2020;
import java.io.File;
import java.io.FileOutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.ConnectException;
import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import com.vmware.vim25.Event;
import com.vmware.vim25.EventFilterSpec;
import com.vmware.vim25.EventFilterSpecByEntity;
import com.vmware.vim25.EventFilterSpecByTime;
import com.vmware.vim25.EventFilterSpecRecursionOption;
import com.vmware.vim25.InvalidLogin;
import com.vmware.vim25.mo.EventManager;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.StoragePod;
//java -cp cim-2.0.0.jar:dom4j-2.1.1.jar:jaxb-api-2.3.1.jar:vijava-55b20130927_c04.jar:vmware2020.jar vmware2020.VmwareEvents <IPADDRESS> <USERNAME> <PASSWORD> '443' 'true' -23 HOUR true
public class VmwareEvents {
ServiceInstance serviceInstance;
VmwareServiceApiServiceImpl vmwareServiceApiServiceImpl;
HashMap<String, StoragePod> dsMap = new HashMap<>();
HashMap<String, String> dsSpodMap = new HashMap<>();
public static void main(String[] args) throws RemoteException, Exception {
VmwareEvents auth = new VmwareEvents();
String ipAddress = args[0];
String userName = args[1];
String password = args[2];
int port = Integer.parseInt(args[3]);
String isSecure = args[4];
boolean secure = (isSecure.equalsIgnoreCase("true")) ? true : false;
int option = Integer.parseInt(args[5]);
String calendarTag = args[6];
String endTimeFlag = args[7];
auth.go(getServiceInstance(ipAddress, userName, password, port, secure), option, calendarTag, endTimeFlag);
}
private void go(ServiceInstance serviceInstance, int option, String calendarTag, String endTimeFlag) {
File file = null;
try {
this.serviceInstance = serviceInstance;
String fileName = "vcenter_events_";
StringBuffer bf = new StringBuffer();
EventManager evtManager = this.serviceInstance.getEventManager();
Event[] events = null;
if (evtManager != null) {
EventFilterSpec fSpec = new EventFilterSpec();
// fSpec.setCategory(null);
// Need to filter with Time
EventFilterSpecByTime fTime = new EventFilterSpecByTime();
// begin time = lastpooled time from if nul api get time
Calendar time = this.serviceInstance.getServerClock();
System.out.println("TIME: "+time);
Calendar beginTime = (Calendar) this.serviceInstance.currentTime();
// System.out.println("BEGIN TIME: "+beginTime);
Calendar endTime = (Calendar) this.serviceInstance.currentTime();
// System.out.println("END TIME: "+endTime);
// java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,
// lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+00:00",offset=0,
// dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,
// ERA=1,YEAR=2020,MONTH=7,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=1,DAY_OF_WEEK=5,
// DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=7,MINUTE=41,SECOND=36,MILLISECOND=916,
// ZONE_OFFSET=0,DST_OFFSET=0]
// Date date = time.getTime();
//
// time.getTimeZone();
// time.getFirstDayOfWeek();
// time.getMinimalDaysInFirstWeek();
//
//
// int ERA = time.ERA;
// int YEAR = time.YEAR;
// int MONTH = time.MONTH;
//
// int WEEK_OF_YEAR = time.WEEK_OF_YEAR;
// int WEEK_OF_MONTH = time.WEEK_OF_MONTH;
//
// int DAY_OF_MONTH = time.DAY_OF_MONTH;
// int DAY_OF_YEAR = time.DAY_OF_YEAR;
// int DAY_OF_WEEK = time.DAY_OF_WEEK;
// int DAY_OF_WEEK_IN_MONTH = time.DAY_OF_WEEK_IN_MONTH;
//
// int AM_PM = time.AM_PM;
// int HOUR = time.HOUR;
// int HOUR_OF_DAY = time.HOUR_OF_DAY;
// int MINUTE = time.MINUTE;
// int SECOND = time.MILLISECOND;
// int MILLISECOND = time.MILLISECOND;
// int ZONE_OFFSET = time.ZONE_OFFSET;
// int DST_OFFSET = time.DST_OFFSET;
System.out.println("OPTION: [" + option + "]");
if (calendarTag.equalsIgnoreCase("YEAR")) {
beginTime.add(Calendar.YEAR, option);
} else if (calendarTag.equalsIgnoreCase("MONTH")) {
beginTime.add(Calendar.MONTH, option);
} else if (calendarTag.equalsIgnoreCase("HOUR")) {
beginTime.add(Calendar.HOUR, option);
} else if (calendarTag.equalsIgnoreCase("DAY_OF_WEEK_IN_MONTH")) {
beginTime.add(Calendar.DAY_OF_WEEK_IN_MONTH, option);
} else if (calendarTag.equalsIgnoreCase("DAY_OF_MONTH")) {
beginTime.add(Calendar.DAY_OF_MONTH, option);
} else if (calendarTag.equalsIgnoreCase("FRIDAY")) {
beginTime.add(Calendar.FRIDAY, option);
}
System.out.println("Begin Time " + beginTime.getTime());
System.out.println("End Time " + endTime.getTime());
String bt = beginTime.getTime().toString().replaceAll(" ", "_");
String et = endTime.getTime().toString().replaceAll(" ", "_");
bt = bt.replaceAll(":", "_");
et = et.replaceAll(":", "_");
file = new File(fileName+bt+"_"+et+"_END_TIME_IS_"+endTimeFlag+".txt");
fTime.setBeginTime(beginTime);
if (endTimeFlag.equalsIgnoreCase("true")) {
fTime.setEndTime(endTime);
}
fSpec.setTime(fTime);
// Need to filter with Entity
EventFilterSpecByEntity entityFilter = new EventFilterSpecByEntity();
entityFilter.setEntity(this.serviceInstance.getRootFolder().getMOR());
entityFilter.setRecursion(EventFilterSpecRecursionOption.all);
fSpec.setEntity(entityFilter);
// Updating with latest time stamp every time
// VMwareMonitorCacheProvider.addEventLastPolledTimeMap(device.getUniqueId(),
// entityUuid, api.getCurrentServerTime());
bf.append(
"Name FullFormatName EventTypeId \n");
events = evtManager.queryEvents(fSpec);
System.out.println(evtManager.getLatestEvent().fullFormattedMessage);
if (events != null) {
System.out.println(events.length);
for (Event event : events) {
if (event instanceof com.vmware.vim25.AlarmStatusChangedEvent) {
com.vmware.vim25.AlarmStatusChangedEvent e = (com.vmware.vim25.AlarmStatusChangedEvent) event;
bf.append("\t" + e.getClass().getSimpleName() + " " + "\t" + e.getFullFormattedMessage()
+ "\n");
} else if (event instanceof com.vmware.vim25.EventEx) {
com.vmware.vim25.EventEx e = (com.vmware.vim25.EventEx) event;
bf.append("\t" + e.getClass().getSimpleName() + " " + "\t" + e.getFullFormattedMessage()
+ "\t" + e.getEventTypeId() + "\n");
} else if (event instanceof com.vmware.vim25.ExtendedEvent) {
com.vmware.vim25.ExtendedEvent e = (com.vmware.vim25.ExtendedEvent) event;
bf.append("\t" + e.getClass().getSimpleName() + " " + "\t" + e.getFullFormattedMessage()
+ "\t" + e.getEventTypeId() + "\n");
} else {
bf.append("\t" + event.getClass().getSimpleName() + " " + "\t"
+ event.getFullFormattedMessage() + "\n");
}
}
}
}
byte[] bytes = bf.toString().getBytes();
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(bytes);
fos.flush();
}
if (this.serviceInstance != null) {
this.serviceInstance.getServerConnection().logout();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static ServiceInstance getServiceInstance(String ipAddress, String userName, String password, int port,
boolean secure) {
String apiEndPoint = null;
URL url = null;
// Create a new ServiceInstance for this API call
ServiceInstance serviceInstance = null;
try {
apiEndPoint = secure ? "https://" + ipAddress + ":" + port + "/sdk"
: "http://" + ipAddress + ":" + port + "/sdk";
try {
url = new URL(apiEndPoint);
} catch (Exception e) {
System.out.println(apiEndPoint);
System.out.println(url);
e.printStackTrace();
}
serviceInstance = new ServiceInstance(url, userName, password, true);
} catch (InvalidLogin ile) {
System.out.println("InvalidLogin");
ile.printStackTrace();
} catch (RemoteException re) {
if (re instanceof InvalidLogin) {
System.out.println("InvalidLogin2");
}
if (re instanceof ConnectException) {
System.out.println("ConnectException");
}
re.printStackTrace();
} catch (MalformedURLException me) {
System.out.println("MalformedURLException");
me.printStackTrace();
} catch (Exception e) {
System.out.println("Exception");
e.printStackTrace();
}
return serviceInstance;
}
public void logout() throws Exception {
if (serviceInstance != null) {
try {
serviceInstance.getServerConnection().logout();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}