Example usage for com.amazonaws.services.cloudwatch AmazonCloudWatch getMetricStatistics

List of usage examples for com.amazonaws.services.cloudwatch AmazonCloudWatch getMetricStatistics

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudwatch AmazonCloudWatch getMetricStatistics.

Prototype

GetMetricStatisticsResult getMetricStatistics(GetMetricStatisticsRequest getMetricStatisticsRequest);

Source Link

Document

Gets statistics for the specified metric.

Usage

From source file:com.appdynamics.extensions.cloudwatch.metricsmanager.MetricsManager.java

License:Apache License

/**
 * Helper method to gather metrics for a particular namespace using certain filter names
 * @param namespace     Name of the namespace
 * @param filterNames   List of filter names (used to filter metrics)
 * @return Map          Map containing metrics for a particular namespace
 *//*www  . j a v  a  2s .  c  o m*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Map<String, Map<String, List<Datapoint>>> gatherMetricsHelper(final AmazonCloudWatch awsCloudWatch,
        final String namespace, String region, String... filterNames) {
    final Map<String, Map<String, List<Datapoint>>> metrics = new ConcurrentHashMap<String, Map<String, List<Datapoint>>>();

    List<Metric> metricsList = getMetrics(awsCloudWatch, namespace, filterNames);
    logger.info("Available metrics Size across all instances for NameSpace:" + namespace + " Region:" + region
            + " - " + metricsList.size());

    //Each metric is of the form {Namespace: AWS/EC2,MetricName: DiskWriteOps,Dimensions: [{Name: InstanceId,Value: i-b0b31ff2}]}
    if (logger.isDebugEnabled()) {
        logMetricPerInstance(metricsList, namespace, region);
    }

    int count = 0;
    ExecutorCompletionService ecs = new ExecutorCompletionService(workerPool);
    for (final Metric metric : metricsList) {
        List<Dimension> dimensions = metric.getDimensions();
        final String key = dimensions.get(0).getValue();

        if (!metrics.containsKey(key)) {
            metrics.put(key, new ConcurrentHashMap<String, List<Datapoint>>());
        }
        if (!metrics.get(key).containsKey(metric.getMetricName())) {
            final String metricName = metric.getMetricName();
            if (!amazonCloudWatchMonitor.isMetricDisabled(namespace, metricName)) {
                ecs.submit(new Callable() {
                    public Object call() throws Exception {
                        try {
                            GetMetricStatisticsRequest request = createGetMetricStatisticsRequest(namespace,
                                    metricName, getMetricType(namespace, metricName).getTypeName(),
                                    metric.getDimensions());
                            GetMetricStatisticsResult result = awsCloudWatch.getMetricStatistics(request);
                            if (logger.isDebugEnabled()) {
                                logger.debug("Fetching MetricStatistics for NameSpace = " + namespace
                                        + " Metric = " + metric + " Result = " + result);
                            }
                            List<Datapoint> dataPoints = result.getDatapoints();
                            if (dataPoints != null && !dataPoints.isEmpty()) {
                                metrics.get(key).put(metricName, dataPoints);
                            }
                        } catch (Exception e) {
                            //Better to log it here when we have the context rather than in get()
                            logger.error("Error while getting the MetricStatistics for NameSpace =" + namespace
                                    + " Metric " + metric, e);
                        }
                        return null;
                    }
                });
                ++count;
            }
        }
    }

    //Wait until its complete
    for (int i = 0; i < count; i++) {
        try {
            ecs.take().get();
        } catch (InterruptedException e) {
            logger.error("Interrupted exception", e);
        } catch (ExecutionException e) {
            // We are catching the exceptions b4hand, so this will not be executed
            logger.error("ExecutionException in MetricStatistics", e);
        }
    }

    // Logging metricName and datapoints corresponding to each instanceID
    if (logger.isDebugEnabled()) {
        logDataPointsPerInstance(metrics);
    }

    return metrics;
}

From source file:com.appdynamics.extensions.cloudwatch.metricsmanager.metricsmanagerimpl.EC2MetricsManager.java

License:Apache License

/**
  * Gather metrics for AWS/EC2/*from   w  ww  .  ja  va  2s.  com*/
  * @return   Map     Map containing metrics
  */
