Authored by qinchao

docker monitor

... ... @@ -19,4 +19,6 @@ public class DockerInfoReq extends PageRequest {
private String projectType;//项目类型
private String clusterId;
}
... ...
package com.model;
import lombok.Data;
@Data
public class DockerMonitorClusterModel {
//集群id,主键
private String clusterId;
//监控标志
private String monitorFlag;
//描述
private String descr;
}
... ...
package com.model;
import lombok.Data;
@Data
public class DockerMonitorServiceModel {
//主键
private int id;
//服务名称
private String serviceName;
//命名空间
private String namespace;
//集群id,
private String clusterId;
//监控标志
private String monitorFlag;
//描述
private String descr;
}
... ...
package com.monitor.mysql.mapper;
import com.model.DockerMonitorClusterModel;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* Created by craig.qin
*/
public interface DockerMonitorClusterMapper {
List<DockerMonitorClusterModel> selectAll();
List<DockerMonitorClusterModel> selectAllMonitor();
int updateMonitorFlag(@Param("clusterId") String clusterId ,@Param("monitorFlag") String monitorFlag);
}
... ...
package com.monitor.mysql.mapper;
import com.model.DockerMonitorServiceModel;
import com.monitor.model.domain.PageBean;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* Created by craig.qin
*/
public interface DockerMonitorServiceMapper {
int selectCountByCodition(PageBean req);
List<DockerMonitorServiceModel> selectDockerServiceByCodition(PageBean page);
List<DockerMonitorServiceModel> selectDockerServiceByClusterId(String clusterId);
int deleteAll();
int deleteById(Integer id);
int deleteExcludeCluster(@Param("includeClusterList") List<String> includeClusterList);
int updateMonitorFlagByCluster(@Param("clusterId") String clusterId, @Param("monitorFlag") String monitorFlag);
int updateMonitorFlagById(@Param("id") int id, @Param("monitorFlag") String monitorFlag);
int insert(DockerMonitorServiceModel model);
}
... ...
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.monitor.mysql.mapper.DockerMonitorClusterMapper" >
<resultMap id="BaseResultMap" type="com.model.DockerMonitorClusterModel" >
<id column="clusterid" property="clusterId" jdbcType="VARCHAR" />
<result column="monitor_flag" property="monitorFlag" jdbcType="VARCHAR" />
<result column="descr" property="descr" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
clusterid,monitor_flag,descr
</sql>
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from docker_monitor_cluster
</select>
<select id="selectAllMonitor" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from docker_monitor_cluster
where monitor_flag='1'
</select>
<update id="updateMonitorFlag" >
update docker_monitor_cluster
set monitor_flag = #{monitorFlag}
where clusterid = #{clusterId}
</update>
</mapper>
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.monitor.mysql.mapper.DockerMonitorServiceMapper" >
<resultMap id="BaseResultMap" type="com.model.DockerMonitorServiceModel" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="servicename" property="serviceName" jdbcType="VARCHAR" />
<result column="namespace" property="namespace" jdbcType="VARCHAR" />
<result column="clusterid" property="clusterId" jdbcType="VARCHAR" />
<result column="monitor_flag" property="monitorFlag" jdbcType="VARCHAR" />
<result column="descr" property="descr" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id,servicename,namespace,clusterid,monitor_flag,descr
</sql>
<update id="updateMonitorFlagByCluster" >
update docker_monitor_service
set monitor_flag = #{monitorFlag}
where clusterid = #{clusterId}
</update>
<update id="updateMonitorFlagById" >
update docker_monitor_service
set monitor_flag = #{monitorFlag}
where id = #{id}
</update>
<select id="selectCountByCodition" resultType="java.lang.Integer">
select
count(1)
from docker_monitor_service
where 1=1
<if test="params.clusterId != null &amp;&amp; params.clusterId != ''" >
and clusterid = #{params.clusterId}
</if>
</select>
<select id="selectDockerServiceByCodition" resultMap="BaseResultMap">
select * from docker_monitor_service
where 1=1
<if test="params.clusterId != null &amp;&amp; params.clusterId != ''" >
and clusterid = #{params.clusterId}
</if>
order by clusterid
limit #{startIndex},#{pageSize}
</select>
<select id="selectDockerServiceByClusterId" resultMap="BaseResultMap">
select * from docker_monitor_service
where clusterid = #{clusterId}
</select>
<delete id="deleteAll">
delete from docker_monitor_service
</delete>
<delete id="deleteById" parameterType="java.lang.Integer" >
delete from docker_monitor_service
where id = #{id,jdbcType=INTEGER}
</delete>
<delete id="deleteExcludeCluster">
delete from docker_monitor_service
where clusterid not in
<foreach item="clsid" index="index" collection="includeClusterList" open="("
separator=" , " close=")">
#{clsid}
</foreach>
</delete>
<insert id="insert" parameterType="com.model.DockerMonitorServiceModel" >
insert into docker_monitor_service (servicename,namespace,clusterid,monitor_flag,descr)
values ( #{serviceName,jdbcType=VARCHAR}, #{namespace,jdbcType=VARCHAR},
#{clusterId,jdbcType=VARCHAR}, #{monitorFlag,jdbcType=VARCHAR}, #{descr,jdbcType=VARCHAR})
</insert>
</mapper>
\ No newline at end of file
... ...
package com.monitor.other.dockermonitor;
import com.model.DockerMonitorClusterModel;
import com.model.DockerMonitorServiceModel;
import com.model.DockerProjectModel;
import com.monitor.model.request.DockerInfoReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.PageResponse;
import com.monitor.mysql.mapper.DockerMonitorClusterMapper;
import com.monitor.mysql.mapper.DockerMonitorServiceMapper;
import com.monitor.other.dockermonitor.service.DockerMonitorServiceImpl;
import com.monitor.other.worksystem.dock.qq.DockerServerDeployService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
@Controller
@RequestMapping("/dockerMonitor")
public class DockerMonitorCtrl {
@Autowired
private DockerMonitorClusterMapper dockerMonitorClusterMapper;
@Autowired
private DockerMonitorServiceMapper dockerMonitorServiceMapper;
@Autowired
private DockerMonitorServiceImpl dockerMonitorService;
@Autowired
private DockerServerDeployService dockerServerDeployService;
/**
* 查询docker 的所有集群信息
*/
@RequestMapping("/getDockerClusterAllPage")
@ResponseBody
public BaseResponse<PageResponse<DockerMonitorClusterModel>> getDockerClusterAllPage() {
List<DockerMonitorClusterModel> ls = dockerMonitorClusterMapper.selectAll();
if (ls == null || CollectionUtils.isEmpty(ls)) {
return new BaseResponse<>();
}
PageResponse<DockerMonitorClusterModel> response = new PageResponse<>();
response.setCurrentPage(1);
response.setRows(ls);
response.setPageSize(1);
response.setTotal(ls.size());
return new BaseResponse<>(response);
}
@RequestMapping("/getDockerService")
@ResponseBody
public BaseResponse<PageResponse<DockerMonitorServiceModel>> getDockerService(@RequestBody DockerInfoReq req) {
PageResponse<DockerMonitorServiceModel> resp = dockerMonitorService.getDockerService(req);
if (resp == null || CollectionUtils.isEmpty(resp.getRows())) {
return new BaseResponse<>();
}
PageResponse<DockerMonitorServiceModel> response = new PageResponse<>();
response.setCurrentPage(resp.getCurrentPage());
response.setRows(resp.getRows());
response.setPageSize(resp.getPageSize());
response.setTotal(resp.getTotal());
return new BaseResponse<>(response);
}
@RequestMapping("/syncDockerService")
@ResponseBody
public BaseResponse syncDockerService(){
List<DockerMonitorClusterModel> clusterIds = dockerMonitorClusterMapper.selectAll();
//根据集群 ,获取集群下的所有命名空间,根据命名空间和集群,获取所有服务。
if(clusterIds!=null&&clusterIds.size()>0){
List<String> clusterIdList=new ArrayList<>();
for(DockerMonitorClusterModel clusterModel:clusterIds){
String monitorFlag=clusterModel.getMonitorFlag();
String clusterId=clusterModel.getClusterId();
clusterIdList.add(clusterId);
//把当前集群下的所有服务查询出来
List<DockerMonitorServiceModel> oldServiceList=dockerMonitorServiceMapper.selectDockerServiceByClusterId(clusterId);
Map<String,DockerMonitorServiceModel> oldServiceMap=new HashMap<>();
if(oldServiceList!=null&&oldServiceList.size()>0){
for(DockerMonitorServiceModel model:oldServiceList){
oldServiceMap.put(model.getServiceName()+model.getClusterId()+model.getNamespace(),model);
}
}
List<String> namespaceList=dockerServerDeployService.queryNamespaceByClusterId(clusterId);
for(String namespace:namespaceList){
List<String> serviceList=dockerServerDeployService.queryServiceUnderClusterIdNamespace(clusterId,namespace);
for(String serviceName:serviceList){
DockerMonitorServiceModel model=new DockerMonitorServiceModel();
model.setServiceName(serviceName);
model.setClusterId(clusterId);
model.setNamespace(namespace);
model.setMonitorFlag(monitorFlag);
String key=model.getServiceName()+model.getClusterId()+model.getNamespace();
//如果不存在,则新增
if(!oldServiceMap.containsKey(key)){
dockerMonitorServiceMapper.insert(model);
}else{
oldServiceMap.remove(key);
}
}
}
//移除不存在的
for(String key:oldServiceMap.keySet()){
dockerMonitorServiceMapper.deleteById(oldServiceMap.get(key).getId());
}
}
//删除不存在的集群
dockerMonitorServiceMapper.deleteExcludeCluster(clusterIdList);
}else{
//删除所有
dockerMonitorServiceMapper.deleteAll();
}
return new BaseResponse();
}
@RequestMapping("/updateClusterMonitorFlag")
@ResponseBody
public BaseResponse updateClusterMonitorFlag(String clusterId,String monitorFlag){
dockerMonitorClusterMapper.updateMonitorFlag(clusterId,monitorFlag);
//更新服务信息
dockerMonitorServiceMapper.updateMonitorFlagByCluster(clusterId,monitorFlag);
return new BaseResponse();
}
@RequestMapping("/updateServiceMonitorFlag")
@ResponseBody
public BaseResponse updateServiceMonitorFlag(int id,String monitorFlag){
//更新服务信息
dockerMonitorServiceMapper.updateMonitorFlagById(id,monitorFlag);
return new BaseResponse();
}
}
... ...
package com.monitor.other.dockermonitor.service;
import com.model.DockerMonitorServiceModel;
import com.model.DockerProjectModel;
import com.monitor.model.domain.PageBean;
import com.monitor.model.request.DockerInfoReq;
import com.monitor.model.response.PageResponse;
import com.monitor.mysql.mapper.DockerMonitorServiceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
@Service
public class DockerMonitorServiceImpl {
@Autowired
private DockerMonitorServiceMapper dockerMonitorServiceMapper;
public PageResponse<DockerMonitorServiceModel> getDockerService( DockerInfoReq req) {
try{
// 组装分页对象
PageBean page = PageBean.initPageInfo(req.getCurrentPage(), req.getPageSize(), req);
// 先查询符合条件的总数量
int total = dockerMonitorServiceMapper.selectCountByCodition(page);
// 数量为0 直接返回
if (total == 0) {
// 返回初始page对象
return null;
}
// 获取列表
List<DockerMonitorServiceModel> dockerServices = dockerMonitorServiceMapper.selectDockerServiceByCodition(page);
if (CollectionUtils.isEmpty(dockerServices)) {
return null;
}
PageResponse<DockerMonitorServiceModel> response = new PageResponse<>();
response.setCurrentPage(req.getCurrentPage());
response.setPageSize(req.getPageSize());
response.setTotal(total);
response.setRows(dockerServices);
return response;
}catch (Exception e){
return null;
}
}
}
... ...
... ... @@ -26,6 +26,37 @@ public class DockerServerDeployService {
private static final Logger logger = LoggerFactory.getLogger(DockerServerDeployService.class);
/**
* 查询集群的所有命名空间
*/
public List<String> queryNamespaceByClusterId(String clusterId){
List<String> ls=new ArrayList<>();
JSONObject searchResult=txServer.describeClusterNameSpaces(clusterId);
if(searchResult!=null&&searchResult.getInteger("code")!=null&& searchResult.getInteger("code").equals(NumberUtils.INTEGER_ZERO)){
JSONArray jaServices=searchResult.getJSONObject("data").getJSONArray("namespaces");
for(int i=0;i<jaServices.size();i++){
ls.add(jaServices.getJSONObject(i).getString("name"));
}
}
return ls;
}
/**
* 查询集群的所有命名空间
*/
public List<String> queryServiceUnderClusterIdNamespace(String clusterId,String namespace){
List<String> ls=new ArrayList<>();
JSONObject searchResult=txServer.describeClusterService(clusterId,namespace);
if(searchResult!=null&&searchResult.getInteger("code")!=null&& searchResult.getInteger("code").equals(NumberUtils.INTEGER_ZERO)){
JSONArray jaServices=searchResult.getJSONObject("data").getJSONArray("services");
for(int i=0;i<jaServices.size();i++){
ls.add(jaServices.getJSONObject(i).getString("serviceName"));
}
}
return ls;
}
/**
* 检查是否存在服务
* @param checkedServiceName 需要检查的服务名称
... ...
... ... @@ -24,6 +24,30 @@ public class QqDockerServiceUtils {
@Autowired
private CcsApiQcloud ccsApiQcloud;
/**
* 用于查询集群的命名空间
*/
public JSONObject describeClusterNameSpaces(String clusterId) {
JSONObject resultJson=null;
try{
Map<String, String> paraMap = Maps.newHashMap();
paraMap.put("Action", "DescribeClusterNameSpaces");
paraMap.put("clusterId", clusterId);
String backString = ccsApiQcloud.pushTest(paraMap);
String uri=requestUrl+"?"+backString;
String qqResponseJsonStr =HttpClientUtil.doget(uri,null,null);
if(StringUtils.isNotBlank(qqResponseJsonStr)){
resultJson = JSONObject.parseObject(qqResponseJsonStr);
}
}catch (Exception e){
logger.error("describeClusterNameSpaces error",e);
}
return resultJson;
}
/**
* 查询服务列表,该接口返回的列表只包含服务的扼要信息,
* 如果需要获取详细的服务信息,需要调用 DescribeClusterServiceInfo 接口
... ...