gpt4 book ai didi

java - 如何使用 vSphere API 和 Java 获取所有连接到 vCenter 服务器的 esx 主机的信息?

转载 作者:行者123 更新时间:2023-12-01 23:05:38 31 4
gpt4 key购买 nike

我正在使用 vSphere API 收集 esx 系统的信息。通过使用这个API,我可以通过提供IP来收集esx主机的信息。

我正在使用 java 和 vsphere API 以及 java.这是代码:

try {
ServiceInstance si = new ServiceInstance(new URL(url), user, pass,true);
System.out.println("host :"+host+"---"+si.getAboutInfo().getFullName());

System.out.println(" Version is .. " +si.getAboutInfo().version);
System.out.println(" os type is .. " +si.getAboutInfo().osType);
System.out.println("Vendor is .. " + si.getAboutInfo().vendor);
System.out.println("name is" + si.getAboutInfo().name);

ManagedEntity[] managedEntities = new InventoryNavigator(
si.getRootFolder()).searchManagedEntities("VirtualMachine");
ManagedEntity[] hostmanagedEntities = new InventoryNavigator(
si.getRootFolder()).searchManagedEntities("HostSystem");

for (ManagedEntity hostmanagedEntity : hostmanagedEntities) {
HostSystem hostsys = (HostSystem) hostmanagedEntity;

String ESXhostname = hostsys.getName();

HostListSummary hls = hostsys.getSummary();
HostHardwareSummary hosthwi = hls.getHardware();
HostListSummaryQuickStats hqs = hls.getQuickStats();
Datastore[] HDS = hostsys.getDatastores();
StringBuilder DS = new StringBuilder();
for (int i=0;i <HDS.length;i++){
DatastoreSummary dsm =HDS[i].getSummary();

DS.append(dsm.name+":"+dsm.capacity+":"+dsm.freeSpace+"-");
}

int MEM=hqs.overallMemoryUsage;
int UPT=hqs.getUptime();
Integer CPU=hqs.getOverallCpuUsage();

String esxkey = "ESXRealInfo";
String esxvalue = "ESXhostname-" + ESXhostname
+ ";CPU Usage-" + CPU + ";MEM Usage-"
+ MEM + ";UPTIME-" + UPT+"; Datastores -"+DS;
}


}
catch (Exception e){ e.printStackTrace();}

通过使用此代码并操作管理实体对象,我可以提取每个主机和在每个主机上工作的虚拟机的信息。

现在我不想从不同的主机收集信息,而是想从vcenter服务器收集信息。 vCenter 服务器还包含有关主机及其虚拟机的所有详细信息。

我想通过使用 vCenter 服务器而不是访问每个主机来获取此信息。是否可以得到这些信息?怎么办?

最佳答案

你走在正确的道路上。您只需要迭代主机系统对象。

这是一个代码:

String url = "https://" + host + "/sdk/vimService";
System.out.println("In main function");
ServiceInstance si = null;
ManagedEntity[] managedEntities = null;
ManagedEntity[] hostmanagedEntities = null;
try {
si = new ServiceInstance(new URL(url), user, pass,true);

hostmanagedEntities = new InventoryNavigator(
si.getRootFolder()).searchManagedEntities("HostSystem");

//process each host and call getVMDetails function to get details of all host
for (ManagedEntity managedEntity : hostmanagedEntities) {
HostSystem hostsystemobj = (HostSystem) managedEntity;
System.out.println("Host: '" + hostsystemobj.getName() + "'");
hostname = hostsystemobj.getName();
String ESXhostname = hostsystemobj.getName();
HostHardwareInfo hw = hostsystemobj.getHardware();
String ESXhostmodel = hw.getSystemInfo().getModel();
String Vendor = hw.getSystemInfo().getVendor();
//print all above variables.
long ESXhostmem = hw.getMemorySize();

short ESXhostcores = hw.getCpuInfo().getNumCpuCores();
long ESXMHZ = hw.getCpuInfo().getHz();

//call function that collects vms data for specific host
getVMDetailsInv(si,hostsystemobj.getName());

}
}
catch (InvalidProperty e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RuntimeFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

private void getVMDetailsInv(ServiceInstance si,String name) {

HostSystem myhost;
System.out.println("host name from details function is .. " + name);
try {
myhost = (HostSystem) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("HostSystem",name);
System.out.println("myhost is .. " + myhost);
ManagedEntity[] hostSpecificEntities = new InventoryNavigator(myhost).searchManagedEntities("VirtualMachine");

for (int i = 0; i < hostSpecificEntities.length; i++) {
VirtualMachine vm = (VirtualMachine) hostSpecificEntities[i];
String macAddress="";
for(VirtualDevice vd:vm.getConfig().getHardware().getDevice()){
try {
VirtualEthernetCard vEth=(VirtualEthernetCard)vd;
macAddress=vEth.macAddress;
}
catch(Exception e){}
}
System.out.println("Name : "+vm.getName());
String vmVersion = vm.getConfig().version;
System.out.println("vm wayer version is ..from inventory.. "+ vm.getConfig().version);
System.out.println("geust os uuid "+vm.getSummary().getConfig().uuid);
System.out.println("geust mac Address of guest "+macAddress);
System.out.println("geust id is "+vm.getSummary().getGuest().guestId);
System.out.println("host id is "+vm.getSummary().getGuest().getIpAddress());
}
} catch (RuntimeFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

这可能对你有帮助..

关于java - 如何使用 vSphere API 和 Java 获取所有连接到 vCenter 服务器的 esx 主机的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22808749/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com