介绍完libvirt Java API的部署工作:

Windows下Libvirt Java API使用教程(一)- 开发环境部署

接下来我们就介绍一下接口的使用和代码样例。

libvirt的管理单位是单个主机,所以探测和监控接口所能获取的信息的最大范围也是主机。所以先从主机入手,验证libvirt接口。主机(libvirt所在管理节点)探测相关接口验证代码如下:
   
   
   
   
  1. @Before 
  2.     public void init() { 
  3.         System.setProperty("jna.library.path"
  4.                 "D:/Git-Repo/git/libvirt-java/libvirt-java/src/test/java/kubi/coder/"); 
  5.         try { 
  6.             xenConn = new Connect("xen+tcp://10.4.55.203/"); 
  7.             // system代表拥有系统权限/session是用户权限 
  8.             kvmConn = new Connect("qemu+tcp://10.4.54.10/system"); 
  9.         } catch (LibvirtException e) { 
  10.             e.printStackTrace(); 
  11.         } 
  12.     } 
  13.  
  14.     /** 
  15.      * 主机信息探测接口验证,验证可以获取的主机属性和监控指标,分别考虑Xen环境和KVM环境 
  16.      *  
  17.      *  
  18.      * @author lihzh 
  19.      * @date 2012-5-15 下午1:28:00 
  20.      */ 
  21.     @Test 
  22.     public void testDetectHost() { 
  23.         // KVM 
  24.         doDetectHost(kvmConn); 
  25.         // XEN 
  26.         doDetectHost(xenConn); 
  27.     } 
  28.  
  29.     /** 
  30.      * 执行探测主机测试函数 
  31.      *  
  32.      * @param conn 
  33.      * @author lihzh 
  34.      * @date 2012-5-15 下午1:37:37 
  35.      */ 
  36.     private void doDetectHost(Connect conn) { 
  37.         try { 
  38.             // Returns the free memory for the connection 
  39.             // System.out.println("FreeMemory: " + conn.getFreeMemory());// 不支持 
  40.              
  41.             // Returns the system hostname on which the hypervisor is running. 
  42.             // (the result of the gethostname(2) system call) 
  43.             // If we are connected to a remote system, 
  44.             // then this returns the hostname of the remote system 
  45.             System.out.println("Host name: " + conn.getHostName()); 
  46.             // Gets the name of the Hypervisor software used. 
  47.             System.out.println("Type: " + conn.getType()); 
  48.             // Gets the version level of the Hypervisor running. This may work 
  49.             // only with hypervisor call, i.e. with priviledged access to the 
  50.             // hypervisor, not with a Read-Only connection. If the version can't 
  51.             // be extracted by lack of capacities returns 0. 
  52.             // Returns: 
  53.             // major * 1,000,000 + minor * 1,000 + release 
  54.             System.out.println(conn.getVersion()); 
  55.  
  56.             NodeInfo nodeInfo = conn.nodeInfo(); 
  57.             System.out.println("the number of active CPUs: " + nodeInfo.cpus); 
  58.             System.out.println("number of core per socket: " + nodeInfo.cores); 
  59.             System.out.println("memory size in kilobytes: " + nodeInfo.memory); 
  60.             System.out.println("expected CPU frequency: " + nodeInfo.mhz); 
  61.             System.out.println("string indicating the CPU model: " 
  62.                     + nodeInfo.model); 
  63.             System.out.println("the number of NUMA cell, 1 for uniform: " 
  64.                     + nodeInfo.nodes); 
  65.             System.out.println("number of CPU socket per node: " 
  66.                     + nodeInfo.sockets); 
  67.             System.out.println("number of threads per core: " 
  68.                     + nodeInfo.threads); 
  69.             System.out 
  70.                     .println("the total number of CPUs supported but not necessarily active in the host.: " 
  71.                             + nodeInfo.maxCpus()); 
  72.  
  73.             // for (String interName : conn.listInterfaces()) { 
  74.             // System.out.println(interName); 
  75.             // } 不支持 
  76.  
  77.             // Provides the list of names of defined interfaces on this host 
  78.  
  79.             // for (String interName : conn.listDefinedInterfaces()) { 
  80.             // System.out.println(interName); 
  81.             // } // 不支持 
  82.  
  83.             // Lists the active networks. 
  84.             for (String networkName : conn.listNetworks()) { 
  85.                 System.out.println("Network name: " + networkName); 
  86.             } 
  87.  
  88.             // Lists the names of the network filters 
  89.             for (String networkFilterName : conn.listNetworkFilters()) { 
  90.                 System.out.println("Network filter name: " + networkFilterName); 
  91.             } 
  92.  
  93.             System.out.println(conn.getCapabilities()); 
  94.         } catch (LibvirtException e) { 
  95.             e.printStackTrace(); 
  96.         } 
  97.     } 
  98.  

 分别在KVM和XEN环境下测试了libvirt接口,测试结果如下:

