Search in sources :

Example 1 with HostResponse

use of org.apache.cloudstack.api.response.HostResponse in project cloudstack by apache.

the class MetricsServiceImpl method listHostMetrics.

@Override
public List<HostMetricsResponse> listHostMetrics(List<HostResponse> hostResponses) {
    final List<HostMetricsResponse> metricsResponses = new ArrayList<>();
    for (final HostResponse hostResponse : hostResponses) {
        HostMetricsResponse metricsResponse = new HostMetricsResponse();
        try {
            BeanUtils.copyProperties(metricsResponse, hostResponse);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate host metrics response");
        }
        final Host host = hostDao.findByUuid(hostResponse.getId());
        if (host == null) {
            continue;
        }
        final Long hostId = host.getId();
        final Long clusterId = host.getClusterId();
        // Thresholds
        final Double cpuThreshold = AlertManager.CPUCapacityThreshold.valueIn(clusterId);
        final Double memoryThreshold = AlertManager.MemoryCapacityThreshold.valueIn(clusterId);
        final Float cpuDisableThreshold = DeploymentClusterPlanner.ClusterCPUCapacityDisableThreshold.valueIn(clusterId);
        final Float memoryDisableThreshold = DeploymentClusterPlanner.ClusterMemoryCapacityDisableThreshold.valueIn(clusterId);
        // Over commit ratios
        final Double cpuOvercommitRatio = findRatioValue(ApiDBUtils.findClusterDetails(clusterId, "cpuOvercommitRatio"));
        final Double memoryOvercommitRatio = findRatioValue(ApiDBUtils.findClusterDetails(clusterId, "memoryOvercommitRatio"));
        Long upInstances = 0L;
        Long totalInstances = 0L;
        for (final VMInstanceVO instance : vmInstanceDao.listByHostId(hostId)) {
            if (instance == null) {
                continue;
            }
            if (instance.getType() == VirtualMachine.Type.User) {
                totalInstances++;
                if (instance.getState() == VirtualMachine.State.Running) {
                    upInstances++;
                }
            }
        }
        metricsResponse.setPowerState(hostResponse.getOutOfBandManagementResponse().getPowerState());
        metricsResponse.setInstances(upInstances, totalInstances);
        metricsResponse.setCpuTotal(hostResponse.getCpuNumber(), hostResponse.getCpuSpeed(), cpuOvercommitRatio);
        metricsResponse.setCpuUsed(hostResponse.getCpuUsed(), hostResponse.getCpuNumber(), hostResponse.getCpuSpeed());
        metricsResponse.setCpuAllocated(hostResponse.getCpuAllocated(), hostResponse.getCpuNumber(), hostResponse.getCpuSpeed());
        metricsResponse.setMemTotal(hostResponse.getMemoryTotal(), memoryOvercommitRatio);
        metricsResponse.setMemAllocated(hostResponse.getMemoryAllocated());
        metricsResponse.setMemUsed(hostResponse.getMemoryUsed());
        metricsResponse.setNetworkRead(hostResponse.getNetworkKbsRead());
        metricsResponse.setNetworkWrite(hostResponse.getNetworkKbsWrite());
        // CPU thresholds
        metricsResponse.setCpuUsageThreshold(hostResponse.getCpuUsed(), cpuThreshold);
        metricsResponse.setCpuUsageDisableThreshold(hostResponse.getCpuUsed(), cpuDisableThreshold);
        metricsResponse.setCpuAllocatedThreshold(hostResponse.getCpuAllocated(), cpuOvercommitRatio, cpuThreshold);
        metricsResponse.setCpuAllocatedDisableThreshold(hostResponse.getCpuAllocated(), cpuOvercommitRatio, cpuDisableThreshold);
        // Memory thresholds
        metricsResponse.setMemoryUsageThreshold(hostResponse.getMemoryUsed(), hostResponse.getMemoryTotal(), memoryThreshold);
        metricsResponse.setMemoryUsageDisableThreshold(hostResponse.getMemoryUsed(), hostResponse.getMemoryTotal(), memoryDisableThreshold);
        metricsResponse.setMemoryAllocatedThreshold(hostResponse.getMemoryAllocated(), hostResponse.getMemoryTotal(), memoryOvercommitRatio, memoryThreshold);
        metricsResponse.setMemoryAllocatedDisableThreshold(hostResponse.getMemoryAllocated(), hostResponse.getMemoryTotal(), memoryOvercommitRatio, memoryDisableThreshold);
        metricsResponses.add(metricsResponse);
    }
    return metricsResponses;
}
Also used : ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) Host(com.cloud.host.Host) HostMetricsResponse(org.apache.cloudstack.response.HostMetricsResponse) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServerApiException(org.apache.cloudstack.api.ServerApiException) HostResponse(org.apache.cloudstack.api.response.HostResponse)

