From 0d90b8a3f156bc33fcd8f6ffa476d99d26730f15 Mon Sep 17 00:00:00 2001 From: zhanghan2021 Date: Thu, 15 Jun 2023 21:15:42 +0800 Subject: [PATCH] add prometheus is installed check --- .../server/httphandler/service/check.go | 19 +++++++ prometheus/server/main.go | 6 +++ sdk/utils/command/command.go | 49 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 prometheus/server/httphandler/service/check.go create mode 100644 sdk/utils/command/command.go diff --git a/prometheus/server/httphandler/service/check.go b/prometheus/server/httphandler/service/check.go new file mode 100644 index 00000000..72b61ed2 --- /dev/null +++ b/prometheus/server/httphandler/service/check.go @@ -0,0 +1,19 @@ +package service + +import ( + "errors" + "fmt" + + "gitee.com/openeuler/PilotGo-plugins/sdk/utils/command" +) + +// Check if prometheus is installed +func CheckPrometheus() error { + exec := "ls /etc/prometheus/prometheus.yml /etc/prometheus/prometheus.yaml" + _, stdout, stderr, err := command.RunCommand(exec) + if len(stdout) > 0 { + fmt.Println("prometheus already installed") + return nil + } + return errors.New(stderr + err.Error()) +} diff --git a/prometheus/server/main.go b/prometheus/server/main.go index e6e92116..06b5c658 100644 --- a/prometheus/server/main.go +++ b/prometheus/server/main.go @@ -9,6 +9,7 @@ import ( "openeuler.org/PilotGo/prometheus-plugin/config" "openeuler.org/PilotGo/prometheus-plugin/db" "openeuler.org/PilotGo/prometheus-plugin/global" + "openeuler.org/PilotGo/prometheus-plugin/httphandler/service" "openeuler.org/PilotGo/prometheus-plugin/plugin" "openeuler.org/PilotGo/prometheus-plugin/router" ) @@ -16,6 +17,11 @@ import ( func main() { fmt.Println("hello prometheus") + if err := service.CheckPrometheus(); err != nil { + fmt.Printf("Please confirm if prometheus is installed first: %s", err) + os.Exit(-1) + } + config.Init() if err := logger.Init(config.Config().Logopts); err != nil { diff --git a/sdk/utils/command/command.go b/sdk/utils/command/command.go new file mode 100644 index 00000000..a5c5f2b1 --- /dev/null +++ b/sdk/utils/command/command.go @@ -0,0 +1,49 @@ +package command + +import ( + "io/ioutil" + "os/exec" +) + +func RunCommand(s string) (int, string, string, error) { + cmd := exec.Command("/bin/bash", "-c", "export LANG=en_US.utf8 ; "+s) + + StdoutPipe, err := cmd.StdoutPipe() + if err != nil { + return 0, "", "", err + } + + StderrPipe, err := cmd.StderrPipe() + if err != nil { + return 0, "", "", err + } + + exitCode := 0 + err = cmd.Start() + if err != nil { + return 0, "", "", err + } + + b1, err := ioutil.ReadAll(StdoutPipe) + if err != nil { + return 0, "", "", err + } + stdout := string(b1) + + b2, err := ioutil.ReadAll(StderrPipe) + if err != nil { + return 0, "", "", err + } + stderr := string(b2) + + err = cmd.Wait() + if err != nil { + e, ok := err.(*exec.ExitError) + if !ok { + return 0, "", "", err + } + exitCode = e.ExitCode() + } + + return exitCode, stdout, stderr, nil +} -- Gitee