Skip to content

Commit a2815dd

Browse files
committed
feat:首次提交完整功能
1 parent 759e04d commit a2815dd

22 files changed

+1861
-1
lines changed

.gitignore

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.ear
17+
*.zip
18+
*.tar.gz
19+
*.rar
20+
21+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22+
hs_err_pid*
23+
24+
25+
# Operating System Files
26+
27+
*.DS_Store
28+
Thumbs.db
29+
*.sw?
30+
.#*
31+
*#
32+
*~
33+
*.sublime-*
34+
35+
# Build Artifacts
36+
37+
.gradle/
38+
build/
39+
target/
40+
bin/
41+
dependency-reduced-pom.xml
42+
43+
# Eclipse Project Files
44+
45+
.classpath
46+
.project
47+
.settings/
48+
49+
# IntelliJ IDEA Files
50+
51+
*.iml
52+
*.ipr
53+
*.iws
54+
*.idea
55+
.idea
56+
out
57+
58+
README.html
59+
/LOGS/

README.md

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,114 @@
11
# netty-monitor-agent
2-
一款用Java Agent实现的Netty无侵入监控软件
2+
# 1. 介绍
3+
4+
## 1.1 基本概况
5+
6+
该项目是基于Java Agent的静态加载实现的Netty无侵入监控,创建目的是为了学习和使用Java Agent技术及Netty数据监控
7+
8+
## 1.2 技术栈
9+
10+
Java Agent + bytebuddy + Netty
11+
12+
## 1.3 使用方式
13+
14+
### 项目中
15+
16+
将代码打包为jar包,或者从标签处下载,在jar包运行时添加参数 -javaagent:agen包路径
17+
18+
例如项目包test.jar和agent包netty-monitor-agent-1.0.jar在同一目录,那么启动命令则如下:
19+
20+
```
21+
java -javaagent:./netty-monitor-agent-1.0.jar.jar -jar test.jar
22+
```
23+
24+
### IDEA中
25+
26+
点击 Add VM options,然后在该行中填入 -javaagent:agent包路径,如下:
27+
28+
```
29+
-javaagent:./netty-monitor-agent-1.0.jar.jar
30+
```
31+
32+
如果需要Debug,则把该agent包的依赖导入即可
33+
34+
## 2. 其它
35+
36+
### 2.1 自定义输出
37+
38+
#### 侵入式(不推荐)
39+
40+
在项目包中引入agent依赖,实现接口MonitorOutService,在该接口中进行监控数据输出,然后通过添加启动参数:-DmonitorOutServiceClass=实现类的全类名,如下:
41+
42+
```
43+
public class TestMonitorOutService implements MonitorOutService {
44+
@Override
45+
public void outputMonitorData(GlobalMonitorData globalMonitorData, List<EventLoopMonitorData> eventLoopMonitorDataList, List<ChannelMonitorData> channelMonitorDataList) {
46+
System.out.println("测试输出");
47+
}
48+
}
49+
50+
启动参数:
51+
-DmonitorOutServiceClass=com.github.netty.monitor.agent.test.TestMonitorOutService
52+
```
53+
54+
#### 非侵入式
55+
56+
自己修改源码,实现接口MonitorOutService,然后添加启动参数:-DmonitorOutServiceClass=实现类的全类名,之后再打jar去做无侵入监控,可以把监控数据持久化记录到Redis、MySQL等数据库中
57+
58+
### 2.2 监控数据
59+
60+
#### 全局数据
61+
62+
| 字段 | 说明 |
63+
| --------------- | ------------------------------------------------------------ |
64+
| ActiveChannel | 激活的连接数 |
65+
| TotalInput | 总接受字节量,单位B、KB、MB、GB、TB自动变化 |
66+
| TotalOutput | 总输出字节量,单位B、KB、MB、GB、TB自动变化 |
67+
| InputRate | 总输入速率,单位B/s、KB/s、MB/s、GB/s、TB/s自动变化 |
68+
| OutputRate | 总输出速率,单位B/s、KB/s、MB/s、GB/s、TB/s自动变化 |
69+
| PoolUseHeap | 池化堆内内存使用总量,单位B、KB、MB、GB、TB自动变化 |
70+
| PoolUseDirect | 池化直接内存使用总量,单位B、KB、MB、GB、TB自动变化 |
71+
| unpoolUseHeap | 非池化堆内内存使用总量,单位B、KB、MB、GB、TB自动变化 |
72+
| unpoolUseDirect | 非池化直接内存使用总量,单位B、KB、MB、GB、TB自动变化 |
73+
| useDirect | 直接内存使用总量(池化+非池化),单位B、KB、MB、GB、TB自动变化 |
74+
75+
#### EventLoop数据
76+
77+
| 字段 | 说明 |
78+
| ------------- | ----------------------------- |
79+
| EventLoopName | 当前EventLoop的名称 |
80+
| ChannelCount | 当前EventLoop下的活跃连接总数 |
81+
| PendingTask | 当前EventLoop下的任务总数 |
82+
| State | 当前EventLoop的状态 |
83+
84+
每个EventLoop都会打印一份
85+
86+
#### Channel数据
87+
88+
| 字段 | 说明 |
89+
| -------------- | --------------------------------------------------- |
90+
| LocalAddress | 本地地址(IP:端口) |
91+
| RemoteAddress | 远端地址(IP:端口) |
92+
| TotalInput | 接受字节量,单位B、KB、MB、GB、TB自动变化 |
93+
| TotalOutput | 输出字节量,单位B、KB、MB、GB、TB自动变化 |
94+
| InputRate | 输入速率,单位B/s、KB/s、MB/s、GB/s、TB/s自动变化 |
95+
| OutputRate | 输出速率,单位B/s、KB/s、MB/s、GB/s、TB/s自动变化 |
96+
| activeDate | 激活时间,格式:MM-dd HH:mm:ss |
97+
| inactiveDate | 断开时间,格式:MM-dd HH:mm:ss |
98+
| LastInputDate | 最后一次接受数据时间,格式:MM-dd HH:mm:ss |
99+
| LastOutputDate | 最后一次输出数据的时间,格式:MM-dd HH:mm:ss |
100+
| LastEventDate | 最后一次发生事件的时间,格式:MM-dd HH:mm:ss |
101+
| LastExceptDate | 最后一次Channel发生异常的时间,格式:MM-dd HH:mm:ss |
102+
| LastExceptMsg | 最后一次Channel发生异常的信息 |
103+
104+
### 2.3 注意事项
105+
106+
1.该项目主要是学习为主,代码可供参考
107+
108+
2.支持JDK版本为1.8以上(其它版本需要自测)
109+
110+
3.暂不支持Netty版本为4.0.45.Final以下的,4.0.0至4.0.44.Final的版本需要自行通过反射去PlatformDepedent类中获取池化和非池化的数据
111+
112+
4.默认是控制台输出,如果导入了日志框架,则会打印日志,默认是每5秒打印一次,对于离线的Channel,离线的30秒后该Channel数据才会消失
113+
114+
5.最好自己修改源码,实现持久化数据存储

