package com.wgcloud.controller; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.wgcloud.config.CommonConfig; import com.wgcloud.entity.HeathMonitor; import com.wgcloud.entity.HeathState; import com.wgcloud.service.HeathMonitorService; import com.wgcloud.service.LogInfoService; import com.wgcloud.util.ResDataUtils; import com.wgcloud.util.TokenUtils; import com.wgcloud.util.msg.WarnMailUtil; import com.wgcloud.util.msg.WarnPools; import com.wgcloud.util.staticvar.BatchData; import com.wgcloud.util.staticvar.StaticKeys; 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.List; /** * @version v3.3 * @ClassName:AgentHeathMonitorGoController.java * @author: http://www.wgstart.com * @date: 2022年9月16日 * @Description: server-backup监控服务接口数据提交处理,默认每10分钟提交一次 * @Copyright: 2019-2022 wgcloud. All rights reserved. */ @Controller @RequestMapping("/agentHeathMonitorGo") public class AgentHeathMonitorGoController { private static final Logger logger = LoggerFactory.getLogger(AgentHeathMonitorGoController.class); @Resource private LogInfoService logInfoService; @Autowired private HeathMonitorService heathMonitorService; @Autowired private TokenUtils tokenUtils; @Autowired private CommonConfig commonConfig; @ResponseBody @RequestMapping("/minTask") public String minTask(@RequestBody String paramBean) { JSONObject agentJsonObject = (JSONObject) JSONUtil.parse(paramBean); logger.debug("server-backup监控服务接口上报数据-------------" + agentJsonObject.toString()); JSONObject resultJson = new JSONObject(); if (!tokenUtils.checkAgentToken(agentJsonObject)) { logger.error(StaticKeys.TOKEN_ERROR); return ResDataUtils.resetErrorJson(StaticKeys.TOKEN_ERROR); } Date nowtime = new Date(); try { //服务接口上报数据 JSONArray heathMonitorsJsonArr = agentJsonObject.getJSONArray("heathMonitorsUpdate"); if (heathMonitorsJsonArr == null) { logger.error("heathMonitorsUpdate is null"); return ResDataUtils.resetErrorJson("heathMonitorsUpdate is null"); } //处理数据源状态数据 List heathMonitorList = JSONUtil.toList(heathMonitorsJsonArr, HeathMonitor.class); for (HeathMonitor h : heathMonitorList) { //之前存贮的旧数据 HeathMonitor heathMonitorSaved = heathMonitorService.selectById(h.getId()); h.setCreateTime(nowtime); //接口下线后,不再更新时间 begin if (!StaticKeys.HEATH_STATUS_200.equals(h.getHeathStatus())) { h.setCreateTime(null); } //接口下线后,不再更新时间 end try { heathMonitorService.updateForServerBackup(h); } catch (Exception e) { e.printStackTrace(); } HeathState heathState = new HeathState(); heathState.setResTimes(h.getResTimes()); heathState.setHeathId(h.getId()); heathState.setCreateTime(nowtime); BatchData.HEATH_STATE_LIST.add(heathState); heathMonitorSaved.setHeathStatus(h.getHeathStatus()); if (!StaticKeys.HEATH_STATUS_200.equals(h.getHeathStatus())) { //返回错误处理 WarnMailUtil.sendHeathInfo(heathMonitorSaved, true); } else { //返回成功处理 if (null != WarnPools.MEM_WARN_MAP.get(h.getId())) { WarnMailUtil.sendHeathInfo(heathMonitorSaved, false); } } } } catch (Exception e) { logger.error("解析server-backup监控服务接口上报数据错误", e); return ResDataUtils.resetErrorJson(e.toString()); } return ResDataUtils.resetSuccessJson(null); } }