List of usage examples for com.amazonaws.services.cloudwatch.model ListMetricsRequest ListMetricsRequest
ListMetricsRequest
From source file:aws.example.cloudwatch.ListMetrics.java
License:Open Source License
public static void main(String[] args) { final String USAGE = "To run this example, supply a metric name and metric namespace\n" + "Ex: ListMetrics <metric-name> <metric-namespace>\n"; if (args.length != 2) { System.out.println(USAGE); System.exit(1);/* w w w. j a v a 2 s.c om*/ } String name = args[0]; String namespace = args[1]; final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); boolean done = false; while (!done) { ListMetricsRequest request = new ListMetricsRequest().withMetricName(name).withNamespace(namespace); ListMetricsResult response = cw.listMetrics(request); for (Metric metric : response.getMetrics()) { System.out.printf("Retrieved metric %s", metric.getMetricName()); } request.setNextToken(response.getNextToken()); if (response.getNextToken() == null) { done = true; } } }
From source file:cloudwatch.src.main.java.aws.example.cloudwatch.ListMetrics.java
License:Open Source License
public static void main(String[] args) { final String USAGE = "To run this example, supply a metric name and metric namespace\n" + "Ex: ListMetrics <metric-name> <metric-namespace>\n"; if (args.length != 2) { System.out.println(USAGE); System.exit(1);/*from w w w. j a v a 2 s . c o m*/ } String metricName = args[0]; String metricNamespace = args[1]; final AmazonCloudWatch cloudWatch = AmazonCloudWatchClientBuilder.defaultClient(); boolean done = false; while (!done) { ListMetricsRequest request = new ListMetricsRequest().withMetricName(metricName) .withNamespace(metricNamespace); ListMetricsResult response = cloudWatch.listMetrics(request); for (Metric metric : response.getMetrics()) { System.out.printf("Retrieved metric %s", metric.getMetricName()); } request.setNextToken(response.getNextToken()); if (response.getNextToken() == null) { done = true; } } }
From source file:com.appdynamics.extensions.cloudwatch.metricsmanager.MetricsManager.java
License:Apache License
/** * Retrieve metrics for a particular namespace using the specified filter names * @param awsCloudWatch /* ww w. j a v a2s .c o m*/ * @param namespace Name of the namespace * @param filterNames List of filter names (used to filter metrics) * @return List<Metric> List of filtered metrics for a particular namespace */ protected List<Metric> getMetrics(AmazonCloudWatch awsCloudWatch, String namespace, String... filterNames) { ListMetricsRequest request = new ListMetricsRequest(); List<DimensionFilter> filters = new ArrayList<DimensionFilter>(); for (String filterName : filterNames) { DimensionFilter dimensionFilter = new DimensionFilter(); dimensionFilter.withName(filterName); filters.add(dimensionFilter); } request.withNamespace(namespace); request.withDimensions(filters); List<Metric> metricList = Lists.newArrayList(); ListMetricsResult listMetricsResult = awsCloudWatch.listMetrics(request); metricList = listMetricsResult.getMetrics(); // Retrieves all the metrics if metricList > 500 while (listMetricsResult.getNextToken() != null) { request.setNextToken(listMetricsResult.getNextToken()); listMetricsResult = awsCloudWatch.listMetrics(request); metricList.addAll(listMetricsResult.getMetrics()); } return metricList; }
From source file:com.pinterest.arcee.autoscaling.AwsAlarmManager.java
License:Apache License
@Override public List<String> listAwsMetrics(String groupName) throws Exception { DimensionFilter dimensionFilter = new DimensionFilter(); dimensionFilter.setName(DIMENTION_NAME); dimensionFilter.setValue(groupName); ListMetricsRequest listMetricsRequest = new ListMetricsRequest(); listMetricsRequest.withDimensions(dimensionFilter).withNamespace(METRIC_NAMESPACE); ListMetricsResult listMetricsResult = acwClient.listMetrics(listMetricsRequest); List<String> metricName = new ArrayList<>(); for (Metric metric : listMetricsResult.getMetrics()) { metricName.add(metric.getMetricName()); }// www . j a v a 2 s. c o m return metricName; }
From source file:org.elasticdroid.model.CloudWatchMetricsModel.java
License:Open Source License
/** * Retrieve the list of metrics//from ww w . j ava2 s . com * * @return Either * <ul> * <li>AmazonServiceException</li> * <li>AmazonClientException</li> * <li>List\<Metric\></li> * </ul> */ public Object retrieveMetricsList(Dimension... dimensions) { //the cloudwatch client to use AmazonCloudWatchClient cloudWatchClient = null; List<Metric> returnedMetrics = null; ArrayList<String> measureNames = new ArrayList<String>(); ListMetricsRequest request = new ListMetricsRequest(); //create credentials using the BasicAWSCredentials class BasicAWSCredentials credentials = new BasicAWSCredentials(connectionData.get("accessKey"), connectionData.get("secretAccessKey")); //create a cloudwatch client try { cloudWatchClient = new AmazonCloudWatchClient(credentials); } catch (AmazonServiceException amazonServiceException) { //if an error response is returned by AmazonIdentityManagement indicating either a //problem with the data in the request, or a server side issue. Log.e(this.getClass().getName(), "Exception:" + amazonServiceException.getMessage()); return amazonServiceException; } catch (AmazonClientException amazonClientException) { //If any internal errors are encountered inside the client while attempting to make //the request or handle the response. For example if a network connection is not //available. Log.e(this.getClass().getName(), "Exception:" + amazonClientException.getMessage()); return amazonClientException; } cloudWatchClient.setEndpoint(cloudWatchEndpoint); //create the request request = new ListMetricsRequest(); //request.setNextToken(nextToken) try { returnedMetrics = cloudWatchClient.listMetrics().getMetrics(); } catch (AmazonServiceException amazonServiceException) { //if an error response is returned by AmazonIdentityManagement indicating either a //problem with the data in the request, or a server side issue. Log.e(this.getClass().getName(), "Exception:" + amazonServiceException.getMessage()); return amazonServiceException; } catch (AmazonClientException amazonClientException) { //If any internal errors are encountered inside the client while attempting to make //the request or handle the response. For example if a network connection is not //available. Log.e(this.getClass().getName(), "Exception:" + amazonClientException.getMessage()); return amazonClientException; } //my own disgusting O(mnp) filter. This is REALLY CRAP! //remove all that does not fit into the dimension //for some reason, there isn't a way to provide dimensions using ListMetricsRequest in Java //you can do it in the C# API though :P List<Dimension> returnedDimensions; boolean added; for (Metric metric : returnedMetrics) { returnedDimensions = metric.getDimensions(); added = false; for (Dimension returnedDimension : returnedDimensions) { //check if any of the dimensions passed in to the model are equal to this. for (Dimension dimension : dimensions) { if (returnedDimension.getValue().equals(dimension.getValue())) { measureNames.add(metric.getMeasureName()); added = true; break; } } if (added) { break; } } } return measureNames; }