Example usage for com.amazonaws.services.cloudwatch.model GetMetricStatisticsResult getDatapoints

List of usage examples for com.amazonaws.services.cloudwatch.model GetMetricStatisticsResult getDatapoints

Introduction

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

Prototype


public java.util.List<Datapoint> getDatapoints() 

Source Link

Document

The data points for the specified metric.

Usage

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  ava2s .  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  a v a  2 s  . c  o  m*/

    //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: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 .java  2 s. 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:br.unb.cic.bionimbuz.elasticity.AmazonMonitoring.java

License:Open Source License

public ArrayList<Datapoint> monitoring(String instanceId) {
    final AmazonCloudWatchClient client = client(awsAccessKey, awsSecretKey);
    final GetMetricStatisticsRequest request = request(instanceId);
    final GetMetricStatisticsResult result = result(client, request);

    ArrayList<Datapoint> infos = new ArrayList();

    for (final Datapoint dataPoint : result.getDatapoints()) {
        infos.add(dataPoint);//from w w w . j a  v a 2 s  .c o  m
        //            System.out.println("teste");
    }

    Collections.sort(infos, (o1, o2) -> o1.getTimestamp().compareTo(o2.getTimestamp()));
    System.out.println(infos);

    return infos;
}

From source file:com.appdynamics.extensions.cloudwatch.metricsmanager.MetricsManager.java

License:Apache License

/**
 * Helper method to gather metrics for a particular namespace using certain filter names
 * @param namespace     Name of the namespace
 * @param filterNames   List of filter names (used to filter metrics)
 * @return Map          Map containing metrics for a particular namespace
 *///from   ww w  .j ava2s .c  om
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Map<String, Map<String, List<Datapoint>>> gatherMetricsHelper(final AmazonCloudWatch awsCloudWatch,
        final String namespace, String region, String... filterNames) {
    final Map<String, Map<String, List<Datapoint>>> metrics = new ConcurrentHashMap<String, Map<String, List<Datapoint>>>();

    List<Metric> metricsList = getMetrics(awsCloudWatch, namespace, filterNames);
    logger.info("Available metrics Size across all instances for NameSpace:" + namespace + " Region:" + region
            + " - " + metricsList.size());

    //Each metric is of the form {Namespace: AWS/EC2,MetricName: DiskWriteOps,Dimensions: [{Name: InstanceId,Value: i-b0b31ff2}]}
    if (logger.isDebugEnabled()) {
        logMetricPerInstance(metricsList, namespace, region);
    }

    int count = 0;
    ExecutorCompletionService ecs = new ExecutorCompletionService(workerPool);
    for (final Metric metric : metricsList) {
        List<Dimension> dimensions = metric.getDimensions();
        final String key = dimensions.get(0).getValue();

        if (!metrics.containsKey(key)) {
            metrics.put(key, new ConcurrentHashMap<String, List<Datapoint>>());
        }
        if (!metrics.get(key).containsKey(metric.getMetricName())) {
            final String metricName = metric.getMetricName();
            if (!amazonCloudWatchMonitor.isMetricDisabled(namespace, metricName)) {
                ecs.submit(new Callable() {
                    public Object call() throws Exception {
                        try {
                            GetMetricStatisticsRequest request = createGetMetricStatisticsRequest(namespace,
                                    metricName, getMetricType(namespace, metricName).getTypeName(),
                                    metric.getDimensions());
                            GetMetricStatisticsResult result = awsCloudWatch.getMetricStatistics(request);
                            if (logger.isDebugEnabled()) {
                                logger.debug("Fetching MetricStatistics for NameSpace = " + namespace
                                        + " Metric = " + metric + " Result = " + result);
                            }
                            List<Datapoint> dataPoints = result.getDatapoints();
                            if (dataPoints != null && !dataPoints.isEmpty()) {
                                metrics.get(key).put(metricName, dataPoints);
                            }
                        } catch (Exception e) {
                            //Better to log it here when we have the context rather than in get()
                            logger.error("Error while getting the MetricStatistics for NameSpace =" + namespace
                                    + " Metric " + metric, e);
                        }
                        return null;
                    }
                });
                ++count;
            }
        }
    }

    //Wait until its complete
    for (int i = 0; i < count; i++) {
        try {
            ecs.take().get();
        } catch (InterruptedException e) {
            logger.error("Interrupted exception", e);
        } catch (ExecutionException e) {
            // We are catching the exceptions b4hand, so this will not be executed
            logger.error("ExecutionException in MetricStatistics", e);
        }
    }

    // Logging metricName and datapoints corresponding to each instanceID
    if (logger.isDebugEnabled()) {
        logDataPointsPerInstance(metrics);
    }

    return metrics;
}

