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:AppInfoController.java * @author: http://www.wgstart.com * @date: 2021年1月16日 * @Description: 进程应用管理 * @Copyright: 2019-2021 wgcloud. All rights reserved. */ @Controller @RequestMapping("/appInfo") public class AppInfoController { private static final Logger logger = LoggerFactory.getLogger(AppInfoController.class); @Resource private AppInfoService appInfoService; @Resource private AppStateService appStateService; @Resource private LogInfoService logInfoService; @Resource private SystemInfoService systemInfoService; @Resource private DashboardService dashboardService; @Resource private ExcelExportService excelExportService; @Resource private HostGroupService hostGroupService; @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 params = new HashMap(); if (null == agentJsonObject.get("hostname") || StringUtils.isEmpty(agentJsonObject.get("hostname").toString())) { return ""; } params.put("hostname", agentJsonObject.get("hostname").toString()); try { params.put("active", StaticKeys.ON_STATE); List appInfoList = appInfoService.selectAllByParams(params); return ResDataUtils.resetSuccessJson(appInfoList); } 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 appInfoList(AppInfo appInfo, Model model, HttpServletRequest request) { Map params = new HashMap(); try { StringBuffer url = new StringBuffer(); String hostname = null; if (!StringUtils.isEmpty(appInfo.getHostname())) { hostname = appInfo.getHostname(); params.put("hostname", hostname.trim()); url.append("&hostname=").append(hostname); } if (!StringUtils.isEmpty(appInfo.getAccount())) { params.put("account", appInfo.getAccount()); url.append("&account=").append(appInfo.getAccount()); } if (!StringUtils.isEmpty(appInfo.getGroupId())) { params.put("groupId", appInfo.getGroupId()); url.append("&groupId=").append(appInfo.getGroupId()); } if (!StringUtils.isEmpty(appInfo.getOrderBy())) { params.put("orderBy", appInfo.getOrderBy()); params.put("orderType", appInfo.getOrderType()); url.append("&orderBy=").append(appInfo.getOrderBy()); url.append("&orderType=").append(appInfo.getOrderType()); } if (!StringUtils.isEmpty(appInfo.getState())) { params.put("state", appInfo.getState()); url.append("&state=").append(appInfo.getState()); } //校验是否需要添加过滤用户查询条件 HostUtil.addAccountquery(request, params); PageInfo pageInfo = appInfoService.selectByParams(params, appInfo.getPage(), appInfo.getPageSize()); Map paramsLogInfo = new HashMap(); for (AppInfo appInfo1 : pageInfo.getList()) { appInfo1.setWritesBytes(FormatUtil.mToG(appInfo1.getWritesBytes())); appInfo1.setReadBytes(FormatUtil.mToG(appInfo1.getReadBytes())); appInfo1.setHostname(appInfo1.getHostname() + HostUtil.addRemark(appInfo1.getHostname())); //设置所属用户账号 begin if (StaticKeys.TRUE_VAL.equals(commonConfig.getUserInfoManage())) { appInfo1.setAccount(HostUtil.getAccount(appInfo1.getHostname())); } //设置所属用户账号 begin //设置累积告警次数 begin if (StaticKeys.TRUE_VAL.equals(commonConfig.getShowWarnCount())) { String warnQueryWd = appInfo1.getAppName() + "," + appInfo1.getHostname(); paramsLogInfo.clear(); paramsLogInfo.put("hostname", warnQueryWd); paramsLogInfo.put("hostnameNe", "已恢复"); appInfo1.setWarnCount(logInfoService.countByParams(paramsLogInfo)); appInfo1.setWarnQueryWd(warnQueryWd); } //设置累积告警次数 end } //设置分组 List hostGroupList = new ArrayList<>(); if (StaticKeys.TRUE_VAL.equals(commonConfig.getHostGroup())) { hostGroupList = appInfoService.setGroupInList(pageInfo.getList()); } //设置用户列表 HostUtil.addAccountListModel(model); PageUtil.initPageNumber(pageInfo, model); model.addAttribute("pageUrl", "/appInfo/list?1=1" + url.toString()); model.addAttribute("page", pageInfo); model.addAttribute("appInfo", appInfo); model.addAttribute("hostGroupList", hostGroupList); } catch (Exception e) { logger.error("查询进程信息错误", e); logInfoService.save("查询进程信息错误", e.toString(), StaticKeys.LOG_XTCZ); } return "app/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)) { AppInfo ho = null; for (String id : ids.split(",")) { ho = appInfoService.selectById(id); ho.setGroupId(groupId); appInfoService.updateById(ho); logInfoService.save(HostUtil.getAccountByRequest(request).getAccount() + "设置进程分组:" + ho.getHostname() + "," + ho.getAppName(), "分组:" + hostGroup.getGroupName(), StaticKeys.LOG_XTCZ); } } } catch (Exception e) { logger.error("保存进程分组信息错误", e); logInfoService.save("保存进程分组信息错误", e.toString(), StaticKeys.LOG_XTCZ); } return "redirect:/appInfo/list"; } /** * 保存应用监控信息 * * @param AppInfo * @param model * @param request * @return */ @RequestMapping(value = "save") public String saveAppInfo(AppInfo AppInfo, Model model, HttpServletRequest request) { String errorMsg = "保存进程错误"; try { if (StringUtils.isEmpty(AppInfo.getId())) { appInfoService.save(AppInfo, request); appInfoService.saveLog(request, StaticKeys.LOG_ADD, AppInfo); } else { appInfoService.updateById(AppInfo); appInfoService.saveLog(request, StaticKeys.LOG_UPDATE, AppInfo); } } catch (Exception e) { logger.error(errorMsg, e); logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ); } return "redirect:/appInfo/list"; } /** * 批量保存应用监控信息 * * @param AppInfo * @param model * @param request * @return */ @RequestMapping(value = "saveBatch") public String saveBatchAppInfo(AppInfo AppInfo, Model model, HttpServletRequest request) { String errorMsg = "批量保存进程错误"; try { String[] hostnames = request.getParameterValues("hostnames"); if (null == hostnames || hostnames.length < 1) { return "redirect:/appInfo/list"; } for (String selectedHost : hostnames) { AppInfo.setHostname(selectedHost); appInfoService.save(AppInfo, request); appInfoService.saveLog(request, StaticKeys.LOG_ADD, AppInfo); } } catch (Exception e) { logger.error(errorMsg, e); logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ); } return "redirect:/appInfo/list"; } /** * 添加 * * @param model * @param request * @return */ @RequestMapping(value = "edit") public String edit(Model model, HttpServletRequest request) { String errorMsg = "添加进程错误"; String id = request.getParameter("id"); AppInfo appInfo = new AppInfo(); try { //校验是否需要添加过滤用户查询条件 Map params = new HashMap(); HostUtil.addAccountquery(request, params); List systemInfoList = systemInfoService.selectAllByParams(params); model.addAttribute("systemInfoList", systemInfoList); if (StringUtils.isEmpty(id)) { model.addAttribute("appInfo", appInfo); return "app/add"; } appInfo = appInfoService.selectById(id); model.addAttribute("appInfo", appInfo); } catch (Exception e) { logger.error(errorMsg, e); logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ); } return "app/add"; } /** * 批量添加 * * @param model * @param request * @return */ @RequestMapping(value = "editBatch") public String editBatch(Model model, HttpServletRequest request) { String errorMsg = "批量添加进程错误"; AppInfo appInfo = new AppInfo(); try { //校验是否需要添加过滤用户查询条件 Map params = new HashMap(); HostUtil.addAccountquery(request, params); List systemInfoList = systemInfoService.selectAllByParams(params); model.addAttribute("systemInfoList", systemInfoList); model.addAttribute("appInfo", appInfo); } catch (Exception e) { logger.error(errorMsg, e); logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ); } return "app/addBatch"; } /** * 查看该应用统计图 * * @param model * @param request * @return */ @RequestMapping(value = "view") public String viewChart(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"); AppInfo appInfo = new AppInfo(); try { appInfo = appInfoService.selectById(id); //提取主机备注 begin appInfo.setHostname(appInfo.getHostname() + HostUtil.addRemark(appInfo.getHostname())); //提取主机备注 end Map params = new HashMap(); dashboardService.setDateParam(am, startTime, endTime, params, model); model.addAttribute("amList", dashboardService.getAmList()); params.put("appInfoId", appInfo.getId()); model.addAttribute("appInfo", appInfo); List appStateList = appStateService.selectAllByParams(params); // 设置图表的副标题,cpu、内存、线程数的最高、平均、最低值 appStateService.setSubtitle(model, appStateList); model.addAttribute("appStateList", JSONUtil.parseArray(appStateList)); } catch (Exception e) { logger.error(errorMsg, e); logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ); } return "app/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 params = new HashMap(); params.put("appInfoId", id); dashboardService.setDateParam(am, startTime, endTime, params, model); excelExportService.exportAppExcel(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 = "删除进程信息错误"; AppInfo appInfo = new AppInfo(); try { if (!StringUtils.isEmpty(request.getParameter("id"))) { String[] ids = request.getParameter("id").split(","); for (String id : ids) { appInfo = appInfoService.selectById(id); appInfoService.saveLog(request, StaticKeys.LOG_DEL, appInfo); } appInfoService.deleteById(request.getParameter("id").split(",")); } } catch (Exception e) { logger.error(errorMsg, e); logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ); } return "redirect:/appInfo/list"; } }