-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
316 lines (285 loc) · 11 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
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
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"strconv"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
containerPrefix = "container_"
dataPrefix = "container_data_"
)
var (
knownContainerIDs map[string]prometheus.Labels
knownContainerNetworks map[string]prometheus.Labels
knownContainerDiskStats map[string]prometheus.Labels
knownContainerInfos map[string]prometheus.Labels
knownDataNames map[string]prometheus.Labels
pids *prometheus.GaugeVec
cpuUsageUser *prometheus.GaugeVec
cpuUsageKernel *prometheus.GaugeVec
cpuUsageTotal *prometheus.GaugeVec
memoryUsage *prometheus.GaugeVec
memoryLimit *prometheus.GaugeVec
networkReceiveBytes *prometheus.GaugeVec
networkTransmitBytes *prometheus.GaugeVec
networkReceivePackets *prometheus.GaugeVec
networkTransmitPackets *prometheus.GaugeVec
networkReceiveErrors *prometheus.GaugeVec
networkTransmitErrors *prometheus.GaugeVec
networkReceiveDropped *prometheus.GaugeVec
networkTransmitDropped *prometheus.GaugeVec
diskIOBytes *prometheus.GaugeVec
containerInfo *prometheus.GaugeVec
)
func setup() {
containerLabels := []string{"container_name", "compose_project", "compose_service"}
containerNetworkLabels := append(containerLabels, "interface")
containerDiskLabels := append(containerLabels, "op")
containerInfoLabels := []string{
"container_id",
"container_name",
"compose_project",
"compose_service",
"container_image_id",
"container_image_name",
"container_state",
"container_state_running",
"container_state_paused",
"container_state_restarting",
"container_state_oomkilled",
"container_state_dead",
}
pids = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "pids",
Help: "Number of running processes in the container",
}, containerLabels)
cpuUsageUser = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "cpu_usage_user_seconds_total",
Help: "Container CPU usage in user mode",
}, containerLabels)
cpuUsageKernel = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "cpu_usage_kernel_seconds_total",
Help: "Container CPU usage in kernel mode",
}, containerLabels)
cpuUsageTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "cpu_usage_seconds_total",
Help: "Container CPU usage",
}, containerLabels)
cpuUsageTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "cpu_usage_seconds_total",
Help: "Container CPU usage",
}, containerLabels)
memoryUsage = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "memory_usage_bytes",
Help: "Container Memory usage",
}, containerLabels)
memoryLimit = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "memory_limit_bytes",
Help: "Container Memory limit",
}, containerLabels)
networkReceiveBytes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "network_receive_bytes_total",
Help: "Container network received bytes",
}, containerNetworkLabels)
networkTransmitBytes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "network_transmit_bytes_total",
Help: "Container network transmitted bytes",
}, containerNetworkLabels)
networkReceivePackets = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "network_receive_packets_total",
Help: "Container network received packets",
}, containerNetworkLabels)
networkTransmitPackets = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "network_transmit_packets_total",
Help: "Container network transmitted packets",
}, containerNetworkLabels)
networkReceiveErrors = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "network_receive_errors_total",
Help: "Container network receive errors",
}, containerNetworkLabels)
networkTransmitErrors = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "network_transmit_errors_total",
Help: "Container network transmit errors",
}, containerNetworkLabels)
networkReceiveDropped = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "network_receive_dropped_total",
Help: "Container network receive drops",
}, containerNetworkLabels)
networkTransmitDropped = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "network_transmit_dropped_total",
Help: "Container network transmit drops",
}, containerNetworkLabels)
diskIOBytes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "disk_io_bytes",
Help: "Container disk IO bytes",
}, containerDiskLabels)
containerInfo = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: containerPrefix + "info",
Help: "Container info",
}, containerInfoLabels)
prometheus.MustRegister(pids)
prometheus.MustRegister(cpuUsageUser)
prometheus.MustRegister(cpuUsageKernel)
prometheus.MustRegister(cpuUsageTotal)
prometheus.MustRegister(memoryUsage)
prometheus.MustRegister(memoryLimit)
prometheus.MustRegister(networkReceiveBytes)
prometheus.MustRegister(networkTransmitBytes)
prometheus.MustRegister(networkReceivePackets)
prometheus.MustRegister(networkTransmitPackets)
prometheus.MustRegister(networkReceiveErrors)
prometheus.MustRegister(networkTransmitErrors)
prometheus.MustRegister(networkReceiveDropped)
prometheus.MustRegister(networkTransmitDropped)
prometheus.MustRegister(diskIOBytes)
prometheus.MustRegister(containerInfo)
}
func updateContainers(docker *client.Client) {
newKnownContainerIDs := make(map[string]prometheus.Labels)
newKnownContainerNetworks := make(map[string]prometheus.Labels)
newKnownContainerDiskStats := make(map[string]prometheus.Labels)
newKnownContainerInfos := make(map[string]prometheus.Labels)
containers, err := docker.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
log.Print("Failed to get container list: ", err)
}
for _, container := range containers {
inspect, err := docker.ContainerInspect(context.Background(), container.ID)
if err != nil {
log.Print("Failed to inspect container: ", err)
continue
}
resp, err := docker.ContainerStatsOneShot(context.Background(), container.ID)
if err != nil {
log.Print("Failed to get container stats: ", err)
continue
}
stats := types.StatsJSON{}
err = json.NewDecoder(resp.Body).Decode(&stats)
if err != nil {
log.Print("Failed to parse container stats: ", err)
continue
}
resp.Body.Close()
// General data
{
labels := prometheus.Labels{
"container_name": strings.TrimPrefix(container.Names[0], "/"),
"compose_project": container.Labels["com.docker.compose.project"],
"compose_service": container.Labels["com.docker.compose.service"],
}
newKnownContainerIDs[container.ID] = labels
pids.With(labels).Set(float64(stats.PidsStats.Current))
cpuUsageUser.With(labels).Set(float64(stats.CPUStats.CPUUsage.UsageInUsermode) / 1e9)
cpuUsageKernel.With(labels).Set(float64(stats.CPUStats.CPUUsage.UsageInKernelmode) / 1e9)
cpuUsageTotal.With(labels).Set(float64(stats.CPUStats.CPUUsage.TotalUsage) / 1e9)
memoryUsage.With(labels).Set(float64(stats.MemoryStats.Usage - stats.MemoryStats.Stats["cache"]))
memoryLimit.With(labels).Set(float64(stats.MemoryStats.Limit))
}
// Networks
for intf, net := range stats.Networks {
labels := prometheus.Labels{
"container_name": strings.TrimPrefix(container.Names[0], "/"),
"compose_project": container.Labels["com.docker.compose.project"],
"compose_service": container.Labels["com.docker.compose.service"],
"interface": intf,
}
newKnownContainerNetworks[container.ID+intf] = labels
networkReceiveBytes.With(labels).Set(float64(net.RxBytes))
networkTransmitBytes.With(labels).Set(float64(net.TxBytes))
networkReceivePackets.With(labels).Set(float64(net.RxPackets))
networkTransmitPackets.With(labels).Set(float64(net.TxPackets))
networkReceiveErrors.With(labels).Set(float64(net.RxErrors))
networkTransmitErrors.With(labels).Set(float64(net.TxErrors))
networkReceiveDropped.With(labels).Set(float64(net.RxDropped))
networkTransmitDropped.With(labels).Set(float64(net.TxDropped))
}
// Disk IO
for _, stat := range stats.BlkioStats.IoServiceBytesRecursive {
labels := prometheus.Labels{
"container_name": strings.TrimPrefix(container.Names[0], "/"),
"compose_project": container.Labels["com.docker.compose.project"],
"compose_service": container.Labels["com.docker.compose.service"],
"op": stat.Op,
}
newKnownContainerDiskStats[container.ID+"bytes"+stat.Op] = labels
diskIOBytes.With(labels).Set(float64(stat.Value))
}
// Container info
{
labels := prometheus.Labels{
"container_id": container.ID,
"container_name": strings.TrimPrefix(container.Names[0], "/"),
"compose_project": container.Labels["com.docker.compose.project"],
"compose_service": container.Labels["com.docker.compose.service"],
"container_image_id": strings.TrimPrefix(container.ImageID, "sha256:"),
"container_image_name": container.Image,
"container_state": container.State,
"container_state_running": strconv.FormatBool(inspect.State.Running),
"container_state_paused": strconv.FormatBool(inspect.State.Paused),
"container_state_restarting": strconv.FormatBool(inspect.State.Restarting),
"container_state_oomkilled": strconv.FormatBool(inspect.State.OOMKilled),
"container_state_dead": strconv.FormatBool(inspect.State.Dead),
}
s, _ := json.Marshal(labels)
newKnownContainerInfos[string(s)] = labels
containerInfo.With(labels).Set(1)
}
}
for id, labels := range knownContainerIDs {
if newKnownContainerIDs[id] == nil {
pids.Delete(labels)
cpuUsageUser.Delete(labels)
cpuUsageKernel.Delete(labels)
cpuUsageTotal.Delete(labels)
memoryUsage.Delete(labels)
memoryLimit.Delete(labels)
}
}
for id, labels := range knownContainerNetworks {
if newKnownContainerNetworks[id] == nil {
networkReceiveBytes.Delete(labels)
networkTransmitBytes.Delete(labels)
networkReceivePackets.Delete(labels)
networkTransmitPackets.Delete(labels)
networkReceiveErrors.Delete(labels)
networkTransmitErrors.Delete(labels)
networkReceiveDropped.Delete(labels)
networkTransmitDropped.Delete(labels)
}
}
for id, labels := range knownContainerDiskStats {
if newKnownContainerDiskStats[id] == nil {
diskIOBytes.Delete(labels)
}
}
for id, labels := range knownContainerInfos {
if newKnownContainerInfos[id] == nil {
containerInfo.Delete(labels)
}
}
knownContainerIDs = newKnownContainerIDs
knownContainerNetworks = newKnownContainerNetworks
knownContainerInfos = newKnownContainerInfos
}
func main() {
docker, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
setup()
go func() {
for {
updateContainers(docker)
}
}()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)
}