package com.wgcloud.controller; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.wgcloud.entity.FileWarnInfo; import com.wgcloud.entity.FileWarnState; import com.wgcloud.service.*; import com.wgcloud.util.ThreadPoolUtil; import com.wgcloud.util.TokenUtils; import com.wgcloud.util.msg.WarnMailUtil; import com.wgcloud.util.staticvar.BatchData; import com.wgcloud.util.staticvar.StaticKeys; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.Date; import java.util.Iterator; /** * @version v3.3 * @ClassName:AgentLogGoController.java * @author: http://www.wgstart.com * @date: 2021年1月16日 * @Description: 日志监控处理,默认每10分钟提交一次 * @Copyright: 2019-2021 wgcloud. All rights reserved. */ @Controller @RequestMapping("/agentLogGo") public class AgentLogGoController { private static final Logger logger = LoggerFactory.getLogger(AgentLogGoController.class); @Resource private LogInfoService logInfoService; @Resource private SystemInfoService systemInfoService; @Autowired private DockerInfoService dockerInfoService; @Autowired private FileWarnInfoService fileWarnInfoService; @Autowired private AppInfoService appInfoService; @Autowired private PortInfoService portInfoService; @Autowired private TokenUtils tokenUtils; @ResponseBody @RequestMapping("/minTask") public JSONObject minTask(@RequestBody String paramBean) { JSONObject agentJsonObject = (JSONObject) JSONUtil.parse(paramBean); logger.debug("agent上报日志监控数据-------------" + agentJsonObject.toString()); JSONObject resultJson = new JSONObject(); if (!tokenUtils.checkAgentToken(agentJsonObject)) { logger.error(StaticKeys.TOKEN_ERROR); resultJson.set("result", StaticKeys.TOKEN_ERROR); return resultJson; } Date nowtime = new Date(); try { JSONObject fileWarnInfosJson = agentJsonObject.getJSONObject("fileWarnInfos"); if (fileWarnInfosJson == null) { logger.error("fileWarnList is null"); resultJson.set("result", "error:fileWarnList is null"); return resultJson; } Iterator iter = fileWarnInfosJson.keySet().iterator(); while (iter.hasNext()) { String id = (String) iter.next(); JSONObject jsonObject = fileWarnInfosJson.getJSONObject(id); String filePath = jsonObject.getStr("filePath");//日志文件路径 String fileSize = jsonObject.getStr("fileSize");//日志文件当前大小 String warnRows = jsonObject.getStr("warnRows");//本次读取日志的行数 String warnContent = jsonObject.getStr("warnContent");//日志告警内容 FileWarnState state = new FileWarnState(); state.setCreateTime(nowtime); state.setFilePath(filePath); state.setFileWarnId(id); state.setWarContent(warnContent); if (!StringUtils.isEmpty(warnContent)) { BatchData.FILEWARN_STATE_LIST.add(state); } FileWarnInfo info = new FileWarnInfo(); info.setId(id); info.setCreateTime(nowtime); info.setFileSize(fileSize); info.setWarnRows(warnRows); BatchData.FILEWARN_INFO_LIST.add(info); //日志有错误信息,发邮件 begin if (!StringUtils.isEmpty(warnContent)) { Runnable runnable = () -> { try { FileWarnInfo fileWarnInfo = fileWarnInfoService.selectById(info.getId()); if (fileWarnInfo != null) { WarnMailUtil.sendFileWarnDown(fileWarnInfo, filePath, warnContent, true); } } catch (Exception e) { e.printStackTrace(); } }; ThreadPoolUtil.executor.execute(runnable); } //日志有错误信息,发邮件 end } resultJson.set("result", "success"); } catch (Exception e) { logger.error("解析日志监控上报数据错误", e); resultJson.set("result", "error:" + e.toString()); } return resultJson; } }