-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
82 lines (68 loc) · 1.97 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
package main
import (
"flag"
"fmt"
"os"
"github.com/containerd/cgroups"
v1 "github.com/containerd/cgroups/stats/v1"
)
type cliOptions struct {
cgroupPath *string
onlyCPU *bool
onlyMem *bool
onlyPids *bool
onlyBlkio *bool
onlyHugetlb *bool
}
func parseFlags(flags *cliOptions) *cliOptions {
flags.cgroupPath = flag.String("path", "", "The cgroup path to read stats. Path should not include `/sys/fs/cgroup/` prefix, it should start with your own cgroups name")
flags.onlyCPU = flag.Bool("cpu", false, "show cpu stats only")
flags.onlyMem = flag.Bool("mem", false, "show mem stats only")
flags.onlyPids = flag.Bool("pids", false, "show pids stats only")
flags.onlyBlkio = flag.Bool("blkio", false, "show blkio stats only")
flags.onlyHugetlb = flag.Bool("HugeTLB", false, "show hugetlb stats only")
flag.Parse()
return flags
}
func getMetrics(cg cgroups.Cgroup) *v1.Metrics {
metrics, err := cg.Stat(cgroups.IgnoreNotExist)
if err != nil {
fmt.Println("err retrieving cgroup stats:", err)
os.Exit(1)
}
return metrics
}
func printMetrics(metrics *v1.Metrics, flags *cliOptions) {
if *flags.onlyCPU {
fmt.Printf("CPU Metrics: %+v\n", metrics.CPU)
}
if *flags.onlyMem {
fmt.Printf("Memory Metrics: %+v\n", metrics.Memory)
}
if *flags.onlyPids {
fmt.Printf("PIDs Metrics: %+v\n", metrics.Pids)
}
if *flags.onlyBlkio {
fmt.Printf("BlkIO Metrics: %+v\n", metrics.Blkio)
}
if *flags.onlyHugetlb {
fmt.Printf("HugeTLB Metrics: %+v\n", metrics.Hugetlb)
}
if !*flags.onlyCPU && !*flags.onlyMem && !*flags.onlyPids && !*flags.onlyBlkio && !*flags.onlyHugetlb {
fmt.Printf("Cgroup metrics: %+v\n", metrics)
}
}
func main() {
flags := parseFlags(new(cliOptions))
if *flags.cgroupPath == "" {
fmt.Println("Missing cgroup path")
os.Exit(1)
}
cg, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(*flags.cgroupPath))
if err != nil {
fmt.Println("loading cgroup error:", err)
os.Exit(1)
}
metrics := getMetrics(cg)
printMetrics(metrics, flags)
}