Example 2 with HostResponse

use of org.apache.cloudstack.api.response.HostResponse in project cloudstack by apache.

the class AddHostCmd method execute.

@Override
public void execute() {
    try {
        List<? extends Host> result = _resourceService.discoverHosts(this);
        ListResponse<HostResponse> response = new ListResponse<HostResponse>();
        List<HostResponse> hostResponses = new ArrayList<HostResponse>();
        if (result != null && result.size() > 0) {
            for (Host host : result) {
                HostResponse hostResponse = _responseGenerator.createHostResponse(host);
                hostResponses.add(hostResponse);
            }
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add host");
        }
        response.setResponses(hostResponses);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } catch (DiscoveryException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    }
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) HostResponse(org.apache.cloudstack.api.response.HostResponse) ArrayList(java.util.ArrayList) Host(com.cloud.host.Host) DiscoveryException(com.cloud.exception.DiscoveryException)

Example 3 with HostResponse

use of org.apache.cloudstack.api.response.HostResponse in project cloudstack by apache.

the class ListHostsCmd method getHostResponses.

protected ListResponse<HostResponse> getHostResponses() {
    ListResponse<HostResponse> response = new ListResponse<>();
    if (getVirtualMachineId() == null) {
        response = _queryService.searchForServers(this);
    } else {
        Pair<List<? extends Host>, Integer> result;
        Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Host, Boolean>> hostsForMigration = _mgr.listHostsForMigrationOfVM(getVirtualMachineId(), this.getStartIndex(), this.getPageSizeVal(), null);
        result = hostsForMigration.first();
        List<? extends Host> hostsWithCapacity = hostsForMigration.second();
        List<HostResponse> hostResponses = new ArrayList<HostResponse>();
        for (Host host : result.first()) {
            HostResponse hostResponse = _responseGenerator.createHostResponse(host, getDetails());
            Boolean suitableForMigration = false;
            if (hostsWithCapacity.contains(host)) {
                suitableForMigration = true;
            }
            hostResponse.setSuitableForMigration(suitableForMigration);
            hostResponse.setObjectName("host");
            hostResponses.add(hostResponse);
        }
        response.setResponses(hostResponses, result.second());
    }
    return response;
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) ArrayList(java.util.ArrayList) Host(com.cloud.host.Host) HostResponse(org.apache.cloudstack.api.response.HostResponse) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Pair(com.cloud.utils.Pair)

Example 4 with HostResponse

use of org.apache.cloudstack.api.response.HostResponse in project cloudstack by apache.

the class ReconnectHostCmd method execute.

@Override
public void execute() {
    try {
        Host result = _resourceService.reconnectHost(this);
        if (result != null) {
            HostResponse response = _responseGenerator.createHostResponse(result);
            response.setResponseName(getCommandName());
            this.setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to reconnect host");
        }
    } catch (Exception ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    }
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) HostResponse(org.apache.cloudstack.api.response.HostResponse) Host(com.cloud.host.Host) ServerApiException(org.apache.cloudstack.api.ServerApiException)

Example 5 with HostResponse

use of org.apache.cloudstack.api.response.HostResponse in project cloudstack by apache.

the class HostJoinDaoImpl method newHostResponse.

