2014-04-04 64 views
0

我想獲得esx主機的序列號。我正在使用java和vSphere API。 我能夠連接到esx服務器(vcenter)並從中獲取一些信息。如何使用java和vsphere查找esx主機的序列號?

這裏是連接和獲取信息的代碼。

String host = "192.168.xx.xx" 
String url = "https://" + host + "/sdk/vimService"; 
    // List host systems 
try { 
ServiceInstance si = new ServiceInstance(new URL(url), user, pass, 
     true); 
ManagedEntity[] managedEntities = new InventoryNavigator(
     si.getRootFolder()).searchManagedEntities("VirtualMachine"); 
ManagedEntity[] hostmanagedEntities = new InventoryNavigator(
     si.getRootFolder()).searchManagedEntities("HostSystem"); 
System.out.println("123 ... "+ si.getAboutInfo().getVersion()); 

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

String ESXhostname = hostsys.getName(); 
//System.out.println("host ip address is "+hostsys.getConfig().); 
HostHardwareInfo hw = hostsys.getHardware(); 
String ESXhostmodel = hw.getSystemInfo().getModel(); 
String Vendor = hw.getSystemInfo().getVendor(); 

long ESXhostmem = hw.getMemorySize(); 
short ESXhostcores = hw.getCpuInfo().getNumCpuCores(); 
long ESXMHZ = hw.getCpuInfo().getHz(); 
HostCpuPackage[] cpuPkg = hw.getCpuPkg(); 
String esxkey = "ESXInfo"; 
String esxvalue = "ESXhostname-" + ESXhostname 
     + ";ESXhostmodel-" + ESXhostmodel 
     + ";ESXhostmem-"+ ESXhostmem 
     + ";ESXhostcores-" + ESXhostcores 
     + ";ESXHz -"+ESXMHZ 
     + ";Vendor-" + Vendor 
     + ";OSType-" + si.getAboutInfo().getOsType() 
     + ";FullName-" + si.getAboutInfo().getFullName() 
     + ";OSVersion-" + si.getAboutInfo().getVersion() 
     ; 

System.out.println("Value are "+ esxvalue); 
} 

for (int i = 0; i < managedEntities.length; i++) { 
VirtualMachine vm = (VirtualMachine) managedEntities[i]; 
String macAddress=""; 
for(VirtualDevice vd:vm.getConfig().getHardware().getDevice()){ 
try { 
VirtualEthernetCard vEth=(VirtualEthernetCard)vd; 
macAddress=vEth.macAddress; 
System.out.println("Macaddress of guest is "+vEth.macAddress); 
} 
catch(Exception e){} 
} 
String vmName = 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()); 
String uuid=vm.getSummary().getConfig().uuid; 
VirtualMachinePowerState deviceState = vm.getSummary().runtime.powerState; 
    System.out.println("Powerstate of device is ......."+deviceState); 
String vmIP=vm.getSummary().getGuest().getIpAddress(); 
VirtualMachineConfigInfo config = vm.getConfig(); 
VirtualHardware hw = config.getHardware(); 
int vmCPU = hw.getNumCPU(); 
int vmMem = hw.getMemoryMB(); 
System.out.println(vmName + vmIP + vmCPU + vmMem); 
String vmkey = "vm" + i; 
String vmvalues = "Name-" + vmName + ";IP-" + vmIP + ";vmCPU-" 
    + vmCPU + ";vmMem-" + vmMem+";uuid-" + uuid + ";host-"+host+";macAddress-"+macAddress 
    +";deviceState-" + deviceState + ";vmVersion-"+vmVersion; 
} 
si.getServerConnection().logout(); 
} 
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(); 
} 

如何獲得esx主機的序列號?

+0

https://開頭communities.vmware.com/thread/232203?start=15&tstart=0可能會幫助 –

回答

1

您需要添加以下行來獲得序列號 -

String serialNumber = hostsys.getSummary().getHardware().otherIdentifyingInfo[2].identifierValue; 
System.out.println("srno.. " + hostsys.getSummary().getHardware().otherIdentifyingInfo[2].identifierValue); 

所以你的代碼會像:

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

String ESXhostname = hostsys.getName(); 
//System.out.println("host ip address is "+hostsys.getConfig().); 
HostHardwareInfo hw = hostsys.getHardware(); 
String ESXhostmodel = hw.getSystemInfo().getModel(); 
String Vendor = hw.getSystemInfo().getVendor(); 

long ESXhostmem = hw.getMemorySize(); 
short ESXhostcores = hw.getCpuInfo().getNumCpuCores(); 
long ESXMHZ = hw.getCpuInfo().getHz(); 
HostCpuPackage[] cpuPkg = hw.getCpuPkg(); 
String serialNumber = hostsys.getSummary().getHardware().otherIdentifyingInfo[2].identifierValue; 
String esxkey = "ESXInfo"; 
String esxvalue = "ESXhostname-" + ESXhostname 
    + ";ESXhostmodel-" + ESXhostmodel 
    + ";ESXhostmem-"+ ESXhostmem 
    + ";ESXhostcores-" + ESXhostcores 
    + ";ESXHz -"+ESXMHZ 
    + ";Vendor-" + Vendor 
    + ";OSType-" + si.getAboutInfo().getOsType() 
    + ";FullName-" + si.getAboutInfo().getFullName() 
    + ";OSVersion-" + si.getAboutInfo().getVersion() 
    +";serialNumber" + serialNumber 
    ; 

System.out.println("Value are "+ esxvalue); 
} 

希望這將是有益的..

+0

爲什麼從數組中獲取第二個值? 是否有任何值的描述? – Vishwas

+0

它給了我想要的輸出,但想要更多的描述.. 謝謝...... – Vishwas

相關問題