Search in sources :

Example 91 with Resource

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

the class EddaInstanceJanitorCrawler method parseJsonElementToInstanceResource.

private Resource parseJsonElementToInstanceResource(String region, JsonNode jsonNode) {
    Validate.notNull(jsonNode);
    String instanceId = jsonNode.get("instanceId").getTextValue();
    long launchTime = jsonNode.get("launchTime").getLongValue();
    Resource resource = new AWSResource().withId(instanceId).withRegion(region).withResourceType(AWSResourceType.INSTANCE).withLaunchTime(new Date(launchTime));
    JsonNode publicDnsName = jsonNode.get("publicDnsName");
    String description = String.format("type=%s; host=%s", jsonNode.get("instanceType").getTextValue(), publicDnsName == null ? "" : publicDnsName.getTextValue());
    resource.setDescription(description);
    String owner = getOwnerEmailForResource(resource);
    resource.setOwnerEmail(owner);
    JsonNode tags = jsonNode.get("tags");
    String asgName = null;
    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);
            if ("aws:autoscaling:groupName".equals(key)) {
                asgName = value;
            } else if (owner == null && BasicSimianArmyContext.GLOBAL_OWNER_TAGKEY.equals(key)) {
                resource.setOwnerEmail(value);
            }
        }
        resource.setDescription(description.toString());
    }
    // If we cannot find ASG name in tags, use the map for the ASG name
    if (asgName == null) {
        asgName = instanceToAsg.get(instanceId);
        if (asgName != null) {
            LOGGER.debug(String.format("Failed to find ASG name in tags of %s, use the ASG name %s from map", instanceId, asgName));
        }
    }
    if (asgName != null) {
        resource.setAdditionalField(InstanceJanitorCrawler.INSTANCE_FIELD_ASG_NAME, asgName);
    }
    ((AWSResource) resource).setAWSResourceState(jsonNode.get("state").get("name").getTextValue());
    String imageId = jsonNode.get("imageId").getTextValue();
    resource.setAdditionalField("imageId", imageId);
    return resource;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) JsonNode(org.codehaus.jackson.JsonNode) Date(java.util.Date)

Example 92 with Resource

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

the class EddaInstanceJanitorCrawler method getInstanceResourcesInRegion.

