Search in sources :

Example 16 with CustomFieldStringValue

use of com.vmware.vim25.CustomFieldStringValue in project cloudstack by apache.

the class VmwareResource method gcAndKillHungWorkerVMs.

private void gcAndKillHungWorkerVMs() {
    try {
        // take the chance to do left-over dummy VM cleanup from previous run
        VmwareContext context = getServiceContext();
        VmwareHypervisorHost hyperHost = getHyperHost(context);
        VmwareManager mgr = hyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
        if (hyperHost.isHyperHostConnected()) {
            mgr.gcLeftOverVMs(context);
            s_logger.info("Scan hung worker VM to recycle");
            int workerKey = ((HostMO) hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_WORKER);
            int workerTagKey = ((HostMO) hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_WORKER_TAG);
            String workerPropName = String.format("value[%d]", workerKey);
            String workerTagPropName = String.format("value[%d]", workerTagKey);
            // GC worker that has been running for too long
            ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] { "name", "config.template", workerPropName, workerTagPropName });
            if (ocs != null) {
                for (ObjectContent oc : ocs) {
                    List<DynamicProperty> props = oc.getPropSet();
                    if (props != null) {
                        boolean template = false;
                        boolean isWorker = false;
                        String workerTag = null;
                        for (DynamicProperty prop : props) {
                            if (prop.getName().equals("config.template")) {
                                template = (Boolean) prop.getVal();
                            } else if (prop.getName().equals(workerPropName)) {
                                CustomFieldStringValue val = (CustomFieldStringValue) prop.getVal();
                                if (val != null && val.getValue() != null && val.getValue().equalsIgnoreCase("true"))
                                    isWorker = true;
                            } else if (prop.getName().equals(workerTagPropName)) {
                                CustomFieldStringValue val = (CustomFieldStringValue) prop.getVal();
                                workerTag = val.getValue();
                            }
                        }
                        VirtualMachineMO vmMo = new VirtualMachineMO(hyperHost.getContext(), oc.getObj());
                        if (!template && isWorker) {
                            boolean recycle = false;
                            recycle = mgr.needRecycle(workerTag);
                            if (recycle) {
                                s_logger.info("Recycle pending worker VM: " + vmMo.getName());
                                vmMo.cancelPendingTasks();
                                vmMo.powerOff();
                                vmMo.detachAllDisksAndDestroy();
                            }
                        }
                    }
                }
            }
        } else {
            s_logger.error("Host is no longer connected.");
        }
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            invalidateServiceContext();
        }
    }
}
Also used : DynamicProperty(com.vmware.vim25.DynamicProperty) VmwareManager(com.cloud.hypervisor.vmware.manager.VmwareManager) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) ObjectContent(com.vmware.vim25.ObjectContent) CustomFieldStringValue(com.vmware.vim25.CustomFieldStringValue) RemoteException(java.rmi.RemoteException)

Example 17 with CustomFieldStringValue

use of com.vmware.vim25.CustomFieldStringValue in project cloudstack by apache.

the class VirtualMachineMO method getNetworksWithDetails.

