Showing
9 changed files
with
223 additions
and
56 deletions
@@ -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 ZkTreeAllReq req) throws Exception { | 79 | + public BaseResponse<PageResponse<ZkConfigAll>> getZkMonitorDetail(@RequestBody ZkConfigAll 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(ZkTreeAllReq req); | 20 | + PageResponse<ZkConfigAll> getZkMonitorDetail(ZkConfigAll req); |
21 | 21 | ||
22 | int editZkMonitorDetail(ZkTreeAllReq req); | 22 | int editZkMonitorDetail(ZkTreeAllReq req); |
23 | 23 |
@@ -22,10 +22,12 @@ import com.model.ZkConfigAll; | @@ -22,10 +22,12 @@ 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; | ||
25 | import com.monitor.model.request.ZkTreeAllReq; | 26 | import com.monitor.model.request.ZkTreeAllReq; |
26 | import com.monitor.model.request.ZkTreeReq; | 27 | import com.monitor.model.request.ZkTreeReq; |
27 | import com.monitor.model.response.PageResponse; | 28 | import com.monitor.model.response.PageResponse; |
28 | import com.monitor.mysql.mapper.MObjectInfoMapper; | 29 | import com.monitor.mysql.mapper.MObjectInfoMapper; |
30 | +import com.monitor.mysql.mapper.ZkMonitorConfigMapper; | ||
29 | 31 | ||
30 | /** | 32 | /** |
31 | * Created by yoho on 2016/6/22. | 33 | * Created by yoho on 2016/6/22. |
@@ -41,6 +43,9 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { | @@ -41,6 +43,9 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { | ||
41 | 43 | ||
42 | @Autowired | 44 | @Autowired |
43 | MObjectInfoMapper mObjectInfoMapper; | 45 | MObjectInfoMapper mObjectInfoMapper; |
46 | + | ||
47 | + @Autowired | ||
48 | + ZkMonitorConfigMapper zkMonitorConfigMapper; | ||
44 | 49 | ||
45 | @Override | 50 | @Override |
46 | public Map<String, Object> getZkMonitorRecords() { | 51 | public Map<String, Object> getZkMonitorRecords() { |
@@ -115,68 +120,28 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { | @@ -115,68 +120,28 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { | ||
115 | } | 120 | } |
116 | 121 | ||
117 | @Override | 122 | @Override |
118 | - public PageResponse<ZkConfigAll> getZkMonitorDetail(ZkTreeAllReq req) { | 123 | + public PageResponse<ZkConfigAll> getZkMonitorDetail(ZkConfigAll req) { |
119 | 124 | ||
120 | PageResponse<ZkConfigAll> response = new PageResponse<ZkConfigAll>(); | 125 | PageResponse<ZkConfigAll> response = new PageResponse<ZkConfigAll>(); |
121 | List<ZkConfigAll> list = new ArrayList<ZkConfigAll>(); | 126 | List<ZkConfigAll> list = new ArrayList<ZkConfigAll>(); |
122 | - int page=0; | ||
123 | - RetryPolicy retryPolicy = new RetryOneTime(1000); | ||
124 | - CuratorFramework client = CuratorFrameworkFactory.newClient(req.getIp()+ ":2181", 5 * 1000, 5 * 1000, retryPolicy); | ||
125 | - client.start(); | ||
126 | - try { | ||
127 | - list=getAllChildren(req.getZkPath(),client,req.getIp()); | ||
128 | - for(ZkConfigAll zk:list){ | ||
129 | - zk.getCurrentPage(); | ||
130 | - page++; | ||
131 | - } | ||
132 | - if (list.size()>10&&req.getCurrentPage()*10<list.size()) { | ||
133 | - list=list.subList(req.getCurrentPage()*10-10, req.getCurrentPage()*10); | ||
134 | - }else if (req.getCurrentPage()*10>list.size()) { | ||
135 | - list=list.subList(req.getCurrentPage()*10-10, list.size()); | ||
136 | - } | ||
137 | - response.setCurrentPage(req.getCurrentPage()); | ||
138 | - response.setPageSize(req.getPageSize()); | ||
139 | - response.setTotal(page); | ||
140 | - response.setRows(list); | ||
141 | - } catch (Exception e) { | ||
142 | - logger.error("getZkMonitorDetail fail with ip is {} and path is {}",req.getIp(),req.getZkPath()); | ||
143 | - e.printStackTrace(); | ||
144 | - }finally { | ||
145 | - client.close(); | ||
146 | - } | 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 | + | ||
135 | + response.setCurrentPage(req.getCurrentPage()); | ||
136 | + response.setPageSize(req.getPageSize()); | ||
137 | + response.setTotal(pageNum); | ||
138 | + response.setRows(list); | ||
147 | return response; | 139 | return response; |
148 | 140 | ||
149 | } | 141 | } |
150 | 142 | ||
151 | 143 | ||
152 | - public List<ZkConfigAll> getAllChildren(String parentPath,CuratorFramework client,String ip) { | ||
153 | - List<ZkConfigAll> childList=new ArrayList<ZkConfigAll>(); | ||
154 | - try { | ||
155 | - //取该路径下的所有子节点 | ||
156 | - List<String> childNodeNames=client.getChildren().forPath(parentPath); | ||
157 | - for(String nodeName: childNodeNames){ | ||
158 | - //拼接路径 | ||
159 | - String nodePath=parentPath.concat("/").concat(nodeName); | ||
160 | - | ||
161 | - ZkConfigAll zkNode=new ZkConfigAll(); | ||
162 | - zkNode.setZkPath(nodePath); | ||
163 | - zkNode.setIp(ip); | ||
164 | - String data =new String(client.getData().forPath(nodePath),"UTF-8"); | ||
165 | - zkNode.setZkValue(data); | ||
166 | - List<String> liString = client.getChildren().forPath(nodePath); | ||
167 | - if(CollectionUtils.isEmpty(liString)){ | ||
168 | - childList.add(zkNode); | ||
169 | - }else { | ||
170 | - //递归 | ||
171 | - childList.addAll(getAllChildren(nodePath, client,ip)); | ||
172 | - } | ||
173 | - } | ||
174 | - | ||
175 | - } catch (Exception e) { | ||
176 | - e.printStackTrace(); | ||
177 | - } | ||
178 | - return childList; | ||
179 | - } | 144 | + |
180 | 145 | ||
181 | @Override | 146 | @Override |
182 | public int editZkMonitorDetail(ZkTreeAllReq req) { | 147 | public int editZkMonitorDetail(ZkTreeAllReq req) { |
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 | +} |
@@ -7,6 +7,8 @@ import lombok.Data; | @@ -7,6 +7,8 @@ import lombok.Data; | ||
7 | import com.monitor.model.page.PageRequest; | 7 | import com.monitor.model.page.PageRequest; |
8 | @Data | 8 | @Data |
9 | public class ZkConfigAll extends PageRequest implements Serializable { | 9 | public class ZkConfigAll extends PageRequest implements Serializable { |
10 | + | ||
11 | + private Integer id; | ||
10 | 12 | ||
11 | private String ip; | 13 | private String ip; |
12 | 14 |
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 | +} |
monitor-service-mysql/src/main/resources/com/monitor/mysql/mapper/ZkMonitorConfigMapper.xml
0 → 100644
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> |
-
Please register or login to post a comment