Authored by fanzelei

update

@@ -76,7 +76,7 @@ public class ZkMonitorCtrl { @@ -76,7 +76,7 @@ public class ZkMonitorCtrl {
76 */ 76 */
77 @RequestMapping("/getZkMonitorDetail") 77 @RequestMapping("/getZkMonitorDetail")
78 @ResponseBody 78 @ResponseBody
79 - public BaseResponse<PageResponse<ZkConfigAll>> getZkMonitorDetail(@RequestBody ZkConfigAll req) throws Exception { 79 + public BaseResponse<PageResponse<ZkConfigAll>> getZkMonitorDetail(@RequestBody ZkTreeAllReq req) throws Exception {
80 log.debug("getZkMonitorTree with req is {}",req); 80 log.debug("getZkMonitorTree with req is {}",req);
81 PageResponse<ZkConfigAll> list = zkMoitorService.getZkMonitorDetail(req); 81 PageResponse<ZkConfigAll> list = zkMoitorService.getZkMonitorDetail(req);
82 if(list==null){ 82 if(list==null){
@@ -17,7 +17,7 @@ public interface IZkMoitorService { @@ -17,7 +17,7 @@ public interface IZkMoitorService {
17 17
18 PageResponse<ZkConfig> getZkMonitorTree(ZkTreeReq req); 18 PageResponse<ZkConfig> getZkMonitorTree(ZkTreeReq req);
19 19
20 - PageResponse<ZkConfigAll> getZkMonitorDetail(ZkConfigAll req); 20 + PageResponse<ZkConfigAll> getZkMonitorDetail(ZkTreeAllReq req);
21 21
22 int editZkMonitorDetail(ZkTreeAllReq req); 22 int editZkMonitorDetail(ZkTreeAllReq req);
23 23
@@ -22,12 +22,10 @@ import com.model.ZkConfigAll; @@ -22,12 +22,10 @@ import com.model.ZkConfigAll;
22 import com.monitor.cmdb.service.IZkMoitorService; 22 import com.monitor.cmdb.service.IZkMoitorService;
23 import com.monitor.influxdb.mapper.IZkMapper; 23 import com.monitor.influxdb.mapper.IZkMapper;
24 import com.monitor.influxdb.model.ZkInfo; 24 import com.monitor.influxdb.model.ZkInfo;
25 -import com.monitor.model.domain.PageBean;  
26 import com.monitor.model.request.ZkTreeAllReq; 25 import com.monitor.model.request.ZkTreeAllReq;
27 import com.monitor.model.request.ZkTreeReq; 26 import com.monitor.model.request.ZkTreeReq;
28 import com.monitor.model.response.PageResponse; 27 import com.monitor.model.response.PageResponse;
29 import com.monitor.mysql.mapper.MObjectInfoMapper; 28 import com.monitor.mysql.mapper.MObjectInfoMapper;
30 -import com.monitor.mysql.mapper.ZkMonitorConfigMapper;  
31 29
32 /** 30 /**
33 * Created by yoho on 2016/6/22. 31 * Created by yoho on 2016/6/22.
@@ -44,8 +42,6 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { @@ -44,8 +42,6 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
44 @Autowired 42 @Autowired
45 MObjectInfoMapper mObjectInfoMapper; 43 MObjectInfoMapper mObjectInfoMapper;
46 44
47 - @Autowired  
48 - ZkMonitorConfigMapper zkMonitorConfigMapper;  
49 45
50 @Override 46 @Override
51 public Map<String, Object> getZkMonitorRecords() { 47 public Map<String, Object> getZkMonitorRecords() {
@@ -101,7 +97,10 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { @@ -101,7 +97,10 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
101 zkConfig.setName(chi); 97 zkConfig.setName(chi);
102 zkConfig.setRoot(rootString); 98 zkConfig.setRoot(rootString);
103 zkConfig.setIp(req.getIp()); 99 zkConfig.setIp(req.getIp());
  100 + if(chi.equals("config")){
104 list.add(zkConfig); 101 list.add(zkConfig);
  102 + }
  103 +
105 page++; 104 page++;
106 } 105 }
107 106
@@ -120,26 +119,68 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { @@ -120,26 +119,68 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
120 } 119 }
121 120
122 @Override 121 @Override
123 - public PageResponse<ZkConfigAll> getZkMonitorDetail(ZkConfigAll req) { 122 + public PageResponse<ZkConfigAll> getZkMonitorDetail(ZkTreeAllReq req) {
124 123
125 PageResponse<ZkConfigAll> response = new PageResponse<ZkConfigAll>(); 124 PageResponse<ZkConfigAll> response = new PageResponse<ZkConfigAll>();
126 List<ZkConfigAll> list = new ArrayList<ZkConfigAll>(); 125 List<ZkConfigAll> list = new ArrayList<ZkConfigAll>();
127 - int pageNum=0;  
128 - // 组装分页对象  
129 - PageBean page = PageBean.initPageInfo(req.getCurrentPage(),  
130 - req.getPageSize(), req);  
131 - pageNum = zkMonitorConfigMapper.getZkConfigCount(req);  
132 -  
133 - list=zkMonitorConfigMapper.getZkConfigAll(page.getStartIndex(),page.getPageSize(),req.getIp(),req.getZkPath());  
134 - 126 + int page=0;
  127 + RetryPolicy retryPolicy = new RetryOneTime(1000);
  128 + CuratorFramework client = CuratorFrameworkFactory.newClient(req.getIp()+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
  129 + client.start();
  130 + try {
  131 + list=getAllChildren(req.getZkPath(),client,req.getIp());
  132 + for(ZkConfigAll zk:list){
  133 + zk.getCurrentPage();
  134 + page++;
  135 + }
  136 + if (list.size()>10&&req.getCurrentPage()*10<list.size()) {
  137 + list=list.subList(req.getCurrentPage()*10-10, req.getCurrentPage()*10);
  138 + }else if (req.getCurrentPage()*10>list.size()) {
  139 + list=list.subList(req.getCurrentPage()*10-10, list.size());
  140 + }
135 response.setCurrentPage(req.getCurrentPage()); 141 response.setCurrentPage(req.getCurrentPage());
136 response.setPageSize(req.getPageSize()); 142 response.setPageSize(req.getPageSize());
137 - response.setTotal(pageNum); 143 + response.setTotal(page);
138 response.setRows(list); 144 response.setRows(list);
  145 + } catch (Exception e) {
  146 + logger.error("getZkMonitorDetail fail with ip is {} and path is {}",req.getIp(),req.getZkPath());
  147 + e.printStackTrace();
  148 + }finally {
  149 + client.close();
  150 + }
139 return response; 151 return response;
140 152
141 } 153 }
142 154
  155 + public List<ZkConfigAll> getAllChildren(String parentPath,CuratorFramework client,String ip) {
  156 + List<ZkConfigAll> childList=new ArrayList<ZkConfigAll>();
  157 + try {
  158 + //取该路径下的所有子节点
  159 + List<String> childNodeNames=client.getChildren().forPath(parentPath);
  160 + for(String nodeName: childNodeNames){
  161 + //拼接路径
  162 + String nodePath=parentPath.concat("/").concat(nodeName);
  163 +
  164 + ZkConfigAll zkNode=new ZkConfigAll();
  165 + zkNode.setZkPath(nodePath);
  166 + zkNode.setIp(ip);
  167 + String data =new String(client.getData().forPath(nodePath),"UTF-8");
  168 + zkNode.setZkValue(data);
  169 + List<String> liString = client.getChildren().forPath(nodePath);
  170 + if(CollectionUtils.isEmpty(liString)){
  171 + childList.add(zkNode);
  172 + }else {
  173 + //递归
  174 + childList.addAll(getAllChildren(nodePath, client,ip));
  175 + }
  176 + }
  177 +
  178 + } catch (Exception e) {
  179 + e.printStackTrace();
  180 + }
  181 + return childList;
  182 + }
  183 +
143 184
144 185
145 186
1 -package com.monitor.middleware.zookeeper.service;  
2 -  
3 -  
4 -  
5 -public interface IZkMonitorConfigService {  
6 -  
7 - void getAllConfigData();  
8 -  
9 -}  
1 -package com.monitor.middleware.zookeeper.service.iml;  
2 -  
3 -import java.util.ArrayList;  
4 -import java.util.List;  
5 -  
6 -import org.apache.curator.RetryPolicy;  
7 -import org.apache.curator.framework.CuratorFramework;  
8 -import org.apache.curator.framework.CuratorFrameworkFactory;  
9 -import org.apache.curator.retry.RetryOneTime;  
10 -import org.slf4j.Logger;  
11 -import org.slf4j.LoggerFactory;  
12 -import org.springframework.beans.factory.annotation.Autowired;  
13 -import org.springframework.scheduling.annotation.EnableScheduling;  
14 -import org.springframework.stereotype.Service;  
15 -import org.springframework.util.CollectionUtils;  
16 -  
17 -import com.model.MObjectInfo;  
18 -import com.model.TypeInfo;  
19 -import com.model.ZkConfigAll;  
20 -import com.monitor.middleware.zookeeper.service.IZkMonitorConfigService;  
21 -import com.monitor.mysql.mapper.MObjectInfoMapper;  
22 -import com.monitor.mysql.mapper.MTypeInfoMapper;  
23 -import com.monitor.mysql.mapper.ZkMonitorConfigMapper;  
24 -  
25 -@EnableScheduling  
26 -@Service  
27 -public class ZkMonitorConfigServiceImpl implements IZkMonitorConfigService{  
28 -  
29 - Logger Log = LoggerFactory.getLogger(ZkMonitorConfigServiceImpl.class);  
30 -  
31 - @Autowired  
32 - MTypeInfoMapper mTypeInfoMapper;  
33 -  
34 - @Autowired  
35 - MObjectInfoMapper mObjectInfoMapper;  
36 -  
37 - @Autowired  
38 - ZkMonitorConfigMapper zkMonitorConfigMapper;  
39 -  
40 - @Override  
41 - public void getAllConfigData(){  
42 - List<TypeInfo> listType = getZkConfigData();  
43 - List<ZkConfigAll> list = new ArrayList<ZkConfigAll>();  
44 - RetryPolicy retryPolicy = new RetryOneTime(1000);  
45 - zkMonitorConfigMapper.deleteZkConfig();  
46 - for(TypeInfo info : listType){  
47 - CuratorFramework client = CuratorFrameworkFactory.newClient(info.getTypeName()+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);  
48 - client.start();  
49 - try {  
50 - list=getAllChildren("/yh",client,info.getTypeName());  
51 - zkMonitorConfigMapper.insertZkMonitor(list);  
52 - } catch (Exception e) {  
53 - Log.error("insertZkMonitor fail");  
54 - e.printStackTrace();  
55 - }finally {  
56 - client.close();  
57 - }  
58 - }  
59 - }  
60 -  
61 - public List<TypeInfo> getZkConfigData() {  
62 -  
63 - List<TypeInfo> list= new ArrayList<TypeInfo>();  
64 - TypeInfo typeInfo=mTypeInfoMapper.selectTypeInfoByName("zookeeper");  
65 - List<TypeInfo> listInfo=mTypeInfoMapper.getChildTypesInfo(typeInfo.getTypeId());  
66 - for(TypeInfo info:listInfo){  
67 - List<TypeInfo> listType=mTypeInfoMapper.getChildTypesInfo(info.getTypeId());  
68 - for(TypeInfo infos:listType){  
69 - List<MObjectInfo> mObjectInfos=mObjectInfoMapper.getTypeMosInfo(infos.getTypeId());  
70 - for(MObjectInfo mInfo:mObjectInfos){  
71 - TypeInfo typeInfos = new TypeInfo();  
72 - typeInfos.setTypeName(mInfo.getMoHostIp());  
73 - typeInfos.setTypeParentId(infos.getTypeId());  
74 - list.add(typeInfos);  
75 - }  
76 - }  
77 - }  
78 - return list;  
79 - }  
80 -  
81 -  
82 - public List<ZkConfigAll> getAllChildren(String parentPath,CuratorFramework client,String ip) {  
83 - List<ZkConfigAll> childList=new ArrayList<ZkConfigAll>();  
84 - try {  
85 - //取该路径下的所有子节点  
86 - List<String> childNodeNames=client.getChildren().forPath(parentPath);  
87 - for(String nodeName: childNodeNames){  
88 - //拼接路径  
89 - String nodePath=parentPath.concat("/").concat(nodeName);  
90 -  
91 - ZkConfigAll zkNode=new ZkConfigAll();  
92 - zkNode.setZkPath(nodePath);  
93 - zkNode.setIp(ip);  
94 - String data =new String(client.getData().forPath(nodePath),"UTF-8");  
95 - zkNode.setZkValue(data);  
96 - List<String> liString = client.getChildren().forPath(nodePath);  
97 - if(CollectionUtils.isEmpty(liString)){  
98 - childList.add(zkNode);  
99 - }else {  
100 - //递归  
101 - childList.addAll(getAllChildren(nodePath, client,ip));  
102 - }  
103 - }  
104 -  
105 - } catch (Exception e) {  
106 - e.printStackTrace();  
107 - }  
108 - return childList;  
109 - }  
110 -  
111 -}  
1 -package com.monitor.middleware.zookeeper.task;  
2 -  
3 -import org.springframework.beans.factory.annotation.Autowired;  
4 -import org.springframework.scheduling.annotation.Scheduled;  
5 -import org.springframework.stereotype.Component;  
6 -  
7 -import com.monitor.middleware.zookeeper.service.IZkMonitorConfigService;  
8 -  
9 -  
10 -@Component  
11 -public class ZkMonitorConfigTask {  
12 -  
13 - @Autowired  
14 - IZkMonitorConfigService zkMonitorConfigService;  
15 -  
16 -// @Scheduled(fixedRate=20000)  
17 - @Scheduled(cron="0 0/30 * * * ? ")  
18 - public void zookeeperMonitor() {  
19 - zkMonitorConfigService.getAllConfigData();  
20 - }  
21 -}  
1 -package com.monitor.mysql.mapper;  
2 -  
3 -import java.util.List;  
4 -  
5 -import org.apache.ibatis.annotations.Param;  
6 -  
7 -import com.model.ZkConfigAll;  
8 -  
9 -  
10 -  
11 -public interface ZkMonitorConfigMapper {  
12 -  
13 - int insertZkMonitor(@Param("list") List<ZkConfigAll> list);  
14 -  
15 - List<ZkConfigAll> getZkConfigAll(@Param("startIndex")int startIndex ,@Param("pageSize")int pageSize, @Param("ip")String ip, @Param("zkPath")String zkPath);  
16 -  
17 - int getZkConfigCount(ZkConfigAll zkConfig);  
18 -  
19 - int deleteZkConfig();  
20 -}  
1 -<?xml version="1.0" encoding="UTF-8" ?>  
2 -<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >  
3 -<mapper namespace="com.monitor.mysql.mapper.ZkMonitorConfigMapper" >  
4 - <resultMap id="BaseResultMap" type="com.model.ZkConfigAll" >  
5 - <id column="id" property="id" jdbcType="BIGINT" />  
6 - <result column="zk_root" property="zkPath" jdbcType="VARCHAR" />  
7 - <result column="zk_data" property="zkValue" jdbcType="VARCHAR" />  
8 - <result column="ip" property="ip" jdbcType="VARCHAR" />  
9 - </resultMap>  
10 - <sql id="Base_Column_List" >  
11 - id, zk_root, zk_data, ip  
12 - </sql>  
13 -  
14 - <select id="getZkConfigCount" resultType="java.lang.Integer" >  
15 - select count(*)  
16 - from zk_node_info  
17 - where ip = #{ip,jdbcType=VARCHAR} and instr(zk_root, #{zkPath})  
18 - </select>  
19 - <select id="getZkConfigAll" resultMap="BaseResultMap" >  
20 - select  
21 - <include refid="Base_Column_List" />  
22 - from zk_node_info  
23 - where ip = #{ip,jdbcType=VARCHAR} and instr(zk_root, #{zkPath}) limit #{startIndex},#{pageSize}  
24 - </select>  
25 - <insert id="insertZkMonitor" parameterType="com.model.ZkConfigAll" >  
26 - insert into zk_node_info  
27 - (zk_root, zk_data,ip)  
28 - values  
29 - <foreach collection="list" item="item" index="index"  
30 - separator=",">  
31 - (#{item.zkPath},#{item.zkValue},#{item.ip})  
32 - </foreach>  
33 - </insert>  
34 -  
35 - <delete id="deleteZkConfig">  
36 - delete from zk_node_info  
37 - </delete>  
38 -  
39 -</mapper>