|
|
package com.monitor.awstools.service;
|
|
|
|
|
|
import com.amazonaws.regions.Regions;
|
|
|
import com.amazonaws.services.autoscaling.AmazonAutoScaling;
|
|
|
import com.amazonaws.services.autoscaling.AmazonAutoScalingClient;
|
|
|
import com.amazonaws.services.autoscaling.model.*;
|
|
|
import com.amazonaws.services.ec2.model.*;
|
|
|
import com.amazonaws.services.ec2.model.Filter;
|
|
|
import com.amazonaws.services.ec2.model.Instance;
|
|
|
import com.amazonaws.services.ec2.model.Tag;
|
|
|
import com.monitor.awstools.comp.AWSClientComp;
|
|
|
import com.monitor.awstools.model.M_Instance;
|
|
|
import com.monitor.awstools.model.M_SnapShot;
|
|
|
import com.monitor.awstools.model.M_Volume;
|
|
|
import com.monitor.awstools.model.*;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
...
|
...
|
@@ -12,10 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Created by yoho on 2016/8/31.
|
...
|
...
|
@@ -27,6 +30,98 @@ public class AwsQueryService { |
|
|
AWSClientComp awsClientComp = new AWSClientComp();
|
|
|
|
|
|
/**
|
|
|
* 查询自动伸缩信息
|
|
|
*/
|
|
|
public List<AwsAutoScalingGroup> autoScalingConfig() {
|
|
|
List<AwsAutoScalingGroup> awsAutoScalingGroups = null;
|
|
|
try {
|
|
|
//获取AutoScaling组信息
|
|
|
awsAutoScalingGroups = this.getAwsAutoScaling();
|
|
|
} catch (Exception e) {
|
|
|
DEBUG.error(" - AwsController - autoScalingConfig - error", e);
|
|
|
}
|
|
|
|
|
|
return awsAutoScalingGroups;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取AutoScaling组的信息
|
|
|
* @return
|
|
|
*/
|
|
|
private List<AwsAutoScalingGroup> getAwsAutoScaling(){
|
|
|
List<AwsAutoScalingGroup> awsGroups = null;
|
|
|
try {
|
|
|
AmazonAutoScaling client = new AmazonAutoScalingClient(awsClientComp.getAWSCredentials());
|
|
|
client.setRegion(awsClientComp.getRegion(Regions.CN_NORTH_1));
|
|
|
DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest()
|
|
|
.withAutoScalingGroupNames();
|
|
|
DescribeAutoScalingGroupsResult result = client .describeAutoScalingGroups(request);
|
|
|
if(result == null || CollectionUtils.isEmpty(result.getAutoScalingGroups())){
|
|
|
return null;
|
|
|
}
|
|
|
awsGroups = new ArrayList<>();
|
|
|
AwsAutoScalingGroup awsGroup;
|
|
|
for(AutoScalingGroup group : result.getAutoScalingGroups()){
|
|
|
awsGroup = new AwsAutoScalingGroup();
|
|
|
awsGroup.setAutoScalingGroupName(group.getAutoScalingGroupName());
|
|
|
awsGroup.setMinSize(group.getMinSize());
|
|
|
awsGroup.setMaxSize(group.getMaxSize());
|
|
|
awsGroup.setDesiredCapacity(group.getDesiredCapacity());
|
|
|
|
|
|
if(CollectionUtils.isNotEmpty(group.getInstances())){
|
|
|
Collection<String> ids = new ArrayList<>();
|
|
|
for(com.amazonaws.services.autoscaling.model.Instance instance : group.getInstances()){
|
|
|
ids.add(instance.getInstanceId());
|
|
|
}
|
|
|
//根据InstanceId获取详细信息
|
|
|
awsGroup.setAwsInstancess(getDescribeInstances(ids));
|
|
|
|
|
|
awsGroups.add(awsGroup);
|
|
|
}
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
DEBUG.error(" - AwsController - getAwsAutoScaling - error", e);
|
|
|
}
|
|
|
|
|
|
return awsGroups;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据AutoScaling组的InstanceId获取详细信息
|
|
|
* @return
|
|
|
*/
|
|
|
private List<AwsInstances> getDescribeInstances(Collection<String> ids){
|
|
|
List<AwsInstances> awsInstancess = null;
|
|
|
try{
|
|
|
DescribeInstancesRequest request = new DescribeInstancesRequest()
|
|
|
.withInstanceIds(ids);
|
|
|
DescribeInstancesResult result = awsClientComp.getClient().describeInstances(request);
|
|
|
if(result == null || CollectionUtils.isEmpty(result.getReservations())){
|
|
|
return null;
|
|
|
}
|
|
|
awsInstancess = new ArrayList<>();
|
|
|
AwsInstances awsInstances;
|
|
|
for(Reservation res : result.getReservations()){
|
|
|
List<Instance> instances = res.getInstances();
|
|
|
if(CollectionUtils.isEmpty(instances)){
|
|
|
continue;
|
|
|
}
|
|
|
for(Instance instance : instances) {
|
|
|
awsInstances = new AwsInstances();
|
|
|
awsInstances.setId(instance.getInstanceId());
|
|
|
awsInstances.setIp(instance.getPrivateIpAddress());
|
|
|
|
|
|
awsInstancess.add(awsInstances);
|
|
|
}
|
|
|
}
|
|
|
}catch(Exception e){
|
|
|
DEBUG.error(" - AwsController - getDescribeInstances - error", e);
|
|
|
}
|
|
|
|
|
|
return awsInstancess;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询volume信息
|
|
|
*
|
|
|
* @param volumeId
|
...
|
...
|
@@ -258,4 +353,4 @@ public class AwsQueryService { |
|
|
|
|
|
DEBUG.info("Clear snapshot {} , result {}", snapshotId, result);
|
|
|
}
|
|
|
} |
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|