use of jpcap.NetworkInterface in project uavstack by uavorg.
the class NetworkIoDetector method run.
@Override
public void run() {
if (portList == null) {
return;
}
String result = null;
String networkdetectTime = this.getConfigManager().getFeatureConfiguration(this.feature, "networkDetect.collectTime");
String ip = NetworkHelper.getLocalIP();
// windows
if (JVMToolHelper.isWindows()) {
JpcapCaptor jpcap = null;
try {
String Local_ip = "/" + ip;
// 存端口流量
HashMap<String, Integer> counter = new HashMap<String, Integer>();
String[] split_ProtList = portList.split(" ");
for (String str : split_ProtList) {
counter.put("in_" + str, 0);
counter.put("out_" + str, 0);
}
// 获取网卡设备列表
NetworkInterface[] devices = JpcapCaptor.getDeviceList();
// 确定网卡设备接口
boolean true_devices = false;
int i = 0;
for (; i < devices.length; i++) {
for (NetworkInterfaceAddress nia : devices[i].addresses) {
if (Local_ip.equals(nia.address.toString())) {
true_devices = true;
break;
}
}
if (true_devices) {
break;
}
}
NetworkInterface nc = devices[i];
// 打开网卡设备 ,创建某个卡口上的抓取对象,最大为65535个
jpcap = JpcapCaptor.openDevice(nc, 65535, false, 20);
// 设置过滤器
jpcap.setFilter("tcp", true);
// 抓包 统计流量
result = portFlux(jpcap, counter, nc, networkdetectTime, Local_ip);
} catch (Exception e) {
log.err(this, "NetworkIo Monitor runs FAIL.", e);
} finally {
if (jpcap != null) {
// 关闭
jpcap.close();
}
}
} else // linux
{
if (countDown != 0) {
countDown--;
return;
}
try {
String netcardName = NetworkHelper.getNetCardName(ip);
String command = "cd bin; sh networkIoDetect.sh " + netcardName + " " + ip + " " + networkdetectTime + " " + portList;
result = RuntimeHelper.exec(10000, "/bin/sh", "-c", command);
if (!result.contains("in_") || result.toLowerCase().contains("error") || result.toLowerCase().contains("traceback")) {
log.err(this, "NetworkIo Monitor runs FAIL with TechError: error=" + result);
countDown = 100;
return;
}
} catch (Exception e) {
log.err(this, "NetworkIo Monitor runs FAIL.", e);
countDown = 100;
}
}
AgentFeatureComponent afc = (AgentFeatureComponent) this.getConfigManager().getComponent(this.feature, "ProcDetectAgent");
if (null != afc) {
afc.exchange("procscan.nodeinfo.portFlux", result, System.currentTimeMillis());
}
if (log.isDebugEnable()) {
log.debug(this, "NetworkIo Monitor Result: " + result);
}
}
Aggregations