Authored by qinchao

zk比较

... ... @@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
... ... @@ -144,19 +145,29 @@ public class ZkMonitorCtrl {
}
/**
* 修改根节点的路径
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/editZkMonitorRoot")
/* @RequestMapping("/editZkMonitorRoot")
@ResponseBody
public BaseResponse<Object> editZkMonitorRoot(@RequestBody ZkTreeReq req) throws Exception {
log.debug("editZkMonitorRoot with req is {}",req);
int result = zkMoitorService.editZkMonitorRoot(req);
return new BaseResponse<Object>(result);
}*/
@RequestMapping("/compareRootTest")
@ResponseBody
public BaseResponse<Object> compareRootTest() throws Exception {
String oldRoot="/yh/service";
String compareRoot="/yh/config";
List<List<String>> node=new ArrayList<>();
List<String> notExistsNode=new ArrayList<>();
List<String> valueNotEqualNode=new ArrayList<>();
node.add(notExistsNode);
node.add(valueNotEqualNode);
zkMoitorService.compareRootTest(oldRoot,compareRoot,node);
return new BaseResponse<Object>(node);
}
}
... ...
... ... @@ -28,7 +28,9 @@ public interface IZkMoitorService {
String getZkValueByPath(ZkTreeAllReq req);
int editZkMonitorRoot(ZkTreeReq req);
void compareRootTest(String oldRoot ,String compareRoot ,List<List<String>> node );
//int editZkMonitorRoot(ZkTreeReq req);
int editAndCreate(ZkTreeAllReq req);
... ...
... ... @@ -17,6 +17,7 @@ import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.utils.ZKPaths;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -324,6 +325,53 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
return value;
}
@Override
public void compareRootTest(String oldRoot ,String compareRoot ,List<List<String>> node ){
CuratorFramework client = zkOnlineClientUtil.getOnlineCuratorFrameworkByKey("zookeeper_aws");
//
//String oldRoot="/yh/service";
//String compareRoot="/yh/config";
List<String> notExistsNode = node.get(0);
List<String> valueNotEqualNode=node.get(0);
try {
//取该路径下的所有子节点
List<String> childNodeNames=client.getChildren().forPath(oldRoot);
for(String nodeName: childNodeNames)
{
//拼接路径
String nodePath=oldRoot.concat("/").concat(nodeName);
String data =new String(client.getData().forPath(nodePath),"UTF-8");
String newPath=compareRoot.concat("/").concat(nodeName);
Stat stat = client.checkExists().forPath(newPath);
if(stat==null){
notExistsNode.add(newPath);
}else{
String newdata =new String(client.getData().forPath(newPath),"UTF-8");
if(newdata==null||data==null){
}else if(newdata!=null&&data!=null&&newdata.equals(data)){
}else{
valueNotEqualNode.add(newPath);
}
}
List<String> liString = client.getChildren().forPath(nodePath);
if(CollectionUtils.isEmpty(liString)){
//递归
compareRootTest(nodePath,newPath,node);
}
}
} catch (Exception e) {
logger.error("getAllChildren fail");
e.printStackTrace();
}
}
private void setValueForZkConfig(CuratorFramework client,List<ZkConfigAll> list){
if(list!=null&&list.size()>0){
try {
... ... @@ -363,13 +411,13 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
return result;
}
@Override
/*@Override
public int editZkMonitorRoot(ZkTreeReq req) {
//String hostIp = getHostIp(req.getIp());
int result = 0;
/*RetryPolicy retryPolicy = new RetryOneTime(1000);
*//*RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
client.start();*/
client.start();*//*
CuratorFramework client=zkOnlineClientUtil.getOnlineCuratorFrameworkByKey(req.getIp());
try {
... ... @@ -405,7 +453,7 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
//client.close();
}
return result;
}
}*/
@Override
public int editAndCreate(ZkTreeAllReq req) {
... ...
package com.monitor.cmdb.ctrl;
package com.monitor.middleware.mongo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.monitor.common.service.AlarmMsgService;
import com.util.GetUsersInfoUtil;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by meiling.ge on 2018/3/22.
*/
@Controller
@RequestMapping("/testMongo")
public class TestMongoController {
private static final Logger LOGGER = LoggerFactory.getLogger(TestMongoController.class);
@Component
public class MongoTask {
private static final Logger LOGGER = LoggerFactory.getLogger(MongoTask.class);
@Value("${mongo.host}")
private String host;
@Value("${mongo.databaseName}")
... ... @@ -35,19 +38,31 @@ public class TestMongoController {
@Value("${mongo.port}")
private int port;
@Autowired
AlarmMsgService alarmMsgService;
@Autowired
private GetUsersInfoUtil getUsersInfoUtil;
//当前环境
@Value("${system.envi}")
private String env;
/**
* 3分钟监控一次
*
* 探测mongodb
* 执行语句: db.job_log.find({ "beginTime" : {"$gt" : new Date('2018/03/16 06:15:00'), "$lt" : new Date('2018/03/16 07:15:00')},success: false },{"name": 1,"success": 1 });
* 等价于:select name , success from job_log where beginTime > new Date('2018/03/16 06:15:00') and beginTime < new Date('2018/03/16 07:15:00') and success = false
* 结束时间:当前的时间 起始时间:当前时间向前推一个小时
* @return
*/
@RequestMapping("/detectMongo")
@ResponseBody
public String detectMongo(){
LOGGER.info("detectMongo -- start ");
@Scheduled(cron = "0 */60 * * * ?")
public void alarm() {
if("test1".equals(env)||"test".equals(env)){
return ;
}
MongoClient mongoClient = null;
String resultStr = "";
try {
/*// 连接数据库--需要用户名 &密码
MongoCredential credential = MongoCredential.createCredential(user, databaseName, pswd.toCharArray());
... ... @@ -68,26 +83,32 @@ public class TestMongoController {
Bson queryCondition3 = Filters.gt("beginTime", new Date(startTime));
Bson queryBson = Filters.and(queryCondition1, queryCondition2, queryCondition3);
FindIterable<Document> result = doc.find(queryBson);
LOGGER.info("detectMongo excute query: host is {}, databaseName is {} , collectionName is {} , queryBson is {}", host, databaseName, collectionName, queryBson );
LOGGER.info("detectMongo excute query: host is {}, databaseName is {} , collectionName is {} , queryJson is {}", host, databaseName, collectionName, queryBson );
String alarmMobile=getUsersInfoUtil.getMobileByAlarmGroup("DBA");
//游标遍历 结果
List<String> resultStr=new ArrayList<>();
MongoCursor<Document> cursor = result.iterator();
while(cursor.hasNext()){
Document d = cursor.next();
resultStr += d.toJson() ;
String jsonString=d.toJson();
resultStr.add(jsonString);
String alarmContent=null;
try{
JSONObject jo =JSON.parseObject(jsonString);
if(jo!=null&&jo.containsKey("name")){
alarmContent= jo.getString("name");
}
}catch (Exception e){
LOGGER.error("MongoTask change json error ",e);
alarmContent = jsonString;
}
//报警
alarmMsgService.sendSms("mongo_exception", "mongo探测到异常"+alarmContent,
alarmMobile);
}
LOGGER.info("detectMongo -- success");
LOGGER.info("detectMongo -- success {}",JSON.toJSONString(resultStr));
//再查一遍
queryCondition1 = Filters.eq("success", true);
result = doc.find(Filters.and(queryCondition1, queryCondition2, queryCondition3));
cursor = result.iterator();
resultStr += "success==true:";
while(cursor.hasNext()){
Document d = cursor.next();
resultStr += d.toJson() ;
}
} catch (Exception e) {
LOGGER.error("detectMongo--error: e : {}" , e);
... ... @@ -97,7 +118,9 @@ public class TestMongoController {
LOGGER.info("detectMongo-- mongoClient.close");
}
}
return resultStr;
}
}
... ...