...
|
...
|
@@ -14,6 +14,7 @@ import com.monitor.model.request.HostInfoReq; |
|
|
import com.monitor.model.response.BaseResponse;
|
|
|
import com.monitor.model.response.PageResponse;
|
|
|
import com.monitor.mysql.mapper.HostInfoMapper;
|
|
|
import com.yoho.ops.cmdb.models.Host;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
...
|
...
|
@@ -153,12 +154,17 @@ public class HostInfoServiceImpl implements IHostInfoService { |
|
|
@Override
|
|
|
public PageResponse<HostInfo> getHostInfos(HostInfoReq req) {
|
|
|
logger.info("getHostInfos with param is {}", req);
|
|
|
|
|
|
if(req.getTagsList() != null && req.getTagsList().contains("isNull")){//查找没有标签的主机
|
|
|
req.setTags("isNull");
|
|
|
req.getTagsList().remove("isNull");
|
|
|
}
|
|
|
// 组装分页对象
|
|
|
PageBean page = PageBean.initPageInfo(req.getCurrentPage(),
|
|
|
req.getPageSize(), req);
|
|
|
// 先查询符合条件的总数量
|
|
|
int total = hostInfoMapper.selectCountByCodition(page);
|
|
|
logger.info("selectUserTotal num is {}, with param is {}", total,
|
|
|
logger.info("selectUserTotal num is {}, with param is {}", total,
|
|
|
req);
|
|
|
// 数量为0 直接返回
|
|
|
if (total == 0) {
|
...
|
...
|
@@ -319,4 +325,112 @@ public class HostInfoServiceImpl implements IHostInfoService { |
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
*hostInfo ip 对比
|
|
|
* @param hosts---线上 主机信息
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
public String compareHostInfo(List<Host> hosts, int cloudType) {
|
|
|
String resultMessage = "";
|
|
|
if(hosts == null || hosts.size() == 0){
|
|
|
logger.error("hostIps get from cloud is null! cloudType is {}",cloudType);
|
|
|
return "hostIps get from cloud is null! cloudType is " + cloudType ;
|
|
|
}
|
|
|
|
|
|
List<String> cloudHosts = new ArrayList<>();
|
|
|
for(Host h : hosts){
|
|
|
if(StringUtils.isNotBlank(h.getIp())){
|
|
|
cloudHosts.add(h.getIp());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
List<String> dbHosts= new ArrayList<>();
|
|
|
try{
|
|
|
dbHosts = hostInfoMapper.selectHostIpByCloud(cloudType);
|
|
|
}catch (Exception e){
|
|
|
logger.error("selectHostIpByCloud from database error! cloudType is {} : ",cloudType, e);
|
|
|
return "selectHostIpByCloud from database error! cloudType is " + cloudType;
|
|
|
}
|
|
|
|
|
|
List<String> newIpList = new ArrayList<>();//需要插入到数据库中的ip
|
|
|
List<String> oldIpList = new ArrayList<>();//需要从数据库中删除的ip
|
|
|
for(String ip : cloudHosts){
|
|
|
if( !dbHosts.contains(ip) ){
|
|
|
newIpList.add(ip);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for(String ip : dbHosts){
|
|
|
if( !cloudHosts.contains(ip)){
|
|
|
oldIpList.add(ip);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
if(newIpList.size() > 0 ){
|
|
|
logger.info("add new ips to database , ipList is {} ,cloudType is {} ,", newIpList, cloudType);
|
|
|
for (String ip : newIpList){
|
|
|
HostInfo hostInfo = new HostInfo();
|
|
|
hostInfo.setHostIp(ip);
|
|
|
hostInfo.setCloudType(cloudType);
|
|
|
if(cloudType == 1){
|
|
|
hostInfo.setAlias("AWS-" + ip);
|
|
|
}else if(cloudType == 2){
|
|
|
hostInfo.setAlias("QCLOUD-" + ip);
|
|
|
}
|
|
|
|
|
|
try{
|
|
|
hostInfoMapper.insert(hostInfo);
|
|
|
}catch (Exception e ){
|
|
|
logger.error("insert hostInfo error ! ip is {} : ", ip , e);
|
|
|
resultMessage += "insert hostInfo error ! ip is " + ip;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(oldIpList.size() > 0 ){
|
|
|
logger.info("delete ip from database , ipList is {} ,cloudType is {} ,", oldIpList, cloudType);
|
|
|
for(String ip : oldIpList){
|
|
|
try{
|
|
|
//删除数据--暂时屏蔽
|
|
|
// hostInfoMapper.deleteByIp(ip);
|
|
|
}catch (Exception e){
|
|
|
logger.error("delete ip error ! ip is {} : {}", ip , cloudType);
|
|
|
resultMessage += "delete ip error ! ip is " + ip ;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return resultMessage;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public BaseResponse<Integer> saveHostTags(List<Integer> hostIdList, String tags) {
|
|
|
int result = 0;
|
|
|
String message = "setTagsError: hostId is ";
|
|
|
if( hostIdList != null && hostIdList.size() > 0){
|
|
|
for(Integer hostId : hostIdList){
|
|
|
HostInfo host = new HostInfo();
|
|
|
host.setTags(tags);
|
|
|
host.setId(hostId);
|
|
|
try{
|
|
|
hostInfoMapper.updateByPrimaryKeySelective(host);
|
|
|
}catch (Exception e){
|
|
|
logger.error("saveHostTags error! hostId is {} : ", hostId , e);
|
|
|
result = 1;
|
|
|
message += hostId + " , ";
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(result == 0){
|
|
|
return new BaseResponse<>();
|
|
|
}
|
|
|
return new BaseResponse<>(message);
|
|
|
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|