package com.wgcloud.service;
|
|
import cn.hutool.core.bean.BeanUtil;
|
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageInfo;
|
import com.wgcloud.entity.DceInfo;
|
import com.wgcloud.entity.DceState;
|
import com.wgcloud.entity.HostGroup;
|
import com.wgcloud.mapper.DceInfoMapper;
|
import com.wgcloud.util.DateUtil;
|
import com.wgcloud.util.HostUtil;
|
import com.wgcloud.util.PingUtil;
|
import com.wgcloud.util.UUIDUtil;
|
import com.wgcloud.util.msg.WarnMailUtil;
|
import com.wgcloud.util.msg.WarnPools;
|
import com.wgcloud.util.staticvar.BatchData;
|
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.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @version v3.3
|
* @ClassName:DceInfoService.java
|
* @author: http://www.wgstart.com
|
* @date: 2021年5月14日
|
* @Description: DceInfoService.java
|
* @Copyright: 2019-2021 wgcloud. All rights reserved.
|
*/
|
@Service
|
public class DceInfoService {
|
|
private static final Logger logger = LoggerFactory.getLogger(DceInfoService.class);
|
|
public PageInfo selectByParams(Map<String, Object> params, int currPage, int pageSize) throws Exception {
|
PageHelper.startPage(currPage, pageSize);
|
List<DceInfo> list = dceInfoMapper.selectByParams(params);
|
PageInfo<DceInfo> pageInfo = new PageInfo<DceInfo>(list);
|
return pageInfo;
|
}
|
|
public void save(DceInfo DceInfo, HttpServletRequest request) throws Exception {
|
DceInfo.setId(UUIDUtil.getUUID());
|
DceInfo.setResTimes(1);
|
Date nowDate = DateUtil.getNowTime();
|
DceInfo.setCreateTime(nowDate);
|
if (StringUtils.isEmpty(DceInfo.getHostname())) {
|
DceInfo.setHostname(DceInfo.getHostname().trim());
|
}
|
dceInfoMapper.save(DceInfo);
|
//保存页面动态添加的端口数据
|
addExtDataForm(DceInfo, request, nowDate);
|
}
|
|
/**
|
* 保存页面动态添加的PING设备数据
|
*
|
* @param dceInfo
|
* @param request
|
*/
|
private void addExtDataForm(DceInfo dceInfo, HttpServletRequest request, Date nowDate) throws Exception {
|
String dataFromIndex = request.getParameter("dataFromIndex");
|
int rowsLen = 0;
|
if (!StringUtils.isEmpty(dataFromIndex)) {
|
for (int i = 0; i <= Integer.valueOf(dataFromIndex); i++) {
|
|
|
String hostname = request.getParameter("hostname_" + i);
|
String remark = request.getParameter("remark_" + i);
|
if (StringUtils.isEmpty(hostname)) {
|
continue;
|
}
|
DceInfo dceInfoExt = new DceInfo();
|
BeanUtil.copyProperties(dceInfo, dceInfoExt, true);
|
dceInfoExt.setId(UUIDUtil.getUUID());
|
dceInfoExt.setCreateTime(nowDate);
|
dceInfoExt.setHostname(hostname);
|
dceInfoExt.setRemark(remark);
|
dceInfoMapper.save(dceInfoExt);
|
|
saveLog(request, StaticKeys.LOG_ADD, dceInfoExt);
|
rowsLen += 1;
|
}
|
}
|
}
|
|
|
@Transactional
|
public void saveRecord(List<DceInfo> recordList) throws Exception {
|
if (recordList.size() < 1) {
|
return;
|
}
|
Date now = DateUtil.getNowTime();
|
for (DceInfo as : recordList) {
|
as.setResTimes(1);
|
as.setId(UUIDUtil.getUUID());
|
as.setCreateTime(now);
|
}
|
dceInfoMapper.insertList(recordList);
|
}
|
|
public int countByParams(Map<String, Object> params) throws Exception {
|
return dceInfoMapper.countByParams(params);
|
}
|
|
@Transactional
|
public int deleteById(String[] id) throws Exception {
|
return dceInfoMapper.deleteById(id);
|
}
|
|
public void updateById(DceInfo dceInfo)
|
throws Exception {
|
if (StringUtils.isEmpty(dceInfo.getHostname())) {
|
dceInfo.setHostname(dceInfo.getHostname().trim());
|
}
|
dceInfoMapper.updateById(dceInfo);
|
}
|
|
public DceInfo selectById(String id) throws Exception {
|
return dceInfoMapper.selectById(id);
|
}
|
|
@Transactional
|
public void updateRecord(List<DceInfo> recordList) throws Exception {
|
dceInfoMapper.updateList(recordList);
|
}
|
|
public List<DceInfo> selectAllByParams(Map<String, Object> params) throws Exception {
|
return dceInfoMapper.selectAllByParams(params);
|
}
|
|
/**
|
* 设置资源分组 begin
|
*
|
* @param recordList
|
*/
|
public List<HostGroup> setGroupInList(List<DceInfo> recordList) throws Exception {
|
Map<String, Object> params = new HashMap<String, Object>();
|
params.put("groupType", StaticKeys.HOST_GROUP_2);
|
List<HostGroup> hostGroupList = hostGroupService.selectAllByParams(params);
|
//查询所有主机分组列表,封装为map
|
Map<String, String> hostGroupMap = new HashMap<>();
|
for (HostGroup hostGroup : hostGroupList) {
|
hostGroupMap.put(hostGroup.getId(), hostGroup.getGroupName());
|
}
|
for (DceInfo dceInfo1 : recordList) {
|
if (!StringUtils.isEmpty(dceInfo1.getGroupId())) {
|
dceInfo1.setGroupId(hostGroupMap.get(dceInfo1.getGroupId()));
|
}
|
}
|
return hostGroupList;
|
}
|
|
/**
|
* 保存操作日志
|
*
|
* @param request 获取当前登录用户
|
* @param action 操作标识
|
*/
|
public void saveLog(HttpServletRequest request, String action, DceInfo dceInfo) {
|
if (null == dceInfo) {
|
return;
|
}
|
logInfoService.save(HostUtil.getAccountByRequest(request).getAccount() + action + "数通设备PING监测信息:" + dceInfo.getHostname(),
|
"数通设备:" + dceInfo.getRemark(), StaticKeys.LOG_XTCZ);
|
}
|
|
/**
|
* 检测数通设备任务具体处理线程
|
*
|
* @param h
|
* @param date
|
*/
|
public void taskThreadHandler(DceInfo h, Date date) {
|
long resTimes = PingUtil.ping(h.getHostname(), 1, 3);
|
h.setCreateTime(date);
|
//设备下线后,不再更新时间 begin
|
if (resTimes < 0 && h.getResTimes() < 0) {
|
h.setCreateTime(null);
|
}
|
//设备下线后,不再更新时间 end
|
h.setResTimes(Integer.valueOf(resTimes + ""));
|
try {
|
updateById(h);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
if (resTimes > 0) {
|
DceState dceState = new DceState();
|
dceState.setResTimes(h.getResTimes());
|
dceState.setDceId(h.getId());
|
dceState.setCreateTime(date);
|
BatchData.DCE_STATE_LIST.add(dceState);
|
}
|
if (h.getResTimes() < 0) {
|
//返回错误处理
|
WarnMailUtil.sendDceInfo(h, true);
|
} else {
|
//返回成功处理
|
if (null != WarnPools.MEM_WARN_MAP.get(h.getId())) {
|
WarnMailUtil.sendDceInfo(h, false);
|
}
|
}
|
}
|
|
@Autowired
|
private DceInfoMapper dceInfoMapper;
|
@Resource
|
private HostGroupService hostGroupService;
|
@Autowired
|
private LogInfoService logInfoService;
|
|
|
}
|