This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.go
127 lines (120 loc) · 2.95 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
package main
import (
"fmt"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"github.com/jet/damon/container"
"github.com/jet/damon/log"
"github.com/jet/damon/metrics"
"github.com/jet/damon/version"
"github.com/jet/damon/win32"
)
func main() {
// Limit Damon to 1 CPU
runtime.GOMAXPROCS(1)
vinfo := version.GetInfo()
if len(os.Args) < 2 {
// print version and exit - no args
fmt.Println(vinfo.FullString(true))
os.Exit(0)
}
var cmd *exec.Cmd
if len(os.Args) > 2 {
cmd = exec.Command(os.Args[1], os.Args[2:]...)
} else {
cmd = exec.Command(os.Args[1])
}
lcfg := LogConfigFromEnvironment()
fields := NomadLogFields()
logger, err := log.NewLogger(lcfg)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
logger = logger.WithFields(fields)
logger.WithFields(map[string]interface{}{
"version": vinfo,
"revision": version.GitCommit,
"cmdline": os.Args,
}).Logln("damon starting")
clogger := logger.WithFields(map[string]interface{}{
"version": vinfo.String(),
"revision": version.GitCommit,
"cmdline": os.Args,
})
ccfg, err := LoadContainerConfigFromEnvironment()
if err != nil {
logger.Error(err, "unable to load container configuration from environment variables")
}
win32.SetLogger(logger)
resources := win32.GetSystemResources()
labels := make(map[string]string)
for k, v := range fields {
labels[k] = fmt.Sprintf("%v", v)
}
m := metrics.Metrics{
Cores: resources.CPUNumCores,
MHzPerCore: resources.CPUMhzPercore,
CPULimitHz: float64(ccfg.CPUMHzLimit * 1000000),
MemoryLimitBytes: float64(ccfg.MemoryMBLimit * 1024 * 1024),
Namespace: "damon",
Labels: labels,
}
m.Init()
c := container.Container{
Command: cmd,
Config: ccfg,
Logger: clogger,
OnStats: func(s container.ProcessStats) {
m.OnStats(s)
},
OnViolation: func(v container.LimitViolation) {
m.OnViolation(v)
},
}
if err := c.Start(); err != nil {
logger.Error(err, "damon startup error")
os.Exit(1)
}
exitCh := make(chan struct{})
sigCh := make(chan os.Signal)
signal.Notify(sigCh)
go func() {
<-sigCh
close(exitCh)
}()
if addr := ListenAddress(); addr != "" {
go func() {
endpoint := MetricsEndpoint()
mux := http.NewServeMux()
mux.Handle(endpoint, m.Handler())
srv := &http.Server{
Addr: addr,
Handler: mux,
}
logger.Logf("metrics on http://%s/%s", addr, endpoint)
logger.Error(srv.ListenAndServe(), "error closing http server")
}()
}
pr, err := c.Wait(exitCh)
if err != nil {
logger.WithFields(map[string]interface{}{
"version": vinfo,
"revision": version.GitCommit,
"cmdline": os.Args,
}).Error(err, "process exited with an error")
}
logger.WithFields(map[string]interface{}{
"version": vinfo,
"revision": version.GitCommit,
"cmdline": os.Args,
"start": pr.Start,
"end": pr.End,
"run_time": pr.End.Sub(pr.Start),
"exit_status": pr.ExitCode,
}).Logln("damon exiting")
os.Exit(pr.ExitCode)
}