From 23353fbd48a0bbde10647c8f4282c35b5528af99 Mon Sep 17 00:00:00 2001 From: zhanghan Date: Thu, 31 Aug 2023 20:57:42 +0800 Subject: [PATCH] refactor plugin prometheus config file init --- server/.gitignore | 1 + server/global/global.go | 8 +-- server/httphandler/service/check.go | 22 ------- .../{inityaml.go => init_prometheus.go} | 32 +++++++++- server/httphandler/service/ymlhash.go | 50 ++++++++++++++++ server/main.go | 9 +-- .../scripts}/init_prometheus_yml.sh | 0 {scripts => server/scripts}/prometheus.yml | 0 server/utils/file.go | 59 +++++++++++++++++++ 9 files changed, 146 insertions(+), 35 deletions(-) delete mode 100644 server/httphandler/service/check.go rename server/httphandler/service/{inityaml.go => init_prometheus.go} (47%) create mode 100644 server/httphandler/service/ymlhash.go rename {scripts => server/scripts}/init_prometheus_yml.sh (100%) rename {scripts => server/scripts}/prometheus.yml (100%) create mode 100644 server/utils/file.go diff --git a/server/.gitignore b/server/.gitignore index 924f075..fd5f58e 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -1,5 +1,6 @@ # config file config.yml +.prometheus-yml.data # bin files server diff --git a/server/global/global.go b/server/global/global.go index 80cb700..c5641d3 100644 --- a/server/global/global.go +++ b/server/global/global.go @@ -6,11 +6,11 @@ import ( ) var ( - GlobalClient *client.Client - GlobalDB *gorm.DB - GlobalPrometheusYml string + GlobalClient *client.Client + GlobalDB *gorm.DB ) const ( - GlobalPrometheusYmlInit = "../scripts/init_prometheus_yml.sh" + GlobalPrometheusYmlInit = "./scripts/init_prometheus_yml.sh" + GlobalPrometheusYml = "/etc/prometheus/prometheus.yml" ) diff --git a/server/httphandler/service/check.go b/server/httphandler/service/check.go deleted file mode 100644 index 9f42b3d..0000000 --- a/server/httphandler/service/check.go +++ /dev/null @@ -1,22 +0,0 @@ -package service - -import ( - "errors" - "strings" - - "gitee.com/openeuler/PilotGo-plugins/sdk/logger" - "gitee.com/openeuler/PilotGo-plugins/sdk/utils/command" - "openeuler.org/PilotGo/prometheus-plugin/global" -) - -// Check if prometheus is installed -func CheckPrometheus() error { - exec := "ls /etc/prometheus/prometheus.yml /etc/prometheus/prometheus.yaml" - _, stdout, stderr, _ := command.RunCommand(exec) - if len(stdout) > 0 { - logger.Debug("prometheus already installed") - global.GlobalPrometheusYml = strings.Trim(stdout, "\n") - return nil - } - return errors.New(stderr) -} diff --git a/server/httphandler/service/inityaml.go b/server/httphandler/service/init_prometheus.go similarity index 47% rename from server/httphandler/service/inityaml.go rename to server/httphandler/service/init_prometheus.go index ae9ef9b..2a7faf2 100644 --- a/server/httphandler/service/inityaml.go +++ b/server/httphandler/service/init_prometheus.go @@ -1,12 +1,40 @@ package service import ( + "errors" + "strconv" + "strings" + "gitee.com/openeuler/PilotGo-plugins/sdk/logger" "gitee.com/openeuler/PilotGo-plugins/sdk/utils/command" "openeuler.org/PilotGo/prometheus-plugin/global" ) -func InitPrometheusYML(httpaddr string) error { +func InitPrometheus(httpaddr string) error { + checkNodeExporter := "rpm -qa |grep golang-github-prometheus-node_exporter" + _, stdout1, _, _ := command.RunCommand(checkNodeExporter) + if len(strings.Trim(stdout1, "\n")) == 0 { + return errors.New(`please use "yum install -y golang-github-prometheus-node_exporter" to install it`) + } + + checkPrometheus := `/usr/bin/prometheus --version | grep -oP '(2+\.\d+)\.\d+' | awk -F '.' '{print $2}'` + _, stdout2, _, _ := command.RunCommand(checkPrometheus) + version, _ := strconv.Atoi(strings.Trim(stdout2, "\n")) + if version <= 28 { + return errors.New(`please install prometheus greater than 2.28`) + } + + ok, err := CheckYMLHash() + if ok && err == nil { + err = initPrometheusYML(httpaddr) + if err != nil { + return err + } + } + return nil +} + +func initPrometheusYML(httpaddr string) error { if err := backup(); err != nil { return err } @@ -15,7 +43,7 @@ func InitPrometheusYML(httpaddr string) error { return err } - logger.Debug("prometheus yml init success") + logger.Debug("prometheus init success") return nil } diff --git a/server/httphandler/service/ymlhash.go b/server/httphandler/service/ymlhash.go new file mode 100644 index 0000000..d2df707 --- /dev/null +++ b/server/httphandler/service/ymlhash.go @@ -0,0 +1,50 @@ +package service + +import ( + "openeuler.org/PilotGo/prometheus-plugin/global" + "openeuler.org/PilotGo/prometheus-plugin/utils" +) + +const ymlfile = "./.prometheus-yml.data" + +func CheckYMLHash() (bool, error) { + + if !utils.IsFileExist(ymlfile) { + err := reset() + if err != nil { + return false, err + } + return true, nil + } + savedContent, err := load() + if err != nil { + return false, err + } + + currentContent, err := utils.FileReadString(global.GlobalPrometheusYml) + if err != nil { + return false, err + } + return currentContent != savedContent, nil +} + +func reset() error { + bs, err := utils.FileReadString(global.GlobalPrometheusYml) + if err != nil { + return err + } + err = utils.FileSaveString(ymlfile, bs) + if err != nil { + return err + } + return nil +} + +func load() (string, error) { + data, err := utils.FileReadString(ymlfile) + if err != nil { + return "", err + } + + return data, nil +} diff --git a/server/main.go b/server/main.go index a97d3a5..7202a05 100644 --- a/server/main.go +++ b/server/main.go @@ -24,13 +24,8 @@ func main() { os.Exit(-1) } - if err := service.CheckPrometheus(); err != nil { - logger.Error("Please confirm if prometheus is installed first: %s", err) - os.Exit(-1) - } - - if err := service.InitPrometheusYML(config.Config().Http.Addr); err != nil { - logger.Error("init prometheus yaml failed: %s", err) + if err := service.InitPrometheus(config.Config().Http.Addr); err != nil { + logger.Error("check prometheus error: %s", err) os.Exit(-1) } diff --git a/scripts/init_prometheus_yml.sh b/server/scripts/init_prometheus_yml.sh similarity index 100% rename from scripts/init_prometheus_yml.sh rename to server/scripts/init_prometheus_yml.sh diff --git a/scripts/prometheus.yml b/server/scripts/prometheus.yml similarity index 100% rename from scripts/prometheus.yml rename to server/scripts/prometheus.yml diff --git a/server/utils/file.go b/server/utils/file.go new file mode 100644 index 0000000..4a17d0a --- /dev/null +++ b/server/utils/file.go @@ -0,0 +1,59 @@ +package utils + +import ( + "io" + "os" +) + +func IsFileExist(filePath string) bool { + _, err := os.Stat(filePath) + return err == nil +} + +func FileReadString(filePath string) (string, error) { + f, err := os.Open(filePath) + defer func(file *os.File) { + // ignore file close error + file.Close() + }(f) + if err != nil { + return "", err + } + + var result []byte + readBuff := make([]byte, 1024*4) + for { + n, err := f.Read(readBuff) + if err != nil { + if err == io.EOF { + if n != 0 { + result = append(result, readBuff[:n]...) + } + break + } + return "", err + } + result = append(result, readBuff[:n]...) + } + return string(result), nil +} +func FileSaveString(filePath string, data string) error { + f, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) + if err != nil { + return err + } + + data_length := len(data) + send_count := 0 + for { + n, err := f.WriteString(data[send_count:]) + if err != nil { + return err + } + if n+send_count >= data_length { + send_count += n + break + } + } + return nil +} -- Gitee