Example usage for com.amazonaws.services.cloudwatch.model Dimension Dimension

List of usage examples for com.amazonaws.services.cloudwatch.model Dimension Dimension

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudwatch.model Dimension Dimension.

Prototype

Dimension

Source Link

Usage

From source file:AwsAuto.java

License:Open Source License

public static void main(String[] args) throws Exception {

    // initialize/determine parameters
    String instanceType = "m3.medium";
    String loadGeneratorAmi = "ami-8ac4e9e0";
    String dataCenterAmi = "ami-349fbb5e";
    String bidPrice = "0.1";
    String loadGeneratorDns = null; // load generator DNS
    String dashboardUrl = null; // the URL where we check our performance
    String testId = null;/*ww w.j  ava 2  s  .co m*/

    // create project tag for instances
    ArrayList<com.amazonaws.services.ec2.model.Tag> loadGeneratorTags = new ArrayList<>();
    com.amazonaws.services.ec2.model.Tag loadGeneratorTag = new com.amazonaws.services.ec2.model.Tag("Project",
            "2.1");
    loadGeneratorTags.add(loadGeneratorTag);

    // create project tag for auto-scaling
    com.amazonaws.services.autoscaling.model.Tag asgTag = new com.amazonaws.services.autoscaling.model.Tag();
    asgTag.setKey("Project");
    asgTag.setValue("2.1");

    // create project tag for load balancer
    ArrayList<com.amazonaws.services.elasticloadbalancing.model.Tag> loadBalancerTags = new ArrayList<>();
    com.amazonaws.services.elasticloadbalancing.model.Tag loadBalancerTag = new com.amazonaws.services.elasticloadbalancing.model.Tag();
    loadBalancerTag.setKey("Project");
    loadBalancerTag.setValue("2.1");
    loadBalancerTags.add(loadBalancerTag);

    /**
     * =========================================================================
     * ================= Create security groups
     * =========================================================================
     * =================
     */

    String loadGeneratorSecurityName = "LoadGeneratorSGP2";
    String allPurposeSecurityName = "AllPurposeSGP2";
    SecurityGroup loadGeneratorSecurityGroup = new SecurityGroup(loadGeneratorSecurityName);
    SecurityGroup allPurposeSecurityGroup = new SecurityGroup(allPurposeSecurityName);

    /**
     * =========================================================================
     * ================= Grant permission and credentials
     * =========================================================================
     * =================
     */

    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider("School").getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (C:\\Users\\Jiabei\\.aws\\credentials), and is in valid format.", e);
    }

    // set region
    AmazonEC2 ec2 = new AmazonEC2Client(credentials);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    ec2.setRegion(usEast1);

    /**
     * =========================================================================
     * ================= Create a load generator and submit password
     * =========================================================================
     * =================
     */

    Requests requestsLg = new Requests(instanceType, loadGeneratorAmi, bidPrice, loadGeneratorSecurityName,
            loadGeneratorTags);
    loadGeneratorDns = requestsLg.submitRequests();
    String submissionUrl = "http://" + loadGeneratorDns
            + "/password?passwd=0WSb4ufhYI7SkxfLWnnIWU0MC1NdcNKT&andrewId=jiabeip";
    sendGET(submissionUrl);

    try {

        /**
         * =======================================================================
         * =================== Create a load balancer
         * =======================================================================
         * ===================
         */

        String loadBalancerDns = null;
        String loadBalancerName = "LoadBalancerProj2";
        String healthCheckPage = "/heartbeat?lg=" + loadGeneratorDns;
        AmazonElasticLoadBalancingClient loadBalancerClient = new AmazonElasticLoadBalancingClient(credentials);

        // configure a request
        CreateLoadBalancerRequest loadBalancerRequest = new CreateLoadBalancerRequest()
                .withAvailabilityZones("us-east-1b").withListeners(new Listener("HTTP", 80, 80))
                .withLoadBalancerName(loadBalancerName).withSecurityGroups(allPurposeSecurityGroup.getGroupId())
                .withTags(loadBalancerTags);

        CreateLoadBalancerResult loadBalancerResult = loadBalancerClient
                .createLoadBalancer(loadBalancerRequest);
        loadBalancerDns = loadBalancerResult.getDNSName();

        // configure health check setting
        HealthCheck loadBalancerHealthCheck = new HealthCheck().withTarget("HTTP:80" + healthCheckPage)
                .withTimeout(5).withInterval(30).withUnhealthyThreshold(2).withHealthyThreshold(10);

        ConfigureHealthCheckRequest healthCheckRequest = new ConfigureHealthCheckRequest()
                .withLoadBalancerName(loadBalancerName).withHealthCheck(loadBalancerHealthCheck);

        // attach health check setting to load balancer
        ConfigureHealthCheckResult healthCheckResult = loadBalancerClient
                .configureHealthCheck(healthCheckRequest);

        System.out.println("Load balancer created!\nDNS: " + loadBalancerDns);

        /**
         * =======================================================================
         * =================== Create launch configuration
         * =======================================================================
         * ===================
         */

        String launchConfigName = "LaunchConfigProj2";
        String autoScalingGroupName = "AutoScalingGroupProj2";

        AmazonAutoScalingClient autoScalingGroupClient = new AmazonAutoScalingClient(credentials);
        AmazonCloudWatchClient cloudWatchClient = new AmazonCloudWatchClient(credentials);

        System.out.println("Creating launch configuration...");

        // configure the request
        CreateLaunchConfigurationRequest launchConfigRequest = new CreateLaunchConfigurationRequest()
                .withImageId(dataCenterAmi).withInstanceType(instanceType)
                .withLaunchConfigurationName(launchConfigName)
                .withSecurityGroups(allPurposeSecurityGroup.getGroupId()).withSpotPrice(bidPrice)
                .withKeyName("primary");

        // enable detail monitoring
        InstanceMonitoring monitor = new InstanceMonitoring();
        monitor.setEnabled(true);
        launchConfigRequest.setInstanceMonitoring(monitor);

        // attach the configuration to the ASG client
        autoScalingGroupClient.createLaunchConfiguration(launchConfigRequest);

        System.out.println("Configuration complete!\nCreating auto-scaling group...");

        /**
         * =======================================================================
         * =================== Create auto-scaling group
         * =======================================================================
         * ===================
         */

        // configure ASG request
        CreateAutoScalingGroupRequest asgRequest = new CreateAutoScalingGroupRequest()
                .withAutoScalingGroupName(autoScalingGroupName).withAvailabilityZones("us-east-1b")
                .withLoadBalancerNames(loadBalancerName).withNewInstancesProtectedFromScaleIn(false)
                .withTags(asgTag).withDefaultCooldown(120).withMinSize(1).withMaxSize(8).withDesiredCapacity(1) // Start
                // from
                // one
                .withHealthCheckGracePeriod(120).withHealthCheckType("ELB")
                .withLaunchConfigurationName(launchConfigName);

        // attach group configuration to ASG client
        autoScalingGroupClient.createAutoScalingGroup(asgRequest);

        /**
         * =======================================================================
         * =================== Create scaling up policy for ASG
         * =======================================================================
         * ===================
         */

        StepAdjustment upRule1 = new StepAdjustment().withMetricIntervalLowerBound(0.0)
                .withMetricIntervalUpperBound(25.0).withScalingAdjustment(1);

        StepAdjustment upRule2 = new StepAdjustment().withMetricIntervalLowerBound(25.0)
                .withMetricIntervalUpperBound(30.0).withScalingAdjustment(2);

        StepAdjustment upRule3 = new StepAdjustment().withMetricIntervalLowerBound(30.0)
                .withMetricIntervalUpperBound(null).withScalingAdjustment(3);

        String upPolicyName = "Scaling Up";

        PutScalingPolicyRequest scalingUpPolicy = new PutScalingPolicyRequest()
                .withAdjustmentType("ChangeInCapacity").withPolicyType("StepScaling")
                .withStepAdjustments(upRule1, upRule2, upRule3).withAutoScalingGroupName(autoScalingGroupName)
                .withPolicyName(upPolicyName).withEstimatedInstanceWarmup(120);

        StepAdjustment downRule1 = new StepAdjustment().withMetricIntervalLowerBound(-20.0)
                .withMetricIntervalUpperBound(-0.0).withScalingAdjustment(-1);

        StepAdjustment downRule2 = new StepAdjustment().withMetricIntervalLowerBound(-30.0)
                .withMetricIntervalUpperBound(-20.0).withScalingAdjustment(-2);

        StepAdjustment downRule3 = new StepAdjustment().withMetricIntervalLowerBound(null)
                .withMetricIntervalUpperBound(-30.0).withScalingAdjustment(-3);

        String downPolicyName = "Scaling Down";

        PutScalingPolicyRequest scalingDownPolicy = new PutScalingPolicyRequest().withAdjustmentType("")
                .withPolicyType("ChangeInCapacity").withStepAdjustments(downRule1, downRule2, downRule3)
                .withAutoScalingGroupName(autoScalingGroupName).withPolicyName(downPolicyName)
                .withEstimatedInstanceWarmup(60);

        // attach policies to ASG and get ARN for setting alarm
        PutScalingPolicyResult scaleUpResult = autoScalingGroupClient.putScalingPolicy(scalingUpPolicy);
        String upArn = scaleUpResult.getPolicyARN();
        PutScalingPolicyResult scaleDownResult = autoScalingGroupClient.putScalingPolicy(scalingDownPolicy);
        String downArn = scaleDownResult.getPolicyARN();

        /**
         * =======================================================================
         * =================== Create alarms for policies
         * =======================================================================
         * ===================
         */

        String upAlarmName = "UpAlarm_Mild";
        String downAlarmName = "DownAlarm_Mild";
        Dimension dimension = new Dimension();
        dimension.setName("AutoScalingGroupName");
        dimension.setValue(autoScalingGroupName);

        PutMetricAlarmRequest upAlarmRequest = new PutMetricAlarmRequest().withAlarmName(upAlarmName)
                .withMetricName("CPUUtilization").withNamespace("AWS/EC2").withDimensions(dimension)
                .withStatistic(Statistic.SampleCount)
                .withComparisonOperator(ComparisonOperator.GreaterThanOrEqualToThreshold).withThreshold(65.0)
                .withEvaluationPeriods(1).withPeriod(120).withAlarmActions(upArn);

        PutMetricAlarmRequest downAlarmRequest = new PutMetricAlarmRequest().withAlarmName(downAlarmName)
                .withNamespace("AWS/EC2").withDimensions(dimension).withMetricName("CPUUtilization")
                .withStatistic(Statistic.Average)
                .withComparisonOperator(ComparisonOperator.LessThanOrEqualToThreshold).withThreshold(60.0)
                .withEvaluationPeriods(1).withPeriod(120).withAlarmActions(downArn);

        cloudWatchClient.putMetricAlarm(upAlarmRequest);
        cloudWatchClient.putMetricAlarm(downAlarmRequest);

        System.out.println("All settings complete! \nReady for warmup...");

        /**
         * =======================================================================
         * =================== Warm up ELB
         * =======================================================================
         * ===================
         */

        String warmUpUrl = "http://" + loadGeneratorDns + "/warmup?dns=" + loadBalancerDns;
        sendGET(warmUpUrl);
        System.out.println("Warmup link: " + warmUpUrl);

        try {
            Thread.sleep(15 * 60 * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Warmup complete!\nReady for battle...");

        /**
         * =======================================================================
         * =================== Start the test
         * =======================================================================
         * ===================
         */

        String testUrl = "http://" + loadGeneratorDns + "/junior?dns=" + loadBalancerDns;
        testId = sendGET(testUrl);
        System.out.println("Test ID is: " + testId);

        System.out.println("Test link: " + testUrl);

        try {
            Thread.sleep(60 * 60 * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Test finished!");

        /**
         * =======================================================================
         * =================== Delete resources
         * =======================================================================
         * ===================
         */

        // clear ASG
        UpdateAutoScalingGroupRequest asgClearRequest = new UpdateAutoScalingGroupRequest()
                .withAutoScalingGroupName(autoScalingGroupName).withMaxSize(0).withMinSize(0);
        autoScalingGroupClient.updateAutoScalingGroup(asgClearRequest);

        // delete policies
        DeletePolicyRequest upPolicyDeleteRequest = new DeletePolicyRequest()
                .withAutoScalingGroupName(autoScalingGroupName).withPolicyName(upPolicyName);
        autoScalingGroupClient.deletePolicy(upPolicyDeleteRequest);

        DeletePolicyRequest downPolicyDeleteRequest = new DeletePolicyRequest()
                .withAutoScalingGroupName(autoScalingGroupName).withPolicyName(downPolicyName);
        autoScalingGroupClient.deletePolicy(downPolicyDeleteRequest);
        System.out.println("Policies deleted...");

        // delete alarms
        DeleteAlarmsRequest alarmsDeleteRequest = new DeleteAlarmsRequest().withAlarmNames(upAlarmName,
                downAlarmName);
        cloudWatchClient.deleteAlarms(alarmsDeleteRequest);
        System.out.println("Alarms deleted...");

        // delete load balancer
        DeleteLoadBalancerRequest lgDeleteRequest = new DeleteLoadBalancerRequest()
                .withLoadBalancerName(loadBalancerName);
        loadBalancerClient.deleteLoadBalancer(lgDeleteRequest);
        System.out.println("Load generator deleted...");

        // delete ASG
        DeleteAutoScalingGroupRequest asgDeleteRequest = new DeleteAutoScalingGroupRequest()
                .withForceDelete(true).withAutoScalingGroupName(autoScalingGroupName);
        autoScalingGroupClient.deleteAutoScalingGroup(asgDeleteRequest);
        System.out.println("ASG deleted...");

        // delete launch configuration
        DeleteLaunchConfigurationRequest launchConfigDeleteRequest = new DeleteLaunchConfigurationRequest()
                .withLaunchConfigurationName(launchConfigName);
        autoScalingGroupClient.deleteLaunchConfiguration(launchConfigDeleteRequest);
        System.out.println("Launch configuration deleted...");

        // delete security group
        DeleteSecurityGroupRequest sgDeleteRequest = new DeleteSecurityGroupRequest()
                .withGroupName(allPurposeSecurityName);
        ec2.deleteSecurityGroup(sgDeleteRequest);
        System.out.println("Security group deleted...");

        /**
         * =======================================================================
         * =================== All done
         * =======================================================================
         * ===================
         */
        System.out.println("All done :)");

    } catch (AmazonServiceException ase) {
        // Write out any exceptions that may have occurred.
        System.out.println("Caught Exception: " + ase.getMessage());
        System.out.println("Reponse Status Code: " + ase.getStatusCode());
        System.out.println("Error Code: " + ase.getErrorCode());
        System.out.println("Request ID: " + ase.getRequestId());

    } catch (Exception e) {
        System.out.println("Unexcepted error!");
        System.out.println(e.getMessage());
    }

}

From source file:HW1.java

License:Open Source License

public static void main(String[] args) throws Exception {

    AWSCredentials credentials = new PropertiesCredentials(
            HW1.class.getResourceAsStream("AwsCredentials.properties"));

    /*********************************************
     * /*from w w w .j  a v a 2 s  . c  o  m*/
     *  #1 Create Amazon Client object
     *  
     *********************************************/
    System.out.println("#1 Create Amazon Client object");
    ec2 = new AmazonEC2Client(credentials);
    ec2.setEndpoint("https://us-east-1.ec2.amazonaws.com");

    System.out.println("Please enter required group name and key name... (consider them to be the same)");
    Scanner scan = new Scanner(System.in);
    final String keyGroupName = scan.nextLine();

    /* create security group */
    CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest();
    createSecurityGroupRequest.withGroupName(keyGroupName).withDescription("My Java Security Group");
    CreateSecurityGroupResult createSecurityGroupResult = ec2.createSecurityGroup(createSecurityGroupRequest);

    /* set ip settings */
    IpPermission ipPermission = new IpPermission();
    /* authorize tcp, ssh 22 */
    ipPermission.withIpRanges("0.0.0.0/0").withIpProtocol("tcp").withFromPort(22)
            /* authorize http 80 */
            .withToPort(80);

    AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest();
    authorizeSecurityGroupIngressRequest.withGroupName(keyGroupName).withIpPermissions(ipPermission);
    ec2.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);

    /* create key pair */
    CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest();
    createKeyPairRequest.withKeyName(keyGroupName);
    CreateKeyPairResult createKeyPairResult = ec2.createKeyPair(createKeyPairRequest);
    KeyPair keyPair = new KeyPair();
    keyPair = createKeyPairResult.getKeyPair();
    String privateKey = keyPair.getKeyMaterial();
    PrintWriter file = new PrintWriter("/Users/will/.ssh/" + keyGroupName + ".pem");
    file.print(privateKey);
    file.close();
    Runtime.getRuntime().exec("chmod 400 /Users/will/.ssh/" + keyGroupName + ".pem");

    try {

        /*********************************************
         * 
         *  #2 Create two Instances
         *  
         *********************************************/
        System.out.println();
        System.out.println("#2 Create two new Instances");
        int ready_num = 0;
        String insDNS1 = new String();
        String insDNS2 = new String();
        String insId1 = new String();
        String insId2 = new String();
        String insZone1 = new String();
        String insZone2 = new String();

        String imageId = "ami-76f0061f"; //Basic 32-bit Amazon Linux AMI
        int minInstanceCount = 2; // create 2 instance
        int maxInstanceCount = 2;

        /* create instances */
        RunInstancesRequest rir = new RunInstancesRequest(imageId, minInstanceCount, maxInstanceCount);
        rir.withKeyName(keyGroupName).withSecurityGroups(keyGroupName);
        ec2.runInstances(rir);

        /* waiting for instance to start */
        System.out.println("Created instance, wait for pending...");

        DescribeInstancesResult describeInstancesRequest;
        List<Reservation> reservations;
        List<Instance> allInstances = new ArrayList<Instance>();

        while (ready_num < 2) {
            describeInstancesRequest = ec2.describeInstances();
            reservations = describeInstancesRequest.getReservations();
            for (Reservation reservation : reservations) {
                for (Instance ins : reservation.getInstances()) {
                    if (ins.getState().getName().compareTo("running") == 0
                            && ins.getPublicIpAddress() != null) {
                        if (allInstances.size() == 0 || (allInstances.size() > 0
                                && allInstances.get(0).getInstanceId().compareTo(ins.getInstanceId()) != 0)) {
                            ready_num++;
                            allInstances.add(ins);
                        }
                    }
                }
            }
        }

        System.out.println("You have " + allInstances.size() + " Amazon EC2 instance(s).");
        insId1 = allInstances.get(0).getInstanceId();
        insId2 = allInstances.get(1).getInstanceId();
        insDNS1 = allInstances.get(0).getPublicIpAddress();
        insDNS2 = allInstances.get(1).getPublicIpAddress();
        insZone1 = allInstances.get(0).getPlacement().getAvailabilityZone();
        insZone2 = allInstances.get(1).getPlacement().getAvailabilityZone();

        for (Instance ins : allInstances) {
            System.out.println("New instance has been created: " + ins.getInstanceId());
        }

        System.out.println("Both instances are running now:");
        System.out.println("Instance id1: " + insId1);
        System.out.println("IP: " + insDNS1);
        System.out.println("Zone: " + insZone1);
        System.out.println("Instance id1: " + insId2);
        System.out.println("IP: " + insDNS2);
        System.out.println("Zone: " + insZone2);
        System.out.println();

        /*********************************************
         *  #3 Check OR Create two volumes
         *********************************************/
        System.out.println();
        System.out.println("#3 Create volumes");
        String volume_name1 = createVolume(insZone1, null);
        String volume_name2 = createVolume(insZone2, null);

        /*********************************************
         *  #4 Attach the volume to the instance
         *********************************************/
        System.out.println();
        System.out.println("#4 Attach the volume to the instance");
        System.out.println("Wait for volumes to be available...");
        Thread.sleep(20000);

        /* attach instances to existing volume */
        attachVolume(insId1, volume_name1);
        attachVolume(insId2, volume_name2);

        /************************************************
        *  #5 S3 bucket and object
        ***************************************************/
        System.out.println();
        System.out.println("#5 S3 bucket and object");
        s3 = new AmazonS3Client(credentials);

        /* create bucket */
        String bucketName = "cloud-hw1-bucket";
        s3.createBucket(bucketName);

        /* set key */
        String key = "object-hw1.txt";

        /* set value */
        File new_file = File.createTempFile("temp", ".txt");
        new_file.deleteOnExit();
        Writer writer = new OutputStreamWriter(new FileOutputStream(new_file));
        writer.write("This is the file stored on the S3 storage on the first day!!!.");
        writer.close();

        /* put object - bucket, key, value(file) */
        s3.putObject(new PutObjectRequest(bucketName, key, new_file));
        System.out.println("Successfully put file temp.txt to S3, we will read it tomorrow...");
        System.out.println();

        /***********************************
        *   #3 Monitoring (CloudWatch)
        *********************************/
        System.out.println();
        System.out.println("#6 set up cloudwatch");
        try {
            /* create CloudWatch client */
            AmazonCloudWatchClient cloudWatch = new AmazonCloudWatchClient(credentials);
            /* create request message1 */
            GetMetricStatisticsRequest statRequest1 = new GetMetricStatisticsRequest();
            GetMetricStatisticsRequest statRequest2 = new GetMetricStatisticsRequest();
            /* set up request message */
            statRequest1.setNamespace("AWS/EC2"); //namespace
            statRequest2.setNamespace("AWS/EC2"); //namespace
            statRequest1.setPeriod(60); //period of data
            statRequest2.setPeriod(60); //period of data
            ArrayList<String> stats = new ArrayList<String>();
            /* Use one of these strings: Average, Maximum, Minimum, SampleCount, Sum */
            stats.add("Average");
            stats.add("Sum");
            statRequest1.setStatistics(stats);
            statRequest2.setStatistics(stats);
            /* Use one of these strings: CPUUtilization, NetworkIn, NetworkOut, DiskReadBytes, DiskWriteBytes, DiskReadOperations  */
            statRequest1.setMetricName("CPUUtilization");
            statRequest2.setMetricName("CPUUtilization");
            /* set time */
            GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
            calendar.add(GregorianCalendar.SECOND, -1 * calendar.get(GregorianCalendar.SECOND)); // 1 second ago
            Date endTime = calendar.getTime();
            calendar.add(GregorianCalendar.MINUTE, -10); // 10 minutes ago
            Date startTime = calendar.getTime();
            statRequest1.setStartTime(startTime);
            statRequest1.setEndTime(endTime);
            statRequest2.setStartTime(startTime);
            statRequest2.setEndTime(endTime);
            /* specify an instance */
            ArrayList<Dimension> dimensions1 = new ArrayList<Dimension>();
            dimensions1.add(new Dimension().withName("InstanceId").withValue(insId1));
            ArrayList<Dimension> dimensions2 = new ArrayList<Dimension>();
            dimensions2.add(new Dimension().withName("InstanceId").withValue(insId2));
            statRequest1.setDimensions(dimensions1);
            statRequest2.setDimensions(dimensions2);
            System.out.println("Set up cloud watch for instance: " + insId1 + " and instance: " + insId2);

            /* !!!!!!!!!!!!here set for 10 loops for now */
            /* get statistics */
            for (int i = 0; i < 10; i++) {
                GetMetricStatisticsResult statResult1 = cloudWatch.getMetricStatistics(statRequest1);
                GetMetricStatisticsResult statResult2 = cloudWatch.getMetricStatistics(statRequest2);
                /* display */
                System.out.println("Instance 1: " + statResult1.toString());
                List<Datapoint> dataList = statResult1.getDatapoints();
                Double averageCPU = null;
                Date timeStamp = null;
                for (Datapoint d : dataList) {
                    averageCPU = d.getAverage();
                    timeStamp = d.getTimestamp();
                    System.out
                            .println("Instance 1 average CPU utlilization for last 10 minutes: " + averageCPU);
                    System.out.println("Instance 1 total CPU utlilization for last 10 minutes: " + d.getSum());
                }
                System.out.println();
                System.out.println("Instance 2: " + statResult1.toString());
                dataList = statResult2.getDatapoints();
                for (Datapoint d : dataList) {
                    averageCPU = d.getAverage();
                    timeStamp = d.getTimestamp();
                    System.out
                            .println("Instance 2 average CPU utlilization for last 10 minutes: " + averageCPU);
                    System.out.println("Instance 2 total CPU utlilization for last 10 minutes: " + d.getSum());
                }
            }

        } catch (AmazonServiceException ase) {
            System.out.println("Caught Exception: " + ase.getMessage());
            System.out.println("Reponse Status Code: " + ase.getStatusCode());
            System.out.println("Error Code: " + ase.getErrorCode());
            System.out.println("Request ID: " + ase.getRequestId());
        }

        /***********************************
        *   # Copy script to 
        *       instance and run
        *********************************/
        System.out.println();
        System.out.println("Waiting for init and automatically SSH...");
        /* call runtime exec to run scp */
        execCmdRuby(insDNS1, keyGroupName);

        /***********************************
        *   # Save instances to image
        *********************************/
        System.out.println();
        System.out.println("******* Approaching 5:00 pm, create ami for instances *********");
        String imageId1;
        String imageId2;
        String snapshot1;
        String snapshot2;

        imageId1 = createAmiFromInstance(insId1, "image1", true);
        imageId2 = createAmiFromInstance(insId2, "image2", true);
        System.out.println("Created first image id: " + imageId1);
        System.out.println("Created second image id: " + imageId2);

        snapshot1 = createSnapShotFromVolume(volume_name1);
        snapshot2 = createSnapShotFromVolume(volume_name2);
        System.out.println("Created first snapshot id: " + snapshot1);
        System.out.println("Created second snapshot id: " + snapshot2);

        /*********************************************
         * 
         *  # Stop Instances
         *  
         *********************************************/
        System.out.println();
        System.out.println("#7 Stop & terminate the Instance");
        List<String> instanceIds = new LinkedList<String>();
        instanceIds.add(insId1);
        instanceIds.add(insId2);
        /* stop instances */
        StopInstancesRequest stopIR = new StopInstancesRequest(instanceIds);
        ec2.stopInstances(stopIR);
        TerminateInstancesRequest tir = new TerminateInstancesRequest(instanceIds);
        ec2.terminateInstances(tir);

        /*********************************************
         * 
         *  # Detach volumes
         *  
         *********************************************/
        System.out.println();
        System.out.println("Detach the volumes from the instances...");
        deatchVolume(insId1, volume_name1);
        deatchVolume(insId2, volume_name2);

        /*********************************************
          * 
          *  # Delete Volumes
          *  
          *********************************************/
        System.out.println();

        while (true) {
            if (getVolumeState(volume_name1).compareTo("available") == 0
                    && getVolumeState(volume_name2).compareTo("available") == 0)
                break;
        }
        System.out.println("Delete volumes...");
        Thread.sleep(10000);
        deleteVolume(volume_name1);
        deleteVolume(volume_name2);

        /*********************************************
          * 
          *  # Second day restore instances and volumes
          *  
          *********************************************/
        System.out.println();
        System.out.println("#8 Second day start up instances from stored amis...");
        String newInsId1 = "";
        String newInsId2 = "";
        String newInsIP1 = "";
        String newInsIP2 = "";
        String newInsZone1 = "";
        String newInsZone2 = "";

        newInsId1 = createInstanceFromImageId(imageId1, keyGroupName);
        newInsId2 = createInstanceFromImageId(imageId2, keyGroupName);
        System.out.println("Second day first instance has been restored with id: " + newInsId1);
        System.out.println("Second day second instance has been restored with id: " + newInsId2);
        newInsZone1 = getInstanceZone(newInsId1);
        newInsZone2 = getInstanceZone(newInsId2);
        System.out.println("New instance 1 zone: " + newInsZone1);
        System.out.println("New instance 2 zone: " + newInsZone2);
        newInsIP1 = getInstanceIP(newInsId1);
        newInsIP2 = getInstanceIP(newInsId2);
        System.out.println("New instance 1 IP: " + newInsIP1);
        System.out.println("New instance 2 IP: " + newInsIP2);

        Thread.sleep(120000);
        /* exec read */
        System.out.println();
        System.out.println("Now start to read the file stored yesterday...");
        execCmdRead(newInsIP1, keyGroupName);

        /*********************************************
         *  
         *  #9 Read data from S3
         *  
         *********************************************/

        /* get the object from the first day */
        System.out.println();
        System.out.println("#9 Reading data from S3 stored on the first day");
        S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
        BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent()));
        String data = null;
        while ((data = reader.readLine()) != null) {
            System.out.println(data);
        }

        /*********************************************
         *  
         *  #10 shutdown client object
         *  
         *********************************************/
        System.out.println("#10 shutdown client objects");
        ec2.shutdown();
        s3.shutdown();

    } catch (AmazonServiceException ase) {
        System.out.println("Caught Exception: " + ase.getMessage());
        System.out.println("Reponse Status Code: " + ase.getStatusCode());
        System.out.println("Error Code: " + ase.getErrorCode());
        System.out.println("Request ID: " + ase.getRequestId());
    }

}

