/
core.go
279 lines (245 loc) · 7.23 KB
/
core.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
package core
import (
"encoding/json"
"errors"
"time"
"../utils"
dapi "./docker-api"
dguard "github.com/90TechSAS/libgo-docker-guard"
)
/*
List of probe's containers
*/
var (
ContainerList map[string]*dguard.Container
DockerDriver string
)
/*
Initialize Core
*/
func Init() {
// API Client
l.Verbose("Init api-client")
InitAPIClient()
l.Verbose("api-client OK")
// Test Docker API
l.Verbose("Test Docker API")
TestDockerAPI()
l.Verbose("Docker API OK")
// Init ContainersList
ContainerList = make(map[string]*dguard.Container)
// Run HTTP Server
l.Verbose("Run HTTP Server")
go RunHTTPServer()
// Run Stats controller
go StatsController()
// Run Storage controller
go StorageController()
// Refresh container list
for {
RefreshContainerList()
time.Sleep(time.Second * time.Duration(DGConfig.DockerGuard.RefreshContainerListInterval))
}
}
/*
Test Docker API connectivity
*/
func TestDockerAPI() {
var status int // HTTP status returned
var body string // HTTP body returned
var err error // Error handling
var dockerVersion dapi.DockerVersion // DockerVersion struct
// Get /version on API
status, body = HTTPReq("/version")
if status != 200 {
l.Critical("Can't get docker version, status:", status)
}
// Parse returned json
err = json.Unmarshal([]byte(body), &dockerVersion)
if err != nil {
l.Critical("Parsing docker version error:", err)
}
// Display version infos
l.Info("Docker API connection OK:")
l.Info("\tAPIVersion:\t", dockerVersion.APIVersion)
l.Info("\tArch:\t\t", dockerVersion.Arch)
l.Info("\tExperimental:\t", dockerVersion.Experimental)
l.Info("\tGitCommit:\t", dockerVersion.GitCommit)
l.Info("\tGoVersion:\t", dockerVersion.GoVersion)
l.Info("\tKernelVersion:\t", dockerVersion.KernelVersion)
l.Info("\tOs:\t\t", dockerVersion.Os)
l.Info("\tVersion:\t", dockerVersion.Version)
}
/*
Refresh core.ContainerList
*/
func RefreshContainerList() error {
var tmpContainerArray []dapi.ContainerShort // Temporary container array
var tmpContainerList = make(map[string]*dguard.Container) // Temporary container list
var tmpContainer *dguard.Container // Temporary container
var status int // HTTP status returned
var body string // HTTP body returned
var err error // Error handling
var ok bool // map getter returned
// Get container list
l.Debug("RefreshContainerList: Get tmpContainerArray")
status, body = HTTPReq("/containers/json?all=1")
if status != 200 {
l.Error("RefreshContainerList: Can't get container list, status:", status)
return errors.New("Can't get container list, status: " + utils.I2S(status))
}
// Parse returned json
err = json.Unmarshal([]byte(body), &tmpContainerArray)
if err != nil {
l.Error("RefreshContainerList: Parsing container list error:", err)
return err
}
l.Debug("RefreshContainerList: Get tmpContainerArray OK")
// Create container list
l.Debug("RefreshContainerList: tmpContainerList")
for _, dapiContainer := range tmpContainerArray {
var tmpDAPIContainer dapi.Container // Temporary DAPI container
var tmpDGSContainer dguard.Container // Temporary DGS container
// Get container infos
status, body = HTTPReq("/containers/" + dapiContainer.ID + "/json")
if status != 200 {
l.Error("RefreshContainerList: Can't get container", dapiContainer.ID, ",status:", status)
continue
}
// Parse returned json
err = json.Unmarshal([]byte(body), &tmpDAPIContainer)
if err != nil {
l.Error("RefreshContainerList: Parsing container list error:", err)
continue
}
// Set tmpDGSContainer fields
tmpDGSContainer.ID = tmpDAPIContainer.ID
tmpDGSContainer.Hostname = tmpDAPIContainer.Config.Hostname
tmpDGSContainer.Image = tmpDAPIContainer.Config.Image
tmpDGSContainer.IPAddress = tmpDAPIContainer.NetworkSettings.IPAddress
tmpDGSContainer.MacAddress = tmpDAPIContainer.NetworkSettings.MacAddress
tmpDGSContainer.SizeRootFs = 0
tmpDGSContainer.SizeRw = 0
tmpDGSContainer.MemoryUsed = 0
tmpDGSContainer.Running = tmpDAPIContainer.State.Running
// Add tmpDGSContainer to the tmpContainerList
tmpContainerList[tmpDGSContainer.ID] = &tmpDGSContainer
// if tmpDGSContainer already exists: update status
tmpC, ok := ContainerList[tmpDGSContainer.ID]
if ok {
tmpC.Running = tmpDGSContainer.Running
ContainerResetTime(tmpDGSContainer.ID)
ContainerList[tmpDGSContainer.ID] = tmpC
}
}
l.Debug("RefreshContainerList: tmpContainerList OK")
// Check new containers
l.Debug("RefreshContainerList: Check new containers")
for _, tmpContainer = range tmpContainerList {
// If containers does not exist: add it
_, ok = ContainerList[tmpContainer.ID]
if !ok {
// Add tmpContainer in ContainerList
ContainerList[tmpContainer.ID] = tmpContainer
l.Debug("RefreshContainerList: Container", tmpContainer.ID, "added in ContainerList")
}
}
l.Debug("RefreshContainerList: Check new containers OK")
// Check removed containers
l.Debug("RefreshContainerList: Check removed containers")
for _, tmpContainer = range ContainerList {
// If containers does not exist: delete it
_, ok = tmpContainerList[tmpContainer.ID]
if !ok {
// Remove tmpContainer in ContainerList
delete(ContainerList, tmpContainer.ID)
l.Debug("RefreshContainerList: Container", tmpContainer.ID, "removed in ContainerList")
}
}
l.Debug("RefreshContainerList: Check removed containers OK")
return nil
}
/*
Set the RootFs size of a container in the ContainerList
*/
func SetContainerSizeRootFs(id string, size float64) bool {
var ok bool
_, ok = ContainerList[id]
if ok {
ContainerList[id].SizeRootFs = size
return true
}
return false
}
/*
Set the Rw size of a container in the ContainerList
*/
func SetContainerSizeRw(id string, size float64) bool {
var ok bool
_, ok = ContainerList[id]
if ok {
ContainerList[id].SizeRw = size
return true
}
return false
}
/*
Set the memory used size of a container in the ContainerList
*/
func SetContainerMemoryUsed(id string, size float64) bool {
var ok bool
_, ok = ContainerList[id]
if ok {
ContainerList[id].MemoryUsed = size
return true
}
return false
}
/*
Set the memory used size of a container in the ContainerList
*/
func ContainerResetTime(id string) bool {
var ok bool
_, ok = ContainerList[id]
if ok {
ContainerList[id].Time = float64(time.Now().Unix())
return true
}
return false
}
/*
Set the net bandwith RX of a container in the ContainerList
*/
func SetContainerNetBandwithRX(id string, size float64) bool {
var ok bool
_, ok = ContainerList[id]
if ok {
ContainerList[id].NetBandwithRX = size
return true
}
return false
}
/*
Set the net bandwith TX of a container in the ContainerList
*/
func SetContainerNetBandwithTX(id string, size float64) bool {
var ok bool
_, ok = ContainerList[id]
if ok {
ContainerList[id].NetBandwithTX = size
return true
}
return false
}
/*
Set the cpu usage of a container in the ContainerList
*/
func SetContainerCPUUsage(id string, size float64) bool {
var ok bool
_, ok = ContainerList[id]
if ok {
ContainerList[id].CPUUsage = size
return true
}
return false
}