-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathagent.go
97 lines (80 loc) · 2.49 KB
/
agent.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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/pkg/profile"
"os"
"path"
"github.com/VertexC/log-formatter/agent"
"github.com/VertexC/log-formatter/agent/config"
_ "github.com/VertexC/log-formatter/agent/include"
mylogger "github.com/VertexC/log-formatter/logger"
"github.com/VertexC/log-formatter/util"
)
// Options contains the top level options of log-formatter
var options = &struct {
configFile string
monitorAddr string
rpcPort string
logDir string
verbose int
cpuProfile bool
memProfile bool
}{}
type Config struct {
Base *config.ConfigBase
LogDir string `yaml:"log" default:"logs"`
}
func init() {
flag.StringVar(&options.configFile, "c", "config.yml", "config file path")
flag.StringVar(&options.monitorAddr, "monitor", "", "monitor rpc server address, empty for standalone mode")
flag.StringVar(&options.rpcPort, "rpcp", "2020", "agent's rpc port")
flag.StringVar(&options.logDir, "l", "logs", "log directory")
flag.IntVar(&options.verbose, "v", 0, mylogger.VerboseDescription)
flag.BoolVar(&options.cpuProfile, "cpuprof", false, "enable cpu profile")
flag.BoolVar(&options.memProfile, "memprof", false, "enable mem profile")
}
func main() {
logger := mylogger.NewLogger("Main")
flag.Parse()
// prof
profiles := []func(*profile.Profile){}
if options.cpuProfile {
profiles = append(profiles, profile.CPUProfile)
}
if options.memProfile {
profiles = append(profiles, profile.MemProfile)
}
if len(profiles) != 0 {
profiles = append(profiles, profile.NoShutdownHook)
defer profile.Start(profiles...).Stop()
}
if _, err := os.Stat(options.logDir); err != nil {
if os.IsNotExist(err) {
if err := os.Mkdir(options.logDir, os.ModePerm); err != nil {
panic(fmt.Sprintf("Failed to create dir <%s>: %s", options.logDir, err))
}
} else {
panic(fmt.Sprintf("Failed to get stats of dir <%s>: %s", options.logDir, err))
}
}
mylogger.Verbose = options.verbose
mylogger.LogFile = path.Join(options.logDir, "agent.log")
// load config content
content, err := config.LoadMapStrFromYamlFile(options.configFile)
if err != nil {
panic(fmt.Sprintf("Failed to load config From File: %s", err))
}
configPretty, _ := json.MarshalIndent(content, "", " ")
logger.Info.Printf("Get config\n %s\n", configPretty)
manager, err := agent.NewAgentsManager(options.monitorAddr, options.rpcPort)
if err != nil {
panic(err)
}
if err := manager.SetConfig(content); err != nil {
panic(err)
}
manager.Run()
util.SigControl(manager.Stop)
}