From source file:virtualIT.java

License:Open Source License

private void cloudWatchMonitor(int userId) throws InterruptedException {

    String instanceId = mapUserInst.get(userId);
    //create request message
    GetMetricStatisticsRequest statRequest = new GetMetricStatisticsRequest();

    //set up request message
    statRequest.setNamespace("AWS/EC2"); //namespace
    statRequest.setPeriod(60); //period of data
    ArrayList<String> stats = new ArrayList<String>();

    //Use one of these strings: Average, Maximum, Minimum, SampleCount, Sum 
    stats.add("Average");
    stats.add("Sum");
    statRequest.setStatistics(stats);//from w w w.  j ava 2s  . c  om

    //Use one of these strings: CPUUtilization, NetworkIn, NetworkOut, DiskReadBytes, DiskWriteBytes, DiskReadOperations  
    statRequest.setMetricName("CPUUtilization");

    // set time
    GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    calendar.add(GregorianCalendar.SECOND, -1 * calendar.get(GregorianCalendar.SECOND)); // 1 second ago
    Date endTime = calendar.getTime();
    calendar.add(GregorianCalendar.MINUTE, -10); // 10 minutes ago
    Date startTime = calendar.getTime();
    statRequest.setStartTime(startTime);
    statRequest.setEndTime(endTime);

    //specify an instance
    ArrayList<Dimension> dimensions = new ArrayList<Dimension>();
    dimensions.add(new Dimension().withName("InstanceId").withValue(instanceId));
    statRequest.setDimensions(dimensions);

    //get statistics
    cloudWatch.getMetricStatistics(statRequest);
    Thread.currentThread().sleep(1000000);
    GetMetricStatisticsResult statResult = cloudWatch.getMetricStatistics(statRequest);

    //display
    System.out.println(statResult.toString());
    List<Datapoint> dataList = statResult.getDatapoints();
    Double averageCPU = null;
    Date timeStamp = null;
    for (Datapoint data : dataList) {
        averageCPU = data.getAverage();
        timeStamp = data.getTimestamp();
        System.out.println("Average CPU utlilization for last 10 minutes: " + averageCPU);
        System.out.println("Totl CPU utlilization for last 10 minutes: " + data.getSum());

    }

    if (!mapUserCpu.containsKey(userId)) {
        mapUserCpu.put(userId, averageCPU);
    } else {
        mapUserCpu.remove(userId);
        /**************
         * update 
         * 
         /
         */
        mapUserCpu.put(userId, averageCPU);
    }

}

