Search in sources :

Example 1 with VROConfig

use of com.vmware.flowgate.vroworker.vro.VROConfig in project flowgate by vmware.

the class VROAsyncJob method syncVROMetricPropertyAlertDefinition.

private void syncVROMetricPropertyAlertDefinition(SDDCSoftwareConfig config) {
    if (!config.checkIsActive()) {
        return;
    }
    VROConfig vroConf = new VROConfig(config);
    MetricClient metricClient = null;
    logger.info("Synce predefinedMetrics for " + config.getServerURL());
    try {
        metricClient = new MetricClient(vroConf, publisher);
        metricClient.checkPredefinedMetricsandProperties();
    } catch (AuthException e) {
        logger.error("Failed to sync the metric data from VRO ", e);
        IntegrationStatus integrationStatus = config.getIntegrationStatus();
        if (integrationStatus == null) {
            integrationStatus = new IntegrationStatus();
        }
        integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
        integrationStatus.setDetail(e.getMessage());
        integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
        updateIntegrationStatus(config);
        return;
    } catch (Exception e) {
        if (e.getCause() instanceof UndeclaredThrowableException) {
            UndeclaredThrowableException undeclaredThrowableException = (UndeclaredThrowableException) e.getCause();
            if (undeclaredThrowableException.getUndeclaredThrowable().getCause() instanceof ConnectException) {
                checkAndUpdateIntegrationStatus(config, undeclaredThrowableException.getUndeclaredThrowable().getCause().getMessage());
                return;
            }
        }
        throw e;
    }
    AlertClient alertClient = new AlertClient(vroConf);
    restClient.setServiceKey(serviceKeyConfig.getServiceKey());
    alertClient.setRestClient(restClient);
    alertClient.run();
}
Also used : IntegrationStatus(com.vmware.flowgate.common.model.IntegrationStatus) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) AuthException(com.vmware.ops.api.client.exceptions.AuthException) AlertClient(com.vmware.flowgate.vroworker.vro.AlertClient) VROConfig(com.vmware.flowgate.vroworker.vro.VROConfig) MetricClient(com.vmware.flowgate.vroworker.vro.MetricClient) AuthException(com.vmware.ops.api.client.exceptions.AuthException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) ConnectException(java.net.ConnectException)

Example 2 with VROConfig

use of com.vmware.flowgate.vroworker.vro.VROConfig in project flowgate by vmware.

the class VROAsyncJob method syncVROMetricData.

