Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取使用vSphere API和Java连接到vCenter server的所有esx主机的信息?_Java - Fatal编程技术网

如何获取使用vSphere API和Java连接到vCenter server的所有esx主机的信息?

如何获取使用vSphere API和Java连接到vCenter server的所有esx主机的信息?,java,Java,我正在使用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()); Syst

我正在使用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();}
试试看{
ServiceInstance si=新ServiceInstance(新URL(URL),用户,通过,true);
System.out.println(“主机:“+host+”--“+si.getAboutInfo().getFullName());
System.out.println(“版本为..”+si.getAboutInfo().Version);
System.out.println(“操作系统类型为..”+si.getAboutInfo().osType);
System.out.println(“供应商为..”+si.getAboutInfo().Vendor);
System.out.println(“名称为”+si.getAboutInfo().name);
ManagedEntity[]managedEntities=新的InventoryNavigator(
si.getRootFolder()).SearchManageIdentity(“VirtualMachine”);
ManagedEntity[]主机managedentities=新的InventoryNavigator(
si.getRootFolder()).SearchManageIdentity(“主机系统”);
for(ManagedEntity hostmanagedEntity:hostmanagedEntities){
HostSystem hostsys=(HostSystem)hostmanagedEntity;
字符串ESXhostname=hostsys.getName();
HostListSummary hls=hostsys.getSummary();
hosthardwaresummaryhosthwi=hls.getHardware();
HostListSummaryQuickStats hqs=hls.getQuickStats();
Datastore[]HDS=hostsys.getDatastores();
StringBuilder DS=新的StringBuilder();

对于(inti=0;i来说,您的思路是正确的。您只需要迭代hostsystems对象

下面是一个代码:

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

}
String url=“https://“+host+”/sdk/vimService”;
System.out.println(“主功能中”);
ServiceInstance si=null;
ManagedEntity[]managedEntities=null;
ManagedEntity[]主机managedentities=null;
试一试{
si=新的ServiceInstance(新的URL,用户,通过,真);
hostmanagedEntities=new InventoryNavigator(
si.getRootFolder()).SearchManageIdentity(“主机系统”);
//处理每个主机并调用getVMDetails函数以获取所有主机的详细信息
for(ManagedEntity ManagedEntity:hostmanagedEntities){
HostSystem hostsystemobj=(HostSystem)管理身份;
System.out.println(“主机:”+hostsystemobj.getName()+”);
hostname=hostsystemobj.getName();
字符串ESXhostname=hostsystemobj.getName();
HostHardwareInfo hw=hostsystemobj.getHardware();
字符串ESXhostmodel=hw.getSystemInfo().getModel();
字符串Vendor=hw.getSystemInfo().getVendor();
//打印所有上述变量。
long ESXhostmem=hw.getMemorySize();
短ESXhostcores=hw.getCpuInfo().getnumpucores();
long ESXMHZ=hw.getCpuInfo().getHz();
//为特定主机收集虚拟机数据的调用函数
getVMDetailsInv(si,hostsystemobj.getName());
}
}
捕获(无效属性e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(运行时间故障e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(远程异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
私有void getVMDetailsInv(ServiceInstance si,字符串名称){
主机系统myhost;
System.out.println(“详细信息函数中的主机名为..”+name);
试一试{
myhost=(主机系统)新的InventoryNavigator(si.getRootFolder()).SearchManageIdentity(“主机系统”,名称);
System.out.println(“myhost是..”+myhost);
ManagedEntity[]hostSpecificEntities=新的InventoryNavigator(myhost).searchManagedEntities(“VirtualMachine”);
对于(int i=0;i

这可能会对您有所帮助。

您的思路是正确的。您只需要迭代hostsystems对象

package com.vcenter;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import com.vmware.vim25.VirtualMachineQuickStats;
import com.vmware.vim25.VirtualMachineSummary;
import com.vmware.vim25.mo.ClusterComputeResource;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.HostSystem;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;

public class VCenterUtils {
 private ServiceInstance serviceInstance = null;

 public VCenterUtils(String url, String userName, String password) {
  try {
   serviceInstance = new ServiceInstance(new URL(url), userName, password, true);
  } catch (RemoteException e) {
   e.printStackTrace();
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }
 }

 private ManagedEntity[] getClusters() {
  Folder rootFolder = null;
  ManagedEntity[] entities = null;
  rootFolder = serviceInstance.getRootFolder();
  try {
   entities = new InventoryNavigator(rootFolder).searchManagedEntities("ClusterComputeResource");
  } catch (RemoteException e) {
   e.printStackTrace();
  }
  return entities;
 }

 public List findVirtualMachines() {
  List machineDTOs = new ArrayList();
  ManagedEntity[] entities = getClusters();
  ClusterComputeResource cluster = null;
  if (entities.length > 0) {
   for (ManagedEntity entity : entities) {
    cluster = (ClusterComputeResource) entity;
    HostSystem[] hosts = cluster.getHosts();
    for(HostSystem host : hosts) {
     VirtualMachine[] vms;
     try {
      vms = host.getVms();
      for(VirtualMachine vm : vms) { 
       VirtualMachineDTO machineDTO = new VirtualMachineDTO();
       machineDTO.setName(vm.getName());
       machineDTO.setStatus(vm.getOverallStatus().name());
       machineDTO.setState(vm.getSummary().getRuntime().getPowerState().name());

       VirtualMachineSummary summary = vm.getSummary();
       Double comittedSpace = Double.valueOf(summary.getStorage().getUncommitted())/1024/1024/1024;
       Double uncomittedSpace = Double.valueOf(summary.getStorage().getUncommitted())/1024/1024/1024;
       //calculating Provisioned Space of a virtual machine
       Double provisionedSpace = comittedSpace + uncomittedSpace;
       machineDTO.setProvisionedSpace(provisionedSpace);
       machineDTO.setUsedSpace(comittedSpace);

       VirtualMachineQuickStats virtualMachineQuickStats =summary.getQuickStats();
       machineDTO.setHostCpu(virtualMachineQuickStats.getOverallCpuUsage());
       machineDTO.setHostMemory(virtualMachineQuickStats.getHostMemoryUsage()); 
       machineDTOs.add(machineDTO);
      }
     } catch (RemoteException e) {
      e.printStackTrace();
     }
    }
   }
  }
  return machineDTOs;
 }
}
下面是一个代码:

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

}
String url=“https://“+host+”/sdk/vimService”;
System.out.println(“主功能中”);
ServiceInstance si=null;
ManagedEntity[]managedEntities=null;
ManagedEntity[]主机managedentities=null;
试一试{
si=新的ServiceInstance(新的URL,用户,通过,真);
hostmanagedEntities=new InventoryNavigator(
si.getRootFolder()).SearchManageIdentity(“主机系统”);
//处理每个主机并调用getVMDetails函数以获取所有主机的详细信息
for(ManagedEntity ManagedEntity:hostmanagedEntities){
HostSystem hostsystemobj=(HostSystem)管理身份;
System.out.println(“主机:”+hostsystemobj.getName()+”);
hostname=hostsystemobj.getName();
字符串ESXhostname=hostsystemobj.getName();
HostHardwareInfo hw=hostsystemobj.getHardware();
字符串ESXhostmodel=hw.getSystemInfo().getModel();
字符串Vendor=hw.getSystemInfo().getVendor();
//打印所有上述变量。
long ESXhostmem=hw.getMemorySize();
短ESXhostcores=hw.getCpuInfo().getnumpucores();
long ESXMHZ=hw.getCpuInfo().getHz();
//为特定主机收集虚拟机数据的调用函数
getVMDetailsInv(si,hostsystemobj.getName());
}
}
捕获(无效属性e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(运行时间故障e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(远程异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}抓(马尔福)