Package org.libvirt

Examples of org.libvirt.Domain


    }

    protected Integer getVncPort(Connect conn, String vmName)
            throws LibvirtException {
        LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
        Domain dm = null;
        try {
            dm = conn.domainLookupByName(vmName);
            String xmlDesc = dm.getXMLDesc(0);
            parser.parseDomainXML(xmlDesc);
            return parser.getVncPort();
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
        }
View Full Code Here


    }


    private String getGuestType(Connect conn, String vmName) {
        LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
        Domain dm = null;
        try {
            dm = conn.domainLookupByName(vmName);
            String xmlDesc = dm.getXMLDesc(0);
            parser.parseDomainXML(xmlDesc);
            return parser.getDescription();
        } catch (LibvirtException e) {
            s_logger.trace("Ignoring libvirt error.", e);
            return null;
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
        }
View Full Code Here

                .domainLookupByName(vmName);
    }

    protected List<InterfaceDef> getInterfaces(Connect conn, String vmName) {
        LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
        Domain dm = null;
        try {
            dm = conn.domainLookupByName(vmName);
            parser.parseDomainXML(dm.getXMLDesc(0));
            return parser.getInterfaces();

        } catch (LibvirtException e) {
            s_logger.debug("Failed to get dom xml: " + e.toString());
            return new ArrayList<InterfaceDef>();
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (LibvirtException e) {
                s_logger.trace("Ignoring libvirt error.", e);
            }
        }
View Full Code Here

        }
    }

    public List<DiskDef> getDisks(Connect conn, String vmName) {
        LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
        Domain dm = null;
        try {
            dm = conn.domainLookupByName(vmName);
            parser.parseDomainXML(dm.getXMLDesc(0));
            return parser.getDisks();

        } catch (LibvirtException e) {
            s_logger.debug("Failed to get dom xml: " + e.toString());
            return new ArrayList<DiskDef>();
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (LibvirtException e) {
                s_logger.trace("Ignoring libvirt error.", e);
            }
        }
View Full Code Here



    private List<VmDiskStatsEntry> getVmDiskStat(Connect conn, String vmName)
            throws LibvirtException {
        Domain dm = null;
        try {
            dm = getDomain(conn, vmName);

            List<VmDiskStatsEntry> stats = new ArrayList<VmDiskStatsEntry>();

            List<DiskDef> disks = getDisks(conn, vmName);

            for (DiskDef disk : disks) {
                if (disk.getDeviceType() != deviceType.DISK)
                    break;
                DomainBlockStats blockStats = dm.blockStats(disk.getDiskLabel());
                String path = disk.getDiskPath(); // for example, path = /mnt/pool_uuid/disk_path/
                String diskPath = null;
                if (path != null) {
                    String[] token = path.split("/");
                    if (token.length > 3) {
                        diskPath = token[3];
                        VmDiskStatsEntry stat = new VmDiskStatsEntry(vmName, diskPath, blockStats.wr_req, blockStats.rd_req, blockStats.wr_bytes, blockStats.rd_bytes);
                        stats.add(stat);
                    }
                }
            }

            return stats;
        } finally {
            if (dm != null) {
                dm.free();
            }
        }
    }
View Full Code Here

        Calendar _timestamp;
    }

    VmStatsEntry getVmStat(Connect conn, String vmName)
            throws LibvirtException {
        Domain dm = null;
        try {
            dm = getDomain(conn, vmName);
            DomainInfo info = dm.getInfo();

            VmStatsEntry stats = new VmStatsEntry();
            stats.setNumCPUs(info.nrVirtCpu);
            stats.setEntityType("vm");

            /* get cpu utilization */
            vmStats oldStats = null;

            Calendar now = Calendar.getInstance();

            oldStats = _vmStats.get(vmName);

            long elapsedTime = 0;
            if (oldStats != null) {
                elapsedTime = now.getTimeInMillis()
                        - oldStats._timestamp.getTimeInMillis();
                double utilization = (info.cpuTime - oldStats._usedTime)
                        / ((double) elapsedTime * 1000000);

                NodeInfo node = conn.nodeInfo();
                utilization = utilization / node.cpus;
                if(utilization > 0){
                    stats.setCPUUtilization(utilization * 100);
                }
            }

            /* get network stats */

            List<InterfaceDef> vifs = getInterfaces(conn, vmName);
            long rx = 0;
            long tx = 0;
            for (InterfaceDef vif : vifs) {
                DomainInterfaceStats ifStats = dm.interfaceStats(vif
                        .getDevName());
                rx += ifStats.rx_bytes;
                tx += ifStats.tx_bytes;
            }

            if (oldStats != null) {
                double deltarx = rx - oldStats._rx;
                if (deltarx > 0)
                    stats.setNetworkReadKBs(deltarx / 1024);
                double deltatx = tx - oldStats._tx;
                if (deltatx > 0)
                    stats.setNetworkWriteKBs(deltatx / 1024);
            }

            /* get disk stats */
            List<DiskDef> disks = getDisks(conn, vmName);
            long io_rd = 0;
            long io_wr = 0;
            long bytes_rd = 0;
            long bytes_wr = 0;
            for (DiskDef disk : disks) {
                DomainBlockStats blockStats = dm.blockStats(disk.getDiskLabel());
                io_rd += blockStats.rd_req;
                io_wr += blockStats.wr_req;
                bytes_rd += blockStats.rd_bytes;
                bytes_wr += blockStats.wr_bytes;
            }
           
            if (oldStats != null) {
                long deltaiord = io_rd - oldStats._io_rd;
                if (deltaiord > 0)
                    stats.setDiskReadIOs(deltaiord);
                long deltaiowr = io_wr - oldStats._io_wr;
                if (deltaiowr > 0)
                    stats.setDiskWriteIOs(deltaiowr);
                double deltabytesrd = bytes_rd - oldStats._bytes_rd;
                if (deltabytesrd > 0)
                    stats.setDiskReadKBs(deltabytesrd / 1024);
                double deltabyteswr = bytes_wr - oldStats._bytes_wr;
                if (deltabyteswr > 0)
                    stats.setDiskWriteKBs(deltabyteswr / 1024);
            }
           
            /* save to Hashmap */
            vmStats newStat = new vmStats();
            newStat._usedTime = info.cpuTime;
            newStat._rx = rx;
            newStat._tx = tx;
            newStat._io_rd = io_rd;
            newStat._io_wr = io_wr;
            newStat._bytes_rd = bytes_rd;
            newStat._bytes_wr = bytes_wr;
            newStat._timestamp = now;
            _vmStats.put(vmName, newStat);
            return stats;
        } finally {
            if (dm != null) {
                dm.free();
            }
        }
    }
