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.CustomInfo;
|
import com.wgcloud.entity.CustomState;
|
import com.wgcloud.entity.HostGroup;
|
import com.wgcloud.entity.SystemInfo;
|
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:CustomInfoController.java
|
* @author: http://www.wgstart.com
|
* @date: 2022年9月16日
|
* @Description: 自定义监控项管理
|
* @Copyright: 2019-2022 wgcloud. All rights reserved.
|
*/
|
@Controller
|
@RequestMapping("/customInfo")
|
public class CustomInfoController {
|
|
|
private static final Logger logger = LoggerFactory.getLogger(CustomInfoController.class);
|
|
@Resource
|
private CustomInfoService customInfoService;
|
|
@Resource
|
private CustomStateService customStateService;
|
|
@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查询自定义监控项列表
|
*
|
* @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>();
|
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<CustomInfo> customInfoList = customInfoService.selectAllByParams(params);
|
//检测系统类型,检查是否含有敏感字符 begin
|
SystemInfo systemInfo = systemInfoService.selectByHostname(agentJsonObject.get("hostname").toString());
|
String shellToRunBlock = commonConfig.getShellToRunLinuxBlock();
|
if (systemInfo != null) {
|
String platForm = systemInfo.getPlatForm().toLowerCase();
|
if (platForm.contains("windows")) {
|
shellToRunBlock = commonConfig.getShellToRunWinBlock();
|
}
|
}
|
|
String blockKey = "";
|
List<CustomInfo> customInfoListResult = new ArrayList<>();
|
for (CustomInfo state : customInfoList) {
|
//检查是否含有敏感字符
|
blockKey = FormatUtil.haveBlockDanger(state.getCustomShell(), shellToRunBlock, "");
|
if (!StringUtils.isEmpty(blockKey)) {
|
logger.error(state.getCustomShell() + "自定义监控项指令含有敏感字符" + blockKey + ",不进行下发");
|
continue;
|
}
|
customInfoListResult.add(state);
|
}
|
//检测系统类型,检查是否含有敏感字符 end
|
return ResDataUtils.resetSuccessJson(customInfoListResult);
|
} 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 customInfoList(CustomInfo customInfo, Model model, HttpServletRequest request) {
|
Map<String, Object> params = new HashMap<String, Object>();
|
try {
|
|
StringBuffer url = new StringBuffer();
|
String hostname = null;
|
if (!StringUtils.isEmpty(customInfo.getHostname())) {
|
hostname = customInfo.getHostname();
|
params.put("hostname", hostname.trim());
|
url.append("&hostname=").append(hostname);
|
}
|
if (!StringUtils.isEmpty(customInfo.getAccount())) {
|
params.put("account", customInfo.getAccount());
|
url.append("&account=").append(customInfo.getAccount());
|
}
|
if (!StringUtils.isEmpty(customInfo.getGroupId())) {
|
params.put("groupId", customInfo.getGroupId());
|
url.append("&groupId=").append(customInfo.getGroupId());
|
}
|
if (!StringUtils.isEmpty(customInfo.getState())) {
|
params.put("state", customInfo.getState());
|
url.append("&state=").append(customInfo.getState());
|
}
|
|
//校验是否需要添加过滤用户查询条件
|
HostUtil.addAccountquery(request, params);
|
|
PageInfo<CustomInfo> pageInfo = customInfoService.selectByParams(params, customInfo.getPage(), customInfo.getPageSize());
|
Map<String, Object> paramsLogInfo = new HashMap<String, Object>();
|
for (CustomInfo customInfo1 : pageInfo.getList()) {
|
//设置所属用户账号 begin
|
if (StaticKeys.TRUE_VAL.equals(commonConfig.getUserInfoManage())) {
|
customInfo1.setAccount(HostUtil.getAccount(customInfo1.getHostname()));
|
}
|
//设置所属用户账号 begin
|
|
customInfo1.setHostname(customInfo1.getHostname() + HostUtil.addRemark(customInfo1.getHostname()));
|
|
//设置累积告警次数 begin
|
if (StaticKeys.TRUE_VAL.equals(commonConfig.getShowWarnCount())) {
|
String warnQueryWd = customInfo1.getCustomName() + "," + customInfo1.getHostname();
|
paramsLogInfo.clear();
|
paramsLogInfo.put("hostname", warnQueryWd);
|
paramsLogInfo.put("hostnameNe", "已恢复");
|
customInfo1.setWarnCount(logInfoService.countByParams(paramsLogInfo));
|
customInfo1.setWarnQueryWd(warnQueryWd);
|
}
|
//设置累积告警次数 end
|
|
}
|
//设置分组
|
List<HostGroup> hostGroupList = new ArrayList<>();
|
if (StaticKeys.TRUE_VAL.equals(commonConfig.getHostGroup())) {
|
hostGroupList = customInfoService.setGroupInList(pageInfo.getList());
|
}
|
|
//设置用户列表
|
HostUtil.addAccountListModel(model);
|
|
PageUtil.initPageNumber(pageInfo, model);
|
model.addAttribute("pageUrl", "/customInfo/list?1=1" + url.toString());
|
model.addAttribute("page", pageInfo);
|
model.addAttribute("customInfo", customInfo);
|
model.addAttribute("hostGroupList", hostGroupList);
|
} catch (Exception e) {
|
logger.error("查询自定义监控项信息错误", e);
|
logInfoService.save("查询自定义监控项信息错误", e.toString(), StaticKeys.LOG_XTCZ);
|
|
}
|
return "customInfo/list";
|
}
|
|
/**
|
* 保存主机分组信息
|
*
|
* @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)) {
|
CustomInfo ho = null;
|
for (String id : ids.split(",")) {
|
ho = customInfoService.selectById(id);
|
ho.setGroupId(groupId);
|
customInfoService.updateById(ho);
|
logInfoService.save(HostUtil.getAccountByRequest(request).getAccount() + "设置自定义监控项分组:" + ho.getHostname() + "," + ho.getCustomName(),
|
"分组:" + hostGroup.getGroupName(), StaticKeys.LOG_XTCZ);
|
}
|
}
|
} catch (Exception e) {
|
logger.error("保存自定义监控项分组信息错误", e);
|
logInfoService.save("保存自定义监控项分组信息错误", e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
return "redirect:/customInfo/list";
|
}
|
|
/**
|
* 保存应用监控信息
|
*
|
* @param model
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value = "save")
|
public String saveCustomInfo(CustomInfo customInfo, Model model, HttpServletRequest request) {
|
String errorMsg = "保存自定义监控项错误";
|
try {
|
//检测指令有无敏感字符 begin
|
String blockKey = FormatUtil.haveBlockDanger(customInfo.getCustomShell(), commonConfig.getShellToRunLinuxBlock(), commonConfig.getShellToRunWinBlock());
|
if (!StringUtils.isEmpty(blockKey)) {
|
model.addAttribute("customInfo", customInfo);
|
model.addAttribute("msg", "下发指令含有敏感字符" + blockKey + ",请检查");
|
//敏感字符组装
|
getBlockStr(model);
|
//校验是否需要添加过滤用户查询条件
|
Map<String, Object> params = new HashMap<String, Object>();
|
HostUtil.addAccountquery(request, params);
|
List<SystemInfo> systemInfoList = systemInfoService.selectAllByParams(params);
|
model.addAttribute("systemInfoList", systemInfoList);
|
return "customInfo/add";
|
}
|
//检测指令有无敏感字符 end
|
|
if (StringUtils.isEmpty(customInfo.getId())) {
|
customInfoService.save(customInfo, request);
|
customInfoService.saveLog(request, StaticKeys.LOG_ADD, customInfo);
|
} else {
|
customInfoService.updateById(customInfo);
|
customInfoService.saveLog(request, StaticKeys.LOG_UPDATE, customInfo);
|
}
|
} catch (Exception e) {
|
logger.error(errorMsg, e);
|
logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
return "redirect:/customInfo/list";
|
}
|
|
/**
|
* 添加
|
*
|
* @param model
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value = "edit")
|
public String edit(Model model, HttpServletRequest request) {
|
String errorMsg = "添加自定义监控项错误";
|
String id = request.getParameter("id");
|
CustomInfo customInfo = new CustomInfo();
|
try {
|
//校验是否需要添加过滤用户查询条件
|
Map<String, Object> params = new HashMap<String, Object>();
|
HostUtil.addAccountquery(request, params);
|
|
//敏感字符组装
|
getBlockStr(model);
|
|
List<SystemInfo> systemInfoList = systemInfoService.selectAllByParams(params);
|
model.addAttribute("systemInfoList", systemInfoList);
|
if (StringUtils.isEmpty(id)) {
|
model.addAttribute("customInfo", customInfo);
|
|
return "customInfo/add";
|
}
|
customInfo = customInfoService.selectById(id);
|
model.addAttribute("customInfo", customInfo);
|
} catch (Exception e) {
|
logger.error(errorMsg, e);
|
logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
return "customInfo/add";
|
}
|
|
|
/**
|
* 查看该监控项统计图
|
*
|
* @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");
|
CustomInfo customInfo = new CustomInfo();
|
try {
|
customInfo = customInfoService.selectById(id);
|
//提取主机备注 begin
|
customInfo.setHostname(customInfo.getHostname() + HostUtil.addRemark(customInfo.getHostname()));
|
//提取主机备注 end
|
Map<String, Object> params = new HashMap<String, Object>();
|
dashboardService.setDateParam(am, startTime, endTime, params, model);
|
model.addAttribute("amList", dashboardService.getAmList());
|
params.put("customInfoId", customInfo.getId());
|
model.addAttribute("customInfo", customInfo);
|
List<CustomState> customStateList = customStateService.selectAllByParams(params);
|
// 设置图表的副标题,最高、平均、最低值
|
customStateService.setSubtitle(model, customStateList);
|
model.addAttribute("customStateList", JSONUtil.parseArray(customStateList));
|
} catch (Exception e) {
|
logger.error(errorMsg, e);
|
logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
return "customInfo/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("customInfoId", id);
|
dashboardService.setDateParam(am, startTime, endTime, params, model);
|
excelExportService.exportCustomExcel(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 = "删除自定义监控项信息错误";
|
CustomInfo customInfo = new CustomInfo();
|
try {
|
if (!StringUtils.isEmpty(request.getParameter("id"))) {
|
String[] ids = request.getParameter("id").split(",");
|
for (String id : ids) {
|
customInfo = customInfoService.selectById(id);
|
customInfoService.saveLog(request, StaticKeys.LOG_DEL, customInfo);
|
}
|
customInfoService.deleteById(request.getParameter("id").split(","));
|
}
|
} catch (Exception e) {
|
logger.error(errorMsg, e);
|
logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
return "redirect:/customInfo/list";
|
}
|
|
/**
|
* 提取已配置的敏感字符串
|
*
|
* @return
|
*/
|
private void getBlockStr(Model model) {
|
//敏感字符组装 begin
|
String blockStr = "";
|
if (!StringUtils.isEmpty(commonConfig.getShellToRunLinuxBlock())) {
|
blockStr = "[linux不能包含敏感字符:" + commonConfig.getShellToRunLinuxBlock() + "]";
|
}
|
if (!StringUtils.isEmpty(commonConfig.getShellToRunLinuxBlock())) {
|
blockStr += "[windows不能包含敏感字符:" + commonConfig.getShellToRunWinBlock() + "]";
|
}
|
model.addAttribute("blockStr", blockStr);
|
//敏感字符组装 end
|
}
|
|
|
}
|