From source file:com.appdynamics.extensions.cloudwatch.metricsmanager.metricsmanagerimpl.EC2MetricsManager.java

License:Apache License

/**
  * Gather metrics for AWS/EC2/*from ww  w.j  a v  a 2  s .c o  m*/
  * @return   Map     Map containing metrics
  */
@Override
public Map<String, Map<String, List<Datapoint>>> gatherMetrics(final AmazonCloudWatch awsCloudWatch,
        String region) {
    final Map<String, Map<String, List<Datapoint>>> metrics = new ConcurrentHashMap<String, Map<String, List<Datapoint>>>();

    List<Metric> metricsList = getMetrics(awsCloudWatch, NAMESPACE, "InstanceId");
    logger.info("Available metrics Size across all instances for NameSpace:" + NAMESPACE + " Region:" + region
            + " - " + metricsList.size());

    //Each metric is of the form {Namespace: AWS/EC2,MetricName: DiskWriteOps,Dimensions: [{Name: InstanceId,Value: i-b0b31ff2}]}
    if (logger.isDebugEnabled()) {
        logMetricPerInstance(metricsList, NAMESPACE, region);
    }

    int count = 0;
    ExecutorCompletionService<Object> ecs = new ExecutorCompletionService<Object>(workerPool);
    for (final Metric metric : metricsList) {
        List<Dimension> dimensions = metric.getDimensions();
        final String key = getInstanceName(region, dimensions.get(0).getValue());

        if (!metrics.containsKey(key)) {
            metrics.put(key, new ConcurrentHashMap<String, List<Datapoint>>());
        }
        if (!metrics.get(key).containsKey(metric.getMetricName())) {
            final String metricName = metric.getMetricName();
            if (!amazonCloudWatchMonitor.isMetricDisabled(NAMESPACE, metricName)) {
                ecs.submit(new Callable<Object>() {
                    public Object call() throws Exception {
                        try {
                            GetMetricStatisticsRequest request = createGetMetricStatisticsRequest(NAMESPACE,
                                    metricName, getMetricType(NAMESPACE, metricName).getTypeName(),
                                    metric.getDimensions());
                            GetMetricStatisticsResult result = awsCloudWatch.getMetricStatistics(request);
                            if (logger.isDebugEnabled()) {
                                logger.debug("Fetching MetricStatistics for NameSpace = " + NAMESPACE
                                        + " Metric = " + metric + " Result = " + result);
                            }
                            List<Datapoint> dataPoints = result.getDatapoints();
                            if (dataPoints != null && !dataPoints.isEmpty()) {
                                metrics.get(key).put(metricName, dataPoints);
                            }
                        } catch (Exception e) {
                            //Better to log it here when we have the context rather than in get()
                            logger.error("Error while getting the MetricStatistics for NameSpace =" + NAMESPACE
                                    + " Metric " + metric, e);
                        }
                        return null;
                    }
                });
                ++count;
            }
        }
    }

    //Wait until its complete
    for (int i = 0; i < count; i++) {
        try {
            ecs.take().get();
        } catch (InterruptedException e) {
            logger.error("Interrupted exception", e);
        } catch (ExecutionException e) {
            // We are catching the exceptions b4hand, so this will not be executed
            logger.error("ExecutionException in MetricStatistics", e);
        }
    }

    // Logging metricName and datapoints corresponding to each instanceID
    if (logger.isDebugEnabled()) {
        logDataPointsPerInstance(metrics);
    }

    return metrics;

}

From source file:com.appdynamics.extensions.cloudwatch.metricsmanager.metricsmanagerimpl.ElastiCacheMetricsManager.java

License:Apache License

/**
 * Gather metrics for AWS/ElastiCache//from  w w w  . jav  a2  s . c om
 * @return   Map     Map containing metrics
 */
