-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetStats.java
More file actions
210 lines (191 loc) · 8.26 KB
/
NetStats.java
File metadata and controls
210 lines (191 loc) · 8.26 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.arloor.forwardproxy.monitor;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
/**
* 网卡网速监控
*/
public class NetStats {
private static final String filename = "/proc/net/dev";
private static List<String> interfaces = new ArrayList<>();
private static List<String> xScales = new ArrayList<>();
private static final int seconds = 600;
static {
for (int i = 1; i <= seconds; i++) {
xScales.add(String.valueOf(i));
}
}
private static class YValue {
String name;
List<Double> data;
String type = "line";
boolean smooth = false;
List<String> color = Lists.newArrayList("#b111f6");//#90EC7D
Map<String, List<Map>> markPoint = ImmutableMap.of("data", Lists.newArrayList(ImmutableMap.of("type", "max", "name", "最大值")));
Map<String, List<Map>> markLine = ImmutableMap.of("data", Lists.newArrayList(ImmutableMap.of("type", "average", "name", "平均值")));
public Map<String, List<Map>> getMarkLine() {
return markLine;
}
public YValue(String name, List<Double> data) {
this.name = name;
this.data = data;
}
public Map<String, List<Map>> getMarkPoint() {
return markPoint;
}
public String getName() {
return name;
}
public List<Double> getData() {
return data;
}
public String getType() {
return type;
}
public boolean isSmooth() {
return smooth;
}
}
private static final Map<String, List<Double>> inSpeedMap = new HashMap<>();
private static final Map<String, List<Double>> outSpeedMap = new HashMap<>();
private static final Map<String, Long> interIn = new HashMap<>();
private static final Map<String, Long> interOut = new HashMap<>();
public static Runnable task = () -> {
File file = new File(filename);
if (file.exists()) {
try {
List<String> lines = Files.readAllLines(Paths.get(file.toURI()));
interfaces = lines.stream().skip(2).map(line -> line.replaceAll("(\\s)+", ",").split(",")[1])
.filter(eth -> !eth.startsWith("lo:"))
.flatMap(eth -> {
ArrayList<String> objects = new ArrayList<>();
objects.add(eth + "入");
objects.add(eth + "出");
return objects.stream();
}).collect(Collectors.toList());
// interfaces.stream().forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
List<String> lines = Files.readAllLines(Paths.get(file.toURI()));
lines.stream().skip(2).forEach((line) -> {
line = line.replaceAll("(\\s)+", ",");
String[] split = line.split(",");
String eth = split[1];
if (eth.startsWith("lo:")) {
return;
}
long in = Long.parseLong(split[2]);
long out = Long.parseLong(split[10]);
long oldOut = interOut.getOrDefault(eth, (long) 0);
long oldIn = interIn.getOrDefault(eth, (long) 0);
long outChange = out - oldOut;
long inChange = in - oldIn;
if (oldIn != 0) {
inSpeedMap.computeIfAbsent(eth, s -> new LinkedList<>());
inSpeedMap.get(eth).add((double) (inChange / 1024));
if (inSpeedMap.get(eth).size() > seconds) {
inSpeedMap.get(eth).remove(0);
}
}
if (oldOut != 0) {
outSpeedMap.computeIfAbsent(eth, s -> new LinkedList<Double>());
outSpeedMap.get(eth).add((double) (outChange / 1024));
if (outSpeedMap.get(eth).size() > seconds) {
outSpeedMap.get(eth).remove(0);
}
}
interIn.put(eth, in);
interOut.put(eth, out);
});
Thread.sleep(1000);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
private static final List<YValue> buildYvalues() {
List<YValue> YValues = new ArrayList<>();
// inSpeedMap.entrySet().forEach(entry->{
// String eth =entry.getKey();
// List<Double> speeds=entry.getValue();
// YValue yValue =new YValue(eth+"入",speeds);
// YValues.add(yValue);
// });
outSpeedMap.entrySet().forEach(entry -> {
String eth = entry.getKey();
List<Double> speeds = entry.getValue();
YValue yValue = new YValue(eth + "出", speeds);
YValues.add(yValue);
});
return YValues;
}
public static final void start() {
new Thread(NetStats.task).start();
}
public static final String html() {
List<YValue> yValues = buildYvalues();
String legends = JSONObject.toJSONString(interfaces);
String scales = JSONObject.toJSONString(xScales);
String series = JSONObject.toJSONString(yValues);
String template = "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>谁在跑流量</title>\n" +
" <script src=\"https://cdn.staticfile.org/echarts/4.8.0/echarts.min.js\"></script>\n" +
"</head>\n" +
"<body>\n" +
"<div id=\"main\" style=\"width: 100%;height: 800px;\"></div>\n" +
"<script type=\"text/javascript\">\n" +
" // 基于准备好的dom,初始化echarts实例\n" +
" var myChart = echarts.init(document.getElementById('main'));\n" +
" // 指定图表的配置项和数据\n" +
" var option = {\n" +
" title: {\n" +
" text: '网卡网速|KB/s'\n" +
" },\n" +
" tooltip: {\n" +
" trigger: 'axis'\n" +
" },\n" +
" legend: {\n" +
" data: " + legends + "\n" +
" },\n" +
" toolbox: {\n" +
" feature: {\n" +
" mark : {show: true},\n" +
" dataView : {show: true, readOnly: false},\n" +
" magicType : {show: true, type: ['line', 'bar']},\n" +
" restore : {show: true},\n" +
" saveAsImage : {show: true}" +
" }\n" +
" },\n" +
" xAxis: {\n" +
" type: 'category',\n" +
" boundaryGap: false,\n" +
" data: " + scales + "\n" +
" },\n" +
" yAxis: {\n" +
" type: \"value\"\n" +
" },\n" +
" series: " + series + "\n" +
" };\n" +
" // 使用刚指定的配置项和数据显示图表。\n" +
" myChart.setOption(option);\n" +
"</script>\n" +
"</body>\n" +
"</html>";
return template;
}
}