Authored by jack

增加service的cmdb对比

@@ -3,18 +3,17 @@ package com.monitor.compare.ctrl; @@ -3,18 +3,17 @@ package com.monitor.compare.ctrl;
3 import com.monitor.awstools.comp.AWSClientComp; 3 import com.monitor.awstools.comp.AWSClientComp;
4 import com.monitor.awstools.comp.AWSELBClientComp; 4 import com.monitor.awstools.comp.AWSELBClientComp;
5 import com.monitor.compare.model.CompareRequest; 5 import com.monitor.compare.model.CompareRequest;
  6 +import com.monitor.compare.model.CompareResponse;
6 import com.monitor.compare.service.GatewayCompare; 7 import com.monitor.compare.service.GatewayCompare;
7 import com.monitor.compare.service.NignxCompare; 8 import com.monitor.compare.service.NignxCompare;
8 import com.monitor.compare.service.ServiceCompare; 9 import com.monitor.compare.service.ServiceCompare;
9 import com.monitor.model.response.BaseResponse; 10 import com.monitor.model.response.BaseResponse;
  11 +import org.apache.commons.net.io.FromNetASCIIInputStream;
10 import org.springframework.beans.factory.annotation.Autowired; 12 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.beans.factory.annotation.Value; 13 import org.springframework.beans.factory.annotation.Value;
12 import org.springframework.web.bind.annotation.*; 14 import org.springframework.web.bind.annotation.*;
13 15
14 -import java.util.ArrayList;  
15 -import java.util.HashMap;  
16 -import java.util.List;  
17 -import java.util.Map; 16 +import java.util.*;
18 17
19 18
20 /** 19 /**
@@ -36,6 +35,8 @@ public class CompareCtrl { @@ -36,6 +35,8 @@ public class CompareCtrl {
36 @RequestMapping(value = "/compare") 35 @RequestMapping(value = "/compare")
37 public BaseResponse compare(@RequestBody CompareRequest request) { 36 public BaseResponse compare(@RequestBody CompareRequest request) {
38 37
  38 + CompareResponse compareResponse=new CompareResponse();
  39 +
39 Map<String, List<String>> configMap = new HashMap<>(); 40 Map<String, List<String>> configMap = new HashMap<>();
40 41
41 String service = request.getService(); 42 String service = request.getService();
@@ -72,10 +73,44 @@ public class CompareCtrl { @@ -72,10 +73,44 @@ public class CompareCtrl {
72 resultList.add(strBuilder.toString()); 73 resultList.add(strBuilder.toString());
73 74
74 } 75 }
  76 + compareResponse.setIps(resultList);
  77 +
  78 + if(!checkConfig(configMap))
  79 + {
  80 + compareResponse.setStatus(1);
  81 + }
  82 +
75 BaseResponse response = new BaseResponse(); 83 BaseResponse response = new BaseResponse();
76 84
77 - response.setData(resultList); 85 + response.setData(compareResponse);
78 86
79 return response; 87 return response;
80 } 88 }
  89 +
  90 +
  91 + private boolean checkConfig(Map<String, List<String>> configMap) {
  92 + List<String> cmdbList = configMap.get("cmdb");
  93 + for (Map.Entry<String, List<String>> entry : configMap.entrySet()) {
  94 + if (!checkListEqual(cmdbList, entry.getValue())) {
  95 + return false;
  96 + }
  97 + }
  98 + return true;
  99 + }
  100 +
  101 +
  102 + private boolean checkListEqual(List<String> list1, List<String> list2) {
  103 + if (null == list1 || null == list2) {
  104 + return false;
  105 + }
  106 + if (list1.size() != list2.size()) {
  107 + return false;
  108 + }
  109 + for (String item : list1) {
  110 + if (!list2.contains(item)) {
  111 + return false;
  112 + }
  113 + }
  114 + return true;
  115 + }
81 } 116 }
  1 +package com.monitor.compare.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +import java.util.List;
  6 +
  7 +/**
  8 + * Created by yoho on 2016/10/22.
  9 + */
  10 +@Data
  11 +public class CompareResponse {
  12 + List<String> ips;
  13 +
  14 + int status = 0;
  15 +}
