From b0c2e8ae211ce26b0fc684bdbbc46836bd51e772 Mon Sep 17 00:00:00 2001 From: zhanghan2021 Date: Mon, 19 Jun 2023 20:14:30 +0800 Subject: [PATCH] add init prometheus yml scripts and prometheus.yml template --- prometheus/scripts/init_prometheus_yml.sh | 24 ++++++ prometheus/scripts/prometheus.yml | 11 +++ prometheus/server/global/global.go | 4 + .../server/httphandler/service/check.go | 11 +-- .../server/httphandler/service/inityaml.go | 38 +++++++++ .../httphandler/service/yaml/inityaml.go | 25 ------ .../server/httphandler/service/yaml/model.go | 16 ---- .../server/httphandler/service/yaml/yaml.go | 81 ------------------- prometheus/server/main.go | 13 ++- 9 files changed, 89 insertions(+), 134 deletions(-) create mode 100644 prometheus/scripts/init_prometheus_yml.sh create mode 100644 prometheus/scripts/prometheus.yml create mode 100644 prometheus/server/httphandler/service/inityaml.go delete mode 100644 prometheus/server/httphandler/service/yaml/inityaml.go delete mode 100644 prometheus/server/httphandler/service/yaml/model.go delete mode 100644 prometheus/server/httphandler/service/yaml/yaml.go diff --git a/prometheus/scripts/init_prometheus_yml.sh b/prometheus/scripts/init_prometheus_yml.sh new file mode 100644 index 00000000..ed9bf3da --- /dev/null +++ b/prometheus/scripts/init_prometheus_yml.sh @@ -0,0 +1,24 @@ +inityml(){ +# init prometheus yml +cat>$2< 0 { - fmt.Println("prometheus already installed") - global.GlobalPrometheusYml = stdout + logger.Debug("prometheus already installed") + global.GlobalPrometheusYml = strings.Trim(stdout, "\n") return nil } - return errors.New(stderr + err.Error()) + return errors.New(stderr) } diff --git a/prometheus/server/httphandler/service/inityaml.go b/prometheus/server/httphandler/service/inityaml.go new file mode 100644 index 00000000..ae9ef9bd --- /dev/null +++ b/prometheus/server/httphandler/service/inityaml.go @@ -0,0 +1,38 @@ +package service + +import ( + "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 { + if err := backup(); err != nil { + return err + } + + if err := initYML(httpaddr); err != nil { + return err + } + + logger.Debug("prometheus yml init success") + return nil +} + +func backup() error { + cmd := "cp " + global.GlobalPrometheusYml + " " + global.GlobalPrometheusYml + ".bak" + exitcode, _, stderr, err := command.RunCommand(cmd) + if exitcode == 0 && stderr == "" && err == nil { + return nil + } + return err +} + +func initYML(httaddr string) error { + cmd := "sh " + global.GlobalPrometheusYmlInit + " " + httaddr + " " + global.GlobalPrometheusYml + exitcode, _, stderr, err := command.RunCommand(cmd) + if exitcode == 0 && stderr == "" && err == nil { + return nil + } + return err +} diff --git a/prometheus/server/httphandler/service/yaml/inityaml.go b/prometheus/server/httphandler/service/yaml/inityaml.go deleted file mode 100644 index aabf0f51..00000000 --- a/prometheus/server/httphandler/service/yaml/inityaml.go +++ /dev/null @@ -1,25 +0,0 @@ -package yaml - -import ( - "openeuler.org/PilotGo/prometheus-plugin/config" -) - -func InitPrometheusYML(conf *config.Prometheus) error { - if err := BackupPrometheusYML(); err != nil { - return err - } - - if err := UpdatePrometheusYML(conf.AlertYaml); err != nil { - return err - } - - updateErr, rollebackErr := InitVerificationOrRollBack() - if updateErr != nil { - return updateErr - } - if rollebackErr != nil { - return rollebackErr - } - - return nil -} diff --git a/prometheus/server/httphandler/service/yaml/model.go b/prometheus/server/httphandler/service/yaml/model.go deleted file mode 100644 index 851f4431..00000000 --- a/prometheus/server/httphandler/service/yaml/model.go +++ /dev/null @@ -1,16 +0,0 @@ -package yaml - -type PrometheusYML struct { - Global struct { - ScrapeInterval string `yaml:"scrape_interval"` - EvaluationInterval string `yaml:"evalution_interval"` - } `yaml:"global"` - RuleFiles []string `yaml:"rule_files"` - ScrapeConfigs []struct { - JobName string `yaml:"job_name"` - HTTPSdConfigs []struct { - Url string `yaml:"url"` - RefreshInterval string `yaml:"refresh_interval"` - } `yaml:"http_sd_configs"` - } `yaml:"scrape_configs"` -} diff --git a/prometheus/server/httphandler/service/yaml/yaml.go b/prometheus/server/httphandler/service/yaml/yaml.go deleted file mode 100644 index c386547d..00000000 --- a/prometheus/server/httphandler/service/yaml/yaml.go +++ /dev/null @@ -1,81 +0,0 @@ -package yaml - -import ( - "errors" - "os" - - "gitee.com/openeuler/PilotGo-plugins/sdk/utils/command" - "gopkg.in/yaml.v2" - "openeuler.org/PilotGo/prometheus-plugin/global" -) - -func BackupPrometheusYML() error { - cmd := "cp " + global.GlobalPrometheusYml + " " + global.GlobalPrometheusYml + ".bak" - exitcode, _, stderr, err := command.RunCommand(cmd) - if exitcode == 0 && stderr == "" && err == nil { - return nil - } - return err -} - -func UpdatePrometheusYML(rulePath string) error { - YML := PrometheusYML{ - Global: struct { - ScrapeInterval string "yaml:\"scrape_interval\"" - EvaluationInterval string "yaml:\"evalution_interval\"" - }{ - ScrapeInterval: "15s", - EvaluationInterval: "15s"}, - RuleFiles: []string{rulePath}, - ScrapeConfigs: []struct { - JobName string "yaml:\"job_name\"" - HTTPSdConfigs []struct { - Url string "yaml:\"url\"" - RefreshInterval string "yaml:\"refresh_interval\"" - } "yaml:\"http_sd_configs\"" - }{ - { - JobName: "node_exporter", - HTTPSdConfigs: []struct { - Url string "yaml:\"url\"" - RefreshInterval string "yaml:\"refresh_interval\"" - }{ - { - Url: "http://192", - RefreshInterval: "60s", - }, - }, - }, - }, - } - - f, err := os.OpenFile(global.GlobalPrometheusYml, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666) - if err != nil { - return err - } - defer f.Close() - yaml.FutureLineWrap() - encoder := yaml.NewEncoder(f) - - err = encoder.Encode(&YML) - if err != nil { - return err - } - return nil -} - -func InitVerificationOrRollBack() (error, error) { - cmd1 := "systemctl restart prometheus" - exitcode1, _, stderr1, err1 := command.RunCommand(cmd1) - if exitcode1 == 0 && stderr1 == "" && err1 == nil { - return nil, nil - } - - cmd2 := "cp " + global.GlobalPrometheusYml + ".bak" + " " + global.GlobalPrometheusYml - exitcode2, _, stderr2, err2 := command.RunCommand(cmd2) - if exitcode2 == 0 && stderr2 == "" && err2 == nil { - return errors.New("There is an error in prometheus yml: %s" + err1.Error()), nil - } - - return err1, errors.New("prometheus yml rollback has failed:%s" + err2.Error()) -} diff --git a/prometheus/server/main.go b/prometheus/server/main.go index bc8bad67..a97d3a5e 100644 --- a/prometheus/server/main.go +++ b/prometheus/server/main.go @@ -10,7 +10,6 @@ import ( "openeuler.org/PilotGo/prometheus-plugin/db" "openeuler.org/PilotGo/prometheus-plugin/global" "openeuler.org/PilotGo/prometheus-plugin/httphandler/service" - yaml "openeuler.org/PilotGo/prometheus-plugin/httphandler/service/yaml" "openeuler.org/PilotGo/prometheus-plugin/plugin" "openeuler.org/PilotGo/prometheus-plugin/router" ) @@ -20,18 +19,18 @@ func main() { config.Init() - if err := service.CheckPrometheus(); err != nil { - fmt.Printf("Please confirm if prometheus is installed first: %s", err) + if err := logger.Init(config.Config().Logopts); err != nil { + fmt.Printf("logger init failed, please check the config file: %s", err) os.Exit(-1) } - if err := yaml.InitPrometheusYML(config.Config().Prometheus); err != nil { - fmt.Printf("init prometheus yaml failed: %s", err) + if err := service.CheckPrometheus(); err != nil { + logger.Error("Please confirm if prometheus is installed first: %s", err) os.Exit(-1) } - if err := logger.Init(config.Config().Logopts); err != nil { - fmt.Printf("logger init failed, please check the config file: %s", err) + if err := service.InitPrometheusYML(config.Config().Http.Addr); err != nil { + logger.Error("init prometheus yaml failed: %s", err) os.Exit(-1) } -- Gitee