From 985168a16eca9a2532697482144be9157e2f066c Mon Sep 17 00:00:00 2001 From: Wangjunqi123 Date: Fri, 24 May 2024 09:20:34 +0800 Subject: [PATCH] add configuration module to the elk plugin --- elk/conf/config.go | 58 +++++++++++++++++++++++++++++++++++++++++++ elk/conf/meta.go | 25 +++++++++++++++++++ elk/elk.yaml.templete | 20 +++++++++++++++ elk/main.go | 11 ++++++-- 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 elk/conf/config.go create mode 100644 elk/conf/meta.go create mode 100644 elk/elk.yaml.templete diff --git a/elk/conf/config.go b/elk/conf/config.go new file mode 100644 index 00000000..c39b65b6 --- /dev/null +++ b/elk/conf/config.go @@ -0,0 +1,58 @@ +package conf + +import ( + "flag" + "fmt" + "os" + "path" + + "gitee.com/openeuler/PilotGo-plugin-elk/global" + "gitee.com/openeuler/PilotGo/sdk/logger" + "github.com/pkg/errors" + "gopkg.in/yaml.v2" +) + +var Global_Config *ServerConfig + +const config_type = "elk.yaml" + +var config_dir string + +type ServerConfig struct { + Elk *ElkConf + PilotGo *PilotGoConf + Logopts *logger.LogOpts `yaml:"log"` + Elasticsearch *ElasticConf + Logstash *LogstashConf + Kibana *KibanaConf +} + +func ConfigFile() string { + configfilepath := path.Join(config_dir, config_type) + + return configfilepath +} + +func InitConfig() { + flag.StringVar(&config_dir, "conf", "/opt/PilotGo/plugin/elk", "elk configuration directory") + flag.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage: %s -conf /path/to/elk.yaml(default:/opt/PilotGo/plugin/elk) \n", os.Args[0]) + } + flag.Parse() + + bytes, err := global.FileReadBytes(ConfigFile()) + if err != nil { + err = errors.Wrapf(err, "open file failed: %s, %s", ConfigFile(), err.Error()) // err top + fmt.Printf("%+v\n", err) + os.Exit(1) + } + + Global_Config = &ServerConfig{} + + err = yaml.Unmarshal(bytes, Global_Config) + if err != nil { + err = errors.Errorf("yaml unmarshal failed: %s", err.Error()) // err top + fmt.Printf("%+v\n", err) + os.Exit(1) + } +} diff --git a/elk/conf/meta.go b/elk/conf/meta.go new file mode 100644 index 00000000..1ed19bfd --- /dev/null +++ b/elk/conf/meta.go @@ -0,0 +1,25 @@ +package conf + +type ElkConf struct { + Addr string `yaml:"http_addr"` +} + +type PilotGoConf struct { + Addr string `yaml:"http_addr"` +} + +type ElasticConf struct { + Addr string `yaml:"http_addr"` + Username string `yaml:"username"` + Password string `yaml:"password"` +} + +type LogstashConf struct { + Addr string `yaml:"http_addr"` +} + +type KibanaConf struct { + Addr string `yaml:"http_addr"` + Username string `yaml:"username"` + Password string `yaml:"password"` +} diff --git a/elk/elk.yaml.templete b/elk/elk.yaml.templete new file mode 100644 index 00000000..3eca47ed --- /dev/null +++ b/elk/elk.yaml.templete @@ -0,0 +1,20 @@ +elk: + http_addr: "localhost:9993" +PilotGo: + http_addr: "localhost:8888" +log: + level: debug + driver: file # 可选stdout和file。stdout:输出到终端控制台;file:输出到path下的指定文件。 + path: /opt/PilotGo/plugin/topology/server/log/toposerver.log + max_file: 1 + max_size: 10485760 +elasticsearch: + http_addr: "localhost:9200" + username: "" + password: "" +logstash: + http_addr: "localhost:9600" +kibana: + http_addr: "localhost:5601" + username: "" + password: "" diff --git a/elk/main.go b/elk/main.go index d3103f97..0fddfe8c 100644 --- a/elk/main.go +++ b/elk/main.go @@ -1,5 +1,12 @@ package main +import ( + "gitee.com/openeuler/PilotGo-plugin-elk/conf" +) + func main() { - -} \ No newline at end of file + /* + init config + */ + conf.InitConfig() +} -- Gitee