Search in sources :

Example 11 with VimPortType

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

the class VmwareResource method execute.

protected Answer execute(GetVmDiskStatsCommand cmd) {
    try {
        final VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
        final ManagedObjectReference perfMgr = getServiceContext().getServiceContent().getPerfManager();
        VimPortType service = getServiceContext().getService();
        Integer windowInterval = getVmwareWindowTimeInterval();
        final XMLGregorianCalendar startTime = VmwareHelper.getXMLGregorianCalendar(new Date(), windowInterval);
        final XMLGregorianCalendar endTime = VmwareHelper.getXMLGregorianCalendar(new Date(), 0);
        PerfCounterInfo diskReadIOPerfCounterInfo = null;
        PerfCounterInfo diskWriteIOPerfCounterInfo = null;
        PerfCounterInfo diskReadKbsPerfCounterInfo = null;
        PerfCounterInfo diskWriteKbsPerfCounterInfo = null;
        // https://pubs.vmware.com/vsphere-5-5/topic/com.vmware.wssdk.apiref.doc/virtual_disk_counters.html
        List<PerfCounterInfo> cInfo = getServiceContext().getVimClient().getDynamicProperty(perfMgr, "perfCounter");
        for (PerfCounterInfo info : cInfo) {
            if ("virtualdisk".equalsIgnoreCase(info.getGroupInfo().getKey()) && "average".equalsIgnoreCase(info.getRollupType().value())) {
                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;
                }
            }
        }
        final ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
        final DatacenterMO dcMo = new DatacenterMO(getServiceContext(), dcMor);
        final HashMap<String, List<VmDiskStatsEntry>> vmStatsMap = new HashMap<>();
        for (final String vmName : cmd.getVmNames()) {
            final VirtualMachineMO vmMo = dcMo.findVm(vmName);
            final List<VmDiskStatsEntry> diskStats = new ArrayList<>();
            for (final VirtualDisk disk : vmMo.getAllDiskDevice()) {
                final String diskBusName = vmMo.getDeviceBusName(vmMo.getAllDeviceList(), disk);
                long readReq = 0;
                long readBytes = 0;
                long writeReq = 0;
                long writeBytes = 0;
                final ArrayList<PerfMetricId> perfMetricsIds = new ArrayList<PerfMetricId>();
                if (diskReadIOPerfCounterInfo != null) {
                    perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskReadIOPerfCounterInfo, diskBusName));
                }
                if (diskWriteIOPerfCounterInfo != null) {
                    perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskWriteIOPerfCounterInfo, diskBusName));
                }
                if (diskReadKbsPerfCounterInfo != null) {
                    perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskReadKbsPerfCounterInfo, diskBusName));
                }
                if (diskWriteKbsPerfCounterInfo != null) {
                    perfMetricsIds.add(VmwareHelper.createPerfMetricId(diskWriteKbsPerfCounterInfo, diskBusName));
                }
                if (perfMetricsIds.size() > 0) {
                    try {
                        final PerfQuerySpec qSpec = new PerfQuerySpec();
                        qSpec.setEntity(vmMo.getMor());
                        qSpec.setFormat("normal");
                        qSpec.setIntervalId(windowInterval);
                        qSpec.setStartTime(startTime);
                        qSpec.setEndTime(endTime);
                        qSpec.getMetricId().addAll(perfMetricsIds);
                        for (final PerfEntityMetricBase perfValue : service.queryPerf(perfMgr, Collections.singletonList(qSpec))) {
                            if (!(perfValue instanceof PerfEntityMetric)) {
                                continue;
                            }
                            final List<PerfMetricSeries> values = ((PerfEntityMetric) perfValue).getValue();
                            if (values == null || values.isEmpty()) {
                                continue;
                            }
                            for (final PerfMetricSeries value : values) {
                                if (!(value instanceof PerfMetricIntSeries) || !value.getId().getInstance().equals(diskBusName)) {
                                    continue;
                                }
                                final List<Long> perfStats = ((PerfMetricIntSeries) value).getValue();
                                if (perfStats.size() > 0) {
                                    long sum = 0;
                                    for (long val : perfStats) {
                                        sum += val;
                                    }
                                    long avg = sum / perfStats.size();
                                    if (value.getId().getCounterId() == diskReadIOPerfCounterInfo.getKey()) {
                                        readReq = avg;
                                    } else if (value.getId().getCounterId() == diskWriteIOPerfCounterInfo.getKey()) {
                                        writeReq = avg;
                                    } else if (value.getId().getCounterId() == diskReadKbsPerfCounterInfo.getKey()) {
                                        readBytes = avg * 1024;
                                    } else if (value.getId().getCounterId() == diskWriteKbsPerfCounterInfo.getKey()) {
                                        writeBytes = avg * 1024;
                                    }
                                }
                            }
                        }
                    } 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);
                    }
                }
                diskStats.add(new VmDiskStatsEntry(vmName, VmwareHelper.getDiskDeviceFileName(disk), writeReq, readReq, writeBytes, readBytes));
            }
            if (CollectionUtils.isNotEmpty(diskStats)) {
                vmStatsMap.put(vmName, diskStats);
            }
        }
        s_logger.debug(String.format("VM Disks Maps is: [%s].", _gson.toJson(vmStatsMap)));
        if (MapUtils.isNotEmpty(vmStatsMap)) {
            return new GetVmDiskStatsAnswer(cmd, "", cmd.getHostName(), vmStatsMap);
        }
    } catch (Exception e) {
        s_logger.error(String.format("Unable to execute GetVmDiskStatsCommand due to [%s].", VmwareHelper.getExceptionMessage(e)), e);
    }
    return new GetVmDiskStatsAnswer(cmd, null, null, null);
}
Also used : PerfCounterInfo(com.vmware.vim25.PerfCounterInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PerfEntityMetricBase(com.vmware.vim25.PerfEntityMetricBase) PerfMetricIntSeries(com.vmware.vim25.PerfMetricIntSeries) ArrayList(java.util.ArrayList) List(java.util.List) PerfMetricSeries(com.vmware.vim25.PerfMetricSeries) PerfMetricId(com.vmware.vim25.PerfMetricId) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) VmDiskStatsEntry(com.cloud.agent.api.VmDiskStatsEntry) VimPortType(com.vmware.vim25.VimPortType) Date(java.util.Date) VirtualDisk(com.vmware.vim25.VirtualDisk) 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) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) PerfQuerySpec(com.vmware.vim25.PerfQuerySpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) PerfEntityMetric(com.vmware.vim25.PerfEntityMetric)

