forked from pterodactyl/wings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnsm.patch
More file actions
320 lines (317 loc) · 8.54 KB
/
Copy pathnsm.patch
File metadata and controls
320 lines (317 loc) · 8.54 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
diff --git a/environment/docker/stats.go b/environment/docker/stats.go
index 7a984399c4f0587e63b41f9bd23e11e67b97cce2..1f6642753d695dd5ecc0c456ac5d5434527ccd88 100644
--- a/environment/docker/stats.go
+++ b/environment/docker/stats.go
@@ -88,6 +88,12 @@ func (e *Environment) pollResources(ctx context.Context) error {
for _, nw := range v.Networks {
st.Network.RxBytes += nw.RxBytes
st.Network.TxBytes += nw.TxBytes
+ st.Network.RxPackets += nw.RxPackets
+ st.Network.TxPackets += nw.TxPackets
+ st.Network.RxErrors += nw.RxErrors
+ st.Network.TxErrors += nw.TxErrors
+ st.Network.RxDropped += nw.RxDropped
+ st.Network.TxDropped += nw.TxDropped
}
e.Events().Publish(environment.ResourceEvent, st)
diff --git a/environment/stats.go b/environment/stats.go
index 5d5c8720307f3d0c717d159eb9be6f851ac25925..9d3084232d9118845e666b201f330ac56291a37c 100644
--- a/environment/stats.go
+++ b/environment/stats.go
@@ -25,6 +25,12 @@ type Stats struct {
}
type NetworkStats struct {
- RxBytes uint64 `json:"rx_bytes"`
- TxBytes uint64 `json:"tx_bytes"`
+ RxBytes uint64 `json:"rx_bytes"`
+ TxBytes uint64 `json:"tx_bytes"`
+ RxPackets uint64 `json:"rx_packets"`
+ TxPackets uint64 `json:"tx_packets"`
+ RxErrors uint64 `json:"rx_errors"`
+ TxErrors uint64 `json:"tx_errors"`
+ RxDropped uint64 `json:"rx_dropped"`
+ TxDropped uint64 `json:"tx_dropped"`
}
diff --git a/router/nsm_router.go b/router/nsm_router.go
new file mode 100644
index 0000000000000000000000000000000000000000..fea5c8df7254725837f5c262047d4c8e62970dfc
--- /dev/null
+++ b/router/nsm_router.go
@@ -0,0 +1,23 @@
+package router
+
+import (
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+
+ "github.com/pterodactyl/wings/router/middleware"
+)
+
+func getServerProtocolStats(c *gin.Context) {
+ s := ExtractServer(c)
+
+ stats, err := s.GetProtocolStats(c.Request.Context())
+ if err != nil {
+ middleware.CaptureAndAbort(c, err)
+ return
+ }
+
+ c.JSON(http.StatusOK, gin.H{
+ "data": stats,
+ })
+}
diff --git a/router/router.go b/router/router.go
index bdac76065242c834f37f74a68fdba2211df96125..411eee4bd6ed7b5eddf9d92f677297fb7ff46bb3 100644
--- a/router/router.go
+++ b/router/router.go
@@ -77,6 +77,7 @@ func Configure(m *wserver.Manager, client remote.Client) *gin.Engine {
server.DELETE("", deleteServer)
server.GET("/logs", getServerLogs)
+ server.GET("/stats/protocols", getServerProtocolStats)
server.POST("/power", postServerPower)
server.POST("/commands", postServerCommands)
server.POST("/install", postServerInstall)
diff --git a/server/protocol_monitor.go b/server/protocol_monitor.go
new file mode 100644
index 0000000000000000000000000000000000000000..acea374a068b0e4b103bddd3af10b4aeb36f71ce
--- /dev/null
+++ b/server/protocol_monitor.go
@@ -0,0 +1,85 @@
+package server
+
+import (
+ "context"
+ "encoding/json"
+ "sync"
+ "time"
+)
+
+type ProtocolMonitor struct {
+ mu sync.RWMutex
+ server *Server
+ stats *ProtocolStats
+ enabled bool
+ stopChan chan struct{}
+}
+
+func NewProtocolMonitor(s *Server) *ProtocolMonitor {
+ return &ProtocolMonitor{
+ server: s,
+ stats: &ProtocolStats{},
+ enabled: false,
+ stopChan: make(chan struct{}),
+ }
+}
+
+func (pm *ProtocolMonitor) Start() error {
+ pm.mu.Lock()
+ defer pm.mu.Unlock()
+
+ if pm.enabled {
+ return nil
+ }
+
+ pm.enabled = true
+ go pm.run()
+
+ return nil
+}
+
+func (pm *ProtocolMonitor) Stop() {
+ pm.mu.Lock()
+ defer pm.mu.Unlock()
+
+ if !pm.enabled {
+ return
+ }
+
+ pm.enabled = false
+ close(pm.stopChan)
+ pm.stopChan = make(chan struct{})
+}
+
+func (pm *ProtocolMonitor) run() {
+ ticker := time.NewTicker(5 * time.Second)
+ defer ticker.Stop()
+
+ for {
+ select {
+ case <-pm.stopChan:
+ return
+ case <-ticker.C:
+ stats, err := pm.server.GetProtocolStats(context.Background())
+ if err != nil {
+ pm.server.Log().WithError(err).Warn("failed to get protocol stats")
+ continue
+ }
+
+ pm.mu.Lock()
+ pm.stats = stats
+ pm.mu.Unlock()
+
+ if b, err := json.Marshal(stats); err == nil {
+ pm.server.Events().Publish(ProtocolStatsEvent, string(b))
+ }
+ }
+ }
+}
+
+func (pm *ProtocolMonitor) Stats() *ProtocolStats {
+ pm.mu.RLock()
+ defer pm.mu.RUnlock()
+
+ return pm.stats
+}
diff --git a/server/protocol_stats.go b/server/protocol_stats.go
new file mode 100644
index 0000000000000000000000000000000000000000..1fcc0a6b9a82d0fdcae7fe28ab07a7bf17ebf9ca
--- /dev/null
+++ b/server/protocol_stats.go
@@ -0,0 +1,135 @@
+package server
+
+import (
+ "context"
+ "fmt"
+ "os/exec"
+ "strings"
+
+ "github.com/pterodactyl/wings/environment/docker"
+)
+
+const ProtocolStatsEvent = "protocol stats"
+
+type ProtocolStats struct {
+ Total struct {
+ InPackets uint64 `json:"in_packets"`
+ OutPackets uint64 `json:"out_packets"`
+ } `json:"total"`
+ TCP struct {
+ InPackets uint64 `json:"in_packets"`
+ OutPackets uint64 `json:"out_packets"`
+ InErrors uint64 `json:"in_errors"`
+ OutErrors uint64 `json:"out_errors"`
+ } `json:"tcp"`
+ UDP struct {
+ InDatagrams uint64 `json:"in_datagrams"`
+ OutDatagrams uint64 `json:"out_datagrams"`
+ InErrors uint64 `json:"in_errors"`
+ NoPorts uint64 `json:"no_ports"`
+ } `json:"udp"`
+ ICMP struct {
+ InMsgs uint64 `json:"in_msgs"`
+ OutMsgs uint64 `json:"out_msgs"`
+ InErrors uint64 `json:"in_errors"`
+ OutErrors uint64 `json:"out_errors"`
+ } `json:"icmp"`
+ Other struct {
+ InPackets uint64 `json:"in_packets"`
+ OutPackets uint64 `json:"out_packets"`
+ } `json:"other"`
+}
+
+func (s *Server) GetProtocolStats(ctx context.Context) (*ProtocolStats, error) {
+ stats := &ProtocolStats{}
+ env, ok := s.Environment.(*docker.Environment)
+ if !ok {
+ return stats, nil
+ }
+ cmd := exec.Command("docker", "exec", env.Id, "cat", "/proc/net/dev")
+ output, err := cmd.Output()
+ if err != nil {
+ return nil, fmt.Errorf("failed to get network device stats: %v", err)
+ }
+ lines := strings.Split(string(output), "\n")
+ for _, line := range lines {
+ if strings.Contains(line, "eth0:") {
+ fields := strings.Fields(line)
+ if len(fields) >= 11 {
+ fmt.Sscanf(fields[2], "%d", &stats.Total.InPackets)
+ fmt.Sscanf(fields[10], "%d", &stats.Total.OutPackets)
+ }
+ break
+ }
+ }
+ cmd = exec.Command("docker", "exec", env.Id, "cat", "/proc/net/snmp")
+ output, err = cmd.Output()
+ if err != nil {
+ return nil, fmt.Errorf("failed to get protocol stats: %v", err)
+ }
+ lines = strings.Split(string(output), "\n")
+ for i := 0; i < len(lines)-1; i += 2 {
+ header := strings.Fields(lines[i])
+ values := strings.Fields(lines[i+1])
+
+ if len(header) == 0 || len(values) == 0 {
+ continue
+ }
+
+ switch header[0] {
+ case "Tcp:":
+ for j, field := range header[1:] {
+ switch field {
+ case "InSegs":
+ fmt.Sscanf(values[j+1], "%d", &stats.TCP.InPackets)
+ case "OutSegs":
+ fmt.Sscanf(values[j+1], "%d", &stats.TCP.OutPackets)
+ case "InErrs":
+ fmt.Sscanf(values[j+1], "%d", &stats.TCP.InErrors)
+ case "OutRsts":
+ fmt.Sscanf(values[j+1], "%d", &stats.TCP.OutErrors)
+ }
+ }
+
+ case "Udp:":
+ for j, field := range header[1:] {
+ switch field {
+ case "InDatagrams":
+ fmt.Sscanf(values[j+1], "%d", &stats.UDP.InDatagrams)
+ case "OutDatagrams":
+ fmt.Sscanf(values[j+1], "%d", &stats.UDP.OutDatagrams)
+ case "InErrors":
+ fmt.Sscanf(values[j+1], "%d", &stats.UDP.InErrors)
+ case "NoPorts":
+ fmt.Sscanf(values[j+1], "%d", &stats.UDP.NoPorts)
+ }
+ }
+
+ case "Icmp:":
+ for j, field := range header[1:] {
+ switch field {
+ case "InMsgs":
+ fmt.Sscanf(values[j+1], "%d", &stats.ICMP.InMsgs)
+ case "OutMsgs":
+ fmt.Sscanf(values[j+1], "%d", &stats.ICMP.OutMsgs)
+ case "InErrors":
+ fmt.Sscanf(values[j+1], "%d", &stats.ICMP.InErrors)
+ case "OutErrors":
+ fmt.Sscanf(values[j+1], "%d", &stats.ICMP.OutErrors)
+ }
+ }
+ }
+ }
+
+ protocolInSum := stats.TCP.InPackets + stats.UDP.InDatagrams + stats.ICMP.InMsgs
+ if protocolInSum <= stats.Total.InPackets {
+ stats.Other.InPackets = stats.Total.InPackets - protocolInSum
+ }
+
+ protocolOutSum := stats.TCP.OutPackets + stats.UDP.OutDatagrams + stats.ICMP.OutMsgs
+ if protocolOutSum <= stats.Total.OutPackets {
+ stats.Other.OutPackets = stats.Total.OutPackets - protocolOutSum
+ }
+
+ return stats, nil
+}
diff --git a/server/resources.go b/server/resources.go
index e11adf59a791aa1c34507cf0ab2e56e87bf8ddb4..d0ef20173835e88178ea4072829d9f6bc4710466 100644
--- a/server/resources.go
+++ b/server/resources.go
@@ -56,4 +56,6 @@ func (ru *ResourceUsage) Reset() {
ru.Uptime = 0
ru.Network.TxBytes = 0
ru.Network.RxBytes = 0
+ ru.Network.TxPackets = 0
+ ru.Network.RxPackets = 0
}