Windows下Libvirt Java API使用教程(二)- 接口使用说明
2012-05-16 19:39:43 来源:WEB开发网核心提示: 说明:注1:标注不支持的,是在当前环境当前libvirt版本下,Windows下Libvirt Java API使用教程(二)- 接口使用说明(3),运行会报错的接口, unsupported in sys interface 注2:名词解释hvm:gives similar information but whe
说明:
注1:标注不支持的,是在当前环境当前libvirt版本下,运行会报错的接口。
unsupported in sys interface
注2:名词解释
hvm:
gives similar information but when running a 32 bit OS fully virtualized with Xen using the hvm support。
numa:
For processors that support hyperthreading, this is the number of hyperthreads
they have per core. On a machine that doesn't support hyperthreading, this
will be 1.
说实话,这么多信息中,笔者关注的不多,有很多甚至说不出其代表的含义,笔者关注只是cpu的个数,核心数,内存总量等直观信息。有就足以。
看完了主机,再看看虚拟机。一个虚拟化环境中的核心资源自然是虚拟机,所以其属性和信息也自然多很多,上测试代码:
/**
* 测试探测虚拟机接口
*
* @author lihzh
* @date 2012-5-16 上午11:14:20
*/
@Test
public void testDetectDomains() {
// KVM
doDetectDomains(kvmConn);
// XEN
doDetectDomains(xenConn);
}
/**
* 执行探测虚拟机测试函数
*
* @param conn
* @author lihzh
* @date 2012-5-16 上午11:15:27
*/
private void doDetectDomains(Connect conn) {
try {
// Lists the active domains.(列出所有处于启动(激活)状态的虚拟机的id)
for (int activeDomId : conn.listDomains()) {
System.out.println("Active vm id: " + activeDomId);
// 根据Id,探测各个虚拟机的详细信息
Domain domain = conn.domainLookupByID(activeDomId);
// Gets the hypervisor ID number for the domain
System.out.println("Domain id: " + domain.getID());
// Gets the public name for this domain
System.out.println("Domain name: " + domain.getName());
// Gets the type of domain operation system.
System.out.println("Domain os type: " + domain.getOSType());
// Gets the UUID for this domain as string.
System.out.println("Domain uuid: " + domain.getUUIDString());
// Retrieve the maximum amount of physical memory allocated to a
// domain.
System.out.println("Domain max memory: "
+ domain.getMaxMemory());
// Provides the maximum number of virtual CPUs supported for the
// guest VM. If the guest is inactive, this is basically the
// same as virConnectGetMaxVcpus. If the guest is running this
// will reflect the maximum number of virtual CPUs the guest was
// booted with.
System.out.println("Domain max vcpu: " + domain.getMaxVcpus());
// Provides an XML description of the domain. The description
// may be
// reused later to relaunch the domain with createLinux().
System.out.println("Domain xml description: "
+ domain.getXMLDesc(0));
System.out.println("Domain maxMen allowed: "
+ domain.getInfo().maxMem);
System.out.println("Domain memory: " + domain.getInfo().memory);
// domain.getJobInfo()
// 不支持
System.out.println("Domain state: " + domain.getInfo().state);
// Provides a boolean value indicating whether the network is
// configured to be automatically started when the host machine
// boots.
System.out.println("Domain network autostart: "
+ domain.getAutostart());
// Extracts information about virtual CPUs of this domain
for (VcpuInfo vcpuInfo : domain.getVcpusInfo()) {
System.out.println("cpu: " + vcpuInfo.cpu);
System.out.println("cpu time: " + vcpuInfo.cpuTime);
System.out.println("cpu number: " + vcpuInfo.number);
System.out.println("cpu state: " + vcpuInfo.state);
}
// 如果是KVM环境
if (conn.getURI().startsWith("qemu")) {
// This function returns block device (disk) stats for block
// devices attached to the domain
DomainBlockInfo blockInfo = domain
.blockInfo("/opt/awcloud/instance/admin/"
+ domain.getName() + "/disk");
System.out.println("Disk Capacity: "
+ blockInfo.getCapacity());
System.out.println("Disk allocation: "
+ blockInfo.getAllocation());
System.out.println("Disk physical: "
+ blockInfo.getPhysical());
DomainBlockStats blockStats = domain.blockStats("vda");
// 磁盘读取请求总数
System.out.println("read request num: " + blockStats.rd_req);
// 磁盘读取总bytes数
System.out.println("read request num: " + blockStats.rd_bytes);
// 磁盘写入请求总数
System.out.println("read request num: " + blockStats.wr_req);
// 磁盘写入总bytes数
System.out.println("read request num: " + blockStats.wr_bytes);
}
}
// 列出所有停止态的虚拟机
for (String name : conn.listDefinedDomains()) {
System.out.println("Inactive domain name: " + name);
}
} catch (LibvirtException e) {
e.printStackTrace();
}
}
更多精彩
赞助商链接
