Recently, Amazon announced that it’s CloudWatch service went into public beta. I’ve been involved with the private beta of this and the Elastic Load Balancing and Auto Scaling services. I’ve just completed testing of the CloudWatch monitoring service APIs in typica and thought I’d share some of what has been added.
First of all, the Jec2 class has 2 new methods, monitorInstances(..) and unmonitorInstances(..). They do exactly what you’d expect by turning monitoring on or off for one or more instances. What I think more people will use is the new flag on LaunchConfiguration to enable monitoring when you launch an instance. Also, if you describe instances, you’ll get the monitoring status back now also.
The real CloudWatch APIs are in their own package. I did this because it seems like while they are initially released for EC2, they are written to allow monitoring other service also (hence the namespace parameter). The new API has only two methods. The first lets you list the metrics you can query in the second call. To do this, you can use some code like this;
Monitoring mon = new Monitoring(accessId, secretKey); List<Metric> metrix = mon.listMetrics(); for (Metric m : metrix) { System.out.println("name = "+m.getName()+":"+m.getNamespace()); for (Dimension dim : m.getDimensions()) { System.out.println(" "+dim.getName()+": "+dim.getValue()); } }
Once you have an instance or an image you’d like to monitor, you can use some code like this to fetch the data;
List<Statistics> stats = new ArrayList<Statistics>(); stats.add(Statistics.AVERAGE); Map<String, String> dimensions = new HashMap<String, String>(); // can be InstanceId, InstanceType, ImageId dimensions.put("ImageId", "ami-85d037ec"); Date end = new Date(); // that means now end = new Date(end.getTime() + 3600000*5); // need to adjust for GMT Date start = new Date(end.getTime() - 3600000*24); // 1 days ago MetricStatisticsResult result = mon.getMetricStatistics( 60, // must be multiple of 60 stats, // see above "AWS/EC2", dimensions, start, // start of interval end, // end of interval // can be NetworkIn, NetworkOut, DiskReadOps, // DiskWriteOps, DiskReadBytes, DiskWriteBytes, // CPUUtilization "CPUUtilization", StandardUnit.PERCENT, null); System.out.println("metrics label = "+result.getLabel()); for (Datapoint dp : result.getDatapoints()) { System.out.println(dp.getTimestamp().getTime().toString()+ " samples:"+dp.getSamples()+" "+dp.getAverage()+" "+dp.getUnit()); }
[java] metrics label = CPUUtilization [java] Fri May 22 10:56:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 11:42:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 12:55:00 EDT 2009 samples:1.0 1.54 Percent [java] Fri May 22 12:41:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 13:10:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 10:09:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 12:51:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 12:40:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 10:07:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 13:41:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 10:34:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 12:01:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 10:17:00 EDT 2009 samples:1.0 0.39 Percent [java] Fri May 22 11:39:00 EDT 2009 samples:1.0 1.15 Percent [java] Fri May 22 10:06:00 EDT 2009 samples:1.0 0.38 Percent [java] Fri May 22 12:10:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 12:09:00 EDT 2009 samples:1.0 0.76 Percent [java] Fri May 22 13:46:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 10:39:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 12:11:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 12:03:00 EDT 2009 samples:1.0 1.15 Percent [java] Fri May 22 11:32:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 10:44:00 EDT 2009 samples:1.0 0.0 Percent [java] Fri May 22 12:45:00 EDT 2009 samples:1.0 0.0 Percent