-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdockerstats.go
69 lines (64 loc) · 2.46 KB
/
dockerstats.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
// Package dockerstats provides the ability to get currently running Docker container statistics,
// including memory and CPU usage.
//
// To get the statistics of running Docker containers, you can use the `Current()` function:
//
// stats, err := dockerstats.Current()
// if err != nil {
// panic(err)
// }
//
// for _, s := range stats {
// fmt.Println(s.Container) // 9f2656020722
// fmt.Println(s.Memory) // {Raw=221.7 MiB / 7.787 GiB, Percent=2.78%}
// fmt.Println(s.CPU) // 99.79%
// }
//
// Alternatively, you can use the `NewMonitor()` function to receive a constant stream of Docker container stats,
// available on the Monitor's `Stream` channel:
//
// m := dockerstats.NewMonitor()
//
// for res := range m.Stream {
// if res.Error != nil {
// panic(err)
// }
//
// for _, s := range res.Stats {
// fmt.Println(s.Container) // 9f2656020722
// }
// }
package dockerstats
import "runtime"
const (
defaultDockerPath string = "/usr/bin/docker"
macOSDockerPath string = "/usr/local/bin/docker"
defaultDockerCommand string = "stats"
defaultDockerNoStreamArg string = "--no-stream"
defaultDockerFormatArg string = "--format"
defaultDockerFormat string = `{"container":"{{.Container}}","memory":{"raw":"{{.MemUsage}}","percent":"{{.MemPerc}}"},"cpu":"{{.CPUPerc}}","io":{"network":"{{.NetIO}}","block":"{{.BlockIO}}"},"pids":{{.PIDs}}}`
)
// DefaultCommunicator is the default way of retrieving stats from Docker.
//
// When calling `Current()`, the `DefaultCommunicator` is used, and when
// retriving a `Monitor` using `NewMonitor()`, it is initialized with the
// `DefaultCommunicator`.
var DefaultCommunicator Communicator = CliCommunicator{
DockerPath: defaultDockerPath,
Command: []string{defaultDockerCommand, defaultDockerNoStreamArg, defaultDockerFormatArg, defaultDockerFormat},
}
var MacOSCommunicator Communicator = CliCommunicator{
DockerPath: macOSDockerPath,
Command: []string{defaultDockerCommand, defaultDockerNoStreamArg, defaultDockerFormatArg, defaultDockerFormat},
}
// Current returns the current `Stats` of each running Docker container.
//
// Current will always return a `[]Stats` slice equal in length to the number of
// running Docker containers, or an `error`. No error is returned if there are no
// running Docker containers, simply an empty slice.
func Current() ([]Stats, error) {
if runtime.GOOS == "darwin" {
return MacOSCommunicator.Stats()
}
return DefaultCommunicator.Stats()
}