|
|
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;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|