public List<NetworkDetails> getNetworksWithDetails() throws Exception {
    List<NetworkDetails> networks = new ArrayList<NetworkDetails>();
    int gcTagKey = getCustomFieldKey("Network", CustomFieldConstants.CLOUD_GC);
    if (gcTagKey == 0) {
        gcTagKey = getCustomFieldKey("DistributedVirtualPortgroup", CustomFieldConstants.CLOUD_GC_DVP);
        s_logger.debug("The custom key for dvPortGroup is : " + gcTagKey);
    }
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("Network");
    pSpec.getPathSet().add("name");
    pSpec.getPathSet().add("vm");
    pSpec.getPathSet().add(String.format("value[%d]", gcTagKey));
    TraversalSpec vm2NetworkTraversal = new TraversalSpec();
    vm2NetworkTraversal.setType("VirtualMachine");
    vm2NetworkTraversal.setPath("network");
    vm2NetworkTraversal.setName("vm2NetworkTraversal");
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(vm2NetworkTraversal);
    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.getPropSet().add(pSpec);
    pfSpec.getObjectSet().add(oSpec);
    List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
    pfSpecArr.add(pfSpec);
    List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
    if (ocs != null && ocs.size() > 0) {
        for (ObjectContent oc : ocs) {
            ArrayOfManagedObjectReference morVms = null;
            String gcTagValue = null;
            String name = null;
            for (DynamicProperty prop : oc.getPropSet()) {
                if (prop.getName().equals("name"))
                    name = prop.getVal().toString();
                else if (prop.getName().equals("vm"))
                    morVms = (ArrayOfManagedObjectReference) prop.getVal();
                else if (prop.getName().startsWith("value[")) {
                    CustomFieldStringValue val = (CustomFieldStringValue) prop.getVal();
                    if (val != null)
                        gcTagValue = val.getValue();
                }
            }
            NetworkDetails details = new NetworkDetails(name, oc.getObj(), (morVms != null ? morVms.getManagedObjectReference().toArray(new ManagedObjectReference[morVms.getManagedObjectReference().size()]) : null), gcTagValue);
            networks.add(details);
        }
        s_logger.debug("Retrieved " + networks.size() + " networks with key : " + gcTagKey);
    }
    return networks;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) DynamicProperty(com.vmware.vim25.DynamicProperty) ArrayList(java.util.ArrayList) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) TraversalSpec(com.vmware.vim25.TraversalSpec) CustomFieldStringValue(com.vmware.vim25.CustomFieldStringValue) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 18 with CustomFieldStringValue

use of com.vmware.vim25.CustomFieldStringValue in project cloudstack by apache.

the class VmwareResource method getVmStats.

