-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (135 loc) · 3.8 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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/fatih/color"
)
func getContainerMemoryStats(cli *client.Client, containerID string) (string, error) {
stats, err := cli.ContainerStats(context.Background(), containerID, false)
if err != nil {
return "", err
}
defer stats.Body.Close()
var memUsage string
for {
var stat types.StatsJSON
if err := json.NewDecoder(stats.Body).Decode(&stat); err != nil {
return "", err
}
memUsage = fmt.Sprintf("%.2f MB", float64(stat.MemoryStats.Usage)/1024/1024)
break
}
return memUsage, nil
}
func getContainerCPUStats(cli *client.Client, containerID string) (string, error) {
stats, err := cli.ContainerStats(context.Background(), containerID, false)
if err != nil {
return "", err
}
defer stats.Body.Close()
var cpuUsage string
for {
var stat types.StatsJSON
if err := json.NewDecoder(stats.Body).Decode(&stat); err != nil {
return "", err
}
cpuUsage = fmt.Sprintf("%.2f %%", float64(stat.CPUStats.CPUUsage.TotalUsage)/float64(stat.CPUStats.SystemCPUUsage)*100)
break
}
return cpuUsage, nil
}
func listRunningContainers(cli *client.Client) ([]types.Container, error) {
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{
All: false,
Filters: filters.NewArgs(filters.Arg("status", "running")),
})
if err != nil {
return nil, err
}
return containers, nil
}
func displayMemoryStats(containers []types.Container, cli *client.Client) {
header := fmt.Sprintf("%-20s %-20s %-15s", "CONTAINER", "ID", "MEM USAGE")
color.Set(color.FgYellow, color.Bold)
fmt.Println(header)
color.Unset()
for _, container := range containers {
memUsage, err := getContainerMemoryStats(cli, container.ID)
if err != nil {
log.Printf("Error fetching stats for container %s: %v", container.ID, err)
continue
}
memStats := fmt.Sprintf("%-20s %-20s %-15s", container.Names[0], container.ID[:12], memUsage)
fmt.Println(memStats)
}
}
func displayCPUStats(containers []types.Container, cli *client.Client) {
header := fmt.Sprintf("%-20s %-20s %-15s", "CONTAINER", "ID", "CPU USAGE")
color.Set(color.FgYellow, color.Bold)
fmt.Println(header)
color.Unset()
for _, container := range containers {
cpuUsage, err := getContainerCPUStats(cli, container.ID)
if err != nil {
log.Printf("Error fetching stats for container %s: %v", container.ID, err)
continue
}
cpuStats := fmt.Sprintf("%-20s %-20s %-15s", container.Names[0], container.ID[:12], cpuUsage)
fmt.Println(cpuStats)
}
}
func displayStatsOptions() {
fmt.Println("Usage: stats <memory|cpu>")
fmt.Println("Options:")
fmt.Println(" memory - Show memory usage stats")
fmt.Println(" cpu - Show CPU usage stats")
fmt.Println(" help - Show this help message")
}
func main() {
cli, err := client.NewClientWithOpts(client.WithVersion("1.41"))
if err != nil {
log.Fatalf("Error creating Docker client: %v", err)
os.Exit(1)
}
if len(os.Args) < 2 {
displayStatsOptions()
return
}
command := os.Args[1]
containers, err := listRunningContainers(cli)
if err != nil {
log.Fatalf("Error listing containers: %v", err)
os.Exit(1)
}
switch strings.ToLower(command) {
case "stats":
if len(os.Args) == 2 {
displayStatsOptions()
} else {
option := os.Args[2]
switch option {
case "memory":
displayMemoryStats(containers, cli)
case "cpu":
displayCPUStats(containers, cli)
default:
fmt.Printf("Unknown option: %s\n", option)
displayStatsOptions()
}
}
case "stats memory":
displayMemoryStats(containers, cli)
case "stats cpu":
displayCPUStats(containers, cli)
default:
fmt.Printf("Invalid command: %s\n", command)
displayStatsOptions()
}
}