@Override
public HostResponse newHostResponse(HostJoinVO host, EnumSet<HostDetails> details) {
    HostResponse hostResponse = new HostResponse();
    hostResponse.setId(host.getUuid());
    hostResponse.setCapabilities(host.getCapabilities());
    hostResponse.setClusterId(host.getClusterUuid());
    hostResponse.setCpuSockets(host.getCpuSockets());
    hostResponse.setCpuNumber(host.getCpus());
    hostResponse.setZoneId(host.getZoneUuid());
    hostResponse.setDisconnectedOn(host.getDisconnectedOn());
    hostResponse.setHypervisor(host.getHypervisorType());
    hostResponse.setHostType(host.getType());
    hostResponse.setLastPinged(new Date(host.getLastPinged()));
    hostResponse.setManagementServerId(host.getManagementServerId());
    hostResponse.setName(host.getName());
    hostResponse.setPodId(host.getPodUuid());
    hostResponse.setRemoved(host.getRemoved());
    hostResponse.setCpuSpeed(host.getSpeed());
    hostResponse.setState(host.getStatus());
    hostResponse.setIpAddress(host.getPrivateIpAddress());
    hostResponse.setVersion(host.getVersion());
    hostResponse.setCreated(host.getCreated());
    List<HostGpuGroupsVO> gpuGroups = ApiDBUtils.getGpuGroups(host.getId());
    if (gpuGroups != null && !gpuGroups.isEmpty()) {
        List<GpuResponse> gpus = new ArrayList<GpuResponse>();
        for (HostGpuGroupsVO entry : gpuGroups) {
            GpuResponse gpuResponse = new GpuResponse();
            gpuResponse.setGpuGroupName(entry.getGroupName());
            List<VGPUTypesVO> vgpuTypes = ApiDBUtils.getVgpus(entry.getId());
            if (vgpuTypes != null && !vgpuTypes.isEmpty()) {
                List<VgpuResponse> vgpus = new ArrayList<VgpuResponse>();
                for (VGPUTypesVO vgpuType : vgpuTypes) {
                    VgpuResponse vgpuResponse = new VgpuResponse();
                    vgpuResponse.setName(vgpuType.getVgpuType());
                    vgpuResponse.setVideoRam(vgpuType.getVideoRam());
                    vgpuResponse.setMaxHeads(vgpuType.getMaxHeads());
                    vgpuResponse.setMaxResolutionX(vgpuType.getMaxResolutionX());
                    vgpuResponse.setMaxResolutionY(vgpuType.getMaxResolutionY());
                    vgpuResponse.setMaxVgpuPerPgpu(vgpuType.getMaxVgpuPerPgpu());
                    vgpuResponse.setRemainingCapacity(vgpuType.getRemainingCapacity());
                    vgpuResponse.setmaxCapacity(vgpuType.getMaxCapacity());
                    vgpus.add(vgpuResponse);
                }
                gpuResponse.setVgpu(vgpus);
            }
            gpus.add(gpuResponse);
        }
        hostResponse.setGpuGroups(gpus);
    }
    if (details.contains(HostDetails.all) || details.contains(HostDetails.capacity) || details.contains(HostDetails.stats) || details.contains(HostDetails.events)) {
        hostResponse.setOsCategoryId(host.getOsCategoryUuid());
        hostResponse.setOsCategoryName(host.getOsCategoryName());
        hostResponse.setZoneName(host.getZoneName());
        hostResponse.setPodName(host.getPodName());
        if (host.getClusterId() > 0) {
            hostResponse.setClusterName(host.getClusterName());
            hostResponse.setClusterType(host.getClusterType().toString());
        }
    }
    DecimalFormat decimalFormat = new DecimalFormat("#.##");
    if (host.getType() == Host.Type.Routing) {
        if (details.contains(HostDetails.all) || details.contains(HostDetails.capacity)) {
            // set allocated capacities
            Long mem = host.getMemReservedCapacity() + host.getMemUsedCapacity();
            Long cpu = host.getCpuReservedCapacity() + host.getCpuUsedCapacity();
            hostResponse.setMemoryTotal(mem);
            Float totalMemorywithOverprovisioning = new Float((host.getTotalMemory() * ApiDBUtils.getMemOverprovisioningFactor(host.getClusterId())));
            hostResponse.setMemWithOverprovisioning(totalMemorywithOverprovisioning.toString());
            hostResponse.setMemoryAllocated(mem);
            String hostTags = host.getTag();
            hostResponse.setHostTags(host.getTag());
            String haTag = ApiDBUtils.getHaTag();
            if (haTag != null && !haTag.isEmpty() && hostTags != null && !hostTags.isEmpty()) {
                if (haTag.equalsIgnoreCase(hostTags)) {
                    hostResponse.setHaHost(true);
                } else {
                    hostResponse.setHaHost(false);
                }
            } else {
                hostResponse.setHaHost(false);
            }
            hostResponse.setHypervisorVersion(host.getHypervisorVersion());
            Float cpuWithOverprovisioning = new Float(host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId()));
            String cpuAlloc = decimalFormat.format(((float) cpu / cpuWithOverprovisioning * 100f)) + "%";
            hostResponse.setCpuAllocated(cpuAlloc);
            hostResponse.setCpuWithOverprovisioning(cpuWithOverprovisioning.toString());
        }
        if (details.contains(HostDetails.all) || details.contains(HostDetails.stats)) {
            // set CPU/RAM/Network stats
            String cpuUsed = null;
            HostStats hostStats = ApiDBUtils.getHostStatistics(host.getId());
            if (hostStats != null) {
                float cpuUtil = (float) hostStats.getCpuUtilization();
                cpuUsed = decimalFormat.format(cpuUtil) + "%";
                hostResponse.setCpuUsed(cpuUsed);
                hostResponse.setMemoryUsed((new Double(hostStats.getUsedMemory())).longValue());
                hostResponse.setNetworkKbsRead((new Double(hostStats.getNetworkReadKBs())).longValue());
                hostResponse.setNetworkKbsWrite((new Double(hostStats.getNetworkWriteKBs())).longValue());
            }
        }
        if (details.contains(HostDetails.all) && host.getHypervisorType() == Hypervisor.HypervisorType.KVM) {
            //only kvm has the requirement to return host details
            try {
                hostResponse.setDetails(hostDetailsDao.findDetails(host.getId()));
            } catch (Exception e) {
                s_logger.debug("failed to get host details", e);
            }
        }
    } else if (host.getType() == Host.Type.SecondaryStorage) {
        StorageStats secStorageStats = ApiDBUtils.getSecondaryStorageStatistics(host.getId());
        if (secStorageStats != null) {
            hostResponse.setDiskSizeTotal(secStorageStats.getCapacityBytes());
            hostResponse.setDiskSizeAllocated(secStorageStats.getByteUsed());
        }
    }
    hostResponse.setLocalStorageActive(ApiDBUtils.isLocalStorageActiveOnHost(host.getId()));
    if (details.contains(HostDetails.all) || details.contains(HostDetails.events)) {
        Set<com.cloud.host.Status.Event> possibleEvents = host.getStatus().getPossibleEvents();
        if ((possibleEvents != null) && !possibleEvents.isEmpty()) {
            String events = "";
            Iterator<com.cloud.host.Status.Event> iter = possibleEvents.iterator();
            while (iter.hasNext()) {
                com.cloud.host.Status.Event event = iter.next();
                events += event.toString();
                if (iter.hasNext()) {
                    events += "; ";
                }
            }
            hostResponse.setEvents(events);
        }
    }
    hostResponse.setOutOfBandManagementResponse(outOfBandManagementDao.findByHost(host.getId()));
    hostResponse.setResourceState(host.getResourceState().toString());
    // set async job
    if (host.getJobId() != null) {
        hostResponse.setJobId(host.getJobUuid());
        hostResponse.setJobStatus(host.getJobStatus());
    }
    hostResponse.setObjectName("host");
    return hostResponse;
}
Also used : DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) GpuResponse(org.apache.cloudstack.api.response.GpuResponse) HostResponse(org.apache.cloudstack.api.response.HostResponse) VgpuResponse(org.apache.cloudstack.api.response.VgpuResponse) VGPUTypesVO(com.cloud.gpu.VGPUTypesVO) StorageStats(com.cloud.storage.StorageStats) HostGpuGroupsVO(com.cloud.gpu.HostGpuGroupsVO) Date(java.util.Date) HostStats(com.cloud.host.HostStats)

Aggregations

HostResponse (org.apache.cloudstack.api.response.HostResponse)10 Host (com.cloud.host.Host)8 ServerApiException (org.apache.cloudstack.api.ServerApiException)6 ArrayList (java.util.ArrayList)5 ListResponse (org.apache.cloudstack.api.response.ListResponse)2 HostJoinVO (com.cloud.api.query.vo.HostJoinVO)1 DiscoveryException (com.cloud.exception.DiscoveryException)1 HostGpuGroupsVO (com.cloud.gpu.HostGpuGroupsVO)1 VGPUTypesVO (com.cloud.gpu.VGPUTypesVO)1 HostStats (com.cloud.host.HostStats)1 StorageStats (com.cloud.storage.StorageStats)1 Pair (com.cloud.utils.Pair)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 DecimalFormat (java.text.DecimalFormat)1 Date (java.util.Date)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Map (java.util.Map)1 GpuResponse (org.apache.cloudstack.api.response.GpuResponse)1