@Override
public Map<String, Map<String, List<Datapoint>>> gatherMetrics(final AmazonCloudWatch awsCloudWatch,
        String region) {
    final Map<String, Map<String, List<Datapoint>>> metrics = new ConcurrentHashMap<String, Map<String, List<Datapoint>>>();

    List<Metric> metricsList = getMetrics(awsCloudWatch, NAMESPACE, "InstanceId");
    logger.info("Available metrics Size across all instances for NameSpace:" + NAMESPACE + " Region:" + region
            + " - " + metricsList.size());

    //Each metric is of the form {Namespace: AWS/EC2,MetricName: DiskWriteOps,Dimensions: [{Name: InstanceId,Value: i-b0b31ff2}]}
    if (logger.isDebugEnabled()) {
        logMetricPerInstance(metricsList, NAMESPACE, region);
    }

    int count = 0;
    ExecutorCompletionService<Object> ecs = new ExecutorCompletionService<Object>(workerPool);
    for (final Metric metric : metricsList) {
        List<Dimension> dimensions = metric.getDimensions();
        final String key = getInstanceName(region, dimensions.get(0).getValue());

        if (!metrics.containsKey(key)) {
            metrics.put(key, new ConcurrentHashMap<String, List<Datapoint>>());
        }
        if (!metrics.get(key).containsKey(metric.getMetricName())) {
            final String metricName = metric.getMetricName();
            if (!amazonCloudWatchMonitor.isMetricDisabled(NAMESPACE, metricName)) {
                ecs.submit(new Callable<Object>() {
                    public Object call() throws Exception {
                        try {
                            GetMetricStatisticsRequest request = createGetMetricStatisticsRequest(NAMESPACE,
                                    metricName, getMetricType(NAMESPACE, metricName).getTypeName(),
                                    metric.getDimensions());
                            GetMetricStatisticsResult result = awsCloudWatch.getMetricStatistics(request);
                            if (logger.isDebugEnabled()) {
                                logger.debug("Fetching MetricStatistics for NameSpace = " + NAMESPACE
                                        + " Metric = " + metric + " Result = " + result);
                            }
                            List<Datapoint> dataPoints = result.getDatapoints();
                            if (dataPoints != null && !dataPoints.isEmpty()) {
                                metrics.get(key).put(metricName, dataPoints);
                            }
                        } catch (Exception e) {
                            //Better to log it here when we have the context rather than in get()
                            logger.error("Error while getting the MetricStatistics for NameSpace =" + NAMESPACE
                                    + " Metric " + metric, e);
                        }
                        return null;
                    }
                });
                ++count;
            }
        }
    }

    //Wait until its complete
    for (int i = 0; i < count; i++) {
        try {
            ecs.take().get();
        } catch (InterruptedException e) {
            logger.error("Interrupted exception", e);
        } catch (ExecutionException e) {
            // We are catching the exceptions b4hand, so this will not be executed
            logger.error("ExecutionException in MetricStatistics", e);
        }
    }

    // Logging metricName and datapoints corresponding to each instanceID
    if (logger.isDebugEnabled()) {
        logDataPointsPerInstance(metrics);
    }

    return metrics;

}

From source file:com.appdynamics.extensions.cloudwatch.metricsmanager.metricsmanagerimpl.ElastiCacheMetricsManager.java

License:Apache License

/**
 * Gather metrics for AWS/ElastiCache//from  w  w  w .  j a  v a  2 s.c om
 * @return   Map     Map containing metrics
 */
@Override
public Map gatherMetrics(AmazonCloudWatch awsCloudWatch, String region) {
    List<Metric> metricsList = getMetrics(awsCloudWatch, NAMESPACE, "CacheClusterId", "CacheNodeId");

    //Top level     -- Key = CacheClusterIds,       Value = HashMap of cache nodes
    //Mid level     -- Key = CacheNodeIds,          Value = HashMap of Metrics
    //Bottom level  -- Key = MetricName,            Value = List of datapoints
    Map<String, Map<String, Map<String, List<Datapoint>>>> cacheClusterMetrics = new HashMap<String, Map<String, Map<String, List<Datapoint>>>>();

    for (com.amazonaws.services.cloudwatch.model.Metric metric : metricsList) {
        List<Dimension> dimensions = metric.getDimensions();
        String cacheClusterId = dimensions.get(0).getValue();
        String cacheNodeId = dimensions.get(1).getValue();
        if (!cacheClusterMetrics.containsKey(cacheClusterId)) {
            cacheClusterMetrics.put(cacheClusterId, new HashMap<String, Map<String, List<Datapoint>>>());
        }
        if (!cacheClusterMetrics.get(cacheClusterId).containsKey(cacheNodeId)) {
            cacheClusterMetrics.get(cacheClusterId).put(cacheNodeId, new HashMap<String, List<Datapoint>>());
        }
        if (!cacheClusterMetrics.get(cacheClusterId).get(cacheNodeId).containsKey(metric.getMetricName())) {

            if (!amazonCloudWatchMonitor.isMetricDisabled(NAMESPACE, metric.getMetricName())) {
                GetMetricStatisticsRequest getMetricStatisticsRequest = createGetMetricStatisticsRequest(
                        NAMESPACE, metric.getMetricName(),
                        getMetricType(NAMESPACE, metric.getMetricName()).getTypeName(), dimensions);
                GetMetricStatisticsResult getMetricStatisticsResult = awsCloudWatch
                        .getMetricStatistics(getMetricStatisticsRequest);
                cacheClusterMetrics.get(cacheClusterId).get(cacheNodeId).put(metric.getMetricName(),
                        getMetricStatisticsResult.getDatapoints());
            }
        }
    }
    return cacheClusterMetrics;
}

