WEB开发网
开发学院软件开发Java Windows下Libvirt Java API使用教程(二)- 接口使用... 阅读

Windows下Libvirt Java API使用教程(二)- 接口使用说明

 2012-05-16 19:39:43 来源:WEB开发网   
核心提示: 介绍完libvirt Java API的部署工作,接下来我们就介绍一下接口的使用和代码样例,Windows下Libvirt Java API使用教程(二)- 接口使用说明,libvirt的管理单位是单个主机,所以探测和监控接口所能获取的信息的最大范围也是主机,所以先从主机入手,验证libvirt接口

 介绍完libvirt Java API的部署工作,接下来我们就介绍一下接口的使用和代码样例。

libvirt的管理单位是单个主机,所以探测和监控接口所能获取的信息的最大范围也是主机。所以先从主机入手,验证libvirt接口。主机(libvirt所在管理节点)探测相关接口验证代码如下:
 
 @Before 
    public void init() { 
        System.setProperty("jna.library.path", 
                "D:/Git-Repo/git/libvirt-java/libvirt-java/src/test/java/kubi/coder/"); 
        try { 
            xenConn = new Connect("xen+tcp://10.4.55.203/"); 
            // system代表拥有系统权限/session是用户权限 
            kvmConn = new Connect("qemu+tcp://10.4.54.10/system"); 
        } catch (LibvirtException e) { 
            e.printStackTrace(); 
        } 
    } 
 
    /** 
     * 主机信息探测接口验证,验证可以获取的主机属性和监控指标,分别考虑Xen环境和KVM环境 
     *  
     *  
     * @author lihzh 
     * @date 2012-5-15 下午1:28:00 
     */ 
    @Test 
    public void testDetectHost() { 
        // KVM 
        doDetectHost(kvmConn); 
        // XEN 
        doDetectHost(xenConn); 
    } 
 
    /** 
     * 执行探测主机测试函数 
     *  
     * @param conn 
     * @author lihzh 
     * @date 2012-5-15 下午1:37:37 
     */ 
    private void doDetectHost(Connect conn) { 
        try { 
            // Returns the free memory for the connection 
            // System.out.println("FreeMemory: " + conn.getFreeMemory());// 不支持 
             
            // Returns the system hostname on which the hypervisor is running. 
            // (the result of the gethostname(2) system call) 
            // If we are connected to a remote system, 
            // then this returns the hostname of the remote system 
            System.out.println("Host name: " + conn.getHostName()); 
            // Gets the name of the Hypervisor software used. 
            System.out.println("Type: " + conn.getType()); 
            // Gets the version level of the Hypervisor running. This may work 
            // only with hypervisor call, i.e. with priviledged access to the 
            // hypervisor, not with a Read-Only connection. If the version can't 
            // be extracted by lack of capacities returns 0. 
            // Returns: 
            // major * 1,000,000 + minor * 1,000 + release 
            System.out.println(conn.getVersion()); 
 
            NodeInfo nodeInfo = conn.nodeInfo(); 
            System.out.println("the number of active CPUs: " + nodeInfo.cpus); 
            System.out.println("number of core per socket: " + nodeInfo.cores); 
            System.out.println("memory size in kilobytes: " + nodeInfo.memory); 
            System.out.println("expected CPU frequency: " + nodeInfo.mhz); 
            System.out.println("string indicating the CPU model: " 
                    + nodeInfo.model); 
            System.out.println("the number of NUMA cell, 1 for uniform: " 
                    + nodeInfo.nodes); 
            System.out.println("number of CPU socket per node: " 
                    + nodeInfo.sockets); 
            System.out.println("number of threads per core: " 
                    + nodeInfo.threads); 
            System.out 
                    .println("the total number of CPUs supported but not necessarily active in the host.: " 
                            + nodeInfo.maxCpus()); 
 
            // for (String interName : conn.listInterfaces()) { 
            // System.out.println(interName); 
            // } 不支持 
 
            // Provides the list of names of defined interfaces on this host 
 
            // for (String interName : conn.listDefinedInterfaces()) { 
            // System.out.println(interName); 
            // } // 不支持 
 
            // Lists the active networks. 
            for (String networkName : conn.listNetworks()) { 
                System.out.println("Network name: " + networkName); 
            } 
 
            // Lists the names of the network filters 
            for (String networkFilterName : conn.listNetworkFilters()) { 
                System.out.println("Network filter name: " + networkFilterName); 
            } 
 
            System.out.println(conn.getCapabilities()); 
        } catch (LibvirtException e) { 
            e.printStackTrace(); 
        } 
    } 

1 2 3 4 5  下一页

Tags:Windows Libvirt Java

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接