Skip to content

Commit

Permalink
Merge pull request #3 from uavorg/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
ieven authored Oct 26, 2017
2 parents c5eac61 + 7c1957b commit 52df900
Show file tree
Hide file tree
Showing 19 changed files with 335 additions and 109 deletions.
6 changes: 5 additions & 1 deletion com.creditease.uav.agent.buildComponent/bin/start.bat
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ if not [%2] == [] set appID=%2
if not "%JAVA_HOME%" == "" set executeJava="%JAVA_HOME%/bin/java"

cd ..
if not exist C:\Windows\System32\Packet.dll (move lib\Packet.dll C:\Windows\System32)
if not exist C:\Windows\System32\wpcap.dll (move lib\wpcap.dll C:\Windows\System32)
if not exist C:\Windows\System32\drivers\npf.sys (move lib\npf.sys C:\Windows\System32\drivers)

set CLASSPATH=bin/com.creditease.uav.base-1.0-boot.jar
set javaAgent="-javaagent:../uavmof/com.creditease.uav.agent/com.creditease.uav.monitorframework.agent-1.0-agent.jar"
set javaOpts=-server -Xms64m -Xmx256m -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:+CMSIncrementalPacing -XX:CMSIncrementalDutyCycleMin=0 -XX:CMSIncrementalDutyCycle=10 -XX:+UseParNewGC
%executeJava% %javaAgent% %javaOpts% -DNetCardIndex=0 -DJAppID=%appID% -DJAppGroup=UAV -classpath "%CLASSPATH%" com.creditease.mscp.boot.MSCPBoot -p %profile%
java %javaAgent% %javaOpts% -Djava.library.path=./lib -DNetCardIndex=0 -DJAppID=%appID% -DJAppGroup=UAV -classpath "%CLASSPATH%" com.creditease.mscp.boot.MSCPBoot -p %profile%
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions com.creditease.uav.agent.buildComponent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
<fileset file="../com.creditease.uav.agent/target/com.creditease.uav.agent-1.0.jar" ></fileset>
<fileset file="../com.creditease.uav.agent/target/lib/*.jar" ></fileset>
<fileset file="../com.creditease.uav.upgrade/target/com.creditease.uav.upgrade-1.0.jar"></fileset>
<fileset file="../com.creditease.uav.agent.buildComponent/exlib/*.dll" ></fileset>
<fileset file="../com.creditease.uav.agent.buildComponent/exlib/npf.sys" ></fileset>
<!-- additional feature jar -->
<fileset file="../com.creditease.uav.threadanalysis/target/com.creditease.uav.threadanalysis-1.0.jar" ></fileset>
</copy>
Expand Down
8 changes: 7 additions & 1 deletion com.creditease.uav.agent.heartbeat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@
<artifactId>com.creditease.uav.cache.redis</artifactId>

</dependency>