Example 12 with VimPortType

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

the class VMwareUtil method getVMwareConnection.

public static VMwareConnection getVMwareConnection(LoginInfo loginInfo) throws Exception {
    trustAllHttpsCertificates();
    HostnameVerifier hv = new HostnameVerifier() {

        @Override
        public boolean verify(String urlHostName, SSLSession session) {
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
    ManagedObjectReference serviceInstanceRef = new ManagedObjectReference();
    final String serviceInstanceName = "ServiceInstance";
    serviceInstanceRef.setType(serviceInstanceName);
    serviceInstanceRef.setValue(serviceInstanceName);
    VimService vimService = new VimService();
    VimPortType vimPortType = vimService.getVimPort();
    Map<String, Object> ctxt = ((BindingProvider) vimPortType).getRequestContext();
    ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://" + loginInfo.getHost() + "/sdk");
    ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
    ServiceContent serviceContent = vimPortType.retrieveServiceContent(serviceInstanceRef);
    vimPortType.login(serviceContent.getSessionManager(), loginInfo.getUsername(), loginInfo.getPassword(), null);
    return new VMwareConnection(vimPortType, serviceContent);
}
Also used : VimService(com.vmware.vim25.VimService) SSLSession(javax.net.ssl.SSLSession) ServiceContent(com.vmware.vim25.ServiceContent) BindingProvider(javax.xml.ws.BindingProvider) VimPortType(com.vmware.vim25.VimPortType) HostnameVerifier(javax.net.ssl.HostnameVerifier) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 13 with VimPortType

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

the class VMwareUtil method getVms.

public static Map<String, ManagedObjectReference> getVms(VMwareConnection connection) throws Exception {
    Map<String, ManagedObjectReference> nameToVm = new HashMap<>();
    ManagedObjectReference rootFolder = connection.getServiceContent().getRootFolder();
    TraversalSpec tSpec = getVMTraversalSpec();
    PropertySpec propertySpec = new PropertySpec();
    propertySpec.setAll(Boolean.FALSE);
    propertySpec.getPathSet().add("name");
    propertySpec.setType("VirtualMachine");
    ObjectSpec objectSpec = new ObjectSpec();
    objectSpec.setObj(rootFolder);
    objectSpec.setSkip(Boolean.TRUE);
    objectSpec.getSelectSet().add(tSpec);
    PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
    propertyFilterSpec.getPropSet().add(propertySpec);
    propertyFilterSpec.getObjectSet().add(objectSpec);
    List<PropertyFilterSpec> lstPfs = new ArrayList<>(1);
    lstPfs.add(propertyFilterSpec);
    VimPortType vimPortType = connection.getVimPortType();
    ManagedObjectReference propertyCollector = connection.getServiceContent().getPropertyCollector();
    List<ObjectContent> lstObjectContent = retrievePropertiesAllObjects(lstPfs, vimPortType, propertyCollector);
    if (lstObjectContent != null) {
        for (ObjectContent oc : lstObjectContent) {
            ManagedObjectReference mor = oc.getObj();
            List<DynamicProperty> dps = oc.getPropSet();
            String vmName = null;
            if (dps != null) {
                for (DynamicProperty dp : dps) {
                    vmName = (String) dp.getVal();
                }
            }
            if (vmName != null) {
                nameToVm.put(vmName, mor);
            }
        }
    }
    return nameToVm;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) DynamicProperty(com.vmware.vim25.DynamicProperty) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VimPortType(com.vmware.vim25.VimPortType) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) TraversalSpec(com.vmware.vim25.TraversalSpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)10 VimPortType (com.vmware.vim25.VimPortType)9 ArrayList (java.util.ArrayList)5 TaskInfo (com.vmware.vim25.TaskInfo)4 HashMap (java.util.HashMap)3 CloudException (com.cloud.exception.CloudException)2 InternalErrorException (com.cloud.exception.InternalErrorException)2 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 DynamicProperty (com.vmware.vim25.DynamicProperty)2 ObjectContent (com.vmware.vim25.ObjectContent)2 PerfCounterInfo (com.vmware.vim25.PerfCounterInfo)2 PerfEntityMetric (com.vmware.vim25.PerfEntityMetric)2 PerfEntityMetricBase (com.vmware.vim25.PerfEntityMetricBase)2 PerfMetricId (com.vmware.vim25.PerfMetricId)2 PerfMetricIntSeries (com.vmware.vim25.PerfMetricIntSeries)2 PerfMetricSeries (com.vmware.vim25.PerfMetricSeries)2 PerfQuerySpec (com.vmware.vim25.PerfQuerySpec)2 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)2 ServiceContent (com.vmware.vim25.ServiceContent)2