private HashMap<String, VmStatsEntry> getVmStats(List<String> vmNames) throws Exception {
    VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
    HashMap<String, VmStatsEntry> vmResponseMap = new HashMap<String, VmStatsEntry>();
    ManagedObjectReference perfMgr = getServiceContext().getServiceContent().getPerfManager();
    VimPortType service = getServiceContext().getService();
    PerfCounterInfo rxPerfCounterInfo = null;
    PerfCounterInfo txPerfCounterInfo = null;
    PerfCounterInfo diskReadIOPerfCounterInfo = null;
    PerfCounterInfo diskWriteIOPerfCounterInfo = null;
    PerfCounterInfo diskReadKbsPerfCounterInfo = null;
    PerfCounterInfo diskWriteKbsPerfCounterInfo = null;
    Integer windowInterval = getVmwareWindowTimeInterval();
    final XMLGregorianCalendar startTime = VmwareHelper.getXMLGregorianCalendar(new Date(), windowInterval);
    final XMLGregorianCalendar endTime = VmwareHelper.getXMLGregorianCalendar(new Date(), 0);
    List<PerfCounterInfo> cInfo = getServiceContext().getVimClient().getDynamicProperty(perfMgr, "perfCounter");
    for (PerfCounterInfo info : cInfo) {
        if ("net".equalsIgnoreCase(info.getGroupInfo().getKey()) && "average".equalsIgnoreCase(info.getRollupType().value())) {
            if ("transmitted".equalsIgnoreCase(info.getNameInfo().getKey())) {
                txPerfCounterInfo = info;
            }
            if ("received".equalsIgnoreCase(info.getNameInfo().getKey())) {
                rxPerfCounterInfo = info;
            }
        }
        if ("virtualdisk".equalsIgnoreCase(info.getGroupInfo().getKey())) {
            if ("numberReadAveraged".equalsIgnoreCase(info.getNameInfo().getKey())) {
                diskReadIOPerfCounterInfo = info;
            }
            if ("numberWriteAveraged".equalsIgnoreCase(info.getNameInfo().getKey())) {
                diskWriteIOPerfCounterInfo = info;
            }
            if ("read".equalsIgnoreCase(info.getNameInfo().getKey())) {
                diskReadKbsPerfCounterInfo = info;
            }
            if ("write".equalsIgnoreCase(info.getNameInfo().getKey())) {
                diskWriteKbsPerfCounterInfo = info;
            }
        }
    }
    int key = ((HostMO) hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
    if (key == 0) {
        s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
    }
    String instanceNameCustomField = "value[" + key + "]";
    final String numCpuStr = "summary.config.numCpu";
    final String cpuUseStr = "summary.quickStats.overallCpuUsage";
    final String guestMemUseStr = "summary.quickStats.guestMemoryUsage";
    final String memLimitStr = "resourceConfig.memoryAllocation.limit";
    final String memMbStr = "config.hardware.memoryMB";
    final String allocatedCpuStr = "summary.runtime.maxCpuUsage";
    ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] { "name", numCpuStr, cpuUseStr, guestMemUseStr, memLimitStr, memMbStr, allocatedCpuStr, instanceNameCustomField });
    if (ocs != null && ocs.length > 0) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> objProps = oc.getPropSet();
            if (objProps != null) {
                String name = null;
                String numberCPUs = null;
                double maxCpuUsage = 0;
                String memlimit = null;
                String memkb = null;
                String guestMemusage = null;
                String vmNameOnVcenter = null;
                String vmInternalCSName = null;
                double allocatedCpu = 0;
                for (DynamicProperty objProp : objProps) {
                    if (objProp.getName().equals("name")) {
                        vmNameOnVcenter = objProp.getVal().toString();
                    } else if (objProp.getName().contains(instanceNameCustomField)) {
                        if (objProp.getVal() != null)
                            vmInternalCSName = ((CustomFieldStringValue) objProp.getVal()).getValue();
                    } else if (objProp.getName().equals(guestMemUseStr)) {
                        guestMemusage = objProp.getVal().toString();
                    } else if (objProp.getName().equals(numCpuStr)) {
                        numberCPUs = objProp.getVal().toString();
                    } else if (objProp.getName().equals(cpuUseStr)) {
                        maxCpuUsage = NumberUtils.toDouble(objProp.getVal().toString());
                    } else if (objProp.getName().equals(memLimitStr)) {
                        memlimit = objProp.getVal().toString();
                    } else if (objProp.getName().equals(memMbStr)) {
                        memkb = objProp.getVal().toString();
                    } else if (objProp.getName().equals(allocatedCpuStr)) {
                        allocatedCpu = NumberUtils.toDouble(objProp.getVal().toString());
                    }
                }
                maxCpuUsage = (maxCpuUsage / allocatedCpu) * 100;
                if (vmInternalCSName != null) {
                    name = vmInternalCSName;
                } else {
                    name = vmNameOnVcenter;
                }
                if (!vmNames.contains(name)) {
                    continue;
                }
                ManagedObjectReference vmMor = hyperHost.findVmOnHyperHost(name).getMor();
                assert (vmMor != null);
                double networkReadKBs = 0;
                double networkWriteKBs = 0;
                double diskReadIops = 0;
                double diskWriteIops = 0;
                double diskReadKbs = 0;
                double diskWriteKbs = 0;
                final ArrayList<PerfMetricId> perfMetricsIds = new ArrayList<PerfMetricId>();
                if (rxPerfCounterInfo != null) {
                    perfMetricsIds.add(VmwareHelper.createPerfMetricId(rxPerfCounterInfo, ""));
                }
                if (txPerfCounterInfo != null) {
                    perfMetricsIds.add(VmwareHelper.createPerfMetricId(txPerfCounterInfo, ""));
                }
                if (diskReadIOPerfCounterInfo != null) {
                    perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskReadIOPerfCounterInfo, "*"));
                }
                if (diskWriteIOPerfCounterInfo != null) {
                    perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskWriteIOPerfCounterInfo, "*"));
                }
                if (diskReadKbsPerfCounterInfo != null) {
                    perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskReadKbsPerfCounterInfo, ""));
                }
                if (diskWriteKbsPerfCounterInfo != null) {
                    perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskWriteKbsPerfCounterInfo, ""));
                }
                if (perfMetricsIds.size() > 0) {
                    try {
                        final PerfQuerySpec qSpec = new PerfQuerySpec();
                        qSpec.setEntity(vmMor);
                        qSpec.setFormat("normal");
                        qSpec.setIntervalId(windowInterval);
                        qSpec.setStartTime(startTime);
                        qSpec.setEndTime(endTime);
                        qSpec.getMetricId().addAll(perfMetricsIds);
                        final List<PerfEntityMetricBase> perfValues = service.queryPerf(perfMgr, Collections.singletonList(qSpec));
                        for (final PerfEntityMetricBase perfValue : perfValues) {
                            if (!(perfValue instanceof PerfEntityMetric)) {
                                continue;
                            }
                            final List<PerfMetricSeries> seriesList = ((PerfEntityMetric) perfValue).getValue();
                            for (final PerfMetricSeries series : seriesList) {
                                if (!(series instanceof PerfMetricIntSeries)) {
                                    continue;
                                }
                                final List<Long> values = ((PerfMetricIntSeries) series).getValue();
                                double sum = 0;
                                for (final Long value : values) {
                                    sum += value;
                                }
                                double avg = sum / (values.size() * 1f);
                                if (series.getId().getCounterId() == rxPerfCounterInfo.getKey()) {
                                    networkReadKBs = avg;
                                }
                                if (series.getId().getCounterId() == txPerfCounterInfo.getKey()) {
                                    networkWriteKBs = avg;
                                }
                                if (series.getId().getCounterId() == diskReadIOPerfCounterInfo.getKey()) {
                                    diskReadIops += avg;
                                }
                                if (series.getId().getCounterId() == diskWriteIOPerfCounterInfo.getKey()) {
                                    diskWriteIops += avg;
                                }
                                if (series.getId().getCounterId() == diskReadKbsPerfCounterInfo.getKey()) {
                                    diskReadKbs = avg;
                                }
                                if (series.getId().getCounterId() == diskWriteKbsPerfCounterInfo.getKey()) {
                                    diskWriteKbs = avg;
                                }
                            }
                        }
                    } catch (Exception e) {
                        s_logger.error(String.format("Unable to execute PerfQuerySpec due to: [%s]. The window interval is enabled in vCenter?", VmwareHelper.getExceptionMessage(e)), e);
                    }
                }
                final VmStatsEntry vmStats = new VmStatsEntry(NumberUtils.toDouble(memkb) * 1024, NumberUtils.toDouble(guestMemusage) * 1024, NumberUtils.toDouble(memlimit) * 1024, maxCpuUsage, networkReadKBs, networkWriteKBs, NumberUtils.toInt(numberCPUs), "vm");
                vmStats.setDiskReadIOs(diskReadIops);
                vmStats.setDiskWriteIOs(diskWriteIops);
                vmStats.setDiskReadKBs(diskReadKbs);
                vmStats.setDiskWriteKBs(diskWriteKbs);
                vmResponseMap.put(name, vmStats);
            }
        }
    }
    return vmResponseMap;
}
Also used : PerfCounterInfo(com.vmware.vim25.PerfCounterInfo) DynamicProperty(com.vmware.vim25.DynamicProperty) HashMap(java.util.HashMap) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) ArrayList(java.util.ArrayList) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) PerfEntityMetricBase(com.vmware.vim25.PerfEntityMetricBase) ObjectContent(com.vmware.vim25.ObjectContent) PerfMetricIntSeries(com.vmware.vim25.PerfMetricIntSeries) PerfMetricSeries(com.vmware.vim25.PerfMetricSeries) PerfMetricId(com.vmware.vim25.PerfMetricId) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) VimPortType(com.vmware.vim25.VimPortType) Date(java.util.Date) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) PerfQuerySpec(com.vmware.vim25.PerfQuerySpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) PerfEntityMetric(com.vmware.vim25.PerfEntityMetric)