@@ -43,7 +43,7 @@ public class GatewayCompare { @@ -43,7 +43,7 @@ public class GatewayCompare {
43 ipsInIptables = new ArrayList<>(0); 43 ipsInIptables = new ArrayList<>(0);
44 } 44 }
45 45
46 - configMap.put("iptables", ipsInIptables); 46 + configMap.put("ip-tables", ipsInIptables);
47 47
48 List<String> ipsInZK; 48 List<String> ipsInZK;
49 try { 49 try {
@@ -53,7 +53,18 @@ public class GatewayCompare { @@ -53,7 +53,18 @@ public class GatewayCompare {
53 DEBUG.error("Failed to find ips in {} from upstream in gitlab, error {}", net, e); 53 DEBUG.error("Failed to find ips in {} from upstream in gitlab, error {}", net, e);
54 } 54 }
55 55
56 - configMap.put("upstream", ipsInZK); 56 + configMap.put("nginx-upstream", ipsInZK);
  57 +
  58 + List<String> ipsInCMDB;
  59 + try {
  60 + ipsInCMDB = queryCMDBIps(net);
  61 + } catch (Exception e) {
  62 + ipsInCMDB = new ArrayList<>(0);
  63 +
  64 + DEBUG.error("Failed to find ips in {} from cmdb, error {}", net, e);
  65 + }
  66 +
  67 + configMap.put("cmdb", ipsInCMDB);
57 68
58 return configMap; 69 return configMap;
59 } 70 }
@@ -126,4 +137,9 @@ public class GatewayCompare { @@ -126,4 +137,9 @@ public class GatewayCompare {
126 137
127 return ipList; 138 return ipList;
128 } 139 }
  140 +
  141 +
  142 + private List<String> queryCMDBIps(String net) {
  143 + return serviceCompare.queryServiceCMDBIps(net, "gateway");
  144 + }
129 } 145 }
@@ -74,7 +74,7 @@ public class NignxCompare { @@ -74,7 +74,7 @@ public class NignxCompare {
74 DEBUG.error("Failed to query qcloud nginx clb instances , error {}", e); 74 DEBUG.error("Failed to query qcloud nginx clb instances , error {}", e);
75 } 75 }
76 76
77 - nginxMap.put("nginx_clb_qcloud", clbInstanceIpList); 77 + nginxMap.put("qcloud_clb", clbInstanceIpList);
78 78
79 List<String> cmdbIpList = new ArrayList<>(); 79 List<String> cmdbIpList = new ArrayList<>();
80 80
@@ -91,7 +91,7 @@ public class NignxCompare { @@ -91,7 +91,7 @@ public class NignxCompare {
91 Collections.sort(cmdbIpList, new IpComparator()); 91 Collections.sort(cmdbIpList, new IpComparator());
92 } 92 }
93 93
94 - nginxMap.put("nignx_cmdb_qcloud", cmdbIpList); 94 + nginxMap.put("cmdb", cmdbIpList);
95 95
96 return nginxMap; 96 return nginxMap;
97 97
@@ -160,7 +160,7 @@ public class NignxCompare { @@ -160,7 +160,7 @@ public class NignxCompare {
160 Collections.sort(elbInstanceIpList, new IpComparator()); 160 Collections.sort(elbInstanceIpList, new IpComparator());
161 } 161 }
162 162
163 - nginxMap.put("nginx_elb_aws", elbInstanceIpList); 163 + nginxMap.put("aws_elb", elbInstanceIpList);
164 164
165 List<String> cmdbIpList = new ArrayList<>(); 165 List<String> cmdbIpList = new ArrayList<>();
166 166
@@ -174,7 +174,7 @@ public class NignxCompare { @@ -174,7 +174,7 @@ public class NignxCompare {
174 if (!cmdbIpList.isEmpty()) { 174 if (!cmdbIpList.isEmpty()) {
175 Collections.sort(cmdbIpList, new IpComparator()); 175 Collections.sort(cmdbIpList, new IpComparator());
176 } 176 }
177 - nginxMap.put("nignx_cmdb_aws", cmdbIpList); 177 + nginxMap.put("cmdb", cmdbIpList);
178 178
179 return nginxMap; 179 return nginxMap;
180 } 180 }
1 package com.monitor.compare.service; 1 package com.monitor.compare.service;
2 2
3 import com.fasterxml.jackson.databind.DeserializationFeature; 3 import com.fasterxml.jackson.databind.DeserializationFeature;
  4 +import com.model.HostInfo;
  5 +import com.monitor.cmdb.service.IHostInfoService;
4 import com.monitor.compare.comparator.IpComparator; 6 import com.monitor.compare.comparator.IpComparator;
5 import com.sun.org.apache.bcel.internal.generic.NEWARRAY; 7 import com.sun.org.apache.bcel.internal.generic.NEWARRAY;
6 import org.apache.commons.lang.StringUtils; 8 import org.apache.commons.lang.StringUtils;
@@ -15,6 +17,7 @@ import org.gitlab.api.TokenType; @@ -15,6 +17,7 @@ import org.gitlab.api.TokenType;
15 import org.gitlab.api.models.GitlabProject; 17 import org.gitlab.api.models.GitlabProject;
16 import org.slf4j.Logger; 18 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory; 19 import org.slf4j.LoggerFactory;
  20 +import org.springframework.beans.factory.annotation.Autowired;