@Override
public Map gatherMetrics(AmazonCloudWatch awsCloudWatch, String region) {
    List<Metric> metricsList = getMetrics(awsCloudWatch, NAMESPACE, "CacheClusterId", "CacheNodeId");

    //Top level     -- Key = CacheClusterIds,       Value = HashMap of cache nodes
    //Mid level     -- Key = CacheNodeIds,          Value = HashMap of Metrics
    //Bottom level  -- Key = MetricName,            Value = List of datapoints
    Map<String, Map<String, Map<String, List<Datapoint>>>> cacheClusterMetrics = new HashMap<String, Map<String, Map<String, List<Datapoint>>>>();

    for (com.amazonaws.services.cloudwatch.model.Metric metric : metricsList) {
        List<Dimension> dimensions = metric.getDimensions();
        String cacheClusterId = dimensions.get(0).getValue();
        String cacheNodeId = dimensions.get(1).getValue();
        if (!cacheClusterMetrics.containsKey(cacheClusterId)) {
            cacheClusterMetrics.put(cacheClusterId, new HashMap<String, Map<String, List<Datapoint>>>());
        }
        if (!cacheClusterMetrics.get(cacheClusterId).containsKey(cacheNodeId)) {
            cacheClusterMetrics.get(cacheClusterId).put(cacheNodeId, new HashMap<String, List<Datapoint>>());
        }
        if (!cacheClusterMetrics.get(cacheClusterId).get(cacheNodeId).containsKey(metric.getMetricName())) {

            if (!amazonCloudWatchMonitor.isMetricDisabled(NAMESPACE, metric.getMetricName())) {
                GetMetricStatisticsRequest getMetricStatisticsRequest = createGetMetricStatisticsRequest(
                        NAMESPACE, metric.getMetricName(),
                        getMetricType(NAMESPACE, metric.getMetricName()).getTypeName(), dimensions);
                GetMetricStatisticsResult getMetricStatisticsResult = awsCloudWatch
                        .getMetricStatistics(getMetricStatisticsRequest);
                cacheClusterMetrics.get(cacheClusterId).get(cacheNodeId).put(metric.getMetricName(),
                        getMetricStatisticsResult.getDatapoints());
            }
        }
    }
    return cacheClusterMetrics;
}

From source file:com.appdynamics.extensions.cloudwatch.metricsmanager.metricsmanagerimpl.ELBMetricsManager.java

License:Apache License

/**
 * Gather metrics for AWS/ELB/*  w  w w. ja  va  2s.co m*/
 * @return   Map     Map containing metrics
 */
@Override
public Map gatherMetrics(final AmazonCloudWatch awsCloudWatch, String region) {
    List<com.amazonaws.services.cloudwatch.model.Metric> elbMetricsList = getMetrics(awsCloudWatch, NAMESPACE,
            "LoadBalancerName", "AvailabilityZone");

    //Top level     -- Key = LoadBalancerName,      Value = HashMap of availability zones
    //Mid level     -- Key = AvailabilityZoneName,  Value = HashMap of Metrics
    //Bottom level  -- Key = MetricName,            Value = List of datapoints
    final HashMap<String, HashMap<String, HashMap<String, List<Datapoint>>>> elbMetrics = new HashMap<String, HashMap<String, HashMap<String, List<Datapoint>>>>();

    int count = 0;
    ExecutorCompletionService<Object> ecs = new ExecutorCompletionService<Object>(workerPool);

    for (final com.amazonaws.services.cloudwatch.model.Metric metric : elbMetricsList) {
        final List<Dimension> dimensions = metric.getDimensions();
        final String loadBalancerName = dimensions.get(0).getValue();
        final String availabilityZone = dimensions.get(1).getValue();

        if (!elbMetrics.containsKey(loadBalancerName)) {
            elbMetrics.put(loadBalancerName, new HashMap<String, HashMap<String, List<Datapoint>>>());
        }

        if (!elbMetrics.get(loadBalancerName).containsKey(availabilityZone)) {
            elbMetrics.get(loadBalancerName).put(availabilityZone, new HashMap<String, List<Datapoint>>());
        }

        if (!elbMetrics.get(loadBalancerName).get(availabilityZone).containsKey(metric.getMetricName())) {

            if (!amazonCloudWatchMonitor.isMetricDisabled(NAMESPACE, metric.getMetricName())) {
                ecs.submit(new Callable<Object>() {
                    public Object call() throws Exception {
                        try {
                            GetMetricStatisticsRequest getMetricStatisticsRequest = createGetMetricStatisticsRequest(
                                    NAMESPACE, metric.getMetricName(),
                                    getMetricType(NAMESPACE, metric.getMetricName()).getTypeName(), dimensions);
                            GetMetricStatisticsResult getMetricStatisticsResult = awsCloudWatch
                                    .getMetricStatistics(getMetricStatisticsRequest);
                            elbMetrics.get(loadBalancerName).get(availabilityZone).put(metric.getMetricName(),
                                    getMetricStatisticsResult.getDatapoints());

                        } catch (Exception e) {
                            //Better to log it here when we have the context rather than in get()
                            logger.error("Error while getting the MetricStatistics for NameSpace =" + NAMESPACE
                                    + " Metric " + metric, e);
                        }
                        return null;
                    }
                });
                ++count;

            }
        }
    }

    //Wait until its complete
    for (int i = 0; i < count; i++) {
        try {
            ecs.take().get();
        } catch (InterruptedException e) {
            logger.error("Interrupted exception", e);
        } catch (ExecutionException e) {
            // We are catching the exceptions b4hand, so this will not be executed
            logger.error("ExecutionException in MetricStatistics", e);
        }
    }

    return elbMetrics;
}