private List<Resource> getInstanceResourcesInRegion(String region, String... instanceIds) {
    refreshAsgInstances();
    String url = eddaClient.getBaseUrl(region) + "/view/instances;";
    if (instanceIds != null && instanceIds.length != 0) {
        url += StringUtils.join(instanceIds, ',');
        LOGGER.info(String.format("Getting instances in region %s for %d ids", region, instanceIds.length));
    } else {
        LOGGER.info(String.format("Getting all instances in region %s", region));
    }
    url += ";state.name=running;_expand:(instanceId,launchTime,state:(name),instanceType,imageId" + ",publicDnsName,tags:(key,value))";
    JsonNode jsonNode = null;
    try {
        jsonNode = eddaClient.getJsonNodeFromUrl(url);
    } catch (Exception e) {
        LOGGER.error(String.format("Failed to get Jason node from edda for instances 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(); ) {
        resources.add(parseJsonElementToInstanceResource(region, it.next()));
    }
    refreshOwnerByImage(region, resources);
    return resources;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) JsonNode(org.codehaus.jackson.JsonNode)

Example 93 with Resource

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

the class JanitorEmailNotifier method sendNotifications.

/**
     * Gets all the resources that are marked and no notifications have been sent. Send email notifications
     * for these resources. If there is a valid email address in the ownerEmail field of the resource, send
     * to that address. Otherwise send to the default email address.
     */
public void sendNotifications() {
    validateEmails();
    Map<String, Collection<Resource>> emailToResources = new HashMap<String, Collection<Resource>>();
    invalidEmailToResources.clear();
    for (Resource r : getMarkedResources()) {
        if (r.isOptOutOfJanitor()) {
            LOGGER.info(String.format("Resource %s is opted out of Janitor Monkey so no notification is sent.", r.getId()));
            continue;
        }
        if (canNotify(r)) {
            String email = r.getOwnerEmail();
            if (email != null && !email.contains("@") && StringUtils.isNotBlank(this.ownerEmailDomain)) {
                email = String.format("%s@%s", email, this.ownerEmailDomain);
            }
            if (!isValidEmail(email)) {
                if (defaultEmail != null) {
                    LOGGER.info(String.format("Email %s is not valid, send to the default email address %s", email, defaultEmail));
                    putEmailAndResource(emailToResources, defaultEmail, r);
                } else {
                    if (email == null) {
                        email = UNKNOWN_EMAIL;
                    }
                    LOGGER.info(String.format("Email %s is not valid and default email is not set for resource %s", email, r.getId()));
                    putEmailAndResource(invalidEmailToResources, email, r);
                }
            } else {
                putEmailAndResource(emailToResources, email, r);
            }
        } else {
            LOGGER.debug(String.format("Not the time to send notification for resource %s", r.getId()));
        }
    }
    emailBuilder.setEmailToResources(emailToResources);
    Date now = calendar.now().getTime();
    for (Map.Entry<String, Collection<Resource>> entry : emailToResources.entrySet()) {
        String email = entry.getKey();
        String emailBody = emailBuilder.buildEmailBody(email);
        String subject = buildEmailSubject(email);
        sendEmail(email, subject, emailBody);
        for (Resource r : entry.getValue()) {
            LOGGER.debug(String.format("Notification is sent for resource %s", r.getId()));
            r.setNotificationTime(now);
            resourceTracker.addOrUpdate(r);
        }
        LOGGER.info(String.format("Email notification has been sent to %s for %d resources.", email, entry.getValue().size()));
    }
}
Also used : HashMap(java.util.HashMap) Resource(com.netflix.simianarmy.Resource) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) Date(java.util.Date)

Example 94 with Resource

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

the class TestRDSJanitorResourceTracker method testGetResource.

@SuppressWarnings("unchecked")
@Test
public void testGetResource() {
    String id1 = "id-1";
    AWSResourceType resourceType = AWSResourceType.INSTANCE;
    Resource.CleanupState state = Resource.CleanupState.MARKED;
    String description = "This is a test resource.";
    String ownerEmail = "owner@test.com";
    String region = "us-east-1";
    String terminationReason = "This is a test termination reason.";
    DateTime now = DateTime.now();
    Date expectedTerminationTime = new Date(now.plusDays(10).getMillis());
    Date markTime = new Date(now.getMillis());
    String fieldName = "fieldName123";
    String fieldValue = "fieldValue456";
    AWSResource result1 = mkResource(id1, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, false, fieldName, fieldValue);
    ArrayList<AWSResource> resources = new ArrayList<>();
    resources.add(result1);
    TestRDSJanitorResourceTracker tracker = new TestRDSJanitorResourceTracker();
    when(tracker.getJdbcTemplate().query(Matchers.anyString(), Matchers.any(Object[].class), Matchers.any(RowMapper.class))).thenReturn(resources);
    Resource resource = tracker.getResource(id1);
    ArrayList<Resource> returnResources = new ArrayList<>();
    returnResources.add(resource);
    verifyResources(returnResources, id1, null, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, fieldName, fieldValue);
}
Also used : AWSResourceType(com.netflix.simianarmy.aws.AWSResourceType) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) RowMapper(org.springframework.jdbc.core.RowMapper) Test(org.testng.annotations.Test)

Example 95 with Resource

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

the class TestRDSJanitorResourceTracker method testGetResources.

@SuppressWarnings("unchecked")
@Test
public void testGetResources() {
    String id1 = "id-1";
    String id2 = "id-2";
    AWSResourceType resourceType = AWSResourceType.INSTANCE;
    Resource.CleanupState state = Resource.CleanupState.MARKED;
    String description = "This is a test resource.";
    String ownerEmail = "owner@test.com";
    String region = "us-east-1";
    String terminationReason = "This is a test termination reason.";
    DateTime now = DateTime.now();
    Date expectedTerminationTime = new Date(now.plusDays(10).getMillis());
    Date markTime = new Date(now.getMillis());
    String fieldName = "fieldName123";
    String fieldValue = "fieldValue456";
    AWSResource result1 = mkResource(id1, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, false, fieldName, fieldValue);
    AWSResource result2 = mkResource(id2, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, true, fieldName, fieldValue);
    ArrayList<AWSResource> resources = new ArrayList<>();
    resources.add(result1);
    resources.add(result2);
    TestRDSJanitorResourceTracker tracker = new TestRDSJanitorResourceTracker();
    when(tracker.getJdbcTemplate().query(Matchers.anyString(), Matchers.any(Object[].class), Matchers.any(RowMapper.class))).thenReturn(resources);
    verifyResources(tracker.getResources(resourceType, state, region), id1, id2, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, fieldName, fieldValue);
}
Also used : AWSResourceType(com.netflix.simianarmy.aws.AWSResourceType) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) RowMapper(org.springframework.jdbc.core.RowMapper) Test(org.testng.annotations.Test)

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