From 46f7ec7af93f559e41f46c399f1c9c4049ed2af5 Mon Sep 17 00:00:00 2001 From: 710leo <710leo@gmail.com> Date: Thu, 15 Apr 2021 19:35:25 +0800 Subject: [PATCH] complete version information --- src/modules/agentd/agentd.go | 23 +++++++++- src/modules/agentd/config/config.go | 18 +------- src/modules/server/config/config.go | 21 +-------- src/modules/server/plugins/all/dlopen.go | 4 -- src/modules/server/server.go | 54 +++++++++++++++++++++++- 5 files changed, 77 insertions(+), 43 deletions(-) diff --git a/src/modules/agentd/agentd.go b/src/modules/agentd/agentd.go index df349684..3f25627a 100644 --- a/src/modules/agentd/agentd.go +++ b/src/modules/agentd/agentd.go @@ -24,6 +24,7 @@ import ( "github.com/didi/nightingale/v4/src/modules/agentd/timer" "github.com/didi/nightingale/v4/src/modules/agentd/udp" + "github.com/toolkits/file" "github.com/toolkits/pkg/logger" "github.com/toolkits/pkg/runner" ) @@ -58,6 +59,7 @@ func init() { } func main() { + aconf() parseConf() loggeri.Init(config.Config.Logger) @@ -127,12 +129,31 @@ func monStart() { } func parseConf() { - if err := config.Parse(); err != nil { + if err := config.Parse(*conf); err != nil { fmt.Println("cannot parse configuration file:", err) os.Exit(1) } } +func aconf() { + if *conf != "" && file.IsExist(*conf) { + return + } + + *conf = "etc/agentd.local.yml" + if file.IsExist(*conf) { + return + } + + *conf = "etc/agentd.yml" + if file.IsExist(*conf) { + return + } + + fmt.Println("no configuration file for server") + os.Exit(1) +} + func endingProc() { c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) diff --git a/src/modules/agentd/config/config.go b/src/modules/agentd/config/config.go index 23ae87cd..df8a7b01 100644 --- a/src/modules/agentd/config/config.go +++ b/src/modules/agentd/config/config.go @@ -82,9 +82,7 @@ var ( Endpoint string ) -func Parse() error { - conf := getYmlFile() - +func Parse(conf string) error { bs, err := file.ReadBytes(conf) if err != nil { return fmt.Errorf("cannot read yml[%s]: %v", conf, err) @@ -164,17 +162,3 @@ func Parse() error { return nil } - -func getYmlFile() string { - yml := "etc/agentd.local.yml" - if file.IsExist(yml) { - return yml - } - - yml = "etc/agentd.yml" - if file.IsExist(yml) { - return yml - } - - return "" -} diff --git a/src/modules/server/config/config.go b/src/modules/server/config/config.go index 14a2541e..4c38cc9f 100644 --- a/src/modules/server/config/config.go +++ b/src/modules/server/config/config.go @@ -208,12 +208,7 @@ var Config *ConfigT var Ident string // Parse configuration file -func Parse() error { - ymlFile := getYmlFile() - if ymlFile == "" { - return fmt.Errorf("configuration file etc/server.yml not found") - } - +func Parse(ymlFile string) error { bs, err := file.ReadBytes(ymlFile) if err != nil { return fmt.Errorf("cannot read yml[%s]: %v", ymlFile, err) @@ -407,20 +402,6 @@ func Parse() error { return identity.Parse() } -func getYmlFile() string { - yml := "etc/server.local.yml" - if file.IsExist(yml) { - return yml - } - - yml = "etc/server.yml" - if file.IsExist(yml) { - return yml - } - - return "" -} - // map["node"]="host1,host2" --> map["node"]=["host1", "host2"] func formatClusterItems(cluster map[string]string) map[string]*tsdb.ClusterNode { ret := make(map[string]*tsdb.ClusterNode) diff --git a/src/modules/server/plugins/all/dlopen.go b/src/modules/server/plugins/all/dlopen.go index 6e279bc5..3848ad13 100644 --- a/src/modules/server/plugins/all/dlopen.go +++ b/src/modules/server/plugins/all/dlopen.go @@ -6,8 +6,6 @@ import ( "path/filepath" "plugin" "strings" - - "github.com/toolkits/pkg/logger" ) const pluginDir = "plugins" @@ -15,14 +13,12 @@ const pluginDir = "plugins" func init() { plugins, err := listPlugins(pluginDir) if err != nil { - logger.Warningf("list plugins: %s", err) return } for _, file := range plugins { _, err := plugin.Open(file) if err != nil { - logger.Warningf("plugin.Open %s err %s", file, err) continue } } diff --git a/src/modules/server/server.go b/src/modules/server/server.go index 5a76f028..169d13d1 100644 --- a/src/modules/server/server.go +++ b/src/modules/server/server.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "log" "os" @@ -36,11 +37,43 @@ import ( _ "github.com/didi/nightingale/v4/src/modules/server/plugins/api" _ "github.com/go-sql-driver/mysql" + "github.com/toolkits/file" pcache "github.com/toolkits/pkg/cache" "github.com/toolkits/pkg/logger" + "github.com/toolkits/pkg/runner" ) +var ( + vers *bool + help *bool + conf *string + + version = "No Version Provided" +) + +func init() { + vers = flag.Bool("v", false, "display the version.") + help = flag.Bool("h", false, "print this help.") + conf = flag.String("f", "", "specify configuration file.") + flag.Parse() + + if *vers { + fmt.Println("Version:", version) + os.Exit(0) + } + + if *help { + flag.Usage() + os.Exit(0) + } + + runner.Init() + fmt.Println("runner.cwd:", runner.Cwd) + fmt.Println("runner.hostname:", runner.Hostname) +} + func main() { + aconf() parseConf() conf := config.Config @@ -146,12 +179,31 @@ func main() { } func parseConf() { - if err := config.Parse(); err != nil { + if err := config.Parse(*conf); err != nil { fmt.Println("cannot parse configuration file:", err) os.Exit(1) } } +func aconf() { + if *conf != "" && file.IsExist(*conf) { + return + } + + *conf = "etc/server.local.yml" + if file.IsExist(*conf) { + return + } + + *conf = "etc/server.yml" + if file.IsExist(*conf) { + return + } + + fmt.Println("no configuration file for server") + os.Exit(1) +} + func checkIdentity() { ip, err := identity.GetIP() if err != nil {