18 import org.springframework.beans.factory.annotation.Value; 21 import org.springframework.beans.factory.annotation.Value;
19 import org.springframework.stereotype.Service; 22 import org.springframework.stereotype.Service;
20 23
@@ -37,6 +40,9 @@ public class ServiceCompare { @@ -37,6 +40,9 @@ public class ServiceCompare {
37 @Value("${qcloud_zk_address}") 40 @Value("${qcloud_zk_address}")
38 private String qcloud_zk_address; 41 private String qcloud_zk_address;
39 42
  43 + @Autowired
  44 + private IHostInfoService hostInfoService;
  45 +
40 public Map<String, List<String>> compare(String net, String service) { 46 public Map<String, List<String>> compare(String net, String service) {
41 Map<String, List<String>> configMap = new HashMap<>(); 47 Map<String, List<String>> configMap = new HashMap<>();
42 48
@@ -53,7 +59,7 @@ public class ServiceCompare { @@ -53,7 +59,7 @@ public class ServiceCompare {
53 DEBUG.error("Failed to query net {} service {} form ip-tables in gitlab, error {} ", net, service, e); 59 DEBUG.error("Failed to query net {} service {} form ip-tables in gitlab, error {} ", net, service, e);
54 } 60 }
55 61
56 - configMap.put("iptables", ipsInIptables); 62 + configMap.put("ip-tables", ipsInIptables);
57 63
58 List<String> ipsInZK; 64 List<String> ipsInZK;
59 65
@@ -66,7 +72,19 @@ public class ServiceCompare { @@ -66,7 +72,19 @@ public class ServiceCompare {
66 DEBUG.error("Failed to query net {} service {} form zk register service, error {} ", net, service, e); 72 DEBUG.error("Failed to query net {} service {} form zk register service, error {} ", net, service, e);
67 } 73 }
68 74
69 - configMap.put("zk", ipsInZK); 75 + configMap.put("zk-instaces", ipsInZK);
  76 +
  77 + List<String> ipsInCMDB;
  78 + try {
  79 + ipsInCMDB=queryServiceCMDBIps(net,service);
  80 + }
  81 + catch (Exception e)
  82 + {
  83 + ipsInCMDB=new ArrayList<>(0);
  84 + DEBUG.error("Failed to query net {} service {} form cmdb, error {} ", net, service, e);
  85 + }
  86 +
  87 + configMap.put("cmdb",ipsInCMDB);
70 88
71 return configMap; 89 return configMap;
72 } 90 }
@@ -113,8 +131,7 @@ public class ServiceCompare { @@ -113,8 +131,7 @@ public class ServiceCompare {
113 131
114 String[] ipArray = ips.split("master@"); 132 String[] ipArray = ips.split("master@");
115 133
116 - if(null==ipArray)  
117 - { 134 + if (null == ipArray) {
118 return ipList; 135 return ipList;
119 } 136 }
120 for (String ip : ipArray) { 137 for (String ip : ipArray) {
@@ -328,7 +345,7 @@ public class ServiceCompare { @@ -328,7 +345,7 @@ public class ServiceCompare {
328 return serviceIps; 345 return serviceIps;
329 } 346 }
330 347
331 - private CuratorFramework createClinet(String address) throws InterruptedException { 348 + private CuratorFramework createClinet(String address) throws Exception {
332 CuratorFramework client = CuratorFrameworkFactory.newClient(address, new RetryOneTime(2000)); 349 CuratorFramework client = CuratorFrameworkFactory.newClient(address, new RetryOneTime(2000));
333 350
334 client.blockUntilConnected(2, TimeUnit.SECONDS); 351 client.blockUntilConnected(2, TimeUnit.SECONDS);
@@ -336,4 +353,24 @@ public class ServiceCompare { @@ -336,4 +353,24 @@ public class ServiceCompare {
336 return client; 353 return client;
337 } 354 }
338 355
  356 + public List<String> queryServiceCMDBIps(String net, String service) {
  357 + List<String> cmdbIpList = new ArrayList<>();
  358 + int netType = 1;
  359 + if (StringUtils.equals(QCLOUD_TYPE, net)) {
  360 + netType = 2;
  361 + }
  362 + for (HostInfo hostInfo : hostInfoService.getHostInfosByTag(service)) {
  363 + //1 aws 2 qcloud
  364 + if (netType == hostInfo.getCloudType()) {
  365 + cmdbIpList.add(hostInfo.getHostIp());
  366 + }
  367 + }
  368 +
  369 + if (!cmdbIpList.isEmpty()) {
  370 + Collections.sort(cmdbIpList, new IpComparator());
  371 + }
  372 +
  373 + return cmdbIpList;
  374 + }
  375 +
339 } 376 }