Host name: s5410
Type: QEMU
9001
the number of active CPUs: 64
number of core per socket: 8
memory size in kilobytes: 49444896
expected CPU frequency: 2131
string indicating the CPU model: x86_64
the number of NUMA cell, 1 for uniform: 1
number of CPU socket per node: 4
number of threads per core: 2
the total number of CPUs supported but not necessarily active in the host.: 64
Network name: hello
Network name: default
Network filter name: no-other-l2-traffic
Network filter name: allow-dhcp
...
 
 
    30b940dd-f79a-21a2-82d5-ddc1b1b4a7e4
   
      x86_64
      core2duo
     
     
     
       ...
   
   
     
     
        tcp
     
   
 
 
 
    hvm
   
      32
      /usr/libexec/qemu-kvm
      rhel5.4.0
      pc
      rhel5.4.4
      rhel5.5.0
      rhel5.6.0
     
     
     
        /usr/libexec/qemu-kvm
     
   
   
     
     
     
     
     
   
 
 
 ...
 
 
Host name: s55203
Type: Xen
3001000
the number of active CPUs: 32
number of core per socket: 8
memory size in kilobytes: 50276352
expected CPU frequency: 1995
string indicating the CPU model: x86_64
the number of NUMA cell, 1 for uniform: 1
number of CPU socket per node: 2
number of threads per core: 2
the total number of CPUs supported but not necessarily active in the host.: 32
Network name: default
Network filter name: allow-dhcp-server
Network filter name: qemu-announce-self-rarp
...
 
 
   
      x86_64
     
       
     
   
   
     
     
        xenmigr
     
   
 
 
 
    xen
   
      64
      /usr/lib64/xen/bin/qemu-dm
      xenpv
     
     
   
 
 
  ... 
 
说明:

