我正在try 使用Java SDK 1.x从CloudWatch中检索一些Elasticache指标.我有几个类用于多个服务,它们都可以工作,除了Elasticache.这个类被传递一个Elasticache node 列表,以及开始和结束时间.对于每个 node ,我try 在30分钟内从Elasticache中检索一些指标.然而,出于某种原因,我一直得到每个指标的值为0.0.我已经确认AWS控制台中的指标不是空的.下面是我的代码:
public class ElastiCacheCWMetricsRetriever {
private final AmazonCloudWatch cloudWatchClient;
public ElastiCacheCWMetricsRetriever(AmazonCloudWatch cloudWatchClient) {
this.cloudWatchClient = cloudWatchClient;
}
public Map<String, Map<String, Object>> getElastiCacheMetrics(List<String> nodeNames,
String startTimeStr, String endTimeStr) {
Map<String, Map<String, Object>> elastiCacheMetricsMap = new HashMap<>();
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date startTime = dateFormat.parse(startTimeStr);
Date endTime = dateFormat.parse(endTimeStr);
for (String nodeName : nodeNames) {
Map<String, Object> nodeMetrics = new HashMap<>();
nodeMetrics.put("SwapUsage", getAverageMetricValue("AWS/ElastiCache",
"SwapUsage", "CacheNodeId", nodeName, startTime, endTime));
Double SwapUsage = getSumMetricValue("AWS/ElastiCache", "SwapUsage",
"CacheNodeId", nodeName, startTime, endTime);
System.out.println("SwapUsage for " + nodeName + " is " +
String.valueOf(SwapUsage));
nodeMetrics.put("CurrentConnections", getSumMetricValue("AWS/ElastiCache",
"CurrConnections", "CacheNodeId", nodeName, startTime, endTime));
nodeMetrics.put("DatabaseMemoryUsagePercentage",
getAverageMetricValue("AWS/ElastiCache", "DatabaseMemoryUsagePercentage",
"CacheNodeId", nodeName, startTime, endTime) * 100.0);
nodeMetrics.put("EngineCPUUtilization",
getAverageMetricValue("AWS/ElastiCache", "EngineCPUUtilization",
"CacheNodeId", nodeName, startTime, endTime) * 100.0);
Double EngineCPUUtil = getAverageMetricValue("AWS/ElastiCache",
"EngineCPUUtilization", "CacheNodeId", nodeName, startTime, endTime) * 100.0;
System.out.println("EngineCPUUtilization for " + nodeName + " is " + String.valueOf(EngineCPUUtil));
elastiCacheMetricsMap.put(nodeName, nodeMetrics);
}
} catch (Exception e) {
System.out.println("Error in Elasticache" + e.getMessage());
}
return elastiCacheMetricsMap;
}
private double getAverageMetricValue(String namespace, String metricName, String
dimensionName, String dimensionValue, Date startTime, Date endTime) {
int period = calculatePeriod(startTime, endTime);
return getMetricValue(namespace, metricName, dimensionName, dimensionValue, startTime, endTime, period, "Average");
}
private double getSumMetricValue(String namespace, String metricName, String dimensionName, String dimensionValue, Date startTime, Date endTime) {
int period = calculatePeriod(startTime, endTime);
return getMetricValue(namespace, metricName, dimensionName, dimensionValue, startTime, endTime, period, "Sum");
}
private double getMetricValue(String namespace, String metricName, String dimensionName, String dimensionValue, Date startTime, Date endTime, int period, String statistic) {
try {
GetMetricStatisticsRequest request = new GetMetricStatisticsRequest()
.withNamespace(namespace)
.withMetricName(metricName)
.withDimensions(new Dimension().withName(dimensionName).withValue(dimensionValue))
.withStartTime(startTime)
.withEndTime(endTime)
.withPeriod(period)
.withStatistics(statistic);
GetMetricStatisticsResult result = cloudWatchClient.getMetricStatistics(request);
System.out.println("Result for " + dimensionValue + " is " + result.toString());
return result.getDatapoints().isEmpty() ? 0 : result.getDatapoints().get(0).getAverage();
} catch (Exception e) {
System.out.println("Error in Elasticache" + e.getMessage());
return 0;
}
}
private int calculatePeriod(Date startTime, Date endTime) {
long diffInSeconds = (endTime.getTime() - startTime.getTime()) / 1000;
int periods = (int) Math.ceil(diffInSeconds / 1800);
return (periods > 0) ? 1800 : 60;
}
}
我正在使用System.out.println("SwapUsage for " + nodeName + " is " + String.valueOf(SwapUsage));
进行故障排除.我不确定下一步该往哪里看.有什么 idea /建议吗?
编辑:所以我又测试了一些,我在getMetricValue
中添加了这行System.out.println("Result for " + dimensionValue + " is " + result.toString());
.我得到的结果是Result for scrsquy-001 is {Label: EngineCPUUtilization,Datapoints: []}
.为什么它是空的,我怎么才能让它工作?