kongdeqiang
2023-02-22 9812f31d6402a268fa6fd35e8c3a2a59582b720d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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.SnmpInfo;
import com.wgcloud.entity.SnmpState;
import com.wgcloud.service.LogInfoService;
import com.wgcloud.service.SnmpInfoService;
import com.wgcloud.service.SnmpStateService;
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:AgentSnmpInfoGoController.java
 * @author: http://www.wgstart.com
 * @date: 2022年9月16日
 * @Description: server-backup监控数通SNMP数据提交处理,默认每20分钟提交一次
 * @Copyright: 2019-2022 wgcloud. All rights reserved.
 */
@Controller
@RequestMapping("/agentSnmpInfoGo")
public class AgentSnmpInfoGoController {
 
 
    private static final Logger logger = LoggerFactory.getLogger(AgentSnmpInfoGoController.class);
 
 
    @Resource
    private LogInfoService logInfoService;
    @Autowired
    private SnmpInfoService snmpInfoService;
    @Autowired
    private SnmpStateService snmpStateService;
    @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监控数通SNMP上报数据-------------" + agentJsonObject.toString());
        if (!tokenUtils.checkAgentToken(agentJsonObject)) {
            logger.error(StaticKeys.TOKEN_ERROR);
            return ResDataUtils.resetErrorJson(StaticKeys.TOKEN_ERROR);
        }
        Date nowtime = new Date();
 
        try {
            //SNMP数据
            JSONArray snmpInfosJsonArr = agentJsonObject.getJSONArray("snmpInfosUpdate");
            if (snmpInfosJsonArr == null) {
                logger.error("snmpInfosUpdate is null");
                return ResDataUtils.resetErrorJson("snmpInfosUpdate is null");
            }
            //处理SNMP状态数据
            List<SnmpInfo> snmpInfoList = JSONUtil.toList(snmpInfosJsonArr, SnmpInfo.class);
            for (SnmpInfo snmpInfo : snmpInfoList) {
                SnmpInfo snmpInfoSaved = snmpInfoService.selectById(snmpInfo.getId());
                snmpInfo.setCreateTime(nowtime);
                snmpInfo.setHostname(snmpInfoSaved.getHostname());
 
                //设备下线后,不再更新时间 begin
                if (StaticKeys.DOWN_STATE.equals(snmpInfo.getState())) {
                    snmpInfo.setCreateTime(null);
                }
                //设备下线后,不再更新时间 end
 
                try {
                    snmpInfoService.updateById(snmpInfo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
 
                if (StaticKeys.ON_STATE.equals(snmpInfo.getState())) {
                    SnmpState snmpState = new SnmpState();
                    snmpState.setRecvAvg(snmpInfo.getRecvAvg());
                    snmpState.setSentAvg(snmpInfo.getSentAvg());
                    snmpState.setCpuPer(snmpInfo.getCpuPer());
                    snmpState.setMemPer(snmpInfo.getMemPer());
                    snmpState.setSnmpInfoId(snmpInfo.getId());
                    snmpState.setCreateTime(nowtime);
                    BatchData.SNMP_STATE_LIST.add(snmpState);
                }
                if (StaticKeys.DOWN_STATE.equals(snmpInfo.getState())) {
                    //返回错误处理
                    WarnMailUtil.sendSnmpInfo(snmpInfoSaved, true);
                } else {
                    //返回成功处理
                    if (null != WarnPools.MEM_WARN_MAP.get(snmpInfoSaved.getId())) {
                        WarnMailUtil.sendSnmpInfo(snmpInfoSaved, false);
                    }
                }
            }
 
        } catch (Exception e) {
            logger.error("解析server-backup监控数通SNMP上报数据错误", e);
            return ResDataUtils.resetErrorJson(e.toString());
        }
        return ResDataUtils.resetSuccessJson(null);
    }
 
 
}