From source file:com.appdynamics.extensions.cloudwatch.metricsmanager.metricsmanagerimpl.RedshiftMetricsManager.java

License:Apache License

/**
 * Gather metrics for AWS/Redshift/*from   ww w  . j  a  va  2 s .c o  m*/
 * @return   Map     Map containing metrics
 */
@Override
public Map gatherMetrics(AmazonCloudWatch awsCloudWatch, String region) {
    HashMap<String, HashMap<String, HashMap<String, List<Datapoint>>>> redshiftMetrics = new HashMap<String, HashMap<String, HashMap<String, List<Datapoint>>>>();
    List<com.amazonaws.services.cloudwatch.model.Metric> metrics = getMetrics(awsCloudWatch, NAMESPACE,
            "ClusterIdentifier", "NodeID");
    for (com.amazonaws.services.cloudwatch.model.Metric metric : metrics) {
        List<Dimension> dimensions = metric.getDimensions();
        String clusterIdentifier = dimensions.get(1).getValue();
        String nodeID = dimensions.get(0).getValue();
        if (!redshiftMetrics.containsKey(clusterIdentifier)) {
            redshiftMetrics.put(clusterIdentifier, new HashMap<String, HashMap<String, List<Datapoint>>>());
        }
        if (!redshiftMetrics.get(clusterIdentifier).containsKey(nodeID)) {
            redshiftMetrics.get(clusterIdentifier).put(nodeID, new HashMap<String, List<Datapoint>>());
        }
        if (!redshiftMetrics.get(clusterIdentifier).get(nodeID).containsKey(metric.getMetricName())) {

            if (!amazonCloudWatchMonitor.isMetricDisabled(NAMESPACE, metric.getMetricName())) {
                GetMetricStatisticsRequest getMetricStatisticsRequest = createGetMetricStatisticsRequest(
                        NAMESPACE, metric.getMetricName(),
                        getMetricType(NAMESPACE, metric.getMetricName()).getTypeName(), dimensions);
                GetMetricStatisticsResult getMetricStatisticsResult = awsCloudWatch
                        .getMetricStatistics(getMetricStatisticsRequest);
                redshiftMetrics.get(clusterIdentifier).get(nodeID).put(metric.getMetricName(),
                        getMetricStatisticsResult.getDatapoints());
            }
        }
    }

    return redshiftMetrics;
}

From source file:com.cfelde.aws.ddb.management.TableThroughput.java

License:Open Source License

/**
 * Returns number of reads per second.//from   ww w.  j a  v  a  2  s .c  om
 *
 * @return Reads per second
 */
public double getReadCapacity() {
    synchronized (lock) {
        GetMetricStatisticsRequest request = new GetMetricStatisticsRequest();
        request.setStatistics(Collections.singleton("Sum"));
        request.setDimensions(Arrays.asList(new Dimension().withName("TableName").withValue(tableName)));
        request.setNamespace("AWS/DynamoDB");
        request.setStartTime(new Date(System.currentTimeMillis() - (1000L * period * 5)));
        request.setEndTime(new Date(System.currentTimeMillis() + 60000L));
        request.setPeriod(period);
        request.setMetricName("ProvisionedReadCapacityUnits");

        GetMetricStatisticsResult result = client.getMetricStatistics(request);

        if (result.getDatapoints().isEmpty())
            return lastReadCapacity == null ? 0 : lastReadCapacity;

        List<Datapoint> dataPoints = new ArrayList<>(result.getDatapoints());

        Collections.sort(dataPoints, new Comparator<Datapoint>() {
            @Override
            public int compare(Datapoint o1, Datapoint o2) {
                return o2.getTimestamp().compareTo(o1.getTimestamp());
            }
        });

        Datapoint datapoint = dataPoints.get(0);

        lastReadCapacity = datapoint.getSum();

        return lastReadCapacity;
    }
}