Example 19 with CustomFieldStringValue

use of com.vmware.vim25.CustomFieldStringValue in project cloudstack by apache.

the class HostDatastoreSystemMO method findDatastoreCluster.

public ManagedObjectReference findDatastoreCluster(String name) throws Exception {
    // added Apache CloudStack specific name convention, we will use custom field "cloud.uuid" as datastore name as well
    CustomFieldsManagerMO cfmMo = new CustomFieldsManagerMO(_context, _context.getServiceContent().getCustomFieldsManager());
    int key = cfmMo.getCustomFieldKey("StoragePod", CustomFieldConstants.CLOUD_UUID);
    assert (key != 0);
    List<ObjectContent> ocs = getDatastoreClusterPropertiesOnHostDatastoreSystem(new String[] { "name", String.format("value[%d]", key) });
    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            if (oc.getPropSet().get(0).getVal().equals(name))
                return oc.getObj();
            if (oc.getPropSet().size() > 1) {
                DynamicProperty prop = oc.getPropSet().get(1);
                if (prop != null && prop.getVal() != null) {
                    if (prop.getVal() instanceof CustomFieldStringValue) {
                        String val = ((CustomFieldStringValue) prop.getVal()).getValue();
                        if (val.equalsIgnoreCase(name))
                            return oc.getObj();
                    }
                }
            }
        }
    }
    return null;
}
Also used : ObjectContent(com.vmware.vim25.ObjectContent) DynamicProperty(com.vmware.vim25.DynamicProperty) CustomFieldStringValue(com.vmware.vim25.CustomFieldStringValue)

