228 lines
8.5 KiB
Plaintext
228 lines
8.5 KiB
Plaintext
|
|
package com.securityControl.system.service.impl;
|
||
|
|
|
||
|
|
import com.securityControl.common.core.utils.DateUtils;
|
||
|
|
import com.securityControl.common.core.utils.ServletUtils;
|
||
|
|
import com.securityControl.common.core.utils.aes.DateTimeHelper;
|
||
|
|
import com.securityControl.common.core.utils.ip.IpUtils;
|
||
|
|
import com.securityControl.common.core.web.domain.AjaxResult;
|
||
|
|
import com.securityControl.common.log.enums.OperationType;
|
||
|
|
import com.securityControl.common.security.utils.SecurityUtils;
|
||
|
|
import com.securityControl.system.api.domain.SysOperLog;
|
||
|
|
import com.securityControl.system.api.domain.SysUser;
|
||
|
|
import com.securityControl.system.api.domain.SysWarningLog;
|
||
|
|
import com.securityControl.system.api.model.LoginUser;
|
||
|
|
import com.securityControl.system.domain.qo.SysLogQo;
|
||
|
|
import com.securityControl.system.domain.qo.SysLogStatisticQo;
|
||
|
|
import com.securityControl.system.domain.vo.SysLogStatisticVo;
|
||
|
|
import com.securityControl.system.domain.vo.SysLogVo;
|
||
|
|
import com.securityControl.system.domain.vo.SysRunningLogVo;
|
||
|
|
import com.securityControl.system.mapper.SysLogsDao;
|
||
|
|
import com.securityControl.system.service.SysLogsService;
|
||
|
|
import org.slf4j.Logger;
|
||
|
|
import org.slf4j.LoggerFactory;
|
||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||
|
|
import org.springframework.stereotype.Service;
|
||
|
|
|
||
|
|
import javax.annotation.Resource;
|
||
|
|
import java.math.BigDecimal;
|
||
|
|
import java.math.RoundingMode;
|
||
|
|
import java.net.InetAddress;
|
||
|
|
import java.net.UnknownHostException;
|
||
|
|
import java.time.LocalDateTime;
|
||
|
|
import java.util.*;
|
||
|
|
|
||
|
|
@Service
|
||
|
|
public class SysLogsServiceImpl implements SysLogsService {
|
||
|
|
|
||
|
|
private static final Logger log = LoggerFactory.getLogger(SysLogsServiceImpl.class);
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private SysLogsDao dao;
|
||
|
|
|
||
|
|
|
||
|
|
@Resource(name = "taskExecutor")
|
||
|
|
private ThreadPoolTaskExecutor taskExecutor;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 记录日志
|
||
|
|
*
|
||
|
|
* @param sysLog
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
@Override
|
||
|
|
public AjaxResult saveLogs(SysOperLog sysLog) {
|
||
|
|
try {
|
||
|
|
dao.saveLogs(sysLog);
|
||
|
|
} catch (Exception e) {
|
||
|
|
log.error(e.toString(), e);
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public List<SysLogVo> queryByPage(SysLogQo sysLogQo) {
|
||
|
|
return dao.queryByPage(sysLogQo);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public List<SysRunningLogVo> getRunningLog(SysLogQo sysLogQo) {
|
||
|
|
List<SysRunningLogVo> runningLog = dao.getRunningLog(sysLogQo);
|
||
|
|
for (SysRunningLogVo logVo : runningLog) {
|
||
|
|
int runningTime = DateUtils.diffDay(logVo.getSysStartTime(), DateUtils.getTime());
|
||
|
|
logVo.setSysRunningTime(runningTime);
|
||
|
|
}
|
||
|
|
return runningLog;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void updateEndTime() {
|
||
|
|
SysOperLog query = new SysOperLog();
|
||
|
|
query.setLogType("运行日志");
|
||
|
|
dao.updateEndTime(query);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public SysLogStatisticVo statistics(SysLogStatisticQo statisticQo) {
|
||
|
|
SysLogStatisticVo totalStatic = dao.getTotalStatic(statisticQo);
|
||
|
|
List<Map<String, Object>> statistics = dao.statistics(statisticQo);
|
||
|
|
List<SysLogStatisticVo.SysLogSubStatistic> statisticList = new ArrayList<>();
|
||
|
|
SysLogStatisticVo.SysLogSubStatistic subStatistic;
|
||
|
|
if (Objects.equals(statisticQo.getKeyword(), "logType")) {
|
||
|
|
for (Map<String, Object> map : statistics) {
|
||
|
|
subStatistic = new SysLogStatisticVo.SysLogSubStatistic();
|
||
|
|
subStatistic.setName(String.valueOf(map.get("logType")));
|
||
|
|
subStatistic.setValue(String.valueOf(map.get("total")));
|
||
|
|
statisticList.add(subStatistic);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
for (Map<String, Object> map : statistics) {
|
||
|
|
subStatistic = new SysLogStatisticVo.SysLogSubStatistic();
|
||
|
|
subStatistic.setName(String.valueOf(map.get("grade")));
|
||
|
|
subStatistic.setValue(String.valueOf(map.get("total")));
|
||
|
|
statisticList.add(subStatistic);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
totalStatic.setStatistics(statisticList);
|
||
|
|
return totalStatic;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void updateContent(String number) {
|
||
|
|
dao.updateContent(number);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public Map<String, Object> colLogContent() {
|
||
|
|
Map<String, Object> resultMap = new HashMap<>(2);
|
||
|
|
Map<String, Object> maxLogSize = dao.getMaxLogSize();
|
||
|
|
Map<String, Object> usedMaxSize = dao.getUsedMaxSize();
|
||
|
|
|
||
|
|
BigDecimal b1 = new BigDecimal(String.valueOf(maxLogSize.get("contentSize")));
|
||
|
|
BigDecimal b2 = new BigDecimal(String.valueOf(usedMaxSize.get("logSize")));
|
||
|
|
|
||
|
|
double rate = b2.divide(b1, RoundingMode.HALF_UP).doubleValue();
|
||
|
|
if (rate >= 0.90) {
|
||
|
|
log.warn("内存报警发送邮件!!!");
|
||
|
|
addWaringLog("基建视频安全管控系统日志容量已超过" + rate * 100 + "%");
|
||
|
|
}
|
||
|
|
resultMap.put("maxLogSize", maxLogSize);
|
||
|
|
resultMap.put("usedMaxSize", usedMaxSize);
|
||
|
|
resultMap.put("useRate", rate);
|
||
|
|
return resultMap;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public Map<String, Object> getLogContent() {
|
||
|
|
return dao.getLogContent();
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void copyLog(String logType) {
|
||
|
|
//上次备份时间
|
||
|
|
Map<String, Object> lastCopyLogSet = dao.getLastCopyLogSet(logType);
|
||
|
|
LocalDateTime updateDate = (LocalDateTime) lastCopyLogSet.get("updateDate");
|
||
|
|
//查询到现在的日志
|
||
|
|
List<SysOperLog> copyData = dao.getCopyData(logType, updateDate);
|
||
|
|
//插入备份表
|
||
|
|
for (SysOperLog datum : copyData) {
|
||
|
|
taskExecutor.execute(() -> {
|
||
|
|
dao.saveCopyLogs(datum);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
//更新备份表的上次备份时间
|
||
|
|
dao.updateLastCopyTime(logType, DateUtils.getTime());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void restoreLog(String logType) {
|
||
|
|
//上次备份时间
|
||
|
|
Map<String, Object> lastCopyLogSet = dao.getLastCopyLogSet(logType);
|
||
|
|
LocalDateTime updateDate = (LocalDateTime) lastCopyLogSet.get("updateDate");
|
||
|
|
//查询所有的备份日志
|
||
|
|
List<SysOperLog> copyData = dao.restoreLog(logType);
|
||
|
|
//删除当前的以备份过的日志
|
||
|
|
int count = dao.delRestoreLog(logType, updateDate);
|
||
|
|
//插入日志表
|
||
|
|
for (SysOperLog datum : copyData) {
|
||
|
|
taskExecutor.execute(() -> {
|
||
|
|
dao.saveRestoreLog(datum);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
//更新备份表的上次备份时间
|
||
|
|
dao.updateLastCopyTime(logType, DateUtils.getTime());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public Map<String, String> getYueQuan() {
|
||
|
|
return dao.getYueQuan();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
private void waringLog(String title, String detail) {
|
||
|
|
InetAddress address = null;
|
||
|
|
String hostAddress = "";
|
||
|
|
try {
|
||
|
|
address = InetAddress.getLocalHost();
|
||
|
|
hostAddress = address.getHostAddress();
|
||
|
|
} catch (UnknownHostException e1) {
|
||
|
|
log.warn("====获取服务器ip异常====");
|
||
|
|
e1.printStackTrace();
|
||
|
|
}
|
||
|
|
SysWarningLog warningLog = new SysWarningLog();
|
||
|
|
warningLog.setOperateType(title);
|
||
|
|
warningLog.setOperationIp(hostAddress);
|
||
|
|
warningLog.setOperateTitle("日志容量上限告警");
|
||
|
|
warningLog.setOperateDetails(detail);
|
||
|
|
warningLog.setOperateJb("2");//5致命,4严重,3中等,2一般,1轻微
|
||
|
|
warningLog.setFastTime(DateUtils.getTime());
|
||
|
|
warningLog.setOperationName("定时任务");
|
||
|
|
dao.insertWarningLog(warningLog);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void addWaringLog(String detail) {
|
||
|
|
SysOperLog sysOperLog = new SysOperLog();
|
||
|
|
sysOperLog.setGrade(OperationType.WARNING_LOG.getInfo());
|
||
|
|
sysOperLog.setTimes(DateTimeHelper.getNowTime());
|
||
|
|
sysOperLog.setOperName(SecurityUtils.getUsername());
|
||
|
|
LoginUser loginUser = SecurityUtils.getLoginUser();
|
||
|
|
if (null != loginUser && null != loginUser.getSysUser()) {
|
||
|
|
SysUser sysUser = loginUser.getSysUser();
|
||
|
|
sysOperLog.setRoleName(sysUser.getRoleName());
|
||
|
|
sysOperLog.setDeptName(sysUser.getOrgName());
|
||
|
|
}
|
||
|
|
sysOperLog.setOperIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
|
||
|
|
sysOperLog.setTitle("日志管理");
|
||
|
|
sysOperLog.setRequestMethod("POST");
|
||
|
|
sysOperLog.setMethod("com.securityControl.system.controller.SysLogsController()");
|
||
|
|
sysOperLog.setBusinessType(0);
|
||
|
|
sysOperLog.setOperUrl("/sys/logs/colLogContent");
|
||
|
|
sysOperLog.setOperParam("");
|
||
|
|
sysOperLog.setDetail(detail);
|
||
|
|
sysOperLog.setLogType("系统日志");
|
||
|
|
sysOperLog.setSysMenu("");
|
||
|
|
saveLogs(sysOperLog);
|
||
|
|
}
|
||
|
|
}
|