用Java来获取VMware ESX Server的信息可以通过一个第三方jar包进行方便的操作:
Vijava – Vmware infrastructure(vSphere) java API
关于ESX Server的介绍,可以参照我的另一篇博客:http://josh-persistence.iteye.com/admin/blogs/1887722
下面看看一些相关介绍,然后再看对应的程序:
1. VCenter/ServiceInstance Strucutre
1). ServiceInstance -- Root of the inventory; created by vSphere. (VCenter)
2). Datacenter -- A container that represents a virtual data center. It contains hosts, network entities, virtual machines and virtual applications, and datastores.
3). ComputeResource -- A compute resource (either a cluster or a stand-alone host) (Cluster)
4). HostSystem -- A single host (ESX Server or VMware Server).
5). AboutInfo -- This data object type describes system information including the name, type, version, and build number. (HostSystem/ESX Server/VMware Sever’s AboutInfo)
<!--[if !supportLists]-->1. 2. Below are EsxServer/HostSystem properties is returned from Vijava.
3. 进一步了解ESXServer的Strutcture
EsxServer contains several vm(Virtual Machine of EsxServer), EsxServer and vm’ relationships set on Network(network of EsxServer), Network contains Virtual NIC, Virtual Switch. Virtual Switch is responsible for assigning Physical NIC to VM, just like the VM has its own NIC(Virtual NIC). There have to consider VM and NIC’s relationship, because the NIC may be assigned to one of the VM unexpectedly which means the VM can be related to different Virtual NIC.
从上面的架构图可看出,如果我们需要获取VCenter,ESXServer等的信息,就包括获取Disk/Volumn/Storage, 虚拟交换机,内存,虚拟网卡,物理网卡,内存等信息,最主要的是由于物理网卡通过虚拟交换机分配给每个虚拟主机作为虚拟网卡,这种关系是随机的,或者说有一定的分配规则的,我们在每次获取VCenter的信息时,都需要重新的去获取物理网卡的分配。
下面就是一个实例,该实例展现了怎样从VMware上通过vSphere提供的API(vijava.jar)来获取VCener/ServerInstance下的所有信息,包括上面2. Below are EsxServer/HostSystem properties is returned from Vijava.中列的所有信息。更多的API,可以参照:http://pubs.vmware.com/vsphere-50/index.jsp
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.ebay.odb.sync.ESXServerSync;
import com.vmware.vim25.AboutInfo;
import com.vmware.vim25.ClusterComputeResourceSummary;
import com.vmware.vim25.ClusterConfigInfo;
import com.vmware.vim25.DatastoreInfo;
import com.vmware.vim25.HostHardwareInfo;
import com.vmware.vim25.HostHostBusAdapter;
import com.vmware.vim25.HostInternetScsiHba;
import com.vmware.vim25.HostInternetScsiHbaSendTarget;
import com.vmware.vim25.HostInternetScsiTargetTransport;
import com.vmware.vim25.HostMultipathInfoLogicalUnit;
import com.vmware.vim25.HostMultipathInfoPath;
import com.vmware.vim25.HostNetworkInfo;
import com.vmware.vim25.HostPortGroup;
import com.vmware.vim25.HostScsiDiskPartition;
import com.vmware.vim25.HostStorageDeviceInfo;
import com.vmware.vim25.HostTargetTransport;
import com.vmware.vim25.HostVirtualNic;
import com.vmware.vim25.HostVirtualSwitch;
import com.vmware.vim25.HostVmfsVolume;
import com.vmware.vim25.HostNasVolume;
import com.vmware.vim25.PhysicalNic;
import com.vmware.vim25.VirtualDevice;
import com.vmware.vim25.VirtualEthernetCard;
import com.vmware.vim25.VirtualHardware;
import com.vmware.vim25.VirtualMachineConfigInfo;
import com.vmware.vim25.VirtualMachineSummary;
import com.vmware.vim25.VmfsDatastoreInfo;
import com.vmware.vim25.NasDatastoreInfo;
import com.vmware.vim25.mo.ClusterComputeResource;
import com.vmware.vim25.mo.Datacenter;
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 VCenterInventory
{
private static final Logger log = Logger.getLogger(ESXServerSync.class);
private String host;
private String username;
private String password;
public VCenterInventory(String host, String username, String password) {
super();
this.host = host;
this.username = username;
this.password = password;
}
public VCenter getVCenterInfo() throws Exception {
URL url = new URL("https", host, "/sdk");
ServiceInstance si = new ServiceInstance(url, username, password, true);
Folder rootFolder = si.getRootFolder();
VCenter vc = new VCenter();
AboutInfo ai = si.getAboutInfo();
vc.setHostname(host);
vc.setDescr(ai.getFullName());
vc.setVersion(ai.getVersion());
InventoryNavigator inav = new InventoryNavigator(rootFolder);
ManagedEntity[] esxs = inav.searchManagedEntities("HostSystem");
List<ESXServer> servers = new ArrayList<ESXServer>();
for (ManagedEntity esx : esxs) {
HostSystem hs = (HostSystem) esx;
HostHardwareInfo hwi = hs.getHardware();
long hz = hwi.cpuInfo.hz;
long e9 = 1000000000;
double hzd = new java.math.BigDecimal(((double) hz) / e9).setScale(2, java.math.BigDecimal.ROUND_HALF_UP)
.doubleValue();
ESXServer server = new ESXServer();
server.setName(hs.getName());
server.setCpufrequency(String.valueOf(hzd) + "GHz");
servers.add(server);
}
vc.setEsxservers(servers);
ManagedEntity[] clusters = inav.searchManagedEntities("Datacenter");
List<Cluster> cs = new ArrayList<Cluster>();
for (ManagedEntity me : clusters) {
Datacenter dc = (Datacenter) me;
String path = dc.getName();
cs.addAll(getClusters(dc, path, dc.getHostFolder()));
}
vc.setClusters(cs);
si.getSessionManager().logout();
return vc;
}
private static List<Cluster> getClusters(Datacenter dc, String path, Folder f) throws Exception {
List<Cluster> cs = new ArrayList<Cluster>();
ManagedEntity[] ces = f.getChildEntity();
for (ManagedEntity ce : ces) {
if (ce instanceof ClusterComputeResource) {
cs.add(getClusterInfo(dc, path, (ClusterComputeResource) ce));
}
else if (ce instanceof Folder) {
cs.addAll(getClusters(dc, path + ":" + ce.getName(), (Folder) ce));
}
}
return cs;
}
public List<VMachine> getAllVM(String esxserver) throws Exception {
ManagedEntity[] esxs = null;
URL url = new URL("https", host, "/sdk");
ServiceInstance si = new ServiceInstance(url, username, password, true);
Folder rootFolder = si.getRootFolder();
esxs = new InventoryNavigator(rootFolder).searchManagedEntities("HostSystem");
for (ManagedEntity esx : esxs) {
HostSystem hs = (HostSystem) esx;
if (hs.getName().equals(esxserver)) {
List<VMachine> vms = getAllVM(hs);
si.getSessionManager().logout();
return vms;
}
}
si.getSessionManager().logout();
throw new RuntimeException("Cannot find esxserver:" + esxserver);
}
public List<Datastore> getAllDatastores(String esxserver) throws Exception {
ManagedEntity[] esxs = null;
URL url = new URL("https", host, "/sdk");
ServiceInstance si = new ServiceInstance(url, username, password, true);
Folder rootFolder = si.getRootFolder();
esxs = new InventoryNavigator(rootFolder).searchManagedEntities("HostSystem");
for (ManagedEntity esx : esxs) {
HostSystem hs = (HostSystem) esx;
if (hs.getName().equals(esxserver)) {
List<Datastore> ds= getAllDatastores(hs);
si.getSessionManager().logout();
return ds;
}
}
si.getSessionManager().logout();
throw new RuntimeException("Cannot find esxserver:" + esxserver);
}
public NetworkInfo getNetworkInfo(String esxserver) throws Exception {
ManagedEntity[] esxs = null;
URL url = new URL("https", host, "/sdk");
ServiceInstance si = new ServiceInstance(url, username, password, true);
Folder rootFolder = si.getRootFolder();
esxs = new InventoryNavigator(rootFolder).searchManagedEntities("HostSystem");
for (ManagedEntity esx : esxs) {
HostSystem hs = (HostSystem) esx;
if (hs.getName().equals(esxserver)) {
NetworkInfo ni= getNetworkInfo(hs);
si.getSessionManager().logout();
return ni;
}
}
si.getSessionManager().logout();
throw new RuntimeException("Cannot find esxserver:" + esxserver);
}
private NetworkInfo getNetworkInfo(HostSystem esxserver) throws Exception {
NetworkInfo ninfo = new NetworkInfo();
HostNetworkInfo nwi = esxserver.getConfig().getNetwork();
HostPortGroup[] portgroups = nwi.getPortgroup();
Map<String, String> pgMap = new HashMap<String, String>();
for (HostPortGroup portgroup : portgroups) {
pgMap.put(portgroup.getKey(), portgroup.getSpec().getName());
}
PhysicalNic[] pnics = nwi.getPnic();
Map<String, String> pnicMap = new HashMap<String, String>();
for (PhysicalNic pnic : pnics) {
pnicMap.put(pnic.getKey(), pnic.getMac());
}
List<VirtualSwitch> vss = new ArrayList<VirtualSwitch>();
HostVirtualSwitch[] vswtichs = nwi.getVswitch();
for (HostVirtualSwitch vswitch : vswtichs) {
VirtualSwitch vs = new VirtualSwitch();
vs.setName(vswitch.getName());
String[] macKeys = vswitch.getPnic();
if (macKeys != null) {
for (String key : macKeys) {
vs.addPhysicalMAC(pnicMap.get(key));
}
}
String[] pgs = vswitch.getPortgroup();
if (pgs != null) {
for (String pg : pgs) {
vs.addPortgroup(pgMap.get(pg));
}
}
// TODO
vss.add(vs);
}
ninfo.setVss(vss);
List<VirtualNic> vnics = new ArrayList<VirtualNic>();
HostVirtualNic[] virtualnics = nwi.getVnic();
for (HostVirtualNic virtualnic : virtualnics) {
VirtualNic vnic = new VirtualNic();
vnic.setName(virtualnic.getDevice());
vnic.setPortgroup(virtualnic.getPortgroup());
vnic.setMac(virtualnic.getSpec().getMac());
vnics.add(vnic);
}
ninfo.setVnics(vnics);
return ninfo;
}
private List<Datastore> getAllDatastores(HostSystem esxserver) throws Exception {
List<Datastore> datastores = new ArrayList<Datastore>();
Map<String, Disk> storages = new HashMap<String, Disk>();
HostStorageDeviceInfo sd = esxserver.getConfig().getStorageDevice();
HostHostBusAdapter[] hostBusAdapters = sd.getHostBusAdapter();
Map<String, String[]> hostBuses = new HashMap<String, String[]>();
for (HostHostBusAdapter adapter : hostBusAdapters) {
if (adapter instanceof HostInternetScsiHba) {
HostInternetScsiHbaSendTarget[] targets = ((HostInternetScsiHba) adapter).getConfiguredSendTarget();
if (null != targets && targets.length > 0) {
String[] floatings = new String[targets.length];
for (int i = 0; i < targets.length; i++) {
floatings[i] = targets[i].getAddress();
}
hostBuses.put(adapter.getKey(), floatings);
}
}
}
HostMultipathInfoLogicalUnit[] luns = sd.getMultipathInfo().getLun();
for (HostMultipathInfoLogicalUnit lun : luns) {
HostMultipathInfoPath[] paths = lun.getPath();
for (HostMultipathInfoPath path : paths) {
String pathName = path.getName();
String diskName = pathName.substring(pathName.lastIndexOf('-') + 1);
Disk s = new Disk();
s.setPath(pathName);
s.setDiskName(diskName);
String[] floatings = hostBuses.get(path.getAdapter());
s.setAddresses(floatings);
HostTargetTransport transport = path.getTransport();
if (transport instanceof HostInternetScsiTargetTransport) {
s.setIScsiName(((HostInternetScsiTargetTransport) transport).iScsiName);
}
storages.put(diskName, s);
}
}
com.vmware.vim25.mo.Datastore[] dss = esxserver.getDatastores();
for (com.vmware.vim25.mo.Datastore ds : dss) {
String name = ds.getName();
DatastoreInfo info = ds.getInfo();
HostVmfsVolume vmfs = null;
HostNasVolume nasfs = null;
if (info instanceof VmfsDatastoreInfo) {
vmfs = ((VmfsDatastoreInfo) info).getVmfs();
//log.warn("get vmfs:"+vmfs.getName());
}else if(info instanceof NasDatastoreInfo){
nasfs = ((NasDatastoreInfo)info).getNas();
//log.warn("get nasfs:"+nasfs.getName());
}
else {
continue;
}
Datastore datastore = new Datastore();
datastore.setName(name);
datastore.setStatus(ds.getOverallStatus().name());
datastore.setCapacity(ds.getSummary().getCapacity());
datastore.setFreeSpace(info.getFreeSpace());
datastore.setMaxFileSize(info.getMaxFileSize());
datastore.setUrl(info.getUrl());
datastore.setType(ds.getSummary().getType());
if(vmfs != null){
datastore.setVersion(vmfs.getVersion());
datastore.setVmfsUUID(vmfs.getUuid());
HostScsiDiskPartition[] extents = vmfs.getExtent();
for (HostScsiDiskPartition disk : extents) {
String diskName = disk.getDiskName();
datastore.addDisk(storages.get(diskName));
}
}
VirtualMachine[] vms = ds.getVms();
for (VirtualMachine vm : vms) {
datastore.addVm(vm.getName());
}
datastores.add(datastore);
}
return datastores;
}
private List<VMachine> getAllVM(HostSystem esxserver) throws Exception {
VirtualMachine[] vms = esxserver.getVms();
List<VMachine> machines = new ArrayList<VMachine>();
for (VirtualMachine vm : vms) {
VirtualMachineConfigInfo config = vm.getConfig();
VirtualMachineSummary summary = vm.getSummary();
VMachine vmachine = new VMachine();
vmachine.setName(vm.getName());
vmachine.setUuid(config.getUuid());
VirtualEthernetCard vec = getMac(config.getHardware());
vmachine.setMac(vec.getMacAddress().toUpperCase());
vmachine.setPortgroup(vec.getDeviceInfo().getSummary());
vmachine.setVmPath(config.getFiles().getVmPathName());
vmachine.setVmID(vm.getMOR().get_value());
vmachine.setModel(config.getGuestFullName());
vmachine.setPower(summary.getRuntime().getPowerState().name());
vmachine.setHealth(vm.getOverallStatus().name());
vmachine.setVersion(config.getVersion());
vmachine.setVmIP(summary.getGuest().getIpAddress());
vmachine.setCpucount(String.valueOf(vm.getSummary().getConfig().numCpu));
machines.add(vmachine);
}
return machines;
}
private static VirtualEthernetCard getMac(VirtualHardware hardware) {
VirtualDevice[] devices = hardware.getDevice();
for (VirtualDevice device : devices) {
if (device instanceof VirtualEthernetCard) {
return ((VirtualEthernetCard) device);
}
}
return null;
}
private static Cluster getClusterInfo(Datacenter dc, String path, ClusterComputeResource ccr) {
Cluster cluster = new Cluster();
cluster.setName(ccr.getName());
cluster.setDatacenter(dc.getName());
cluster.setPath(path);
ClusterComputeResourceSummary summary = (ClusterComputeResourceSummary) ccr.getSummary();
cluster.setCpu(summary.getTotalCpu());
cluster.setCpucount(summary.getNumCpuCores());
cluster.setMemory(summary.getTotalMemory());
cluster.setEvcMode(summary.getCurrentEVCModeKey());
ClusterConfigInfo config = ccr.getConfiguration();
cluster.setHaEnabled(config.getDasConfig().getEnabled());
cluster.setDrsEnabled(config.getDrsConfig().getEnabled());
HostSystem[] hs = ccr.getHosts();
for (HostSystem hostSystem : hs) {
cluster.addServer(hostSystem.getName());
}
return cluster;
}
}
相关推荐
VMware-vCenter-Server-Appliance-6.5.0.14000-7515524-updaterepo
VMware vCenter Server是VMware公司推出的一款企业级虚拟化管理平台,它负责管理虚拟化环境中的ESXi主机。在这个平台上,管理员可以对虚拟机进行集中管理、监控、配置和自动化任务等。VMware vCenter Server 5.5作为...
根据提供的百度网盘链接,可以获取到VMware vCenter Server 6.0 u2 的安装文件。为了确保顺利安装,请遵循以下步骤: 1. **下载安装包**:首先,访问提供的百度网盘链接,下载vCenter Server 6.0 u2 的安装程序。 2...
vSphere ESXi 5.1 VMware vSphere 5.1 vCenter Server 5.1 U1 VMware-viclient-all-5.1.0-1064113.exe347.4MB VMware-vCenter-Server-Appliance-5.1.0.10000-1...vmdk1.91GB VMware-VIMSetup-all-5.1.0-1065152.iso...
### VMware vCenter Server 5.5 安装教程知识点总结 #### 一、VMware vCenter Server 数据库 **知识点1:SQL Server 2008 R2 的安装与配置** - **安装功能选择**:在安装过程中选择必要的组件和服务。 - **验证...
VMware vCenter Server 5.1 是 VMware 公司推出的虚拟化管理平台,是虚拟化环境中的核心组件之一。本文档将详细介绍 VMware vCenter Server 5.1 的安装过程,并提供相关的知识点。 一、硬件要求 在安装 VMware ...
VMware ESXi、vCenter、vRealize和vSphere Replication是VMware公司推出的四个关键产品,它们在虚拟化和云计算领域中起着至关重要的作用。这些产品构成了VMware vSphere套件的核心部分,该套件是业界领先的服务器...
VMware vCenter Server是vSphere体系的核心组件,它作为统一的控制中心,允许IT管理员集中管理整个vSphere环境中的ESXi主机及其上运行的虚拟机。vCenter Server不仅提供了详细的虚拟基础架构信息,还支持大规模的...
Mware vCenter Server 6.7 U2a 官方原版镜像,无任何修改
支持vmware,vCenter 虚拟化的开发
在这个背景下,VMware vCenter Server作为虚拟化管理的核心工具,扮演着至关重要的角色。本文将深入探讨VMware vCenter Server的功能、优势及其在企业环境中的应用。 #### 一、概述 VMware vCenter Server是用于...
VMware vSphere 5.1 及VMware.vCenter.Server.v5.1 注册机,信就下,不信就算了,注册机杀软报毒
VMware vSphere 5.1 是一款虚拟化管理平台,其中vCenter Server是核心组件,负责集中管理和监控ESXi主机及虚拟机。本教程将详细阐述如何安装部署VMware vSphere 5.1 vCenter Server。 首先,vCenter Server 对硬件...
- **下载安装包**:从VMware官方网站获取vCenter Server 5.1的安装文件。 - **创建用户**:在域控制器上创建一个专门用于运行vCenter Server的服务账户。 - **网络配置**:确保网络连接稳定,并配置好DNS、DHCP等...
VMware vSphere/vCenter Server 8.0 是虚拟化管理平台的核心组件,用于管理和监控企业的虚拟化环境。本文将深入介绍如何安装和设置这些组件,以及它们的关键特性。 首先,vSphere 安装和设置涉及安装vSphere Client...
VMware vSphere 5.1 及VMware.vCenter.Server.v5.1 注册机 具体什么东西 就不用解释了
VMware.vCenter.Server.v5.1注册机