package com.wgcloud.controller; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.wgcloud.entity.CustomInfo; import com.wgcloud.entity.CustomState; import com.wgcloud.service.CustomInfoService; import com.wgcloud.service.DockerInfoService; import com.wgcloud.service.LogInfoService; import com.wgcloud.service.SystemInfoService; import com.wgcloud.util.FormatUtil; 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:AgentCustomGoController.java * @author: http://www.wgstart.com * @date: 2022年9月16日 * @Description: 自定义监控项处理,默认每10分钟提交一次 * @Copyright: 2019-2022 wgcloud. All rights reserved. */ @Controller @RequestMapping("/agentCustomGo") public class AgentCustomGoController { private static final Logger logger = LoggerFactory.getLogger(AgentCustomGoController.class); @Resource private LogInfoService logInfoService; @Resource private SystemInfoService systemInfoService; @Autowired private DockerInfoService dockerInfoService; @Autowired private CustomInfoService customInfoService; @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 customInfosJson = agentJsonObject.getJSONObject("customInfos"); if (customInfosJson == null) { logger.error("customInfoList is null"); resultJson.set("result", "error:customInfoList is null"); return resultJson; } Iterator iter = customInfosJson.keySet().iterator(); while (iter.hasNext()) { String id = (String) iter.next(); JSONObject jsonObject = customInfosJson.getJSONObject(id); //监控项执行结果值 String customValue = jsonObject.getStr("customValue"); CustomState state = new CustomState(); state.setCreateTime(nowtime); state.setCustomInfoId(id); state.setCustomValue(customValue); if (!StringUtils.isEmpty(customValue)) { BatchData.CUSTOM_STATE_LIST.add(state); } CustomInfo info = new CustomInfo(); info.setId(id); info.setCreateTime(nowtime); info.setCustomValue(customValue); info.setState(StaticKeys.ON_STATE); BatchData.CUSTOM_INFO_LIST.add(info); //判断告警表达式是否成立,发邮件 begin if (!StringUtils.isEmpty(customValue)) { Runnable runnable = () -> { try { CustomInfo customInfo = customInfoService.selectById(info.getId()); if (customInfo != null) { Double customValueDouble = Double.valueOf(customValue); Boolean result = FormatUtil.validateExpression(customInfo.getResultExp(), customValueDouble); if (result) { info.setState(StaticKeys.DOWN_STATE); customInfo.setCustomValue(customValue); WarnMailUtil.sendCustomInfoDown(customInfo, customInfo.getAccount()); } } } 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; } }