use of org.jclouds.cloudwatch.domain.GetMetricStatisticsResponse in project legacy-jclouds-examples by jclouds.
the class MainApp method getCPUUtilizationStatsForInstanceOverTheLast24Hours.
/**
* Return an array of doubles with the CPUUtilization {@link EC2Constants.MetricName#CPU_UTILIZATION}
* average, maximum and minimum values in respective order over the last 24 hours.
*
* @param metricClient the {@link MetricClient} to use
* @param nodeId the instance id whose CPUUtilization statistics we're intersted in calculating
*
* @return the array of doubles describe above or null if there are no CPUUtilization metrics stored for the given
* instance id over the past 24 hours
*/
private static double[] getCPUUtilizationStatsForInstanceOverTheLast24Hours(MetricClient metricClient, String nodeId) {
Dimension instanceIdDimension = new Dimension(EC2Constants.Dimension.INSTANCE_ID, nodeId);
ListMetricsOptions lmOptions = ListMetricsOptions.builder().metricName(EC2Constants.MetricName.CPU_UTILIZATION).namespace(Namespaces.EC2).dimension(instanceIdDimension).build();
// Return null to indicate there are no CPUUtilization metrics stored for the given node id
if (Iterators.size(metricClient.listMetrics(lmOptions).iterator()) == 0) {
return null;
}
// Now
Date endDate = new Date();
// One day ago
Date startDate = new Date(endDate.getTime() - (1000 * 60 * 60 * 24));
GetMetricStatistics statistics = GetMetricStatistics.builder().dimension(instanceIdDimension).metricName(EC2Constants.MetricName.CPU_UTILIZATION).namespace(Namespaces.EC2).statistic(Statistics.AVERAGE).statistic(Statistics.MAXIMUM).statistic(Statistics.MINIMUM).startTime(startDate).endTime(endDate).period(3600).unit(Unit.PERCENT).build();
GetMetricStatisticsResponse statisticsResponse = metricClient.getMetricStatistics(statistics);
double avg = 0d;
double max = 0d;
double min = 0d;
Iterator<Datapoint> datapointIterator = statisticsResponse.iterator();
while (datapointIterator.hasNext()) {
Datapoint datapoint = datapointIterator.next();
Double dAvg = datapoint.getAverage();
Double dMax = datapoint.getMaximum();
Double dMin = datapoint.getMinimum();
if (dAvg != null) {
avg = ((avg + dAvg) / 2);
}
if (dMax != null) {
if (dMax > max) {
max = dMax;
}
}
if (dMin != null) {
if (dMin < min) {
min = dMin;
}
}
}
return new double[] { avg, max, min };
}
Aggregations