|
|
package com.monitor.cmdb.service.impl;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.Comparator;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Properties;
|
|
|
import java.util.Set;
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
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.apache.curator.utils.ZKPaths;
|
|
|
import org.apache.zookeeper.CreateMode;
|
|
|
import org.apache.zookeeper.KeeperException;
|
|
|
import org.slf4j.Logger;
|
...
|
...
|
@@ -97,6 +103,73 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { |
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public PageResponse<ZkConfig> getZkConfigCenterTree(ZkTreeReq req) {
|
|
|
String hostIp = getHostIp(req.getIp());
|
|
|
PageResponse<ZkConfig> response = new PageResponse<ZkConfig>();
|
|
|
List<ZkConfig> resultList = new ArrayList<ZkConfig>();
|
|
|
String root = "/yh/config";
|
|
|
String rootString = "";
|
|
|
|
|
|
RetryPolicy retryPolicy = new RetryOneTime(1000);
|
|
|
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
|
|
|
client.start();
|
|
|
try {
|
|
|
getConfigInfoRecurse(root, "", req, 0, resultList, client);
|
|
|
|
|
|
response.setCurrentPage(req.getCurrentPage());
|
|
|
response.setPageSize(req.getPageSize());
|
|
|
response.setTotal(resultList.size());
|
|
|
response.setRows(resultList);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
logger.error("getZkConfigCenterTree fail with ip is {}",req.getIp(), e);
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
client.close();
|
|
|
}
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 递归获取所有根节点
|
|
|
* @param parentPath
|
|
|
* @param parentName
|
|
|
* @param req
|
|
|
* @param level 层数
|
|
|
* @param resultList
|
|
|
* @param client
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
private void getConfigInfoRecurse(String parentPath, String parentName, ZkTreeReq req, int level, List<ZkConfig> resultList, CuratorFramework client) throws Exception{
|
|
|
List<String> children = client.getChildren().forPath(parentPath);
|
|
|
String val = new String(client.getData().forPath(parentPath),"UTF-8");
|
|
|
if (CollectionUtils.isEmpty(children) && StringUtils.isNotEmpty(val)) {
|
|
|
return;
|
|
|
}
|
|
|
if (StringUtils.isNotEmpty(parentName) && parentName.indexOf(".") > -1) {
|
|
|
return;
|
|
|
}
|
|
|
if (level > 0) {
|
|
|
ZkConfig zkConfig = new ZkConfig();
|
|
|
if (level == 1) {
|
|
|
zkConfig.setName(parentName);
|
|
|
} else {
|
|
|
zkConfig.setName("");
|
|
|
}
|
|
|
zkConfig.setRoot(parentPath);
|
|
|
zkConfig.setIp(req.getIp());
|
|
|
resultList.add(zkConfig);
|
|
|
}
|
|
|
if (CollectionUtils.isEmpty(children)) {
|
|
|
return;
|
|
|
}
|
|
|
level ++;
|
|
|
for (String name: children) {
|
|
|
getConfigInfoRecurse(ZKPaths.makePath(parentPath, name), name, req, level, resultList, client);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public PageResponse<ZkConfig> getZkMonitorTree4Log4j(ZkTreeReq req) {
|
|
|
return getZkMonitorTreeWithPath(req,"log4j");
|
|
|
}
|
...
|
...
|
@@ -153,6 +226,7 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { |
|
|
client.start();
|
|
|
try {
|
|
|
list=getAllChildren(req.getZkPath(),client,req.getIp());
|
|
|
sortByZkName(list);
|
|
|
for(ZkConfigAll zk:list){
|
|
|
zk.getCurrentPage();
|
|
|
page++;
|
...
|
...
|
@@ -170,6 +244,28 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { |
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
private void sortByZkName(List<ZkConfigAll> itemList) {
|
|
|
if (CollectionUtils.isEmpty(itemList)) {
|
|
|
return;
|
|
|
}
|
|
|
//顺序:空、数字、字母
|
|
|
Collections.sort(itemList, new Comparator<ZkConfigAll>() {
|
|
|
@Override
|
|
|
public int compare(ZkConfigAll o1, ZkConfigAll o2) {
|
|
|
if (Objects.equals(o1.getZkName(), o2.getZkName()) || (StringUtils.isEmpty(o1.getZkName()) && StringUtils.isEmpty(o2.getZkName()))) {
|
|
|
return 0;
|
|
|
}
|
|
|
if (null == o1.getZkName()) {
|
|
|
return -1;
|
|
|
}
|
|
|
if (null == o2.getZkName()) {
|
|
|
return 1;
|
|
|
}
|
|
|
return o1.getZkName().toLowerCase().compareTo(o2.getZkName().toLowerCase());
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
public List<ZkConfigAll> getAllChildren(String parentPath,CuratorFramework client,String ip) {
|
|
|
List<ZkConfigAll> childList=new ArrayList<ZkConfigAll>();
|
...
|
...
|
@@ -267,6 +363,60 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { |
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int addZkMonitorRoot(ZkTreeReq req) {
|
|
|
String hostIp = getHostIp(req.getIp());
|
|
|
int result = 0;
|
|
|
RetryPolicy retryPolicy = new RetryOneTime(1000);
|
|
|
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
|
|
|
client.start();
|
|
|
try {
|
|
|
if (null == client.checkExists().forPath(req.getRoot())) {
|
|
|
client.create().creatingParentsIfNeeded().forPath(req.getRoot(), new byte[]{});
|
|
|
result = 1;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error("addZkMonitorRoot fail with oldRoot is {}",req.getRoot());
|
|
|
} finally {
|
|
|
client.close();
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int batchImport(ZkTreeReq req) {
|
|
|
String hostIp = getHostIp(req.getIp());
|
|
|
String root = req.getRoot();
|
|
|
int cnt = 0;
|
|
|
RetryPolicy retryPolicy = new RetryOneTime(1000);
|
|
|
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
|
|
|
client.start();
|
|
|
try {
|
|
|
Properties properties = req.getProperties();
|
|
|
Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();
|
|
|
for (Map.Entry<Object, Object> ins: entrySet) {
|
|
|
String key = String.valueOf(ins.getKey());
|
|
|
String value = String.valueOf(ins.getValue());
|
|
|
if (StringUtils.isEmpty(key)) {
|
|
|
continue;
|
|
|
}
|
|
|
String path = ZKPaths.makePath(root, key);
|
|
|
if(null == client.checkExists().forPath(path)) {
|
|
|
client.create().withMode(CreateMode.PERSISTENT).forPath(path, value.getBytes("UTF-8"));
|
|
|
} else {
|
|
|
client.setData().forPath(path, value.getBytes("UTF-8"));
|
|
|
}
|
|
|
cnt ++;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error("batchImport fail with root is {}", root);
|
|
|
} finally {
|
|
|
client.close();
|
|
|
}
|
|
|
|
|
|
return cnt;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int editAndCreate(ZkTreeAllReq req) {
|
|
|
String hostIp = getHostIp(req.getIp());
|
|
|
int result = 0;
|
...
|
...
|
|