Rainbond/node/nodem/healthy/shell_probe.go

75 lines
1.7 KiB
Go
Raw Normal View History

2018-07-20 18:01:24 +08:00
package healthy
import (
"context"
"github.com/goodrain/rainbond/node/nodem/service"
"github.com/goodrain/rainbond/util"
"time"
"os/exec"
"bytes"
"strings"
2018-07-23 18:57:34 +08:00
"github.com/goodrain/rainbond/node/nodem/client"
2018-07-20 18:01:24 +08:00
)
type SHELLProbe interface {
Check()
}
type ShellProbe struct {
name string
address string
resultsChan chan *service.HealthStatus
ctx context.Context
cancel context.CancelFunc
TimeInterval int
2018-07-23 18:57:34 +08:00
hostNode *client.HostNode
2018-07-20 18:01:24 +08:00
}
func (h *ShellProbe) ShellCheck() {
util.Exec(h.ctx, func() error {
HealthMap := GetShellHealth(h.address)
result := &service.HealthStatus{
Name: h.name,
Status: HealthMap["status"],
Info: HealthMap["info"],
}
2018-07-23 18:57:34 +08:00
if result.Status != service.Stat_healthy {
v := client.NodeCondition{
Type: client.NodeConditionType(result.Name),
Status: client.ConditionFalse,
2018-07-24 16:09:51 +08:00
LastHeartbeatTime:time.Now(),
LastTransitionTime:time.Now(),
2018-07-23 18:57:34 +08:00
Message: result.Info,
}
2018-07-24 17:10:48 +08:00
h.hostNode.UpdataCondition(v)
}else {
v := client.NodeCondition{
Type:client.NodeConditionType(result.Name),
Status:client.ConditionTrue,
2018-07-24 16:09:51 +08:00
LastHeartbeatTime:time.Now(),
LastTransitionTime:time.Now(),
2018-07-23 18:57:34 +08:00
}
2018-07-24 17:10:48 +08:00
h.hostNode.UpdataCondition(v)
2018-07-23 18:57:34 +08:00
}
2018-07-20 18:01:24 +08:00
h.resultsChan <- result
return nil
}, time.Second*time.Duration(h.TimeInterval))
}
func GetShellHealth(address string) map[string]string {
cmd := exec.Command("/bin/bash", "-c", address)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
2018-07-23 18:57:34 +08:00
if err != nil {
2018-07-20 18:01:24 +08:00
errStr := string(stderr.Bytes())
2018-07-23 18:57:34 +08:00
return map[string]string{"status": service.Stat_death, "info": strings.TrimSpace(errStr)}
2018-07-20 18:01:24 +08:00
}
2018-07-23 18:57:34 +08:00
return map[string]string{"status": service.Stat_healthy, "info": "service healthy"}
2018-07-20 18:01:24 +08:00
}