Commit bcb0424f authored by gaozhentao's avatar gaozhentao

1

parent f4a511d3
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -97,5 +97,18 @@
<scope>compile</scope>
</dependency>
<!-- 分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
......@@ -56,4 +57,26 @@ public class ReliabilityApi {
map.put("poolSize", poolSize);
return map;
}
@GET
@Path("getAlarmLog")
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> getAlarmLog(@RequestBody String json) {
return manager.getAlarmLog(json);
}
@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);
}
}
package com.yingxin.prms.config;
/**
* Description: 常量
* Datetime: 2020/10/21 13:53
* Author: gaozhentao
*/
public interface Constant {
/* 告警恢复 */
String ALARM_RECOVERY = "1";
/* 已告警 */
String ALARM_ING = "0";
/* 告警发生变化 */
String ALARM_CHANGE = "-1";
/* 告警级别 不稳定 */
String ALARM_UNSTABLE = "unstable";
/* 告警级别 宕机 */
String ALARM_DOWN = "down";
/* 告警级别 健康 */
String ALARM_OK = "ok";
}
package com.yingxin.prms.domain;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;
/**
* Description: 告警记录
* Datetime: 2020/10/21 13:16
* Author: gaozhentao
*/
@Table(value = "alarm_log")
public class AlarmLog {
@PrimaryKey
/* 告警序列号 */
private String serialnum;
private String ip;
private String port;
/* ok 正常 unstable 不稳定 down 宕机 */
private String alarmLevel;
/* */
private String alarmMessage;
/* 告警时间 */
private String alatime;
/* 恢复时间 */
private String rectime;
/* 1 告警恢复 0告警 -1 告警发生变化*/
private String state;
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 getRectime() {
return rectime;
}
public void setRectime(String rectime) {
this.rectime = rectime;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
package com.yingxin.prms.service.asyncTask;
import com.yingxin.prms.config.Constant;
import com.yingxin.prms.domain.AlarmLog;
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.utils.TimeUtil;
import org.apache.tomcat.util.bcel.Const;
import org.kairosdb.client.HttpClient;
import org.kairosdb.client.builder.*;
import org.kairosdb.client.response.QueryResponse;
......@@ -17,9 +23,7 @@ import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.TimeZone;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@Component
......@@ -29,6 +33,8 @@ public class AlarmTask {
private final HttpClient client;
private final CassandraDaoImpl cassandraDao;
@Value("${alarm.ip}")
private String ALARM_IP;
......@@ -38,12 +44,18 @@ public class AlarmTask {
@Value("${alarm.systemType}")
private String ALARM_SYSTEMTYPE;
@Value("${sssa}")
private String KAIROSDB_URL;
@Autowired
public AlarmTask(HttpClient client) {
public AlarmTask(HttpClient client,CassandraDaoImpl cassandraDao) {
this.client = client;
this.cassandraDao = cassandraDao;
}
@Async("asyncTaskPool")
/* @Async("asyncTaskPool")
public void doTask(Server_Host_Info hostInfo, ConcurrentHashMap<String, String> alarmHost) {
System.out.println("判断告警");
QueryBuilder builderHeartbeat = QueryBuilder.getInstance();
......@@ -66,7 +78,7 @@ public class AlarmTask {
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();
......@@ -99,7 +111,88 @@ public class AlarmTask {
} catch (IOException | DataFormatException e) {
e.printStackTrace();
}
}*/
@Async("asyncTaskPool")
public void doTask(Server_Host_Info hostInfo) {
HttpClient httpClient;
String tag = hostInfo.getHost_ip() + ":" + hostInfo.getHost_port();
Aggregator aggregator5Min = AggregatorFactory.createSumAggregator(5, TimeUnit.MINUTES);
Aggregator aggregator1Min = AggregatorFactory.createSumAggregator(1, TimeUnit.MINUTES);
QueryBuilder query5Min = createQuery(5, TimeUnit.MINUTES, "heartbeat", tag, aggregator5Min);
QueryBuilder query1Min = createQuery(1, TimeUnit.MINUTES, "heartbeat", tag, aggregator1Min);
try {
httpClient = new HttpClient(KAIROSDB_URL);
QueryResponse heartbeat5Min = httpClient.query(query5Min);
QueryResponse heartbeat1Min = httpClient.query(query1Min);
List<DataPoint> dataPoints5Min = heartbeat5Min.getQueries().get(0).getResults().get(0).getDataPoints();
List<DataPoint> dataPoints1Min = heartbeat1Min.getQueries().get(0).getResults().get(0).getDataPoints();
long faultCount5Min = dataPoints5Min.get(0).longValue();
long faultCount1Min = 0;
if (dataPoints1Min.size() != 0) {
faultCount1Min = dataPoints1Min.get(0).longValue();
}
if (dataPoints5Min.size() > 1) {
faultCount5Min = dataPoints5Min.get(dataPoints5Min.size() - 1).longValue();
}
if (dataPoints1Min.size() > 1) {
faultCount1Min = dataPoints1Min.get(dataPoints1Min.size() - 1).longValue() + dataPoints1Min.get(dataPoints1Min.size() - 2).longValue();
}
int correctCount1Min = 60 / 10 - 1;
String uuid = UUID.randomUUID().toString();
AlarmLog alarmLog = new AlarmLog();
alarmLog.setSerialnum(uuid);
alarmLog.setIp(hostInfo.getHost_ip());
alarmLog.setPort(String.valueOf(hostInfo.getHost_port()));
if (faultCount5Min != 0) {
/* 不稳定告警 */
alarmLog.setAlatime(TimeUtil.getDate());
alarmLog.setState(Constant.ALARM_ING);
alarmLog.setAlarmLevel(Constant.ALARM_UNSTABLE);
String msg ="系统不稳当"+" " + hostInfo.getCluster_name() + " " + hostInfo.getHost_name()
+ " " + hostInfo.getHost_ip() + " 5分钟内服务存在调用失败";
alarmLog.setAlarmMessage(msg);
}else if(faultCount1Min >= correctCount1Min) {
/* 宕机告警 */
alarmLog.setAlatime(TimeUtil.getDate());
alarmLog.setState(Constant.ALARM_ING);
alarmLog.setAlarmLevel(Constant.ALARM_DOWN);
String msg ="系统宕机"+ " " + hostInfo.getCluster_name() + " " + hostInfo.getHost_name()
+ " " + hostInfo.getHost_ip() + " 连续1分钟服务调用失败";
alarmLog.setAlarmMessage(msg);
}else {
/* 校验是否存在告警 */
AlarmLog alarmCheck = cassandraDao.checkAlarm(hostInfo.getHost_ip(), String.valueOf(hostInfo.getHost_port()));
if(alarmCheck != null){
/* 如果存在告警 则恢复 */
cassandraDao.updateAlarm(alarmCheck.getSerialnum());
}
}
/* 宕机告警 */
} catch (IOException | DataFormatException e) {
logger.error("告警校验发生错误。inMonitoring返回false");
logger.error(e.getMessage());
}
}
private QueryBuilder createQuery(int start, TimeUnit timeUnit, String metric, String tag, Aggregator aggregator) {
QueryBuilder queryBuilder = QueryBuilder.getInstance();
queryBuilder.setStart(start, timeUnit).setTimeZone(TimeZone.getDefault())
.addMetric(metric)
.addTag("host", tag)
.addAggregator(aggregator);
return queryBuilder;
}
public void doAlarm(String ip, int port, AlarmMessage alarmMessage) {
System.out.println("告警信息:"+alarmMessage);
......
package com.yingxin.prms.service.business;
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.dto.*;
import com.yingxin.prms.service.asyncTask.QueryHostStatusTask;
import com.yingxin.prms.service.dao.CassandraDaoImpl;
import net.sf.json.JSONObject;
import org.kairosdb.client.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -108,6 +112,42 @@ 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);
PageHelper.startPage( jsonObject.getInt("pageNum"),jsonObject.getInt("pageSize"));
List<AlarmLog> logList = cassandraDao.getAlarmLog();
PageInfo pageInfo = new PageInfo(logList);
map.put("log",pageInfo.getList());
map.put("logSize",pageInfo.getTotal());
return map;
}
public Map<String, Object> aaa22(String json) {
JSONObject jsonObject = JSONObject.fromObject(json);
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;
}
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;
}
// /**
// * 获取一包基准值
// * @return 基准值实体类
......
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.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.stereotype.Repository;
......@@ -53,4 +55,24 @@ public class CassandraDaoImpl {
// public List<CTID_Process_Perf_BaseLineValue> getProBLV() {
// return template.select("select * from ctid_prof.ctid_process_perf_base_line_value;", CTID_Process_Perf_BaseLineValue.class);
// }
public List<AlarmLog> getAlarmLog() {
String cql="select * from ctid_prof.alarm_log where state in ("+ Constant.ALARM_CHANGE+","+Constant.ALARM_RECOVERY +")";
return template.select(cql, AlarmLog.class);
}
public List<AlarmLog> getAlarmList() {
String cql="select * from ctid_prof.alarm_log where state = " +Constant.ALARM_ING;
return template.select(cql, AlarmLog.class);
}
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;
return template.selectOne(cql, AlarmLog.class);
}
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;
return template.getCqlOperations().execute(cql);
}
}
......@@ -56,7 +56,7 @@ public class Schedule {
List<Server_Host_Info> server_host_infos = list.getClusterBeMonitoredList();
ConcurrentHashMap<String, String> alarmHost = list.getAlarmHost();
for (Server_Host_Info hostInfo : server_host_infos) {
alarmTask.doTask(hostInfo, alarmHost);
alarmTask.doTask(hostInfo);
}
}
}
package com.yingxin.prms.utils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.util.Date;
import java.util.TimeZone;
import org.kairosdb.client.HttpClient;
import org.kairosdb.client.builder.*;
import org.kairosdb.client.response.GetResponse;
import org.kairosdb.client.response.QueryResponse;
import org.kairosdb.client.response.Response;
public class Demo01 {
static String url = "http://192.168.189.130:8080";
// 获取metric的name集合
public static void getMetricNames() {
HttpClient client;
GetResponse response;
try {
client = new HttpClient(url);
response = client.getMetricNames();
System.out.println("*********************MetricNames**********************");
System.out.println("Response Code =" + response.getStatusCode());
for (String name : response.getResults()) {
System.out.println(name);
}
client.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}
// 获取Tag的name集合
public static void getTagNames() {
HttpClient client;
GetResponse response;
try {
client = new HttpClient(url);
response = client.getTagNames();
System.out.println("*********************TagNames**********************");
System.out.println("Response Code =" + response.getStatusCode());
for (String name : response.getResults()) {
System.out.println(name);
}
client.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}
// 获取Tag的值集合
public static void getTagValues() {
HttpClient client;
GetResponse response;
try {
client = new HttpClient(url);
response = client.getTagValues();
System.out.println("*********************TagValues**********************");
System.out.println("Response Code =" + response.getStatusCode());
for (String name : response.getResults()) {
System.out.println(name);
}
client.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}
// pushMetric
public static void pushMetric() throws URISyntaxException, IOException {
MetricBuilder builder = MetricBuilder.getInstance();
builder.addMetric("proc.loadavg.1m").addTag("host", "kairosdb")
.addDataPoint(System.currentTimeMillis(), 10)
.addDataPoint(System.currentTimeMillis(), 30L);
HttpClient client = new HttpClient(url);
Response response = client.pushMetrics(builder);
System.out.println("*********************Push Metric**********************");
System.out.println("Response Code =" + response.getStatusCode());
client.shutdown();
}
// 查询数据节点
public static void queryDataPoint() throws IOException, URISyntaxException {
QueryBuilder builder = QueryBuilder.getInstance();
builder.setStart(1000, TimeUnit.MINUTES).setEnd(1, TimeUnit.MINUTES).addMetric("heartbeat")
.addAggregator(AggregatorFactory.createSumAggregator(1, TimeUnit.MINUTES));
HttpClient client = new HttpClient(url);
QueryResponse response = client.query(builder);
String body = response.getBody();
System.out.println("*********************Query DataPoint**********************");
System.out.println("Response Code =" + response.getStatusCode());
System.out.println(body);
client.shutdown();
}
// 删除Metric中的数据点
public static void deleteMetric() throws IOException {
// MetricBuilder builder = MetricBuilder.getInstance();
HttpClient client = new HttpClient(url);
Response response = client.deleteMetric("proc.loadavg.1m");
System.out.println("*********************Delete Metric**********************");
System.out.println("Response Code =" + response.getStatusCode());
client.shutdown();
}
// 删除Metric中的数据点
public static void demo01() throws IOException {
MetricBuilder builderHeartbeat = MetricBuilder.getInstance();
// builderHeartbeat.addMetric("heartbeat")
// .addTag("host", 1111 + ":" + 2222)
// .addDataPoint(System.currentTimeMillis(), 1).addTtl(172800); //172800=48小时
builderHeartbeat.addMetric("respdur")
.addTag("host", "host11")
.addTag("customer", "customer222")
.addDataPoint(System.currentTimeMillis(), 10)
.addDataPoint(System.currentTimeMillis(), 30L);
HttpClient client = new HttpClient(url);
Response response = client.pushMetrics(builderHeartbeat);
System.out.println("*********************Push Metric**********************");
System.out.println("Response Code =" + response.getStatusCode());
}
private static QueryBuilder createQuery(int start, TimeUnit timeUnit, String metric, String tag, Aggregator aggregator) {
QueryBuilder queryBuilder = QueryBuilder.getInstance();
queryBuilder.setStart(start, timeUnit).setTimeZone(TimeZone.getDefault())
.addMetric(metric)
.addTag("host", tag)
.addAggregator(aggregator);
return queryBuilder;
}
// 查询数据节点
public static void queryDemo() throws IOException, URISyntaxException {
QueryBuilder builder = QueryBuilder.getInstance();
// builder.setStart(1000, TimeUnit.MINUTES).setEnd(1, TimeUnit.MINUTES).addMetric("heartbeat")
// .addAggregator(AggregatorFactory.createSumAggregator(1, TimeUnit.MINUTES));
HttpClient httpClient = new HttpClient(url);
// QueryResponse response = client.query(builder);
String tag = "192.168.65.11:8888";
Aggregator aggregatorDay = AggregatorFactory.createSumAggregator(1, TimeUnit.DAYS).withStartTimeAlignment(0);
QueryBuilder queryDay = createQuery(1, TimeUnit.DAYS, "heartbeat", tag, aggregatorDay);
QueryResponse heartbeatDay = httpClient.query(queryDay);
String body = heartbeatDay.getBody();
System.out.println("*********************Query DataPoint**********************");
System.out.println("Response Code =" + heartbeatDay.getStatusCode());
System.out.println(body);
httpClient.shutdown();
}
public static void toDate(long time) {
Date date = new Date(time);
DateFormat dateFormat = DateFormat.getDateInstance();
System.out.println( dateFormat.format(date));
}
/**
* 类描述:
*
* @author: blank
* @date: 日期:2016-5-9 时间:上午10:30:14
* @param args
* @version 1.0
* @throws IOException
* @throws URISyntaxException
*/
public static void main(String[] args) throws URISyntaxException, IOException {
// getMetricNames();
// getTagNames();
// getTagValues();
// pushMetric();
// deleteMetric();
// queryDataPoint();
queryDemo();
// demo01();
}
}
package com.yingxin.prms.utils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
......@@ -30,4 +31,10 @@ public class TimeUtil {
formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(newDate);
}
public static String getDate(){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
}
......@@ -24,6 +24,9 @@ jwt:
secret: "spring-security-@Jwt!&Secret^#"
expiration: 1800000
tokenHead: "Bearer "
sssa: "http://192.168.189.130:8080"
demo:
kairosdb:
# url: "http://192.168.209.128:8080"
......
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