通过 java 客户端获取 运行 aws 实例的 CPU 利用率指标
Getting the CPU Utilization metric of running aws instance through java client
我已经从我创建的 AMI 启动了一个新实例。然后我在那个实例上启用了云监控。
我想要的是通过 java 客户端获得该特定实例的 CPU 利用率。我只想通过传递凭据和 instanceid 来检索该信息。我附上了我编写的以下代码。
我遇到的问题是无法检索 CPU 利用率指标。我在这里遗漏了什么吗?
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.http.impl.client.BasicCredentialsProvider;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder;
import com.amazonaws.services.cloudwatch.model.Datapoint;
import com.amazonaws.services.cloudwatch.model.Dimension;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult;
import com.amazonaws.services.cloudwatch.model.ListMetricsResult;
import software.amazon.awssdk.services.cloudwatch.model.Metric;
/**
* Lists CloudWatch metrics
*/
public class AwsCloudWatchMetrics {
final static String InstanceId = "i-xxxxxxxxxxxxx";
public static void main(String[] args) {
MonitorCPU();
}
private static void MonitorCPU() {
// AmazonCloudWatchClient cw = new AmazonCloudWatchClient(credentials);
AWSCredentialsProvider awsp = new AWSCredentialsProvider() {
@Override
public void refresh() {
// TODO Auto-generated method stub
}
@Override
public AWSCredentials getCredentials() {
AWSCredentials awsCredentials = null;
try {
awsCredentials = new AWSCredentials() {
public String getAWSSecretKey() {
return "awssecretkeygoeshere";
}
public String getAWSAccessKeyId() {
return "awsaccesskeygoeshere";
}
};
} catch (Exception e) {
throw new AmazonClientException(
"can not load your aws credentials, please check your credentials !!", e);
}
return awsCredentials;
}
};
try{
AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.standard()
.withCredentials(awsp).withRegion("us-east-2").build();
long offsetInMilliseconds = 1000 * 60 * 60 * 24 ;
Dimension dimension = new Dimension()
.withName("InstanceId")
.withValue("i-xxxxxxxxxxxxx");
GetMetricStatisticsRequest request = new GetMetricStatisticsRequest()
.withStartTime(new Date(new Date().getTime() - offsetInMilliseconds)).withNamespace("AWS/EC2")
.withPeriod(60 * 60)
.withMetricName("CPUUtilization").withStatistics("Average").withEndTime(new Date())
.withDimensions(dimension);
// Dimension().withName("InstanceType").withValue("r5.large"))
// Dimension().withName("InstanceId").withValue("i-xxxxxxxxxxxxxxxxx")
// .withMeasureName("CPUUtilization")
//withMetricName("CPUUtilization")
GetMetricStatisticsResult getMetricStatisticsResult = cw.getMetricStatistics(request);
System.out.println("request " + request.toString());
System.out.println("label : " + getMetricStatisticsResult.getLabel());
System.out.println("DataPoint Size : " + getMetricStatisticsResult.getDatapoints().size());
double avgCPUUtilization = 0;
List<Datapoint> dataPoint = getMetricStatisticsResult.getDatapoints();
for (Object aDataPoint : dataPoint) {
Datapoint dp = (Datapoint) aDataPoint;
System.out.println(" avgCPUUtilization " + avgCPUUtilization);
}
}catch(AmazonServiceException ase){
ase.printStackTrace();
}
/*
* TreeMap metricValues = new TreeMap<Long, Double>(); for (Datapoint dp
* : getMetricStatisticsResult.getDatapoints()) {
* metricValues.put(dp.getTimestamp().getTime(), dp.getAverage()); }
*
* Set set = metricValues.entrySet(); Iterator i = set.iterator(); while
* (i.hasNext()) { Map.Entry me = (Map.Entry) i.next();
* System.out.print(me.getKey() + ": ");
* System.out.println(me.getValue()); }
*/
}
}
使用上面的代码,我总是得到零数据点和 0.0 作为 cpu 利用率,而在 aws 控制台上,对于给定的日期范围,我看到一些 cpu 峰值。
EC2 实例是区域特定的,因此指向正确的区域解决了我的问题。我们需要在 .withRegion()
.
中传递正确的区域值
我已经从我创建的 AMI 启动了一个新实例。然后我在那个实例上启用了云监控。
我想要的是通过 java 客户端获得该特定实例的 CPU 利用率。我只想通过传递凭据和 instanceid 来检索该信息。我附上了我编写的以下代码。
我遇到的问题是无法检索 CPU 利用率指标。我在这里遗漏了什么吗?
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.http.impl.client.BasicCredentialsProvider;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder;
import com.amazonaws.services.cloudwatch.model.Datapoint;
import com.amazonaws.services.cloudwatch.model.Dimension;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest;
import com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult;
import com.amazonaws.services.cloudwatch.model.ListMetricsResult;
import software.amazon.awssdk.services.cloudwatch.model.Metric;
/**
* Lists CloudWatch metrics
*/
public class AwsCloudWatchMetrics {
final static String InstanceId = "i-xxxxxxxxxxxxx";
public static void main(String[] args) {
MonitorCPU();
}
private static void MonitorCPU() {
// AmazonCloudWatchClient cw = new AmazonCloudWatchClient(credentials);
AWSCredentialsProvider awsp = new AWSCredentialsProvider() {
@Override
public void refresh() {
// TODO Auto-generated method stub
}
@Override
public AWSCredentials getCredentials() {
AWSCredentials awsCredentials = null;
try {
awsCredentials = new AWSCredentials() {
public String getAWSSecretKey() {
return "awssecretkeygoeshere";
}
public String getAWSAccessKeyId() {
return "awsaccesskeygoeshere";
}
};
} catch (Exception e) {
throw new AmazonClientException(
"can not load your aws credentials, please check your credentials !!", e);
}
return awsCredentials;
}
};
try{
AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.standard()
.withCredentials(awsp).withRegion("us-east-2").build();
long offsetInMilliseconds = 1000 * 60 * 60 * 24 ;
Dimension dimension = new Dimension()
.withName("InstanceId")
.withValue("i-xxxxxxxxxxxxx");
GetMetricStatisticsRequest request = new GetMetricStatisticsRequest()
.withStartTime(new Date(new Date().getTime() - offsetInMilliseconds)).withNamespace("AWS/EC2")
.withPeriod(60 * 60)
.withMetricName("CPUUtilization").withStatistics("Average").withEndTime(new Date())
.withDimensions(dimension);
// Dimension().withName("InstanceType").withValue("r5.large"))
// Dimension().withName("InstanceId").withValue("i-xxxxxxxxxxxxxxxxx")
// .withMeasureName("CPUUtilization")
//withMetricName("CPUUtilization")
GetMetricStatisticsResult getMetricStatisticsResult = cw.getMetricStatistics(request);
System.out.println("request " + request.toString());
System.out.println("label : " + getMetricStatisticsResult.getLabel());
System.out.println("DataPoint Size : " + getMetricStatisticsResult.getDatapoints().size());
double avgCPUUtilization = 0;
List<Datapoint> dataPoint = getMetricStatisticsResult.getDatapoints();
for (Object aDataPoint : dataPoint) {
Datapoint dp = (Datapoint) aDataPoint;
System.out.println(" avgCPUUtilization " + avgCPUUtilization);
}
}catch(AmazonServiceException ase){
ase.printStackTrace();
}
/*
* TreeMap metricValues = new TreeMap<Long, Double>(); for (Datapoint dp
* : getMetricStatisticsResult.getDatapoints()) {
* metricValues.put(dp.getTimestamp().getTime(), dp.getAverage()); }
*
* Set set = metricValues.entrySet(); Iterator i = set.iterator(); while
* (i.hasNext()) { Map.Entry me = (Map.Entry) i.next();
* System.out.print(me.getKey() + ": ");
* System.out.println(me.getValue()); }
*/
}
}
使用上面的代码,我总是得到零数据点和 0.0 作为 cpu 利用率,而在 aws 控制台上,对于给定的日期范围,我看到一些 cpu 峰值。
EC2 实例是区域特定的,因此指向正确的区域解决了我的问题。我们需要在 .withRegion()
.