From source file:virtualIT.java

License:Open Source License

private static void createTriggers(int userId) {
    System.out.println("Creating Trigger");
    Dimension dimension = new Dimension();
    dimension.setName("AutoScalingGroupName");
    dimension.setValue(virtualIT.autoScalingGroupName);

    PutMetricAlarmRequest putMetricBusyAlarmRequest = new PutMetricAlarmRequest();
    putMetricBusyAlarmRequest.withAlarmName(BUSY_ALARM_NAME)
            .withComparisonOperator("GreaterThanOrEqualToThreshold").withUnit("Seconds")
            .withEvaluationPeriods(EVALUATION_PERIOD).withMetricName(METRIC_NAME).withNamespace(NAMESPACE)
            .withPeriod(EC2_METRIC_SUBMIT_PERIOD).withStatistic(STATISTIC).withThreshold(THRESHOLD)
            .withAlarmActions(SCALE_UP_POLICY_ARN).withDimensions(dimension);

    PutMetricAlarmRequest putMetricIdleAlarmRequest = new PutMetricAlarmRequest();
    putMetricIdleAlarmRequest.withAlarmName(IDLE_ALARM_NAME)
            .withComparisonOperator("LessThanOrEqualToThreshold").withUnit("Seconds")
            .withEvaluationPeriods(EVALUATION_PERIOD).withMetricName(METRIC_NAME).withNamespace(NAMESPACE)
            .withPeriod(EC2_METRIC_SUBMIT_PERIOD).withStatistic(STATISTIC).withThreshold(THRESHOLD)
            .withAlarmActions(SCALE_DOWN_POLICY_ARN).withDimensions(dimension);

    cloudWatch.putMetricAlarm(putMetricBusyAlarmRequest);
    cloudWatch.putMetricAlarm(putMetricIdleAlarmRequest);

}

