我想使用此代码获取EC2实例性能数据:
public void testCreateSpotInstance() throws Exception
{
BasicAWSCredentials bawsc = new BasicAWSCredentials(
"AKI2IUYDKF4KZV1DAL2A", "Onv+nq32tUkiLlKI32H3JtIB7Y4QM3esh01J37tL");
AmazonEC2 ec2 = new AmazonEC2Client(bawsc);
ec2.setEndpoint("ec2.ap-southeast-1.amazonaws.com");
ec2.setRegion(Region.getRegion(Regions.EU_CENTRAL_1));
fetchInstancePublicIP(ec2);
}
public void fetchInstancePublicIP(AmazonEC2 ec2)
{
DescribeInstancesRequest request = new DescribeInstancesRequest().withInstanceIds("i-03f5b2c1");
DescribeInstancesResult result = ec2.describeInstances(request);
List<Reservation> list = result.getReservations();
for (Reservation res : list)
{
List<Instance> instanceList = res.getInstances();
for (Instance instance : instanceList)
{
System.out.println("Public IP : " + instance.getPublicIpAddress());
System.out.println("Public DNS : " + instance.getPublicDnsName());
System.out.println("Instance State : " + instance.getState());
System.out.println("Instance TAGS : " + instance.getTags());
System.out.println("Instance Architecture : " + instance.getArchitecture());
System.out.println("Instance VpcId : " + instance.getVpcId());
System.out.println("Instance Launch Time : " + instance.getLaunchTime());
System.out.println("Instance Root Device Name : " + instance.getRootDeviceName());
System.out.println("Instance Hypervisor : " + instance.getHypervisor());
System.out.println("Instance Root Device Type : " + instance.getRootDeviceType());
System.out.println("Instance Virtualization Type : " + instance.getVirtualizationType());
System.out.println("Instance Location : " + instance.getPlacement());
instance.getNetworkInterfaces();
for (int i = 0; i < instance.getNetworkInterfaces().size(); i++)
{
InstanceNetworkInterface get = instance.getNetworkInterfaces().get(i);
System.out.println("Private Ip Address " + get.getPrivateIpAddress());
System.out.println("Mac Address " + get.getMacAddress());
System.out.println("Private Dns Name " + get.getPrivateDnsName());
InstanceNetworkInterfaceAssociation association = get.getAssociation();
System.out.println("Public Ip " + association.getPublicIp());
System.out.println("Public Dns Name " + association.getPublicDnsName());
System.out.println("Ip Owner Id " + association.getIpOwnerId());
}
}
}
}
我可以获取实例数据但我无法获得有关CPU性能和RAM的数据。
您能否提供一些有关如何使用Java获取此数据的建议?
答案 0 :(得分:2)
步骤1:配置服务器以将RAM使用信息发送到CloudWatch:http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/mon-scripts.html
步骤2:查询CloudWatch的CPU和RAM使用情况。
说明:
RAM使用率仅在服务器上运行的操作系统中可用,因此您必须将其推送到CloudWatch才能使其可访问。在CloudWatch中使用它也很不错,这样您就可以针对该指标设置警报。
在CloudWatch中获得所需的所有值后,您可以使用A mazonCloudWatchClient.getMetricStatistics()方法在Java应用程序中获取这些值。