-
Notifications
You must be signed in to change notification settings - Fork 439
/
init.go
105 lines (90 loc) · 3.75 KB
/
init.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
package common
import (
"flag"
"fmt"
"github.com/bytedance/Elkeid/server/agent_center/common/kafka"
"github.com/bytedance/Elkeid/server/agent_center/common/userconfig"
"github.com/bytedance/Elkeid/server/agent_center/common/ylog"
"os"
"strings"
)
func init() {
confPath := flag.String("c", "conf/svr.yml", "ConfigPath")
flag.Parse()
ConfPath = *confPath
initConfig()
}
func initConfig() {
var err error
if UserConfig, err = userconfig.NewUserConfig(userconfig.WithPath(ConfPath)); err != nil {
fmt.Printf("####LOAD_CONFIG_ERROR: %v", err)
os.Exit(-1)
}
initLog()
initDefault()
initComponents()
}
func initDefault() {
var err error
LocalIP, err = GetOutboundIP()
if err != nil {
ylog.Fatalf("init", "GET_LOCALIP_ERROR: %s Error: %v", LocalIP, err)
}
SSLKeyFile = UserConfig.GetString("server.ssl.keyfile")
SSLCertFile = UserConfig.GetString("server.ssl.certfile")
SSLRawDataKeyFile = UserConfig.GetString("server.ssl.rawdata_keyfile")
SSLRawDataCertFile = UserConfig.GetString("server.ssl.rawdata_certfile")
SSLCaFile = UserConfig.GetString("server.ssl.cafile")
if SSLKeyFile == "" || SSLCertFile == "" || SSLCaFile == "" || SSLRawDataKeyFile == "" || SSLRawDataCertFile == "" {
ylog.Fatalf("init", "ssl file empty SSLKeyFile:%s SSLCertFile:%s SSLCaFile:%s SSLRawDataKeyFile:%s SSLRawDataCertFile:%s", SSLKeyFile, SSLCertFile, SSLCaFile, SSLRawDataKeyFile, SSLRawDataCertFile)
}
SvrName = UserConfig.GetString("sd.name")
SdAddrs = UserConfig.GetStringSlice("sd.addrs")
SvrAK = strings.ToLower(UserConfig.GetString("sd.auth.ak"))
SvrSK = UserConfig.GetString("sd.auth.sk")
ManageAddrs = UserConfig.GetStringSlice("manage.addrs")
GRPCPort = UserConfig.GetInt("server.grpc.port")
ConnLimit = UserConfig.GetInt("server.grpc.connlimit")
HttpPort = UserConfig.GetInt("server.http.port")
HttpSSLEnable = UserConfig.GetBool("server.http.ssl.enable")
HttpAuthEnable = UserConfig.GetBool("server.http.auth.enable")
HttpAkSkMap = UserConfig.GetStringMapString("server.http.auth.aksk")
PProfEnable = UserConfig.GetBool("server.pprof.enable")
PProfPort = UserConfig.GetInt("server.pprof.port")
RawDataPort = UserConfig.GetInt("server.rawdata.port")
}
func initLog() {
logLevel := UserConfig.GetInt("server.log.applog.loglevel")
logPath := UserConfig.GetString("server.log.applog.path")
logger := ylog.NewYLog(
ylog.WithLogFile(logPath),
ylog.WithMaxAge(3),
ylog.WithMaxSize(10),
ylog.WithMaxBackups(3),
ylog.WithLevel(logLevel),
)
ylog.InitLogger(logger)
}
func initComponents() {
var (
err error
)
//kafka
kafkaAddr := UserConfig.GetStringSlice("kafka.addrs")
kafkaTopic := UserConfig.GetString("kafka.topic")
RawdataTopic := UserConfig.GetString("kafka.rawdata_topic")
kafkaLog := UserConfig.GetString("kafka.logpath")
kafkaEnable := UserConfig.GetBool("kafka.sasl.enable")
kafkaUser := UserConfig.GetString("kafka.sasl.username")
kafkaPassword := UserConfig.GetString("kafka.sasl.password")
ylog.Infof("InitComponents", "KAFKA Producer: %v - %v", kafkaAddr, kafkaTopic)
if KafkaProducer, err = kafka.NewProducerWithLog(kafkaAddr, kafkaTopic, fmt.Sprintf("sarama-%s", LocalIP), kafkaLog, kafkaUser, kafkaPassword, kafkaEnable); err != nil {
fmt.Printf("#### %s %s CONNECT_KAFKA_ERROR: %v", kafkaAddr, kafkaTopic, err)
ylog.Fatalf("InitComponents", "%s %s CONNECT_KAFKA_ERROR: %v", kafkaAddr, kafkaTopic, err)
}
ylog.Infof("InitComponents", "KAFKA Producer: %v - %v", kafkaAddr, RawdataTopic)
if KafkaRawDataProducer, err = kafka.NewProducerWithLog(kafkaAddr, RawdataTopic, fmt.Sprintf("sarama-%s", LocalIP), kafkaLog, kafkaUser, kafkaPassword, kafkaEnable); err != nil {
fmt.Printf("#### %s %s CONNECT_KAFKA_ERROR: %v", kafkaAddr, RawdataTopic, err)
ylog.Fatalf("InitComponents", "%s %s CONNECT_KAFKA_ERROR: %v", kafkaAddr, RawdataTopic, err)
}
}