-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
212 lines (185 loc) · 5.4 KB
/
main.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
package main
import (
"context"
"fmt"
"bytes"
"net/http"
"encoding/json"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
// "github.com/moby/moby/api/types/swarm"
)
type StatsJson struct {
/* Id string `json:"id"`
Read string `json:"read"`
Preread string `json:"preread"` */
CpuStats cpu `json:"cpu_stats"`
PreCpuStats cpu `json:"precpu_stats"`
Name string `json:"name"`
MemStats mem `json:"memory_stats"`
NetStats map[string]net `json:"networks"`
BulkIoStats bulkio `json:"blkio_stats"`
}
type bulkio struct {
Iops []io `json:"io_serviced_recursive"`
}
type io struct {
Major int `json:"major"`
Minor int `json:"minor"`
Op string `json:"op"`
Value uint64 `json:"value"`
}
type cpu struct {
Usage cpuUsage `json:"cpu_usage"`
SystemUsage float64 `json:"system_cpu_usage"`
}
type cpuUsage struct {
Total float64 `json:"total_usage"`
PerCPU []float64 `json:"percpu_usage"`
}
type mem struct {
MaxUsage float64 `json:"max_usage"`
Limit float64 `json:"limit"`
}
type net struct {
RxBytes float64 `json:"rx_bytes"`
RxDropped float64 `json:"rx_dropped"`
TxBytes float64 `json:"tx_bytes"`
TxDropped float64 `json:"tx_dropped"`
}
type Result struct {
Mem float64 `json:"mem_pct"`
CPU float64 `json:"total_cpu_pct"`
NetRx float64 `json:"rx_bytes"`
NetRd float64 `json:"rx_dropped"`
NetTx float64 `json:"tx_bytes"`
NetTd float64 `json:"tx_dropped"`
IoRead float64 `json:"iops_read"`
IoWrite float64 `json:"iops_write"`
}
type ResultSet map[string]Result
// error check alias
func Check(err error) {
if err != nil {
fmt.Println(err)
}
}
func main() {
ctx := context.Background()
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
type totaliops struct {
readiops uint64
writeiops uint64
}
previo := make(map[string]totaliops)
prevnet := make(map[string]net)
var lastT time.Time = time.Now()
for {
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
panic(err)
}
curT := time.Now()
curDur := curT.Sub(lastT).Seconds()
rs := make(ResultSet)
for _, container := range containers {
// fmt.Println(container.ID)
// fmt.Println(container.Names,container.Created,container.HostConfig)
stats, err := cli.ContainerStats(ctx, container.ID, false)
Check(err)
/*
rr := stats.Body
buf := new(bytes.Buffer)
buf.ReadFrom(rr)
s := buf.String() // Does a complete co py of the bytes in the buffer.
fmt.Println("stats\n",s,"\n")
*/
decoder := json.NewDecoder(stats.Body)
var containerStats StatsJson
err = decoder.Decode(&containerStats)
Check(err)
//parse cpu
system_delta := containerStats.CpuStats.SystemUsage - containerStats.PreCpuStats.SystemUsage
var cpu_delta_sum float64 = 0
var cpu_delta float64
for i := 0; i < len(containerStats.CpuStats.Usage.PerCPU); i++ {
cpu_delta = containerStats.CpuStats.Usage.PerCPU[i] - containerStats.PreCpuStats.Usage.PerCPU[i]
cpu_delta_sum += cpu_delta
}
totalCPU := cpu_delta_sum / system_delta
// parse mem
maxMem := containerStats.MemStats.MaxUsage/containerStats.MemStats.Limit
// parse net
var sumRxB float64 = 0
var sumRxD float64 = 0
var sumTxB float64 = 0
var sumTxD float64 = 0
for _, netstat := range containerStats.NetStats {
sumRxB += netstat.RxBytes
sumRxD += netstat.RxDropped
sumTxB += netstat.TxBytes
sumTxD += netstat.TxDropped
}
// convert net bw into per second
var rxB float64 = 0
var rxD float64 = 0
var txB float64 = 0
var txD float64 = 0
prevNetEntry, ok := prevnet[containerStats.Name]
if ok {
rxB = float64(sumRxB - prevNetEntry.RxBytes)/curDur
rxD = float64(sumRxD - prevNetEntry.RxDropped)/curDur
txB = float64(sumTxB - prevNetEntry.TxBytes)/curDur
txD = float64(sumTxD - prevNetEntry.TxDropped)/curDur
}
prevnet[containerStats.Name] = net{sumRxB, sumRxD, sumTxB, sumTxD,}
// parse iops
var sumReadIops uint64
var sumWriteIops uint64
for _, ioentry := range containerStats.BulkIoStats.Iops {
switch ioentry.Op {
case "Read":
sumReadIops += ioentry.Value
case "Write":
sumWriteIops += ioentry.Value
}
}
// convert iops into per second
var readiops float64 = 0
var writeiops float64 = 0
preventry, ok := previo[containerStats.Name]
if ok {
readiops = float64(sumReadIops - preventry.readiops)/curDur
writeiops = float64(sumWriteIops - preventry.writeiops)/curDur
}
previo[containerStats.Name] = totaliops{sumReadIops, sumWriteIops, }
rs[containerStats.Name] = Result{
Mem: maxMem,
CPU: totalCPU,
NetRx: rxB,
NetRd: rxD,
NetTx: txB,
NetTd: txD,
IoRead: readiops,
IoWrite: writeiops,
}
stats.Body.Close()
}
//fmt.Println(rs)
d, err := json.Marshal(rs)
Check(err)
url := "http://robinhood_stat_server/pututils"
resp, err := http.Post(url, "application/json", bytes.NewBuffer(d))
Check(err)
if err == nil {
resp.Body.Close()
}
// update timestamp and sleep 1 second
lastT = curT
time.Sleep(time.Second * 1)
}
}