TopoDiscoThread.java
5.09 KB
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
package com.sitech.ismp.topo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.sitech.topo.bean.Node;
import com.sitech.topo.db.domain.TbTuopuDiscoTask;
import com.sitech.topo.discover.HostDiscover;
import com.sitech.topo.discover.NetTopoDiscover;
import com.sitech.topo.report.ReportHandler;
import com.sitech.topo.util.DiscoUtils;
import com.sitech.topo.util.IPTools;
import com.sitech.topo.util.PingUtils;
import com.sitech.topo.util.TopoConstant;
public class TopoDiscoThread implements Runnable {
private Logger logger = Logger.getLogger(TopoDiscoThread.class);
/** 自动发现任务ID */
private String scheduleId;
/** 自动发现种子路由器 */
private List<String> seedRouterList = new ArrayList<String>();
/** 自动发现IP范围 */
private List<String> discoRange = new ArrayList<String>();
/** SNMP通信串 */
private List<String> communityList = new ArrayList<String>();
/** 主机登录方式 */
private List<String> loginInfoList = new ArrayList<String>();
private boolean isDiscoHost = false;
private boolean isRun = false;
public TopoDiscoThread(Map<String, String> params) {
init(params);
}
public void run() {
isRun = true;
TbTuopuDiscoTask task = ReportHandler.getInstance().startScheduler(scheduleId);
// 获得所有支持SNMP的设备
List<Node> snmpNodeList = doSnmp();
// 执行网络层拓扑发现
NetTopoDiscover netTopoDiscover = new NetTopoDiscover(scheduleId,
seedRouterList, discoRange, communityList, snmpNodeList);
netTopoDiscover.netCollect();
if (isDiscoHost) {
doHostDisco(snmpNodeList);
}
isRun = false;
ReportHandler.getInstance().stopScheduler(task);
}
/**
* 主机发现
* @param snmpNodeList
*/
private void doHostDisco(List<Node> snmpNodeList) {
try{
List<String> pingEnableIpList = doPing();
Map<String, Node> snmpNodeMap = new HashMap<String, Node>();
for(Node node : snmpNodeList){
snmpNodeMap.put(node.getNodeIp(), node);
}
List<String> hostIpList = new ArrayList<String>();
for(String ip : pingEnableIpList){
Node node = snmpNodeMap.get(ip);
if(node != null){
String nodeType = node.getNodeDevType();
if (nodeType.equals(TopoConstant.NODE_TYPE_ROUTER)
|| nodeType.equals(TopoConstant.NODE_TYPE_SWITCH2)
|| nodeType.equals(TopoConstant.NODE_TYPE_SWITCH3)
|| nodeType.equals(TopoConstant.NODE_TYPE_SWITCH4)
|| nodeType.equals(TopoConstant.NODE_TYPE_FIREWALL)) {
// 排除类型为网络设备的
continue;
}
}
hostIpList.add(ip);
}
HostDiscover hostDiscover = new HostDiscover(scheduleId, hostIpList, loginInfoList);
hostDiscover.run();
}catch (Exception e) {
logger.error("Exception while discovery host", e);
}
}
private List<Node> doSnmp() {
DiscoUtils discoUtil = new DiscoUtils(discoRange, communityList);
List<Node> snmpNodeList = discoUtil.disco();
return snmpNodeList;
}
/**
* ping所有的IP,丰富路由信息
* @return 可以ping通的IP列表
*/
private List<String> doPing() {
List<String> pingEnableIpList = null;
if(discoRange != null || discoRange.size() > 0){
try {
for (String rang : discoRange) {
String[] temp = rang.split(":");
pingEnableIpList = PingUtils.ping(temp[0], temp[1]);
}
} catch (Exception e) {
logger.error("Exception while doPing().", e);
}
}
return pingEnableIpList;
}
private void init(Map<String, String> params) {
try {
scheduleId = params.get("TASK_ID");
String descoSeedStr = params.get("DISCO_SEED");
if (descoSeedStr == null || "".equals(descoSeedStr.trim())) {
logger.error("自动发现种子为空!!");
return;
} else {
String[] temp = descoSeedStr.split(",");
for (int i = 0; i < temp.length; i++) {
seedRouterList.add(temp[i]);
}
}
String discoRangeStr = params.get("DISCO_RANGE");
if (discoRangeStr != null && !"".equals(discoRangeStr.trim())) {
String[] temp = discoRangeStr.split(",");
for (int i = 0; i < temp.length; i++) {
String[] elem = temp[i].split(":");
String ip = elem[0];
String mask = elem[1];
discoRange.add(IPTools.getSubnetRange(ip,mask));
}
}
String communityStr = params.get("DISCO_COMMUNITY");
if (communityStr == null || "".equals(communityStr.trim())) {
logger.info("未设置SNMP读共同体,使用默认值!");
communityList.add("public");
} else {
String[] temp = communityStr.split(",");
for (int i = 0; i < temp.length; i++) {
communityList.add(temp[i]);
}
}
String hostDiscoFlag = params.get("DISCO_HOST_FLAG");
if (null != hostDiscoFlag && "true".equals(hostDiscoFlag.trim())) {
isDiscoHost = true;
String loginInfo = params.get("HOST_LOGIN_INFO");
if (loginInfo != null) {
String[] temp = loginInfo.split(",");
for (int i = 0; i < temp.length; i++) {
loginInfoList.add(temp[i]);
}
}
}
} catch (Exception e) {
logger.error("Exception while init discover thread!", e);
}
}
public boolean isRun() {
return isRun;
}
}