Search in sources :

Example 21 with PowerState

use of com.cloud.vm.VirtualMachine.PowerState 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)

Aggregations

PowerState (com.cloud.vm.VirtualMachine.PowerState)21 HashMap (java.util.HashMap)9 LibvirtException (org.libvirt.LibvirtException)8 CheckVirtualMachineAnswer (com.cloud.agent.api.CheckVirtualMachineAnswer)7 Domain (org.libvirt.Domain)6 HostVmStateReportEntry (com.cloud.agent.api.HostVmStateReportEntry)4 RevertToVMSnapshotAnswer (com.cloud.agent.api.RevertToVMSnapshotAnswer)4 Connection (com.xensource.xenapi.Connection)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Connect (org.libvirt.Connect)4 VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)3 VMSnapshot (com.cloud.vm.snapshot.VMSnapshot)3 VirtualMachinePowerState (com.vmware.vim25.VirtualMachinePowerState)3 VBD (com.xensource.xenapi.VBD)2 VDI (com.xensource.xenapi.VDI)2 VM (com.xensource.xenapi.VM)2 Map (java.util.Map)2 Answer (com.cloud.agent.api.Answer)1 BackupSnapshotAnswer (com.cloud.agent.api.BackupSnapshotAnswer)1 CheckHealthAnswer (com.cloud.agent.api.CheckHealthAnswer)1