/
start.go
146 lines (130 loc) · 5.23 KB
/
start.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
package command
import (
"io/ioutil"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
"github.com/lodastack/agent/agent/agent"
"github.com/lodastack/agent/config"
"github.com/lodastack/agent/member"
"github.com/lodastack/agent/trace"
"github.com/lodastack/log"
"github.com/oiooj/cli"
)
var logBackend *log.FileBackend
var CmdStart = cli.Command{
Name: "start",
Usage: "start agent",
Description: "start agent client",
Action: runStart,
Flags: []cli.Flag{
cli.StringFlag{
Name: "f",
Value: "/etc/agent.conf",
Usage: "default config file:/etc/agent.conf",
},
cli.StringFlag{
Name: "cpuprofile",
Value: "",
Usage: "CPU pprof file",
},
cli.StringFlag{
Name: "memprofile",
Value: "",
Usage: "Memory pprof file",
},
},
}
func runStart(c *cli.Context) {
//parse config file
err := config.ParseConfig(c.String("f"))
if err != nil {
log.Fatalf("Parse Config File Error: %s", err.Error())
}
start(c, config.C)
}
func start(c *cli.Context, conf *config.Config) {
//init log setting
initLog(conf.Log)
//start agent module
a, err := agent.New(conf)
if err != nil {
log.Fatalf("New agent Error: %s", err)
}
if err := a.Start(); err != nil {
log.Fatalf("agent start failed: %s", err)
}
// Print sweet Agent logo.
printLogo()
if runtime.GOOS != "windows" {
//save pid to file
ioutil.WriteFile(config.PID, []byte(strconv.Itoa(os.Getpid())), 0644)
go notify()
}
// trace module
if config.C.Trace.Enable {
err = trace.Start(config.C.Trace.Collector, config.C.Log.Dir)
if err != nil {
log.Errorf("trace module start failed: %s", err)
}
log.Info("trace module started")
}
// member module
if config.C.Member.Enable {
if err := member.Member.Start(config.C.Member.Nodes, config.C.Member.Key); err != nil {
log.Errorf("member module start failed: %s", err)
}
log.Info("member module started")
}
//pprof
startProfile(c.String("cpuprofile"), c.String("memprofile"))
select {}
}
func initLog(conf config.LogConfig) {
var err error
logBackend, err = log.NewFileBackend(conf.Dir)
if err != nil {
log.Fatalf("failed to new log backend:%v\n", err)
}
log.SetLogging(conf.Level, logBackend)
logBackend.Rotate(conf.Logrotatenum, conf.Logrotatesize)
}
func notify() {
message := make(chan os.Signal, 1)
signal.Notify(message, syscall.SIGINT, syscall.SIGKILL, os.Interrupt)
<-message
log.Info("receive signal, exit...")
logBackend.Flush()
stopProfile()
os.Exit(0)
}
func printLogo() {
log.Printf(logo)
}
const logo = `
AAA tttt
A:::A ttt:::t
A:::::A t:::::t
A:::::::A t:::::t
A:::::::::A ggggggggg ggggg eeeeeeeeeeee nnnn nnnnnnnn ttttttt:::::ttttttt
A:::::A:::::A g:::::::::ggg::::g ee::::::::::::ee n:::nn::::::::nn t:::::::::::::::::t
A:::::A A:::::A g:::::::::::::::::g e::::::eeeee:::::een::::::::::::::nn t:::::::::::::::::t
A:::::A A:::::A g::::::ggggg::::::gge::::::e e:::::enn:::::::::::::::ntttttt:::::::tttttt
A:::::A A:::::A g:::::g g:::::g e:::::::eeeee::::::e n:::::nnnn:::::n t:::::t
A:::::AAAAAAAAA:::::A g:::::g g:::::g e:::::::::::::::::e n::::n n::::n t:::::t
A:::::::::::::::::::::A g:::::g g:::::g e::::::eeeeeeeeeee n::::n n::::n t:::::t
A:::::AAAAAAAAAAAAA:::::A g::::::g g:::::g e:::::::e n::::n n::::n t:::::t tttttt
A:::::A A:::::Ag:::::::ggggg:::::g e::::::::e n::::n n::::n t::::::tttt:::::t
A:::::A A:::::Ag::::::::::::::::g e::::::::eeeeeeee n::::n n::::n tt::::::::::::::t
A:::::A A:::::Agg::::::::::::::g ee:::::::::::::e n::::n n::::n tt:::::::::::tt
AAAAAAA AAAAAAA gggggggg::::::g eeeeeeeeeeeeee nnnnnn nnnnnn ttttttttttt
g:::::g
gggggg g:::::g
g:::::gg gg:::::g
g::::::ggg:::::::g
gg:::::::::::::g
ggg::::::ggg
gggggg
`