forked from KyleBanks/dockerstats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
communicator.go
45 lines (37 loc) · 919 Bytes
/
communicator.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
package dockerstats
import (
"encoding/json"
"os/exec"
"strings"
)
// Communicator provides an interface for communicating with and retrieving stats
// from Docker.
type Communicator interface {
Stats() ([]Stats, error)
}
// CliCommunicator uses the Docker CLI to retrieve stats for currently running Docker
// containers.
type CliCommunicator struct {
DockerPath string
Command []string
}
// Stats returns Docker container statistics using the Docker CLI.
func (c CliCommunicator) Stats() ([]Stats, error) {
out, err := exec.Command(c.DockerPath, c.Command...).Output()
if err != nil {
return nil, err
}
containers := strings.Split(string(out), "\n")
stats := make([]Stats, 0)
for _, con := range containers {
if len(con) == 0 {
continue
}
var s Stats
if err := json.Unmarshal([]byte(con), &s); err != nil {
return nil, err
}
stats = append(stats, s)
}
return stats, nil
}