diff --git a/prometheus/server/httphandler/service/check.go b/prometheus/server/httphandler/service/check.go new file mode 100644 index 0000000000000000000000000000000000000000..72b61ed2bbac487d1fc3a4d06b5fddc40bfc0da3 --- /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 e6e921161d01b3d249e6242c03b6b910b29bab8d..06b5c6584f20bd469b5da496821bb8e8b6466697 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 0000000000000000000000000000000000000000..a5c5f2b1e8af9e0c6bbcd5c06e335a7582d08cc4 --- /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 +}