Authored by mlge

主机管理--同步更新ip&&支持统一标签设置

package com.monitor.cmdb.ctrl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.model.HostInfo;
import com.monitor.cmdb.service.IHostInfoService;
import com.monitor.model.request.HostInfoReq;
import com.monitor.model.request.SetHostTagsReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.PageResponse;
import com.yoho.ops.cmdb.aws.ec2.AwsEc2Fetcher;
import com.yoho.ops.cmdb.models.CmdbApiReq;
import com.yoho.ops.cmdb.models.Host;
import com.yoho.ops.cmdb.qcloud.autoscaling.QcloudAutoScalingFetcher;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -19,6 +24,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
... ... @@ -36,6 +42,10 @@ public class HostInfoCtrl {
@Autowired
IHostInfoService hostInfoService;
@Autowired
private AwsEc2Fetcher ec2;
@Autowired
private QcloudAutoScalingFetcher qcloudFetcher;
... ... @@ -158,5 +168,33 @@ public class HostInfoCtrl {
return hostInfoService.saveHostListInfo(hostInfos);
}
/**
* 同步主机信息(线上 &&database)
* @return
*/
@RequestMapping("/synHostInfo")
@ResponseBody
public BaseResponse synHostInfo() {
String message = "";
List<Host> awsHosts = ec2.getAllEc2Details();
message = hostInfoService.compareHostInfo(awsHosts, 1);
List<Host> qcloudHosts = qcloudFetcher.getAllInstancesSet();
message += hostInfoService.compareHostInfo(qcloudHosts,2);
if(StringUtils.isNotBlank(message)){
return new BaseResponse(message);
}
return new BaseResponse<>();
}
@RequestMapping("/saveHostTags")
@ResponseBody
public BaseResponse<Integer> saveHostTags(@RequestBody SetHostTagsReq req) {
String hostIds = req.getHostIds();
String tags = req.getTags();
log.info("saveHostTags with param is {}, {}", hostIds, tags);
List<Integer> hostIdList = JSON.parseArray(hostIds,Integer.class);
return hostInfoService.saveHostTags(hostIdList,tags);
}
}
... ...
... ... @@ -4,6 +4,7 @@ import com.model.HostInfo;
import com.monitor.model.request.HostInfoReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.PageResponse;
import com.yoho.ops.cmdb.models.Host;
import java.util.List;
... ... @@ -35,4 +36,8 @@ public interface IHostInfoService {
BaseResponse saveHostListInfo(List<HostInfo> hostInfos);
BaseResponse removeByIps(List<String> removeIps);
String compareHostInfo(List<Host> cloudHosts, int cloudType);
BaseResponse<Integer> saveHostTags(List<Integer> hostIdList, String tags);
}
... ...
... ... @@ -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);
}
}
... ...
package com.monitor.model.request;
import lombok.Data;
/**
* Created by meiling.ge on 2017/11/8.
*/
@Data
public class SetHostTagsReq {
private String hostIds;
private String tags;
}
... ...
... ... @@ -40,4 +40,6 @@ public interface HostInfoMapper {
int updateHostInfoByIp(HostInfo record);
int deleteByIp(String ip);
List<String> selectHostIpByCloud(@Param("cloudType") int cloudType);
}
\ No newline at end of file
... ...
... ... @@ -94,13 +94,13 @@
<if test="alias != null" >
alias = #{alias,jdbcType=VARCHAR},
</if>
<if test="hostIp != null" >
<if test="hostIp != null " >
host_ip = #{hostIp,jdbcType=VARCHAR},
</if>
<if test="groupId != null" >
<if test="groupId != null &amp;&amp; groupId != 0 " >
group_id = #{groupId,jdbcType=INTEGER},
</if>
<if test="cloudType != null" >
<if test="cloudType != null &amp;&amp; cloudType != 0" >
cloud_type = #{cloudType,jdbcType=BIT},
</if>
<if test="tags != null" >
... ... @@ -141,7 +141,10 @@
<if test="params.hostIp != null &amp;&amp; params.hostIp != ''" >
and a.host_ip = #{params.hostIp}
</if>
<if test="params.tagsList != null &amp;&amp; params.tagsList != ''" >
<if test=" params.tags == 'isNull'" >
and ( ISNULL(a.tags) or trim(a.tags) = "")
</if>
<if test="params.tagsList != null &amp;&amp; params.tagsList.size()>0" >
and
<foreach collection="params.tagsList" index="index" item="item" separator="and">
instr(a.tags, #{item}) &gt; 0
... ... @@ -164,7 +167,10 @@
<if test="params.hostIp != null &amp;&amp; params.hostIp != ''" >
and a.host_ip = #{params.hostIp}
</if>
<if test="params.tagsList != null &amp;&amp; params.tagsList.size > 0" >
<if test=" params.tags == 'isNull'" >
and ( ISNULL(a.tags) or trim(a.tags) = "")
</if>
<if test="params.tagsList != null &amp;&amp; params.tagsList.size() > 0" >
and
<foreach collection="params.tagsList" index="index" item="item" separator="and">
instr(a.tags, #{item}) &gt; 0
... ... @@ -266,4 +272,10 @@
delete from host_info
where host_ip = #{ip,jdbcType=VARCHAR}
</delete>
<select id="selectHostIpByCloud" parameterType="java.lang.Integer" resultType="java.lang.String">
select distinct(host_ip)
from host_info
where cloud_type = #{cloudType,jdbcType=INTEGER}
</select>
</mapper>
\ No newline at end of file
... ...