Search in sources :

Example 1 with HostVmStateReportEntry

use of com.cloud.legacymodel.vm.HostVmStateReportEntry in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResource method getHostVmStateReport.

private HashMap<String, HostVmStateReportEntry> getHostVmStateReport(final Connect conn) {
    final HashMap<String, HostVmStateReportEntry> vmStates = new HashMap<>();
    String[] vms = null;
    int[] ids = null;
    try {
        ids = conn.listDomains();
    } catch (final LibvirtException e) {
        logger.warn("Unable to listDomains", e);
        return null;
    }
    try {
        vms = conn.listDefinedDomains();
    } catch (final LibvirtException e) {
        logger.warn("Unable to listDomains", e);
        return null;
    }
    Domain dm = null;
    for (final int id : ids) {
        try {
            dm = conn.domainLookupByID(id);
            final DomainState ps = dm.getInfo().state;
            final PowerState state = convertToPowerState(ps);
            logger.trace("VM " + dm.getName() + ": powerstate = " + ps + "; vm state=" + state.toString());
            final String vmName = dm.getName();
            vmStates.put(vmName, new HostVmStateReportEntry(state, conn.getHostName()));
        } catch (final LibvirtException e) {
            logger.warn("Unable to get vms", e);
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (final LibvirtException e) {
                logger.trace("Ignoring libvirt error.", e);
            }
        }
    }
    for (final String vm : vms) {
        try {
            dm = conn.domainLookupByName(vm);
            final DomainState ps = dm.getInfo().state;
            final PowerState state = convertToPowerState(ps);
            final String vmName = dm.getName();
            logger.trace("VM " + vmName + ": powerstate = " + ps + "; vm state=" + state.toString());
            vmStates.put(vmName, new HostVmStateReportEntry(state, conn.getHostName()));
        } catch (final LibvirtException e) {
            logger.warn("Unable to get vms", e);
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (final LibvirtException e) {
                logger.trace("Ignoring libvirt error.", e);
            }
        }
    }
    return vmStates;
}
Also used : LibvirtException(org.libvirt.LibvirtException) HostVmStateReportEntry(com.cloud.legacymodel.vm.HostVmStateReportEntry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DomainState(org.libvirt.DomainInfo.DomainState) Domain(org.libvirt.Domain) PowerState(com.cloud.legacymodel.vm.VirtualMachine.PowerState)

Example 2 with HostVmStateReportEntry

use of com.cloud.legacymodel.vm.HostVmStateReportEntry in project cosmic by MissionCriticalCloud.

the class CitrixResourceBase method getHostVmStateReport.

protected HashMap<String, HostVmStateReportEntry> getHostVmStateReport(final Connection conn) {
    // TODO : new VM sync model does not require a cluster-scope report, we
    // need to optimize
    // the report accordingly
    final HashMap<String, HostVmStateReportEntry> vmStates = new HashMap<>();
    Map<VM, VM.Record> vm_map = null;
    for (int i = 0; i < 2; i++) {
        try {
            // USE THIS TO GET ALL VMS FROM
            vm_map = VM.getAllRecords(conn);
            // A CLUSTER
            break;
        } catch (final XmlRpcException | XenAPIException e) {
            s_logger.warn("Unable to get vms", e);
        }
        try {
            Thread.sleep(1000);
        } catch (final InterruptedException e) {
            s_logger.warn("Caught (previously ignored) interrupted exception", e);
        }
    }
    if (vm_map == null) {
        return vmStates;
    }
    for (final VM.Record record : vm_map.values()) {
        if (record.isControlDomain || record.isASnapshot || record.isATemplate) {
            // Skip DOM0
            continue;
        }
        final VmPowerState ps = record.powerState;
        final Host host = record.residentOn;
        String host_uuid = null;
        if (!isRefNull(host)) {
            try {
                host_uuid = host.getUuid(conn);
            } catch (final BadServerResponse e) {
                s_logger.error("Failed to get host uuid for host " + host.toWireString(), e);
            } catch (final XenAPIException e) {
                s_logger.error("Failed to get host uuid for host " + host.toWireString(), e);
            } catch (final XmlRpcException e) {
                s_logger.error("Failed to get host uuid for host " + host.toWireString(), e);
            }
            if (host_uuid.equalsIgnoreCase(this._host.getUuid())) {
                vmStates.put(record.nameLabel, new HostVmStateReportEntry(convertToPowerState(ps), host_uuid));
            }
        }
    }
    return vmStates;
}
Also used : BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) HostVmStateReportEntry(com.cloud.legacymodel.vm.HostVmStateReportEntry) HashMap(java.util.HashMap) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) VM(com.xensource.xenapi.VM) VmPowerState(com.xensource.xenapi.Types.VmPowerState) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 3 with HostVmStateReportEntry

use of com.cloud.legacymodel.vm.HostVmStateReportEntry in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResource method getHostVmStateReport.

private HashMap<String, HostVmStateReportEntry> getHostVmStateReport() {
    final HashMap<String, HostVmStateReportEntry> vmStates = new HashMap<>();
    Connect conn = null;
    if (getHypervisorType() == HypervisorType.KVM) {
        try {
            conn = LibvirtConnection.getConnectionByType(HypervisorType.KVM.toString());
            vmStates.putAll(getHostVmStateReport(conn));
        } catch (final LibvirtException e) {
            logger.debug("Failed to get connection: " + e.getMessage());
        }
    }
    return vmStates;
}
Also used : LibvirtException(org.libvirt.LibvirtException) HostVmStateReportEntry(com.cloud.legacymodel.vm.HostVmStateReportEntry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Connect(org.libvirt.Connect)

Aggregations

HostVmStateReportEntry (com.cloud.legacymodel.vm.HostVmStateReportEntry)3 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 LibvirtException (org.libvirt.LibvirtException)2 PowerState (com.cloud.legacymodel.vm.VirtualMachine.PowerState)1 Host (com.xensource.xenapi.Host)1 BadServerResponse (com.xensource.xenapi.Types.BadServerResponse)1 VmPowerState (com.xensource.xenapi.Types.VmPowerState)1 XenAPIException (com.xensource.xenapi.Types.XenAPIException)1 VM (com.xensource.xenapi.VM)1 XmlRpcException (org.apache.xmlrpc.XmlRpcException)1 Connect (org.libvirt.Connect)1 Domain (org.libvirt.Domain)1 DomainState (org.libvirt.DomainInfo.DomainState)1