Search in sources :

Example 1 with PrometheusAlert

use of com.sequenceiq.periscope.domain.PrometheusAlert in project cloudbreak by hortonworks.

the class PrometheusAlertResponseConverter method convert.

@Override
public PrometheusAlert convert(PrometheusAlertResponse source) {
    PrometheusAlert alert = new PrometheusAlert();
    alert.setName(source.getAlertName());
    alert.setDescription(source.getDescription());
    alert.setPeriod(source.getPeriod());
    alert.setAlertState(source.getAlertState() != null ? source.getAlertState() : CRITICAL);
    double threshold = source.getThreshold();
    String alertRuleName = source.getAlertRuleName();
    try {
        AlertOperator alertOperator = source.getAlertOperator() != null ? source.getAlertOperator() : AlertOperator.MORE_THAN;
        String operator = alertOperator.getOperator();
        String alertRule = templateService.createAlert(alertRuleName, alert.getName(), String.valueOf(threshold), alert.getPeriod(), operator);
        alert.setAlertRule(alertRule);
        alert.setParameters(createParametersFrom(threshold, alertOperator));
        if (source.getScalingPolicy() != null) {
            ScalingPolicy scalingPolicy = scalingPolicyRequestConverter.convert(source.getScalingPolicy());
            scalingPolicy.setAlert(alert);
            alert.setScalingPolicy(scalingPolicy);
        }
    } catch (Exception e) {
        throw new ConversionFailedException(TypeDescriptor.valueOf(PrometheusAlertRequest.class), TypeDescriptor.valueOf(PrometheusAlert.class), source.toString(), e);
    }
    return alert;
}
Also used : ScalingPolicy(com.sequenceiq.periscope.domain.ScalingPolicy) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) PrometheusAlert(com.sequenceiq.periscope.domain.PrometheusAlert) AlertOperator(com.sequenceiq.periscope.api.model.AlertOperator) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with PrometheusAlert

use of com.sequenceiq.periscope.domain.PrometheusAlert in project cloudbreak by hortonworks.

the class AlertService method updatePrometheusAlert.

public PrometheusAlert updatePrometheusAlert(Long clusterId, Long alertId, PrometheusAlert prometheusAlert) {
    PrometheusAlert alert = findPrometheusAlertByCluster(clusterId, alertId);
    alert.setName(prometheusAlert.getName());
    alert.setAlertRule(prometheusAlert.getAlertRule());
    alert.setPeriod(prometheusAlert.getPeriod());
    alert.setDescription(prometheusAlert.getDescription());
    alert.setAlertState(prometheusAlert.getAlertState());
    PrometheusAlert savedAlert = prometheusAlertRepository.save(alert);
    Cluster cluster = clusterService.find(clusterId);
    consulKeyValueService.addAlert(cluster, savedAlert);
    LOGGER.info("Prometheus alert '{}' has been updated for cluster 'ID:{}'", alert.getName(), cluster.getId());
    return savedAlert;
}
Also used : PrometheusAlert(com.sequenceiq.periscope.domain.PrometheusAlert) Cluster(com.sequenceiq.periscope.domain.Cluster)

Example 3 with PrometheusAlert

use of com.sequenceiq.periscope.domain.PrometheusAlert in project cloudbreak by hortonworks.

the class AlertService method createPrometheusAlert.

public PrometheusAlert createPrometheusAlert(Long clusterId, PrometheusAlert alert) {
    Cluster cluster = clusterService.findOneById(clusterId);
    alert.setCluster(cluster);
    PrometheusAlert savedAlert = (PrometheusAlert) save(alert);
    cluster.addPrometheusAlert(savedAlert);
    clusterRepository.save(cluster);
    consulKeyValueService.addAlert(cluster, savedAlert);
    LOGGER.info("Prometheus alert '{}' has been created for cluster 'ID:{}'", alert.getName(), cluster.getId());
    return savedAlert;
}
Also used : PrometheusAlert(com.sequenceiq.periscope.domain.PrometheusAlert) Cluster(com.sequenceiq.periscope.domain.Cluster)

Example 4 with PrometheusAlert

use of com.sequenceiq.periscope.domain.PrometheusAlert in project cloudbreak by hortonworks.

the class PrometheusAlertRequestConverter method convert.

@Override
public PrometheusAlert convert(PrometheusAlertRequest source) {
    PrometheusAlert alert = new PrometheusAlert();
    alert.setName(source.getAlertName());
    alert.setDescription(source.getDescription());
    alert.setPeriod(source.getPeriod());
    alert.setAlertState(source.getAlertState() != null ? source.getAlertState() : CRITICAL);
    double threshold = source.getThreshold();
    String alertRuleName = source.getAlertRuleName();
    try {
        AlertOperator alertOperator = source.getAlertOperator() != null ? source.getAlertOperator() : AlertOperator.MORE_THAN;
        String operator = alertOperator.getOperator();
        String alertRule = templateService.createAlert(alertRuleName, alert.getName(), String.valueOf(threshold), alert.getPeriod(), operator);
        alert.setAlertRule(alertRule);
        alert.setParameters(createParametersFrom(threshold, alertOperator));
    } catch (Exception e) {
        throw new ConversionFailedException(TypeDescriptor.valueOf(PrometheusAlertRequest.class), TypeDescriptor.valueOf(PrometheusAlert.class), source.toString(), e);
    }
    return alert;
}
Also used : ConversionFailedException(org.springframework.core.convert.ConversionFailedException) PrometheusAlert(com.sequenceiq.periscope.domain.PrometheusAlert) AlertOperator(com.sequenceiq.periscope.api.model.AlertOperator) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 5 with PrometheusAlert