private void syncVROMetricData(SDDCSoftwareConfig config) {
    if (!config.checkIsActive()) {
        return;
    }
    VROConfig vro = new VROConfig(config);
    long currentTime = System.currentTimeMillis();
    MetricClient metricClient = null;
    List<ResourceDto> hosts = null;
    try {
        metricClient = new MetricClient(vro, publisher);
        hosts = metricClient.getHostSystemsResources();
    } catch (AuthException authException) {
        logger.error("Failed to connect to VROps manager ", authException);
        IntegrationStatus integrationStatus = config.getIntegrationStatus();
        if (integrationStatus == null) {
            integrationStatus = new IntegrationStatus();
        }
        integrationStatus.setStatus(IntegrationStatus.Status.ERROR);
        integrationStatus.setDetail(authException.getMessage());
        integrationStatus.setRetryCounter(FlowgateConstant.DEFAULTNUMBEROFRETRIES);
        updateIntegrationStatus(config);
        return;
    } catch (Exception e) {
        if (e.getCause() instanceof UndeclaredThrowableException) {
            UndeclaredThrowableException undeclaredThrowableException = (UndeclaredThrowableException) e.getCause();
            if (undeclaredThrowableException.getUndeclaredThrowable().getCause() instanceof ConnectException) {
                checkAndUpdateIntegrationStatus(config, undeclaredThrowableException.getUndeclaredThrowable().getCause().getMessage());
                return;
            }
        }
        throw e;
    }
    // read all host/asset maaping from apiserver
    Map<String, Asset> assetDictionary = new HashMap<String, Asset>();
    try {
        restClient.setServiceKey(serviceKeyConfig.getServiceKey());
        Asset[] servers = restClient.getAssetsByVRO(vro.getId()).getBody();
        for (Asset server : servers) {
            assetDictionary.put(server.getId(), server);
        }
    } catch (HttpClientErrorException clientError) {
        if (clientError.getRawStatusCode() != HTTP_NOTFOUND) {
            throw clientError;
        }
    }
    ServerMapping[] mappings = null;
    try {
        mappings = restClient.getServerMappingsByVRO(vro.getId()).getBody();
    } catch (HttpClientErrorException clientError) {
        if (clientError.getRawStatusCode() != HTTP_NOTFOUND) {
            throw clientError;
        }
        mappings = new ServerMapping[0];
    }
    HashMap<String, ServerMapping> entityNameDictionary = new HashMap<String, ServerMapping>();
    HashMap<String, ServerMapping> objectIDDictionary = new HashMap<String, ServerMapping>();
    for (ServerMapping mapping : mappings) {
        entityNameDictionary.put(mapping.getVroVMEntityName(), mapping);
        objectIDDictionary.put(mapping.getVroVMEntityObjectID(), mapping);
    // vcIDDictionary.put(mapping.getVroVMEntityVCID(), mapping);
    }
    List<ServerMapping> validMapping = new ArrayList<ServerMapping>();
    for (ResourceDto host : hosts) {
        // There are several things that can be used as a identifier of a host.
        // the first is the Name
        // beside that there are three other identifier
        // VMEntityName -- 10.112.113.160
        // VMEntityObjectID  host-81  the mobID of the host.
        // VMEntityVCID  this is the uuid of the vc object.
        // currently we only add new servers and don't delete servers
        List<ResourceIdentifier> identifiers = host.getResourceKey().getResourceIdentifiers();
        boolean newHost = true;
        String entityName = "";
        String objectID = "";
        String vcID = "";
        ServerMapping refer = null;
        for (ResourceIdentifier identifier : identifiers) {
            if (!newHost) {
                break;
            }
            switch(identifier.getIdentifierType().getName()) {
                case EntityName:
                    entityName = identifier.getValue();
                    if (entityNameDictionary.containsKey(entityName)) {
                        newHost = false;
                        refer = entityNameDictionary.get(entityName);
                    }
                    break;
                case VMEntityObjectID:
                    objectID = identifier.getValue();
                    if (objectIDDictionary.containsKey(objectID)) {
                        newHost = false;
                        refer = objectIDDictionary.get(objectID);
                    }
                    break;
                case VMEntityVCID:
                    vcID = identifier.getValue();
                    break;
                default:
                    break;
            }
        }
        if (newHost) {
            // try to notify the other system.
            String ipaddress = IPAddressUtil.getIPAddress(entityName);
            if (null != ipaddress) {
                publisher.publish(null, ipaddress);
                ipaddress = entityName;
            }
            ServerMapping newMapping = new ServerMapping();
            newMapping.setVroID(vro.getId());
            newMapping.setVroResourceName(host.getResourceKey().getName());
            newMapping.setVroVMEntityName(entityName);
            newMapping.setVroVMEntityObjectID(objectID);
            newMapping.setVroVMEntityVCID(vcID);
            newMapping.setVroResourceID(host.getIdentifier().toString());
            AssetIPMapping[] ipMappings = restClient.getHostnameIPMappingByIP(ipaddress).getBody();
            if (null != ipMappings && ipMappings.length > 0) {
                // update the mapping
                String assetName = ipMappings[0].getAssetname();
                Asset asset = restClient.getAssetByName(assetName).getBody();
                if (asset != null) {
                    newMapping.setAsset(asset.getId());
                }
            }
            restClient.saveServerMapping(newMapping);
            validMapping.add(newMapping);
        } else {
            if (refer.getAsset() == null) {
                String ipaddress = IPAddressUtil.getIPAddress(refer.getVroVMEntityName());
                AssetIPMapping[] ipMappings = restClient.getHostnameIPMappingByIP(ipaddress).getBody();
                if (null != ipMappings && ipMappings.length > 0) {
                    // update the mapping
                    String assetName = ipMappings[0].getAssetname();
                    Asset asset = restClient.getAssetByName(assetName).getBody();
                    if (asset != null) {
                        refer.setAsset(asset.getId());
                        restClient.saveServerMapping(refer);
                        validMapping.add(refer);
                    }
                } else {
                    if (null != ipaddress) {
                        logger.info("Notify Infoblox to query the assetname");
                        publisher.publish(null, ipaddress);
                    }
                }
            } else {
                validMapping.add(refer);
            }
        }
    }
    // validMapping.stream().map(ServerMapping::getAssetID).collect(Collectors.toSet());
    if (validMapping.isEmpty()) {
        logger.info("No Mapping find.Sync nothing for this execution.");
        return;
    }
    Long latencyFactor = latencyFactorMap.get(config.getServerURL());
    if (latencyFactor == null) {
        latencyFactor = 24L;
    }
    Long lastUpdateTimeStamp = lastUpdateTimeMap.get(config.getServerURL());
    if (lastUpdateTimeStamp == null) {
        lastUpdateTimeStamp = currentTime - FIVE_MINUTES * latencyFactor;
    }
    boolean hasNewData = false;
    logger.info(String.format("Start prepare data.%s, lastUpdateTime:%s, latencyFactor:%s", executionCount, lastUpdateTimeStamp, latencyFactor));
    long newUpdateTimeStamp = lastUpdateTimeStamp;
    for (ServerMapping mapping : validMapping) {
        if (mapping.getAsset() != null) {
            MetricData[] sensorDatas = restClient.getServerRelatedSensorDataByServerID(mapping.getAsset(), lastUpdateTimeStamp, FIVE_MINUTES * latencyFactor).getBody();
            Asset server = restClient.getAssetByID(mapping.getAsset()).getBody();
            List<String> pdus = server.getPdus();
            StatContents contents = new StatContents();
            StatContent frontTemp = new StatContent();
            List<Double> frontValues = new ArrayList<Double>();
            List<Long> frontTimes = new ArrayList<Long>();
            StatContent backTemp = new StatContent();
            List<Double> backValues = new ArrayList<Double>();
            List<Long> backTimes = new ArrayList<Long>();
            StatContent pduAMPSValue = new StatContent();
            List<Double> pduAMPSValues = new ArrayList<Double>();
            List<Long> pduAMPSTimes = new ArrayList<Long>();
            StatContent pduRealtimeVoltage = new StatContent();
            List<Double> voltageValues = new ArrayList<Double>();
            List<Long> voltageTimes = new ArrayList<Long>();
            StatContent pduRealtimePower = new StatContent();
            List<Double> powerValues = new ArrayList<Double>();
            List<Long> powerTimes = new ArrayList<Long>();
            StatContent frontHumidityPercent = new StatContent();
            List<Double> frontHumidityValues = new ArrayList<Double>();
            List<Long> frontHumidityTimes = new ArrayList<Long>();
            StatContent backHumidityPercent = new StatContent();
            List<Double> backHumidityValues = new ArrayList<Double>();
            List<Long> backHumidityTimes = new ArrayList<Long>();
            StatContent currentLoad = new StatContent();
            List<Double> currentLoadValues = new ArrayList<Double>();
            List<Long> currentLoadTimes = new ArrayList<Long>();
            StatContent powerLoad = new StatContent();
            List<Double> powerLoadValues = new ArrayList<Double>();
            List<Long> powerLoadTimes = new ArrayList<Long>();
            String pduId = null;
            String currentMetricName = MetricName.SERVER_CONNECTED_PDU_CURRENT;
            String powerMetricName = MetricName.SERVER_CONNECTED_PDU_POWER;
            String voltageMetricName = MetricName.SERVER_VOLTAGE;
            String currentLoadMetricName = MetricName.PDU_CURRENT_LOAD;
            String powerLoadMetricName = MetricName.PDU_POWER_LOAD;
            if (pdus != null && !pdus.isEmpty()) {
                pduId = pdus.get(0);
                currentMetricName = String.format(MetricKeyName.SERVER_CONNECTED_PDUX_TOTAL_CURRENT, pduId);
                powerMetricName = String.format(MetricKeyName.SERVER_CONNECTED_PDUX_TOTAL_POWER, pduId);
                currentLoadMetricName = String.format(MetricKeyName.SERVER_CONNECTED_PDUX_CURRENT_LOAD, pduId);
                powerLoadMetricName = String.format(MetricKeyName.SERVER_CONNECTED_PDUX_POWER_LOAD, pduId);
            }
            for (MetricData data : sensorDatas) {
                if (data.getTimeStamp() > newUpdateTimeStamp) {
                    newUpdateTimeStamp = data.getTimeStamp();
                    hasNewData = true;
                }
                String metricName = data.getMetricName();
                if (metricName.equals(currentMetricName)) {
                    pduAMPSValues.add(data.getValueNum());
                    pduAMPSTimes.add(data.getTimeStamp());
                    continue;
                } else if (metricName.equals(powerMetricName)) {
                    powerValues.add(data.getValueNum());
                    powerTimes.add(data.getTimeStamp());
                    continue;
                } else if (metricName.equals(voltageMetricName)) {
                    voltageValues.add(data.getValueNum());
                    voltageTimes.add(data.getTimeStamp());
                    continue;
                } else if (metricName.equals(currentLoadMetricName)) {
                    currentLoadValues.add(data.getValueNum());
                    currentLoadTimes.add(data.getTimeStamp());
                    continue;
                } else if (metricName.equals(powerLoadMetricName)) {
                    powerLoadValues.add(data.getValueNum());
                    powerLoadTimes.add(data.getTimeStamp());
                    continue;
                } else if (metricName.contains(MetricName.SERVER_FRONT_HUMIDITY)) {
                    frontHumidityValues.add(data.getValueNum());
                    frontHumidityTimes.add(data.getTimeStamp());
                    continue;
                } else if (metricName.contains(MetricName.SERVER_FRONT_TEMPERATURE)) {
                    frontValues.add(data.getValueNum());
                    frontTimes.add(data.getTimeStamp());
                    continue;
                } else if (metricName.contains(MetricName.SERVER_BACK_TEMPREATURE)) {
                    backValues.add(data.getValueNum());
                    backTimes.add(data.getTimeStamp());
                    continue;
                } else if (metricName.contains(MetricName.SERVER_BACK_HUMIDITY)) {
                    backHumidityValues.add(data.getValueNum());
                    backHumidityTimes.add(data.getTimeStamp());
                    continue;
                }
            }
            if (!frontValues.isEmpty()) {
                frontTemp.setStatKey(VROConsts.ENVRIONMENT_FRONT_TEMPERATURE_METRIC);
                frontTemp.setData(frontValues.stream().mapToDouble(Double::valueOf).toArray());
                frontTemp.setTimestamps(frontTimes.stream().mapToLong(Long::valueOf).toArray());
                contents.addStatContent(frontTemp);
            }
            if (!backValues.isEmpty()) {
                backTemp.setStatKey(VROConsts.ENVRIONMENT_BACK_TEMPERATURE_METRIC);
                backTemp.setData(backValues.stream().mapToDouble(Double::valueOf).toArray());
                backTemp.setTimestamps(backTimes.stream().mapToLong(Long::valueOf).toArray());
                contents.addStatContent(backTemp);
            }
            if (!pduAMPSValues.isEmpty()) {
                pduAMPSValue.setStatKey(VROConsts.ENVRIONMENT_PDU_AMPS_METRIC);
                pduAMPSValue.setData(pduAMPSValues.stream().mapToDouble(Double::valueOf).toArray());
                pduAMPSValue.setTimestamps(pduAMPSTimes.stream().mapToLong(Long::valueOf).toArray());
                contents.addStatContent(pduAMPSValue);
            }
            if (!voltageValues.isEmpty()) {
                pduRealtimeVoltage.setStatKey(VROConsts.ENVRIONMENT_PDU_VOLTS_METRIC);
                pduRealtimeVoltage.setData(voltageValues.stream().mapToDouble(Double::valueOf).toArray());
                pduRealtimeVoltage.setTimestamps(voltageTimes.stream().mapToLong(Long::valueOf).toArray());
                contents.addStatContent(pduRealtimeVoltage);
            }
            if (!powerValues.isEmpty()) {
                pduRealtimePower.setStatKey(VROConsts.ENVRIONMENT_PDU_POWER_METRIC);
                pduRealtimePower.setData(powerValues.stream().mapToDouble(Double::valueOf).toArray());
                pduRealtimePower.setTimestamps(powerTimes.stream().mapToLong(Long::valueOf).toArray());
                contents.addStatContent(pduRealtimePower);
            }
            if (!frontHumidityValues.isEmpty()) {
                frontHumidityPercent.setStatKey(VROConsts.ENVRIONMENT_FRONT_HUMIDITY_METRIC);
                frontHumidityPercent.setData(frontHumidityValues.stream().mapToDouble(Double::valueOf).toArray());
                frontHumidityPercent.setTimestamps(frontHumidityTimes.stream().mapToLong(Long::valueOf).toArray());
                contents.addStatContent(frontHumidityPercent);
            }
            if (!backHumidityValues.isEmpty()) {
                backHumidityPercent.setStatKey(VROConsts.ENVRIONMENT_BACK_HUMIDITY_METRIC);
                backHumidityPercent.setData(backHumidityValues.stream().mapToDouble(Double::valueOf).toArray());
                backHumidityPercent.setTimestamps(backHumidityTimes.stream().mapToLong(Long::valueOf).toArray());
                contents.addStatContent(backHumidityPercent);
            }
            if (!currentLoadValues.isEmpty()) {
                currentLoad.setStatKey(VROConsts.ENVRIONMENT_PDU_AMPS_LOAD_METRIC);
                currentLoad.setData(currentLoadValues.stream().mapToDouble(Double::valueOf).toArray());
                currentLoad.setTimestamps(currentLoadTimes.stream().mapToLong(Long::valueOf).toArray());
                contents.addStatContent(currentLoad);
            }
            if (!powerLoadValues.isEmpty()) {
                powerLoad.setStatKey(VROConsts.ENVRIONMENT_PDU_POWER_LOAD_METRIC);
                powerLoad.setData(powerLoadValues.stream().mapToDouble(Double::valueOf).toArray());
                powerLoad.setTimestamps(powerLoadTimes.stream().mapToLong(Long::valueOf).toArray());
                contents.addStatContent(powerLoad);
            }
            if (!contents.getStatContents().isEmpty()) {
                logger.info("Push data to VRO: " + config.getServerURL());
                metricClient.addStats(null, UUID.fromString(mapping.getVroResourceID()), contents, false);
            }
            // UPDATE THE PROPERTIES
            if (executionCount % 1000 == 0) {
                PropertyContents propertyContents = new PropertyContents();
                packagingPropertyContent(assetDictionary.get(mapping.getAsset()), propertyContents);
                metricClient.addProperties(null, UUID.fromString(mapping.getVroResourceID()), propertyContents);
            }
        }
    }
    if (hasNewData) {
        Long factor = (currentTime - newUpdateTimeStamp) / FIVE_MINUTES + 1;
        if (factor > 24) {
            factor = 24L;
        } else if (factor < 0) {
            factor = 1L;
        }
        latencyFactorMap.put(config.getServerURL(), factor);
        if (newUpdateTimeStamp > currentTime) {
            newUpdateTimeStamp = currentTime;
        }
        lastUpdateTimeMap.put(config.getServerURL(), newUpdateTimeStamp);
    } else {
        latencyFactor = latencyFactor + 1;
        if (latencyFactor > 24) {
            latencyFactor = 24L;
        }
        latencyFactorMap.put(config.getServerURL(), latencyFactor);
    }
    executionCount++;
    logger.info("Finished Sync metric data for VRO: " + config.getServerURL());
}
Also used : HashMap(java.util.HashMap) ServerMapping(com.vmware.flowgate.common.model.ServerMapping) ArrayList(java.util.ArrayList) AuthException(com.vmware.ops.api.client.exceptions.AuthException) ResourceIdentifier(com.vmware.ops.api.model.resource.ResourceIdentifier) AssetIPMapping(com.vmware.flowgate.common.model.AssetIPMapping) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) Asset(com.vmware.flowgate.common.model.Asset) ConnectException(java.net.ConnectException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) IntegrationStatus(com.vmware.flowgate.common.model.IntegrationStatus) AuthException(com.vmware.ops.api.client.exceptions.AuthException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) ResourceDto(com.vmware.ops.api.model.resource.ResourceDto) PropertyContents(com.vmware.ops.api.model.property.PropertyContents) StatContent(com.vmware.ops.api.model.stat.StatContent) StatContents(com.vmware.ops.api.model.stat.StatContents) VROConfig(com.vmware.flowgate.vroworker.vro.VROConfig) MetricClient(com.vmware.flowgate.vroworker.vro.MetricClient) MetricData(com.vmware.flowgate.common.model.MetricData)

Aggregations

IntegrationStatus (com.vmware.flowgate.common.model.IntegrationStatus)2 MetricClient (com.vmware.flowgate.vroworker.vro.MetricClient)2 VROConfig (com.vmware.flowgate.vroworker.vro.VROConfig)2 AuthException (com.vmware.ops.api.client.exceptions.AuthException)2 IOException (java.io.IOException)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 ConnectException (java.net.ConnectException)2 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)2 Asset (com.vmware.flowgate.common.model.Asset)1 AssetIPMapping (com.vmware.flowgate.common.model.AssetIPMapping)1 MetricData (com.vmware.flowgate.common.model.MetricData)1 ServerMapping (com.vmware.flowgate.common.model.ServerMapping)1 AlertClient (com.vmware.flowgate.vroworker.vro.AlertClient)1 PropertyContents (com.vmware.ops.api.model.property.PropertyContents)1 ResourceDto (com.vmware.ops.api.model.resource.ResourceDto)1 ResourceIdentifier (com.vmware.ops.api.model.resource.ResourceIdentifier)1 StatContent (com.vmware.ops.api.model.stat.StatContent)1 StatContents (com.vmware.ops.api.model.stat.StatContents)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1