From 9e4511674e0f5a49a4f1b57f3c4e9f3f4b2b7955 Mon Sep 17 00:00:00 2001 From: zhanghan2021 Date: Sat, 17 Jun 2023 19:19:24 +0800 Subject: [PATCH] init prometheus yaml --- prometheus/server/config.yml.templete | 1 + prometheus/server/config/config.go | 1 + prometheus/server/global/global.go | 5 +- .../server/httphandler/service/check.go | 2 + .../httphandler/service/yaml/inityaml.go | 25 ++++++ .../server/httphandler/service/yaml/model.go | 16 ++++ .../server/httphandler/service/yaml/yaml.go | 81 +++++++++++++++++++ prometheus/server/main.go | 8 +- sdk/go.mod | 2 +- .../klauspost/cpuid/v2/CONTRIBUTING.txt | 70 ++++++++-------- 10 files changed, 172 insertions(+), 39 deletions(-) create mode 100644 prometheus/server/httphandler/service/yaml/inityaml.go create mode 100644 prometheus/server/httphandler/service/yaml/model.go create mode 100644 prometheus/server/httphandler/service/yaml/yaml.go diff --git a/prometheus/server/config.yml.templete b/prometheus/server/config.yml.templete index cefac625..3b20ddfd 100644 --- a/prometheus/server/config.yml.templete +++ b/prometheus/server/config.yml.templete @@ -2,6 +2,7 @@ prometheus: url: "http://localhost:8090/plugin/Prometheus" plugin_type: "micro-app" reverseDest: "http://localhost:9090" + alertYaml: "/etc/prometheus/rules.yaml" http: addr: "0.0.0.0:8090" log: diff --git a/prometheus/server/config/config.go b/prometheus/server/config/config.go index 97fac6e5..2f4cbb30 100644 --- a/prometheus/server/config/config.go +++ b/prometheus/server/config/config.go @@ -13,6 +13,7 @@ type Prometheus struct { URL string `yaml:"url"` PluginType string `yaml:"plugin_type"` ReverseDest string `yaml:"reverseDest"` + AlertYaml string `yaml:"alertYaml"` } type HttpConf struct { Addr string `yaml:"addr"` diff --git a/prometheus/server/global/global.go b/prometheus/server/global/global.go index a96e4dde..ff2b95f3 100644 --- a/prometheus/server/global/global.go +++ b/prometheus/server/global/global.go @@ -6,6 +6,7 @@ import ( ) var ( - GlobalClient *client.Client - GlobalDB *gorm.DB + GlobalClient *client.Client + GlobalDB *gorm.DB + GlobalPrometheusYml string ) diff --git a/prometheus/server/httphandler/service/check.go b/prometheus/server/httphandler/service/check.go index 72b61ed2..828da47a 100644 --- a/prometheus/server/httphandler/service/check.go +++ b/prometheus/server/httphandler/service/check.go @@ -5,6 +5,7 @@ import ( "fmt" "gitee.com/openeuler/PilotGo-plugins/sdk/utils/command" + "openeuler.org/PilotGo/prometheus-plugin/global" ) // Check if prometheus is installed @@ -13,6 +14,7 @@ func CheckPrometheus() error { _, stdout, stderr, err := command.RunCommand(exec) if len(stdout) > 0 { fmt.Println("prometheus already installed") + global.GlobalPrometheusYml = stdout return nil } return errors.New(stderr + err.Error()) diff --git a/prometheus/server/httphandler/service/yaml/inityaml.go b/prometheus/server/httphandler/service/yaml/inityaml.go new file mode 100644 index 00000000..aabf0f51 --- /dev/null +++ b/prometheus/server/httphandler/service/yaml/inityaml.go @@ -0,0 +1,25 @@ +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 new file mode 100644 index 00000000..851f4431 --- /dev/null +++ b/prometheus/server/httphandler/service/yaml/model.go @@ -0,0 +1,16 @@ +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 new file mode 100644 index 00000000..c386547d --- /dev/null +++ b/prometheus/server/httphandler/service/yaml/yaml.go @@ -0,0 +1,81 @@ +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 06b5c658..bc8bad67 100644 --- a/prometheus/server/main.go +++ b/prometheus/server/main.go @@ -10,6 +10,7 @@ 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" ) @@ -17,12 +18,17 @@ import ( func main() { fmt.Println("hello prometheus") + config.Init() + if err := service.CheckPrometheus(); err != nil { fmt.Printf("Please confirm if prometheus is installed first: %s", err) os.Exit(-1) } - config.Init() + if err := yaml.InitPrometheusYML(config.Config().Prometheus); err != nil { + fmt.Printf("init prometheus yaml failed: %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) diff --git a/sdk/go.mod b/sdk/go.mod index 37bff005..16701a12 100644 --- a/sdk/go.mod +++ b/sdk/go.mod @@ -6,7 +6,6 @@ require ( github.com/gin-gonic/gin v1.9.0 github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/sirupsen/logrus v1.9.3 - gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -36,4 +35,5 @@ require ( golang.org/x/sys v0.6.0 // indirect golang.org/x/text v0.7.0 // indirect google.golang.org/protobuf v1.28.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/sdk/vendor/github.com/klauspost/cpuid/v2/CONTRIBUTING.txt b/sdk/vendor/github.com/klauspost/cpuid/v2/CONTRIBUTING.txt index 452d28ed..2ef4714f 100644 --- a/sdk/vendor/github.com/klauspost/cpuid/v2/CONTRIBUTING.txt +++ b/sdk/vendor/github.com/klauspost/cpuid/v2/CONTRIBUTING.txt @@ -1,35 +1,35 @@ -Developer Certificate of Origin -Version 1.1 - -Copyright (C) 2015- Klaus Post & Contributors. -Email: klauspost@gmail.com - -Everyone is permitted to copy and distribute verbatim copies of this -license document, but changing it is not allowed. - - -Developer's Certificate of Origin 1.1 - -By making a contribution to this project, I certify that: - -(a) The contribution was created in whole or in part by me and I - have the right to submit it under the open source license - indicated in the file; or - -(b) The contribution is based upon previous work that, to the best - of my knowledge, is covered under an appropriate open source - license and I have the right under that license to submit that - work with modifications, whether created in whole or in part - by me, under the same open source license (unless I am - permitted to submit under a different license), as indicated - in the file; or - -(c) The contribution was provided directly to me by some other - person who certified (a), (b) or (c) and I have not modified - it. - -(d) I understand and agree that this project and the contribution - are public and that a record of the contribution (including all - personal information I submit with it, including my sign-off) is - maintained indefinitely and may be redistributed consistent with - this project or the open source license(s) involved. +Developer Certificate of Origin +Version 1.1 + +Copyright (C) 2015- Klaus Post & Contributors. +Email: klauspost@gmail.com + +Everyone is permitted to copy and distribute verbatim copies of this +license document, but changing it is not allowed. + + +Developer's Certificate of Origin 1.1 + +By making a contribution to this project, I certify that: + +(a) The contribution was created in whole or in part by me and I + have the right to submit it under the open source license + indicated in the file; or + +(b) The contribution is based upon previous work that, to the best + of my knowledge, is covered under an appropriate open source + license and I have the right under that license to submit that + work with modifications, whether created in whole or in part + by me, under the same open source license (unless I am + permitted to submit under a different license), as indicated + in the file; or + +(c) The contribution was provided directly to me by some other + person who certified (a), (b) or (c) and I have not modified + it. + +(d) I understand and agree that this project and the contribution + are public and that a record of the contribution (including all + personal information I submit with it, including my sign-off) is + maintained indefinitely and may be redistributed consistent with + this project or the open source license(s) involved. -- Gitee