feat:修改Host指标信息

This commit is contained in:
nieziyan 2024-02-04 14:06:14 +08:00
parent fc1ff16fac
commit 4ccf691ed9
8 changed files with 137 additions and 47 deletions

View File

@ -125,4 +125,35 @@ public class NumUtil {
return false;
}
}
public static String kb2Gb(String kb, int scale){
if (StrUtil.isBlank(kb))
return null;
BigDecimal decimal = new BigDecimal(kb);
double v = decimal.divide(new BigDecimal(1024), scale, RoundingMode.HALF_UP)
.divide(new BigDecimal(1024), scale, RoundingMode.HALF_UP)
.doubleValue();
return String.valueOf(v);
}
public static String byte2Mb(String bytes, int scale){
if (StrUtil.isBlank(bytes))
return null;
BigDecimal decimal = new BigDecimal(bytes);
double v = decimal.divide(new BigDecimal(1024), scale, RoundingMode.HALF_UP)
.divide(new BigDecimal(1024), scale, RoundingMode.HALF_UP)
.doubleValue();
return String.valueOf(v);
}
public static String byte2Gb(String bytes, int scale){
if (StrUtil.isBlank(bytes))
return null;
BigDecimal decimal = new BigDecimal(bytes);
double v = decimal.divide(new BigDecimal(1024), scale, RoundingMode.HALF_UP)
.divide(new BigDecimal(1024), scale, RoundingMode.HALF_UP)
.divide(new BigDecimal(1024), scale, RoundingMode.HALF_UP)
.doubleValue();
return String.valueOf(v);
}
}

View File

@ -1,5 +1,6 @@
package org.jeecg.modules.base.dto;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.experimental.Accessors;
@ -21,21 +22,29 @@ public class DatabaseDto implements Serializable {
private boolean online;
private String slowQuery;
private Integer alarms;
private boolean alarmRed;
private String cpuUutilzation;
private String dbMemory; // kb
private boolean cpuRed;
private String logRemainingSize; // byte
private String memoryUsage;
private String dbSize; // byte
private boolean memoryRed;
private String diskUsage;
private boolean diskRed;
public void setDbMemory(String dbMemory) {
if (StrUtil.isNotBlank(dbMemory))
this.dbMemory = dbMemory + "GB";
}
public void setLogRemainingSize(String logRemainingSize) {
if (StrUtil.isNotBlank(logRemainingSize))
this.logRemainingSize = logRemainingSize + "MB";
}
public void setDbSize(String dbSize) {
if (StrUtil.isNotBlank(dbSize))
this.dbSize = dbSize + "GB";
}
}

View File

@ -15,17 +15,10 @@ public class NameValue implements Serializable {
private Boolean value;
private String valueT;
private Integer usage; // 单位 MB
public NameValue(String name, Boolean value) {
this.name = name;
this.value = value;
}
public NameValue(String name, String valueT) {
this.name = name;
this.valueT = valueT;
}
}

View File

@ -0,0 +1,14 @@
package org.jeecg.modules.base.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum DbItem {
DBMEMORY("dbMemory"),
LOGREMAININGSIZE("logRemainingSize"),
DBSIZE("dblSize");
private final String value;
}

View File

