Search in sources :

Example 56 with ObjectContent

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

the class ClusterMO method getVmVncPortsOnCluster.

public HashMap<String, Integer> getVmVncPortsOnCluster() throws Exception {
    ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] { "name", "config.extraConfig[\"RemoteDisplay.vnc.port\"]" });
    HashMap<String, Integer> portInfo = new HashMap<String, Integer>();
    if (ocs != null && ocs.length > 0) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> objProps = oc.getPropSet();
            if (objProps != null) {
                String name = null;
                String value = null;
                for (DynamicProperty objProp : objProps) {
                    if (objProp.getName().equals("name")) {
                        name = (String) objProp.getVal();
                    } else {
                        OptionValue optValue = (OptionValue) objProp.getVal();
                        value = (String) optValue.getValue();
                    }
                }
                if (name != null && value != null) {
                    portInfo.put(name, Integer.parseInt(value));
                }
            }
        }
    }
    return portInfo;
}
Also used : ObjectContent(com.vmware.vim25.ObjectContent) DynamicProperty(com.vmware.vim25.DynamicProperty) HashMap(java.util.HashMap) OptionValue(com.vmware.vim25.OptionValue)

Example 57 with ObjectContent

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

the class VirtualMachineMO method getNetworks.

public String[] getNetworks() throws Exception {
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("Network");
    pSpec.getPathSet().add("name");
    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);
    List<String> networks = new ArrayList<String>();
    if (ocs != null && ocs.size() > 0) {
        for (ObjectContent oc : ocs) {
            networks.add(oc.getPropSet().get(0).getVal().toString());
        }
    }
    return networks.toArray(new String[0]);
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) TraversalSpec(com.vmware.vim25.TraversalSpec) ArrayList(java.util.ArrayList)

Example 58 with ObjectContent

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

the class VmwareResource method getTargetHyperHost.

private VmwareHypervisorHost getTargetHyperHost(DatacenterMO dcMo, String destIp) throws Exception {
    VmwareManager mgr = dcMo.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
    List<ObjectContent> ocs = dcMo.getHostPropertiesOnDatacenterHostFolder(new String[] { "name", "parent" });
    if (ocs != null && ocs.size() > 0) {
        for (ObjectContent oc : ocs) {
            HostMO hostMo = new HostMO(dcMo.getContext(), oc.getObj());
            VmwareHypervisorHostNetworkSummary netSummary = hostMo.getHyperHostNetworkSummary(mgr.getManagementPortGroupByHost(hostMo));
            if (destIp.equalsIgnoreCase(netSummary.getHostIp())) {
                return new HostMO(dcMo.getContext(), oc.getObj());
            }
        }
    }
    throw new Exception("Unable to locate dest host by " + destIp);
}
Also used : ObjectContent(com.vmware.vim25.ObjectContent) VmwareManager(com.cloud.hypervisor.vmware.manager.VmwareManager) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VmwareHypervisorHostNetworkSummary(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHostNetworkSummary) 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)

Example 59 with ObjectContent

