Commit 93c86fed authored by chenjian's avatar chenjian

重写监控模块

parent 6f611aba
package com.ebo.workOrder.BtnEnhance;
package com.ebo.workOrder.serviceBtnEnhance;
import cn.hutool.core.date.DateTime;
import com.ebo.entity.WorkOrderEntity;
......
package com.ebo.workOrder.BtnEnhance;
package com.ebo.workOrder.serviceBtnEnhance;
import com.alibaba.fastjson.JSON;
import com.ebo.entity.WorkOrderEntity;
......
package com.ebo.workOrder.BtnEnhance;
package com.ebo.workOrder.serviceBtnEnhance;
import com.ebo.entity.WorkOrderEntity;
import com.ebo.framework.common.exception.ServerException;
......
package com.ebo.workOrder.BtnEnhance;
package com.ebo.workOrder.serviceBtnEnhance;
import com.alibaba.fastjson.JSON;
import com.ebo.entity.WorkOrderEntity;
......
package com.ebo.workOrder.BtnEnhance;
package com.ebo.workOrder.serviceBtnEnhance;
import com.alibaba.fastjson.JSON;
import com.ebo.framework.common.exception.ServerException;
import com.ebo.module.fast.common.BtnServerEnhanceJavaInter;
import com.ebo.entity.WorkOrderEntity;
......@@ -10,8 +9,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Slf4j
......
package com.ebo.workOrder.BtnEnhance;
package com.ebo.workOrder.serviceBtnEnhance;
import cn.hutool.core.date.DateTime;
import com.ebo.entity.WorkOrderEntity;
......
package com.ebo.workOrder.BtnEnhance;
package com.ebo.workOrder.serviceBtnEnhance;
import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.ebo.entity.WorkOrderEntity;
import com.ebo.framework.common.exception.ServerException;
import com.ebo.mapper.WorkOrderMapper;
......
......@@ -41,6 +41,12 @@
<artifactId>xxl-job-core</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.ebo</groupId>
<artifactId>oms-module-monitor</artifactId>
<version>3.5.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
......
package com.ebo.module.task;
import com.ebo.module.utils.GetAccessToken;
import com.ebo.module.utils.SendMessageEmail;
import com.google.gson.Gson;
import com.ebo.monitor.model.Cpu;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Map;
@Component
@Slf4j
public class CpuMonitorHandler extends IJobHandler {
@Autowired
private SendMessageEmail sendMessageEmail;
private final static String apiUrl = "http://localhost:8659/monitor/server/cpu";
@XxlJob("cpuMonitorJob")
@Override
public void execute() throws Exception {
log.info("Cpu Monitor Job Start");
String accessToken = GetAccessToken.getAccessToken();
if (accessToken == null) {
log.error("获取令牌失败");
return;
}
URIBuilder uriBuilder = new URIBuilder(apiUrl);
uriBuilder.addParameter("access_token", accessToken);
String urlWithParams = uriBuilder.build().toString();
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(urlWithParams);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity);
Map<String, Object> responseresult = new Gson().fromJson(result, Map.class);
Map<String, Object> data = (Map<String, Object>) responseresult.get("data");
double free = Double.parseDouble(data.get("free").toString());
double threshold = 80; // 设置阈值
if ((100-free) > threshold) {
Cpu cpu = new Cpu();
double free = cpu.getFree();
double threshold = 90;
if((100-free) > threshold){
try{
sendMessageEmail.sendAlert("Cpu内存占用过高警告", "目前内存占用为: " + (100-free) + "%");
} else {
log.info("Cpu内存占用正常: " + (100-free) + "%");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpClient.close();
} catch (IOException e) {
}catch (Exception e){
e.printStackTrace();
}
}else {
log.info("Cpu内存占用正常: " + (100-free) + "%");
}
log.info("Cpu Monitor Job End");
}
......
package com.ebo.module.task;
import com.ebo.module.utils.GetAccessToken;
import com.ebo.module.utils.SendMessageEmail;
import com.google.gson.Gson;
import com.ebo.monitor.model.Jvm;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Map;
@Component
@Slf4j
public class JvmMonitorHandler extends IJobHandler {
@Autowired
private SendMessageEmail sendMessageEmail;
private final static String apiUrl = "http://localhost:8659/monitor/server/jvm";
@XxlJob("jvmMonitorJob")
@Override
public void execute() throws Exception {
log.info("Jvm Monitor Job Start");
String accessToken = GetAccessToken.getAccessToken();
if (accessToken == null) {
log.error("获取令牌失败");
return;
}
// 构建带有查询参数的 URL
URIBuilder uriBuilder = new URIBuilder(apiUrl);
uriBuilder.addParameter("access_token", accessToken);
String urlWithParams = uriBuilder.build().toString();
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(urlWithParams);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity);
// 解析响应数据
Map<String, Object> responseresult = new Gson().fromJson(result, Map.class);
Map<String, Object> data = (Map<String, Object>) responseresult.get("data");
log.info("data为{}",data);
double max = Double.parseDouble(data.get("max").toString());
double total = Double.parseDouble(data.get("total").toString());
double jvmUsage = total/max;
double threshold = 90; // 设置阈值
if (jvmUsage > threshold) {
// 发送警报
sendMessageEmail.sendAlert("内存占用过高警告", "目前内存占用为: " + jvmUsage + "%");
} else {
log.info("内存占用正常: " + jvmUsage + "%");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpClient.close();
} catch (IOException e) {
Jvm jvm = new Jvm();
double jvmUsage = jvm.getUsage();
double threshold = 90;
if (jvmUsage > threshold){
try{
sendMessageEmail.sendAlert("Jvm内存占用过高","目前Jvm内存占用为:"+jvmUsage+"%,请及时检测内存是否泄露");
}catch (RuntimeException e){
e.printStackTrace();
}
}else {
log.info("内存占用正常: " + jvmUsage + "%");
}
log.info("Jvm Monitor Job End");
}
......
package com.ebo.module.task;
import com.ebo.module.utils.GetAccessToken;
import com.ebo.module.utils.SendMessageEmail;
import com.google.gson.Gson;
import com.ebo.monitor.model.Mem;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Map;
@Component
@Slf4j
......@@ -29,55 +19,20 @@ public class MemoryMonitorHandler extends IJobHandler {
@Override
public void execute() throws Exception {
log.info("Memory Monitor Job Start");
String accessToken = GetAccessToken.getAccessToken();
if (accessToken == null) {
log.error("获取令牌失败");
return;
}
String apiUrl = "http://localhost:8659/monitor/server/mem";
// 构建带有查询参数的 URL
URIBuilder uriBuilder = new URIBuilder(apiUrl);
uriBuilder.addParameter("access_token", accessToken);
String urlWithParams = uriBuilder.build().toString();
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(urlWithParams);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity);
// 解析响应数据
Map<String, Object> responseresult = new Gson().fromJson(result, Map.class);
Map<String, Object> data = (Map<String, Object>) responseresult.get("data");
// 检查内存使用率
double usage = Double.parseDouble(data.get("usage").toString());
double threshold = 80.0; // 设置阈值
Mem men = new Mem();
double usage = men.getUsage();
double threshold = 90.0;
if (usage > threshold) {
try{
log.info("开始发送邮箱");
// 发送警报
sendMessageEmail.sendAlert("内存占用过高警告", "目前内存占用为: " + usage + "%");
} else {
log.info("内存占用正常: " + usage + "%");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpClient.close();
} catch (IOException e) {
}catch (Exception e){
e.printStackTrace();
}
} else {
log.info("内存占用正常: " + usage + "%");
}
log.info("Memory Monitor Job End");
}
......
package com.ebo.module.task;
import com.ebo.module.utils.SendMessageEmail;
import com.ebo.monitor.model.Sys;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class SysMonitorHandler extends IJobHandler {
@Autowired
private SendMessageEmail sendMessageEmail;
@XxlJob("sysMonitorJob")
@Override
public void execute() throws Exception {
log.info("Memory Monitor Job Start");
try {
Sys sys = new Sys();
sendMessageEmail.sendAlert("操作系统信息查看", "目前操作系统为 " + sys);
}catch (RuntimeException e){
e.printStackTrace();
}
}
}
package com.ebo.module.utils;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.Map;
@Slf4j
public class GetAccessToken {
/**
* 以管理员身份跳过认证
* @return
*/
public static String getAccessToken() {
String authUrl = "http://localhost:8659/sys/auth/login";
String username = "guoxianhui";
String password = "guoxianhui2002";
String key = ""; // 如果有 key,填写对应的值
String captcha = ""; // 如果有 captcha,填写对应的值
HttpPost httpPost = new HttpPost(authUrl);
httpPost.setHeader("Content-Type", "application/json");
String json = String.format("{\"username\":\"%s\"," +
"\"password\":\"%s\"," +
"\"key\":\"%s\"," +
"\"captcha\":\"%s\"}",
username, password, key, captcha);
try (CloseableHttpClient httpClient = HttpClients.createDefault();){
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
try (CloseableHttpResponse response = httpClient.execute(httpPost);){
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
String result = EntityUtils.toString(responseEntity);
log.info("Auth Response: " + result);
// 解析响应数据
Map<String, Object> responseMap = new Gson().fromJson(result, Map.class);
Map<String, Object> dataMap = (Map<String, Object>) responseMap.get("data");
return dataMap.get("access_token").toString();
}
}
} catch (IOException e) {
log.error("Failed to get access token", e);
}
return null;
}
}
......@@ -64,3 +64,12 @@ mybatis:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
xxljob:
executor:
qqemail:
smtp-host: smtp.qq.com
smtp-port: 587
smtp-user: 1498377512@qq.com
smtp-pass: fumtxqlqsudliaia
to-email: 1498377512@qq.com
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