-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.go
96 lines (85 loc) · 2.73 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
package main
import (
"flag"
"fmt"
"os"
"strconv"
"github.com/mhausenblas/cinf/namespaces"
)
const (
BANNER = ` ___ ___ ___
/\__\ /\ \ /\__\
/:/ / ___ \:\ \ /:/ _/_
/:/ / /\__\ \:\ \ /:/ /\__\
/:/ / ___ /:/__/ _____\:\ \ /:/ /:/ /
/:/__/ /\__\/::\ \ /::::::::\__\/:/_/:/ /
\:\ \ /:/ /\/\:\ \__\:\~~\~~\/__/\:\/:/ /
\:\ /:/ / ~~\:\/\__\\:\ \ \::/__/
\:\/:/ / \::/ / \:\ \ \:\ \
\::/ / /:/ / \:\__\ \:\__\
\/__/ \/__/ \/__/ \/__/
`
)
var (
DEBUG bool
version string
showversion bool
targetns string
targetpid string
cgspec string
monspec string
logspec string
)
func debug(m string) {
if DEBUG {
fmt.Printf("DEBUG: %s\n", m)
}
}
func about() {
fmt.Printf(BANNER)
fmt.Printf("\nThis is cinf in version %s\n", version)
fmt.Print("See also https://github.com/mhausenblas/cinf\n\n")
}
func init() {
flag.BoolVar(&showversion, "version", false, "List info about cinf, including its version.")
flag.StringVar(&targetns, "namespace", "", "List details about namespace with provided ID. You can get the namespace ID by running cinf without arguments.")
flag.StringVar(&targetpid, "pid", "", "List namespaces the process with provided process ID is in.")
flag.StringVar(&cgspec, "cgroup", "", "List details of a cgroup a process belongs to. Format is PID:CGROUP_HIERARCHY, for example 1000:2.")
flag.StringVar(&monspec, "mon", "", "Monitor process with provided process ID/control file(s). Format is PID:CF1,CF2,… for example 1000:memory.usage_in_bytes")
flag.StringVar(&logspec, "log", "", "Continuously output namespace and cgroups metrics. Format is OUTPUT_DEF:INTERVAL, for example RAW:200 or HTTP:3000")
flag.Usage = func() {
fmt.Printf("Usage: %s [args]\n\n", os.Args[0])
fmt.Println("Arguments:")
flag.PrintDefaults()
}
flag.Parse()
DEBUG = false
if envd := os.Getenv("DEBUG"); envd != "" {
if d, err := strconv.ParseBool(envd); err == nil {
DEBUG = d
}
}
}
func main() {
namespaces.DEBUG = DEBUG
debug("=== SHOWING DEBUG MESSAGES ===")
if showversion {
about()
os.Exit(0)
}
namespaces.Gather()
switch {
case targetns != "": // we have a --namespace flag
namespaces.LookupNS(targetns)
case targetpid != "": // we have a --pid flag
namespaces.LookupPID(targetpid)
case cgspec != "": // we have a --cgroup flag
namespaces.LookupCG(cgspec)
case monspec != "": // we have a --mon flag
namespaces.MonitorPID(monspec)
case logspec != "": // we have a --log flag
namespaces.DoMetrics(logspec)
default: // list all active namespaces
namespaces.Showall()
}
}