|
|
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();
|
|
|
}
|
|
|
} |
...
|
...
|
|