From source file:Assignment1.java

License:Open Source License

public static void main(String[] args) throws Exception {

    AWSCredentials credentials = new PropertiesCredentials(
            Assignment1.class.getResourceAsStream("AwsCredentials.properties"));

    /*********************************************
     *  #1 Create Amazon Client object/*from   w w  w .  ja  v  a 2s  .  c  o  m*/
     **********************************************/
    ec2 = new AmazonEC2Client(credentials);

    // We assume that we've already created an instance. Use the id of the instance.
    //String instanceId = "i-4e6c2a3d"; //put your own instance id to test this code.

    try {

        CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest();

        createSecurityGroupRequest.withGroupName("mini").withDescription("My Java Security Group");

        CreateSecurityGroupResult createSecurityGroupResult = ec2
                .createSecurityGroup(createSecurityGroupRequest);

        IpPermission ipPermission = new IpPermission();

        ipPermission.withIpRanges("0.0.0.0/0", "150.150.150.150/32").withIpProtocol("tcp").withFromPort(22)
                .withToPort(22);
        AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest();

        authorizeSecurityGroupIngressRequest.withGroupName("mini").withIpPermissions(ipPermission);
        ec2.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);

        CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest();

        createKeyPairRequest.withKeyName("E3instance_key");

        CreateKeyPairResult createKeyPairResult = ec2.createKeyPair(createKeyPairRequest);

        KeyPair keyPair = new KeyPair();
        keyPair = createKeyPairResult.getKeyPair();

        String privateKey = keyPair.getKeyMaterial();

        System.out.print(privateKey);

        /*********************************************
         *                 
         *  #1.1 Describe Key Pair
         *                 
         *********************************************/
        System.out.println("\n#1.1 Describe Key Pair");
        DescribeKeyPairsResult dkr = ec2.describeKeyPairs();
        System.out.println(dkr.toString());

        /*********************************************
         * 
         *  #1.2 Create an Instance
         *  
         *********************************************/

        RunInstancesRequest runInstancesRequest = new RunInstancesRequest();

        runInstancesRequest.withImageId("ami-ab844dc2").withInstanceType("t1.micro").withMinCount(2)
                .withMaxCount(2).withKeyName("E3instance_key").withSecurityGroups("mini");
        RunInstancesResult runInstancesResult = ec2.runInstances(runInstancesRequest);

        System.out.println("\n#1.2 Create an Instance");

        List<Instance> resultInstance = runInstancesResult.getReservation().getInstances();

        String createdInstanceId = null;

        for (Instance ins : resultInstance) {
            createdInstanceId = ins.getInstanceId();
            System.out.println("New instance has been created: " + ins.getInstanceId());
        }

        String myinstanceZone = resultInstance.get(0).getPlacement().getAvailabilityZone();
        String myinstanceZone1 = resultInstance.get(1).getPlacement().getAvailabilityZone();
        String myinstanceID = resultInstance.get(0).getInstanceId();
        String myinstanceID1 = resultInstance.get(1).getInstanceId();

        Thread.sleep(1000 * 1 * 60);

        /*********************************************
         *  #2.1 Create a volume
         *********************************************/
        //create a volume
        CreateVolumeRequest cvr = new CreateVolumeRequest();
        CreateVolumeRequest cvr1 = new CreateVolumeRequest();
        cvr.setAvailabilityZone(myinstanceZone);
        cvr1.setAvailabilityZone(myinstanceZone1);
        cvr.setSize(10); //size = 10 gigabytes
        cvr1.setSize(10);
        CreateVolumeResult volumeResult = ec2.createVolume(cvr);
        CreateVolumeResult volumeResult1 = ec2.createVolume(cvr1);
        String createdVolumeId = volumeResult.getVolume().getVolumeId();
        String createdVolumeId1 = volumeResult1.getVolume().getVolumeId();
        String[] volumeID = new String[2];
        volumeID[0] = createdVolumeId;
        volumeID[1] = createdVolumeId1;
        System.out.println("\n#2.1 Create a volume for each instance");

        Thread.sleep(1000 * 1 * 60);
        /*********************************************
         *  #2.2 Attach the volume to the instance
         *********************************************/
        AttachVolumeRequest avr = new AttachVolumeRequest();
        AttachVolumeRequest avr1 = new AttachVolumeRequest();
        avr.setInstanceId(myinstanceID);
        avr1.setInstanceId(myinstanceID1);
        avr.setVolumeId(createdVolumeId);
        avr1.setVolumeId(createdVolumeId1);
        avr.setDevice("/dev/sda2");
        avr1.setDevice("/dev/sda2");
        //avr.setVolumeId(createdVolumeId);
        //avr.setInstanceId(createdInstanceId);
        //avr.setDevice("/dev/sdf");
        ec2.attachVolume(avr);
        ec2.attachVolume(avr1);
        System.out.println("\n#2.2 Attach the volume");
        System.out.println("EBS volume has been attached and the volume ID is: " + createdVolumeId);
        System.out.println("EBS volume has been attached and the volume ID is: " + createdVolumeId1);

        Thread.sleep(1000 * 2 * 60);
        /***********************************
        *   #2.3 Monitoring (CloudWatch)
        *********************************/

        //create CloudWatch client
        AmazonCloudWatchClient cloudWatch = new AmazonCloudWatchClient(credentials);

        //create request message
        GetMetricStatisticsRequest statRequest = new GetMetricStatisticsRequest();

        //set up request message
        statRequest.setNamespace("AWS/EC2"); //namespace
        statRequest.setPeriod(60); //period of data
        ArrayList<String> stats = new ArrayList<String>();

        //Use one of these strings: Average, Maximum, Minimum, SampleCount, Sum 
        stats.add("Average");
        stats.add("Sum");
        statRequest.setStatistics(stats);

        //Use one of these strings: CPUUtilization, NetworkIn, NetworkOut, DiskReadBytes, DiskWriteBytes, DiskReadOperations  
        statRequest.setMetricName("CPUUtilization");

        // set time
        GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
        calendar.add(GregorianCalendar.SECOND, -1 * calendar.get(GregorianCalendar.SECOND)); // 1 second ago
        Date endTime = calendar.getTime();
        calendar.add(GregorianCalendar.MINUTE, -10); // 10 minutes ago
        Date startTime = calendar.getTime();
        statRequest.setStartTime(startTime);
        statRequest.setEndTime(endTime);

        //specify an instance
        ArrayList<Dimension> dimensions = new ArrayList<Dimension>();

        String monitorInstanceId = null;
        int i = 0;
        String[] idleInstance = new String[2];

        for (Instance ins : resultInstance) {
            monitorInstanceId = ins.getInstanceId();
            dimensions.add(new Dimension().withName("InstanceId").withValue(monitorInstanceId));
            statRequest.setDimensions(dimensions);

            Thread.sleep(1000 * 3 * 60);
            //get statistics
            GetMetricStatisticsResult statResult = cloudWatch.getMetricStatistics(statRequest);

            //display
            System.out.println(statResult.toString());
            List<Datapoint> dataList = statResult.getDatapoints();
            Double averageCPU = null;
            Date timeStamp = null;
            for (Datapoint data : dataList) {
                averageCPU = data.getAverage();
                timeStamp = data.getTimestamp();
                System.out.println("Average CPU utlilization for last 1 minutes: " + averageCPU);
                //System.out.println("Total CPU utlilization for last 1 minutes: "+data.getSum());

                //Calendar vmTime=GregorianCalendar.getInstance();
                //vmTime.setTime(timeStamp);
                //vmTime.get(Calendar.HOUR_OF_DAY);
                if (averageCPU < 50 && i < 2) {
                    idleInstance[i] = monitorInstanceId;
                    i++;
                }
            }

        }
        System.out.println("\n" + i + " instance(s) idling.");
        /*********************************************
         *  #2.4 Detach the volume from the instance 
         *********************************************/

        DetachVolumeRequest dvr = new DetachVolumeRequest();
        DetachVolumeRequest dvr1 = new DetachVolumeRequest();
        dvr.setVolumeId(createdVolumeId);
        dvr1.setVolumeId(createdVolumeId1);
        dvr.setInstanceId(myinstanceID);
        dvr1.setInstanceId(myinstanceID1);
        dvr.setDevice("/dev/sda2");
        dvr1.setDevice("/dev/sda2");
        ec2.detachVolume(dvr);
        ec2.detachVolume(dvr1);
        System.out.println("\n#2.4 Detach the volume");

        Thread.sleep(1000 * 1 * 60);

        /*********************************************
          *  #2.5 Create new AMI for idle instance
          *********************************************/
        String[] idleAMIID = new String[2];
        int j = 0;
        for (j = 0; j < idleInstance.length; j++) {
            CreateImageRequest Im = new CreateImageRequest(idleInstance[j], "image" + j);

            //CreateImageRequest Im1=new CreateImageRequest(myinstanceID1, "image1");
            Im.setInstanceId(idleInstance[j]);
            //Im1.setInstanceId(myinstanceID1);

            CreateImageResult myAMI = ec2.createImage(Im);
            idleAMIID[j] = myAMI.getImageId();

            //CreateImageResult myAMI1= ec2.createImage(Im1); 
            System.out.println("\n#2.5 Create new AMI");
        }
        Thread.sleep(1000 * 1 * 60);
        /*********************************************
          * 
          *  # Terminate an Instance
          *  
          *********************************************/
        //System.out.println("#8 Terminate the Instance");

        // TerminateInstancesRequest tir = new TerminateInstancesRequest(instanceIds);

        //ec2.terminateInstances(tir);
        /*********************************************
          *  #2.6 Create new VMs
          *********************************************/
        RunInstancesRequest runNewInstancesRequest = new RunInstancesRequest();
        int m;
        String[] newCreatedInstanceId = new String[2];
        for (m = 0; m < j; m++)//j is the number of AMI created
        {
            runNewInstancesRequest.withImageId(idleAMIID[m]).withInstanceType("t1.micro").withMinCount(1)
                    .withMaxCount(1).withKeyName("E3instance_key").withSecurityGroups("mini");
            RunInstancesResult runNewInstancesResult = ec2.runInstances(runNewInstancesRequest);
            List<Instance> newResultInstance = runNewInstancesResult.getReservation().getInstances();

            String newInstanceId = null;

            for (Instance ins : newResultInstance) {
                newInstanceId = ins.getInstanceId();
            }
            newCreatedInstanceId[m] = newInstanceId;
            System.out.println("Using AMI, a new instance has been created: " + newCreatedInstanceId[m]);

        }
        Thread.sleep(1000 * 1 * 60);
        //System.out.println("\n#2.6 Create "+ m + " instance using AMI");

        /*********************************************
          *  #2.7 Attach the volume to the new instance
          *********************************************/
        int n;
        for (n = 0; n < idleInstance.length; n++) {
            AttachVolumeRequest new_avr = new AttachVolumeRequest();
            //AttachVolumeRequest new_avr1 = new AttachVolumeRequest();
            new_avr.setInstanceId(newCreatedInstanceId[n]);
            //avr1.setInstanceId(myinstanceID1);
            new_avr.setVolumeId(volumeID[n]);
            //avr1.setVolumeId(createdVolumeId1);
            new_avr.setDevice("/dev/sda2");
            //avr1.setDevice("/dev/sda2");
            //avr.setVolumeId(createdVolumeId);
            //avr.setInstanceId(createdInstanceId);
            //avr.setDevice("/dev/sdf");
            ec2.attachVolume(new_avr);
            //ec2.attachVolume(avr1);
            System.out.println("\n#2.7 Re-attach the volume");
            System.out.println("EBS volume has been attached and the volume ID is: " + volumeID[n]);
            //System.out.println("EBS volume has been attached and the volume ID is: "+createdVolumeId1);

            Thread.sleep(1000 * 1 * 60);
        }
        /************************************************
        *    #3 S3 bucket and object
        ***************************************************/
        s3 = new AmazonS3Client(credentials);

        //create bucket
        String bucketName = "lucinda.duan";
        s3.createBucket(bucketName);

        //set key
        String key = "object-name.txt";

        //set value
        File file = File.createTempFile("temp", ".txt");
        file.deleteOnExit();
        Writer writer = new OutputStreamWriter(new FileOutputStream(file));
        writer.write("This is a sample sentence.\r\nYes!");
        writer.close();

        //put object - bucket, key, value(file)
        s3.putObject(new PutObjectRequest(bucketName, key, file));

        //get object
        S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
        BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent()));
        String data = null;
        while ((data = reader.readLine()) != null) {
            System.out.println(data);
        }

        /*********************************************
         *  #4 shutdown client object
         *********************************************/
        // ec2.shutdown();
        // s3.shutdown();

    } catch (AmazonServiceException ase) {
        System.out.println("Caught Exception: " + ase.getMessage());
        System.out.println("Reponse Status Code: " + ase.getStatusCode());
        System.out.println("Error Code: " + ase.getErrorCode());
        System.out.println("Request ID: " + ase.getRequestId());
    }

}