Example 20 with CustomFieldStringValue

use of com.vmware.vim25.CustomFieldStringValue in project cloudstack by apache.

the class ClusterMO method findDatastore.

@Override
public ManagedObjectReference findDatastore(String poolUuid) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - findDatastore(). target MOR: " + _mor.getValue() + ", poolUuid: " + poolUuid);
    CustomFieldsManagerMO cfmMo = new CustomFieldsManagerMO(_context, _context.getServiceContent().getCustomFieldsManager());
    int key = cfmMo.getCustomFieldKey("Datastore", CustomFieldConstants.CLOUD_UUID);
    assert (key != 0);
    ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] { "name", String.format("value[%d]", key) });
    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            if (oc.getPropSet().get(0).getVal().equals(poolUuid))
                return oc.getObj();
            if (oc.getPropSet().size() > 1) {
                DynamicProperty prop = oc.getPropSet().get(1);
                if (prop != null && prop.getVal() != null) {
                    if (prop.getVal() instanceof CustomFieldStringValue) {
                        String val = ((CustomFieldStringValue) prop.getVal()).getValue();
                        if (val.equalsIgnoreCase(poolUuid)) {
                            if (s_logger.isTraceEnabled())
                                s_logger.trace("vCenter API trace - findDatastore() done(successfully)");
                            return oc.getObj();
                        }
                    }
                }
            }
        }
    }
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - findDatastore() done(failed)");
    return null;
}
Also used : ObjectContent(com.vmware.vim25.ObjectContent) DynamicProperty(com.vmware.vim25.DynamicProperty) CustomFieldStringValue(com.vmware.vim25.CustomFieldStringValue)

Aggregations

DynamicProperty (com.vmware.vim25.DynamicProperty)18 ObjectContent (com.vmware.vim25.ObjectContent)18 CustomFieldStringValue (com.vmware.vim25.CustomFieldStringValue)13 ArrayList (java.util.ArrayList)5 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)4 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)4 HashMap (java.util.HashMap)4 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)2 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)2 ObjectSpec (com.vmware.vim25.ObjectSpec)2 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)2 PropertySpec (com.vmware.vim25.PropertySpec)2 TraversalSpec (com.vmware.vim25.TraversalSpec)2 VirtualMachinePowerState (com.vmware.vim25.VirtualMachinePowerState)2 RemoteException (java.rmi.RemoteException)2 HostVmStateReportEntry (com.cloud.agent.api.HostVmStateReportEntry)1 VmStatsEntry (com.cloud.agent.api.VmStatsEntry)1 CloudException (com.cloud.exception.CloudException)1 InternalErrorException (com.cloud.exception.InternalErrorException)1 VmwareManager (com.cloud.hypervisor.vmware.manager.VmwareManager)1