This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
/
common.go
341 lines (318 loc) · 10.6 KB
/
common.go
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package stats
import (
"bufio"
"encoding/json"
"fmt"
jwt "github.com/dgrijalva/jwt-go"
"github.com/shirou/gopsutil/mem"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
"strings"
"time"
)
func pathParts(path string) []string {
path = strings.TrimPrefix(path, "/")
path = strings.TrimSuffix(path, "/")
return strings.Split(path, "/")
}
func parseRequestToken(tokenString string, parsedPublicKey interface{}) (*jwt.Token, error) {
if tokenString == "" {
return nil, fmt.Errorf("No JWT token provided")
}
return jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return parsedPublicKey, nil
})
}
func getContainerStats(reader *bufio.Reader, count int, id string, pid int) (containerInfo, error) {
i, err := getDockerContainerInfo(reader, count, id, pid)
return i, err
}
type DockerStats struct {
Read time.Time `json:"read"`
PidsStats struct {
Current int64 `json:"current"`
} `json:"pids_stats"`
Networks map[string]struct {
RxBytes int64 `json:"rx_bytes"`
RxDropped int64 `json:"rx_dropped"`
RxErrors int64 `json:"rx_errors"`
RxPackets int64 `json:"rx_packets"`
TxBytes int64 `json:"tx_bytes"`
TxDropped int64 `json:"tx_dropped"`
TxErrors int64 `json:"tx_errors"`
TxPackets int64 `json:"tx_packets"`
} `json:"networks"`
BlkioStats struct {
IoServiceBytesRecursive []struct {
Major int64 `json:"major"`
Minor int64 `json:"minor"`
Op string `json:"op"`
Value int64 `json:"value"`
} `json:"io_service_bytes_recursive"`
IoServicedRecursive []struct {
Major int64 `json:"major"`
Minor int64 `json:"minor"`
Op string `json:"op"`
Value int64 `json:"value"`
} `json:"io_serviced_recursive"`
IoQueueRecursive []struct {
Major int64 `json:"major"`
Minor int64 `json:"minor"`
Op string `json:"op"`
Value int64 `json:"value"`
} `json:"io_queue_recursive"`
IoServiceTimeRecursive []struct {
Major int64 `json:"major"`
Minor int64 `json:"minor"`
Op string `json:"op"`
Value int64 `json:"value"`
} `json:"io_service_time_recursive"`
IoWaitTimeRecursive []struct {
Major int64 `json:"major"`
Minor int64 `json:"minor"`
Op string `json:"op"`
Value int64 `json:"value"`
} `json:"io_wait_time_recursive"`
IoMergedRecursive []struct {
Major int64 `json:"major"`
Minor int64 `json:"minor"`
Op string `json:"op"`
Value int64 `json:"value"`
} `json:"io_merged_recursive"`
IoTimeRecursive []struct {
Major int64 `json:"major"`
Minor int64 `json:"minor"`
Op string `json:"op"`
Value int64 `json:"value"`
} `json:"io_time_recursive"`
SectorsRecursive []struct {
Major int64 `json:"major"`
Minor int64 `json:"minor"`
Op string `json:"op"`
Value int64 `json:"value"`
} `json:"sectors_recursive"`
} `json:"blkio_stats"`
MemoryStats struct {
Stats struct {
TotalPgmajfault int64 `json:"total_pgmajfault"`
Cache int64 `json:"cache"`
MappedFile int64 `json:"mapped_file"`
TotalInactiveFile int64 `json:"total_inactive_file"`
Pgpgout int64 `json:"pgpgout"`
Rss int64 `json:"rss"`
TotalMappedFile int64 `json:"total_mapped_file"`
Writeback int64 `json:"writeback"`
Unevictable int64 `json:"unevictable"`
Pgpgin int64 `json:"pgpgin"`
TotalUnevictable int64 `json:"total_unevictable"`
Pgmajfault int64 `json:"pgmajfault"`
TotalRss int64 `json:"total_rss"`
TotalRssHuge int64 `json:"total_rss_huge"`
TotalWriteback int64 `json:"total_writeback"`
TotalInactiveAnon int64 `json:"total_inactive_anon"`
RssHuge int64 `json:"rss_huge"`
HierarchicalMemoryLimit uint64 `json:"hierarchical_memory_limit"`
TotalPgfault int64 `json:"total_pgfault"`
TotalActiveFile int64 `json:"total_active_file"`
ActiveAnon int64 `json:"active_anon"`
TotalActiveAnon int64 `json:"total_active_anon"`
TotalPgpgout int64 `json:"total_pgpgout"`
TotalCache int64 `json:"total_cache"`
InactiveAnon int64 `json:"inactive_anon"`
ActiveFile int64 `json:"active_file"`
Pgfault int64 `json:"pgfault"`
InactiveFile int64 `json:"inactive_file"`
TotalPgpgin int64 `json:"total_pgpgin"`
} `json:"stats"`
MaxUsage int64 `json:"max_usage"`
Usage int64 `json:"usage"`
Failcnt int64 `json:"failcnt"`
Limit int64 `json:"limit"`
} `json:"memory_stats"`
CPUStats struct {
CPUUsage struct {
PercpuUsage []int64 `json:"percpu_usage"`
UsageInUsermode int64 `json:"usage_in_usermode"`
TotalUsage int64 `json:"total_usage"`
UsageInKernelmode int64 `json:"usage_in_kernelmode"`
} `json:"cpu_usage"`
SystemCPUUsage int64 `json:"system_cpu_usage"`
ThrottlingData struct {
Periods int64 `json:"periods"`
ThrottledPeriods int64 `json:"throttled_periods"`
ThrottledTime int64 `json:"throttled_time"`
} `json:"throttling_data"`
} `json:"cpu_stats"`
PrecpuStats struct {
CPUUsage struct {
PercpuUsage []int64 `json:"percpu_usage"`
UsageInUsermode int64 `json:"usage_in_usermode"`
TotalUsage int64 `json:"total_usage"`
UsageInKernelmode int64 `json:"usage_in_kernelmode"`
} `json:"cpu_usage"`
SystemCPUUsage int64 `json:"system_cpu_usage"`
ThrottlingData struct {
Periods int64 `json:"periods"`
ThrottledPeriods int64 `json:"throttled_periods"`
ThrottledTime int64 `json:"throttled_time"`
} `json:"throttling_data"`
} `json:"precpu_stats"`
}
type containerInfo struct {
Id string
Stats []*containerStats
}
type containerStats struct {
Timestamp time.Time `json:"timestamp"`
Cpu CpuStats `json:"cpu,omitempty"`
DiskIo DiskIoStats `json:"diskio,omitempty"`
Network NetworkStats `json:"network,omitempty"`
Memory MemoryStats `json:"memory,omitempty"`
}
type CpuStats struct {
Usage CpuUsage `json:"usage"`
}
type CpuUsage struct {
// Total CPU usage.
// Units: nanoseconds
Total uint64 `json:"total"`
// Per CPU/core usage of the container.
// Unit: nanoseconds.
PerCpu []uint64 `json:"per_cpu_usage,omitempty"`
// Time spent in user space.
// Unit: nanoseconds
User uint64 `json:"user"`
// Time spent in kernel space.
// Unit: nanoseconds
System uint64 `json:"system"`
}
type DiskIoStats struct {
IoServiceBytes []PerDiskStats `json:"io_service_bytes,omitempty"`
}
type PerDiskStats struct {
Major uint64 `json:"major"`
Minor uint64 `json:"minor"`
Stats map[string]uint64 `json:"stats"`
}
type NetworkStats struct {
InterfaceStats
Interfaces []InterfaceStats `json:"interfaces,omitempty"`
}
type MemoryStats struct {
// Current memory usage, this includes all memory regardless of when it was
// accessed.
// Units: Bytes.
Usage uint64 `json:"usage"`
}
type InterfaceStats struct {
// The name of the interface.
Name string `json:"name"`
// Cumulative count of bytes received.
RxBytes uint64 `json:"rx_bytes"`
// Cumulative count of packets received.
RxPackets uint64 `json:"rx_packets"`
// Cumulative count of receive errors encountered.
RxErrors uint64 `json:"rx_errors"`
// Cumulative count of packets dropped while receiving.
RxDropped uint64 `json:"rx_dropped"`
// Cumulative count of bytes transmitted.
TxBytes uint64 `json:"tx_bytes"`
// Cumulative count of packets transmitted.
TxPackets uint64 `json:"tx_packets"`
// Cumulative count of transmit errors encountered.
TxErrors uint64 `json:"tx_errors"`
// Cumulative count of packets dropped while transmitting.
TxDropped uint64 `json:"tx_dropped"`
}
func getContainerInfo(reader *bufio.Reader, count int, id string, pid int) (containerInfo, error) {
contInfo := containerInfo{}
contInfo.Id = id
stats := []*containerStats{}
for i := 0; i < count; i++ {
str, err := reader.ReadString([]byte("\n")[0])
if err != nil {
return containerInfo{}, err
}
dockerStats, err := FromString(str)
if err != nil {
return containerInfo{}, err
}
contStats := convertDockerStats(dockerStats, pid)
stats = append(stats, contStats)
}
contInfo.Stats = stats
return contInfo, nil
}
func convertDockerStats(stats DockerStats, pid int) *containerStats {
containerStats := containerStats{}
containerStats.Timestamp = stats.Read
containerStats.Cpu.Usage.Total = uint64(stats.CPUStats.CPUUsage.TotalUsage)
containerStats.Cpu.Usage.PerCpu = []uint64{}
for _, value := range stats.CPUStats.CPUUsage.PercpuUsage {
containerStats.Cpu.Usage.PerCpu = append(containerStats.Cpu.Usage.PerCpu, uint64(value))
}
containerStats.Cpu.Usage.System = uint64(stats.CPUStats.CPUUsage.UsageInKernelmode)
containerStats.Cpu.Usage.User = uint64(stats.CPUStats.CPUUsage.UsageInKernelmode)
containerStats.Memory.Usage = uint64(stats.MemoryStats.Usage)
containerStats.Network.Interfaces = []InterfaceStats{}
for name, netStats := range getLinkStats(pid) {
data := InterfaceStats{}
data.Name = name
data.RxBytes = uint64(netStats.RxBytes)
data.RxDropped = uint64(netStats.RxDropped)
data.RxErrors = uint64(netStats.RxErrors)
data.RxPackets = uint64(netStats.RxPackets)
data.TxBytes = uint64(netStats.TxBytes)
data.TxDropped = uint64(netStats.TxDropped)
data.TxPackets = uint64(netStats.TxPackets)
data.TxErrors = uint64(netStats.TxErrors)
containerStats.Network.Interfaces = append(containerStats.Network.Interfaces, data)
}
containerStats.DiskIo.IoServiceBytes = []PerDiskStats{}
for _, diskStats := range stats.BlkioStats.IoServiceBytesRecursive {
data := PerDiskStats{}
data.Stats = map[string]uint64{}
data.Stats[diskStats.Op] = uint64(diskStats.Value)
containerStats.DiskIo.IoServiceBytes = append(containerStats.DiskIo.IoServiceBytes, data)
}
return &containerStats
}
func FromString(rawstring string) (DockerStats, error) {
obj := DockerStats{}
err := json.Unmarshal([]byte(rawstring), &obj)
if err != nil {
return obj, err
}
return obj, nil
}
func getMemCapcity() (uint64, error) {
data, err := mem.VirtualMemory()
if err != nil {
return 0, err
}
return data.Total, nil
}
func getLinkStats(pid int) map[string]*netlink.LinkStatistics {
ret := map[string]*netlink.LinkStatistics{}
nsHandler, err := netns.GetFromPid(pid)
if err != nil {
return nil
}
defer nsHandler.Close()
handler, err := netlink.NewHandleAt(nsHandler)
if err != nil {
return nil
}
defer handler.Delete()
links, err := handler.LinkList()
if err != nil {
return nil
}
for _, link := range links {
attr := link.Attrs()
if attr.Name != "lo" {
ret[attr.Name] = attr.Statistics
}
}
return ret
}