kongdeqiang
2023-02-14 18d6ac5798b9ef96077f592ccc3f94b365006e6a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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;
 
 
}