-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_stats_graph.go
More file actions
135 lines (114 loc) · 2.81 KB
/
docker_stats_graph.go
File metadata and controls
135 lines (114 loc) · 2.81 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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"math"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/guptarohit/asciigraph"
)
type memoryStats struct {
Usage int `json:"usage"`
Limit int `json:"limit"`
}
type dockerStats struct {
MemStats memoryStats `json:"memory_stats"`
}
// Returns a stream of container stats json for the container with
// the provided name.
func getContainerStats(containerName string) types.ContainerStats {
ctx := context.Background()
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
cli.NegotiateAPIVersion(ctx)
containers, err := cli.ContainerList(
context.Background(), types.ContainerListOptions{})
if err != nil {
panic(err)
}
cont_id := new(bytes.Buffer)
// Get ID of container with matching name
for _, container := range containers {
var stats map[string]interface{}
resp, err := cli.ContainerStatsOneShot(ctx, container.ID)
if err != nil {
panic(err)
}
defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(&stats)
if stats["name"] == containerName {
fmt.Fprintf(cont_id, "%s", stats["id"])
}
}
resp, err := cli.ContainerStats(ctx, cont_id.String(), true)
if err != nil {
panic(err)
}
return resp
}
// Appends value to the buffer data and truncates from the
// front whenever the internal length threshold has been reached
func insertBuffer(value float64, data []float64) []float64 {
plotLength := 20
if len(data) > plotLength {
data = (data)[1:]
}
return append(data, value)
}
func getGraphOpts(contName string) []asciigraph.Option {
return []asciigraph.Option{
asciigraph.Height(15),
asciigraph.Width(75),
asciigraph.LowerBound(0),
asciigraph.SeriesColors(
asciigraph.LightCoral,
asciigraph.Turquoise,
),
asciigraph.Caption(
fmt.Sprintf(
"Memory usage for container: %s",
contName,
),
),
asciigraph.SeriesLegends(
"Memory limit", "Current usage",
),
}
}
func byteToGiB(val float64) float64 {
return val / math.Pow(1024, 3)
}
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: %s container-name\n", os.Args[0])
os.Exit(1)
}
containerName := fmt.Sprintf("/%s", os.Args[1])
containerStats := getContainerStats(containerName)
var stats dockerStats
dec := json.NewDecoder(containerStats.Body)
usageSeries := make([]float64, 0)
limitSeries := make([]float64, 0)
for dec.More() {
err := dec.Decode(&stats)
if err != nil {
panic(err)
}
usageGiB := byteToGiB(float64(stats.MemStats.Usage))
limitGiB := byteToGiB(float64(stats.MemStats.Limit))
usageSeries = insertBuffer(usageGiB, usageSeries)
limitSeries = insertBuffer(limitGiB, limitSeries)
data := [][]float64{limitSeries, usageSeries}
asciigraph.Clear()
graph := asciigraph.PlotMany(
data,
getGraphOpts(containerName)...,
)
fmt.Println(graph)
}
}