Commit 36c08bdf authored by gaozhentao's avatar gaozhentao

新增历史记录检索接口、修改告警记录、告警信息数据表结构、修改接口返回信息

parent 2b951996
......@@ -59,6 +59,14 @@ public class ReliabilityApi {
public Map<String, Object> getAlarmLog(@RequestBody String json) {
return manager.getAlarmLog(json);
}
@PostMapping("getAlarmMsg")
public Map<String, Object> getAlarmMsg() {
return manager.getAlarmMsg();
}
@PostMapping("searchLog")
public Map<String, Object> searchLog(@RequestBody String json) {
return manager.searchLog(json);
}
@PostMapping("login")
public Map<String, Object> login(@RequestBody String json) {
......
......@@ -9,10 +9,8 @@ public interface Constant {
/* 已告警 */
String ALARM_ING = "1";
/* 告警恢复 */
/* 告警解除 */
String ALARM_RECOVERY = "0";
/* 告警发生变化 */
String ALARM_CHANGE = "-1";
/* 告警级别 不稳定 */
String ALARM_UNSTABLE = "unstable";
/* 告警级别 宕机 */
......
......@@ -47,8 +47,9 @@ public class WebConfiguration implements WebMvcConfigurer {
.excludePathPatterns("/js/**")
.excludePathPatterns("/index.html")
.excludePathPatterns("/rest/rel/login")
.excludePathPatterns("/rest/rel/changeSwitch")
.excludePathPatterns("/rest/rel/getSwitch");
.excludePathPatterns("/rest/rel/getAlarmLog")
.excludePathPatterns("/rest/rel/searchLog")
.excludePathPatterns("/rest/rel/getAlarmMsg");
WebMvcConfigurer.super.addInterceptors(registry);
}
......
package com.yingxin.prms.domain;
import org.springframework.data.cassandra.core.cql.Ordering;
import org.springframework.data.cassandra.core.cql.PrimaryKeyType;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.core.mapping.Table;
/**
......@@ -12,9 +15,13 @@ import org.springframework.data.cassandra.core.mapping.Table;
@Table(value = "alarm_log")
public class AlarmLog {
/* 1 告警 0 告警恢复 -1 告警发生变化*/
@PrimaryKey
private String state;
/* 告警序列号 */
private String serialnum;
/* 告警时间 */
private String alatime;
private String ip;
private String port;
......@@ -27,12 +34,9 @@ public class AlarmLog {
@Column("cluster_name")
private String clusterName;
/* 告警时间 */
private String alatime;
/* 恢复时间 */
private String rectime;
/* 1 告警 0 告警恢复 -1 告警发生变化*/
private String state;
public String getSerialnum() {
return serialnum;
......
package com.yingxin.prms.domain;
import org.springframework.data.cassandra.core.cql.Ordering;
import org.springframework.data.cassandra.core.cql.PrimaryKeyType;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.core.mapping.Table;
/**
* Description: 告警记录
* Datetime: 2020/10/21 13:16
* Author: gaozhentao
*/
@Table(value = "alarm_msg")
public class AlarmMsg {
/* 1 告警 0 告警恢复 -1 告警发生变化*/
@PrimaryKey
private String state;
/* 告警序列号 */
private String serialnum;
/* 告警时间 */
private String alatime;
private String ip;
private String port;
/* ok 正常 unstable 不稳定 down 宕机 */
@Column("alarm_level")
private String alarmLevel;
/* */
@Column("alarm_message")
private String alarmMessage;
@Column("cluster_name")
private String clusterName;
public String getSerialnum() {
return serialnum;
}
public void setSerialnum(String serialnum) {
this.serialnum = serialnum;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getAlarmLevel() {
return alarmLevel;
}
public void setAlarmLevel(String alarmLevel) {
this.alarmLevel = alarmLevel;
}
public String getAlarmMessage() {
return alarmMessage;
}
public void setAlarmMessage(String alarmMessage) {
this.alarmMessage = alarmMessage;
}
public String getAlatime() {
return alatime;
}
public void setAlatime(String alatime) {
this.alatime = alatime;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getClusterName() {
return clusterName;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
@Override
public String toString() {
return "AlarmLog{" +
"serialnum='" + serialnum + '\'' +
", ip='" + ip + '\'' +
", port='" + port + '\'' +
", alarmLevel='" + alarmLevel + '\'' +
", alarmMessage='" + alarmMessage + '\'' +
", clusterName='" + clusterName + '\'' +
", alatime='" + alatime + '\'' +
", state='" + state + '\'' +
'}';
}
}
......@@ -3,6 +3,7 @@ package com.yingxin.prms.service.asyncTask;
import com.yingxin.prms.config.Constant;
import com.yingxin.prms.config.filter.TokenInterceptor;
import com.yingxin.prms.domain.AlarmLog;
import com.yingxin.prms.domain.AlarmMsg;
import com.yingxin.prms.domain.Server_Host_Info;
import com.yingxin.prms.dto.wx.MonitorConfig;
import com.yingxin.prms.service.dao.CassandraDaoImpl;
......@@ -96,31 +97,32 @@ public class AlarmTask {
String uuid = UUID.randomUUID().toString();
AlarmLog alarmLog = new AlarmLog();
alarmLog.setSerialnum(uuid);
alarmLog.setClusterName(hostInfo.getCluster_name());
alarmLog.setIp(hostInfo.getHost_ip());
alarmLog.setPort(String.valueOf(hostInfo.getHost_port()));
AlarmLog alarmCheck = cassandraDao.checkAlarm(hostInfo.getCluster_name(),hostInfo.getHost_ip(), String.valueOf(hostInfo.getHost_port()));
AlarmMsg alarmMsg = new AlarmMsg();
alarmMsg.setClusterName(hostInfo.getCluster_name());
alarmMsg.setIp(hostInfo.getHost_ip());
alarmMsg.setPort(String.valueOf(hostInfo.getHost_port()));
AlarmMsg alarmCheck = cassandraDao.checkAlarm(hostInfo.getCluster_name(),hostInfo.getHost_ip(), String.valueOf(hostInfo.getHost_port()));
String msg = "";
boolean flag = true;
if (faultCount5Min != 0 ) {
/* 不稳定告警 */
alarmLog.setAlatime(TimeUtil.getDate());
alarmLog.setState(Constant.ALARM_ING);
alarmLog.setAlarmLevel(Constant.ALARM_UNSTABLE);
alarmMsg.setState(Constant.ALARM_ING);
alarmMsg.setAlarmLevel(Constant.ALARM_UNSTABLE);
msg ="系统不稳定"+" " + hostInfo.getCluster_name() + " " + hostInfo.getHost_name()
+ " " + hostInfo.getHost_ip() + " 5分钟内服务存在调用失败";
alarmLog.setAlarmMessage(msg);
alarmMsg.setAlarmMessage(msg);
flag = false;
}
if(faultCount1Min >= correctCount1Min || dataPoints5Min.size() == 0 ) {
/* 宕机告警 */
alarmLog.setAlatime(TimeUtil.getDate());
alarmLog.setState(Constant.ALARM_ING);
alarmLog.setAlarmLevel(Constant.ALARM_DOWN);
alarmMsg.setState(Constant.ALARM_ING);
alarmMsg.setAlarmLevel(Constant.ALARM_DOWN);
msg ="系统宕机"+ " " + hostInfo.getCluster_name() + " " + hostInfo.getHost_name()
+ " " + hostInfo.getHost_ip() + " 连续1分钟服务调用失败";
......@@ -131,7 +133,7 @@ public class AlarmTask {
if(flag){
if(alarmCheck != null){
/* 如果存在告警 则恢复 */
cassandraDao.updateAlarm(alarmCheck.getSerialnum(),TimeUtil.getDate());
cassandraDao.insertAlarmLog(alarmMsg,TimeUtil.getDate());
String recoveryMsg = alarmCheck.getClusterName() + " " + alarmCheck.getIp()
+ ":" + alarmCheck.getPort() + ",告警已恢复";
monitorConfig.setAlarmMsg("【告警恢复】"+recoveryMsg);
......@@ -143,7 +145,8 @@ public class AlarmTask {
}else{
if(alarmCheck == null){
/* 如果不存在告警 新增数据 */
cassandraDao.insertAlarm(alarmLog);
cassandraDao.insertAlarmMsg(alarmMsg);
monitorConfig.setAlarmMsg(alarmLog.getAlarmMessage());
if(TokenInterceptor.currSwitch){
MonitorUtil.wxAlarm(monitorConfig);
......@@ -151,8 +154,12 @@ public class AlarmTask {
}else {
/* 如果告警发生变化 回复时间变成告警时间 */
if(!alarmCheck.getAlarmLevel().equals(alarmLog.getAlarmLevel())) {
cassandraDao.updateAlarm(alarmCheck.getSerialnum(),"------");
cassandraDao.insertAlarm(alarmLog);
/* 之前告警取消,重新发送新的告警 */
cassandraDao.insertAlarmLog(alarmCheck,"-------");
cassandraDao.insertAlarmMsg(alarmMsg);
/* 记录本次告警 */
alarmLog.setAlarmLevel(Constant.ALARM_OK);
alarmLog.setState(Constant.ALARM_RECOVERY);
monitorConfig.setAlarmMsg("【告警内容更新】"+alarmLog.getAlarmMessage());
if(TokenInterceptor.currSwitch){
MonitorUtil.wxAlarm(monitorConfig);
......
......@@ -121,9 +121,6 @@ public class HttpTask extends BasicTask{
default:
flag = checkStatusCode(map);
}
if(!flag){
logger.warn("错误:"+hostInfo.getService_path()+", body:"+map.get("body"));
}
if (map.get("body") != null && !map.get("body").isEmpty() ) {
insertToKairos(httpClient, hostInfo.getService_path(), dur, flag);
logger.info("httpTask to {}|{}", hostInfo.getHost_ip(), flag);
......
......@@ -51,7 +51,7 @@ public class UdpTask extends BasicTask{
applicationReturn = receivePacket.getData();
dur = System.currentTimeMillis() - start;
} catch (IOException e) {
logger.debug("error: udpTask to {}", hostInfo.getService_path());
logger.error("error: udpTask to {}", hostInfo.getService_path());
insertToKairos(httpClient, hostInfo.getService_path(), dur, false);
return;
}
......
......@@ -17,6 +17,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.xml.stream.events.StartDocument;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.*;
......@@ -95,19 +96,16 @@ public class Manager {
public Map<String, Object> getAlarmLog(String json) {
Map<String,Object> map = new HashMap();
List<AlarmLog> alarmList = cassandraDao.getAlarmList();
map.put("alarm",alarmList);
JSONObject jsonObject = JSONObject.fromObject(json);
int pageNum = jsonObject.getInt("pageNum");
int pageSize = jsonObject.getInt("pageSize");
int i = 0;
String flag = "";
String flag = String.valueOf(System.currentTimeMillis());
Map total = cassandraDao.getAlarmLogSize();
List<AlarmLog> logList = null;
do{
logList = cassandraDao.getAlarmLog(pageSize,flag);
if (logList.size()==0){
break;
}
......@@ -120,6 +118,13 @@ public class Manager {
map.put("logSize",total.get("count"));
return map;
}
public Map<String, Object> getAlarmMsg() {
Map<String,Object> map = new HashMap();
List<AlarmMsg> alarmList = cassandraDao.getAlarmMsgList();
map.put("alarmMsg",alarmList);
return map;
}
public Map selectClusterStatus() {
/*查询集群监控列表*/
List<Server_Host_Info> host_info_List = authModeAndHostList.getClusterBeMonitoredList();
......@@ -614,6 +619,86 @@ public class Manager {
return map;
}
public Map<String, Object> searchLog(String json) {
Map<String,Object> map = new HashMap();
JSONObject jsonObject = JSONObject.fromObject(json);
int pageNum = jsonObject.getInt("pageNum");
int pageSize = jsonObject.getInt("pageSize");
List<AlarmLog> logList = cassandraDao.getAlarmLogList();
String flag = String.valueOf(System.currentTimeMillis());
if(jsonObject.containsKey("start")&&jsonObject.containsKey("ip")){
String ip = jsonObject.getString("ip");
String start = jsonObject.getString("start");
String end = jsonObject.getString("end");
Map total = cassandraDao.getAlarmLogSize(ip,start,end);
List<AlarmLog> alarmList = null;
int i = 0;
do{
alarmList = cassandraDao.getAlarmLogList(ip,start,end, pageSize,flag);
if (logList.size()==0){
break;
}
AlarmLog alarmLog = logList.get(logList.size()-1);
flag = alarmLog.getSerialnum();
i++;
}while(i<pageNum);
map.put("log",alarmList);
map.put("logSize",total.get("count"));
}else if(jsonObject.containsKey("start")){
String start = jsonObject.getString("start");
String end = jsonObject.getString("end");
Map total = cassandraDao.getAlarmLogSize(start,end);
List<AlarmLog> alarmList = null;
int i = 0;
do{
alarmList = cassandraDao.getAlarmLogList(start,end,pageSize,flag);
if (logList.size()==0){
break;
}
AlarmLog alarmLog = logList.get(logList.size()-1);
flag = alarmLog.getSerialnum();
i++;
}while(i<pageNum);
map.put("log",alarmList);
map.put("logSize",total.get("count"));
}else if(jsonObject.containsKey("ip")){
String ip = jsonObject.getString("ip");
List<AlarmLog> alarmList = null;
Map total = cassandraDao.getAlarmLogSize(ip);
int i = 0;
do{
alarmList = cassandraDao.getAlarmLogList(ip,pageSize,flag);
if (logList.size()==0){
break;
}
AlarmLog alarmLog = logList.get(logList.size()-1);
flag = alarmLog.getSerialnum();
i++;
}while(i<pageNum);
map.put("log",alarmList);
map.put("logSize",total.get("count"));
}else{
Map total = cassandraDao.getAlarmLogSize();
List<AlarmLog> alarmList = null;
int i = 0;
do{
alarmList = cassandraDao.getAlarmLog(pageSize,flag);
if (logList.size()==0){
break;
}
AlarmLog alarmLog = logList.get(logList.size()-1);
flag = alarmLog.getSerialnum();
i++;
}while(i<pageNum);
map.put("log",alarmList);
map.put("logSize",total.get("count"));
}
return map;
}
/*
public Map selectClusterStatus() {
......
......@@ -2,6 +2,7 @@ package com.yingxin.prms.service.dao;
import com.yingxin.prms.config.Constant;
import com.yingxin.prms.domain.*;
import com.yingxin.prms.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.stereotype.Repository;
......@@ -54,29 +55,38 @@ public class CassandraDaoImpl {
// }
public List<AlarmLog> getAlarmLog(int size,String flag) {
String cql="select * from ctid_prof.alarm_log where token(serialnum) > token( '"+flag+"' ) and state < '"+Constant.ALARM_ING+"' limit "+size+ " allow filtering;";
String cql="select * from ctid_prof.alarm_log where state = '"+Constant.ALARM_RECOVERY+"' and serialnum<'"+flag+"' order by alatime desc limit "+size+ " allow filtering;";
return template.select(cql, AlarmLog.class);
}
public List<AlarmLog> getAlarmList() {
String cql="select * from ctid_prof.alarm_log where state = '" +Constant.ALARM_ING+"' allow filtering";
public List<AlarmLog> getAlarmLogList() {
String cql="select * from ctid_prof.alarm_log where state = '" +Constant.ALARM_RECOVERY+"' allow filtering";
return template.select(cql, AlarmLog.class);
}
public AlarmLog checkAlarm(String name,String ip,String port) {
String cql="select * from ctid_prof.alarm_log where cluster_name = '"+name+"' and ip = '"+ip+"' and port = '"+port+"' and state = '" +Constant.ALARM_ING+"' allow filtering";
return template.selectOne(cql, AlarmLog.class);
public List<AlarmMsg> getAlarmMsgList() {
String cql="select * from ctid_prof.alarm_msg where state = '" +Constant.ALARM_ING+"' allow filtering";
return template.select(cql, AlarmMsg.class);
}
public boolean updateAlarm(String serialnum,String time) {
public AlarmMsg checkAlarm(String name,String ip,String port) {
String cql="select * from ctid_prof.alarm_msg where cluster_name = '"+name+"' and ip = '"+ip+"' and port = '"+port+"' and state = '" +Constant.ALARM_ING+"' allow filtering";
return template.selectOne(cql, AlarmMsg.class);
}
/*变更告警信息(已启用)*/
/* public boolean updateAlarm(String serialnum,String time) {
String cql="update ctid_prof.alarm_log set rectime = '"+time+"' , state = '" +Constant.ALARM_RECOVERY +"', alarm_level = '"+Constant.ALARM_OK+"' where serialnum = '"+serialnum +"'";
System.out.println("cql:"+cql);
return template.getCqlOperations().execute(cql);
}
public void insertAlarm(AlarmLog alarmLog) {
}*/
public void insertAlarmLog(AlarmMsg alarmMsg,String time) {
String cql ="INSERT INTO ctid_prof.alarm_log (serialnum, alarm_level, alarm_message, alatime, cluster_name, ip, port, rectime, state)" +
"VALUES('"+alarmLog.getSerialnum()+"', '"+alarmLog.getAlarmLevel()+"', '"+alarmLog.getAlarmMessage()+"', '"+alarmLog.getAlatime()+"', '"+alarmLog.getClusterName()+"', '"+alarmLog.getIp()+"', '"+alarmLog.getPort()+"', '"+alarmLog.getRectime()+"', '"+alarmLog.getState()+"');";
System.out.println("cql:"+cql);
"VALUES('"+System.currentTimeMillis()+"', '"+Constant.ALARM_OK+"', '"+alarmMsg.getAlarmMessage()+"', '"+TimeUtil.getDate()+"', '"+alarmMsg.getClusterName()+"', '"+alarmMsg.getIp()+"', '"+alarmMsg.getPort()+"', '"+time+"', '"+Constant.ALARM_RECOVERY+"');";
template.getCqlOperations().execute(cql);
}
public void insertAlarmMsg(AlarmMsg alarmMsg) {
String cql ="INSERT INTO ctid_prof.alarm_msg (serialnum, alarm_level, alarm_message, alatime, cluster_name, ip, port, state)" +
"VALUES('"+System.currentTimeMillis()+"', '"+alarmMsg.getAlarmLevel()+"', '"+alarmMsg.getAlarmMessage()+"', '"+ TimeUtil.getDate()+"', '"+alarmMsg.getClusterName()+"', '"+alarmMsg.getIp()+"', '"+alarmMsg.getPort()+"', '"+Constant.ALARM_ING+"');";
template.getCqlOperations().execute(cql);
}
public Map getAlarmLogSize() {
......@@ -114,4 +124,34 @@ public class CassandraDaoImpl {
String cql ="SELECT * FROM ctid_prof.alarm_switch WHERE username='admin';";
return template.selectOne(cql,Map.class);
}
public List<AlarmLog> getAlarmLogList(String start, String end,int size,String flag) {
String cql="select * from ctid_prof.alarm_log where alatime > '"+start+"' and alatime < '"+end+"'and state = '" +Constant.ALARM_RECOVERY+"' and serialnum<'"+flag+"' order by alatime desc limit "+size+ " allow filtering";
return template.select(cql, AlarmLog.class);
}
public List<AlarmLog> getAlarmLogList(String ip,int size,String flag) {
String cql="select * from ctid_prof.alarm_log where ip like '%"+ip+"%' and state = '" +Constant.ALARM_RECOVERY+"' and serialnum<'"+flag+"' order by alatime desc limit "+size+ " allow filtering";
return template.select(cql, AlarmLog.class);
}
public List<AlarmLog> getAlarmLogList(String ip, String start, String end,int size,String flag) {
String cql="select * from ctid_prof.alarm_log where ip like '%"+ip+"%' and alatime>'"+start+"' and alatime <'"+end+"' and state = '" +Constant.ALARM_RECOVERY+"' and serialnum<'"+flag+"' limit "+size+ " allow filtering";
return template.select(cql, AlarmLog.class);
}
public Map getAlarmLogSize(String start, String end) {
String cql="select count(*) from ctid_prof.alarm_log where alatime > '"+start+"' and alatime < '"+end+"' and state < '" +Constant.ALARM_ING+"' allow filtering";
return template.selectOne(cql,Map.class);
}
public Map getAlarmLogSize(String ip) {
String cql="select count(*) from ctid_prof.alarm_log where ip like '%"+ip+"%' and state < '" +Constant.ALARM_ING+"' allow filtering";
return template.selectOne(cql,Map.class);
}
public Map getAlarmLogSize(String ip, String start, String end) {
String cql="select count(*) from ctid_prof.alarm_log where ip like '%"+ip+"%' and alatime > '"+start+"' and alatime <'"+end+"' and state < '" +Constant.ALARM_ING+"' allow filtering";
return template.selectOne(cql,Map.class);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment