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.AccountInfo;
|
import com.wgcloud.entity.HeathMonitor;
|
import com.wgcloud.entity.HeathState;
|
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.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @version v3.3
|
* @ClassName:HeathMonitorController.java
|
* @author: http://www.wgstart.com
|
* @date: 2021年1月16日
|
* @Description: 服务接口监控
|
* @Copyright: 2019-2021 wgcloud. All rights reserved.
|
*/
|
@Controller
|
@RequestMapping("/heathMonitor")
|
public class HeathMonitorController {
|
|
|
private static final Logger logger = LoggerFactory.getLogger(HeathMonitorController.class);
|
|
@Resource
|
private HeathMonitorService heathMonitorService;
|
@Resource
|
private HeathStateService heathStateService;
|
@Resource
|
private LogInfoService logInfoService;
|
@Resource
|
private DashboardService dashboardService;
|
@Resource
|
private AccountInfoService accountInfoService;
|
@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("heathNames") && !StringUtils.isEmpty(agentJsonObject.get("heathNames").toString())) {
|
params.put("heathNames", agentJsonObject.get("heathNames").toString().split(","));
|
}
|
params.put("active", StaticKeys.ON_STATE);
|
List<HeathMonitor> heathMonitorList = heathMonitorService.selectAllByParams(params);
|
// 将server-backup节点处理的的服务接口ID,暂存起来,server节点就不监控这些的服务接口了
|
ServerBackupUtil.cacheSaveHeathMontiorId(heathMonitorList);
|
return ResDataUtils.resetSuccessJson(heathMonitorList);
|
} catch (Exception e) {
|
logger.error("agent获取监控服务接口信息错误", e);
|
logInfoService.save("agent获取监控服务接口信息错误", e.toString(), StaticKeys.LOG_XTCZ);
|
return ResDataUtils.resetErrorJson(e.toString());
|
}
|
}
|
|
/**
|
* 根据条件查询心跳监控列表
|
*
|
* @param model
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value = "list")
|
public String heathMonitorList(HeathMonitor HeathMonitor, Model model, HttpServletRequest request) {
|
Map<String, Object> params = new HashMap<String, Object>();
|
try {
|
|
StringBuffer url = new StringBuffer();
|
String appName = null;
|
String heathStatus = null;
|
if (!StringUtils.isEmpty(HeathMonitor.getAppName())) {
|
appName = HeathMonitor.getAppName();
|
params.put("appName", appName.trim());
|
url.append("&appName=").append(appName);
|
}
|
if (!StringUtils.isEmpty(HeathMonitor.getAccount())) {
|
params.put("account", HeathMonitor.getAccount());
|
url.append("&account=").append(HeathMonitor.getAccount());
|
}
|
if (!StringUtils.isEmpty(HeathMonitor.getHeathStatus())) {
|
heathStatus = HeathMonitor.getHeathStatus();
|
params.put("heathStatus", heathStatus.trim());
|
url.append("&heathStatus=").append(heathStatus);
|
}
|
if (!StringUtils.isEmpty(HeathMonitor.getOrderBy())) {
|
params.put("orderBy", HeathMonitor.getOrderBy());
|
params.put("orderType", HeathMonitor.getOrderType());
|
url.append("&orderBy=").append(HeathMonitor.getOrderBy());
|
url.append("&orderType=").append(HeathMonitor.getOrderType());
|
}
|
|
//校验是否需要添加过滤用户查询条件
|
HostUtil.addAccountquery(request, params);
|
|
PageInfo<HeathMonitor> pageInfo = heathMonitorService.selectByParams(params, HeathMonitor.getPage(), HeathMonitor.getPageSize());
|
Map<String, Object> paramsLogInfo = new HashMap<String, Object>();
|
for (HeathMonitor heathMonitor : pageInfo.getList()) {
|
if (StringUtils.isEmpty(heathMonitor.getResKeyword())) {
|
heathMonitor.setResKeyword("");
|
}
|
//设置累积告警次数 begin
|
if (StaticKeys.TRUE_VAL.equals(commonConfig.getShowWarnCount())) {
|
String warnQueryWd = "服务接口检测告警:" + heathMonitor.getAppName();
|
paramsLogInfo.clear();
|
paramsLogInfo.put("hostname", warnQueryWd);
|
heathMonitor.setWarnCount(logInfoService.countByParams(paramsLogInfo));
|
heathMonitor.setWarnQueryWd(warnQueryWd);
|
}
|
//设置累积告警次数 end
|
}
|
|
PageUtil.initPageNumber(pageInfo, model);
|
|
//设置用户列表
|
HostUtil.addAccountListModel(model);
|
|
model.addAttribute("pageUrl", "/heathMonitor/list?1=1" + url.toString());
|
model.addAttribute("page", pageInfo);
|
model.addAttribute("heathMonitor", HeathMonitor);
|
} catch (Exception e) {
|
logger.error("查询服务接口监控错误", e);
|
logInfoService.save("查询服务接口监控错误", e.toString(), StaticKeys.LOG_XTCZ);
|
|
}
|
return "heath/list";
|
}
|
|
|
/**
|
* 保存心跳监控信息
|
*
|
* @param HeathMonitor
|
* @param model
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value = "save")
|
public String saveHeathMonitor(HeathMonitor HeathMonitor, Model model, HttpServletRequest request) {
|
try {
|
if (StringUtils.isEmpty(HeathMonitor.getId())) {
|
AccountInfo accountInfo = HostUtil.getAccountByRequest(request);
|
if (null != accountInfo) {
|
if (!StaticKeys.ROLE_ADMIN.equals(accountInfo.getRole())) {
|
HeathMonitor.setAccount(accountInfo.getAccount());
|
}
|
}
|
heathMonitorService.save(HeathMonitor);
|
heathMonitorService.saveLog(request, StaticKeys.LOG_ADD, HeathMonitor);
|
} else {
|
heathMonitorService.updateById(HeathMonitor);
|
heathMonitorService.saveLog(request, StaticKeys.LOG_UPDATE, HeathMonitor);
|
}
|
|
} catch (Exception e) {
|
logger.error("保存服务心跳监控错误", e);
|
logInfoService.save("保存服务心跳监控错误", e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
return "redirect:/heathMonitor/list";
|
}
|
|
|
/**
|
* 查看该心跳监控
|
*
|
* @param HeathMonitor
|
* @param model
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value = "edit")
|
public String edit(Model model, HttpServletRequest request) {
|
String errorMsg = "编辑服务接口监控";
|
String id = request.getParameter("id");
|
HeathMonitor heathMonitor = new HeathMonitor();
|
try {
|
if (StringUtils.isEmpty(id)) {
|
model.addAttribute("heathMonitor", heathMonitor);
|
|
return "heath/add";
|
}
|
|
|
heathMonitor = heathMonitorService.selectById(id);
|
//如果是post方式,处理header键值对页面回显
|
heathMonitorService.displayHeaderJson(heathMonitor);
|
model.addAttribute("heathMonitor", heathMonitor);
|
} catch (Exception e) {
|
logger.error(errorMsg, e);
|
logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
return "heath/add";
|
}
|
|
/**
|
* 查看该心跳监控
|
*
|
* @param HeathMonitor
|
* @param model
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value = "view")
|
public String view(Model model, HttpServletRequest request) {
|
String errorMsg = "查看服务接口统计图表错误";
|
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");
|
HeathMonitor heathMonitor = new HeathMonitor();
|
try {
|
heathMonitor = heathMonitorService.selectById(id);
|
Map<String, Object> params = new HashMap<String, Object>();
|
model.addAttribute("heathMonitor", heathMonitor);
|
dashboardService.setDateParam(am, startTime, endTime, params, model);
|
model.addAttribute("amList", dashboardService.getAmList());
|
params.put("heathId", heathMonitor.getId());
|
List<HeathState> heathStateList = heathStateService.selectAllByParams(params);
|
//设置图表的副标题,响应时间的最高、平均、最低值
|
heathStateService.setSubtitle(model, heathStateList);
|
model.addAttribute("heathStateList", JSONUtil.parseArray(heathStateList));
|
} catch (Exception e) {
|
logger.error(errorMsg, e);
|
logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
return "heath/view";
|
}
|
|
/**
|
* 查看该服务接口统计图导出excel
|
*
|
* @param model
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value = "chartExcel")
|
public void chartExcel(Model model, HttpServletRequest request, HttpServletResponse response) {
|
String errorMsg = "服务接口统计图导出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("heathId", id);
|
dashboardService.setDateParam(am, startTime, endTime, params, model);
|
excelExportService.exportHeathExcel(params, response);
|
} catch (Exception e) {
|
logger.error(errorMsg, e);
|
logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
}
|
|
|
/**
|
* 删除心跳监控
|
*
|
* @param id
|
* @param model
|
* @param request
|
* @param redirectAttributes
|
* @return
|
*/
|
@RequestMapping(value = "del")
|
public String delete(Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
|
String errorMsg = "删除服务接口监控错误";
|
HeathMonitor HeathMonitor = new HeathMonitor();
|
try {
|
if (!StringUtils.isEmpty(request.getParameter("id"))) {
|
String[] ids = request.getParameter("id").split(",");
|
for (String id : ids) {
|
HeathMonitor = heathMonitorService.selectById(id);
|
heathMonitorService.saveLog(request, StaticKeys.LOG_DEL, HeathMonitor);
|
}
|
heathMonitorService.deleteById(ids);
|
}
|
} catch (Exception e) {
|
logger.error(errorMsg, e);
|
logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
|
return "redirect:/heathMonitor/list";
|
}
|
|
|
}
|