Vmware vSphere最实用的入门级教程
vSphere,ESXi 和 vCenter 的区别
vSphere 是在数据中心产品下的一套软件,vSphere 类似微软的 Office 办公套件,Office 办公套件包含了许多软件如 Word, Excel, Access 等,和Office 一样,vSphere 也是一个软件的集合,他包括了 vCenter, ESXi 和vSphere client 等。
ESXi 是 vSphere 中最重要的一个组件。ESXi 是虚拟化服务,所有的虚拟机都是运行在 ESXi 服务上面。为了安装,管理和访问这些虚拟机,你需要另外的 vSphere 套件,也就是vSphere client 或 vCenter。
vSphere client 允许管理员访问 ESXi 服务并管理虚拟机。vSphere client 是安装在客户机(也就是管理员的笔记本)上面。
vCenter server 和 vSphere client 很像,但是它和功能更加强大。vCenter server 是安装在 Window 服务器或 Linux 服务器里面。是一个中心化的管理应用,你可以通过它管理所有的虚拟机和 ESXi 物理机。如测试环境中 192.168.0.66 的 vCenter 目前添加管理 192.168.0.6 的 EXSI 服务器(6.7 版本)和 192.168.0.164(6.0 版本)的 EXSI 服务器,可管理192.168.0.6 和 192.168.0.164 服务器下的所有虚拟机。
使用vijava进行二次开发
导包
<!-- exsi sdk -->
<dependency>
<groupId>com.vmware</groupId>
<artifactId>vijava</artifactId>
<version>5.1</version>
</dependency>
初始化ServiceInstance工具类
package com.fastech.esxi.utils;
import com.vmware.vim25.mo.ServiceInstance;
import java.net.URL;
/**
* @ClassName: SessionUtil
* @Description:
* @Author: zhangjin
* @Date: 2022/4/7
*/
public class SessionUtil {
private static String ipAddress = "";
private static String userName = "";
private static String passWord = "";
private static String url = "";
private static ServiceInstance serviceInstance = null;
public synchronized static ServiceInstance getInstance(String serverName, String userName, String passWord) throws Exception{
initServiceInstance(serverName, userName, passWord);
return serviceInstance;
}
public static void initServiceInstance(String ip, String userName, String passWord) throws Exception {
ipAddress = ip;
userName = userName;
passWord = passWord;
url = "https://" + ipAddress + "/sdk";
if(serviceInstance==null){
serviceInstance = new ServiceInstance(new URL(url), userName, passWord, true);
}
}
}
测试主机HostSystem
package com.fastech.esxi;
import com.fastech.esxi.utils.SessionUtil;
import com.vmware.vim25.*;
import com.vmware.vim25.mo.*;
import java.text.NumberFormat;
public class HostSystemConfigTest {
public static void main(String[] args) {
ServiceInstance serviceInstance = null;
try{
String ip = "192.168.0.66";
String username = "administrator@vsphere.local";
String password = "1qaz@WSX";
serviceInstance = SessionUtil.getInstance(ip, username, password);
Folder rootFolder = serviceInstance.getRootFolder();
ManagedEntity managedEntity = new InventoryNavigator(rootFolder).searchManagedEntity("HostSystem", "192.168.0.6");
HostSystem hostSystem = (HostSystem) managedEntity;
HostListSummary summary = hostSystem.getSummary();
HostHardwareInfo hardware = hostSystem.getHardware();
Datastore[] datastores = hostSystem.getDatastores();
HostNetworkSystem hostNetworkSystem = hostSystem.getHostNetworkSystem();
HostNetworkConfig networkConfig = hostNetworkSystem.getNetworkConfig();
HostVirtualNicConfig[] vnic = networkConfig.vnic;
String subnetMask = vnic[0].spec.getIp().subnetMask;
HostConfigInfo config = hostSystem.getConfig();
HostRuntimeInfo runtime = hostSystem.getRuntime();
Network[] networks = hostSystem.getNetworks();
HostNetworkInfo network = hostSystem.getConfig().getNetwork();
PhysicalNic[] pnic = network.getPnic();
String mac = pnic[0].getMac();
String hsName = hostSystem.getName();
System.out.println("IP地址:" + hsName);
AboutInfo product = config.getProduct();
String manageProgram = product.getFullName();
System.out.println("管理程序:" + manageProgram);
HostRuntimeInfo hostRuntimeInfo = summary.getRuntime();
HostSystemConnectionState connectionState = hostRuntimeInfo.getConnectionState();
System.out.println("状况:" + connectionState);
HostSystemInfo systemInfo = hardware.getSystemInfo();
String vendor = systemInfo.getVendor();
System.out.println("制造商:" + vendor);
String model = systemInfo.getModel();
System.out.println("型号:" + model);
HostHardwareSummary hostHardwareSummary = summary.getHardware();
String cpuModel = hostHardwareSummary.getCpuModel();
System.out.println("处理器类型:" + cpuModel);
short numCpuPkgs = hostHardwareSummary.getNumCpuPkgs();
System.out.println("插槽数量(cpu):" + numCpuPkgs);
short numCpuCores = hostHardwareSummary.getNumCpuCores();
System.out.println("CPU内核数:" + numCpuCores);
int numNetCards = hostHardwareSummary.getNumNics();
System.out.println("网卡数量:" + numNetCards);
VirtualMachine[] vms = hostSystem.getVms();
System.out.println("虚拟机数量:" + vms.length);
HostListSummaryQuickStats quickStats = summary.getQuickStats();
Integer uptime = quickStats.uptime == null ? null : summary.quickStats.uptime / 3600 / 24;
System.out.println("正常运行时间:" + uptime);
NumberFormat nf = NumberFormat.getNumberInstance();
double storageAll = 0;
double storageUsage = 0;
double storageAvail = 0;
for (int k = 0; k < datastores.length; k++) {
// String name = datastores[k].getName();
DatastoreSummary ds = datastores[k].getSummary();
double capacity = ds.capacity;
double freeSpace = ds.freeSpace;
double capacityTB = capacity / 1024 / 1024 / 1024 / 1024;//总容量
double freeSpaceTB = freeSpace / 1024 / 1024 / 1024 / 1024;//剩余容量
double usedTmp = capacityTB - freeSpaceTB;//已使用容量
storageAll += capacityTB;
storageAvail += freeSpaceTB;
storageUsage += usedTmp;
}
System.out.println("存储容量:" + nf.format(storageAll) + "TB");
System.out.println("存储已用容量:" + nf.format(storageUsage) + "TB");
int storagePer = (int) (storageUsage * 100 / storageAll);
System.out.println("存储使用百分比(%):" + storagePer);
System.out.println("存储可用容量:" + nf.format(storageAvail) + "TB");
String cpuUsage = "N/A";
int overallCpuUsageMhz = summary.quickStats.overallCpuUsage == null ? 0 : summary.quickStats.overallCpuUsage;
if (overallCpuUsageMhz > 0 && overallCpuUsageMhz < 1000) {
cpuUsage = overallCpuUsageMhz + "Mhz";
} else if (overallCpuUsageMhz >= 1000) {
cpuUsage = (overallCpuUsageMhz / 1000) + "Ghz";
}
String cpuAll = "N/A";
int overallCpuAllMhz = summary.hardware.cpuMhz * summary.hardware.numCpuCores;
if (overallCpuAllMhz > 0 && overallCpuAllMhz < 1000) {
cpuAll = overallCpuAllMhz + "Mhz";
} else if (overallCpuUsageMhz >= 1000) {
cpuAll = (overallCpuAllMhz / 1000) + "Ghz";
}
String cpuAvail = "N/A";
int overallCpuAvailMhz = (overallCpuAllMhz - overallCpuUsageMhz);
if (overallCpuAvailMhz > 0 && overallCpuAvailMhz < 1000) {
cpuAvail = overallCpuAvailMhz + "Mhz";
} else if (overallCpuAvailMhz >= 1000) {
cpuAvail = (overallCpuAvailMhz / 1000) + "Ghz";
}
System.out.println("CPU容量:" + cpuAll);
System.out.println("CPU已用容量:" + cpuUsage);
String cpuPer = nf.format(overallCpuUsageMhz * 100 / overallCpuAllMhz) + "%";
System.out.println("CPU使用百分比(%):" + cpuPer);
System.out.println("CPU可用容量:" + cpuAvail);
String memoryUsage = "N/A";
double memoryUseSizeM = summary.quickStats.overallMemoryUsage;
double memoryUseSizeG = Double.parseDouble(nf.format(memoryUseSizeM / 1024));
if (memoryUseSizeM > 0 && memoryUseSizeM < 1024) {
memoryUsage = memoryUseSizeM + "MB";
} else if (memoryUseSizeM >= 1024) {
memoryUsage = memoryUseSizeG + "GB";
}
String memoryAll = "N/A";
double memoryAllSizeB = summary.hardware.memorySize;
double memoryAllSizeG = Double.parseDouble(nf.format(memoryAllSizeB / 1024 / 1024 / 1024));
if (memoryAllSizeB > 0 && memoryAllSizeB < 1024) {
memoryAll = memoryAllSizeB + "MB";
} else if (memoryAllSizeB >= 1024) {
memoryAll = memoryAllSizeG + "GB";
}
String memoryAvail = "N/A";
double memoryAvailSizeB = (memoryAllSizeB - (memoryUseSizeM * 1024 * 1024));
double memoryAvailSizeG = Double.parseDouble(nf.format(memoryAvailSizeB / 1024 / 1024 / 1024));
if (memoryAllSizeB > 0 && memoryAvailSizeB < 1024) {
memoryAvail = memoryAvailSizeB + "MB";
} else if (memoryAvailSizeB >= 1024) {
memoryAvail = memoryAvailSizeG + "GB";
}
System.out.println("内存容量:" + memoryAll);
System.out.println("内存已用容量:" + memoryUsage);
int memoryPer = (int) (memoryUseSizeG * 100 / memoryAllSizeG);
System.out.println("内存使用百分比(%):" + memoryPer);
System.out.println("内存可用容量:" + memoryAvail);
String uuid = summary.hardware.getUuid();
System.out.println("uuid:" + uuid);
HostConfigSummary hostConfigSummary = summary.getConfig();
String osHostname = hostConfigSummary.name;
System.out.println("主机的名称:" + osHostname);
serviceInstance.getServerConnection().logout();
} catch (Exception e){
e.printStackTrace();
} finally {
if (serviceInstance != null) {
serviceInstance.getServerConnection().logout();
}
}
}
}
测试虚拟机VirtualMachine
package com.fastech.esxi;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fastech.esxi.utils.SessionUtil;
import com.vmware.vim25.*;
import com.vmware.vim25.mo.*;
import java.math.BigDecimal;
import java.util.Calendar;
public class VirtualMachineConfigTest {
public static void main(String[] args) {
ServiceInstance serviceInstance = null;
try{
String ip = "192.168.0.66";
String username = "administrator@vsphere.local";
String password = "1qaz@WSX";
String vmName = "192.168.0.201-dev-centos7.9-201";
serviceInstance = SessionUtil.getInstance(ip, username, password);
Folder rootFolder = serviceInstance.getRootFolder();
ManagedEntity mes = new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmName);
VirtualMachine virtualMachine = (VirtualMachine)mes;
VirtualMachineSummary virtualMachineSummary = virtualMachine.getSummary();
long uncommitted = virtualMachineSummary.storage.uncommitted;
long unshared = virtualMachineSummary.storage.unshared;
BigDecimal committedG = new BigDecimal(uncommitted).add(new BigDecimal(unshared)).divide(new BigDecimal(1024*1024*1024),4, BigDecimal.ROUND_HALF_UP).stripTrailingZeros();
BigDecimal storageUsageGB = new BigDecimal(virtualMachineSummary.storage.committed).divide(new BigDecimal(1024*1024*1024),4, BigDecimal.ROUND_HALF_UP).stripTrailingZeros();
Network[] networks = virtualMachine.getNetworks();
VirtualMachineRuntimeInfo runtime1 = virtualMachine.getRuntime();
VirtualMachineConfigInfo config1 = virtualMachine.getConfig();
GuestInfo guest1 = virtualMachine.getGuest();
VirtualMachineGuestSummary guest = virtualMachineSummary.getGuest();
String ipAddress = guest.getIpAddress();
System.out.println("IP地址:" + ipAddress);
String hostName = guest.getHostName();
System.out.println("主机名:" + hostName);
GuestNicInfo[] guestNicInfos = virtualMachine.getGuest().getNet();
VirtualMachineRuntimeInfo runtime = virtualMachineSummary.getRuntime();
VirtualMachinePowerState powerState = runtime.getPowerState();
System.out.println("主机状态:" + powerState.name());
VirtualMachineConfigSummary config = virtualMachineSummary.getConfig();
String os = config.getGuestFullName();
System.out.println("客户机操作系统:" + os);
VirtualMachineQuickStats quickStats = virtualMachineSummary.quickStats;
Integer uptimeSeconds = quickStats.getUptimeSeconds();
System.out.println("启动时间:" + uptimeSeconds);
VirtualMachineStorageInfo storage = virtualMachine.getStorage();
VirtualMachineUsageOnDatastore[] perDatastoreUsage = storage.perDatastoreUsage;
long storageUsageB = 0;
for (VirtualMachineUsageOnDatastore pdu : perDatastoreUsage) {
storageUsageB += pdu.committed / 1024 / 1024 / 1024;
}
System.out.println("存储已用容量(B):" + storageUsageB);
long committed = virtualMachineSummary.storage.committed / 1024 / 1024 / 1024;
System.out.println("存储已用容量(B):" + committed);
Integer cpuUsageupperlimit = virtualMachineSummary.runtime.getMaxCpuUsage() / 1024;
System.out.println("cpu使用:"+cpuUsageupperlimit);
Integer memoryUpperlimit = virtualMachineSummary.runtime.getMaxMemoryUsage() / 1024;
System.out.println("内存使用:"+memoryUpperlimit);
Integer cpuReservation = virtualMachineSummary.getConfig().cpuReservation / 1024;
System.out.println("cpu预留:"+cpuReservation);
ResourceConfigSpec resourceConfig = virtualMachine.getResourceConfig();
int cpuAllocationShares = resourceConfig.getCpuAllocation().shares.shares / 1024;
Long cpuAllocationLimit = resourceConfig.getCpuAllocation().limit / 1024;
Integer memoryReservation = virtualMachineSummary.getConfig().memoryReservation / 1024;
Long memoryAllocationLimit = resourceConfig.getMemoryAllocation().limit / 1024;
int memoryAllocationShares = resourceConfig.getMemoryAllocation().shares.shares / 1024;
Integer consumedOverheadMemory = virtualMachineSummary.quickStats.consumedOverheadMemory / 1024;
VirtualMachineConfigSummary virtualMachineConfigSummary = virtualMachineSummary.config;
Integer numCpu = virtualMachineConfigSummary.getNumCpu();
System.out.println("CPU数量:" + numCpu);
int cpuUsageMhz = virtualMachineSummary.getQuickStats().overallCpuUsage == null ? 0 : virtualMachineSummary.getQuickStats().overallCpuUsage;
System.out.println("CPU已用容量(Mhz):" + cpuUsageMhz);
ManagedObjectReference mor = new ManagedObjectReference();
mor.setType("HostSystem");
mor.setVal(virtualMachine.getRuntime().getHost().getVal());
HostSystem result = new HostSystem(serviceInstance.getServerConnection(), mor);
System.out.println("宿主机:" + result.getName());
HostListSummary summary = result.getSummary();
String cpuPer = cpuUsageMhz * 100 / summary.hardware.cpuMhz * numCpu + "%";
System.out.println("CPU使用百分比(%):" + cpuPer);
// //虚拟机 虚拟磁盘大小
// VirtualMachineStorageSummary virtualMachineStorageSummary=virtualMachineSummary.storage;
// long committed=virtualMachineStorageSummary.committed;//已用容量
// System.out.println("存储已用容量(B):" + committed);
// long unshared=virtualMachineStorageSummary.unshared;
// long uncommit= virtualMachineStorageSummary.uncommitted;//空闲容量
// System.out.println("硬盘容量(B):"+uncommit + committed);
//虚拟机 内存大小
int memorySizeMB = virtualMachineConfigSummary.memorySizeMB == null ? 0 : virtualMachineConfigSummary.memorySizeMB;
System.out.println("内存容量(MB):" + memorySizeMB);
int memoryUsageMb = virtualMachineSummary.getQuickStats().guestMemoryUsage == null ? 0 : virtualMachineSummary.getQuickStats().guestMemoryUsage;
System.out.println("内存已用容量(MB):" + memoryUsageMb);
String memoryPer = (memoryUsageMb * 100 / memorySizeMB) + "%";
System.out.println("内存使用百分比(%):" + memoryPer);
String uuid = virtualMachineConfigSummary.getUuid();
System.out.println("uuid:" + uuid);
Integer numEthernetCards = virtualMachineConfigSummary.getNumEthernetCards();
System.out.println("网卡数量:" + numEthernetCards);
Calendar laststartupTime = summary.runtime.bootTime;
System.out.println("虚拟机最近启动的时间戳:" + laststartupTime);
// Integer cpuUsageupperlimit = virtualMachineSummary.runtime.getMaxCpuUsage();
// System.out.println("当前CPU使用上限:" + cpuUsageupperlimit);
//
// Integer memoryUpperlimit = virtualMachineSummary.runtime.getMaxMemoryUsage();
// System.out.println("当前内存使用上限:" + memoryUpperlimit);
if (guestNicInfos != null && guestNicInfos.length > 0) {
for (GuestNicInfo guestNicInfo : guestNicInfos) {
JSONObject vminfonetjo = new JSONObject();
vminfonetjo.put("network", guestNicInfo.getNetwork());
vminfonetjo.put("macAddress", guestNicInfo.getMacAddress());
vminfonetjo.put("ipAddress", guestNicInfo.getIpAddress() == null ? new JSONArray() : guestNicInfo.getIpAddress());
}
}
} catch (Exception e){
e.printStackTrace();
} finally {
if (serviceInstance != null) {
serviceInstance.getServerConnection().logout();
}
}
}
}
附vijava学习笔记:https://blog.csdn.net/zhouzhiwengang/category_3106045.html