Search in sources :

Example 81 with Resource

use of com.netflix.simianarmy.Resource 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;
}
Also used : HashMap(java.util.HashMap) Instance(com.amazonaws.services.ec2.model.Instance) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) AutoScalingInstanceDetails(com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

Example 82 with Resource

use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.

the class RDSJanitorResourceTracker method addOrUpdate.

/** {@inheritDoc} */
@Override
public void addOrUpdate(Resource resource) {
    Resource orig = getResource(resource.getId(), resource.getRegion());
    LOGGER.debug(String.format("Saving resource %s to RDB table %s in region %s", resource.getId(), table, resource.getRegion()));
    String json;
    try {
        json = new ObjectMapper().writeValueAsString(additionalFieldsAsMap(resource));
    } catch (JsonProcessingException e) {
        LOGGER.error("ERROR generating additional field JSON when saving resource " + resource.getId(), e);
        return;
    }
    if (orig == null) {
        StringBuilder sb = new StringBuilder();
        sb.append("insert into ").append(table);
        sb.append(" (");
        sb.append(AWSResource.FIELD_RESOURCE_ID).append(",");
        sb.append(AWSResource.FIELD_RESOURCE_TYPE).append(",");
        sb.append(AWSResource.FIELD_REGION).append(",");
        sb.append(AWSResource.FIELD_OWNER_EMAIL).append(",");
        sb.append(AWSResource.FIELD_DESCRIPTION).append(",");
        sb.append(AWSResource.FIELD_STATE).append(",");
        sb.append(AWSResource.FIELD_TERMINATION_REASON).append(",");
        sb.append(AWSResource.FIELD_EXPECTED_TERMINATION_TIME).append(",");
        sb.append(AWSResource.FIELD_ACTUAL_TERMINATION_TIME).append(",");
        sb.append(AWSResource.FIELD_NOTIFICATION_TIME).append(",");
        sb.append(AWSResource.FIELD_LAUNCH_TIME).append(",");
        sb.append(AWSResource.FIELD_MARK_TIME).append(",");
        sb.append(AWSResource.FIELD_OPT_OUT_OF_JANITOR).append(",");
        sb.append("additionalFields").append(") values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        LOGGER.debug(String.format("Insert statement is '%s'", sb));
        int updated = this.jdbcTemplate.update(sb.toString(), resource.getId(), value(resource.getResourceType().toString()), value(resource.getRegion()), emailValue(resource.getOwnerEmail()), value(resource.getDescription()), value(resource.getState().toString()), value(resource.getTerminationReason()), value(resource.getExpectedTerminationTime()), value(resource.getActualTerminationTime()), value(resource.getNotificationTime()), value(resource.getLaunchTime()), value(resource.getMarkTime()), value(resource.isOptOutOfJanitor()), json);
        LOGGER.debug(String.format("%d rows inserted", updated));
    } else {
        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(table).append(" set ");
        sb.append(AWSResource.FIELD_RESOURCE_TYPE).append("=?,");
        sb.append(AWSResource.FIELD_REGION).append("=?,");
        sb.append(AWSResource.FIELD_OWNER_EMAIL).append("=?,");
        sb.append(AWSResource.FIELD_DESCRIPTION).append("=?,");
        sb.append(AWSResource.FIELD_STATE).append("=?,");
        sb.append(AWSResource.FIELD_TERMINATION_REASON).append("=?,");
        sb.append(AWSResource.FIELD_EXPECTED_TERMINATION_TIME).append("=?,");
        sb.append(AWSResource.FIELD_ACTUAL_TERMINATION_TIME).append("=?,");
        sb.append(AWSResource.FIELD_NOTIFICATION_TIME).append("=?,");
        sb.append(AWSResource.FIELD_LAUNCH_TIME).append("=?,");
        sb.append(AWSResource.FIELD_MARK_TIME).append("=?,");
        sb.append(AWSResource.FIELD_OPT_OUT_OF_JANITOR).append("=?,");
        sb.append("additionalFields").append("=? where ");
        sb.append(AWSResource.FIELD_RESOURCE_ID).append("=? and ");
        sb.append(AWSResource.FIELD_REGION).append("=?");
        LOGGER.debug(String.format("Update statement is '%s'", sb));
        int updated = this.jdbcTemplate.update(sb.toString(), resource.getResourceType().toString(), value(resource.getRegion()), emailValue(resource.getOwnerEmail()), value(resource.getDescription()), value(resource.getState().toString()), value(resource.getTerminationReason()), value(resource.getExpectedTerminationTime()), value(resource.getActualTerminationTime()), value(resource.getNotificationTime()), value(resource.getLaunchTime()), value(resource.getMarkTime()), value(resource.isOptOutOfJanitor()), json, resource.getId(), resource.getRegion());
        LOGGER.debug(String.format("%d rows updated", updated));
    }
    LOGGER.debug("Successfully saved.");
}
Also used : Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 83 with Resource

use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.

the class RDSJanitorResourceTracker method mapResource.

private Resource mapResource(ResultSet rs) throws SQLException {
    String json = rs.getString("additionalFields");
    Resource resource = null;
    try {
        // put additional fields
        Map<String, String> map = new HashMap<>();
        if (json != null) {
            TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {
            };
            map = new ObjectMapper().readValue(json, typeRef);
        }
        // put everything else
        map.put(AWSResource.FIELD_RESOURCE_ID, rs.getString(AWSResource.FIELD_RESOURCE_ID));
        map.put(AWSResource.FIELD_RESOURCE_TYPE, rs.getString(AWSResource.FIELD_RESOURCE_TYPE));
        map.put(AWSResource.FIELD_REGION, rs.getString(AWSResource.FIELD_REGION));
        map.put(AWSResource.FIELD_DESCRIPTION, rs.getString(AWSResource.FIELD_DESCRIPTION));
        map.put(AWSResource.FIELD_STATE, rs.getString(AWSResource.FIELD_STATE));
        map.put(AWSResource.FIELD_TERMINATION_REASON, rs.getString(AWSResource.FIELD_TERMINATION_REASON));
        map.put(AWSResource.FIELD_OPT_OUT_OF_JANITOR, rs.getString(AWSResource.FIELD_OPT_OUT_OF_JANITOR));
        String email = rs.getString(AWSResource.FIELD_OWNER_EMAIL);
        if (StringUtils.isBlank(email) || email.equals("0")) {
            email = null;
        }
        map.put(AWSResource.FIELD_OWNER_EMAIL, email);
        String expectedTerminationTime = millisToFormattedDate(rs.getString(AWSResource.FIELD_EXPECTED_TERMINATION_TIME));
        String actualTerminationTime = millisToFormattedDate(rs.getString(AWSResource.FIELD_ACTUAL_TERMINATION_TIME));
        String notificationTime = millisToFormattedDate(rs.getString(AWSResource.FIELD_NOTIFICATION_TIME));
        String launchTime = millisToFormattedDate(rs.getString(AWSResource.FIELD_LAUNCH_TIME));
        String markTime = millisToFormattedDate(rs.getString(AWSResource.FIELD_MARK_TIME));
        if (expectedTerminationTime != null) {
            map.put(AWSResource.FIELD_EXPECTED_TERMINATION_TIME, expectedTerminationTime);
        }
        if (actualTerminationTime != null) {
            map.put(AWSResource.FIELD_ACTUAL_TERMINATION_TIME, actualTerminationTime);
        }
        if (notificationTime != null) {
            map.put(AWSResource.FIELD_NOTIFICATION_TIME, notificationTime);
        }
        if (launchTime != null) {
            map.put(AWSResource.FIELD_LAUNCH_TIME, launchTime);
        }
        if (markTime != null) {
            map.put(AWSResource.FIELD_MARK_TIME, markTime);
        }
        resource = AWSResource.parseFieldtoValueMap(map);
    } catch (IOException ie) {
        String msg = "Error parsing resource from result set";
        LOGGER.error(msg, ie);
        throw new SQLException(msg);
    }
    return resource;
}
Also used : SQLException(java.sql.SQLException) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 84 with Resource

use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.

the class EddaASGJanitorCrawler method parseJsonElementToresource.

private Resource parseJsonElementToresource(String region, JsonNode jsonNode, Map<String, Long> lcNameToCreationTime) {
    Validate.notNull(jsonNode);
    String asgName = jsonNode.get("autoScalingGroupName").getTextValue();
    long createdTime = jsonNode.get("createdTime").getLongValue();
    Resource resource = new AWSResource().withId(asgName).withRegion(region).withResourceType(AWSResourceType.ASG).withLaunchTime(new Date(createdTime));
    JsonNode tags = jsonNode.get("tags");
    if (tags == null || !tags.isArray() || tags.size() == 0) {
        LOGGER.debug(String.format("No tags is found for %s", resource.getId()));
    } else {
        for (Iterator<JsonNode> it = tags.getElements(); it.hasNext(); ) {
            JsonNode tag = it.next();
            String key = tag.get("key").getTextValue();
            String value = tag.get("value").getTextValue();
            resource.setTag(key, value);
        }
    }
    String owner = getOwnerEmailForResource(resource);
    if (owner != null) {
        resource.setOwnerEmail(owner);
    }
    JsonNode maxSize = jsonNode.get("maxSize");
    if (maxSize != null) {
        resource.setAdditionalField(ASG_FIELD_MAX_SIZE, String.valueOf(maxSize.getIntValue()));
    }
    // Adds instances and ELBs as additional fields.
    JsonNode instances = jsonNode.get("instances");
    resource.setDescription(String.format("%d instances", instances.size()));
    List<String> instanceIds = Lists.newArrayList();
    for (Iterator<JsonNode> it = instances.getElements(); it.hasNext(); ) {
        instanceIds.add(it.next().get("instanceId").getTextValue());
    }
    resource.setAdditionalField(ASG_FIELD_INSTANCES, StringUtils.join(instanceIds, ","));
    JsonNode elbs = jsonNode.get("loadBalancerNames");
    List<String> elbNames = Lists.newArrayList();
    for (Iterator<JsonNode> it = elbs.getElements(); it.hasNext(); ) {
        elbNames.add(it.next().getTextValue());
    }
    resource.setAdditionalField(ASG_FIELD_ELBS, StringUtils.join(elbNames, ","));
    JsonNode lc = jsonNode.get("launchConfigurationName");
    if (lc != null) {
        String lcName = lc.getTextValue();
        Long lcCreationTime = lcNameToCreationTime.get(lcName);
        if (lcName != null) {
            resource.setAdditionalField(ASG_FIELD_LC_NAME, lcName);
        }
        if (lcCreationTime != null) {
            resource.setAdditionalField(ASG_FIELD_LC_CREATION_TIME, String.valueOf(lcCreationTime));
        }
    }
    // sets the field for the time when the ASG's traffic is suspended from ELB
    JsonNode suspendedProcesses = jsonNode.get("suspendedProcesses");
    for (Iterator<JsonNode> it = suspendedProcesses.getElements(); it.hasNext(); ) {
        JsonNode sp = it.next();
        if ("AddToLoadBalancer".equals(sp.get("processName").getTextValue())) {
            String suspensionTime = getSuspensionTimeString(sp.get("suspensionReason").getTextValue());
            if (suspensionTime != null) {
                LOGGER.info(String.format("Suspension time of ASG %s is %s", asgName, suspensionTime));
                resource.setAdditionalField(ASG_FIELD_SUSPENSION_TIME, suspensionTime);
                break;
            }
        }
    }
    Long lastChangeTime = regionToAsgToLastChangeTime.get(region).get(asgName);
    if (lastChangeTime != null) {
        resource.setAdditionalField(ASG_FIELD_LAST_CHANGE_TIME, String.valueOf(lastChangeTime));
    }
    return resource;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) JsonNode(org.codehaus.jackson.JsonNode) Date(java.util.Date)

Example 85 with Resource

use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.

the class EddaEBSSnapshotJanitorCrawler method getSnapshotResourcesInRegion.

private List<Resource> getSnapshotResourcesInRegion(String region, String... snapshotIds) {
    refreshSnapshotToAMIs(region);
    String url = eddaClient.getBaseUrl(region) + "/aws/snapshots/";
    if (snapshotIds != null && snapshotIds.length != 0) {
        url += StringUtils.join(snapshotIds, ',');
        LOGGER.info(String.format("Getting snapshots in region %s for %d ids", region, snapshotIds.length));
    } else {
        LOGGER.info(String.format("Getting all snapshots in region %s", region));
    }
    url += ";state=completed;_expand:(snapshotId,state,description,startTime,tags,ownerId)";
    JsonNode jsonNode = null;
    try {
        jsonNode = eddaClient.getJsonNodeFromUrl(url);
    } catch (Exception e) {
        LOGGER.error(String.format("Failed to get Jason node from edda for snapshots in region %s.", region), e);
    }
    if (jsonNode == null || !jsonNode.isArray()) {
        throw new RuntimeException(String.format("Failed to get valid document from %s, got: %s", url, jsonNode));
    }
    List<Resource> resources = Lists.newArrayList();
    for (Iterator<JsonNode> it = jsonNode.getElements(); it.hasNext(); ) {
        JsonNode elem = it.next();
        // Filter out shared snapshots that do not have the specified owner id.
        String ownerId = elem.get("ownerId").getTextValue();
        if (defaultOwnerId != null && !defaultOwnerId.equals(ownerId)) {
            LOGGER.info(String.format("Ignoring snapshotIds %s since it does not have the specified ownerId.", elem.get("snapshotId").getTextValue()));
        } else {
            resources.add(parseJsonElementToSnapshotResource(region, elem));
        }
    }
    return resources;
}
Also used : Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) JsonNode(org.codehaus.jackson.JsonNode)

Aggregations

Resource (com.netflix.simianarmy.Resource)145 AWSResource (com.netflix.simianarmy.aws.AWSResource)132 Test (org.testng.annotations.Test)110 TestMonkeyCalendar (com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar)64 DateTime (org.joda.time.DateTime)60 Date (java.util.Date)45 MonkeyCalendar (com.netflix.simianarmy.MonkeyCalendar)21 AWSClient (com.netflix.simianarmy.client.aws.AWSClient)18 JsonNode (org.codehaus.jackson.JsonNode)17 AWSResourceType (com.netflix.simianarmy.aws.AWSResourceType)11 BeforeTest (org.testng.annotations.BeforeTest)9 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)8 LoadBalancerDescription (com.amazonaws.services.elasticloadbalancing.model.LoadBalancerDescription)6 HashSet (java.util.HashSet)6 AutoScalingInstanceDetails (com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails)5 Instance (com.amazonaws.services.ec2.model.Instance)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 RowMapper (org.springframework.jdbc.core.RowMapper)5 Snapshot (com.amazonaws.services.ec2.model.Snapshot)4