forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
health.go
94 lines (81 loc) · 2.2 KB
/
health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"github.com/goodrain/rainbond/cmd/node/option"
"bytes"
"github.com/Sirupsen/logrus"
"net/http"
"os/exec"
"strings"
"time"
)
//t==0 url:http t==1 cmd:service_status
func StartGoruntine(todo string, t int, stopCheck chan int) {
failedCount := 0
interval := option.Config.CheckIntervalSec
for {
select {
case <-stopCheck:
return
default:
time.Sleep(time.Duration(interval) * time.Second)
if check(todo, t) {
continue
} else {
if failedCount < option.Config.FailTime {
failedCount = failedCount + 1
} else {
//检测失败逻辑,notify
checkFailed(todo, t)
}
}
}
}
}
func checkFailed(url string, ty int) {
}
func check(todo string, ty int) bool {
if ty == 0 {
return checkHttp(todo)
} else if ty == 1 {
return checkCommand(todo)
}
return false
}
func checkCommand(cmd string) bool {
toRun := strings.Split(cmd, " ")
c := exec.Command(toRun[0], toRun[1:]...)
var b bytes.Buffer
c.Stdout = &b
err := c.Run()
if err != nil {
logrus.Warnf("check %s 's status failed,details %s", cmd, err.Error())
return false
}
logrus.Infof("check %s 's result is %s", cmd, b.String())
return true
}
func checkHttp(url string) bool {
resp, err := http.Get(url)
if err != nil {
return false
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
return false
}
return true
}