注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的个数,核心数,内存总量等直观信息。有就足以。
看完了主机,再看看虚拟机。一个虚拟化环境中的核心资源自然是虚拟机,所以其属性和信息也自然多很多,上测试代码:

   
   
   
   
  1. /**  
  2.      * 测试探测虚拟机接口  
  3.      *   
  4.      * @author lihzh  
  5.      * @date 2012-5-16 上午11:14:20  
  6.      */ 
  7.     @Test 
  8.     public void testDetectDomains() {  
  9.         // KVM  
  10.         doDetectDomains(kvmConn);  
  11.         // XEN  
  12.         doDetectDomains(xenConn);  
  13.     }  
  14.  
  15. /**  
  16.      * 执行探测虚拟机测试函数  
  17.      *   
  18.      * @param conn  
  19.      * @author lihzh  
  20.      * @date 2012-5-16 上午11:15:27  
  21.      */ 
  22.     private void doDetectDomains(Connect conn) {  
  23.         try {  
  24.             // Lists the active domains.(列出所有处于启动(激活)状态的虚拟机的id)  
  25.             for (int activeDomId : conn.listDomains()) {  
  26.                 System.out.println("Active vm id: " + activeDomId);  
  27.                 // 根据Id,探测各个虚拟机的详细信息  
  28.                 Domain domain = conn.domainLookupByID(activeDomId);  
  29.                 // Gets the hypervisor ID number for the domain  
  30.                 System.out.println("Domain id: " + domain.getID());  
  31.                 // Gets the public name for this domain  
  32.                 System.out.println("Domain name: " + domain.getName());  
  33.                 // Gets the type of domain operation system.  
  34.                 System.out.println("Domain os type: " + domain.getOSType());  
  35.                 // Gets the UUID for this domain as string.  
  36.                 System.out.println("Domain uuid: " + domain.getUUIDString());  
  37.                 // Retrieve the maximum amount of physical memory allocated to a  
  38.                 // domain.  
  39.                 System.out.println("Domain max memory: " 
  40.                         + domain.getMaxMemory());  
  41.                 // Provides the maximum number of virtual CPUs supported for the  
  42.                 // guest VM. If the guest is inactive, this is basically the  
  43.                 // same as virConnectGetMaxVcpus. If the guest is running this  
  44.                 // will reflect the maximum number of virtual CPUs the guest was  
  45.                 // booted with.  
  46.                 System.out.println("Domain max vcpu: " + domain.getMaxVcpus());  
  47.                 // Provides an XML description of the domain. The description  
  48.                 // may be  
  49.                 // reused later to relaunch the domain with createLinux().  
  50.                 System.out.println("Domain xml description: " 
  51.                         + domain.getXMLDesc(0));  
  52.                 System.out.println("Domain maxMen allowed: " 
  53.                         + domain.getInfo().maxMem);  
  54.                 System.out.println("Domain memory: " + domain.getInfo().memory);  
  55.                 // domain.getJobInfo()  
  56.                 // 不支持  
  57.                 System.out.println("Domain state: " + domain.getInfo().state);  
  58.                 // Provides a boolean value indicating whether the network is  
  59.                 // configured to be automatically started when the host machine  
  60.                 // boots.  
  61.                 System.out.println("Domain network autostart: " 
  62.                         + domain.getAutostart());  
  63.                 // Extracts information about virtual CPUs of this domain  
  64.                 for (VcpuInfo vcpuInfo : domain.getVcpusInfo()) {  
  65.                     System.out.println("cpu: " + vcpuInfo.cpu);  
  66.                     System.out.println("cpu time: " + vcpuInfo.cpuTime);  
  67.                     System.out.println("cpu number: " + vcpuInfo.number);  
  68.                     System.out.println("cpu state: " + vcpuInfo.state);  
  69.                 }  
  70.               
  71.                 // 如果是KVM环境  
  72.                 if (conn.getURI().startsWith("qemu")) {  
  73.                     // This function returns block device (disk) stats for block  
  74.                     // devices attached to the domain  
  75.                     DomainBlockInfo blockInfo = domain  
  76.                             .blockInfo("/opt/awcloud/instance/admin/" 
  77.                                     + domain.getName() + "/disk");  
  78.                     System.out.println("Disk Capacity: " 
  79.                             + blockInfo.getCapacity());  
  80.                     System.out.println("Disk allocation: " 
  81.                             + blockInfo.getAllocation());  
  82.                     System.out.println("Disk physical: " 
  83.                             + blockInfo.getPhysical());  
  84.  
  85.                     DomainBlockStats blockStats = domain.blockStats("vda");  
  86.                     // 磁盘读取请求总数  
  87.                     System.out.println("read request num: " + blockStats.rd_req);  
  88.                     // 磁盘读取总bytes数  
  89.                     System.out.println("read request num: " + blockStats.rd_bytes);  
  90.                     // 磁盘写入请求总数  
  91.                     System.out.println("read request num: " + blockStats.wr_req);  
  92.                     // 磁盘写入总bytes数  
  93.                     System.out.println("read request num: " + blockStats.wr_bytes);  
  94.                 }  
  95.  
  96.             }  
  97.               
  98.             // 列出所有停止态的虚拟机  
  99.             for (String name : conn.listDefinedDomains()) {  
  100.                 System.out.println("Inactive domain name: " + name);  
  101.             }  
  102.  
  103.         } catch (LibvirtException e) {  
  104.             e.printStackTrace();  
  105.         }  
  106.     } 

循环较多,摘取部分结果如下:

   
   
   
   
Active vm id: 53
Domain id: 53
Domain name: i-546A099E
Domain os type: hvm
Domain uuid: e608560a-2c03-8e48-2e60-d0d01693f530
Domain max memory: 147456
Domain max vcpu: 1
Domain xml description:
  i-546A099E
  e608560a-2c03-8e48-2e60-d0d01693f530
  131072
  131072
  1
 
    hvm
    /usr/lib/xen/boot/hvmloader
   
 
 
   
   
   
 
 
  destroy
  restart
  restart
 
    /usr/lib64/xen/bin/qemu-dm
   
     
     
     
   
   
     
     
     
   
   
     
     
     

你可能感兴趣的:(使用说明,虚拟机,xen,kvm,libvirt)