Search in sources :

Example 6 with HostVmStateReportEntry

use of com.cloud.agent.api.HostVmStateReportEntry in project cloudstack by apache.

the class LibvirtComputingResource method getHostVmStateReport.

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

Example 7 with HostVmStateReportEntry

use of com.cloud.agent.api.HostVmStateReportEntry in project cloudstack by apache.

the class LibvirtComputingResource method getHostVmStateReport.

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

Example 8 with HostVmStateReportEntry

use of com.cloud.agent.api.HostVmStateReportEntry in project cloudstack by apache.

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<String, HostVmStateReportEntry>();
    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 Throwable e) {
            s_logger.warn("Unable to get vms", e);
        }
        try {
            Thread.sleep(1000);
        } catch (final InterruptedException ex) {
        }
    }
    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(_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.agent.api.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)

Aggregations

HostVmStateReportEntry (com.cloud.agent.api.HostVmStateReportEntry)8 HashMap (java.util.HashMap)8 PowerState (com.cloud.vm.VirtualMachine.PowerState)4 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 LibvirtException (org.libvirt.LibvirtException)2 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)1 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)1 State (com.cloud.vm.VirtualMachine.State)1 DynamicProperty (com.vmware.vim25.DynamicProperty)1 ObjectContent (com.vmware.vim25.ObjectContent)1 VirtualMachinePowerState (com.vmware.vim25.VirtualMachinePowerState)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