From source file:aws.example.cloudwatch.PutMetricAlarm.java

License:Open Source License

public static void main(String[] args) {

    final String USAGE = "To run this example, supply an alarm name and instance id\n"
            + "Ex: DeleteAlarm <alarm-name> <instance-id>\n";

    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);/*from w  w w.j  a  va  2 s  .  c  o  m*/
    }

    String alarmName = args[0];
    String instanceId = args[1];

    final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient();

    Dimension dimension = new Dimension().withName("InstanceId").withValue(instanceId);

    PutMetricAlarmRequest request = new PutMetricAlarmRequest().withAlarmName(alarmName)
            .withComparisonOperator(ComparisonOperator.GreaterThanThreshold).withEvaluationPeriods(1)
            .withMetricName("CPUUtilization").withNamespace("AWS/EC2").withPeriod(60)
            .withStatistic(Statistic.Average).withThreshold(70.0).withActionsEnabled(false)
            .withAlarmDescription("Alarm when server CPU utilization exceeds 70%")
            .withUnit(StandardUnit.Seconds).withDimensions(dimension);

    PutMetricAlarmResult response = cw.putMetricAlarm(request);

    System.out.printf("Successfully created alarm with name %s", alarmName);

}

From source file:aws.example.cloudwatch.PutMetricData.java