From source file:com.appdynamics.extensions.cloudwatch.metricsmanager.metricsmanagerimpl.ELBMetricsManager.java

License:Apache License

/**
 * Gather metrics for AWS/ELB//from   ww  w . j  a v  a 2  s .  c  o  m
 * @return   Map     Map containing metrics
 */
@Override
public Map gatherMetrics(final AmazonCloudWatch awsCloudWatch, String region) {
    List<com.amazonaws.services.cloudwatch.model.Metric> elbMetricsList = getMetrics(awsCloudWatch, NAMESPACE,
            "LoadBalancerName", "AvailabilityZone");

    //Top level     -- Key = LoadBalancerName,      Value = HashMap of availability zones
    //Mid level     -- Key = AvailabilityZoneName,  Value = HashMap of Metrics
    //Bottom level  -- Key = MetricName,            Value = List of datapoints
    final HashMap<String, HashMap<String, HashMap<String, List<Datapoint>>>> elbMetrics = new HashMap<String, HashMap<String, HashMap<String, List<Datapoint>>>>();

    int count = 0;
    ExecutorCompletionService<Object> ecs = new ExecutorCompletionService<Object>(workerPool);

    for (final com.amazonaws.services.cloudwatch.model.Metric metric : elbMetricsList) {
        final List<Dimension> dimensions = metric.getDimensions();
        final String loadBalancerName = dimensions.get(0).getValue();
        final String availabilityZone = dimensions.get(1).getValue();

        if (!elbMetrics.containsKey(loadBalancerName)) {
            elbMetrics.put(loadBalancerName, new HashMap<String, HashMap<String, List<Datapoint>>>());
        }

        if (!elbMetrics.get(loadBalancerName).containsKey(availabilityZone)) {
            elbMetrics.get(loadBalancerName).put(availabilityZone, new HashMap<String, List<Datapoint>>());
        }

        if (!elbMetrics.get(loadBalancerName).get(availabilityZone).containsKey(metric.getMetricName())) {

            if (!amazonCloudWatchMonitor.isMetricDisabled(NAMESPACE, metric.getMetricName())) {
                ecs.submit(new Callable<Object>() {
                    public Object call() throws Exception {
                        try {
                            GetMetricStatisticsRequest getMetricStatisticsRequest = createGetMetricStatisticsRequest(
                                    NAMESPACE, metric.getMetricName(),
                                    getMetricType(NAMESPACE, metric.getMetricName()).getTypeName(), dimensions);
                            GetMetricStatisticsResult getMetricStatisticsResult = awsCloudWatch
                                    .getMetricStatistics(getMetricStatisticsRequest);
                            elbMetrics.get(loadBalancerName).get(availabilityZone).put(metric.getMetricName(),
                                    getMetricStatisticsResult.getDatapoints());

                        } catch (Exception e) {
                            //Better to log it here when we have the context rather than in get()
                            logger.error("Error while getting the MetricStatistics for NameSpace =" + NAMESPACE
                                    + " Metric " + metric, e);
                        }
                        return null;
                    }
                });
                ++count;

            }
        }
    }

    //Wait until its complete
    for (int i = 0; i < count; i++) {
        try {
            ecs.take().get();
        } catch (InterruptedException e) {
            logger.error("Interrupted exception", e);
        } catch (ExecutionException e) {
            // We are catching the exceptions b4hand, so this will not be executed
            logger.error("ExecutionException in MetricStatistics", e);
        }
    }

    return elbMetrics;
}

From source file:com.appdynamics.extensions.cloudwatch.metricsmanager.metricsmanagerimpl.RedshiftMetricsManager.java

License:Apache License

/**
 * Gather metrics for AWS/Redshift// w w w. ja  v a 2s  .co  m
 * @return   Map     Map containing metrics
 */
