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.SnmpInfo;
|
import com.wgcloud.entity.SnmpState;
|
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.snmp4j.mp.SnmpConstants;
|
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:SnmpInfoController.java
|
* @author: http://www.wgstart.com
|
* @date: 2022年4月10日
|
* @Description: snmp设备监测
|
* @Copyright: 2019-2021 wgcloud. All rights reserved.
|
*/
|
@Controller
|
@RequestMapping("/snmpInfo")
|
public class SnmpInfoController {
|
|
|
private static final Logger logger = LoggerFactory.getLogger(SnmpInfoController.class);
|
|
@Resource
|
private SnmpInfoService snmpInfoService;
|
@Resource
|
private SnmpStateService snmpStateService;
|
@Resource
|
private LogInfoService logInfoService;
|
@Resource
|
private DashboardService dashboardService;
|
@Resource
|
private ExcelExportService excelExportService;
|
@Autowired
|
private TokenUtils tokenUtils;
|
@Autowired
|
private CommonConfig commonConfig;
|
|
/**
|
* agent查询监控snmp设备监测列表
|
*
|
* @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("snmpHostNames") && !StringUtils.isEmpty(agentJsonObject.get("snmpHostNames").toString())) {
|
params.put("snmpHostNames", agentJsonObject.get("snmpHostNames").toString().split(","));
|
}
|
params.put("active", StaticKeys.ON_STATE);
|
List<SnmpInfo> snmpInfoList = snmpInfoService.selectAllByParams(params);
|
//将server-backup节点处理的SNMP设备ID,标记起来,server节点就不监控这些SNMP设备了
|
ServerBackupUtil.cacheSaveSnmpInfoId(snmpInfoList);
|
return ResDataUtils.resetSuccessJson(snmpInfoList);
|
} catch (Exception e) {
|
logger.error("agent获取snmp设备监测信息错误", e);
|
logInfoService.save("agent获取snmp设备监测信息错误", e.toString(), StaticKeys.LOG_XTCZ);
|
return ResDataUtils.resetErrorJson(e.toString());
|
}
|
}
|
|
/**
|
* 根据条件查询snmp设备监测信息列表
|
*
|
* @param model
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value = "list")
|
public String snmpInfoList(SnmpInfo SnmpInfo, Model model, HttpServletRequest request) {
|
Map<String, Object> params = new HashMap<String, Object>();
|
try {
|
|
StringBuffer url = new StringBuffer();
|
String hostName = null;
|
if (!StringUtils.isEmpty(SnmpInfo.getHostname())) {
|
hostName = SnmpInfo.getHostname();
|
params.put("hostname", hostName.trim());
|
url.append("&hostname=").append(hostName);
|
}
|
if (!StringUtils.isEmpty(SnmpInfo.getAccount())) {
|
params.put("account", SnmpInfo.getAccount());
|
url.append("&account=").append(SnmpInfo.getAccount());
|
}
|
if (!StringUtils.isEmpty(SnmpInfo.getOrderBy())) {
|
params.put("orderBy", SnmpInfo.getOrderBy());
|
params.put("orderType", SnmpInfo.getOrderType());
|
url.append("&orderBy=").append(SnmpInfo.getOrderBy());
|
url.append("&orderType=").append(SnmpInfo.getOrderType());
|
}
|
if (!StringUtils.isEmpty(SnmpInfo.getState())) {
|
params.put("state", SnmpInfo.getState());
|
url.append("&state=").append(SnmpInfo.getState());
|
}
|
|
//校验是否需要添加过滤用户查询条件
|
HostUtil.addAccountquery(request, params);
|
|
PageInfo<SnmpInfo> pageInfo = snmpInfoService.selectByParams(params, SnmpInfo.getPage(), SnmpInfo.getPageSize());
|
//设置累积告警次数 begin
|
Map<String, Object> paramsLogInfo = new HashMap<String, Object>();
|
for (SnmpInfo snmpInfoTmp : pageInfo.getList()) {
|
if (StaticKeys.TRUE_VAL.equals(commonConfig.getShowWarnCount())) {
|
String warnQueryWd = "snmp设备监测告警:" + snmpInfoTmp.getHostname();
|
paramsLogInfo.clear();
|
paramsLogInfo.put("hostname", warnQueryWd);
|
snmpInfoTmp.setWarnCount(logInfoService.countByParams(paramsLogInfo));
|
snmpInfoTmp.setWarnQueryWd(warnQueryWd);
|
}
|
//格式化流量显示单位
|
snmpInfoTmp.setBytesRecv(FormatUtil.bytesFormatUnit(snmpInfoTmp.getBytesRecv(), snmpInfoTmp.getSnmpUnit()));
|
snmpInfoTmp.setBytesSent(FormatUtil.bytesFormatUnit(snmpInfoTmp.getBytesSent(), snmpInfoTmp.getSnmpUnit()));
|
snmpInfoTmp.setRecvAvg(FormatUtil.byteToM(snmpInfoTmp.getRecvAvg(), snmpInfoTmp.getSnmpUnit()) + "/s");
|
snmpInfoTmp.setSentAvg(FormatUtil.byteToM(snmpInfoTmp.getSentAvg(), snmpInfoTmp.getSnmpUnit()) + "/s");
|
if (StringUtils.isEmpty(snmpInfoTmp.getCpuPerOID())) {
|
snmpInfoTmp.setCpuPer(StaticKeys.NAN_FLAG);
|
}
|
if (StringUtils.isEmpty(snmpInfoTmp.getMemSizeOID()) || StringUtils.isEmpty(snmpInfoTmp.getMemTotalSizeOID())) {
|
snmpInfoTmp.setMemPer(StaticKeys.NAN_FLAG);
|
}
|
}
|
//设置累积告警次数 end
|
|
PageUtil.initPageNumber(pageInfo, model);
|
|
//设置用户列表
|
HostUtil.addAccountListModel(model);
|
|
model.addAttribute("pageUrl", "/snmpInfo/list?1=1" + url.toString());
|
model.addAttribute("page", pageInfo);
|
model.addAttribute("snmpInfo", SnmpInfo);
|
} catch (Exception e) {
|
logger.error("查询snmp设备监测错误", e);
|
logInfoService.save("查询snmp设备监测错误", e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
return "snmp/list";
|
}
|
|
|
/**
|
* 保存snmp设备信息
|
*
|
* @param SnmpInfo
|
* @param model
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value = "save")
|
public String saveSnmpInfo(SnmpInfo SnmpInfo, Model model, HttpServletRequest request) {
|
String errorMsg = "保存snmp设备监测错误";
|
try {
|
if (StringUtils.isEmpty(SnmpInfo.getId())) {
|
AccountInfo accountInfo = HostUtil.getAccountByRequest(request);
|
if (null != accountInfo) {
|
if (!StaticKeys.ROLE_ADMIN.equals(accountInfo.getRole())) {
|
SnmpInfo.setAccount(accountInfo.getAccount());
|
}
|
}
|
snmpInfoService.save(SnmpInfo);
|
snmpInfoService.saveLog(request, StaticKeys.LOG_ADD, SnmpInfo);
|
} else {
|
snmpInfoService.updateById(SnmpInfo);
|
snmpInfoService.saveLog(request, StaticKeys.LOG_UPDATE, SnmpInfo);
|
}
|
|
} catch (Exception e) {
|
logger.error(errorMsg, e);
|
logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
return "redirect:/snmpInfo/list";
|
}
|
|
|
/**
|
* 查看该snmp设备
|
*
|
* @param SnmpInfo
|
* @param model
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value = "edit")
|
public String edit(Model model, HttpServletRequest request) {
|
String errorMsg = "编辑snmp设备监测";
|
String id = request.getParameter("id");
|
SnmpInfo SnmpInfo = new SnmpInfo();
|
try {
|
if (StringUtils.isEmpty(id)) {
|
//添加页面默认值,snmp端口为161,团体名称为public
|
SnmpInfo.setSnmpPort("161");
|
SnmpInfo.setSnmpCommunity("public");
|
SnmpInfo.setSnmpVersion(SnmpConstants.version2c + "");
|
model.addAttribute("snmpInfo", SnmpInfo);
|
|
return "snmp/add";
|
}
|
|
SnmpInfo = snmpInfoService.selectById(id);
|
model.addAttribute("snmpInfo", SnmpInfo);
|
} catch (Exception e) {
|
logger.error(errorMsg, e);
|
logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
return "snmp/add";
|
}
|
|
/**
|
* 查看该snmp设备
|
*
|
* @param SnmpInfo
|
* @param model
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value = "view")
|
public String view(Model model, HttpServletRequest request) {
|
String errorMsg = "查看snmp设备图表错误";
|
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");
|
SnmpInfo SnmpInfo = new SnmpInfo();
|
try {
|
SnmpInfo = snmpInfoService.selectById(id);
|
Map<String, Object> params = new HashMap<String, Object>();
|
model.addAttribute("snmpInfo", SnmpInfo);
|
dashboardService.setDateParam(am, startTime, endTime, params, model);
|
model.addAttribute("amList", dashboardService.getAmList());
|
params.put("snmpInfoId", SnmpInfo.getId());
|
List<SnmpState> snmpStateList = snmpStateService.selectAllByParams(params);
|
//设置图表的副标题,进出口流量的最高、平均、最低值,并把进出口流量单位转为MB
|
snmpStateService.setSubtitle(model, snmpStateList, SnmpInfo);
|
model.addAttribute("snmpStateList", JSONUtil.parseArray(snmpStateList));
|
} catch (Exception e) {
|
logger.error(errorMsg, e);
|
logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
return "snmp/view";
|
}
|
|
/**
|
* 查看该snmp设备统计图导出excel
|
*
|
* @param model
|
* @param request
|
* @return
|
*/
|
@RequestMapping(value = "chartExcel")
|
public void chartExcel(Model model, HttpServletRequest request, HttpServletResponse response) {
|
String errorMsg = "snmp设备统计图导出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("snmpInfoId", id);
|
dashboardService.setDateParam(am, startTime, endTime, params, model);
|
excelExportService.exportSnmpExcel(params, response);
|
} catch (Exception e) {
|
logger.error(errorMsg, e);
|
logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
}
|
|
|
/**
|
* 删除snmp设备
|
*
|
* @param id
|
* @param model
|
* @param request
|
* @param redirectAttributes
|
* @return
|
*/
|
@RequestMapping(value = "del")
|
public String delete(Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
|
String errorMsg = "删除snmp设备监测错误";
|
SnmpInfo SnmpInfo = new SnmpInfo();
|
try {
|
if (!StringUtils.isEmpty(request.getParameter("id"))) {
|
String[] ids = request.getParameter("id").split(",");
|
for (String id : ids) {
|
SnmpInfo = snmpInfoService.selectById(id);
|
snmpInfoService.saveLog(request, StaticKeys.LOG_DEL, SnmpInfo);
|
}
|
snmpInfoService.deleteById(ids);
|
}
|
} catch (Exception e) {
|
logger.error(errorMsg, e);
|
logInfoService.save(errorMsg, e.toString(), StaticKeys.LOG_XTCZ);
|
}
|
return "redirect:/snmpInfo/list";
|
}
|
|
}
|