付延余
2022-12-16 f0f8ee8c4a945adbc742d9bab69382b28ad311fb
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package com.wgcloud.controller;
 
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.wgcloud.config.CommonConfig;
import com.wgcloud.entity.*;
import com.wgcloud.service.*;
import com.wgcloud.util.*;
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.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @version v3.3
 * @ClassName:DceInfoController.java
 * @author: http://www.wgstart.com
 * @date: 2021年5月14日
 * @Description: 数通设备PING监测
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
@Controller
@RequestMapping("/dceInfo")
public class DceInfoController {
 
 
    private static final Logger logger = LoggerFactory.getLogger(DceInfoController.class);
 
    @Resource
    private DceInfoService dceInfoService;
    @Resource
    private SystemInfoService systemInfoService;
    @Resource
    private DceStateService dceStateService;
    @Resource
    private LogInfoService logInfoService;
    @Resource
    private DashboardService dashboardService;
    @Resource
    private HostGroupService hostGroupService;
    @Resource
    private ExcelExportService excelExportService;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private CommonConfig commonConfig;
 
    /**
     * agent查询监控数据设备监测列表
     *
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "agentList")
    public String agentList(@RequestBody String paramBean) {
        JSONObject agentJsonObject = (JSONObject) JSONUtil.parse(paramBean);
        if (!tokenUtils.checkAgentToken(agentJsonObject)) {
            logger.error(StaticKeys.TOKEN_ERROR);
            return ResDataUtils.resetErrorJson(StaticKeys.TOKEN_ERROR);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        try {
            if (null != agentJsonObject.get("dceHostNames") && !StringUtils.isEmpty(agentJsonObject.get("dceHostNames").toString())) {
                params.put("dceHostNames", agentJsonObject.get("dceHostNames").toString().split(","));
            }
            params.put("active", StaticKeys.ON_STATE);
            List<DceInfo> dceInfoList = dceInfoService.selectAllByParams(params);
            //将server-backup节点处理的PING设备ID,标记起来,server节点就不监控这些PING设备了
            ServerBackupUtil.cacheSaveDceInfoId(dceInfoList);
            return ResDataUtils.resetSuccessJson(dceInfoList);
        } catch (Exception e) {
            logger.error("agent获取数控设备PING监测信息错误", e);
            logInfoService.save("agent获取数控设备PING监测信息错误", e.toString(), StaticKeys.LOG_XTCZ);
            return ResDataUtils.resetErrorJson(e.toString());
        }
    }
 
    /**
     * 根据条件查询数控设备监测信息列表
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "list")
    public String dceInfoList(DceInfo DceInfo, Model model, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        try {
 
            StringBuffer url = new StringBuffer();
            String hostName = null;
            String heathStatus = null;
            if (!StringUtils.isEmpty(DceInfo.getHostname())) {
                hostName = DceInfo.getHostname();
                params.put("hostname", hostName.trim());
                url.append("&hostname=").append(hostName);
            }
            if (!StringUtils.isEmpty(DceInfo.getAccount())) {
                params.put("account", DceInfo.getAccount());
                url.append("&account=").append(DceInfo.getAccount());
            }
            if (!StringUtils.isEmpty(DceInfo.getGroupId())) {
                params.put("groupId", DceInfo.getGroupId());
                url.append("&groupId=").append(DceInfo.getGroupId());
            }
            if (!StringUtils.isEmpty(DceInfo.getOrderBy())) {
                params.put("orderBy", DceInfo.getOrderBy());
                params.put("orderType", DceInfo.getOrderType());
                url.append("&orderBy=").append(DceInfo.getOrderBy());
                url.append("&orderType=").append(DceInfo.getOrderType());
            }
            if (!StringUtils.isEmpty(request.getParameter("heathStatus"))) {
                heathStatus = request.getParameter("heathStatus");
                if (StaticKeys.HEATH_STATUS_200.equals(heathStatus)) {
                    params.put("resTimesGt", -1);
                }
                if (StaticKeys.HEATH_STATUS_500.equals(heathStatus)) {
                    params.put("resTimes", -1);
                }
                url.append("&heathStatus=").append(heathStatus);
            }
 
            //校验是否需要添加过滤用户查询条件
            HostUtil.addAccountquery(request, params);
 
            PageInfo<DceInfo> pageInfo = dceInfoService.selectByParams(params, DceInfo.getPage(), DceInfo.getPageSize());
            //设置累积告警次数 begin
            if (StaticKeys.TRUE_VAL.equals(commonConfig.getShowWarnCount())) {
                Map<String, Object> paramsLogInfo = new HashMap<String, Object>();
                for (DceInfo dceInfo : pageInfo.getList()) {
                    String warnQueryWd = "数通设备PING超时告警:" + dceInfo.getHostname();
                    paramsLogInfo.clear();
                    paramsLogInfo.put("hostname", warnQueryWd);
                    dceInfo.setWarnCount(logInfoService.countByParams(paramsLogInfo));
                    dceInfo.setWarnQueryWd(warnQueryWd);
                }
            }
            //设置累积告警次数 end
 
            //设置分组
            List<HostGroup> hostGroupList = new ArrayList<>();
            if (StaticKeys.TRUE_VAL.equals(commonConfig.getHostGroup())) {
                hostGroupList = dceInfoService.setGroupInList(pageInfo.getList());
            }
 
            //设置用户列表
            HostUtil.addAccountListModel(model);
 
            PageUtil.initPageNumber(pageInfo, model);
 
            model.addAttribute("pageUrl", "/dceInfo/list?1=1" + url.toString());
            model.addAttribute("page", pageInfo);
            model.addAttribute("dceInfo", DceInfo);
            model.addAttribute("hostGroupList", hostGroupList);
        } catch (Exception e) {
            logger.error("查询数通设备PING监测错误", e);
            logInfoService.save("查询数通设备PING监测错误", e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "dce/list";
    }
 
    /**
     * 保存主机分组信息
     *
     * @param SystemInfo
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "saveGroupId")
    public String saveGroupId(Model model, HttpServletRequest request) {
        try {
            String ids = request.getParameter("ids");
            String groupId = request.getParameter("groupId");
            HostGroup hostGroup = hostGroupService.selectById(groupId);
            if (!StringUtils.isEmpty(ids)) {
                DceInfo ho = null;
                for (String id : ids.split(",")) {
                    ho = dceInfoService.selectById(id);
                    ho.setGroupId(groupId);
                    dceInfoService.updateById(ho);
                    logInfoService.save(HostUtil.getAccountByRequest(request).getAccount() + "设置PING分组:" + ho.getHostname(),
                            "分组:" + hostGroup.getGroupName(), StaticKeys.LOG_XTCZ);
                }
            }
        } catch (Exception e) {
            logger.error("保存数通设备分组信息错误", e);
            logInfoService.save("保存数通设备分组信息错误", e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "redirect:/dceInfo/list";
    }
 
    /**
     * 保存数通设备信息
     *
     * @param DceInfo
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "save")
    public String saveDceInfo(DceInfo dceInfo, Model model, HttpServletRequest request) {
        String errorMsg = "保存数通设备PING监测错误";
        try {
            if (StringUtils.isEmpty(dceInfo.getId())) {
                AccountInfo accountInfo = HostUtil.getAccountByRequest(request);
                if (null != accountInfo) {
                    if (!StaticKeys.ROLE_ADMIN.equals(accountInfo.getRole())) {
                        dceInfo.setAccount(accountInfo.getAccount());
                    }
                }
                dceInfoService.save(dceInfo, request);
                dceInfoService.saveLog(request, StaticKeys.LOG_ADD, dceInfo);
            } else {
                dceInfoService.updateById(dceInfo);
                dceInfoService.saveLog(request, StaticKeys.LOG_UPDATE, dceInfo);
            }
 
        } catch (Exception e) {
            logger.error(errorMsg, e);
            logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "redirect:/dceInfo/list";
    }
 
    /**
     * 将监控主机导入到数通设备列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "importHosts")
    public String importHosts(HttpServletRequest request) {
        try {
            Map<String, Object> map = new HashMap<>();
 
            //校验是否需要添加过滤用户查询条件
            HostUtil.addAccountquery(request, map);
 
            List<SystemInfo> systemInfoList = systemInfoService.selectAllByParams(map);
            List<DceInfo> dceInfoQueryList = dceInfoService.selectAllByParams(map);
            List<DceInfo> dceInfoSaveList = new ArrayList<>();
            //标识是否有ip重名,重名则跳过
            boolean sign = false;
            for (SystemInfo systemInfo : systemInfoList) {
                DceInfo dceInfo = new DceInfo();
                dceInfo.setHostname(systemInfo.getHostname());
                sign = false;
                for (DceInfo d : dceInfoQueryList) {
                    if (d.getHostname().equals(systemInfo.getHostname())) {
                        sign = true;
                        break;
                    }
                }
                if (sign) {
                    continue;
                }
                dceInfo.setActive(StaticKeys.ON_STATE);
                dceInfo.setRemark(systemInfo.getRemark());
 
                AccountInfo accountInfo = HostUtil.getAccountByRequest(request);
                if (null != accountInfo) {
                    if (!StaticKeys.ROLE_ADMIN.equals(accountInfo.getRole())) {
                        dceInfo.setAccount(accountInfo.getAccount());
                    }
                }
 
                dceInfoSaveList.add(dceInfo);
            }
            dceInfoService.saveRecord(dceInfoSaveList);
            dceInfoService.saveLog(request, "将监控主机导入", new DceInfo());
        } catch (Exception e) {
            logger.error("将监控主机导入到数通设备PING列表错误", e);
            logInfoService.save("将监控主机导入到数通设备PING列表错误", e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "sucess";
    }
 
 
    /**
     * 查看该数通设备
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "edit")
    public String edit(Model model, HttpServletRequest request) {
        String errorMsg = "编辑数通设备PING监测";
        String id = request.getParameter("id");
        DceInfo dceInfo = new DceInfo();
        try {
            if (StringUtils.isEmpty(id)) {
                model.addAttribute("dceInfo", dceInfo);
 
                return "dce/add";
            }
 
            dceInfo = dceInfoService.selectById(id);
            model.addAttribute("dceInfo", dceInfo);
        } catch (Exception e) {
            logger.error(errorMsg, e);
            logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "dce/add";
    }
 
    /**
     * 查看该数通设备
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "view")
    public String view(Model model, HttpServletRequest request) {
        String errorMsg = "查看数通设备PING图表错误";
        String id = request.getParameter("id");
        String startTime = request.getParameter(StaticKeys.SEARCH_START_TIME);
        String endTime = request.getParameter(StaticKeys.SEARCH_END_TIME);
        String am = request.getParameter("am");
        DceInfo dceInfo = new DceInfo();
        try {
            dceInfo = dceInfoService.selectById(id);
            Map<String, Object> params = new HashMap<String, Object>();
            model.addAttribute("dceInfo", dceInfo);
            dashboardService.setDateParam(am, startTime, endTime, params, model);
            model.addAttribute("amList", dashboardService.getAmList());
            params.put("dceId", dceInfo.getId());
            List<DceState> dceStateList = dceStateService.selectAllByParams(params);
            //设置图表的副标题,响应时间的最高、平均、最低值
            dceStateService.setSubtitle(model, dceStateList);
            model.addAttribute("dceStateList", JSONUtil.parseArray(dceStateList));
        } catch (Exception e) {
            logger.error(errorMsg, e);
            logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "dce/view";
    }
 
    /**
     * 查看该ping设备统计图导出excel
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "chartExcel")
    public void chartExcel(Model model, HttpServletRequest request, HttpServletResponse response) {
        String errorMsg = "ping设备统计图导出excel错误";
        String id = request.getParameter("id");
        String startTime = request.getParameter(StaticKeys.SEARCH_START_TIME);
        String endTime = request.getParameter(StaticKeys.SEARCH_END_TIME);
        String am = request.getParameter("am");
        try {
            if (StringUtils.isEmpty(id)) {
                response.setContentType("text/html;charset=UTF-8");
                response.getOutputStream().write(StaticKeys.EXCEL_PARAM_ERROR.getBytes());
                return;
            }
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("dceId", id);
            dashboardService.setDateParam(am, startTime, endTime, params, model);
            excelExportService.exportDceExcel(params, response);
        } catch (Exception e) {
            logger.error(errorMsg, e);
            logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
        }
    }
 
    /**
     * 删除数通设备
     *
     * @param model
     * @param request
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "del")
    public String delete(Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        String errorMsg = "删除数通设备PING监测错误";
        DceInfo dceInfo = new DceInfo();
        try {
            if (!StringUtils.isEmpty(request.getParameter("id"))) {
                String[] ids = request.getParameter("id").split(",");
                for (String id : ids) {
                    dceInfo = dceInfoService.selectById(id);
                    dceInfoService.saveLog(request, StaticKeys.LOG_DEL, dceInfo);
                }
                dceInfoService.deleteById(ids);
            }
        } catch (Exception e) {
            logger.error(errorMsg, e);
            logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
        }
        return "redirect:/dceInfo/list";
    }
 
}