License:Open Source License

public static void main(String[] args) {

    final String USAGE = "To run this example, supply a data point:\n" + "Ex: PutMetricData <data_point>\n";

    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);/*from  w ww .  j  a va  2 s  .c o  m*/
    }

    Double data_point = Double.parseDouble(args[0]);

    final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient();

    Dimension dimension = new Dimension().withName("UNIQUE_PAGES").withValue("URLS");

    MetricDatum datum = new MetricDatum().withMetricName("PAGES_VISITED").withUnit(StandardUnit.None)
            .withValue(data_point).withDimensions(dimension);

    PutMetricDataRequest request = new PutMetricDataRequest().withNamespace("SITE/TRAFFIC")
            .withMetricData(datum);

    PutMetricDataResult response = cw.putMetricData(request);

    System.out.printf("Successfully put data point %f", data_point);
}

From source file:be.dataminded.nifi.plugins.PutCloudWatchCountMetricAndAlarm.java

License:Apache License

@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();//from w  w  w.  j  a va 2 s. c o m
    if (flowFile == null) {
        return;
    }

    long totalTableCount = 0;
    long sumCount = 0;
    String tableName = "";
    String schemaName = "";
    String source = "";
    String tenantName = "";

    try (InputStream inputStream = session.read(flowFile)) {

        StringWriter writer = new StringWriter();
        IOUtils.copy(inputStream, writer, "UTF-8");
        String flowFileContent = writer.toString();

        // The MergeContent controller will be configured to append the JSON content with commas
        // We have to surround this list with square brackets to become a valid JSON Array
        String jsonContent = "[" + flowFileContent + "]";

        JSONArray jsonArray = new JSONArray(jsonContent);

        Iterator iterator = jsonArray.iterator();

        ArrayList<Long> counts = new ArrayList<>();

        while (iterator.hasNext()) {
            JSONObject o = (JSONObject) iterator.next();
            counts.add(o.getLong(context.getProperty(NAME_ELEMENT_TO_SUM).getValue()));
        }
        sumCount = counts.stream().mapToLong(Long::longValue).sum();

        JSONObject firstElement = (JSONObject) jsonArray.get(0);
        totalTableCount = firstElement.getLong(context.getProperty(NAME_ELEMENT_TOTAL_COUNT).getValue());
        tableName = firstElement.getString(TABLE_NAME);
        schemaName = firstElement.getString(SCHEMA_NAME);
        source = firstElement.getString(SOURCE_NAME);
        tenantName = firstElement.getString(TENANT_NAME);

    } catch (IOException e) {
        logger.error("Something went wrong when trying to read the flowFile body: " + e.getMessage());
    } catch (org.json.JSONException e) {
        logger.error("Something when trying to parse the JSON body of the flowFile: " + e.getMessage());
    } catch (Exception e) {
        logger.error("something else went wrong in body processing of this FlowFile: " + e.getMessage());
        session.transfer(flowFile, REL_FAILURE);
    }

    try {

        String environment = context.getProperty(ENVIRONMENT).getValue();
        String alarmPrefix = context.getProperty(NAME_PREFIX_ALARM).getValue();

        Map<String, Long> metrics = new HashMap<>();
        // first metric: this is the total count of the records that were exported
        metrics.put("COUNT_", sumCount);
        // second metric: this is the difference between the records exported
        // and the total amount of records counted in the DB, should always be 0 !!!
        // we take a margin into account because we can't be sure there won't be any deletes
        // between counting and executing the queries
        long diff = Math.abs(totalTableCount - sumCount);
        double diffProcent = Math.round((diff / totalTableCount) * 1000);
        metrics.put("DIFF_", (long) diffProcent);

        ArrayList<Dimension> dimensions = new ArrayList<>();
        dimensions.add(new Dimension().withName("tableName").withValue(tableName));
        dimensions.add(new Dimension().withName("tenantName").withValue(tenantName));
        dimensions.add(new Dimension().withName("sourceName").withValue(source));
        dimensions.add(new Dimension().withName("schemaName").withValue(schemaName));
        dimensions.add(new Dimension().withName("environment").withValue(environment));

        for (Map.Entry<String, Long> metric : metrics.entrySet()) {
            MetricDatum datum = new MetricDatum();
            datum.setMetricName(metric.getKey() + tableName);
            datum.setValue((double) metric.getValue());
            datum.setUnit("Count");
            datum.setDimensions(dimensions);

            final PutMetricDataRequest metricDataRequest = new PutMetricDataRequest().withNamespace("NIFI")
                    .withMetricData(datum);

            putMetricData(metricDataRequest);
        }

        // the alarm we create is a static one that will check if the diff is zero
        String comparisonOperator = context.getProperty(ALARM_COMPARISON_OPERATOR).getValue();
        String alarmStatistic = context.getProperty(ALARM_STATISTIC).getValue();
        String alarmPeriod = context.getProperty(ALARM_PERIOD).getValue();
        String alarmEvaluatePeriods = context.getProperty(ALARM_EVALUATE_PERIODS).getValue();
        String alarmAction = context.getProperty(ALARM_ACTION).getValue();

        PutMetricAlarmRequest putMetricAlarmRequest = new PutMetricAlarmRequest()
                .withMetricName("DIFF_" + tableName)
                .withAlarmName(environment + "_" + alarmPrefix + "_" + "DIFF_" + tableName)
                .withDimensions(dimensions).withComparisonOperator(comparisonOperator).withNamespace("NIFI")
                .withStatistic(alarmStatistic).withPeriod(Integer.parseInt(alarmPeriod))
                .withEvaluationPeriods(Integer.parseInt(alarmEvaluatePeriods)).withThreshold((double) 0)
                //.withTreatMissingData("notBreaching") // aws java SDK has to be upgraded for this
                .withAlarmDescription("The daily Count Alarm for table " + tableName).withActionsEnabled(true)
                .withAlarmActions(alarmAction);
        putAlarmData(putMetricAlarmRequest);

        session.transfer(flowFile, REL_SUCCESS);
        getLogger().info("Successfully published cloudwatch metric for {}", new Object[] { flowFile });
    } catch (final Exception e) {
        getLogger().error("Failed to publish cloudwatch metric for {} due to {}", new Object[] { flowFile, e });
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
    }

}

