Authored by jack

增加service的cmdb对比

... ... @@ -3,18 +3,17 @@ package com.monitor.compare.ctrl;
import com.monitor.awstools.comp.AWSClientComp;
import com.monitor.awstools.comp.AWSELBClientComp;
import com.monitor.compare.model.CompareRequest;
import com.monitor.compare.model.CompareResponse;
import com.monitor.compare.service.GatewayCompare;
import com.monitor.compare.service.NignxCompare;
import com.monitor.compare.service.ServiceCompare;
import com.monitor.model.response.BaseResponse;
import org.apache.commons.net.io.FromNetASCIIInputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
... ... @@ -36,6 +35,8 @@ public class CompareCtrl {
@RequestMapping(value = "/compare")
public BaseResponse compare(@RequestBody CompareRequest request) {
CompareResponse compareResponse=new CompareResponse();
Map<String, List<String>> configMap = new HashMap<>();
String service = request.getService();
... ... @@ -72,10 +73,44 @@ public class CompareCtrl {
resultList.add(strBuilder.toString());
}
compareResponse.setIps(resultList);
if(!checkConfig(configMap))
{
compareResponse.setStatus(1);
}
BaseResponse response = new BaseResponse();
response.setData(resultList);
response.setData(compareResponse);
return response;
}
private boolean checkConfig(Map<String, List<String>> configMap) {
List<String> cmdbList = configMap.get("cmdb");
for (Map.Entry<String, List<String>> entry : configMap.entrySet()) {
if (!checkListEqual(cmdbList, entry.getValue())) {
return false;
}
}
return true;
}
private boolean checkListEqual(List<String> list1, List<String> list2) {
if (null == list1 || null == list2) {
return false;
}
if (list1.size() != list2.size()) {
return false;
}
for (String item : list1) {
if (!list2.contains(item)) {
return false;
}
}
return true;
}
}
... ...
package com.monitor.compare.model;
import lombok.Data;
import java.util.List;
/**
* Created by yoho on 2016/10/22.
*/
@Data
public class CompareResponse {
List<String> ips;
int status = 0;
}
... ...
... ... @@ -43,7 +43,7 @@ public class GatewayCompare {
ipsInIptables = new ArrayList<>(0);
}
configMap.put("iptables", ipsInIptables);
configMap.put("ip-tables", ipsInIptables);
List<String> ipsInZK;
try {
... ... @@ -53,7 +53,18 @@ public class GatewayCompare {
DEBUG.error("Failed to find ips in {} from upstream in gitlab, error {}", net, e);
}
configMap.put("upstream", ipsInZK);
configMap.put("nginx-upstream", ipsInZK);
List<String> ipsInCMDB;
try {
ipsInCMDB = queryCMDBIps(net);
} catch (Exception e) {
ipsInCMDB = new ArrayList<>(0);
DEBUG.error("Failed to find ips in {} from cmdb, error {}", net, e);
}
configMap.put("cmdb", ipsInCMDB);
return configMap;
}
... ... @@ -126,4 +137,9 @@ public class GatewayCompare {
return ipList;
}
private List<String> queryCMDBIps(String net) {
return serviceCompare.queryServiceCMDBIps(net, "gateway");
}
}
... ...
... ... @@ -74,7 +74,7 @@ public class NignxCompare {
DEBUG.error("Failed to query qcloud nginx clb instances , error {}", e);
}
nginxMap.put("nginx_clb_qcloud", clbInstanceIpList);
nginxMap.put("qcloud_clb", clbInstanceIpList);
List<String> cmdbIpList = new ArrayList<>();
... ... @@ -91,7 +91,7 @@ public class NignxCompare {
Collections.sort(cmdbIpList, new IpComparator());
}
nginxMap.put("nignx_cmdb_qcloud", cmdbIpList);
nginxMap.put("cmdb", cmdbIpList);
return nginxMap;
... ... @@ -160,7 +160,7 @@ public class NignxCompare {
Collections.sort(elbInstanceIpList, new IpComparator());
}
nginxMap.put("nginx_elb_aws", elbInstanceIpList);
nginxMap.put("aws_elb", elbInstanceIpList);
List<String> cmdbIpList = new ArrayList<>();
... ... @@ -174,7 +174,7 @@ public class NignxCompare {
if (!cmdbIpList.isEmpty()) {
Collections.sort(cmdbIpList, new IpComparator());
}
nginxMap.put("nignx_cmdb_aws", cmdbIpList);
nginxMap.put("cmdb", cmdbIpList);
return nginxMap;
}
... ...
package com.monitor.compare.service;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.model.HostInfo;
import com.monitor.cmdb.service.IHostInfoService;
import com.monitor.compare.comparator.IpComparator;
import com.sun.org.apache.bcel.internal.generic.NEWARRAY;
import org.apache.commons.lang.StringUtils;
... ... @@ -15,6 +17,7 @@ import org.gitlab.api.TokenType;
import org.gitlab.api.models.GitlabProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
... ... @@ -37,6 +40,9 @@ public class ServiceCompare {
@Value("${qcloud_zk_address}")
private String qcloud_zk_address;
@Autowired
private IHostInfoService hostInfoService;
public Map<String, List<String>> compare(String net, String service) {
Map<String, List<String>> configMap = new HashMap<>();
... ... @@ -53,7 +59,7 @@ public class ServiceCompare {
DEBUG.error("Failed to query net {} service {} form ip-tables in gitlab, error {} ", net, service, e);
}
configMap.put("iptables", ipsInIptables);
configMap.put("ip-tables", ipsInIptables);
List<String> ipsInZK;
... ... @@ -66,7 +72,19 @@ public class ServiceCompare {
DEBUG.error("Failed to query net {} service {} form zk register service, error {} ", net, service, e);
}
configMap.put("zk", ipsInZK);
configMap.put("zk-instaces", ipsInZK);
List<String> ipsInCMDB;
try {
ipsInCMDB=queryServiceCMDBIps(net,service);
}
catch (Exception e)
{
ipsInCMDB=new ArrayList<>(0);
DEBUG.error("Failed to query net {} service {} form cmdb, error {} ", net, service, e);
}
configMap.put("cmdb",ipsInCMDB);
return configMap;
}
... ... @@ -113,8 +131,7 @@ public class ServiceCompare {
String[] ipArray = ips.split("master@");
if(null==ipArray)
{
if (null == ipArray) {
return ipList;
}
for (String ip : ipArray) {
... ... @@ -328,7 +345,7 @@ public class ServiceCompare {
return serviceIps;
}
private CuratorFramework createClinet(String address) throws InterruptedException {
private CuratorFramework createClinet(String address) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(address, new RetryOneTime(2000));
client.blockUntilConnected(2, TimeUnit.SECONDS);
... ... @@ -336,4 +353,24 @@ public class ServiceCompare {
return client;
}
public List<String> queryServiceCMDBIps(String net, String service) {
List<String> cmdbIpList = new ArrayList<>();
int netType = 1;
if (StringUtils.equals(QCLOUD_TYPE, net)) {
netType = 2;
}
for (HostInfo hostInfo : hostInfoService.getHostInfosByTag(service)) {
//1 aws 2 qcloud
if (netType == hostInfo.getCloudType()) {
cmdbIpList.add(hostInfo.getHostIp());
}
}
if (!cmdbIpList.isEmpty()) {
Collections.sort(cmdbIpList, new IpComparator());
}
return cmdbIpList;
}
}
... ...