<dependency>
<groupId>jpcap</groupId>
<artifactId>jpcap</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/../com.creditease.uav.agent.buildComponent/exlib/jpcap.jar</systemPath>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,28 +291,25 @@ private String getIODiskInfo() throws Exception {
if (JVMToolHelper.isWindows()) {
File[] roots = File.listRoots();

StringBuilder sb = new StringBuilder("{");

Map<String, Map<String, String>> resultMap = new HashMap<String, Map<String, String>>();
for (int i = 0; i < roots.length; i++) {
File file = roots[i];
sb.append("\"" + file.getPath() + "\":{")
.append("\"free\":\"" + Math.round((double) (file.getFreeSpace() / 1024)) + "\",")// 空闲空间
.append("\"total\":\"" + +Math.round((double) (file.getTotalSpace() / 1024)) + "\",")// 总空间
.append("\"use\":\""
+ +Math.round((double) ((file.getTotalSpace() - file.getFreeSpace()) / 1024)) + "\",")// 已使用空间
.append("\"useRate\":\""
+ ((file.getTotalSpace() == 0) ? "0"
: (file.getTotalSpace() - file.getFreeSpace()) * 100 / file.getTotalSpace())
+ "%\"")// 空间占用率
.append("}");

if (i < roots.length - 1) {
sb.append(",");
}
if (0 == file.getTotalSpace())
continue;

Map<String, String> temp = new HashMap<String, String>();
temp.put("free", String.valueOf(Math.round((double) (file.getFreeSpace() / 1024))));
temp.put("total", String.valueOf(Math.round((double) (file.getTotalSpace() / 1024))));
temp.put("use",
String.valueOf(Math.round((double) ((file.getTotalSpace() - file.getFreeSpace()) / 1024))));
temp.put("useRate", ((file.getTotalSpace() == 0) ? 0
: (file.getTotalSpace() - file.getFreeSpace()) * 100 / file.getTotalSpace()) + "%");
resultMap.put(file.getPath().substring(0, file.getPath().length() - 1), temp);
}

sb.append("}");
return sb.toString();
dioc.collectWin(resultMap);

return JSON.toJSONString(resultMap);
}
else {
Map<String, Map<String, String>> resultMap = new HashMap<String, Map<String, String>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,22 @@

package com.creditease.agent.feature.procdetectagent;

import java.text.DecimalFormat;
import java.util.HashMap;

import com.creditease.agent.helpers.JSONHelper;
import com.creditease.agent.helpers.JVMToolHelper;
import com.creditease.agent.helpers.NetworkHelper;
import com.creditease.agent.helpers.RuntimeHelper;
import com.creditease.agent.spi.AbstractTimerWork;
import com.creditease.agent.spi.AgentFeatureComponent;

import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
import jpcap.packet.Packet;
import jpcap.packet.TCPPacket;

public class NetworkIoDetector extends AbstractTimerWork {

private String portList;
Expand All @@ -39,54 +49,150 @@ public NetworkIoDetector(String cName, String feature) {
@Override
public void run() {

/**
* NOTE: not support windows yet
*/
if (JVMToolHelper.isWindows()) {
if (portList == null) {
return;
}
if (countDown != 0) {
countDown--;
return;

String result = null;
String networkdetectTime = this.getConfigManager().getFeatureConfiguration(this.feature,
"networkDetect.collectTime");
String ip = NetworkHelper.getLocalIP();

// windows
if (JVMToolHelper.isWindows()) {

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个
JpcapCaptor 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);
}

}
if (portList != null) {

// linux
else {

if (countDown != 0) {
countDown--;
return;
}

try {
String networkdetectTime = this.getConfigManager().getFeatureConfiguration(this.feature,
"networkDetect.collectTime");
String ip = NetworkHelper.getLocalIP();

String netcardName = NetworkHelper.getNetCardName(ip);
String command = "cd bin; sh networkIoDetect.sh " + netcardName + " " + ip + " " + networkdetectTime
+ " " + portList;

String result = RuntimeHelper.exec(10000, "/bin/sh", "-c", command);
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;
}

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);
}

}
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);
}

}

public void setPortList(String portList) {

this.portList = portList;
}

public String portFlux(JpcapCaptor jpcap, HashMap<String, Integer> counter, NetworkInterface nc,
String networkdetectTime, String Local_ip) {

Packet packet;

int int_networkdetectTime = 1024 * Integer.parseInt(networkdetectTime) / 1000;
long time = Long.parseLong(networkdetectTime);
long startTime = System.currentTimeMillis();
while (startTime + time >= System.currentTimeMillis()) {
// 抓包
packet = jpcap.getPacket();
if (null == packet) {
continue;
}
TCPPacket p = (TCPPacket) packet;
// 端口不在protList 忽略
String dst_ip = p.dst_ip.toString();
String src_ip = p.src_ip.toString();
// 统计入口流量
if (dst_ip.equals(Local_ip)) {
String in_port = "in_" + p.dst_port;
if (counter.containsKey(in_port)) {
int in_value = counter.get(in_port) + p.len;
counter.put(in_port, in_value);
}
}
// 统计出口流量
if (src_ip.equals(Local_ip)) {
String out_port = "out_" + p.src_port;
if (counter.containsKey(out_port)) {
int out_value = counter.get(out_port) + p.len;
counter.put(out_port, out_value);
}
}
}

HashMap<String, String> counterValueIntToString = new HashMap<String, String>();
// 字节转kb/s
for (String key : counter.keySet()) {
DecimalFormat df = new DecimalFormat("#0.00");
Double value = Double.valueOf(df.format(counter.get(key) / int_networkdetectTime * 1.0));
counterValueIntToString.put(key, value.toString());
}
// 将hashmap 转为json 返回
return JSONHelper.toString(counterValueIntToString);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public void scanWindowsProcesses(Map<String, OSProcess> procs, StringBuffer port
proc.addPort(port);

portList = portList.append(port).append(" ");
if (!"UNKOWN".equals(proc.getName())) {
if (!"UNKNOWN".equals(proc.getName())) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,7 @@ var mvcObj={
"node.pid":"info/node.pid",
"node.procs":"info/node.procs",
"node.services":"info/node.services",
"node.version":"info/node.version",
"node.docker.info": "info/node.docker.info",
"node.docker.container": "info/node.docker.container"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.regex.Pattern;

import com.creditease.agent.helpers.IOHelper;
import com.creditease.agent.helpers.RuntimeHelper;
import com.creditease.agent.helpers.StringHelper;

public class DiskIOCollector {

Expand All @@ -39,6 +41,39 @@ public class DiskIOCollector {
private Map<String, Map<String, Long>> lastDiskIoMap = new HashMap<String, Map<String, Long>>();
private long time = 0;

public void collectWin(Map<String, Map<String, String>> resultMap) {

try {
String output = RuntimeHelper.exec(
"wmic path Win32_PerfFormattedData_PerfDisk_LogicalDisk get DiskReadBytesPerSec,DiskWriteBytesPerSec,Name");

if (StringHelper.isEmpty(output)) {
return;
}

String[] strs = output.split("\n");
for (String str : strs) {
str = str.replaceAll("\\s{2,}", " ");
String[] args = str.split(" ");
if (1 == args.length) {
continue;
}

if (!(args[2].equals("_Total")) && !(args[2].equals("Name"))) {
double rd_persec = Long.parseLong(args[0]) / 1024.0;
double wr_persec = Long.parseLong(args[1]) / 1024.0;

DecimalFormat df = new DecimalFormat("#0.00");
resultMap.get(args[2]).put("disk_read", df.format(rd_persec));
resultMap.get(args[2]).put("disk_write", df.format(wr_persec));
}
}
}
catch (Exception e) {
// ignore
}
}

public void collect(Map<String, Map<String, String>> resultMap) {

Map<String, Map<String, Long>> currentDiskIoMap = new HashMap<String, Map<String, Long>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class OSProcess {
private Set<String> ports = new HashSet<String>();

// program name
private String name = "UNKOWN";
private String name = "UNKNOWN";

// extract info to describe the process
private Map<String, String> tags = new LinkedHashMap<String, String>();
Expand Down
Loading

0 comments on commit 52df900

Please sign in to comment.