Authored by xuhui

亚马逊接口

恶意ip aws地址更改
@@ -132,7 +132,11 @@ @@ -132,7 +132,11 @@
132 <artifactId>servlet-api</artifactId> 132 <artifactId>servlet-api</artifactId>
133 <version>2.5</version> 133 <version>2.5</version>
134 </dependency> 134 </dependency>
135 - 135 + <dependency>
  136 + <groupId>com.amazonaws</groupId>
  137 + <artifactId>aws-java-sdk-autoscaling</artifactId>
  138 + <version>1.11.31</version>
  139 + </dependency>
136 </dependencies> 140 </dependencies>
137 141
138 </project> 142 </project>
@@ -17,7 +17,7 @@ public class AWSClientComp { @@ -17,7 +17,7 @@ public class AWSClientComp {
17 17
18 public AmazonEC2 getClient() { 18 public AmazonEC2 getClient() {
19 19
20 - AWSCredentials awsCredentials = new BasicAWSCredentials(Constants.ACCESSKEYID, Constants.ACCESSKEY); 20 + AWSCredentials awsCredentials = new BasicAWSCredentials(Constants.NEWACCESSKEYID, Constants.NEWACCESSKEY);
21 21
22 AmazonEC2 amazonEC2 = new AmazonEC2Client(awsCredentials); 22 AmazonEC2 amazonEC2 = new AmazonEC2Client(awsCredentials);
23 23
@@ -27,4 +27,17 @@ public class AWSClientComp { @@ -27,4 +27,17 @@ public class AWSClientComp {
27 27
28 return amazonEC2; 28 return amazonEC2;
29 } 29 }
30 -} 30 +
  31 + public AWSCredentials getAWSCredentials(){
  32 + return new BasicAWSCredentials(Constants.NEWACCESSKEYID, Constants.NEWACCESSKEY);
  33 + }
  34 +
  35 + /**
  36 + * 时区
  37 + * @param regions
  38 + * @return
  39 + */
  40 + public Region getRegion(Regions regions){
  41 + return Region.getRegion(regions);
  42 + }
  43 +}
@@ -32,4 +32,7 @@ public class Constants { @@ -32,4 +32,7 @@ public class Constants {
32 32
33 public static final String CREATETIME = "createTime"; 33 public static final String CREATETIME = "createTime";
34 34
35 -} 35 + public static final String NEWACCESSKEYID = "AKIAPGFGVQ6I44746SYQ";
  36 +
  37 + public static final String NEWACCESSKEY = "4DbP9jLY63FHeTeIhvws/v2TgKm27F0//WXFOTi7";
  38 +}
@@ -4,10 +4,10 @@ import com.monitor.awstools.constant.Constants; @@ -4,10 +4,10 @@ import com.monitor.awstools.constant.Constants;
4 import com.monitor.awstools.constant.JobType; 4 import com.monitor.awstools.constant.JobType;
5 import com.monitor.awstools.constant.TaskStatus; 5 import com.monitor.awstools.constant.TaskStatus;
6 import com.monitor.awstools.constant.TaskType; 6 import com.monitor.awstools.constant.TaskType;
7 -import com.monitor.awstools.task.AwsTask;  
8 import com.monitor.awstools.model.*; 7 import com.monitor.awstools.model.*;
9 import com.monitor.awstools.service.AwsQueryService; 8 import com.monitor.awstools.service.AwsQueryService;
10 import com.monitor.awstools.service.TaskService; 9 import com.monitor.awstools.service.TaskService;
  10 +import com.monitor.awstools.task.AwsTask;
11 import com.monitor.model.response.BaseResponse; 11 import com.monitor.model.response.BaseResponse;
12 import org.apache.commons.lang.StringUtils; 12 import org.apache.commons.lang.StringUtils;
13 import org.quartz.CronExpression; 13 import org.quartz.CronExpression;
@@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory; @@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory;
16 import org.springframework.beans.factory.annotation.Autowired; 16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.web.bind.annotation.RequestBody; 17 import org.springframework.web.bind.annotation.RequestBody;
18 import org.springframework.web.bind.annotation.RequestMapping; 18 import org.springframework.web.bind.annotation.RequestMapping;
  19 +import org.springframework.web.bind.annotation.ResponseBody;
19 import org.springframework.web.bind.annotation.RestController; 20 import org.springframework.web.bind.annotation.RestController;
20 21
21 import java.text.SimpleDateFormat; 22 import java.text.SimpleDateFormat;
@@ -36,6 +37,19 @@ public class AwsToolController { @@ -36,6 +37,19 @@ public class AwsToolController {
36 @Autowired 37 @Autowired
37 TaskService taskService; 38 TaskService taskService;
38 39
  40 + /**
  41 + * 自动伸缩查询
  42 + * @return
  43 + */
  44 + @RequestMapping("/autoScalingConfig")
  45 + @ResponseBody
  46 + public BaseResponse autoScalingConfig() {
  47 + List<AwsAutoScalingGroup> awsAutoScalingGroups = queryService.autoScalingConfig();
  48 + BaseResponse response = new BaseResponse();
  49 + response.setData(awsAutoScalingGroups);
  50 + return response;
  51 + }
  52 +
39 @RequestMapping(value = "/snapshot/check") 53 @RequestMapping(value = "/snapshot/check")
40 public BaseResponse checkSnapShot(@RequestBody AwsEbsReq task) { 54 public BaseResponse checkSnapShot(@RequestBody AwsEbsReq task) {
41 List<M_SnapShot> snapShotList = null; 55 List<M_SnapShot> snapShotList = null;
@@ -356,4 +370,4 @@ public class AwsToolController { @@ -356,4 +370,4 @@ public class AwsToolController {
356 370
357 return ebsTaskResp; 371 return ebsTaskResp;
358 } 372 }
359 -} 373 +}
1 package com.monitor.awstools.service; 1 package com.monitor.awstools.service;
2 2
  3 +import com.amazonaws.regions.Regions;
  4 +import com.amazonaws.services.autoscaling.AmazonAutoScaling;
  5 +import com.amazonaws.services.autoscaling.AmazonAutoScalingClient;
  6 +import com.amazonaws.services.autoscaling.model.*;
3 import com.amazonaws.services.ec2.model.*; 7 import com.amazonaws.services.ec2.model.*;
  8 +import com.amazonaws.services.ec2.model.Filter;
  9 +import com.amazonaws.services.ec2.model.Instance;
  10 +import com.amazonaws.services.ec2.model.Tag;
4 import com.monitor.awstools.comp.AWSClientComp; 11 import com.monitor.awstools.comp.AWSClientComp;
5 -import com.monitor.awstools.model.M_Instance;  
6 -import com.monitor.awstools.model.M_SnapShot;  
7 -import com.monitor.awstools.model.M_Volume; 12 +import com.monitor.awstools.model.*;
  13 +import org.apache.commons.collections.CollectionUtils;
8 import org.apache.commons.lang.StringUtils; 14 import org.apache.commons.lang.StringUtils;
9 import org.slf4j.Logger; 15 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory; 16 import org.slf4j.LoggerFactory;
@@ -12,10 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -12,10 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.stereotype.Service; 18 import org.springframework.stereotype.Service;
13 19
14 import java.text.SimpleDateFormat; 20 import java.text.SimpleDateFormat;
15 -import java.util.ArrayList;  
16 -import java.util.Collections;  
17 -import java.util.Comparator;  
18 -import java.util.List; 21 +import java.util.*;
19 22
20 /** 23 /**
21 * Created by yoho on 2016/8/31. 24 * Created by yoho on 2016/8/31.
@@ -27,6 +30,98 @@ public class AwsQueryService { @@ -27,6 +30,98 @@ public class AwsQueryService {
27 AWSClientComp awsClientComp = new AWSClientComp(); 30 AWSClientComp awsClientComp = new AWSClientComp();
28 31
29 /** 32 /**
  33 + * 查询自动伸缩信息
  34 + */
  35 + public List<AwsAutoScalingGroup> autoScalingConfig() {
  36 + List<AwsAutoScalingGroup> awsAutoScalingGroups = null;
  37 + try {
  38 + //获取AutoScaling组信息
  39 + awsAutoScalingGroups = this.getAwsAutoScaling();
  40 + } catch (Exception e) {
  41 + DEBUG.error(" - AwsController - autoScalingConfig - error", e);
  42 + }
  43 +
  44 + return awsAutoScalingGroups;
  45 + }
  46 +
  47 + /**
  48 + * 获取AutoScaling组的信息
  49 + * @return
  50 + */
  51 + private List<AwsAutoScalingGroup> getAwsAutoScaling(){
  52 + List<AwsAutoScalingGroup> awsGroups = null;
  53 + try {
  54 + AmazonAutoScaling client = new AmazonAutoScalingClient(awsClientComp.getAWSCredentials());
  55 + client.setRegion(awsClientComp.getRegion(Regions.CN_NORTH_1));
  56 + DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest()
  57 + .withAutoScalingGroupNames();
  58 + DescribeAutoScalingGroupsResult result = client .describeAutoScalingGroups(request);
  59 + if(result == null || CollectionUtils.isEmpty(result.getAutoScalingGroups())){
  60 + return null;
  61 + }
  62 + awsGroups = new ArrayList<>();
  63 + AwsAutoScalingGroup awsGroup;
  64 + for(AutoScalingGroup group : result.getAutoScalingGroups()){
  65 + awsGroup = new AwsAutoScalingGroup();
  66 + awsGroup.setAutoScalingGroupName(group.getAutoScalingGroupName());
  67 + awsGroup.setMinSize(group.getMinSize());
  68 + awsGroup.setMaxSize(group.getMaxSize());
  69 + awsGroup.setDesiredCapacity(group.getDesiredCapacity());
  70 +
  71 + if(CollectionUtils.isNotEmpty(group.getInstances())){
  72 + Collection<String> ids = new ArrayList<>();
  73 + for(com.amazonaws.services.autoscaling.model.Instance instance : group.getInstances()){
  74 + ids.add(instance.getInstanceId());
  75 + }
  76 + //根据InstanceId获取详细信息
  77 + awsGroup.setAwsInstancess(getDescribeInstances(ids));
  78 +
  79 + awsGroups.add(awsGroup);
  80 + }
  81 + }
  82 + }catch (Exception e){
  83 + DEBUG.error(" - AwsController - getAwsAutoScaling - error", e);
  84 + }
  85 +
  86 + return awsGroups;
  87 + }
  88 +
  89 + /**
  90 + * 根据AutoScaling组的InstanceId获取详细信息
  91 + * @return
  92 + */
  93 + private List<AwsInstances> getDescribeInstances(Collection<String> ids){
  94 + List<AwsInstances> awsInstancess = null;
  95 + try{
  96 + DescribeInstancesRequest request = new DescribeInstancesRequest()
  97 + .withInstanceIds(ids);
  98 + DescribeInstancesResult result = awsClientComp.getClient().describeInstances(request);
  99 + if(result == null || CollectionUtils.isEmpty(result.getReservations())){
  100 + return null;
  101 + }
  102 + awsInstancess = new ArrayList<>();
  103 + AwsInstances awsInstances;
  104 + for(Reservation res : result.getReservations()){
  105 + List<Instance> instances = res.getInstances();
  106 + if(CollectionUtils.isEmpty(instances)){
  107 + continue;
  108 + }
  109 + for(Instance instance : instances) {
  110 + awsInstances = new AwsInstances();
  111 + awsInstances.setId(instance.getInstanceId());
  112 + awsInstances.setIp(instance.getPrivateIpAddress());
  113 +
  114 + awsInstancess.add(awsInstances);
  115 + }
  116 + }
  117 + }catch(Exception e){
  118 + DEBUG.error(" - AwsController - getDescribeInstances - error", e);
  119 + }
  120 +
  121 + return awsInstancess;
  122 + }
  123 +
  124 + /**
30 * 查询volume信息 125 * 查询volume信息
31 * 126 *
32 * @param volumeId 127 * @param volumeId
@@ -258,4 +353,4 @@ public class AwsQueryService { @@ -258,4 +353,4 @@ public class AwsQueryService {
258 353
259 DEBUG.info("Clear snapshot {} , result {}", snapshotId, result); 354 DEBUG.info("Clear snapshot {} , result {}", snapshotId, result);
260 } 355 }
261 -} 356 +}
@@ -24,8 +24,8 @@ malicious.ip.redis.read.port=6379 @@ -24,8 +24,8 @@ malicious.ip.redis.read.port=6379
24 24
25 malicious.ip.redis.write.qq.host1=10.66.0.3 25 malicious.ip.redis.write.qq.host1=10.66.0.3
26 malicious.ip.redis.write.qq.host2=10.66.0.2 26 malicious.ip.redis.write.qq.host2=10.66.0.2
27 -malicious.ip.redis.write.aws.host1=172.31.20.188  
28 -malicious.ip.redis.write.aws.host2=172.31.20.187 27 +malicious.ip.redis.write.aws.host1=172.31.70.163
  28 +malicious.ip.redis.write.aws.host2=172.31.70.53
29 malicious.ip.redis.write.port=6379 29 malicious.ip.redis.write.port=6379
30 30
31 #restTemplate 31 #restTemplate