use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.
the class SimpleDBJanitorResourceTracker method getResource.
@Override
public Resource getResource(String resourceId) {
Validate.notEmpty(resourceId);
StringBuilder query = new StringBuilder();
query.append(String.format("select * from `%s` where resourceId = '%s'", domain, resourceId));
LOGGER.debug(String.format("Query is '%s'", query));
List<Item> items = querySimpleDBItems(query.toString());
Validate.isTrue(items.size() <= 1);
if (items.size() == 0) {
LOGGER.info(String.format("Not found resource with id %s", resourceId));
return null;
} else {
Resource resource = null;
try {
resource = parseResource(items.get(0));
} catch (Exception e) {
// Ignore the item that cannot be parsed.
LOGGER.error(String.format("SimpleDB item %s cannot be parsed into a resource.", items.get(0)));
}
return resource;
}
}
use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.
the class AWSResource method cloneResource.
@Override
public Resource cloneResource() {
Resource clone = new AWSResource().withActualTerminationTime(getActualTerminationTime()).withDescription(getDescription()).withExpectedTerminationTime(getExpectedTerminationTime()).withId(getId()).withLaunchTime(getLaunchTime()).withMarkTime(getMarkTime()).withNnotificationTime(getNotificationTime()).withOwnerEmail(getOwnerEmail()).withRegion(getRegion()).withResourceType(getResourceType()).withState(getState()).withTerminationReason(getTerminationReason()).withOptOutOfJanitor(isOptOutOfJanitor());
((AWSResource) clone).setAWSResourceState(awsResourceState);
((AWSResource) clone).additionalFields.putAll(additionalFields);
for (String key : this.getAllTagKeys()) {
clone.setTag(key, this.getTag(key));
}
return clone;
}
use of com.netflix.simianarmy.Resource 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.Resource 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.Resource 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;
}
Aggregations