-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
44 lines (36 loc) · 871 Bytes
/
service.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
package verticacheckd
import (
"fmt"
"os/exec"
"regexp"
)
type CheckService interface {
HostState() (bool, error)
DBHostState(db string) (bool, error)
}
type checkService struct {
address string
cmd string
cmdArgs []string
}
func NewService(addr, cmd string, cmdArgs []string) *checkService {
return &checkService{
address: addr,
cmd: cmd,
cmdArgs: cmdArgs,
}
}
func (c checkService) state(regex string) (bool, error) {
cmdOut, err := exec.Command(c.cmd, c.cmdArgs...).CombinedOutput()
if err != nil {
return false, err
}
exp := regexp.MustCompile(regex)
return exp.Match(cmdOut), nil
}
func (c checkService) HostState() (bool, error) {
return c.state(fmt.Sprintf(`.%s\s+\W\s+UP`, c.address))
}
func (c checkService) DBHostState(db string) (bool, error) {
return c.state(fmt.Sprintf(`\s+%s\s+\W\s+%s\s+\W\s+UP`, db, c.address))
}