@ -2,6 +2,7 @@ package org.jeecg.modules.aspect;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
@ -103,8 +104,7 @@ public class StatusAspect {
databaseService.status2Redis(database);
String token = ManageUtil.getToken();
Page<Host> hostPage = monitorAlarm.dbList(null, 1, 99, token).getResult();
if (ObjectUtil.isNull(hostPage) || CollUtil.isEmpty(hostPage.getRecords()))
return;
if (ObjectUtil.isNull(hostPage) || CollUtil.isEmpty(hostPage.getRecords())) return;
List<Host> hosts = hostPage.getRecords();
String name = database.getName();
Host host = null;
@ -112,8 +112,7 @@ public class StatusAspect {
if (StrUtil.equals(name, oneHost.getCode()))
host = oneHost;
}
if (ObjectUtil.isNull(host))
return;
if (ObjectUtil.isNull(host)) return;
// 更新数据源的HostId
database.setHostId(host.getHostId());
databaseService.updateById(database);
@ -127,7 +126,7 @@ public class StatusAspect {
/*
* 新增|修改服务器信息后 异步更新其状态信息
* 更新服务器的状态(-1未知 1正常 2离线 3告警)和它对应的系统监控的HostId
* 更新服务器的状态(1正常 2离线 3告警)和它对应的系统监控的HostId
* */
@Async
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysServerServiceImpl.update(..)) || " +
@ -136,7 +135,7 @@ public class StatusAspect {
Object[] args = point.getArgs();
if (ArrayUtil.length(args) == 0) return;
String key = RedisConstant.SERVER_STATUS;
String status = ServerStatus.OFF.getValue();
Boolean online = null;
SysServer server = (SysServer) args[0];
String id = server.getId();
String name = server.getName();
@ -146,7 +145,7 @@ public class StatusAspect {
Page<Host> hostPage = monitorAlarm.listApp(ipAddress, MonitorConstant.SERVER_APP,
MonitorConstant.pageNo, MonitorConstant.pageSize, token).getResult();
if (ObjectUtil.isNull(hostPage) || CollUtil.isEmpty(hostPage.getRecords())){
redisUtil.hset(key, id, new NameValue(name, status));
redisUtil.hset(key, id, new NameValue(name, online));
return;
}
List<Host> hosts = hostPage.getRecords();
@ -156,12 +155,14 @@ public class StatusAspect {
host = oneHost;
}
if (ObjectUtil.isNull(host)){
redisUtil.hset(key, id, new NameValue(name, status));
redisUtil.hset(key, id, new NameValue(name, online));
return;
}
// 更新该服务器状态信息
status = host.getStatus();
redisUtil.hset(key, id, new NameValue(name, status));
String status = host.getStatus();
online = CollUtil.contains(ListUtil.toList(ServerStatus.ON.getValue(),
ServerStatus.WARN.getValue()), status);
redisUtil.hset(key, id, new NameValue(name, online));
// 更新该服务器的HostId
server.setHostId(host.getHostId());
serverService.updateById(server);

View File

@ -2,24 +2,33 @@ package org.jeecg.modules.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.QueryRequest;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.*;
import org.jeecg.common.constant.enums.DbType;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.JDBCUtil;
import org.jeecg.common.util.NumUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.base.dto.*;
import org.jeecg.modules.base.entity.monitor.Host;
import org.jeecg.modules.base.entity.monitor.Item;
import org.jeecg.modules.base.entity.postgre.SysDatabase;
import org.jeecg.modules.base.bizVo.SourceVo;
import org.jeecg.modules.base.enums.DbItem;
import org.jeecg.modules.entity.AlarmHistory;
import org.jeecg.modules.feignclient.ManageUtil;
import org.jeecg.modules.feignclient.MonitorAlarm;
import org.jeecg.modules.feignclient.SystemClient;
import org.jeecg.modules.mapper.DBRowMapper;
import org.jeecg.modules.mapper.SpaceRowMapper;
@ -40,7 +49,7 @@ import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static org.jeecg.common.constant.enums.DbType.*;
@Slf4j
@Service("sysDatabaseService")
public class SysDatabaseServiceImpl extends ServiceImpl<SysDatabaseMapper, SysDatabase> implements ISysDatabaseService {
@ -50,6 +59,9 @@ public class SysDatabaseServiceImpl extends ServiceImpl<SysDatabaseMapper, SysDa
@Autowired
private SystemClient systemClient;
@Autowired
private MonitorAlarm monitorAlarm;
@Override
public Result<?> findPage(QueryRequest query) {
Integer pageNo = query.getPageNo();
@ -72,23 +84,53 @@ public class SysDatabaseServiceImpl extends ServiceImpl<SysDatabaseMapper, SysDa
// 数据库连接状态Map key:id value:状态值(true|false)
String statusKey = RedisConstant.DATABASE_STATUS;
Map<Object, Object> statusMap = redisUtil.hmget(statusKey);
// 向运管系统查询数据库相关指标信息
Map<String,Host> dbMap = new HashMap<>();
try {
String token = ManageUtil.getToken();
Page<Host> dbPage = monitorAlarm.dbList(null, 1, 99, token).getResult();
if (ObjectUtil.isNotNull(dbPage) && CollUtil.isNotEmpty(dbPage.getRecords()))
dbMap = dbPage.getRecords().stream().collect(Collectors.toMap(Host::getName, host -> host));
}catch (FeignException.Unauthorized e){
ManageUtil.refreshToken();
log.warn("向运管系统查询DB信息异常: Token失效,已刷新Token");
}catch (Exception e){
log.error("向运管系统查询DB信息异常: {}", e.getMessage());
}
for (DatabaseDto databaseDto : databaseDtos) {
boolean online = false;
String id = databaseDto.getId();
String type = databaseDto.getType();
String name = databaseDto.getName();
Integer alarms = databaseDto.getAlarms();
boolean alarmRed = ObjectUtil.isNotNull(alarms) && alarms > 0;
NameValue nameValue = (NameValue) statusMap.get(id);
if (ObjectUtil.isNotNull(nameValue)){
Boolean value = nameValue.getValue();
if (ObjectUtil.isNotNull(value))
online = value;
if (ObjectUtil.isNotNull(value)) online = value;
}
String dataBaseType = dataSourceMap.get(type);
databaseDto.setDataBaseType(dataBaseType)
.setOnline(online).setSlowQuery("328/s")
.setAlarmRed(alarmRed).setCpuUutilzation("35.8%")
.setMemoryUsage("55.8%").setDiskUsage("35.6%");
.setAlarmRed(alarmRed).setOnline(online);
// 获取数据库指标信息
Host db = dbMap.get(name);
if (ObjectUtil.isNull(db)) continue;
if (!online) continue;
Map<String, Item> itemMap = db.getItems();
if (MapUtil.isEmpty(itemMap)) continue;
// dbMemory kb -> GB
Item item = itemMap.get(DbItem.DBMEMORY.getValue());
if (ObjectUtil.isNotNull(item))
databaseDto.setDbMemory(NumUtil.kb2Gb(item.getLastValue(), 2));
// logRemainingSize byte -> MB
item = itemMap.get(DbItem.LOGREMAININGSIZE.getValue());
if (ObjectUtil.isNotNull(item))
databaseDto.setLogRemainingSize(NumUtil.byte2Mb(item.getLastValue(), 2));
// dbSize byte -> GB
item = itemMap.get(DbItem.DBSIZE.getValue());
if (ObjectUtil.isNotNull(item))
databaseDto.setDbSize(NumUtil.byte2Gb(item.getLastValue(), 2));
}
page.setRecords(databaseDtos);
return Result.OK(page);

View File

@ -2,6 +2,7 @@ package org.jeecg.modules.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
@ -100,13 +101,10 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
serverDto.setAlarmRed(alarms > 0);
// 设置服务器状态信息
NameValue nameValue = (NameValue) statusMap.get(id);
String status = OFF.getValue();
if (ObjectUtil.isNotNull(nameValue)){
String valueT = nameValue.getValueT();
if (StrUtil.isNotBlank(valueT))
status = valueT;
}
boolean online = StrUtil.equals(status, ON.getValue());
Boolean value = null;
if (ObjectUtil.isNotNull(nameValue))
value = nameValue.getValue();
boolean online = ObjectUtil.isNotNull(value) && value;
// 设置服务器的硬件使用情况信息
Host host = hostMap.get(hostId);
if (ObjectUtil.isNull(host))
@ -123,7 +121,6 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
String memory = StrUtil.isBlank(memoryValue) ? "--" :
NumUtil.keepStr(memoryValue, 1) + "%";
// 磁盘使用率
serverDto.setOnline(online).setCpuUutilzation(cpu)
.setMemoryUsage(memory).setDiskUsage("--");
}
@ -372,6 +369,7 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
MonitorConstant.pageNo, MonitorConstant.pageSize, token).getResult().getRecords();
Map<String, Host> hostMap = hosts.stream().collect(Collectors.toMap(Host::getHostId, Host -> Host));
for (SysServer server : sysServers) {
Boolean online = null;
String hostId = server.getHostId();
String name = server.getName();
String serverId = server.getId();
@ -379,11 +377,13 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
// 获取该服务器的状态并保存
if (ObjectUtil.isNotNull(host)) {
String status = host.getStatus();
values.put(serverId, new NameValue(name, status));
online = CollUtil.contains(ListUtil.toList(ServerStatus.ON.getValue(),
ServerStatus.WARN.getValue()), status);
values.put(serverId, new NameValue(name, online));
continue;
}
// 当前服务器不在监控服务器列表 将它的状态设置为未知
values.put(serverId, new NameValue(name, OFF.getValue()));
// 当前服务器不在监控服务器列表
values.put(serverId, new NameValue(name, online));
}
redisUtil.hmset(key, values);
}catch (FeignException.Unauthorized e){
@ -412,7 +412,7 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
for (SysServer sysServer : sysServers) {
String id = sysServer.getId();
String name = sysServer.getName();
values.put(id, new NameValue(name, OFF.getValue()));
values.put(id, new NameValue(name, null));
}
redisUtil.hmset(key, values);
}

View File

@ -63,11 +63,11 @@ public class JeecgSpectrumAnalysisApplication extends SpringBootServletInitializ
@Override
public void run(String... args) throws Exception {
//Windows加载dll工具库
System.loadLibrary("ReadPHDFile");
System.loadLibrary("GammaAnaly");
/*System.loadLibrary("ReadPHDFile");
System.loadLibrary("GammaAnaly");*/
//Linux版本加载dll工具库
// System.load("/usr/local/jdk/lib/libReadPHDFile.so");
// System.load("/usr/local/jdk/lib/libGammaAnalyALG.so");
System.load("/usr/local/jdk/lib/libReadPHDFile.so");
System.load("/usr/local/jdk/lib/libGammaAnalyALG.so");
//创建缓存
betaCache.initCache();
localCache.initCache();