@Override
public Map gatherMetrics(AmazonCloudWatch awsCloudWatch, String region) {
    HashMap<String, HashMap<String, HashMap<String, List<Datapoint>>>> redshiftMetrics = new HashMap<String, HashMap<String, HashMap<String, List<Datapoint>>>>();
    List<com.amazonaws.services.cloudwatch.model.Metric> metrics = getMetrics(awsCloudWatch, NAMESPACE,
            "ClusterIdentifier", "NodeID");
    for (com.amazonaws.services.cloudwatch.model.Metric metric : metrics) {
        List<Dimension> dimensions = metric.getDimensions();
        String clusterIdentifier = dimensions.get(1).getValue();
        String nodeID = dimensions.get(0).getValue();
        if (!redshiftMetrics.containsKey(clusterIdentifier)) {
            redshiftMetrics.put(clusterIdentifier, new HashMap<String, HashMap<String, List<Datapoint>>>());
        }
        if (!redshiftMetrics.get(clusterIdentifier).containsKey(nodeID)) {
            redshiftMetrics.get(clusterIdentifier).put(nodeID, new HashMap<String, List<Datapoint>>());
        }
        if (!redshiftMetrics.get(clusterIdentifier).get(nodeID).containsKey(metric.getMetricName())) {

            if (!amazonCloudWatchMonitor.isMetricDisabled(NAMESPACE, metric.getMetricName())) {
                GetMetricStatisticsRequest getMetricStatisticsRequest = createGetMetricStatisticsRequest(
                        NAMESPACE, metric.getMetricName(),
                        getMetricType(NAMESPACE, metric.getMetricName()).getTypeName(), dimensions);
                GetMetricStatisticsResult getMetricStatisticsResult = awsCloudWatch
                        .getMetricStatistics(getMetricStatisticsRequest);
                redshiftMetrics.get(clusterIdentifier).get(nodeID).put(metric.getMetricName(),
                        getMetricStatisticsResult.getDatapoints());
            }
        }
    }

    return redshiftMetrics;
}

From source file:com.remediatetheflag.global.utils.AWSHelper.java

License:Apache License

public Double getClusterMemoryReservation(Region region) {
    AmazonCloudWatch client = AmazonCloudWatchClientBuilder.standard().withRegion(region.getName())
            .withCredentials(new DefaultAWSCredentialsProviderChain()).build();

    Dimension dimension = new Dimension();
    dimension.setName("ClusterName");
    dimension.setValue(RTFConfig.getExercisesCluster());
    Date date = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);//from   w  ww . j a  va 2 s. c om
    cal.add(Calendar.MINUTE, -5);
    GetMetricStatisticsRequest request = new GetMetricStatisticsRequest().withMetricName("MemoryReservation")
            .withDimensions(dimension).withPeriod(60).withStartTime(cal.getTime()).withEndTime(date)
            .withStatistics("Average").withNamespace("AWS/ECS");
    try {
        logger.debug("Requesting memory reservation for region " + region.getName() + " cluster "
                + RTFConfig.getExercisesCluster());

        GetMetricStatisticsResult response = client.getMetricStatistics(request);
        if (response.getDatapoints().isEmpty())
            return 0.0;
        return response.getDatapoints().get(0).getAverage();
    } catch (Exception e) {
        logger.error("Error getClusterContainerInstances for memory reservation in region " + region.getName()
                + " due to:\n" + e.getMessage());
        return 0.0;
    }
}

From source file:eu.optimis.monitoring.amazoncollector.MeasurementsHelper.java

License:Apache License

private Measurement getCWMeasurement(String metric_name, String optimis_metric_name, boolean custom,
        String instance_id, String service_id) {
    AmazonCloudWatch cw = getAmazonCloudWatchClient();
    String ns = custom ? "System/Linux" : "AWS/EC2";

    List<Dimension> dimensions = new LinkedList<Dimension>();
    Dimension dim = new Dimension();
    dim.setName("InstanceId");
    dim.setValue(instance_id);/* ww  w .  ja  va 2s  .com*/
    dimensions.add(dim);

    GetMetricStatisticsRequest req = new GetMetricStatisticsRequest();
    req.setStartTime(new Date(System.currentTimeMillis() - HOUR_AND_HALF_AGO - PERIOD));
    req.setEndTime(new Date(System.currentTimeMillis() - HOUR_AND_HALF_AGO));
    req.setMetricName(metric_name);
    req.setNamespace(ns);
    List<String> statistics = new LinkedList<String>();
    statistics.add(Statistic.Average.toString());
    req.setStatistics(statistics);
    req.setPeriod((int) PERIOD / 1000);
    req.setDimensions(dimensions);

    GetMetricStatisticsResult res = cw.getMetricStatistics(req);
    if (res.getDatapoints().isEmpty())
        return null;
    Datapoint dp = res.getDatapoints().get(0);
    return new Measurement(optimis_metric_name, dp.getAverage().toString(), dp.getUnit(), new Date(),
            instance_id, service_id);
}