Commit 346b0d98 authored by gaozhentao's avatar gaozhentao

1.新增告警和告警记录查询

parent bcb0424f
...@@ -133,5 +133,10 @@ ...@@ -133,5 +133,10 @@
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.5" level="project" /> <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.5" level="project" />
<orderEntry type="library" name="Maven: redis.clients:jedis:2.9.0" level="project" /> <orderEntry type="library" name="Maven: redis.clients:jedis:2.9.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-pool2:2.5.0" level="project" /> <orderEntry type="library" name="Maven: org.apache.commons:commons-pool2:2.5.0" level="project" />
<orderEntry type="library" name="Maven: net.sf.json-lib:json-lib:jdk15:2.4" level="project" />
<orderEntry type="library" name="Maven: commons-beanutils:commons-beanutils:1.8.0" level="project" />
<orderEntry type="library" name="Maven: commons-collections:commons-collections:3.2.1" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.1" level="project" />
<orderEntry type="library" name="Maven: net.sf.ezmorph:ezmorph:1.0.6" level="project" />
</component> </component>
</module> </module>
\ No newline at end of file
This diff is collapsed.
...@@ -97,12 +97,6 @@ ...@@ -97,12 +97,6 @@
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!-- 分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
<dependency> <dependency>
<groupId>net.sf.json-lib</groupId> <groupId>net.sf.json-lib</groupId>
......
...@@ -66,17 +66,4 @@ public class ReliabilityApi { ...@@ -66,17 +66,4 @@ public class ReliabilityApi {
} }
@GET
@Path("aaa11")
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> aaa11(@RequestBody String json) {
return manager.aaa11(json);
}
@GET
@Path("aaa22")
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> aaa22(@RequestBody String json) {
return manager.aaa22(json);
}
} }
...@@ -7,10 +7,10 @@ package com.yingxin.prms.config; ...@@ -7,10 +7,10 @@ package com.yingxin.prms.config;
*/ */
public interface Constant { public interface Constant {
/* 告警恢复 */
String ALARM_RECOVERY = "1";
/* 已告警 */ /* 已告警 */
String ALARM_ING = "0"; String ALARM_ING = "1";
/* 告警恢复 */
String ALARM_RECOVERY = "0";
/* 告警发生变化 */ /* 告警发生变化 */
String ALARM_CHANGE = "-1"; String ALARM_CHANGE = "-1";
/* 告警级别 不稳定 */ /* 告警级别 不稳定 */
......
...@@ -25,7 +25,7 @@ public class AlarmLog { ...@@ -25,7 +25,7 @@ public class AlarmLog {
private String alatime; private String alatime;
/* 恢复时间 */ /* 恢复时间 */
private String rectime; private String rectime;
/* 1 告警恢复 0告警 -1 告警发生变化*/ /* 1 告警 0 告警恢复 -1 告警发生变化*/
private String state; private String state;
......
...@@ -3,28 +3,21 @@ package com.yingxin.prms.service.asyncTask; ...@@ -3,28 +3,21 @@ package com.yingxin.prms.service.asyncTask;
import com.yingxin.prms.config.Constant; import com.yingxin.prms.config.Constant;
import com.yingxin.prms.domain.AlarmLog; import com.yingxin.prms.domain.AlarmLog;
import com.yingxin.prms.domain.Server_Host_Info; import com.yingxin.prms.domain.Server_Host_Info;
import com.yingxin.prms.dto.AlarmMessage;
import com.yingxin.prms.dto.SingleHostStatus;
import com.yingxin.prms.service.dao.CassandraDaoImpl; import com.yingxin.prms.service.dao.CassandraDaoImpl;
import com.yingxin.prms.utils.TimeUtil; import com.yingxin.prms.utils.TimeUtil;
import org.apache.tomcat.util.bcel.Const;
import org.kairosdb.client.HttpClient; import org.kairosdb.client.HttpClient;
import org.kairosdb.client.builder.*; import org.kairosdb.client.builder.*;
import org.kairosdb.client.response.QueryResponse; import org.kairosdb.client.response.QueryResponse;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.*; import java.io.IOException;
import java.net.Socket; import java.util.List;
import java.nio.charset.StandardCharsets; import java.util.TimeZone;
import java.time.LocalDateTime; import java.util.UUID;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@Component @Component
public class AlarmTask { public class AlarmTask {
...@@ -33,99 +26,26 @@ public class AlarmTask { ...@@ -33,99 +26,26 @@ public class AlarmTask {
private final HttpClient client; private final HttpClient client;
private final CassandraDaoImpl cassandraDao;
@Value("${alarm.ip}") @Autowired
private String ALARM_IP; private CassandraDaoImpl cassandraDao;
@Value("${alarm.port}")
private int ALARM_PORT;
@Value("${alarm.systemType}")
private String ALARM_SYSTEMTYPE;
@Value("${sssa}")
private String KAIROSDB_URL;
@Autowired @Autowired
public AlarmTask(HttpClient client,CassandraDaoImpl cassandraDao) { public AlarmTask(HttpClient client) {
this.client = client; this.client = client;
this.cassandraDao = cassandraDao;
} }
/* @Async("asyncTaskPool")
public void doTask(Server_Host_Info hostInfo, ConcurrentHashMap<String, String> alarmHost) {
System.out.println("判断告警");
QueryBuilder builderHeartbeat = QueryBuilder.getInstance();
builderHeartbeat.setStart(2, TimeUnit.MINUTES).setTimeZone(TimeZone.getDefault())
.addMetric("heartbeat")
.addTag("host", hostInfo.getHost_ip() + ":" + hostInfo.getHost_port())
.addAggregator(AggregatorFactory.createSumAggregator(1, TimeUnit.MINUTES));
try {
QueryResponse responseHeartbeat = client.query(builderHeartbeat);
List<DataPoint> dataPoints = responseHeartbeat.getQueries().get(0).getResults().get(0).getDataPoints();
long count = 0;
if (dataPoints.size() == 1) {
count = dataPoints.get(0).longValue();
}
if (dataPoints.size() > 1) {
count = dataPoints.get(dataPoints.size() - 1).longValue() + dataPoints.get(dataPoints.size() - 2).longValue();
}
int countPerMin = 60 / 10 - 1;
boolean status = (count != 0) && count >= countPerMin;
String uuid;
logger.info("count:{}|countPerMin:{}", count, countPerMin);
logger.info("alarm info:{}|{}|{}", hostInfo.getHost_ip(), hostInfo.getHost_port(), status);
*//*排除网证开通接口*//*
if (status && !hostInfo.getCluster_name().equals("网证开通接口")) {
System.out.println("===============开始告警=======================");
AlarmMessage alarmMessage = new AlarmMessage();
alarmMessage.setDeviceIp(hostInfo.getHost_ip());
alarmMessage.setSystemType(ALARM_SYSTEMTYPE);
uuid = alarmHost.get(hostInfo.getService_path());
if (uuid != null) {
return;
}
uuid = UUID.randomUUID().toString();
alarmHost.put(hostInfo.getService_path(), uuid);
alarmMessage.setAlarmSeq(uuid);
alarmMessage.setAlarmTitle(hostInfo.getHost_ip() + " 系统宕机");
alarmMessage.setAlarmDesc(hostInfo.getCluster_name() + " " + hostInfo.getHost_name()
+ " " + hostInfo.getHost_ip() + " 连续1分钟服务调用失败");
alarmMessage.setAlarmLevel("3");
alarmMessage.setAlarmType("13");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
String time = formatter.format(LocalDateTime.now());
alarmMessage.setAlarmTime(time);
alarmMessage.setAlarmStatus("0");
alarmMessage.setBreakMakeType("03");
System.out.println("发送告警");
doAlarm(ALARM_IP, ALARM_PORT, alarmMessage);
System.out.println("========================================================");
}
if (!status) {
alarmHost.remove(hostInfo.getService_path());
}
} catch (IOException | DataFormatException e) {
e.printStackTrace();
}
}*/
@Async("asyncTaskPool") @Async("asyncTaskPool")
public void doTask(Server_Host_Info hostInfo) { public void doTask(Server_Host_Info hostInfo) {
HttpClient httpClient;
String tag = hostInfo.getHost_ip() + ":" + hostInfo.getHost_port(); String tag = hostInfo.getHost_ip() + ":" + hostInfo.getHost_port();
Aggregator aggregator5Min = AggregatorFactory.createSumAggregator(5, TimeUnit.MINUTES); Aggregator aggregator5Min = AggregatorFactory.createSumAggregator(5, TimeUnit.MINUTES);
Aggregator aggregator1Min = AggregatorFactory.createSumAggregator(1, TimeUnit.MINUTES); Aggregator aggregator1Min = AggregatorFactory.createSumAggregator(1, TimeUnit.MINUTES);
QueryBuilder query5Min = createQuery(5, TimeUnit.MINUTES, "heartbeat", tag, aggregator5Min); QueryBuilder query5Min = createQuery(5, TimeUnit.MINUTES, "heartbeat", tag, aggregator5Min);
QueryBuilder query1Min = createQuery(1, TimeUnit.MINUTES, "heartbeat", tag, aggregator1Min); QueryBuilder query1Min = createQuery(1, TimeUnit.MINUTES, "heartbeat", tag, aggregator1Min);
try { try {
httpClient = new HttpClient(KAIROSDB_URL); QueryResponse heartbeat5Min = client.query(query5Min);
QueryResponse heartbeat5Min = httpClient.query(query5Min); QueryResponse heartbeat1Min = client.query(query1Min);
QueryResponse heartbeat1Min = httpClient.query(query1Min);
List<DataPoint> dataPoints5Min = heartbeat5Min.getQueries().get(0).getResults().get(0).getDataPoints(); List<DataPoint> dataPoints5Min = heartbeat5Min.getQueries().get(0).getResults().get(0).getDataPoints();
List<DataPoint> dataPoints1Min = heartbeat1Min.getQueries().get(0).getResults().get(0).getDataPoints(); List<DataPoint> dataPoints1Min = heartbeat1Min.getQueries().get(0).getResults().get(0).getDataPoints();
...@@ -144,7 +64,7 @@ public class AlarmTask { ...@@ -144,7 +64,7 @@ public class AlarmTask {
int correctCount1Min = 60 / 10 - 1; int correctCount1Min = 60 / 10 - 1;
String uuid = UUID.randomUUID().toString(); String uuid = UUID.randomUUID().toString();
AlarmLog alarmLog = new AlarmLog(); AlarmLog alarmLog = new AlarmLog();
alarmLog.setSerialnum(uuid); alarmLog.setSerialnum(uuid);
alarmLog.setIp(hostInfo.getHost_ip()); alarmLog.setIp(hostInfo.getHost_ip());
alarmLog.setPort(String.valueOf(hostInfo.getHost_port())); alarmLog.setPort(String.valueOf(hostInfo.getHost_port()));
...@@ -191,30 +111,4 @@ public class AlarmTask { ...@@ -191,30 +111,4 @@ public class AlarmTask {
.addAggregator(aggregator); .addAggregator(aggregator);
return queryBuilder; return queryBuilder;
} }
public void doAlarm(String ip, int port, AlarmMessage alarmMessage) {
System.out.println("告警信息:"+alarmMessage);
logger.info("开始告警.{}|{}|{}", ip, port, alarmMessage.toString());
try(Socket socket = new Socket(ip, port);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8);
PrintWriter printWriter = new PrintWriter(outputStreamWriter);
InputStreamReader streamReader = new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(streamReader)) {
socket.setSoTimeout(300);
printWriter.write(alarmMessage.toString());
printWriter.flush();
String returnStr = bufferedReader.readLine();
logger.info("returnStr:{}", returnStr);
if (returnStr.equals("0")) {
logger.info("告警信息发送成功");
//todo 写入告警记录表
// String alarmID = alarmMessage.getAlarmSeq();
}
if (!returnStr.equals("0")) logger.info("告警异常 返回值:{}", returnStr);
} catch (IOException e) {
logger.info("I/O错误,告警信息发送不正常:{}",e.getMessage());
}
}
} }
package com.yingxin.prms.service.business; package com.yingxin.prms.service.business;
import com.fasterxml.jackson.databind.util.JSONPObject; import com.fasterxml.jackson.databind.util.JSONPObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yingxin.prms.domain.*; import com.yingxin.prms.domain.*;
import com.yingxin.prms.dto.*; import com.yingxin.prms.dto.*;
import com.yingxin.prms.service.asyncTask.QueryHostStatusTask; import com.yingxin.prms.service.asyncTask.QueryHostStatusTask;
...@@ -21,6 +19,8 @@ import java.util.Map; ...@@ -21,6 +19,8 @@ import java.util.Map;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static sun.misc.MessageUtils.where;
@Service @Service
public class Manager { public class Manager {
...@@ -35,8 +35,7 @@ public class Manager { ...@@ -35,8 +35,7 @@ public class Manager {
private final HttpClient httpClient; private final HttpClient httpClient;
@Value("${dailyAuthCount.url}")
private String dailyAuthCountUrl;
@Autowired @Autowired
public Manager(AuthModeAndHostList authModeAndHostList, CassandraDaoImpl cassandraDao, HttpClient httpClient) { public Manager(AuthModeAndHostList authModeAndHostList, CassandraDaoImpl cassandraDao, HttpClient httpClient) {
...@@ -115,38 +114,35 @@ public class Manager { ...@@ -115,38 +114,35 @@ public class Manager {
public Map<String, Object> getAlarmLog(String json) { public Map<String, Object> getAlarmLog(String json) {
Map<String,Object> map = new HashMap(); Map<String,Object> map = new HashMap();
List<AlarmLog> alarmList = cassandraDao.getAlarmList(); List<AlarmLog> alarmList = cassandraDao.getAlarmList();
System.out.println(alarmList.size());
map.put("alarm",alarmList); map.put("alarm",alarmList);
JSONObject jsonObject = JSONObject.fromObject(json); JSONObject jsonObject = JSONObject.fromObject(json);
PageHelper.startPage( jsonObject.getInt("pageNum"),jsonObject.getInt("pageSize")); int pageNum = jsonObject.getInt("pageNum");
List<AlarmLog> logList = cassandraDao.getAlarmLog(); int pageSize = jsonObject.getInt("pageSize");
PageInfo pageInfo = new PageInfo(logList);
map.put("log",pageInfo.getList()); System.out.println("111"+pageNum+":::"+pageSize);
map.put("logSize",pageInfo.getTotal()); int i = 0;
return map; String flag = "";
Map total = cassandraDao.getAlarmLogSize();
} List<AlarmLog> logList = null;
do{
logList = cassandraDao.getAlarmLog(pageSize,flag);
System.out.println("aa:"+logList.size());
if (logList.size()==0){
break;
}
AlarmLog alarmLog = logList.get(logList.size()-1);
flag = alarmLog.getSerialnum();
i++;
}while(i<pageNum);
public Map<String, Object> aaa22(String json) { map.put("log",logList);
JSONObject jsonObject = JSONObject.fromObject(json); map.put("logSize",total.get("count"));
Map<String, Object> map = new HashMap<>();
map.put("aaa22","aaa22");
boolean alarmLog = cassandraDao.updateAlarm(jsonObject.getString("number"));
System.out.println("====================aaa22=============================");
System.out.println(alarmLog);
System.out.println("=========================================================");
return map; return map;
} }
public Map<String, Object> aaa11(String json) {
JSONObject jsonObject = JSONObject.fromObject(json);
Map<String, Object> map = new HashMap<>();
map.put("aaa11","aaa11");
AlarmLog alarmLog = cassandraDao.checkAlarm(jsonObject.getString("ip"), jsonObject.getString("port"));
System.out.println("====================aaa11=============================");
System.out.println(alarmLog);
System.out.println("=========================================================");
return map;
}
// /** // /**
// * 获取一包基准值 // * 获取一包基准值
......
...@@ -9,6 +9,7 @@ import org.springframework.data.cassandra.core.CassandraTemplate; ...@@ -9,6 +9,7 @@ import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List; import java.util.List;
import java.util.Map;
import static org.springframework.data.cassandra.core.query.Criteria.where; import static org.springframework.data.cassandra.core.query.Criteria.where;
import static org.springframework.data.cassandra.core.query.Query.query; import static org.springframework.data.cassandra.core.query.Query.query;
...@@ -56,23 +57,29 @@ public class CassandraDaoImpl { ...@@ -56,23 +57,29 @@ public class CassandraDaoImpl {
// return template.select("select * from ctid_prof.ctid_process_perf_base_line_value;", CTID_Process_Perf_BaseLineValue.class); // return template.select("select * from ctid_prof.ctid_process_perf_base_line_value;", CTID_Process_Perf_BaseLineValue.class);
// } // }
public List<AlarmLog> getAlarmLog() { public List<AlarmLog> getAlarmLog(int size,String flag) {
String cql="select * from ctid_prof.alarm_log where state in ("+ Constant.ALARM_CHANGE+","+Constant.ALARM_RECOVERY +")"; String cql="select * from ctid_prof.alarm_log where token(serialnum) > token( '"+flag+"' ) and state < '"+Constant.ALARM_ING+"' limit "+size+ " allow filtering;";
System.out.println(cql);
return template.select(cql, AlarmLog.class); return template.select(cql, AlarmLog.class);
} }
public List<AlarmLog> getAlarmList() { public List<AlarmLog> getAlarmList() {
String cql="select * from ctid_prof.alarm_log where state = " +Constant.ALARM_ING; String cql="select * from ctid_prof.alarm_log where state = '" +Constant.ALARM_ING+"' allow filtering";
return template.select(cql, AlarmLog.class); return template.select(cql, AlarmLog.class);
} }
public AlarmLog checkAlarm(String ip,String port) { public AlarmLog checkAlarm(String ip,String port) {
String cql="select * from ctid_prof.alarm_log where ip = "+ip+" and port ="+port+" and state = " +Constant.ALARM_ING; String cql="select * from ctid_prof.alarm_log where ip = "+ip+" and port = "+port+" and state = '" +Constant.ALARM_ING+"' allow filtering";
return template.selectOne(cql, AlarmLog.class); return template.selectOne(cql, AlarmLog.class);
} }
public boolean updateAlarm(String serialnum) { public boolean updateAlarm(String serialnum) {
String cql="update from ctid_prof.alarm_log set state = " +Constant.ALARM_RECOVERY +"and alarm_level = "+Constant.ALARM_OK+"where serialnum = "+serialnum; String cql="update from ctid_prof.alarm_log set state = '" +Constant.ALARM_RECOVERY +"' and alarm_level = "+Constant.ALARM_OK+" where serialnum = "+serialnum ;
return template.getCqlOperations().execute(cql); return template.getCqlOperations().execute(cql);
} }
public Map getAlarmLogSize() {
String cql="select count(*) from ctid_prof.alarm_log where state < '" +Constant.ALARM_ING+"' allow filtering";
return template.selectOne(cql,Map.class);
}
} }
server:
port: 8090
spring: spring:
data: data:
...@@ -26,7 +29,6 @@ jwt: ...@@ -26,7 +29,6 @@ jwt:
tokenHead: "Bearer " tokenHead: "Bearer "
sssa: "http://192.168.189.130:8080"
demo: demo:
kairosdb: kairosdb:
# url: "http://192.168.209.128:8080" # url: "http://192.168.209.128:8080"
...@@ -34,6 +36,7 @@ demo: ...@@ -34,6 +36,7 @@ demo:
url: "http://192.168.189.130:8080" url: "http://192.168.189.130:8080"
maxConnTotal: 50 maxConnTotal: 50
maxConnPerRoute: 30 maxConnPerRoute: 30
dailyAuthCount: dailyAuthCount:
url: "http://15.1.3.8:9000/acl/v1/dailyCount" url: "http://15.1.3.8:9000/acl/v1/dailyCount"
ascynTaskThreadPool: ascynTaskThreadPool:
...@@ -43,4 +46,4 @@ ascynTaskThreadPool: ...@@ -43,4 +46,4 @@ ascynTaskThreadPool:
alarm: alarm:
ip: "15.2.249.205" ip: "15.2.249.205"
port: 8899 port: 8899
systemType: 101 systemType: 101
\ No newline at end of file
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