I want to get serial number of esx host. I am using java and vSphere API. I am able to connect to esx server (vcenter) and fetch some information from it.

Here is a code to connect and fetch information.

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

How do I get serial number of esx host?

有帮助吗?

解决方案

You need to add following line to get serial number-

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

So your code will be like:

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

Hope this will be helpfull..

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top