pom.xml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.xy</groupId>
8+
<artifactId>netty-monitor-agent</artifactId>
9+
<version>1.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>8</maven.compiler.source>
13+
<maven.compiler.target>8</maven.compiler.target>
14+
</properties>
15+
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-source-plugin</artifactId>
22+
<version>3.2.1</version>
23+
<executions>
24+
<execution>
25+
<id>attach-sources</id>
26+
<phase>package</phase>
27+
<goals>
28+
<goal>jar</goal>
29+
</goals>
30+
</execution>
31+
</executions>
32+
</plugin>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-assembly-plugin</artifactId>
36+
<version>2.4</version>
37+
<configuration>
38+
<appendAssemblyId>false</appendAssemblyId>
39+
<descriptorRefs>
40+
<descriptorRef>jar-with-dependencies</descriptorRef>
41+
</descriptorRefs>
42+
<archive>
43+
<manifest>
44+
<!-- 添加 mplementation-*和Specification-*配置项-->
45+
<addDefaultImplementationEntries>true
46+
</addDefaultImplementationEntries>
47+
<addDefaultSpecificationEntries>true
48+
</addDefaultSpecificationEntries>
49+
</manifest>
50+
<manifestEntries>
51+
<!-- 设置manifest配置文件-->
52+
<Premain-Class>com.github.netty.monitor.agent.NettyMonitorAgent</Premain-Class>
53+
<Can-Redefine-Classes>true</Can-Redefine-Classes>
54+
<Can-Retransform-Classes>true</Can-Retransform-Classes>
55+
</manifestEntries>
56+
</archive>
57+
</configuration>
58+
59+
<executions>
60+
<execution>
61+
<id>make-assembly</id>
62+
<phase>package</phase>
63+
<goals>
64+
<goal>single</goal>
65+
</goals>
66+
</execution>
67+
</executions>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
<dependencies>
72+
<dependency>
73+
<groupId>net.bytebuddy</groupId>
74+
<artifactId>byte-buddy</artifactId>
75+
<version>1.10.18</version>
76+
</dependency>
77+
<dependency>
78+
<groupId>net.bytebuddy</groupId>
79+
<artifactId>byte-buddy-agent</artifactId>
80+
<version>1.10.18</version>
81+
</dependency>
82+
<dependency>
83+
<groupId>io.netty</groupId>
84+
<artifactId>netty-all</artifactId>
85+
<version>4.1.109.Final</version>
86+
<scope>provided</scope>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.projectlombok</groupId>
90+
<artifactId>lombok</artifactId>
91+
<version>1.18.4</version>
92+
<scope>provided</scope>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.slf4j</groupId>
96+
<artifactId>slf4j-api</artifactId>
97+
<version>1.7.21</version>
98+
</dependency>
99+
</dependencies>
100+
101+
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.github.netty.monitor.agent;
2+
3+
4+
/**
5+
* @Date: 2022/6/11 10:52
6+
* @Description: 常量类
7+
* @author: xzc-coder
8+
*/
9+
public class Constant {
10+
11+
private Constant() {
12+
13+
}
14+
15+
/**
16+
* 统计间隔,每5秒统计一次
17+
*/
18+
public static int STATISTICS_INTERVAL_SECOND = 5;
19+
20+
/**
21+
* Channel断开连接后延迟多少秒移除
22+
*/
23+
24+
public static int CHANNEL_MONITOR_REMOVE_DELAY_SECOND = 30;
25+
26+
public static String SPACE = " ";
27+
28+
public static String EMPTY = "";
29+
30+
public static final String B = "B";
31+
32+
public static final String KB = "KB";
33+
34+
public static final String MB = "MB";
35+
36+
public static final String GB = "GB";
37+
38+
public static final String TB = "TB";
39+
40+
public static final String RATE = "/s";
41+
42+
public static final long CARRY = 1024L;
43+
44+
public static final long KB_CARRY = CARRY;
45+
46+
public static final long MB_CARRY = KB_CARRY * CARRY;
47+
48+
public static final long GB_CARRY = MB_CARRY * CARRY;
49+
50+
public static final long TB_CARRY = GB_CARRY * CARRY;
51+
52+
public static final int LOG_MEMORY_SCALE = 6;
53+
54+
public static final int LOG_SCALE = 3;
55+
56+
public static final int LOG_SCALE_B = 0;
57+
58+
public static final String MONITOR_OUT_SERVICE_CLASS = "monitorOutServiceClass";
59+
}

0 commit comments

Comments
 (0)