如何使用java和vsphere查找esx主机的序列号?

时间:2014-04-04 06:05:34

标签: java vsphere

我想获得esx主机的序列号。我正在使用java和vSphere API。 我能够连接到esx服务器(vcenter)并从中获取一些信息。

以下是连接和获取信息的代码。

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主机的序列号?

1 个答案:

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

希望这会有所帮助..