kongdeqiang
2022-12-16 daf6a95086087ec99232eea8b4648b7541881f7c
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
124
125
126
127
128
129
130
131
132
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;
    }
 
 
}