-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
125 lines (104 loc) · 3.29 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
package main
//*************************************************************
//
//
// 这里只是程序的引导代码,具体处理直接看 agentd.go 的 Main()方法
//
// Too Long , Don't Read !
//*************************************************************
import (
"flag"
"fmt"
"github.com/BurntSushi/toml"
"github.com/domac/mafio/agent"
"github.com/domac/mafio/register"
"github.com/domac/mafio/version"
"github.com/judwhite/go-svc/svc"
"github.com/mreiferson/go-options"
"log"
"os"
"path/filepath"
"runtime"
"syscall"
)
//程序启动参数
var (
flagSet = flag.NewFlagSet("mafio", flag.ExitOnError)
showVersion = flagSet.Bool("version", false, "print version string") //版本
//httpAddress = flagSet.String("http-address", "0.0.0.0:10630", "<addr>:<port> to listen on for HTTP clients") //http定义地址
httpAddress = flagSet.String("http-address", "", "<addr>:<port> to listen on for HTTP clients") //http定义地址
config = flagSet.String("config", "", "path to config file")
MaxReadChannelSize = flagSet.Int("max-read-channel-size", 4096, "max readChannel size")
MaxWriteChannelSize = flagSet.Int("max-write-channel-size", 4096, "max writeChannel size")
MaxWriteBulkSize = flagSet.Int("max-write-bulk-size", 500, "max writeBulk size")
sendInterval = flagSet.Int("send-interval", 500, "send data interval (ms)")
AgentId = flagSet.String("m-id", "sky01", "the service name which ectd can find it")
AgentGroup = flagSet.String("m-group", "net01", "the service group which agent work on")
Input = flagSet.String("input", "stdin", "input plugin")
Outout = flagSet.String("output", "stdout", "output plugin")
Filter = flagSet.String("filter", "valid", "filter plugin")
FilePath = flagSet.String("filepath", "", "use for file watch")
InfluxDBAddr = flagSet.String("influxdb-addr", "", "influxDB addr to metrics")
formatStr = flagSet.String("f", "", "function string")
)
//程序封装
type program struct {
Agentd *agent.Agentd
}
//框架初始化
func (p *program) Init(env svc.Environment) error {
if env.IsWindowsService() {
//切换工作目录
dir := filepath.Dir(os.Args[0])
return os.Chdir(dir)
}
return nil
}
//Agent的参数使用说明
//go run main.go -h 展示的内容
func agentUsage() {
fmt.Printf("%s\n", version.Show())
flagSet.PrintDefaults()
}
//程序启动
func (p *program) Start() error {
flagSet.Usage = agentUsage
flagSet.Parse(os.Args[1:])
if *showVersion {
fmt.Println(version.Verbose("mafio"))
os.Exit(0)
}
fmt.Println(version.Show())
var cfg map[string]interface{}
if *config != "" {
_, err := toml.DecodeFile(*config, &cfg)
if err != nil {
log.Fatalf("ERROR: failed to load config file %s - %s", *config, err.Error())
}
}
opts := agent.NewOptions(*config)
options.Resolve(opts, flagSet, cfg)
//初始化插件注册
register.Init()
pluginsConf := cfg["plugins_config_paths"]
//后台进程创建
daemon := agent.New(opts, pluginsConf)
daemon.Main()
p.Agentd = daemon
return nil
}
//程序停止
func (p *program) Stop() error {
if p.Agentd != nil {
p.Agentd.Exit()
}
return nil
}
//引导程序
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
prg := &program{}
if err := svc.Run(prg, syscall.SIGINT, syscall.SIGTERM); err != nil {
log.Fatal(err)
}
}