Rainbond/node/nodem/healthy/probe/shell_probe.go

65 lines
1.4 KiB
Go
Raw Normal View History

2018-07-27 12:55:18 +08:00
package probe
2018-07-20 18:01:24 +08:00
import (
"bytes"
2018-07-20 18:01:24 +08:00
"context"
"os/exec"
"strings"
"time"
2018-07-23 18:57:34 +08:00
"github.com/goodrain/rainbond/node/nodem/client"
"github.com/goodrain/rainbond/node/nodem/service"
2018-07-20 18:01:24 +08:00
)
type ShellProbe struct {
2018-07-27 12:55:18 +08:00
Name string
Address string
ResultsChan chan *service.HealthStatus
Ctx context.Context
Cancel context.CancelFunc
2018-07-20 18:01:24 +08:00
TimeInterval int
2018-07-27 12:55:18 +08:00
HostNode *client.HostNode
MaxErrorsNum int
2018-07-20 18:01:24 +08:00
}
func (h *ShellProbe) Check() {
go h.ShellCheck()
}
func (h *ShellProbe) Stop() {
h.Cancel()
}
2018-07-20 18:01:24 +08:00
func (h *ShellProbe) ShellCheck() {
timer := time.NewTimer(time.Second * time.Duration(h.TimeInterval))
defer timer.Stop()
for {
HealthMap := GetShellHealth(h.Address)
2018-07-20 18:01:24 +08:00
result := &service.HealthStatus{
2018-07-27 12:55:18 +08:00
Name: h.Name,
2018-07-20 18:01:24 +08:00
Status: HealthMap["status"],
Info: HealthMap["info"],
}
h.ResultsChan <- result
timer.Reset(time.Second * time.Duration(h.TimeInterval))
select {
case <-h.Ctx.Done():
return
case <-timer.C:
}
}
2018-07-20 18:01:24 +08:00
}
func GetShellHealth(address string) map[string]string {
2018-07-20 18:01:24 +08:00
cmd := exec.Command("/bin/bash", "-c", address)
2018-07-20 18:01:24 +08:00
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
errStr := string(stderr.Bytes())
return map[string]string{"status": service.Stat_death, "info": strings.TrimSpace(errStr)}
2018-07-20 18:01:24 +08:00
}
return map[string]string{"status": service.Stat_healthy, "info": "service healthy"}
2018-07-20 18:01:24 +08:00
}