List of usage examples for com.amazonaws.services.ec2 AmazonEC2 describeTags
DescribeTagsResult describeTags(DescribeTagsRequest describeTagsRequest);
Describes the specified tags for your EC2 resources.
From source file:com.amazon.kinesis.streaming.agent.processing.processors.AddEC2MetadataConverter.java
License:Open Source License
private void refreshEC2Metadata() { LOGGER.info("Refreshing EC2 metadata"); metadataTimestamp = System.currentTimeMillis(); try {/*from ww w .j a v a 2s. c o m*/ EC2MetadataUtils.InstanceInfo info = EC2MetadataUtils.getInstanceInfo(); metadata = new LinkedHashMap<String, Object>(); metadata.put("privateIp", info.getPrivateIp()); metadata.put("availabilityZone", info.getAvailabilityZone()); metadata.put("instanceId", info.getInstanceId()); metadata.put("instanceType", info.getInstanceType()); metadata.put("accountId", info.getAccountId()); metadata.put("amiId", info.getImageId()); metadata.put("region", info.getRegion()); metadata.put("metadataTimestamp", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(new Date(metadataTimestamp))); final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); DescribeTagsResult result = ec2.describeTags(new DescribeTagsRequest() .withFilters(new Filter().withName("resource-id").withValues(info.getInstanceId()))); List<TagDescription> tags = result.getTags(); Map<String, Object> metadataTags = new LinkedHashMap<String, Object>(); for (TagDescription tag : tags) { metadataTags.put(tag.getKey().toLowerCase(), tag.getValue()); } metadata.put("tags", metadataTags); } catch (Exception ex) { LOGGER.warn("Error while updating EC2 metadata - " + ex.getMessage() + ", ignoring"); } }
From source file:com.kixeye.chassis.bootstrap.aws.AwsUtils.java
License:Apache License
/** * Fetches and instance's name Tag or null if it does not have one * @param instanceId// w ww. j a v a 2s.co m * @param amazonEC2 * @return */ public static String getInstanceName(String instanceId, AmazonEC2 amazonEC2) { DescribeTagsResult result = amazonEC2.describeTags( new DescribeTagsRequest().withFilters(new Filter().withName("resource-id").withValues(instanceId), new Filter().withName("resource-type").withValues("instance"), new Filter().withName("key").withValues(TAG_KEY_NAME))); if (result.getTags().isEmpty()) { return null; } String name = result.getTags().get(0).getValue(); return name == null || name.trim().equals("") ? null : name; }
From source file:org.plukh.fluffymeow.aws.AWSInstanceInfoProviderImpl.java
License:Open Source License
@Override public InstanceInfo getInstanceInfo() { if (instanceInfo == null) { try {/*from ww w.ja va 2 s . co m*/ AmazonEC2 ec2 = new AmazonEC2Client(); String instanceId = Request.Get("http://169.254.169.254/latest/meta-data/instance-id").execute() .returnContent().asString(); if (log.isDebugEnabled()) log.debug("Instance Id: " + instanceId); DescribeTagsRequest tagsRequest = new DescribeTagsRequest().withFilters( new Filter().withName("resource-id").withValues(instanceId), new Filter().withName("key").withValues(NAME_TAG, DEPLOYMENT_ID_TAG)); DescribeTagsResult tagsResult = ec2.describeTags(tagsRequest); String name = getTag(tagsResult, NAME_TAG); if (log.isDebugEnabled()) log.debug("Instance name: " + name); String deploymentId = getTag(tagsResult, DEPLOYMENT_ID_TAG); if (log.isDebugEnabled()) log.debug("Deployment: " + deploymentId); instanceInfo = new InstanceInfo().withInstanceId(instanceId).withName(name) .withDeploymentId(deploymentId); } catch (IOException e) { throw new AWSInstanceInfoException("Error retrieving AWS instance info", e); } } return instanceInfo; }
From source file:org.web.online.cloudwatch.tomcat.valve.ElapsedTimeAggregator.java
License:Apache License
/** * Construct the instance. Sets up the Cloud Watch metric with the given parameters. * //from w w w .java 2s . c o m * @param namespace namespace value to use to push data to CloudWatch * @param region region used to look for instance tags and to push CloudWatch data * @param instanceId instanceId to use as dimension * @param asgName autoscaling group name to use as dimension * @param ec2Client ec2 client to use in querying tags to find ASG name * @param cloudWatchClient cloud watch client to use to push CloudWatch data */ public ElapsedTimeAggregator(String namespace, Region region, String instanceId, String asgName, AmazonEC2 ec2Client, AmazonCloudWatch cloudWatchClient) { this.region = region; if (instanceId == null) { throw new IllegalStateException("unable to find instance id"); } // get the ASG name if (asgName == null) { ec2Client.setRegion(region); List<TagDescription> tagDescriptions = ec2Client.describeTags(new DescribeTagsRequest().withFilters( new Filter().withName("resource-id").withValues(instanceId), new Filter().withName("key").withValues("aws:autoscaling:groupName"))).getTags(); if (tagDescriptions.size() == 1) { asgName = tagDescriptions.get(0).getValue(); } } if (asgName == null) { log.warn("unable to determine AutoScalingGroupName for " + instanceId + ". No statistics will be published under the AutoScalingGroupName dimension"); } cloudWatchClient.setRegion(region); this.cloudWatchClient = cloudWatchClient; String metricName = "ElapsedTime"; Dimension instanceDimension = new Dimension().withName("InstanceId").withValue(instanceId); StatisticSet statisticSet = new StatisticSet(); // set up the static MetricDatum and associate to the PutMetricDataRequest MetricDatum instanceMetricDatum = new MetricDatum().withMetricName(metricName) .withDimensions(instanceDimension).withStatisticValues(statisticSet) .withUnit(StandardUnit.Milliseconds); putMetricDataRequest = new PutMetricDataRequest().withNamespace(namespace) .withMetricData(instanceMetricDatum); // and a special zero value request since statistic set doesn't // support zero values MetricDatum zeroValueInstanceMetricDatum = new MetricDatum().withMetricName(metricName) .withDimensions(instanceDimension).withValue(0d).withUnit(StandardUnit.Milliseconds); zeroValuePutMetricDataRequest = new PutMetricDataRequest().withNamespace(namespace) .withMetricData(zeroValueInstanceMetricDatum); // also push metrics for the ASG dimension if we have an ASG name if (asgName != null) { Dimension asgDimension = new Dimension().withName("AutoScalingGroupName").withValue(asgName); MetricDatum asgMetricDatum = new MetricDatum().withMetricName(metricName).withDimensions(asgDimension) .withStatisticValues(statisticSet).withUnit(StandardUnit.Milliseconds); putMetricDataRequest.withMetricData(asgMetricDatum); MetricDatum zeroValueAsgMetricDatum = new MetricDatum().withMetricName(metricName) .withDimensions(asgDimension).withValue(0d).withUnit(StandardUnit.Milliseconds); zeroValuePutMetricDataRequest.withMetricData(zeroValueAsgMetricDatum); } }