use of com.sequenceiq.periscope.domain.PrometheusAlert in project cloudbreak by hortonworks.

the class PrometheusEvaluator method run.

@Override
public void run() {
    try {
        Cluster cluster = clusterService.find(clusterId);
        MDCBuilder.buildMdcContext(cluster);
        TlsConfiguration tlsConfig = tlsSecurityService.getConfiguration(cluster);
        Client client = RestClientUtil.createClient(tlsConfig.getServerCert(), tlsConfig.getClientCert(), tlsConfig.getClientKey(), true, PrometheusEvaluator.class);
        String prometheusAddress = String.format("https://%s:%s/prometheus", cluster.getAmbari().getHost(), cluster.getPort());
        WebTarget target = client.target(prometheusAddress);
        for (PrometheusAlert alert : alertRepository.findAllByCluster(clusterId)) {
            String alertName = alert.getName();
            LOGGER.info("Checking Prometheus based alert: '{}'", alertName);
            String query = URLEncoder.encode(String.format("ALERTS{alertname=\"%s\"}[%dm]", alert.getName(), alert.getPeriod()), "UTF-8");
            Response response = target.path("/api/v1/query").queryParam("query", query).request().header("Accept", MediaType.APPLICATION_JSON_VALUE).get();
            PrometheusResponse prometheusResponse = JaxRSUtil.response(response, PrometheusResponse.class);
            boolean triggerScale = false;
            switch(alert.getAlertState()) {
                case OK:
                    triggerScale = prometheusResponse.getData().getResult().isEmpty();
                    break;
                case CRITICAL:
                    for (Result alertResult : prometheusResponse.getData().getResult()) {
                        if ("firing".equals(alertResult.getMetric().getAlertstate())) {
                            List<Object> lastSample = alertResult.getValues().get(alertResult.getValues().size() - 1);
                            Object alertValue = lastSample.get(1);
                            if (alertValue instanceof String) {
                                if ("0".equals(alertValue)) {
                                    break;
                                }
                                triggerScale = true;
                            }
                        }
                    }
                    break;
                default:
                    triggerScale = false;
                    break;
            }
            if (triggerScale && isPolicyAttached(alert)) {
                publishEvent(new ScalingEvent(alert));
            }
        }
    } catch (Exception e) {
        LOGGER.error("Failed to retrieve alerts from Prometheus", e);
        publishEvent(new UpdateFailedEvent(clusterId));
    }
}
Also used : UpdateFailedEvent(com.sequenceiq.periscope.monitor.event.UpdateFailedEvent) PrometheusResponse(com.sequenceiq.periscope.model.PrometheusResponse) Cluster(com.sequenceiq.periscope.domain.Cluster) Result(com.sequenceiq.periscope.model.PrometheusResponse.Result) PrometheusResponse(com.sequenceiq.periscope.model.PrometheusResponse) Response(javax.ws.rs.core.Response) PrometheusAlert(com.sequenceiq.periscope.domain.PrometheusAlert) ScalingEvent(com.sequenceiq.periscope.monitor.event.ScalingEvent) TlsConfiguration(com.sequenceiq.periscope.model.TlsConfiguration) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Aggregations

PrometheusAlert (com.sequenceiq.periscope.domain.PrometheusAlert)7 Cluster (com.sequenceiq.periscope.domain.Cluster)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 AlertOperator (com.sequenceiq.periscope.api.model.AlertOperator)2 MetricAlert (com.sequenceiq.periscope.domain.MetricAlert)2 TimeAlert (com.sequenceiq.periscope.domain.TimeAlert)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 AmbariClient (com.sequenceiq.ambari.client.AmbariClient)1 CommonService (com.sequenceiq.ambari.client.services.CommonService)1 AlertRuleDefinitionEntry (com.sequenceiq.periscope.api.model.AlertRuleDefinitionEntry)1 AutoscaleClusterRequest (com.sequenceiq.periscope.api.model.AutoscaleClusterRequest)1 MetricAlertRequest (com.sequenceiq.periscope.api.model.MetricAlertRequest)1 PrometheusAlertRequest (com.sequenceiq.periscope.api.model.PrometheusAlertRequest)1 ScalingConfigurationRequest (com.sequenceiq.periscope.api.model.ScalingConfigurationRequest)1 TimeAlertRequest (com.sequenceiq.periscope.api.model.TimeAlertRequest)1 BaseAlert (com.sequenceiq.periscope.domain.BaseAlert)1 ScalingPolicy (com.sequenceiq.periscope.domain.ScalingPolicy)1