From source file:br.unb.cic.bionimbuz.elasticity.AmazonMonitoring.java

License:Open Source License

public GetMetricStatisticsRequest request(final String instanceId) {
    final long start = 1000 * 60 * 60 * 1;
    final int period = 60 * 15;
    return new GetMetricStatisticsRequest().withStartTime(new Date(new Date().getTime() - start))
            .withNamespace("AWS/EC2").withPeriod(period)
            .withDimensions(new Dimension().withName("InstanceId").withValue(instanceId))
            .withMetricName("CPUUtilization").withStatistics("Average", "Maximum").withEndTime(new Date());
}

From source file:cloudwatch.src.main.java.aws.example.cloudwatch.PutMetricAlarm.java

License:Open Source License

public static void main(String[] args) {

    final String USAGE = "To run this example, supply an alarm name and instance id\n"
            + "Ex: DeleteAlarm <alarm-name> <instance-id>\n";

    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);/*w ww  . jav  a 2  s . c om*/
    }

    String alarmName = args[0];
    String instanceId = args[1];

    final AmazonCloudWatch cloudWatch = AmazonCloudWatchClientBuilder.defaultClient();

    Dimension dimension = new Dimension().withName("InstanceId").withValue(instanceId);

    PutMetricAlarmRequest request = new PutMetricAlarmRequest().withAlarmName(alarmName)
            .withComparisonOperator(ComparisonOperator.GreaterThanThreshold).withEvaluationPeriods(1)
            .withMetricName("CPUUtilization").withNamespace("AWS/EC2").withPeriod(60)
            .withStatistic(Statistic.Average).withThreshold(70.0).withActionsEnabled(false)
            .withAlarmDescription("Alarm when server CPU utilization exceeds 70%")
            .withUnit(StandardUnit.Seconds).withDimensions(dimension);

    PutMetricAlarmResult response = cloudWatch.putMetricAlarm(request);

    System.out.printf("Successfully created alarm with name %s", alarmName);

}