Friday, October 4, 2013

AWS JAVA client examples for ELB and RDS metrics (Asynchronous)

When I started working on the AWS JAVA client for fetching the ELB and RDS metrics, I could not find many examples on the internet. So here I am putting down some sample code snippets.

So first you need to create a Client object


AWSCredentials credentials = new BasicAWSCredentials(AWS_ACCESS_KEY,AWS_SECRET_KEY);
AmazonCloudWatchAsyncClient client = new AmazonCloudWatchAsyncClient(credentials);

ELB:

Now creating an init object . Note that we are getting an aggregation of 5 mins. Which means that we are asking for one data point every five minutes and since time range is 5 mins, we would get exactly one data point every time we invoke this method

private static GetMetricStatisticsRequest initializeRequestObject(AmazonCloudWatchAsyncClient client){ 
        GetMetricStatisticsRequest request   = new GetMetricStatisticsRequest();
         
        request.setPeriod(60*5); // 5 minutes 
         
        request.setNamespace("AWS/ELB"); 
         
        List<Dimension> dims      = new ArrayList<Dimension>(); 
        Dimension dim       = new Dimension(); 
        dim.setName("LoadBalancerName");  
        dim.setValue("NAME");  
        dims.add(dim); 
         
        Date end = new Date(); 
        request.setEndTime(end); 

        // Back up 5 minutes 
        Date beg = new Date(end.getTime() - 5*60*1000); 
        request.setStartTime(beg); 
        request.setDimensions(dims); 
        return request; 
    } 


Now fetching data for individual metrics:

ELB 4XX:


public static void get5MinELB4XX(AmazonCloudWatchAsyncClient client){ 
        client.setEndpoint("monitoring.ap-southeast-1.amazonaws.com"); // endpoints are listed at http://docs.aws.amazon.com/general/latest/gr/rande.html
        GetMetricStatisticsRequest request = initializeRequestObject(client); 
         
        request.setMetricName("HTTPCode_ELB_4XX"); 
        request.setUnit(StandardUnit.Count); 
         
        List<String> stats  = new ArrayList<String>(); 
        stats.add("Sum"); 
        request.setStatistics(stats); 
         
        client.getMetricStatisticsAsync(request, new AsyncHandler<GetMetricStatisticsRequest, GetMetricStatisticsResult>() { 
             
            @Override 
            public void onSuccess(GetMetricStatisticsRequest arg0,
                    GetMetricStatisticsResult arg1) { 
                List<Datapoint> data = arg1.getDatapoints(); 
                // Do something with this data here 
            } 
             
            @Override 
            public void onError(Exception arg0) {
                // log an error 
            } 
        }); 
        return; 
         
    }

ELB Latency:


public static void get5MinLatency(AmazonCloudWatchAsyncClient client){ 
        client.setEndpoint("monitoring.ap-southeast-1.amazonaws.com"); // endpoints are listed at http://docs.aws.amazon.com/general/latest/gr/rande.html
        GetMetricStatisticsRequest request = initializeRequestObject(client); 
         
        request.setMetricName("Latency"); 
        request.setUnit(StandardUnit.Seconds); 
         
        List<String> stats     = new ArrayList<String>(); 
        stats.add("Average"); 
        stats.add("Maximum"); 
        stats.add("Minimum"); 
        request.setStatistics(stats); 
         
        client.getMetricStatisticsAsync(request, new AsyncHandler<GetMetricStatisticsRequest, GetMetricStatisticsResult>() { 
             
            @Override 
            public void onSuccess(GetMetricStatisticsRequest arg0,
                    GetMetricStatisticsResult arg1) { 
                List<Datapoint> data = arg1.getDatapoints(); 
                // Do something with this data here  
            } 
             
            @Override 
            public void onError(Exception arg0) {
                 // log an error  
            } 
        }); 
        return; 
    }

ELB Request Count:

public static void get5MinRequestCount(AmazonCloudWatchAsyncClient client){ 
        client.setEndpoint("monitoring.ap-southeast-1.amazonaws.com"); // endpoints are listed at http://docs.aws.amazon.com/general/latest/gr/rande.html
        GetMetricStatisticsRequest request = initializeRequestObject(client); 
         
        request.setMetricName("RequestCount"); 
        request.setUnit(StandardUnit.Count); 
         
        List<String> stats = new ArrayList<String>(); 
        stats.add("Sum"); 
        request.setStatistics(stats); 
         
        client.getMetricStatisticsAsync(request, new AsyncHandler<GetMetricStatisticsRequest, GetMetricStatisticsResult>() { 
             
            @Override 
            public void onSuccess(GetMetricStatisticsRequest arg0,
                    GetMetricStatisticsResult arg1) { 
                List<Datapoint> data = arg1.getDatapoints(); 
                // Do something with this data here  
                 
            } 
             
            @Override 
            public void onError(Exception arg0) {
                // log an error  
            } 
        }); 
        return; 
    }

RDS:

CPU Utilization:


public static void get5MinCPUUtilization(AmazonCloudWatchAsyncClient client){ 
        client.setEndpoint("monitoring.ap-southeast-1.amazonaws.com"); // endpoints are listed at http://docs.aws.amazon.com/general/latest/gr/rande.html
        GetMetricStatisticsRequest request = initializeRequestObject(client); 
         
        request.setMetricName("CPUUtilization"); 
        request.setUnit(StandardUnit.Percent); 
         
        List<String> stats     = new ArrayList<String>(); 
        stats.add("Average"); 
        stats.add("Maximum"); 
        stats.add("Minimum"); 
        request.setStatistics(stats); 
         
        client.getMetricStatisticsAsync(request, new AsyncHandler<GetMetricStatisticsRequest, GetMetricStatisticsResult>() { 
             
            @Override 
            public void onSuccess(GetMetricStatisticsRequest arg0,
                    GetMetricStatisticsResult arg1) { 
                List<Datapoint> data = arg1.getDatapoints(); 
                // Do something with this data here  
            } 
             
            @Override 
            public void onError(Exception arg0) {
                // log an error 
            } 
        }); 
        return; 
    }

Similarly for other metrics of RDS you just need to chose the correct metric name and its unit.

1 comment:

  1. Nice work, your blog is concept-oriented, kindly share more blogs like this
    AWS Online Training

    ReplyDelete