View Full Code Here

        } else {
            nicTO.setBroadcastType(BroadcastDomainType.Vlan);
            nicTO.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlanId));
        }

        Domain vm = getDomain(conn, vmName);
        vm.attachDevice(_vifDriver.plug(nicTO, "Other PV (32-bit)").toString());
    }
View Full Code Here

        Connect conn;
        NicTO nic = cmd.getNic();
        String vmName = cmd.getVmName();
        try {
            conn = LibvirtConnection.getConnection();
            Domain vm = getDomain(conn, vmName);
            List<InterfaceDef> pluggedNics = getInterfaces(conn, vmName);
            Integer nicnum = 0;
            for (InterfaceDef pluggedNic : pluggedNics) {
                if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
                    s_logger.debug("found existing nic for mac "+ pluggedNic.getMacAddress() + " at index "+nicnum);
                    return new PlugNicAnswer(cmd, true, "success");
                }
                nicnum++;
            }
            vm.attachDevice(_vifDriver.plug(nic, "Other PV (32-bit)").toString());
            return new PlugNicAnswer(cmd, true, "success");
        } catch (Exception e) {
            String msg = " Plug Nic failed due to " + e.toString();
            s_logger.warn(msg, e);
            return new PlugNicAnswer(cmd, false, msg);
View Full Code Here

        Connect conn;
        NicTO nic = cmd.getNic();
        String vmName = cmd.getInstanceName();
        try {
            conn = LibvirtConnection.getConnection();
            Domain vm = getDomain(conn, vmName);
            List<InterfaceDef> pluggedNics = getInterfaces(conn, vmName);
            for (InterfaceDef pluggedNic : pluggedNics) {
                if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
                    vm.detachDevice(pluggedNic.toString());
                    return new UnPlugNicAnswer(cmd, true, "success");
                }
            }
            return new UnPlugNicAnswer(cmd, true, "success");
        } catch (Exception e) {
View Full Code Here

            }
        }

        try {
            conn = LibvirtConnection.getConnection();
            Domain vm = getDomain(conn, routerName);
            List<InterfaceDef> pluggedNics = getInterfaces(conn, routerName);
            InterfaceDef routerNic = null;

            for (InterfaceDef pluggedNic : pluggedNics) {
                if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
View Full Code Here

TOP

Related Classes of org.libvirt.Domain

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.