use of com.vmware.vim25.ObjectContent 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;
    List<PerfCounterInfo> cInfo = getServiceContext().getVimClient().getDynamicProperty(perfMgr, "perfCounter");
    for (PerfCounterInfo info : cInfo) {
        if ("net".equalsIgnoreCase(info.getGroupInfo().getKey())) {
            if ("transmitted".equalsIgnoreCase(info.getNameInfo().getKey())) {
                txPerfCounterInfo = info;
            }
            if ("received".equalsIgnoreCase(info.getNameInfo().getKey())) {
                rxPerfCounterInfo = 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(guestMemusage)) {
                        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;
                new VirtualMachineMO(hyperHost.getContext(), oc.getObj());
                if (vmInternalCSName != null) {
                    name = vmInternalCSName;
                } else {
                    name = vmNameOnVcenter;
                }
                if (!vmNames.contains(name)) {
                    continue;
                }
                ManagedObjectReference vmMor = hyperHost.findVmOnHyperHost(name).getMor();
                assert (vmMor != null);
                ArrayList<PerfMetricId> vmNetworkMetrics = new ArrayList<PerfMetricId>();
                // get all the metrics from the available sample period
                List<PerfMetricId> perfMetrics = service.queryAvailablePerfMetric(perfMgr, vmMor, null, null, null);
                if (perfMetrics != null) {
                    for (int index = 0; index < perfMetrics.size(); ++index) {
                        if (((rxPerfCounterInfo != null) && (perfMetrics.get(index).getCounterId() == rxPerfCounterInfo.getKey())) || ((txPerfCounterInfo != null) && (perfMetrics.get(index).getCounterId() == txPerfCounterInfo.getKey()))) {
                            vmNetworkMetrics.add(perfMetrics.get(index));
                        }
                    }
                }
                double networkReadKBs = 0;
                double networkWriteKBs = 0;
                long sampleDuration = 0;
                if (vmNetworkMetrics.size() != 0) {
                    PerfQuerySpec qSpec = new PerfQuerySpec();
                    qSpec.setEntity(vmMor);
                    PerfMetricId[] availableMetricIds = vmNetworkMetrics.toArray(new PerfMetricId[0]);
                    qSpec.getMetricId().addAll(Arrays.asList(availableMetricIds));
                    List<PerfQuerySpec> qSpecs = new ArrayList<PerfQuerySpec>();
                    qSpecs.add(qSpec);
                    List<PerfEntityMetricBase> values = service.queryPerf(perfMgr, qSpecs);
                    for (int i = 0; i < values.size(); ++i) {
                        List<PerfSampleInfo> infos = ((PerfEntityMetric) values.get(i)).getSampleInfo();
                        if (infos != null && infos.size() > 0) {
                            int endMs = infos.get(infos.size() - 1).getTimestamp().getSecond() * 1000 + infos.get(infos.size() - 1).getTimestamp().getMillisecond();
                            int beginMs = infos.get(0).getTimestamp().getSecond() * 1000 + infos.get(0).getTimestamp().getMillisecond();
                            sampleDuration = (endMs - beginMs) / 1000;
                            List<PerfMetricSeries> vals = ((PerfEntityMetric) values.get(i)).getValue();
                            for (int vi = 0; ((vals != null) && (vi < vals.size())); ++vi) {
                                if (vals.get(vi) instanceof PerfMetricIntSeries) {
                                    PerfMetricIntSeries val = (PerfMetricIntSeries) vals.get(vi);
                                    List<Long> perfValues = val.getValue();
                                    Long sumRate = 0L;
                                    for (int j = 0; j < infos.size(); j++) {
                                        // Size of the array matches the size as the PerfSampleInfo
                                        sumRate += perfValues.get(j);
                                    }
                                    Long averageRate = sumRate / infos.size();
                                    if (vals.get(vi).getId().getCounterId() == rxPerfCounterInfo.getKey()) {
                                        //get the average RX rate multiplied by sampled duration
                                        networkReadKBs = sampleDuration * averageRate;
                                    }
                                    if (vals.get(vi).getId().getCounterId() == txPerfCounterInfo.getKey()) {
                                        //get the average TX rate multiplied by sampled duration
                                        networkWriteKBs = sampleDuration * averageRate;
                                    }
                                }
                            }
                        }
                    }
                }
                vmResponseMap.put(name, new VmStatsEntry(NumberUtils.toDouble(memkb) * 1024, NumberUtils.toDouble(guestMemusage) * 1024, NumberUtils.toDouble(memlimit) * 1024, maxCpuUsage, networkReadKBs, networkWriteKBs, NumberUtils.toInt(numberCPUs), "vm"));
            }
        }
    }
    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) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) VimPortType(com.vmware.vim25.VimPortType) PerfSampleInfo(com.vmware.vim25.PerfSampleInfo) PerfQuerySpec(com.vmware.vim25.PerfQuerySpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) PerfEntityMetric(com.vmware.vim25.PerfEntityMetric)

Example 60 with ObjectContent

use of com.vmware.vim25.ObjectContent 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.powerOff();
                                vmMo.detachAllDisks();
                                vmMo.destroy();
                            }
                        }
                    }
                }
            }
        } 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)

Aggregations

ObjectContent (com.vmware.vim25.ObjectContent)65 ObjectSpec (com.vmware.vim25.ObjectSpec)44 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)44 PropertySpec (com.vmware.vim25.PropertySpec)44 TraversalSpec (com.vmware.vim25.TraversalSpec)38 ArrayList (java.util.ArrayList)38 DynamicProperty (com.vmware.vim25.DynamicProperty)34 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)15 Pair (com.cloud.utils.Pair)12 Gson (com.google.gson.Gson)10 CustomFieldStringValue (com.vmware.vim25.CustomFieldStringValue)9 SelectionSpec (com.vmware.vim25.SelectionSpec)9 HashMap (java.util.HashMap)7 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)5 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)4 OptionValue (com.vmware.vim25.OptionValue)4 VmwareManager (com.cloud.hypervisor.vmware.manager.VmwareManager)2 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)2 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)2 DVPortgroupConfigInfo (com.vmware.vim25.DVPortgroupConfigInfo)2