use of com.netflix.simianarmy.client.aws.AWSClient in project SimianArmy by Netflix.
the class ASGJanitorCrawler method getASGResources.
private List<Resource> getASGResources(String... asgNames) {
AWSClient awsClient = getAWSClient();
List<LaunchConfiguration> launchConfigurations = awsClient.describeLaunchConfigurations();
for (LaunchConfiguration lc : launchConfigurations) {
nameToLaunchConfig.put(lc.getLaunchConfigurationName(), lc);
}
List<Resource> resources = new LinkedList<Resource>();
for (AutoScalingGroup asg : awsClient.describeAutoScalingGroups(asgNames)) {
Resource asgResource = new AWSResource().withId(asg.getAutoScalingGroupName()).withResourceType(AWSResourceType.ASG).withRegion(awsClient.region()).withLaunchTime(asg.getCreatedTime());
for (TagDescription tag : asg.getTags()) {
asgResource.setTag(tag.getKey(), tag.getValue());
}
asgResource.setDescription(String.format("%d instances", asg.getInstances().size()));
asgResource.setOwnerEmail(getOwnerEmailForResource(asgResource));
if (asg.getStatus() != null) {
((AWSResource) asgResource).setAWSResourceState(asg.getStatus());
}
Integer maxSize = asg.getMaxSize();
if (maxSize != null) {
asgResource.setAdditionalField(ASG_FIELD_MAX_SIZE, String.valueOf(maxSize));
}
// Adds instances and ELBs as additional fields.
List<String> instances = new ArrayList<String>();
for (Instance instance : asg.getInstances()) {
instances.add(instance.getInstanceId());
}
asgResource.setAdditionalField(ASG_FIELD_INSTANCES, StringUtils.join(instances, ","));
asgResource.setAdditionalField(ASG_FIELD_ELBS, StringUtils.join(asg.getLoadBalancerNames(), ","));
String lcName = asg.getLaunchConfigurationName();
LaunchConfiguration lc = nameToLaunchConfig.get(lcName);
if (lc != null) {
asgResource.setAdditionalField(ASG_FIELD_LC_NAME, lcName);
}
if (lc != null && lc.getCreatedTime() != null) {
asgResource.setAdditionalField(ASG_FIELD_LC_CREATION_TIME, String.valueOf(lc.getCreatedTime().getTime()));
}
// sets the field for the time when the ASG's traffic is suspended from ELB
for (SuspendedProcess sp : asg.getSuspendedProcesses()) {
if ("AddToLoadBalancer".equals(sp.getProcessName())) {
String suspensionTime = getSuspensionTimeString(sp.getSuspensionReason());
if (suspensionTime != null) {
LOGGER.info(String.format("Suspension time of ASG %s is %s", asg.getAutoScalingGroupName(), suspensionTime));
asgResource.setAdditionalField(ASG_FIELD_SUSPENSION_TIME, suspensionTime);
break;
}
}
}
resources.add(asgResource);
}
return resources;
}
use of com.netflix.simianarmy.client.aws.AWSClient in project SimianArmy by Netflix.
the class EBSVolumeJanitorCrawler method getVolumeResources.
private List<Resource> getVolumeResources(String... volumeIds) {
List<Resource> resources = new LinkedList<Resource>();
AWSClient awsClient = getAWSClient();
for (Volume volume : awsClient.describeVolumes(volumeIds)) {
Resource volumeResource = new AWSResource().withId(volume.getVolumeId()).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.EBS_VOLUME).withLaunchTime(volume.getCreateTime());
for (Tag tag : volume.getTags()) {
LOGGER.info(String.format("Adding tag %s = %s to resource %s", tag.getKey(), tag.getValue(), volumeResource.getId()));
volumeResource.setTag(tag.getKey(), tag.getValue());
}
volumeResource.setOwnerEmail(getOwnerEmailForResource(volumeResource));
volumeResource.setDescription(getVolumeDescription(volume));
((AWSResource) volumeResource).setAWSResourceState(volume.getState());
resources.add(volumeResource);
}
return resources;
}
use of com.netflix.simianarmy.client.aws.AWSClient in project SimianArmy by Netflix.
the class ELBJanitorCrawler method getELBResources.
private List<Resource> getELBResources(String... elbNames) {
List<Resource> resources = new LinkedList<Resource>();
AWSClient awsClient = getAWSClient();
for (LoadBalancerDescription elb : awsClient.describeElasticLoadBalancers(elbNames)) {
Resource resource = new AWSResource().withId(elb.getLoadBalancerName()).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.ELB).withLaunchTime(elb.getCreatedTime());
resource.setOwnerEmail(getOwnerEmailForResource(resource));
resources.add(resource);
List<Instance> instances = elb.getInstances();
if (instances == null || instances.size() == 0) {
resource.setAdditionalField("instanceCount", "0");
resource.setDescription("instances=none");
LOGGER.debug(String.format("No instances found for ELB %s", resource.getId()));
} else {
resource.setAdditionalField("instanceCount", "" + instances.size());
ArrayList<String> instanceList = new ArrayList<String>(instances.size());
LOGGER.debug(String.format("Found %d instances for ELB %s", instances.size(), resource.getId()));
for (Instance instance : instances) {
String instanceId = instance.getInstanceId();
instanceList.add(instanceId);
}
String instancesStr = StringUtils.join(instanceList, ",");
resource.setDescription(String.format("instances=%s", instances));
LOGGER.debug(String.format("Resource ELB %s has instances %s", resource.getId(), instancesStr));
}
for (TagDescription tagDescription : awsClient.describeElasticLoadBalancerTags(resource.getId())) {
for (Tag tag : tagDescription.getTags()) {
LOGGER.debug(String.format("Adding tag %s = %s to resource %s", tag.getKey(), tag.getValue(), resource.getId()));
resource.setTag(tag.getKey(), tag.getValue());
}
}
}
Map<String, List<String>> elbtoASGMap = buildELBtoASGMap();
for (Resource resource : resources) {
List<String> asgList = elbtoASGMap.get(resource.getId());
if (asgList != null && asgList.size() > 0) {
resource.setAdditionalField("referencedASGCount", "" + asgList.size());
String asgStr = StringUtils.join(asgList, ",");
resource.setDescription(resource.getDescription() + ", ASGS=" + asgStr);
LOGGER.debug(String.format("Resource ELB %s is referenced by ASGs %s", resource.getId(), asgStr));
} else {
resource.setAdditionalField("referencedASGCount", "0");
resource.setDescription(resource.getDescription() + ", ASGS=none");
LOGGER.debug(String.format("No ASGs found for ELB %s", resource.getId()));
}
}
return resources;
}
use of com.netflix.simianarmy.client.aws.AWSClient in project SimianArmy by Netflix.
the class InstanceJanitorCrawler method getInstanceResources.
private List<Resource> getInstanceResources(String... instanceIds) {
List<Resource> resources = new LinkedList<Resource>();
AWSClient awsClient = getAWSClient();
Map<String, AutoScalingInstanceDetails> idToASGInstance = new HashMap<String, AutoScalingInstanceDetails>();
for (AutoScalingInstanceDetails instanceDetails : awsClient.describeAutoScalingInstances(instanceIds)) {
idToASGInstance.put(instanceDetails.getInstanceId(), instanceDetails);
}
for (Instance instance : awsClient.describeInstances(instanceIds)) {
Resource instanceResource = new AWSResource().withId(instance.getInstanceId()).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.INSTANCE).withLaunchTime(instance.getLaunchTime());
for (Tag tag : instance.getTags()) {
instanceResource.setTag(tag.getKey(), tag.getValue());
}
String description = String.format("type=%s; host=%s", instance.getInstanceType(), instance.getPublicDnsName() == null ? "" : instance.getPublicDnsName());
instanceResource.setDescription(description);
instanceResource.setOwnerEmail(getOwnerEmailForResource(instanceResource));
String asgName = getAsgName(instanceResource, idToASGInstance);
if (asgName != null) {
instanceResource.setAdditionalField(INSTANCE_FIELD_ASG_NAME, asgName);
LOGGER.info(String.format("instance %s has a ASG tag name %s.", instanceResource.getId(), asgName));
}
String opsworksStackName = getOpsWorksStackName(instanceResource);
if (opsworksStackName != null) {
instanceResource.setAdditionalField(INSTANCE_FIELD_OPSWORKS_STACK_NAME, opsworksStackName);
LOGGER.info(String.format("instance %s is part of an OpsWorks stack named %s.", instanceResource.getId(), opsworksStackName));
}
if (instance.getState() != null) {
((AWSResource) instanceResource).setAWSResourceState(instance.getState().getName());
}
resources.add(instanceResource);
}
return resources;
}
use of com.netflix.simianarmy.client.aws.AWSClient in project SimianArmy by Netflix.
the class InstanceTooOld method getInstanceLaunchTimes.
/**
* Gets the launch time (in milliseconds) for a list of instance ids of the same region. The default
* implementation is using an AWS client. The method can be overridden in subclasses to get the instance
* launch times differently.
* @param region
* the region of the instances
* @param instanceIds
* the instance ids, all instances should be in the same region.
* @return
* the map from instance id to the launch time in milliseconds
*/
protected Map<String, Long> getInstanceLaunchTimes(String region, String... instanceIds) {
Map<String, Long> result = Maps.newHashMap();
if (instanceIds == null || instanceIds.length == 0) {
return result;
}
AWSClient awsClient = new AWSClient(region, awsCredentialsProvider);
for (Instance instance : awsClient.describeInstances(instanceIds)) {
if (instance.getLaunchTime() != null) {
result.put(instance.getInstanceId(), instance.getLaunchTime().getTime());
} else {
LOGGER.warn(String.format("No launch time found for instance %s", instance.getInstanceId()));
}
}
return result;
}
Aggregations