用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 vsphere开发小demo",你可以学习到如何使用vSphere API与vCenter进行交互,获取和管理虚拟化环境中的各种资源。这个过程涉及到网络编程、SOAP通信、对象模型理解和错误处理等多个方面,对于...
在虚拟化环境中,vCenter Server 是 VMware vSphere 的核心组件,用于集中管理数据中心的资源。VASA(Virtual Storage Area Network)Provider 是一个重要的组件,它允许存储系统与 vCenter 进行通信,以便实现更高...
与Perl和Java SDK一样,您可以使用它来管理ESX和vCenter Server。 当前版本支持vSphere 7.0 API。 特定于RbVmomi的文档在,可以与官方一起使用。 安装 gem install rbvmomi 用法 打开虚拟机的一个简单示例: ...
嵌入式系统开发_STM32微控制器_ESP8266WiFi模块_心率传感器_加速度计_OLED显示屏_蓝牙40_低功耗设计_实时操作系统_智能手表_多功能健康监测_运动数据记录_
驾校自动化_网页自动化爬虫技术_Python27多线程HTTP请求模拟_龙泉驾校2014版约车系统自动预约助手_通过模拟登录和循环请求实现自动约车功能_支持失败自动递增车号重试_
Linux系统编程_操作系统内核_系统调用_进程线程_信号处理_文件IO_进程间通信_多线程同步_网络编程_UNIX环境编程_中文翻译勘误_错误修正_代码示例优化_技术文档校对_开
wanjunshe_Python-Tensorflow_12888_1745868924470
scratch少儿编程逻辑思维游戏源码-铅笔画.zip
即时通讯应用开发_基于LeanCloud云服务与Android原生开发_集成QQ第三方登录与即时聊天功能的社交应用_实现用户注册登录创建聊天室发送文字消息展示用户信息头像昵称并提供
scratch少儿编程逻辑思维游戏源码-伞兵大乱斗(云变量).zip
scratch少儿编程逻辑思维游戏源码-楼层酷跑.zip
scratch少儿编程逻辑思维游戏源码-零下之寒颤.zip
scratch少儿编程逻辑思维游戏源码-密室逃生.zip
少儿编程scratch项目源代码文件案例素材-爪猫足球.zip
命令行完成git本地仓库创建、将代码提交到暂存区、查看暂存区信息、将代码提交到本地仓库、将本地仓库关联到远程仓库、推送到远程仓库全过程的截图
少儿编程scratch项目源代码文件案例素材-纸.zip
scratch少儿编程逻辑思维游戏源码-日本冒险.zip
scratch少儿编程逻辑思维游戏源码-狼人杀跑酷.zip
scratch少儿编程逻辑思维游戏源码-史莱姆杀手.zip
少儿编程scratch项目源代码文件案例素材-粘粘世界.zip