Authored by fanzelei

update

... ... @@ -76,7 +76,7 @@ public class ZkMonitorCtrl {
*/
@RequestMapping("/getZkMonitorDetail")
@ResponseBody
public BaseResponse<PageResponse<ZkConfigAll>> getZkMonitorDetail(@RequestBody ZkConfigAll req) throws Exception {
public BaseResponse<PageResponse<ZkConfigAll>> getZkMonitorDetail(@RequestBody ZkTreeAllReq req) throws Exception {
log.debug("getZkMonitorTree with req is {}",req);
PageResponse<ZkConfigAll> list = zkMoitorService.getZkMonitorDetail(req);
if(list==null){
... ...
... ... @@ -17,7 +17,7 @@ public interface IZkMoitorService {
PageResponse<ZkConfig> getZkMonitorTree(ZkTreeReq req);
PageResponse<ZkConfigAll> getZkMonitorDetail(ZkConfigAll req);
PageResponse<ZkConfigAll> getZkMonitorDetail(ZkTreeAllReq req);
int editZkMonitorDetail(ZkTreeAllReq req);
... ...
... ... @@ -22,12 +22,10 @@ import com.model.ZkConfigAll;
import com.monitor.cmdb.service.IZkMoitorService;
import com.monitor.influxdb.mapper.IZkMapper;
import com.monitor.influxdb.model.ZkInfo;
import com.monitor.model.domain.PageBean;
import com.monitor.model.request.ZkTreeAllReq;
import com.monitor.model.request.ZkTreeReq;
import com.monitor.model.response.PageResponse;
import com.monitor.mysql.mapper.MObjectInfoMapper;
import com.monitor.mysql.mapper.ZkMonitorConfigMapper;
/**
* Created by yoho on 2016/6/22.
... ... @@ -44,8 +42,6 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
@Autowired
MObjectInfoMapper mObjectInfoMapper;
@Autowired
ZkMonitorConfigMapper zkMonitorConfigMapper;
@Override
public Map<String, Object> getZkMonitorRecords() {
... ... @@ -101,7 +97,10 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
zkConfig.setName(chi);
zkConfig.setRoot(rootString);
zkConfig.setIp(req.getIp());
list.add(zkConfig);
if(chi.equals("config")){
list.add(zkConfig);
}
page++;
}
... ... @@ -120,26 +119,68 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
}
@Override
public PageResponse<ZkConfigAll> getZkMonitorDetail(ZkConfigAll req) {
public PageResponse<ZkConfigAll> getZkMonitorDetail(ZkTreeAllReq req) {
PageResponse<ZkConfigAll> response = new PageResponse<ZkConfigAll>();
List<ZkConfigAll> list = new ArrayList<ZkConfigAll>();
int pageNum=0;
// 组装分页对象
PageBean page = PageBean.initPageInfo(req.getCurrentPage(),
req.getPageSize(), req);
pageNum = zkMonitorConfigMapper.getZkConfigCount(req);
list=zkMonitorConfigMapper.getZkConfigAll(page.getStartIndex(),page.getPageSize(),req.getIp(),req.getZkPath());
response.setCurrentPage(req.getCurrentPage());
response.setPageSize(req.getPageSize());
response.setTotal(pageNum);
response.setRows(list);
int page=0;
RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(req.getIp()+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
client.start();
try {
list=getAllChildren(req.getZkPath(),client,req.getIp());
for(ZkConfigAll zk:list){
zk.getCurrentPage();
page++;
}
if (list.size()>10&&req.getCurrentPage()*10<list.size()) {
list=list.subList(req.getCurrentPage()*10-10, req.getCurrentPage()*10);
}else if (req.getCurrentPage()*10>list.size()) {
list=list.subList(req.getCurrentPage()*10-10, list.size());
}
response.setCurrentPage(req.getCurrentPage());
response.setPageSize(req.getPageSize());
response.setTotal(page);
response.setRows(list);
} catch (Exception e) {
logger.error("getZkMonitorDetail fail with ip is {} and path is {}",req.getIp(),req.getZkPath());
e.printStackTrace();
}finally {
client.close();
}
return response;
}
public List<ZkConfigAll> getAllChildren(String parentPath,CuratorFramework client,String ip) {
List<ZkConfigAll> childList=new ArrayList<ZkConfigAll>();
try {
//取该路径下的所有子节点
List<String> childNodeNames=client.getChildren().forPath(parentPath);
for(String nodeName: childNodeNames){
//拼接路径
String nodePath=parentPath.concat("/").concat(nodeName);
ZkConfigAll zkNode=new ZkConfigAll();
zkNode.setZkPath(nodePath);
zkNode.setIp(ip);
String data =new String(client.getData().forPath(nodePath),"UTF-8");
zkNode.setZkValue(data);
List<String> liString = client.getChildren().forPath(nodePath);
if(CollectionUtils.isEmpty(liString)){
childList.add(zkNode);
}else {
//递归
childList.addAll(getAllChildren(nodePath, client,ip));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return childList;
}
... ...
package com.monitor.middleware.zookeeper.service;
public interface IZkMonitorConfigService {
void getAllConfigData();
}
package com.monitor.middleware.zookeeper.service.iml;
import java.util.ArrayList;
import java.util.List;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryOneTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.model.MObjectInfo;
import com.model.TypeInfo;
import com.model.ZkConfigAll;
import com.monitor.middleware.zookeeper.service.IZkMonitorConfigService;
import com.monitor.mysql.mapper.MObjectInfoMapper;
import com.monitor.mysql.mapper.MTypeInfoMapper;
import com.monitor.mysql.mapper.ZkMonitorConfigMapper;
@EnableScheduling
@Service
public class ZkMonitorConfigServiceImpl implements IZkMonitorConfigService{
Logger Log = LoggerFactory.getLogger(ZkMonitorConfigServiceImpl.class);
@Autowired
MTypeInfoMapper mTypeInfoMapper;
@Autowired
MObjectInfoMapper mObjectInfoMapper;
@Autowired
ZkMonitorConfigMapper zkMonitorConfigMapper;
@Override
public void getAllConfigData(){
List<TypeInfo> listType = getZkConfigData();
List<ZkConfigAll> list = new ArrayList<ZkConfigAll>();
RetryPolicy retryPolicy = new RetryOneTime(1000);
zkMonitorConfigMapper.deleteZkConfig();
for(TypeInfo info : listType){
CuratorFramework client = CuratorFrameworkFactory.newClient(info.getTypeName()+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
client.start();
try {
list=getAllChildren("/yh",client,info.getTypeName());
zkMonitorConfigMapper.insertZkMonitor(list);
} catch (Exception e) {
Log.error("insertZkMonitor fail");
e.printStackTrace();
}finally {
client.close();
}
}
}
public List<TypeInfo> getZkConfigData() {
List<TypeInfo> list= new ArrayList<TypeInfo>();
TypeInfo typeInfo=mTypeInfoMapper.selectTypeInfoByName("zookeeper");
List<TypeInfo> listInfo=mTypeInfoMapper.getChildTypesInfo(typeInfo.getTypeId());
for(TypeInfo info:listInfo){
List<TypeInfo> listType=mTypeInfoMapper.getChildTypesInfo(info.getTypeId());
for(TypeInfo infos:listType){
List<MObjectInfo> mObjectInfos=mObjectInfoMapper.getTypeMosInfo(infos.getTypeId());
for(MObjectInfo mInfo:mObjectInfos){
TypeInfo typeInfos = new TypeInfo();
typeInfos.setTypeName(mInfo.getMoHostIp());
typeInfos.setTypeParentId(infos.getTypeId());
list.add(typeInfos);
}
}
}
return list;
}
public List<ZkConfigAll> getAllChildren(String parentPath,CuratorFramework client,String ip) {
List<ZkConfigAll> childList=new ArrayList<ZkConfigAll>();
try {
//取该路径下的所有子节点
List<String> childNodeNames=client.getChildren().forPath(parentPath);
for(String nodeName: childNodeNames){
//拼接路径
String nodePath=parentPath.concat("/").concat(nodeName);
ZkConfigAll zkNode=new ZkConfigAll();
zkNode.setZkPath(nodePath);
zkNode.setIp(ip);
String data =new String(client.getData().forPath(nodePath),"UTF-8");
zkNode.setZkValue(data);
List<String> liString = client.getChildren().forPath(nodePath);
if(CollectionUtils.isEmpty(liString)){
childList.add(zkNode);
}else {
//递归
childList.addAll(getAllChildren(nodePath, client,ip));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return childList;
}
}
package com.monitor.middleware.zookeeper.task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.monitor.middleware.zookeeper.service.IZkMonitorConfigService;
@Component
public class ZkMonitorConfigTask {
@Autowired
IZkMonitorConfigService zkMonitorConfigService;
// @Scheduled(fixedRate=20000)
@Scheduled(cron="0 0/30 * * * ? ")
public void zookeeperMonitor() {
zkMonitorConfigService.getAllConfigData();
}
}
package com.monitor.mysql.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.model.ZkConfigAll;
public interface ZkMonitorConfigMapper {
int insertZkMonitor(@Param("list") List<ZkConfigAll> list);
List<ZkConfigAll> getZkConfigAll(@Param("startIndex")int startIndex ,@Param("pageSize")int pageSize, @Param("ip")String ip, @Param("zkPath")String zkPath);
int getZkConfigCount(ZkConfigAll zkConfig);
int deleteZkConfig();
}
\ 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.ZkMonitorConfigMapper" >
<resultMap id="BaseResultMap" type="com.model.ZkConfigAll" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="zk_root" property="zkPath" jdbcType="VARCHAR" />
<result column="zk_data" property="zkValue" jdbcType="VARCHAR" />
<result column="ip" property="ip" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, zk_root, zk_data, ip
</sql>
<select id="getZkConfigCount" resultType="java.lang.Integer" >
select count(*)
from zk_node_info
where ip = #{ip,jdbcType=VARCHAR} and instr(zk_root, #{zkPath})
</select>
<select id="getZkConfigAll" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from zk_node_info
where ip = #{ip,jdbcType=VARCHAR} and instr(zk_root, #{zkPath}) limit #{startIndex},#{pageSize}
</select>
<insert id="insertZkMonitor" parameterType="com.model.ZkConfigAll" >
insert into zk_node_info
(zk_root, zk_data,ip)
values
<foreach collection="list" item="item" index="index"
separator=",">
(#{item.zkPath},#{item.zkValue},#{item.ip})
</foreach>
</insert>
<delete id="deleteZkConfig">
delete from zk_node_info
</delete>
</mapper>
\ No newline at end of file